blob: 650044583c3cfe8d4694cc96f9716f8f9b60ad82 [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/*
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +02002 * X.509 certificate parsing and verification
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker7c6b2c32013-09-16 13:49:26 +020018 */
19/*
20 * The ITU-T X.509 standard defines a certificate format for PKI.
21 *
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +020022 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
23 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
24 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020025 *
26 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
27 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +020028 *
29 * [SIRO] https://cabforum.org/wp-content/uploads/Chunghwatelecom201503cabforumV4.pdf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020030 */
31
Gilles Peskinedb09ef62020-06-03 01:43:33 +020032#include "common.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020035
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/x509_crt.h"
Janos Follath73c616b2019-12-18 15:07:04 +000037#include "mbedtls/error.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050039#include "mbedtls/platform_util.h"
Rich Evans00ab4702015-02-06 13:43:58 +000040
Rich Evans00ab4702015-02-06 13:43:58 +000041#include <string.h>
42
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020045#endif
46
Andrzej Kurekd4a65532018-10-31 06:18:39 -040047#if defined(MBEDTLS_USE_PSA_CRYPTO)
48#include "psa/crypto.h"
49#include "mbedtls/psa_util.h"
50#endif
51
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020054#else
Simon Butcherd2642582018-10-03 15:11:19 +010055#include <stdio.h>
Rich Evans00ab4702015-02-06 13:43:58 +000056#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#define mbedtls_free free
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020058#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#define mbedtls_snprintf snprintf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020060#endif
61
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000063#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010064#endif
65
Paul Bakkerfa6a6202013-10-28 18:48:30 +010066#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020067#include <windows.h>
68#else
69#include <time.h>
70#endif
71
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072#if defined(MBEDTLS_FS_IO)
Rich Evans00ab4702015-02-06 13:43:58 +000073#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020074#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020075#include <sys/types.h>
76#include <sys/stat.h>
Martino Facchin0ec05ec2021-05-04 11:47:36 +020077#if defined(__MBED__)
78#include <platform/mbed_retarget.h>
79#else
Paul Bakker7c6b2c32013-09-16 13:49:26 +020080#include <dirent.h>
Martino Facchin0ec05ec2021-05-04 11:47:36 +020081#endif /* __MBED__ */
Rich Evans00ab4702015-02-06 13:43:58 +000082#endif /* !_WIN32 || EFIX64 || EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020083#endif
84
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +020085/*
86 * Item in a verification chain: cert and flags for it
87 */
88typedef struct {
89 mbedtls_x509_crt *crt;
90 uint32_t flags;
91} x509_crt_verify_chain_item;
92
93/*
94 * Max size of verification chain: end-entity + intermediates + trusted root
95 */
96#define X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
Paul Bakker34617722014-06-13 17:20:13 +020097
Gilles Peskineffb92da2021-06-02 00:03:26 +020098/* Default profile. Do not remove items unless there are serious security
99 * concerns. */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200100const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
101{
Gilles Peskineae270bf2021-06-02 00:05:29 +0200102 /* Hashes from SHA-256 and above. Note that this selection
103 * should be aligned with ssl_preset_default_hashes in ssl_tls.c. */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200104 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
105 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
106 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
107 0xFFFFFFF, /* Any PK alg */
108#if defined(MBEDTLS_ECP_C)
Gilles Peskineae270bf2021-06-02 00:05:29 +0200109 /* Curves at or above 128-bit security level. Note that this selection
110 * should be aligned with ssl_preset_default_curves in ssl_tls.c. */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200111 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
112 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
113 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
114 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
115 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
116 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
117 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256K1 ),
118#else
119 0,
120#endif
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200121 2048,
122};
123
Gilles Peskineffb92da2021-06-02 00:03:26 +0200124/* Next-generation profile. Currently identical to the default, but may
125 * be tightened at any time. */
126const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200127{
128 /* Hashes from SHA-256 and above. */
129 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
130 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
131 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
132 0xFFFFFFF, /* Any PK alg */
133#if defined(MBEDTLS_ECP_C)
134 /* Curves at or above 128-bit security level. */
135 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
136 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
137 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
138 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
139 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
140 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
141 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256K1 ),
142#else
143 0,
144#endif
145 2048,
146};
Gilles Peskineffb92da2021-06-02 00:03:26 +0200147
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200148/*
149 * NSA Suite B Profile
150 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200151const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
152{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200153 /* Only SHA-256 and 384 */
154 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
155 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ),
156 /* Only ECDSA */
Ron Eldor85e1dcf2018-02-06 15:59:38 +0200157 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECDSA ) |
158 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECKEY ),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200159#if defined(MBEDTLS_ECP_C)
160 /* Only NIST P-256 and P-384 */
161 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
162 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ),
163#else
164 0,
165#endif
166 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200167};
168
169/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200170 * Check md_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200171 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200172 */
173static int x509_profile_check_md_alg( const mbedtls_x509_crt_profile *profile,
174 mbedtls_md_type_t md_alg )
175{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200176 if( md_alg == MBEDTLS_MD_NONE )
177 return( -1 );
178
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200179 if( ( profile->allowed_mds & MBEDTLS_X509_ID_FLAG( md_alg ) ) != 0 )
180 return( 0 );
181
182 return( -1 );
183}
184
185/*
186 * Check pk_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200187 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200188 */
189static int x509_profile_check_pk_alg( const mbedtls_x509_crt_profile *profile,
190 mbedtls_pk_type_t pk_alg )
191{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200192 if( pk_alg == MBEDTLS_PK_NONE )
193 return( -1 );
194
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200195 if( ( profile->allowed_pks & MBEDTLS_X509_ID_FLAG( pk_alg ) ) != 0 )
196 return( 0 );
197
198 return( -1 );
199}
200
201/*
202 * Check key against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200203 * Return 0 if pk is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200204 */
205static int x509_profile_check_key( const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200206 const mbedtls_pk_context *pk )
207{
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200208 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type( pk );
Manuel Pégourié-Gonnard19773ff2017-10-24 10:51:26 +0200209
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200210#if defined(MBEDTLS_RSA_C)
211 if( pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS )
212 {
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200213 if( mbedtls_pk_get_bitlen( pk ) >= profile->rsa_min_bitlen )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200214 return( 0 );
215
216 return( -1 );
217 }
218#endif
219
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200220#if defined(MBEDTLS_ECP_C)
221 if( pk_alg == MBEDTLS_PK_ECDSA ||
222 pk_alg == MBEDTLS_PK_ECKEY ||
223 pk_alg == MBEDTLS_PK_ECKEY_DH )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200224 {
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200225 const mbedtls_ecp_group_id gid = mbedtls_pk_ec( *pk )->grp.id;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200226
Philippe Antoineb5b25432018-05-11 11:06:29 +0200227 if( gid == MBEDTLS_ECP_DP_NONE )
228 return( -1 );
229
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200230 if( ( profile->allowed_curves & MBEDTLS_X509_ID_FLAG( gid ) ) != 0 )
231 return( 0 );
232
233 return( -1 );
234 }
235#endif
236
237 return( -1 );
238}
239
240/*
Hanno Becker1f8527f2018-11-02 09:19:16 +0000241 * Like memcmp, but case-insensitive and always returns -1 if different
242 */
243static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
244{
245 size_t i;
246 unsigned char diff;
247 const unsigned char *n1 = s1, *n2 = s2;
248
249 for( i = 0; i < len; i++ )
250 {
251 diff = n1[i] ^ n2[i];
252
253 if( diff == 0 )
254 continue;
255
256 if( diff == 32 &&
257 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
258 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
259 {
260 continue;
261 }
262
263 return( -1 );
264 }
265
266 return( 0 );
267}
268
269/*
270 * Return 0 if name matches wildcard, -1 otherwise
271 */
272static int x509_check_wildcard( const char *cn, const mbedtls_x509_buf *name )
273{
274 size_t i;
275 size_t cn_idx = 0, cn_len = strlen( cn );
276
277 /* We can't have a match if there is no wildcard to match */
278 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
279 return( -1 );
280
281 for( i = 0; i < cn_len; ++i )
282 {
283 if( cn[i] == '.' )
284 {
285 cn_idx = i;
286 break;
287 }
288 }
289
290 if( cn_idx == 0 )
291 return( -1 );
292
293 if( cn_len - cn_idx == name->len - 1 &&
294 x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
295 {
296 return( 0 );
297 }
298
299 return( -1 );
300}
301
302/*
303 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
304 * variations (but not all).
305 *
306 * Return 0 if equal, -1 otherwise.
307 */
308static int x509_string_cmp( const mbedtls_x509_buf *a, const mbedtls_x509_buf *b )
309{
310 if( a->tag == b->tag &&
311 a->len == b->len &&
312 memcmp( a->p, b->p, b->len ) == 0 )
313 {
314 return( 0 );
315 }
316
317 if( ( a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
318 ( b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
319 a->len == b->len &&
320 x509_memcasecmp( a->p, b->p, b->len ) == 0 )
321 {
322 return( 0 );
323 }
324
325 return( -1 );
326}
327
328/*
329 * Compare two X.509 Names (aka rdnSequence).
330 *
331 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
332 * we sometimes return unequal when the full algorithm would return equal,
333 * but never the other way. (In particular, we don't do Unicode normalisation
334 * or space folding.)
335 *
336 * Return 0 if equal, -1 otherwise.
337 */
338static int x509_name_cmp( const mbedtls_x509_name *a, const mbedtls_x509_name *b )
339{
340 /* Avoid recursion, it might not be optimised by the compiler */
341 while( a != NULL || b != NULL )
342 {
343 if( a == NULL || b == NULL )
344 return( -1 );
345
346 /* type */
347 if( a->oid.tag != b->oid.tag ||
348 a->oid.len != b->oid.len ||
349 memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 )
350 {
351 return( -1 );
352 }
353
354 /* value */
355 if( x509_string_cmp( &a->val, &b->val ) != 0 )
356 return( -1 );
357
358 /* structure of the list of sets */
359 if( a->next_merged != b->next_merged )
360 return( -1 );
361
362 a = a->next;
363 b = b->next;
364 }
365
366 /* a == NULL == b */
367 return( 0 );
368}
369
370/*
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200371 * Reset (init or clear) a verify_chain
372 */
373static void x509_crt_verify_chain_reset(
374 mbedtls_x509_crt_verify_chain *ver_chain )
375{
376 size_t i;
377
378 for( i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++ )
379 {
380 ver_chain->items[i].crt = NULL;
Hanno Beckera9375b32019-01-10 09:19:26 +0000381 ver_chain->items[i].flags = (uint32_t) -1;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200382 }
383
384 ver_chain->len = 0;
Hanno Beckerf53893b2019-03-28 13:45:55 +0000385
386#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
387 ver_chain->trust_ca_cb_result = NULL;
388#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200389}
390
391/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200392 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
393 */
394static int x509_get_version( unsigned char **p,
395 const unsigned char *end,
396 int *ver )
397{
Janos Follath865b3eb2019-12-16 11:46:15 +0000398 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200399 size_t len;
400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
402 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200403 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200405 {
406 *ver = 0;
407 return( 0 );
408 }
409
Chris Jones9f7a6932021-04-14 12:12:09 +0100410 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200411 }
412
413 end = *p + len;
414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100416 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_VERSION, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200417
418 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100419 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_VERSION,
420 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200421
422 return( 0 );
423}
424
425/*
426 * Validity ::= SEQUENCE {
427 * notBefore Time,
428 * notAfter Time }
429 */
430static int x509_get_dates( unsigned char **p,
431 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 mbedtls_x509_time *from,
433 mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200434{
Janos Follath865b3eb2019-12-16 11:46:15 +0000435 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200436 size_t len;
437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
439 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100440 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200441
442 end = *p + len;
443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444 if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200445 return( ret );
446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447 if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200448 return( ret );
449
450 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100451 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE,
452 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200453
454 return( 0 );
455}
456
457/*
458 * X.509 v2/v3 unique identifier (not parsed)
459 */
460static int x509_get_uid( unsigned char **p,
461 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 mbedtls_x509_buf *uid, int n )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200463{
Janos Follath865b3eb2019-12-16 11:46:15 +0000464 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200465
466 if( *p == end )
467 return( 0 );
468
469 uid->tag = **p;
470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471 if( ( ret = mbedtls_asn1_get_tag( p, end, &uid->len,
472 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200473 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200475 return( 0 );
476
Chris Jones9f7a6932021-04-14 12:12:09 +0100477 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200478 }
479
480 uid->p = *p;
481 *p += uid->len;
482
483 return( 0 );
484}
485
486static int x509_get_basic_constraints( unsigned char **p,
487 const unsigned char *end,
488 int *ca_istrue,
489 int *max_pathlen )
490{
Janos Follath865b3eb2019-12-16 11:46:15 +0000491 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200492 size_t len;
493
494 /*
495 * BasicConstraints ::= SEQUENCE {
496 * cA BOOLEAN DEFAULT FALSE,
497 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
498 */
499 *ca_istrue = 0; /* DEFAULT FALSE */
500 *max_pathlen = 0; /* endless */
501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
503 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100504 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200505
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_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200510 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
512 ret = mbedtls_asn1_get_int( p, end, ca_istrue );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200513
514 if( ret != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100515 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200516
517 if( *ca_istrue != 0 )
518 *ca_istrue = 1;
519 }
520
521 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200522 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100525 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200526
527 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100528 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
529 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200530
Andrzej Kurek16050742020-04-14 09:49:52 -0400531 /* Do not accept max_pathlen equal to INT_MAX to avoid a signed integer
532 * overflow, which is an undefined behavior. */
533 if( *max_pathlen == INT_MAX )
Chris Jones9f7a6932021-04-14 12:12:09 +0100534 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
535 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Andrzej Kurek16050742020-04-14 09:49:52 -0400536
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200537 (*max_pathlen)++;
538
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200539 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200540}
541
542static int x509_get_ns_cert_type( unsigned char **p,
543 const unsigned char *end,
544 unsigned char *ns_cert_type)
545{
Janos Follath865b3eb2019-12-16 11:46:15 +0000546 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100550 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200551
552 if( bs.len != 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100553 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
554 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200555
556 /* Get actual bitstring */
557 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200558 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200559}
560
561static int x509_get_key_usage( unsigned char **p,
562 const unsigned char *end,
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +0100563 unsigned int *key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200564{
Janos Follath865b3eb2019-12-16 11:46:15 +0000565 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200566 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100570 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200571
572 if( bs.len < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100573 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
574 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200575
576 /* Get actual bitstring */
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200577 *key_usage = 0;
578 for( i = 0; i < bs.len && i < sizeof( unsigned int ); i++ )
579 {
580 *key_usage |= (unsigned int) bs.p[i] << (8*i);
581 }
582
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200583 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200584}
585
586/*
587 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
588 *
589 * KeyPurposeId ::= OBJECT IDENTIFIER
590 */
591static int x509_get_ext_key_usage( unsigned char **p,
592 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200594{
Janos Follath865b3eb2019-12-16 11:46:15 +0000595 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 if( ( ret = mbedtls_asn1_get_sequence_of( p, end, ext_key_usage, MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100598 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200599
600 /* Sequence length must be >= 1 */
601 if( ext_key_usage->buf.p == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100602 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
603 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200604
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200605 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200606}
607
608/*
609 * SubjectAltName ::= GeneralNames
610 *
611 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
612 *
613 * GeneralName ::= CHOICE {
614 * otherName [0] OtherName,
615 * rfc822Name [1] IA5String,
616 * dNSName [2] IA5String,
617 * x400Address [3] ORAddress,
618 * directoryName [4] Name,
619 * ediPartyName [5] EDIPartyName,
620 * uniformResourceIdentifier [6] IA5String,
621 * iPAddress [7] OCTET STRING,
622 * registeredID [8] OBJECT IDENTIFIER }
623 *
624 * OtherName ::= SEQUENCE {
625 * type-id OBJECT IDENTIFIER,
626 * value [0] EXPLICIT ANY DEFINED BY type-id }
627 *
628 * EDIPartyName ::= SEQUENCE {
629 * nameAssigner [0] DirectoryString OPTIONAL,
630 * partyName [1] DirectoryString }
631 *
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300632 * NOTE: we list all types, but only use dNSName and otherName
633 * of type HwModuleName, as defined in RFC 4108, at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200634 */
635static int x509_get_subject_alt_name( unsigned char **p,
636 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 mbedtls_x509_sequence *subject_alt_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200638{
Janos Follath865b3eb2019-12-16 11:46:15 +0000639 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200640 size_t len, tag_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 mbedtls_asn1_buf *buf;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200642 unsigned char tag;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 mbedtls_asn1_sequence *cur = subject_alt_name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200644
645 /* Get main sequence tag */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
647 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100648 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200649
650 if( *p + len != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100651 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
652 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200653
654 while( *p < end )
655 {
Ron Eldordbbd9662019-05-15 15:46:03 +0300656 mbedtls_x509_subject_alternative_name dummy_san_buf;
657 memset( &dummy_san_buf, 0, sizeof( dummy_san_buf ) );
658
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200659 tag = **p;
660 (*p)++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 if( ( ret = mbedtls_asn1_get_len( p, end, &tag_len ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100662 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200663
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100664 if( ( tag & MBEDTLS_ASN1_TAG_CLASS_MASK ) !=
665 MBEDTLS_ASN1_CONTEXT_SPECIFIC )
666 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100667 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
668 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100669 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200670
Ron Eldordbbd9662019-05-15 15:46:03 +0300671 /*
irwir74aee1c2019-09-21 18:21:48 +0300672 * Check that the SAN is structured correctly.
Ron Eldordbbd9662019-05-15 15:46:03 +0300673 */
674 ret = mbedtls_x509_parse_subject_alt_name( &(cur->buf), &dummy_san_buf );
675 /*
676 * In case the extension is malformed, return an error,
677 * and clear the allocated sequences.
678 */
679 if( ret != 0 && ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
680 {
681 mbedtls_x509_sequence *seq_cur = subject_alt_name->next;
682 mbedtls_x509_sequence *seq_prv;
683 while( seq_cur != NULL )
684 {
685 seq_prv = seq_cur;
686 seq_cur = seq_cur->next;
687 mbedtls_platform_zeroize( seq_prv,
688 sizeof( mbedtls_x509_sequence ) );
689 mbedtls_free( seq_prv );
690 }
Ron Eldor5aebeeb2019-05-22 16:41:21 +0300691 subject_alt_name->next = NULL;
Ron Eldordbbd9662019-05-15 15:46:03 +0300692 return( ret );
693 }
694
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200695 /* Allocate and assign next pointer */
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200696 if( cur->buf.p != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200697 {
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100698 if( cur->next != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100700
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200701 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200702
703 if( cur->next == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100704 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
705 MBEDTLS_ERR_ASN1_ALLOC_FAILED ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200706
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200707 cur = cur->next;
708 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200709
710 buf = &(cur->buf);
711 buf->tag = tag;
712 buf->p = *p;
713 buf->len = tag_len;
714 *p += buf->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200715 }
716
717 /* Set final sequence entry's next pointer to NULL */
718 cur->next = NULL;
719
720 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100721 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
722 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200723
724 return( 0 );
725}
726
727/*
Ron Eldor74d9acc2019-03-21 14:00:03 +0200728 * id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 }
729 *
730 * anyPolicy OBJECT IDENTIFIER ::= { id-ce-certificatePolicies 0 }
731 *
732 * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
733 *
734 * PolicyInformation ::= SEQUENCE {
735 * policyIdentifier CertPolicyId,
736 * policyQualifiers SEQUENCE SIZE (1..MAX) OF
737 * PolicyQualifierInfo OPTIONAL }
738 *
739 * CertPolicyId ::= OBJECT IDENTIFIER
740 *
741 * PolicyQualifierInfo ::= SEQUENCE {
742 * policyQualifierId PolicyQualifierId,
743 * qualifier ANY DEFINED BY policyQualifierId }
744 *
745 * -- policyQualifierIds for Internet policy qualifiers
746 *
747 * id-qt OBJECT IDENTIFIER ::= { id-pkix 2 }
748 * id-qt-cps OBJECT IDENTIFIER ::= { id-qt 1 }
749 * id-qt-unotice OBJECT IDENTIFIER ::= { id-qt 2 }
750 *
751 * PolicyQualifierId ::= OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice )
752 *
753 * Qualifier ::= CHOICE {
754 * cPSuri CPSuri,
755 * userNotice UserNotice }
756 *
757 * CPSuri ::= IA5String
758 *
759 * UserNotice ::= SEQUENCE {
760 * noticeRef NoticeReference OPTIONAL,
761 * explicitText DisplayText OPTIONAL }
762 *
763 * NoticeReference ::= SEQUENCE {
764 * organization DisplayText,
765 * noticeNumbers SEQUENCE OF INTEGER }
766 *
767 * DisplayText ::= CHOICE {
768 * ia5String IA5String (SIZE (1..200)),
769 * visibleString VisibleString (SIZE (1..200)),
770 * bmpString BMPString (SIZE (1..200)),
771 * utf8String UTF8String (SIZE (1..200)) }
772 *
773 * NOTE: we only parse and use anyPolicy without qualifiers at this point
774 * as defined in RFC 5280.
775 */
776static int x509_get_certificate_policies( unsigned char **p,
777 const unsigned char *end,
778 mbedtls_x509_sequence *certificate_policies )
779{
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300780 int ret, parse_ret = 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200781 size_t len;
782 mbedtls_asn1_buf *buf;
783 mbedtls_asn1_sequence *cur = certificate_policies;
784
785 /* Get main sequence tag */
786 ret = mbedtls_asn1_get_tag( p, end, &len,
787 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
788 if( ret != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100789 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200790
791 if( *p + len != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100792 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
793 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200794
795 /*
796 * Cannot be an empty sequence.
797 */
798 if( len == 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100799 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
800 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200801
802 while( *p < end )
803 {
804 mbedtls_x509_buf policy_oid;
805 const unsigned char *policy_end;
806
807 /*
808 * Get the policy sequence
809 */
810 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
811 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100812 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200813
814 policy_end = *p + len;
815
Ron Eldor08063792019-05-13 16:38:39 +0300816 if( ( ret = mbedtls_asn1_get_tag( p, policy_end, &len,
Ron Eldor74d9acc2019-03-21 14:00:03 +0200817 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100818 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200819
820 policy_oid.tag = MBEDTLS_ASN1_OID;
821 policy_oid.len = len;
822 policy_oid.p = *p;
823
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300824 /*
825 * Only AnyPolicy is currently supported when enforcing policy.
826 */
827 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_POLICY, &policy_oid ) != 0 )
828 {
829 /*
830 * Set the parsing return code but continue parsing, in case this
TRodziewicz3ecb92e2021-05-11 18:22:05 +0200831 * extension is critical.
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300832 */
833 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
834 }
835
Ron Eldor74d9acc2019-03-21 14:00:03 +0200836 /* Allocate and assign next pointer */
837 if( cur->buf.p != NULL )
838 {
839 if( cur->next != NULL )
840 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
841
842 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
843
844 if( cur->next == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100845 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
846 MBEDTLS_ERR_ASN1_ALLOC_FAILED ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200847
848 cur = cur->next;
849 }
850
851 buf = &( cur->buf );
852 buf->tag = policy_oid.tag;
853 buf->p = policy_oid.p;
854 buf->len = policy_oid.len;
Ron Eldor08063792019-05-13 16:38:39 +0300855
856 *p += len;
857
858 /*
859 * If there is an optional qualifier, then *p < policy_end
860 * Check the Qualifier len to verify it doesn't exceed policy_end.
861 */
862 if( *p < policy_end )
863 {
864 if( ( ret = mbedtls_asn1_get_tag( p, policy_end, &len,
865 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100866 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor08063792019-05-13 16:38:39 +0300867 /*
868 * Skip the optional policy qualifiers.
869 */
870 *p += len;
871 }
872
873 if( *p != policy_end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100874 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
875 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200876 }
877
878 /* Set final sequence entry's next pointer to NULL */
879 cur->next = NULL;
880
881 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100882 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
883 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200884
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300885 return( parse_ret );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200886}
887
888/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200889 * X.509 v3 extensions
890 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200891 */
892static int x509_get_crt_ext( unsigned char **p,
893 const unsigned char *end,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200894 mbedtls_x509_crt *crt,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200895 mbedtls_x509_crt_ext_cb_t cb,
896 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200897{
Janos Follath865b3eb2019-12-16 11:46:15 +0000898 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200899 size_t len;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200900 unsigned char *end_ext_data, *start_ext_octet, *end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200901
Hanno Becker12f62fb2019-02-12 17:22:36 +0000902 if( *p == end )
903 return( 0 );
904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200905 if( ( ret = mbedtls_x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200906 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200907
Hanno Becker12f62fb2019-02-12 17:22:36 +0000908 end = crt->v3_ext.p + crt->v3_ext.len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200909 while( *p < end )
910 {
911 /*
912 * Extension ::= SEQUENCE {
913 * extnID OBJECT IDENTIFIER,
914 * critical BOOLEAN DEFAULT FALSE,
915 * extnValue OCTET STRING }
916 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917 mbedtls_x509_buf extn_oid = {0, 0, NULL};
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200918 int is_critical = 0; /* DEFAULT FALSE */
919 int ext_type = 0;
920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
922 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100923 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200924
925 end_ext_data = *p + len;
926
927 /* Get extension ID */
k-stachowiakdcae78a2018-06-28 16:32:54 +0200928 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &extn_oid.len,
k-stachowiak463928a2018-07-24 12:50:59 +0200929 MBEDTLS_ASN1_OID ) ) != 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
k-stachowiak463928a2018-07-24 12:50:59 +0200932 extn_oid.tag = MBEDTLS_ASN1_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200933 extn_oid.p = *p;
934 *p += extn_oid.len;
935
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200936 /* Get optional critical */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200937 if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
938 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
Chris Jones9f7a6932021-04-14 12:12:09 +0100939 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200940
941 /* Data should be octet string type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
943 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100944 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200945
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200946 start_ext_octet = *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200947 end_ext_octet = *p + len;
948
949 if( end_ext_octet != end_ext_data )
Chris Jones9f7a6932021-04-14 12:12:09 +0100950 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
951 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200952
953 /*
954 * Detect supported extensions
955 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200957
958 if( ret != 0 )
959 {
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200960 /* Give the callback (if any) a chance to handle the extension */
Nicola Di Lieto2c3a9172020-05-28 17:20:42 +0200961 if( cb != NULL )
962 {
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200963 ret = cb( p_ctx, crt, &extn_oid, is_critical, *p, end_ext_octet );
Nicola Di Lieto565b52b2020-05-29 22:46:56 +0200964 if( ret != 0 && is_critical )
965 return( ret );
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200966 *p = end_ext_octet;
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200967 continue;
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200968 }
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200969
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200970 /* No parser found, skip extension */
971 *p = end_ext_octet;
972
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200973 if( is_critical )
974 {
975 /* Data is marked as critical: fail */
Chris Jones9f7a6932021-04-14 12:12:09 +0100976 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
977 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200978 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200979 continue;
980 }
981
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100982 /* Forbid repeated extensions */
983 if( ( crt->ext_types & ext_type ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200984 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100985
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200986 crt->ext_types |= ext_type;
987
988 switch( ext_type )
989 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100990 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200991 /* Parse basic constraints */
992 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
993 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200994 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200995 break;
996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200998 /* Parse key usage */
999 if( ( ret = x509_get_key_usage( p, end_ext_octet,
1000 &crt->key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001001 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001002 break;
1003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001005 /* Parse extended key usage */
1006 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
1007 &crt->ext_key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001008 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001009 break;
1010
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001011 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001012 /* Parse subject alt name */
1013 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
1014 &crt->subject_alt_names ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001015 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001016 break;
1017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001019 /* Parse netscape certificate type */
1020 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
1021 &crt->ns_cert_type ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001022 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001023 break;
1024
Ron Eldor74d9acc2019-03-21 14:00:03 +02001025 case MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES:
1026 /* Parse certificate policies type */
1027 if( ( ret = x509_get_certificate_policies( p, end_ext_octet,
1028 &crt->certificate_policies ) ) != 0 )
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001029 {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +02001030 /* Give the callback (if any) a chance to handle the extension
1031 * if it contains unsupported policies */
1032 if( ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE && cb != NULL &&
1033 cb( p_ctx, crt, &extn_oid, is_critical,
1034 start_ext_octet, end_ext_octet ) == 0 )
1035 break;
1036
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001037 if( is_critical )
1038 return( ret );
1039 else
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001040 /*
Ron Eldora2913912019-05-16 16:17:38 +03001041 * If MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE is returned, then we
1042 * cannot interpret or enforce the policy. However, it is up to
1043 * the user to choose how to enforce the policies,
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001044 * unless the extension is critical.
1045 */
1046 if( ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1047 return( ret );
1048 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001049 break;
1050
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001051 default:
Ron Eldordf48efa2019-04-08 13:28:24 +03001052 /*
1053 * If this is a non-critical extension, which the oid layer
1054 * supports, but there isn't an x509 parser for it,
1055 * skip the extension.
1056 */
Ron Eldordf48efa2019-04-08 13:28:24 +03001057 if( is_critical )
1058 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1059 else
Ron Eldordf48efa2019-04-08 13:28:24 +03001060 *p = end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001061 }
1062 }
1063
1064 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +01001065 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1066 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001067
1068 return( 0 );
1069}
1070
1071/*
1072 * Parse and fill a single X.509 certificate in DER format
1073 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001074static int x509_crt_parse_der_core( mbedtls_x509_crt *crt,
1075 const unsigned char *buf,
1076 size_t buflen,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001077 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001078 mbedtls_x509_crt_ext_cb_t cb,
1079 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001080{
Janos Follath865b3eb2019-12-16 11:46:15 +00001081 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001082 size_t len;
1083 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001084 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086 memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) );
1087 memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) );
1088 memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001089
1090 /*
1091 * Check for valid input
1092 */
1093 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001094 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001095
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001096 /* Use the original buffer until we figure out actual length. */
Janos Follathcc0e49d2016-02-17 14:34:12 +00001097 p = (unsigned char*) buf;
1098 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001099 end = p + len;
1100
1101 /*
1102 * Certificate ::= SEQUENCE {
1103 * tbsCertificate TBSCertificate,
1104 * signatureAlgorithm AlgorithmIdentifier,
1105 * signatureValue BIT STRING }
1106 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001107 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1108 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001109 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001110 mbedtls_x509_crt_free( crt );
1111 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001112 }
1113
Janos Follathcc0e49d2016-02-17 14:34:12 +00001114 end = crt_end = p + len;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001115 crt->raw.len = crt_end - buf;
1116 if( make_copy != 0 )
1117 {
1118 /* Create and populate a new buffer for the raw field. */
1119 crt->raw.p = p = mbedtls_calloc( 1, crt->raw.len );
1120 if( crt->raw.p == NULL )
1121 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
1122
1123 memcpy( crt->raw.p, buf, crt->raw.len );
1124 crt->own_buffer = 1;
1125
1126 p += crt->raw.len - len;
1127 end = crt_end = p + len;
1128 }
1129 else
1130 {
1131 crt->raw.p = (unsigned char*) buf;
1132 crt->own_buffer = 0;
1133 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001134
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001135 /*
1136 * TBSCertificate ::= SEQUENCE {
1137 */
1138 crt->tbs.p = p;
1139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1141 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001142 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001144 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001145 }
1146
1147 end = p + len;
1148 crt->tbs.len = end - crt->tbs.p;
1149
1150 /*
1151 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1152 *
1153 * CertificateSerialNumber ::= INTEGER
1154 *
1155 * signature AlgorithmIdentifier
1156 */
1157 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158 ( ret = mbedtls_x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1159 ( ret = mbedtls_x509_get_alg( &p, end, &crt->sig_oid,
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001160 &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001161 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001163 return( ret );
1164 }
1165
Andres AG7ca4a032017-03-09 16:16:11 +00001166 if( crt->version < 0 || crt->version > 2 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168 mbedtls_x509_crt_free( crt );
1169 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001170 }
1171
Andres AG7ca4a032017-03-09 16:16:11 +00001172 crt->version++;
1173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001174 if( ( ret = mbedtls_x509_get_sig_alg( &crt->sig_oid, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02001175 &crt->sig_md, &crt->sig_pk,
1176 &crt->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001178 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001179 return( ret );
1180 }
1181
1182 /*
1183 * issuer Name
1184 */
1185 crt->issuer_raw.p = p;
1186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1188 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001189 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001191 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001192 }
1193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001194 if( ( ret = mbedtls_x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001195 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001196 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001197 return( ret );
1198 }
1199
1200 crt->issuer_raw.len = p - crt->issuer_raw.p;
1201
1202 /*
1203 * Validity ::= SEQUENCE {
1204 * notBefore Time,
1205 * notAfter Time }
1206 *
1207 */
1208 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1209 &crt->valid_to ) ) != 0 )
1210 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001211 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001212 return( ret );
1213 }
1214
1215 /*
1216 * subject Name
1217 */
1218 crt->subject_raw.p = p;
1219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001220 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1221 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001222 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001223 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001224 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001225 }
1226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001227 if( len && ( ret = mbedtls_x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001228 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001229 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001230 return( ret );
1231 }
1232
1233 crt->subject_raw.len = p - crt->subject_raw.p;
1234
1235 /*
1236 * SubjectPublicKeyInfo
1237 */
Hanno Becker494dd7a2019-02-06 16:13:41 +00001238 crt->pk_raw.p = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001239 if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001240 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001241 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001242 return( ret );
1243 }
Hanno Becker494dd7a2019-02-06 16:13:41 +00001244 crt->pk_raw.len = p - crt->pk_raw.p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001245
1246 /*
1247 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1248 * -- If present, version shall be v2 or v3
1249 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1250 * -- If present, version shall be v2 or v3
1251 * extensions [3] EXPLICIT Extensions OPTIONAL
1252 * -- If present, version shall be v3
1253 */
1254 if( crt->version == 2 || crt->version == 3 )
1255 {
1256 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1257 if( ret != 0 )
1258 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001260 return( ret );
1261 }
1262 }
1263
1264 if( crt->version == 2 || crt->version == 3 )
1265 {
1266 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1267 if( ret != 0 )
1268 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001269 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001270 return( ret );
1271 }
1272 }
1273
1274 if( crt->version == 3 )
Manuel Pégourié-Gonnarda252af72015-03-27 16:15:55 +01001275 {
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001276 ret = x509_get_crt_ext( &p, end, crt, cb, p_ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001277 if( ret != 0 )
1278 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001279 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001280 return( ret );
1281 }
1282 }
1283
1284 if( p != end )
1285 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001286 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001287 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT,
1288 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001289 }
1290
1291 end = crt_end;
1292
1293 /*
1294 * }
1295 * -- end of TBSCertificate
1296 *
1297 * signatureAlgorithm AlgorithmIdentifier,
1298 * signatureValue BIT STRING
1299 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300 if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001301 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001302 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001303 return( ret );
1304 }
1305
Manuel Pégourié-Gonnard1022fed2015-03-27 16:30:47 +01001306 if( crt->sig_oid.len != sig_oid2.len ||
1307 memcmp( crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len ) != 0 ||
Paul Elliottca17ebf2020-11-24 17:30:18 +00001308 sig_params1.tag != sig_params2.tag ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001309 sig_params1.len != sig_params2.len ||
Manuel Pégourié-Gonnard159c5242015-04-30 11:15:22 +02001310 ( sig_params1.len != 0 &&
1311 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313 mbedtls_x509_crt_free( crt );
1314 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001315 }
1316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317 if( ( ret = mbedtls_x509_get_sig( &p, end, &crt->sig ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001319 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001320 return( ret );
1321 }
1322
1323 if( p != end )
1324 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001326 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT,
1327 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001328 }
1329
1330 return( 0 );
1331}
1332
1333/*
1334 * Parse one X.509 certificate in DER format from a buffer and add them to a
1335 * chained list
1336 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001337static int mbedtls_x509_crt_parse_der_internal( mbedtls_x509_crt *chain,
1338 const unsigned char *buf,
1339 size_t buflen,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001340 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001341 mbedtls_x509_crt_ext_cb_t cb,
1342 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001343{
Janos Follath865b3eb2019-12-16 11:46:15 +00001344 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001346
1347 /*
1348 * Check for valid input
1349 */
1350 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001352
1353 while( crt->version != 0 && crt->next != NULL )
1354 {
1355 prev = crt;
1356 crt = crt->next;
1357 }
1358
1359 /*
1360 * Add new certificate on the end of the chain if needed.
1361 */
Paul Bakker66d5d072014-06-17 16:39:18 +02001362 if( crt->version != 0 && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001363 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001364 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001365
1366 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001367 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001368
1369 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001370 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001371 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001372 }
1373
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001374 ret = x509_crt_parse_der_core( crt, buf, buflen, make_copy, cb, p_ctx );
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001375 if( ret != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001376 {
1377 if( prev )
1378 prev->next = NULL;
1379
1380 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001381 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001382
1383 return( ret );
1384 }
1385
1386 return( 0 );
1387}
1388
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001389int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
1390 const unsigned char *buf,
1391 size_t buflen )
1392{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001393 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 0, NULL, NULL ) );
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001394}
1395
Nicola Di Lietofde98f72020-05-28 08:34:33 +02001396int mbedtls_x509_crt_parse_der_with_ext_cb( mbedtls_x509_crt *chain,
1397 const unsigned char *buf,
1398 size_t buflen,
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +02001399 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001400 mbedtls_x509_crt_ext_cb_t cb,
1401 void *p_ctx )
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001402{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001403 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, make_copy, cb, p_ctx ) );
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001404}
1405
1406int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
1407 const unsigned char *buf,
1408 size_t buflen )
1409{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001410 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 1, NULL, NULL ) );
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001411}
1412
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001413/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001414 * Parse one or more PEM certificates from a buffer and add them to the chained
1415 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001416 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001417int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain,
1418 const unsigned char *buf,
1419 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001420{
Janos Follath98e28a72016-05-31 14:03:54 +01001421#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001422 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001424#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001425
1426 /*
1427 * Check for valid input
1428 */
1429 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001431
1432 /*
1433 * Determine buffer content. Buffer contains either one DER certificate or
1434 * one or more PEM certificates.
1435 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001437 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001438 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1439 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001441 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001443 if( buf_format == MBEDTLS_X509_FORMAT_DER )
1444 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Janos Follath98e28a72016-05-31 14:03:54 +01001445#else
1446 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
1447#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449#if defined(MBEDTLS_PEM_PARSE_C)
1450 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001451 {
Janos Follath865b3eb2019-12-16 11:46:15 +00001452 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001453 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001454
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001455 /* 1 rather than 0 since the terminating NULL byte is counted in */
1456 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001457 {
1458 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001459 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001460
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001461 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001462 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001463 "-----BEGIN CERTIFICATE-----",
1464 "-----END CERTIFICATE-----",
1465 buf, NULL, 0, &use_len );
1466
1467 if( ret == 0 )
1468 {
1469 /*
1470 * Was PEM encoded
1471 */
1472 buflen -= use_len;
1473 buf += use_len;
1474 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001475 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001476 {
1477 return( ret );
1478 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001479 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001480 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001482
1483 /*
1484 * PEM header and footer were found
1485 */
1486 buflen -= use_len;
1487 buf += use_len;
1488
1489 if( first_error == 0 )
1490 first_error = ret;
1491
Paul Bakker5a5fa922014-09-26 14:53:04 +02001492 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001493 continue;
1494 }
1495 else
1496 break;
1497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001498 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001500 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001501
1502 if( ret != 0 )
1503 {
1504 /*
1505 * Quit parsing on a memory error
1506 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001507 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001508 return( ret );
1509
1510 if( first_error == 0 )
1511 first_error = ret;
1512
1513 total_failed++;
1514 continue;
1515 }
1516
1517 success = 1;
1518 }
1519 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001520
1521 if( success )
1522 return( total_failed );
1523 else if( first_error )
1524 return( first_error );
1525 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001526 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Janos Follath98e28a72016-05-31 14:03:54 +01001527#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001528}
1529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001531/*
1532 * Load one or more certificates and add them to the chained list
1533 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001534int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001535{
Janos Follath865b3eb2019-12-16 11:46:15 +00001536 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001537 size_t n;
1538 unsigned char *buf;
1539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001540 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001541 return( ret );
1542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001543 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001544
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001545 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001546 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001547
1548 return( ret );
1549}
1550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001552{
1553 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001554#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001555 int w_ret;
1556 WCHAR szDir[MAX_PATH];
1557 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001558 char *p;
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001559 size_t len = strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001560
Paul Bakker9af723c2014-05-01 13:03:14 +02001561 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001562 HANDLE hFind;
1563
1564 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001565 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001566
Paul Bakker9af723c2014-05-01 13:03:14 +02001567 memset( szDir, 0, sizeof(szDir) );
1568 memset( filename, 0, MAX_PATH );
1569 memcpy( filename, path, len );
1570 filename[len++] = '\\';
1571 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001572 filename[len++] = '*';
1573
Simon B3c6b18d2016-11-03 01:11:37 +00001574 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001575 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001576 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001577 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001578
1579 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001580 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001581 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001582
1583 len = MAX_PATH - len;
1584 do
1585 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001586 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001587
1588 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1589 continue;
1590
Paul Bakker9af723c2014-05-01 13:03:14 +02001591 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001592 lstrlenW( file_data.cFileName ),
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001593 p, (int) len - 1,
Paul Bakker9af723c2014-05-01 13:03:14 +02001594 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001595 if( w_ret == 0 )
Ron Eldor36d90422017-01-09 15:09:16 +02001596 {
1597 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1598 goto cleanup;
1599 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001602 if( w_ret < 0 )
1603 ret++;
1604 else
1605 ret += w_ret;
1606 }
1607 while( FindNextFileW( hFind, &file_data ) != 0 );
1608
Paul Bakker66d5d072014-06-17 16:39:18 +02001609 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001611
Ron Eldor36d90422017-01-09 15:09:16 +02001612cleanup:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001613 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001614#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001615 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001616 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001617 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001618 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001619 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001620 DIR *dir = opendir( path );
1621
Paul Bakker66d5d072014-06-17 16:39:18 +02001622 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001623 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001624
Ron Eldor63140682017-01-09 19:27:59 +02001625#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001626 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001627 {
1628 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001629 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001630 }
Ron Eldor63140682017-01-09 19:27:59 +02001631#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001632
Paul Elliottfb91a482021-03-05 14:17:51 +00001633 memset( &sb, 0, sizeof( sb ) );
1634
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001635 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001636 {
Andres AGf9113192016-09-02 14:06:04 +01001637 snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name,
1638 "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001639
Andres AGf9113192016-09-02 14:06:04 +01001640 if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001641 {
Andres AGf9113192016-09-02 14:06:04 +01001642 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1643 goto cleanup;
1644 }
1645 else if( stat( entry_name, &sb ) == -1 )
1646 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001647 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001648 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001649 }
1650
1651 if( !S_ISREG( sb.st_mode ) )
1652 continue;
1653
1654 // Ignore parse errors
1655 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001656 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001657 if( t_ret < 0 )
1658 ret++;
1659 else
1660 ret += t_ret;
1661 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001662
1663cleanup:
Andres AGf9113192016-09-02 14:06:04 +01001664 closedir( dir );
1665
Ron Eldor63140682017-01-09 19:27:59 +02001666#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001667 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001668 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Ron Eldor63140682017-01-09 19:27:59 +02001669#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001670
Paul Bakkerbe089b02013-10-14 15:51:50 +02001671#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001672
1673 return( ret );
1674}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001675#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001676
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001677/*
1678 * OtherName ::= SEQUENCE {
1679 * type-id OBJECT IDENTIFIER,
1680 * value [0] EXPLICIT ANY DEFINED BY type-id }
1681 *
1682 * HardwareModuleName ::= SEQUENCE {
1683 * hwType OBJECT IDENTIFIER,
1684 * hwSerialNum OCTET STRING }
1685 *
1686 * NOTE: we currently only parse and use otherName of type HwModuleName,
1687 * as defined in RFC 4108.
1688 */
1689static int x509_get_other_name( const mbedtls_x509_buf *subject_alt_name,
1690 mbedtls_x509_san_other_name *other_name )
1691{
Janos Follathab23cd12019-05-09 13:53:57 +01001692 int ret = 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001693 size_t len;
1694 unsigned char *p = subject_alt_name->p;
1695 const unsigned char *end = p + subject_alt_name->len;
1696 mbedtls_x509_buf cur_oid;
1697
1698 if( ( subject_alt_name->tag &
1699 ( MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK ) ) !=
1700 ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ) )
1701 {
1702 /*
1703 * The given subject alternative name is not of type "othername".
1704 */
1705 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
1706 }
1707
1708 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1709 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001710 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001711
1712 cur_oid.tag = MBEDTLS_ASN1_OID;
1713 cur_oid.p = p;
1714 cur_oid.len = len;
1715
1716 /*
1717 * Only HwModuleName is currently supported.
1718 */
1719 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid ) != 0 )
1720 {
1721 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1722 }
1723
Ron Eldor890819a2019-05-13 19:03:04 +03001724 if( p + len >= end )
1725 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001726 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001727 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1728 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001729 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001730 p += len;
1731 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1732 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001733 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001734
1735 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1736 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001737 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001738
1739 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001740 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001741
1742 other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID;
1743 other_name->value.hardware_module_name.oid.p = p;
1744 other_name->value.hardware_module_name.oid.len = len;
1745
Ron Eldor890819a2019-05-13 19:03:04 +03001746 if( p + len >= end )
1747 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001748 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001749 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1750 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001751 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001752 p += len;
1753 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1754 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001755 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001756
1757 other_name->value.hardware_module_name.val.tag = MBEDTLS_ASN1_OCTET_STRING;
1758 other_name->value.hardware_module_name.val.p = p;
1759 other_name->value.hardware_module_name.val.len = len;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001760 p += len;
1761 if( p != end )
1762 {
Ron Eldor890819a2019-05-13 19:03:04 +03001763 mbedtls_platform_zeroize( other_name,
Sébastien Duquette661d7252019-06-23 17:45:26 -04001764 sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001765 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1766 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001767 }
1768 return( 0 );
1769}
1770
Peter Kolbus9a969b62018-12-11 13:55:56 -06001771int mbedtls_x509_parse_subject_alt_name( const mbedtls_x509_buf *san_buf,
1772 mbedtls_x509_subject_alternative_name *san )
1773{
1774 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1775 switch( san_buf->tag &
1776 ( MBEDTLS_ASN1_TAG_CLASS_MASK |
1777 MBEDTLS_ASN1_TAG_VALUE_MASK ) )
1778 {
1779 /*
1780 * otherName
1781 */
1782 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ):
1783 {
1784 mbedtls_x509_san_other_name other_name;
1785
1786 ret = x509_get_other_name( san_buf, &other_name );
1787 if( ret != 0 )
1788 return( ret );
1789
1790 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1791 san->type = MBEDTLS_X509_SAN_OTHER_NAME;
1792 memcpy( &san->san.other_name,
1793 &other_name, sizeof( other_name ) );
1794
1795 }
1796 break;
1797
1798 /*
1799 * dNSName
1800 */
1801 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME ):
1802 {
1803 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1804 san->type = MBEDTLS_X509_SAN_DNS_NAME;
1805
1806 memcpy( &san->san.unstructured_name,
1807 san_buf, sizeof( *san_buf ) );
1808
1809 }
1810 break;
1811
1812 /*
1813 * Type not supported
1814 */
1815 default:
1816 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1817 }
1818 return( 0 );
1819}
1820
1821#if !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001822static int x509_info_subject_alt_name( char **buf, size_t *size,
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001823 const mbedtls_x509_sequence
1824 *subject_alt_name,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001825 const char *prefix )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001826{
Janos Follath865b3eb2019-12-16 11:46:15 +00001827 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001828 size_t n = *size;
1829 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001830 const mbedtls_x509_sequence *cur = subject_alt_name;
Ron Eldor890819a2019-05-13 19:03:04 +03001831 mbedtls_x509_subject_alternative_name san;
1832 int parse_ret;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001833
1834 while( cur != NULL )
1835 {
Ron Eldor890819a2019-05-13 19:03:04 +03001836 memset( &san, 0, sizeof( san ) );
1837 parse_ret = mbedtls_x509_parse_subject_alt_name( &cur->buf, &san );
1838 if( parse_ret != 0 )
1839 {
1840 if( parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1841 {
1842 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
1843 MBEDTLS_X509_SAFE_SNPRINTF;
1844 }
1845 else
1846 {
1847 ret = mbedtls_snprintf( p, n, "\n%s <malformed>", prefix );
1848 MBEDTLS_X509_SAFE_SNPRINTF;
1849 }
1850 cur = cur->next;
1851 continue;
1852 }
1853
1854 switch( san.type )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001855 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001856 /*
1857 * otherName
1858 */
Ron Eldora2913912019-05-16 16:17:38 +03001859 case MBEDTLS_X509_SAN_OTHER_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001860 {
1861 mbedtls_x509_san_other_name *other_name = &san.san.other_name;
1862
1863 ret = mbedtls_snprintf( p, n, "\n%s otherName :", prefix );
1864 MBEDTLS_X509_SAFE_SNPRINTF;
1865
1866 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME,
1867 &other_name->value.hardware_module_name.oid ) != 0 )
Janos Follath22f605f2019-05-10 10:37:17 +01001868 {
Ron Eldor890819a2019-05-13 19:03:04 +03001869 ret = mbedtls_snprintf( p, n, "\n%s hardware module name :", prefix );
1870 MBEDTLS_X509_SAFE_SNPRINTF;
1871 ret = mbedtls_snprintf( p, n, "\n%s hardware type : ", prefix );
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001872 MBEDTLS_X509_SAFE_SNPRINTF;
1873
Ron Eldor890819a2019-05-13 19:03:04 +03001874 ret = mbedtls_oid_get_numeric_string( p, n, &other_name->value.hardware_module_name.oid );
1875 MBEDTLS_X509_SAFE_SNPRINTF;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001876
Ron Eldor890819a2019-05-13 19:03:04 +03001877 ret = mbedtls_snprintf( p, n, "\n%s hardware serial number : ", prefix );
1878 MBEDTLS_X509_SAFE_SNPRINTF;
1879
1880 if( other_name->value.hardware_module_name.val.len >= n )
1881 {
1882 *p = '\0';
1883 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Janos Follath22f605f2019-05-10 10:37:17 +01001884 }
1885
Ron Eldora2913912019-05-16 16:17:38 +03001886 memcpy( p, other_name->value.hardware_module_name.val.p,
1887 other_name->value.hardware_module_name.val.len );
1888 p += other_name->value.hardware_module_name.val.len;
1889
Ron Eldor890819a2019-05-13 19:03:04 +03001890 n -= other_name->value.hardware_module_name.val.len;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001891
Ron Eldor890819a2019-05-13 19:03:04 +03001892 }/* MBEDTLS_OID_ON_HW_MODULE_NAME */
1893 }
1894 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001895
1896 /*
1897 * dNSName
1898 */
Ron Eldora2913912019-05-16 16:17:38 +03001899 case MBEDTLS_X509_SAN_DNS_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001900 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001901 ret = mbedtls_snprintf( p, n, "\n%s dNSName : ", prefix );
1902 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldor890819a2019-05-13 19:03:04 +03001903 if( san.san.unstructured_name.len >= n )
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001904 {
1905 *p = '\0';
1906 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
1907 }
Ron Eldora2913912019-05-16 16:17:38 +03001908
1909 memcpy( p, san.san.unstructured_name.p, san.san.unstructured_name.len );
1910 p += san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001911 n -= san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001912 }
1913 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001914
1915 /*
Ron Eldor890819a2019-05-13 19:03:04 +03001916 * Type not supported, skip item.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001917 */
1918 default:
Janos Follath22f605f2019-05-10 10:37:17 +01001919 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
1920 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001921 break;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001922 }
1923
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001924 cur = cur->next;
1925 }
1926
1927 *p = '\0';
1928
1929 *size = n;
1930 *buf = p;
1931
1932 return( 0 );
1933}
1934
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001935#define PRINT_ITEM(i) \
1936 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001937 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001938 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001939 sep = ", "; \
1940 }
1941
1942#define CERT_TYPE(type,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001943 if( ns_cert_type & (type) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001944 PRINT_ITEM( name );
1945
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001946static int x509_info_cert_type( char **buf, size_t *size,
1947 unsigned char ns_cert_type )
1948{
Janos Follath865b3eb2019-12-16 11:46:15 +00001949 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001950 size_t n = *size;
1951 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001952 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001954 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001955 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001956 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
1957 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1958 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001959 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
1960 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1961 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001962
1963 *size = n;
1964 *buf = p;
1965
1966 return( 0 );
1967}
1968
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001969#define KEY_USAGE(code,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001970 if( key_usage & (code) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001971 PRINT_ITEM( name );
1972
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001973static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001974 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001975{
Janos Follath865b3eb2019-12-16 11:46:15 +00001976 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001977 size_t n = *size;
1978 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001979 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001981 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
1982 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001983 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1984 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1985 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001986 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
1987 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001988 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
1989 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001990
1991 *size = n;
1992 *buf = p;
1993
1994 return( 0 );
1995}
1996
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001997static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001998 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001999{
Janos Follath865b3eb2019-12-16 11:46:15 +00002000 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002001 const char *desc;
2002 size_t n = *size;
2003 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002004 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002005 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002006
2007 while( cur != NULL )
2008 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002009 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002010 desc = "???";
2011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002012 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002013 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002014
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002015 sep = ", ";
2016
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002017 cur = cur->next;
2018 }
2019
2020 *size = n;
2021 *buf = p;
2022
2023 return( 0 );
2024}
2025
Ron Eldor74d9acc2019-03-21 14:00:03 +02002026static int x509_info_cert_policies( char **buf, size_t *size,
2027 const mbedtls_x509_sequence *certificate_policies )
2028{
Janos Follath865b3eb2019-12-16 11:46:15 +00002029 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor74d9acc2019-03-21 14:00:03 +02002030 const char *desc;
2031 size_t n = *size;
2032 char *p = *buf;
2033 const mbedtls_x509_sequence *cur = certificate_policies;
2034 const char *sep = "";
2035
2036 while( cur != NULL )
2037 {
2038 if( mbedtls_oid_get_certificate_policies( &cur->buf, &desc ) != 0 )
2039 desc = "???";
2040
2041 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
2042 MBEDTLS_X509_SAFE_SNPRINTF;
2043
2044 sep = ", ";
2045
2046 cur = cur->next;
2047 }
2048
2049 *size = n;
2050 *buf = p;
2051
2052 return( 0 );
2053}
2054
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002055/*
2056 * Return an informational string about the certificate.
2057 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002058#define BEFORE_COLON 18
2059#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002060int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
2061 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002062{
Janos Follath865b3eb2019-12-16 11:46:15 +00002063 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002064 size_t n;
2065 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002066 char key_size_str[BEFORE_COLON];
2067
2068 p = buf;
2069 n = size;
2070
Janos Follath98e28a72016-05-31 14:03:54 +01002071 if( NULL == crt )
2072 {
2073 ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" );
2074 MBEDTLS_X509_SAFE_SNPRINTF;
2075
2076 return( (int) ( size - n ) );
2077 }
2078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002079 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002080 prefix, crt->version );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002081 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002082 ret = mbedtls_snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002083 prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002084 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002086 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002087 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002089 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002090 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002091 ret = mbedtls_x509_dn_gets( p, n, &crt->issuer );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002092 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002094 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002095 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002096 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
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%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002100 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2101 crt->valid_from.year, crt->valid_from.mon,
2102 crt->valid_from.day, crt->valid_from.hour,
2103 crt->valid_from.min, crt->valid_from.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%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002107 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2108 crt->valid_to.year, crt->valid_to.mon,
2109 crt->valid_to.day, crt->valid_to.hour,
2110 crt->valid_to.min, crt->valid_to.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002111 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002113 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002114 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002116 ret = mbedtls_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02002117 crt->sig_md, crt->sig_opts );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002118 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002119
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002120 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002121 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
2122 mbedtls_pk_get_name( &crt->pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002123 {
2124 return( ret );
2125 }
2126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002127 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +02002128 (int) mbedtls_pk_get_bitlen( &crt->pk ) );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002129 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002130
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002131 /*
2132 * Optional extensions
2133 */
2134
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002135 if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002136 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002137 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002138 crt->ca_istrue ? "true" : "false" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002139 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002140
2141 if( crt->max_pathlen > 0 )
2142 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002143 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002144 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002145 }
2146 }
2147
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002148 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002149 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002150 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name :", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002151 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002152
2153 if( ( ret = x509_info_subject_alt_name( &p, &n,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002154 &crt->subject_alt_names,
Ron Eldor6aeae9e2019-05-20 12:00:36 +03002155 prefix ) ) != 0 )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002156 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002157 }
2158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002159 if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002160 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002161 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002162 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002163
2164 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
2165 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002166 }
2167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002168 if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002170 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002171 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002172
2173 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
2174 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002175 }
2176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002177 if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002178 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002179 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002180 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002181
2182 if( ( ret = x509_info_ext_key_usage( &p, &n,
2183 &crt->ext_key_usage ) ) != 0 )
2184 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002185 }
2186
Ron Eldor74d9acc2019-03-21 14:00:03 +02002187 if( crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES )
2188 {
2189 ret = mbedtls_snprintf( p, n, "\n%scertificate policies : ", prefix );
2190 MBEDTLS_X509_SAFE_SNPRINTF;
2191
2192 if( ( ret = x509_info_cert_policies( &p, &n,
2193 &crt->certificate_policies ) ) != 0 )
2194 return( ret );
2195 }
2196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002197 ret = mbedtls_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002198 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002199
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002200 return( (int) ( size - n ) );
2201}
2202
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002203struct x509_crt_verify_string {
2204 int code;
2205 const char *string;
2206};
2207
Hanno Becker7ac83f92020-10-09 10:48:22 +01002208#define X509_CRT_ERROR_INFO( err, err_str, info ) { err, info },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002209static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Hanno Becker7ac83f92020-10-09 10:48:22 +01002210 MBEDTLS_X509_CRT_ERROR_INFO_LIST
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002211 { 0, NULL }
2212};
Hanno Becker7ac83f92020-10-09 10:48:22 +01002213#undef X509_CRT_ERROR_INFO
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002214
2215int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002216 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002217{
Janos Follath865b3eb2019-12-16 11:46:15 +00002218 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002219 const struct x509_crt_verify_string *cur;
2220 char *p = buf;
2221 size_t n = size;
2222
2223 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
2224 {
2225 if( ( flags & cur->code ) == 0 )
2226 continue;
2227
2228 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002229 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002230 flags ^= cur->code;
2231 }
2232
2233 if( flags != 0 )
2234 {
2235 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
2236 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002237 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002238 }
2239
2240 return( (int) ( size - n ) );
2241}
Hanno Becker612a2f12020-10-09 09:19:39 +01002242#endif /* MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002243
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002244int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
2245 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002246{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002247 unsigned int usage_must, usage_may;
2248 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
2249 | MBEDTLS_X509_KU_DECIPHER_ONLY;
2250
2251 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
2252 return( 0 );
2253
2254 usage_must = usage & ~may_mask;
2255
2256 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
2257 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
2258
2259 usage_may = usage & may_mask;
2260
2261 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002262 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002263
2264 return( 0 );
2265}
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002267int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002268 const char *usage_oid,
2269 size_t usage_len )
2270{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002271 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002272
2273 /* Extension is not mandatory, absent means no restriction */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002274 if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002275 return( 0 );
2276
2277 /*
2278 * Look for the requested usage (or wildcard ANY) in our list
2279 */
2280 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
2281 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002282 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002283
2284 if( cur_oid->len == usage_len &&
2285 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
2286 {
2287 return( 0 );
2288 }
2289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002290 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002291 return( 0 );
2292 }
2293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002294 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002295}
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002297#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002298/*
2299 * Return 1 if the certificate is revoked, or 0 otherwise.
2300 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002301int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002302{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002303 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002304
2305 while( cur != NULL && cur->serial.len != 0 )
2306 {
2307 if( crt->serial.len == cur->serial.len &&
2308 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
2309 {
Raoul Strackxa4e86142020-06-15 17:03:13 +02002310 return( 1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002311 }
2312
2313 cur = cur->next;
2314 }
2315
2316 return( 0 );
2317}
2318
2319/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002320 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002321 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002322 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002323static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002324 mbedtls_x509_crl *crl_list,
2325 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002326{
2327 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002328 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2329 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002330
2331 if( ca == NULL )
2332 return( flags );
2333
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002334 while( crl_list != NULL )
2335 {
2336 if( crl_list->version == 0 ||
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002337 x509_name_cmp( &crl_list->issuer, &ca->subject ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002338 {
2339 crl_list = crl_list->next;
2340 continue;
2341 }
2342
2343 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002344 * Check if the CA is configured to sign CRLs
2345 */
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002346 if( mbedtls_x509_crt_check_key_usage( ca,
2347 MBEDTLS_X509_KU_CRL_SIGN ) != 0 )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002348 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002349 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002350 break;
2351 }
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002352
2353 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002354 * Check if CRL is correctly signed by the trusted CA
2355 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002356 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
2357 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
2358
2359 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
2360 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
2361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002362 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002363 if( mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002364 {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002365 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002366 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002367 break;
2368 }
2369
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002370 if( x509_profile_check_key( profile, &ca->pk ) != 0 )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002371 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002373 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
2374 crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02002375 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002376 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002377 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002378 break;
2379 }
2380
2381 /*
2382 * Check for validity of CRL (Do not drop out)
2383 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002384 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002385 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002386
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002387 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002388 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002389
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002390 /*
2391 * Check if certificate is revoked
2392 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002393 if( mbedtls_x509_crt_is_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002394 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002395 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002396 break;
2397 }
2398
2399 crl_list = crl_list->next;
2400 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002401
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002402 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002403}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002404#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002405
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002406/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002407 * Check the signature of a certificate by its parent
2408 */
2409static int x509_crt_check_signature( const mbedtls_x509_crt *child,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002410 mbedtls_x509_crt *parent,
2411 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002412{
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002413 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002414 size_t hash_len;
2415#if !defined(MBEDTLS_USE_PSA_CRYPTO)
2416 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002417 md_info = mbedtls_md_info_from_type( child->sig_md );
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002418 hash_len = mbedtls_md_get_size( md_info );
Andrzej Kurek8b38ff52018-11-20 03:20:09 -05002419
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002420 /* Note: hash errors can happen only after an internal error */
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002421 if( mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash ) != 0 )
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002422 return( -1 );
2423#else
Jaeden Amero34973232019-02-20 10:32:28 +00002424 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002425 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( child->sig_md );
2426
2427 if( psa_hash_setup( &hash_operation, hash_alg ) != PSA_SUCCESS )
2428 return( -1 );
2429
2430 if( psa_hash_update( &hash_operation, child->tbs.p, child->tbs.len )
2431 != PSA_SUCCESS )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002432 {
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002433 return( -1 );
2434 }
2435
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002436 if( psa_hash_finish( &hash_operation, hash, sizeof( hash ), &hash_len )
2437 != PSA_SUCCESS )
2438 {
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002439 return( -1 );
2440 }
2441#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002442 /* Skip expensive computation on obvious mismatch */
2443 if( ! mbedtls_pk_can_do( &parent->pk, child->sig_pk ) )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002444 return( -1 );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002445
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002446#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002447 if( rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA )
2448 {
2449 return( mbedtls_pk_verify_restartable( &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002450 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002451 child->sig.p, child->sig.len, &rs_ctx->pk ) );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002452 }
2453#else
2454 (void) rs_ctx;
2455#endif
2456
2457 return( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002458 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002459 child->sig.p, child->sig.len ) );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002460}
2461
2462/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002463 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2464 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002465 *
2466 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002467 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002468static int x509_crt_check_parent( const mbedtls_x509_crt *child,
2469 const mbedtls_x509_crt *parent,
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002470 int top )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002471{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002472 int need_ca_bit;
2473
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002474 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02002475 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002476 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002477
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002478 /* Parent must have the basicConstraints CA bit set as a general rule */
2479 need_ca_bit = 1;
2480
2481 /* Exception: v1/v2 certificates that are locally trusted. */
2482 if( top && parent->version < 3 )
2483 need_ca_bit = 0;
2484
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002485 if( need_ca_bit && ! parent->ca_istrue )
2486 return( -1 );
2487
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002488 if( need_ca_bit &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002489 mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002490 {
2491 return( -1 );
2492 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002493
2494 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002495}
2496
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002497/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002498 * Find a suitable parent for child in candidates, or return NULL.
2499 *
2500 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002501 * 1. subject name matches child's issuer
2502 * 2. if necessary, the CA bit is set and key usage allows signing certs
2503 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002504 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002505 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002506 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002507 * If there's a suitable candidate which is also time-valid, return the first
2508 * such. Otherwise, return the first suitable candidate (or NULL if there is
2509 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002510 *
2511 * The rationale for this rule is that someone could have a list of trusted
2512 * roots with two versions on the same root with different validity periods.
2513 * (At least one user reported having such a list and wanted it to just work.)
2514 * The reason we don't just require time-validity is that generally there is
2515 * only one version, and if it's expired we want the flags to state that
2516 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002517 *
2518 * The rationale for rule 3 (signature for trusted roots) is that users might
2519 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002520 * way we select the correct one is by checking the signature (as we don't
2521 * rely on key identifier extensions). (This is one way users might choose to
2522 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002523 *
2524 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002525 * - [in] child: certificate for which we're looking for a parent
2526 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002527 * - [out] r_parent: parent found (or NULL)
2528 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002529 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2530 * of the chain, 0 otherwise
2531 * - [in] path_cnt: number of intermediates seen so far
2532 * - [in] self_cnt: number of self-signed intermediates seen so far
2533 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002534 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002535 *
2536 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002537 * - 0 on success
2538 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002539 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002540static int x509_crt_find_parent_in(
2541 mbedtls_x509_crt *child,
2542 mbedtls_x509_crt *candidates,
2543 mbedtls_x509_crt **r_parent,
2544 int *r_signature_is_good,
2545 int top,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002546 unsigned path_cnt,
2547 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002548 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002549{
Janos Follath865b3eb2019-12-16 11:46:15 +00002550 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002551 mbedtls_x509_crt *parent, *fallback_parent;
Benjamin Kier36050732019-05-30 14:49:17 -04002552 int signature_is_good = 0, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002553
2554#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002555 /* did we have something in progress? */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002556 if( rs_ctx != NULL && rs_ctx->parent != NULL )
2557 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002558 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002559 parent = rs_ctx->parent;
2560 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002561 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002562
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002563 /* clear saved state */
2564 rs_ctx->parent = NULL;
2565 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002566 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002567
2568 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002569 goto check_signature;
2570 }
2571#endif
2572
2573 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002574 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002575
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002576 for( parent = candidates; parent != NULL; parent = parent->next )
2577 {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002578 /* basic parenting skills (name, CA bit, key usage) */
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002579 if( x509_crt_check_parent( child, parent, top ) != 0 )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002580 continue;
2581
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002582 /* +1 because stored max_pathlen is 1 higher that the actual value */
2583 if( parent->max_pathlen > 0 &&
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002584 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt )
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002585 {
2586 continue;
2587 }
2588
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002589 /* Signature */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002590#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2591check_signature:
2592#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002593 ret = x509_crt_check_signature( child, parent, rs_ctx );
2594
2595#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002596 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2597 {
2598 /* save state */
2599 rs_ctx->parent = parent;
2600 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002601 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002602
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002603 return( ret );
2604 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002605#else
2606 (void) ret;
2607#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002608
2609 signature_is_good = ret == 0;
2610 if( top && ! signature_is_good )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002611 continue;
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002612
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002613 /* optional time check */
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002614 if( mbedtls_x509_time_is_past( &parent->valid_to ) ||
2615 mbedtls_x509_time_is_future( &parent->valid_from ) )
2616 {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002617 if( fallback_parent == NULL )
2618 {
2619 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002620 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002621 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002622
2623 continue;
2624 }
2625
Andy Gross1f627142019-01-30 10:25:53 -06002626 *r_parent = parent;
2627 *r_signature_is_good = signature_is_good;
2628
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002629 break;
2630 }
2631
Andy Gross1f627142019-01-30 10:25:53 -06002632 if( parent == NULL )
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002633 {
2634 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002635 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002636 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002637
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002638 return( 0 );
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002639}
2640
2641/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002642 * Find a parent in trusted CAs or the provided chain, or return NULL.
2643 *
2644 * Searches in trusted CAs first, and return the first suitable parent found
2645 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002646 *
2647 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002648 * - [in] child: certificate for which we're looking for a parent, followed
2649 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002650 * - [in] trust_ca: list of locally trusted certificates
2651 * - [out] parent: parent found (or NULL)
2652 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2653 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2654 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2655 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002656 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002657 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002658 *
2659 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002660 * - 0 on success
2661 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002662 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002663static int x509_crt_find_parent(
2664 mbedtls_x509_crt *child,
2665 mbedtls_x509_crt *trust_ca,
2666 mbedtls_x509_crt **parent,
2667 int *parent_is_trusted,
2668 int *signature_is_good,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002669 unsigned path_cnt,
2670 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002671 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002672{
Janos Follath865b3eb2019-12-16 11:46:15 +00002673 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002674 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002675
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002676 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002677
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002678#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002679 /* restore then clear saved state if we have some stored */
2680 if( rs_ctx != NULL && rs_ctx->parent_is_trusted != -1 )
2681 {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002682 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002683 rs_ctx->parent_is_trusted = -1;
2684 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002685#endif
2686
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002687 while( 1 ) {
2688 search_list = *parent_is_trusted ? trust_ca : child->next;
2689
2690 ret = x509_crt_find_parent_in( child, search_list,
2691 parent, signature_is_good,
2692 *parent_is_trusted,
2693 path_cnt, self_cnt, rs_ctx );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002694
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002695#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002696 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2697 {
2698 /* save state */
2699 rs_ctx->parent_is_trusted = *parent_is_trusted;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002700 return( ret );
2701 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002702#else
2703 (void) ret;
2704#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002705
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002706 /* stop here if found or already in second iteration */
2707 if( *parent != NULL || *parent_is_trusted == 0 )
2708 break;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002709
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002710 /* prepare second iteration */
2711 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002712 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002713
2714 /* extra precaution against mistakes in the caller */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002715 if( *parent == NULL )
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002716 {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002717 *parent_is_trusted = 0;
2718 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002719 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002720
2721 return( 0 );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002722}
2723
2724/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002725 * Check if an end-entity certificate is locally trusted
2726 *
2727 * Currently we require such certificates to be self-signed (actually only
2728 * check for self-issued as self-signatures are not checked)
2729 */
2730static int x509_crt_check_ee_locally_trusted(
2731 mbedtls_x509_crt *crt,
2732 mbedtls_x509_crt *trust_ca )
2733{
2734 mbedtls_x509_crt *cur;
2735
2736 /* must be self-issued */
2737 if( x509_name_cmp( &crt->issuer, &crt->subject ) != 0 )
2738 return( -1 );
2739
2740 /* look for an exact match with trusted cert */
2741 for( cur = trust_ca; cur != NULL; cur = cur->next )
2742 {
2743 if( crt->raw.len == cur->raw.len &&
2744 memcmp( crt->raw.p, cur->raw.p, crt->raw.len ) == 0 )
2745 {
2746 return( 0 );
2747 }
2748 }
2749
2750 /* too bad */
2751 return( -1 );
2752}
2753
2754/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002755 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002756 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002757 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2758 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002759 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002760 * such that every cert in the chain is a child of the next one,
2761 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002762 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002763 * Verify that chain and return it with flags for all issues found.
2764 *
2765 * Special cases:
2766 * - EE == Rj -> return a one-element list containing it
2767 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2768 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002769 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002770 * Tests for (aspects of) this function should include at least:
2771 * - trusted EE
2772 * - EE -> trusted root
Antonin Décimo36e89b52019-01-23 15:24:37 +01002773 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002774 * - if relevant: EE untrusted
2775 * - if relevant: EE -> intermediate, untrusted
2776 * with the aspect under test checked at each relevant level (EE, int, root).
2777 * For some aspects longer chains are required, but usually length 2 is
2778 * enough (but length 1 is not in general).
2779 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002780 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002781 * - [in] crt: the cert list EE, C1, ..., Cn
2782 * - [in] trust_ca: the trusted list R1, ..., Rp
2783 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002784 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002785 * Only valid when return value is 0, may contain garbage otherwise!
2786 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002787 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002788 *
2789 * Return value:
2790 * - non-zero if the chain could not be fully built and examined
2791 * - 0 is the chain was successfully built and examined,
2792 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002793 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002794static int x509_crt_verify_chain(
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002795 mbedtls_x509_crt *crt,
2796 mbedtls_x509_crt *trust_ca,
2797 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00002798 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2799 void *p_ca_cb,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002800 const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002801 mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002802 mbedtls_x509_crt_restart_ctx *rs_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002803{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002804 /* Don't initialize any of those variables here, so that the compiler can
2805 * catch potential issues with jumping ahead when restarting */
Janos Follath865b3eb2019-12-16 11:46:15 +00002806 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002807 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002808 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002809 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002810 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002811 int parent_is_trusted;
2812 int child_is_trusted;
2813 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002814 unsigned self_cnt;
Hanno Beckerf53893b2019-03-28 13:45:55 +00002815 mbedtls_x509_crt *cur_trust_ca = NULL;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002816
2817#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2818 /* resume if we had an operation in progress */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002819 if( rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent )
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002820 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002821 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002822 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002823 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002824
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002825 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002826 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002827 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002828 flags = &cur->flags;
2829
2830 goto find_parent;
2831 }
2832#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002833
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002834 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002835 self_cnt = 0;
2836 parent_is_trusted = 0;
2837 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002838
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002839 while( 1 ) {
2840 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002841 cur = &ver_chain->items[ver_chain->len];
2842 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002843 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002844 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002845 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002846
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002847 /* Check time-validity (all certificates) */
2848 if( mbedtls_x509_time_is_past( &child->valid_to ) )
2849 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002850
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002851 if( mbedtls_x509_time_is_future( &child->valid_from ) )
2852 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002853
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002854 /* Stop here for trusted roots (but not for trusted EE certs) */
2855 if( child_is_trusted )
2856 return( 0 );
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002857
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002858 /* Check signature algorithm: MD & PK algs */
2859 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
2860 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002861
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002862 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
2863 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002864
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002865 /* Special case: EE certs that are locally trusted */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002866 if( ver_chain->len == 1 &&
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002867 x509_crt_check_ee_locally_trusted( child, trust_ca ) == 0 )
2868 {
2869 return( 0 );
2870 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002871
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002872#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2873find_parent:
2874#endif
Hanno Beckerf53893b2019-03-28 13:45:55 +00002875
2876 /* Obtain list of potential trusted signers from CA callback,
2877 * or use statically provided list. */
2878#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
2879 if( f_ca_cb != NULL )
2880 {
2881 mbedtls_x509_crt_free( ver_chain->trust_ca_cb_result );
2882 mbedtls_free( ver_chain->trust_ca_cb_result );
2883 ver_chain->trust_ca_cb_result = NULL;
2884
2885 ret = f_ca_cb( p_ca_cb, child, &ver_chain->trust_ca_cb_result );
2886 if( ret != 0 )
2887 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2888
2889 cur_trust_ca = ver_chain->trust_ca_cb_result;
2890 }
2891 else
2892#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2893 {
2894 ((void) f_ca_cb);
2895 ((void) p_ca_cb);
2896 cur_trust_ca = trust_ca;
2897 }
2898
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002899 /* Look for a parent in trusted CAs or up the chain */
Hanno Beckerf53893b2019-03-28 13:45:55 +00002900 ret = x509_crt_find_parent( child, cur_trust_ca, &parent,
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002901 &parent_is_trusted, &signature_is_good,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002902 ver_chain->len - 1, self_cnt, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002903
2904#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002905 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2906 {
2907 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002908 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002909 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002910 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002911
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002912 return( ret );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002913 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002914#else
2915 (void) ret;
2916#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002917
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002918 /* No parent? We're done here */
2919 if( parent == NULL )
2920 {
2921 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2922 return( 0 );
2923 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002924
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002925 /* Count intermediate self-issued (not necessarily self-signed) certs.
2926 * These can occur with some strategies for key rollover, see [SIRO],
2927 * and should be excluded from max_pathlen checks. */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002928 if( ver_chain->len != 1 &&
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002929 x509_name_cmp( &child->issuer, &child->subject ) == 0 )
2930 {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002931 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002932 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002933
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002934 /* path_cnt is 0 for the first intermediate CA,
2935 * and if parent is trusted it's not an intermediate CA */
2936 if( ! parent_is_trusted &&
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002937 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002938 {
2939 /* return immediately to avoid overflow the chain array */
2940 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2941 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002942
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002943 /* signature was checked while searching parent */
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002944 if( ! signature_is_good )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002945 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2946
2947 /* check size of signing key */
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002948 if( x509_profile_check_key( profile, &parent->pk ) != 0 )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002949 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002951#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002952 /* Check trusted CA's CRL for the given crt */
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002953 *flags |= x509_crt_verifycrl( child, parent, ca_crl, profile );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002954#else
2955 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002956#endif
2957
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002958 /* prepare for next iteration */
2959 child = parent;
2960 parent = NULL;
2961 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002962 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002963 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002964}
2965
2966/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002967 * Check for CN match
2968 */
2969static int x509_crt_check_cn( const mbedtls_x509_buf *name,
2970 const char *cn, size_t cn_len )
2971{
2972 /* try exact match */
2973 if( name->len == cn_len &&
2974 x509_memcasecmp( cn, name->p, cn_len ) == 0 )
2975 {
2976 return( 0 );
2977 }
2978
2979 /* try wildcard match */
Manuel Pégourié-Gonnard900fba62017-10-18 14:28:11 +02002980 if( x509_check_wildcard( cn, name ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002981 {
2982 return( 0 );
2983 }
2984
2985 return( -1 );
2986}
2987
2988/*
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002989 * Check for SAN match, see RFC 5280 Section 4.2.1.6
2990 */
2991static int x509_crt_check_san( const mbedtls_x509_buf *name,
2992 const char *cn, size_t cn_len )
2993{
2994 const unsigned char san_type = (unsigned char) name->tag &
2995 MBEDTLS_ASN1_TAG_VALUE_MASK;
2996
2997 /* dNSName */
2998 if( san_type == MBEDTLS_X509_SAN_DNS_NAME )
2999 return( x509_crt_check_cn( name, cn, cn_len ) );
3000
3001 /* (We may handle other types here later.) */
3002
3003 /* Unrecognized type */
3004 return( -1 );
3005}
3006
3007/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003008 * Verify the requested CN - only call this if cn is not NULL!
3009 */
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003010static void x509_crt_verify_name( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003011 const char *cn,
3012 uint32_t *flags )
3013{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003014 const mbedtls_x509_name *name;
3015 const mbedtls_x509_sequence *cur;
3016 size_t cn_len = strlen( cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003017
3018 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
3019 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003020 for( cur = &crt->subject_alt_names; cur != NULL; cur = cur->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003021 {
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003022 if( x509_crt_check_san( &cur->buf, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003023 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003024 }
3025
3026 if( cur == NULL )
3027 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3028 }
3029 else
3030 {
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02003031 for( name = &crt->subject; name != NULL; name = name->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003032 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003033 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, &name->oid ) == 0 &&
3034 x509_crt_check_cn( &name->val, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003035 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003036 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003037 }
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003038 }
3039
3040 if( name == NULL )
3041 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3042 }
3043}
3044
3045/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003046 * Merge the flags for all certs in the chain, after calling callback
3047 */
3048static int x509_crt_merge_flags_with_cb(
3049 uint32_t *flags,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003050 const mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003051 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3052 void *p_vrfy )
3053{
Janos Follath865b3eb2019-12-16 11:46:15 +00003054 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003055 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003056 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003057 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003058
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003059 for( i = ver_chain->len; i != 0; --i )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003060 {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003061 cur = &ver_chain->items[i-1];
3062 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003063
3064 if( NULL != f_vrfy )
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003065 if( ( ret = f_vrfy( p_vrfy, cur->crt, (int) i-1, &cur_flags ) ) != 0 )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003066 return( ret );
3067
3068 *flags |= cur_flags;
3069 }
3070
3071 return( 0 );
3072}
3073
3074/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003075 * Verify the certificate validity, with profile, restartable version
3076 *
3077 * This function:
3078 * - checks the requested CN (if any)
3079 * - checks the type and size of the EE cert's key,
3080 * as that isn't done as part of chain building/verification currently
3081 * - builds and verifies the chain
3082 * - then calls the callback and merges the flags
Hanno Becker3116fb32019-03-28 13:34:42 +00003083 *
3084 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
3085 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
3086 * verification routine to search for trusted signers, and CRLs will
3087 * be disabled. Otherwise, `trust_ca` will be used as the static list
3088 * of trusted signers, and `ca_crl` will be use as the static list
3089 * of CRLs.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003090 */
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003091static int x509_crt_verify_restartable_ca_cb( mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003092 mbedtls_x509_crt *trust_ca,
3093 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003094 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3095 void *p_ca_cb,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003096 const mbedtls_x509_crt_profile *profile,
3097 const char *cn, uint32_t *flags,
3098 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3099 void *p_vrfy,
3100 mbedtls_x509_crt_restart_ctx *rs_ctx )
3101{
Janos Follath865b3eb2019-12-16 11:46:15 +00003102 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003103 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003104 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003105 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003106
3107 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003108 ee_flags = 0;
3109 x509_crt_verify_chain_reset( &ver_chain );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003110
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003111 if( profile == NULL )
3112 {
3113 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3114 goto exit;
3115 }
3116
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003117 /* check name if requested */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003118 if( cn != NULL )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003119 x509_crt_verify_name( crt, cn, &ee_flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003120
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003121 /* Check the type and size of the key */
3122 pk_type = mbedtls_pk_get_type( &crt->pk );
3123
3124 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003125 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003126
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02003127 if( x509_profile_check_key( profile, &crt->pk ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003128 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003129
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003130 /* Check the chain */
Hanno Becker3116fb32019-03-28 13:34:42 +00003131 ret = x509_crt_verify_chain( crt, trust_ca, ca_crl,
3132 f_ca_cb, p_ca_cb, profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003133 &ver_chain, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003134
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003135 if( ret != 0 )
3136 goto exit;
3137
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003138 /* Merge end-entity flags */
3139 ver_chain.items[0].flags |= ee_flags;
3140
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003141 /* Build final flags, calling callback on the way if any */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003142 ret = x509_crt_merge_flags_with_cb( flags, &ver_chain, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003143
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003144exit:
Hanno Beckerf53893b2019-03-28 13:45:55 +00003145
3146#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3147 mbedtls_x509_crt_free( ver_chain.trust_ca_cb_result );
3148 mbedtls_free( ver_chain.trust_ca_cb_result );
3149 ver_chain.trust_ca_cb_result = NULL;
3150#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3151
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003152#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3153 if( rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
3154 mbedtls_x509_crt_restart_free( rs_ctx );
3155#endif
3156
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003157 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3158 * the SSL module for authmode optional, but non-zero return from the
3159 * callback means a fatal error so it shouldn't be ignored */
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003160 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
3161 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3162
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003163 if( ret != 0 )
3164 {
3165 *flags = (uint32_t) -1;
3166 return( ret );
3167 }
3168
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003169 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003170 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003171
3172 return( 0 );
3173}
3174
Hanno Becker3116fb32019-03-28 13:34:42 +00003175
3176/*
3177 * Verify the certificate validity (default profile, not restartable)
3178 */
3179int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
3180 mbedtls_x509_crt *trust_ca,
3181 mbedtls_x509_crl *ca_crl,
3182 const char *cn, uint32_t *flags,
3183 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3184 void *p_vrfy )
3185{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003186 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003187 NULL, NULL,
3188 &mbedtls_x509_crt_profile_default,
3189 cn, flags,
3190 f_vrfy, p_vrfy, NULL ) );
3191}
3192
3193/*
3194 * Verify the certificate validity (user-chosen profile, not restartable)
3195 */
3196int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
3197 mbedtls_x509_crt *trust_ca,
3198 mbedtls_x509_crl *ca_crl,
3199 const mbedtls_x509_crt_profile *profile,
3200 const char *cn, uint32_t *flags,
3201 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3202 void *p_vrfy )
3203{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003204 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003205 NULL, NULL,
3206 profile, cn, flags,
3207 f_vrfy, p_vrfy, NULL ) );
3208}
3209
3210#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3211/*
3212 * Verify the certificate validity (user-chosen profile, CA callback,
3213 * not restartable).
3214 */
Jarno Lamsa31d9db62019-04-01 14:33:49 +03003215int mbedtls_x509_crt_verify_with_ca_cb( mbedtls_x509_crt *crt,
Hanno Becker3116fb32019-03-28 13:34:42 +00003216 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3217 void *p_ca_cb,
3218 const mbedtls_x509_crt_profile *profile,
3219 const char *cn, uint32_t *flags,
3220 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3221 void *p_vrfy )
3222{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003223 return( x509_crt_verify_restartable_ca_cb( crt, NULL, NULL,
Hanno Becker3116fb32019-03-28 13:34:42 +00003224 f_ca_cb, p_ca_cb,
3225 profile, cn, flags,
3226 f_vrfy, p_vrfy, NULL ) );
3227}
3228#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3229
3230int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
3231 mbedtls_x509_crt *trust_ca,
3232 mbedtls_x509_crl *ca_crl,
3233 const mbedtls_x509_crt_profile *profile,
3234 const char *cn, uint32_t *flags,
3235 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3236 void *p_vrfy,
3237 mbedtls_x509_crt_restart_ctx *rs_ctx )
3238{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003239 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003240 NULL, NULL,
3241 profile, cn, flags,
3242 f_vrfy, p_vrfy, rs_ctx ) );
3243}
3244
3245
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003246/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003247 * Initialize a certificate chain
3248 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003249void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02003250{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003251 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02003252}
3253
3254/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003255 * Unallocate all certificate data
3256 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003257void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003258{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003259 mbedtls_x509_crt *cert_cur = crt;
3260 mbedtls_x509_crt *cert_prv;
3261 mbedtls_x509_name *name_cur;
3262 mbedtls_x509_name *name_prv;
3263 mbedtls_x509_sequence *seq_cur;
3264 mbedtls_x509_sequence *seq_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003265
3266 if( crt == NULL )
3267 return;
3268
3269 do
3270 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003271 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003273#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
3274 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003275#endif
3276
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003277 name_cur = cert_cur->issuer.next;
3278 while( name_cur != NULL )
3279 {
3280 name_prv = name_cur;
3281 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003282 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003283 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003284 }
3285
3286 name_cur = cert_cur->subject.next;
3287 while( name_cur != NULL )
3288 {
3289 name_prv = name_cur;
3290 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003291 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003292 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003293 }
3294
3295 seq_cur = cert_cur->ext_key_usage.next;
3296 while( seq_cur != NULL )
3297 {
3298 seq_prv = seq_cur;
3299 seq_cur = seq_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003300 mbedtls_platform_zeroize( seq_prv,
3301 sizeof( mbedtls_x509_sequence ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003302 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003303 }
3304
3305 seq_cur = cert_cur->subject_alt_names.next;
3306 while( seq_cur != NULL )
3307 {
3308 seq_prv = seq_cur;
3309 seq_cur = seq_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003310 mbedtls_platform_zeroize( seq_prv,
3311 sizeof( mbedtls_x509_sequence ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003312 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003313 }
3314
Ron Eldor74d9acc2019-03-21 14:00:03 +02003315 seq_cur = cert_cur->certificate_policies.next;
3316 while( seq_cur != NULL )
3317 {
3318 seq_prv = seq_cur;
3319 seq_cur = seq_cur->next;
3320 mbedtls_platform_zeroize( seq_prv,
3321 sizeof( mbedtls_x509_sequence ) );
3322 mbedtls_free( seq_prv );
3323 }
3324
Hanno Becker1a65dcd2019-01-31 08:57:44 +00003325 if( cert_cur->raw.p != NULL && cert_cur->own_buffer )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003326 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003327 mbedtls_platform_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003328 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003329 }
3330
3331 cert_cur = cert_cur->next;
3332 }
3333 while( cert_cur != NULL );
3334
3335 cert_cur = crt;
3336 do
3337 {
3338 cert_prv = cert_cur;
3339 cert_cur = cert_cur->next;
3340
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003341 mbedtls_platform_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003342 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003343 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003344 }
3345 while( cert_cur != NULL );
3346}
3347
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003348#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3349/*
3350 * Initialize a restart context
3351 */
3352void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx )
3353{
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003354 mbedtls_pk_restart_init( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003355
3356 ctx->parent = NULL;
3357 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003358 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003359
3360 ctx->parent_is_trusted = -1;
3361
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003362 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003363 ctx->self_cnt = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003364 x509_crt_verify_chain_reset( &ctx->ver_chain );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003365}
3366
3367/*
3368 * Free the components of a restart context
3369 */
3370void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx )
3371{
3372 if( ctx == NULL )
3373 return;
3374
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003375 mbedtls_pk_restart_free( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003376 mbedtls_x509_crt_restart_init( ctx );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003377}
3378#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003380#endif /* MBEDTLS_X509_CRT_PARSE_C */