blob: fdddbd3b099485cbc0743e03683006b55550c14e [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
Paul Bakker7c6b2c32013-09-16 13:49:26 +020098/*
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020099 * Default profile
100 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200101const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
102{
Gilles Peskine5e79cb32017-05-04 16:17:21 +0200103 /* Only SHA-2 hashes */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200104 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) |
105 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
106 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
107 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
108 0xFFFFFFF, /* Any PK alg */
109 0xFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200110 2048,
111};
112
113/*
114 * Next-default profile
115 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200116const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
117{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200118 /* Hashes from SHA-256 and above */
119 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
120 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
121 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
122 0xFFFFFFF, /* Any PK alg */
123#if defined(MBEDTLS_ECP_C)
124 /* Curves at or above 128-bit security level */
125 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
126 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
127 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
128 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
129 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
130 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
131 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256K1 ),
132#else
133 0,
134#endif
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200135 2048,
136};
137
138/*
139 * NSA Suite B Profile
140 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200141const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
142{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200143 /* Only SHA-256 and 384 */
144 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
145 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ),
146 /* Only ECDSA */
Ron Eldor85e1dcf2018-02-06 15:59:38 +0200147 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECDSA ) |
148 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECKEY ),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200149#if defined(MBEDTLS_ECP_C)
150 /* Only NIST P-256 and P-384 */
151 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
152 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ),
153#else
154 0,
155#endif
156 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200157};
158
159/*
Manuel Pégourié-Gonnard9d4c2c42021-06-18 09:48:27 +0200160 * Empty / all-forbidden profile
161 */
162const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_none =
163{
164 0,
165 0,
166 0,
167 (uint32_t) -1,
168};
169
170/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200171 * Check md_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200172 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200173 */
174static int x509_profile_check_md_alg( const mbedtls_x509_crt_profile *profile,
175 mbedtls_md_type_t md_alg )
176{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200177 if( md_alg == MBEDTLS_MD_NONE )
178 return( -1 );
179
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200180 if( ( profile->allowed_mds & MBEDTLS_X509_ID_FLAG( md_alg ) ) != 0 )
181 return( 0 );
182
183 return( -1 );
184}
185
186/*
187 * Check pk_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200188 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200189 */
190static int x509_profile_check_pk_alg( const mbedtls_x509_crt_profile *profile,
191 mbedtls_pk_type_t pk_alg )
192{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200193 if( pk_alg == MBEDTLS_PK_NONE )
194 return( -1 );
195
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200196 if( ( profile->allowed_pks & MBEDTLS_X509_ID_FLAG( pk_alg ) ) != 0 )
197 return( 0 );
198
199 return( -1 );
200}
201
202/*
203 * Check key against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200204 * Return 0 if pk is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200205 */
206static int x509_profile_check_key( const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200207 const mbedtls_pk_context *pk )
208{
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200209 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type( pk );
Manuel Pégourié-Gonnard19773ff2017-10-24 10:51:26 +0200210
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200211#if defined(MBEDTLS_RSA_C)
212 if( pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS )
213 {
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200214 if( mbedtls_pk_get_bitlen( pk ) >= profile->rsa_min_bitlen )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200215 return( 0 );
216
217 return( -1 );
218 }
219#endif
220
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200221#if defined(MBEDTLS_ECP_C)
222 if( pk_alg == MBEDTLS_PK_ECDSA ||
223 pk_alg == MBEDTLS_PK_ECKEY ||
224 pk_alg == MBEDTLS_PK_ECKEY_DH )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200225 {
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200226 const mbedtls_ecp_group_id gid = mbedtls_pk_ec( *pk )->grp.id;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200227
Philippe Antoineb5b25432018-05-11 11:06:29 +0200228 if( gid == MBEDTLS_ECP_DP_NONE )
229 return( -1 );
230
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200231 if( ( profile->allowed_curves & MBEDTLS_X509_ID_FLAG( gid ) ) != 0 )
232 return( 0 );
233
234 return( -1 );
235 }
236#endif
237
238 return( -1 );
239}
240
241/*
Hanno Becker1f8527f2018-11-02 09:19:16 +0000242 * Like memcmp, but case-insensitive and always returns -1 if different
243 */
244static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
245{
246 size_t i;
247 unsigned char diff;
248 const unsigned char *n1 = s1, *n2 = s2;
249
250 for( i = 0; i < len; i++ )
251 {
252 diff = n1[i] ^ n2[i];
253
254 if( diff == 0 )
255 continue;
256
257 if( diff == 32 &&
258 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
259 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
260 {
261 continue;
262 }
263
264 return( -1 );
265 }
266
267 return( 0 );
268}
269
270/*
271 * Return 0 if name matches wildcard, -1 otherwise
272 */
273static int x509_check_wildcard( const char *cn, const mbedtls_x509_buf *name )
274{
275 size_t i;
276 size_t cn_idx = 0, cn_len = strlen( cn );
277
278 /* We can't have a match if there is no wildcard to match */
279 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
280 return( -1 );
281
282 for( i = 0; i < cn_len; ++i )
283 {
284 if( cn[i] == '.' )
285 {
286 cn_idx = i;
287 break;
288 }
289 }
290
291 if( cn_idx == 0 )
292 return( -1 );
293
294 if( cn_len - cn_idx == name->len - 1 &&
295 x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
296 {
297 return( 0 );
298 }
299
300 return( -1 );
301}
302
303/*
304 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
305 * variations (but not all).
306 *
307 * Return 0 if equal, -1 otherwise.
308 */
309static int x509_string_cmp( const mbedtls_x509_buf *a, const mbedtls_x509_buf *b )
310{
311 if( a->tag == b->tag &&
312 a->len == b->len &&
313 memcmp( a->p, b->p, b->len ) == 0 )
314 {
315 return( 0 );
316 }
317
318 if( ( a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
319 ( b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
320 a->len == b->len &&
321 x509_memcasecmp( a->p, b->p, b->len ) == 0 )
322 {
323 return( 0 );
324 }
325
326 return( -1 );
327}
328
329/*
330 * Compare two X.509 Names (aka rdnSequence).
331 *
332 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
333 * we sometimes return unequal when the full algorithm would return equal,
334 * but never the other way. (In particular, we don't do Unicode normalisation
335 * or space folding.)
336 *
337 * Return 0 if equal, -1 otherwise.
338 */
339static int x509_name_cmp( const mbedtls_x509_name *a, const mbedtls_x509_name *b )
340{
341 /* Avoid recursion, it might not be optimised by the compiler */
342 while( a != NULL || b != NULL )
343 {
344 if( a == NULL || b == NULL )
345 return( -1 );
346
347 /* type */
348 if( a->oid.tag != b->oid.tag ||
349 a->oid.len != b->oid.len ||
350 memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 )
351 {
352 return( -1 );
353 }
354
355 /* value */
356 if( x509_string_cmp( &a->val, &b->val ) != 0 )
357 return( -1 );
358
359 /* structure of the list of sets */
360 if( a->next_merged != b->next_merged )
361 return( -1 );
362
363 a = a->next;
364 b = b->next;
365 }
366
367 /* a == NULL == b */
368 return( 0 );
369}
370
371/*
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200372 * Reset (init or clear) a verify_chain
373 */
374static void x509_crt_verify_chain_reset(
375 mbedtls_x509_crt_verify_chain *ver_chain )
376{
377 size_t i;
378
379 for( i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++ )
380 {
381 ver_chain->items[i].crt = NULL;
Hanno Beckera9375b32019-01-10 09:19:26 +0000382 ver_chain->items[i].flags = (uint32_t) -1;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200383 }
384
385 ver_chain->len = 0;
Hanno Beckerf53893b2019-03-28 13:45:55 +0000386
387#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
388 ver_chain->trust_ca_cb_result = NULL;
389#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200390}
391
392/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200393 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
394 */
395static int x509_get_version( unsigned char **p,
396 const unsigned char *end,
397 int *ver )
398{
Janos Follath865b3eb2019-12-16 11:46:15 +0000399 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200400 size_t len;
401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
403 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200404 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200406 {
407 *ver = 0;
408 return( 0 );
409 }
410
Chris Jones9f7a6932021-04-14 12:12:09 +0100411 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200412 }
413
414 end = *p + len;
415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100417 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_VERSION, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200418
419 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100420 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_VERSION,
421 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200422
423 return( 0 );
424}
425
426/*
427 * Validity ::= SEQUENCE {
428 * notBefore Time,
429 * notAfter Time }
430 */
431static int x509_get_dates( unsigned char **p,
432 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 mbedtls_x509_time *from,
434 mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200435{
Janos Follath865b3eb2019-12-16 11:46:15 +0000436 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200437 size_t len;
438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
440 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100441 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200442
443 end = *p + len;
444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445 if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200446 return( ret );
447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448 if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200449 return( ret );
450
451 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100452 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE,
453 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200454
455 return( 0 );
456}
457
458/*
459 * X.509 v2/v3 unique identifier (not parsed)
460 */
461static int x509_get_uid( unsigned char **p,
462 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 mbedtls_x509_buf *uid, int n )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200464{
Janos Follath865b3eb2019-12-16 11:46:15 +0000465 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200466
467 if( *p == end )
468 return( 0 );
469
470 uid->tag = **p;
471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472 if( ( ret = mbedtls_asn1_get_tag( p, end, &uid->len,
473 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200474 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200476 return( 0 );
477
Chris Jones9f7a6932021-04-14 12:12:09 +0100478 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200479 }
480
481 uid->p = *p;
482 *p += uid->len;
483
484 return( 0 );
485}
486
487static int x509_get_basic_constraints( unsigned char **p,
488 const unsigned char *end,
489 int *ca_istrue,
490 int *max_pathlen )
491{
Janos Follath865b3eb2019-12-16 11:46:15 +0000492 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200493 size_t len;
494
495 /*
496 * BasicConstraints ::= SEQUENCE {
497 * cA BOOLEAN DEFAULT FALSE,
498 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
499 */
500 *ca_istrue = 0; /* DEFAULT FALSE */
501 *max_pathlen = 0; /* endless */
502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
504 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100505 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200506
507 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200508 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 if( ( ret = mbedtls_asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
513 ret = mbedtls_asn1_get_int( p, end, ca_istrue );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200514
515 if( ret != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100516 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200517
518 if( *ca_istrue != 0 )
519 *ca_istrue = 1;
520 }
521
522 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200523 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100526 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200527
528 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100529 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
530 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200531
Andrzej Kurek16050742020-04-14 09:49:52 -0400532 /* Do not accept max_pathlen equal to INT_MAX to avoid a signed integer
533 * overflow, which is an undefined behavior. */
534 if( *max_pathlen == INT_MAX )
Chris Jones9f7a6932021-04-14 12:12:09 +0100535 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
536 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Andrzej Kurek16050742020-04-14 09:49:52 -0400537
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200538 (*max_pathlen)++;
539
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200540 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200541}
542
543static int x509_get_ns_cert_type( unsigned char **p,
544 const unsigned char *end,
545 unsigned char *ns_cert_type)
546{
Janos Follath865b3eb2019-12-16 11:46:15 +0000547 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100551 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200552
553 if( bs.len != 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100554 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
555 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200556
557 /* Get actual bitstring */
558 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200559 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200560}
561
562static int x509_get_key_usage( unsigned char **p,
563 const unsigned char *end,
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +0100564 unsigned int *key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200565{
Janos Follath865b3eb2019-12-16 11:46:15 +0000566 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200567 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100571 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200572
573 if( bs.len < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100574 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
575 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200576
577 /* Get actual bitstring */
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200578 *key_usage = 0;
579 for( i = 0; i < bs.len && i < sizeof( unsigned int ); i++ )
580 {
581 *key_usage |= (unsigned int) bs.p[i] << (8*i);
582 }
583
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200584 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200585}
586
587/*
588 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
589 *
590 * KeyPurposeId ::= OBJECT IDENTIFIER
591 */
592static int x509_get_ext_key_usage( unsigned char **p,
593 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200595{
Janos Follath865b3eb2019-12-16 11:46:15 +0000596 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200598 if( ( ret = mbedtls_asn1_get_sequence_of( p, end, ext_key_usage, MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100599 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200600
601 /* Sequence length must be >= 1 */
602 if( ext_key_usage->buf.p == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100603 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
604 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200605
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200606 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200607}
608
609/*
610 * SubjectAltName ::= GeneralNames
611 *
612 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
613 *
614 * GeneralName ::= CHOICE {
615 * otherName [0] OtherName,
616 * rfc822Name [1] IA5String,
617 * dNSName [2] IA5String,
618 * x400Address [3] ORAddress,
619 * directoryName [4] Name,
620 * ediPartyName [5] EDIPartyName,
621 * uniformResourceIdentifier [6] IA5String,
622 * iPAddress [7] OCTET STRING,
623 * registeredID [8] OBJECT IDENTIFIER }
624 *
625 * OtherName ::= SEQUENCE {
626 * type-id OBJECT IDENTIFIER,
627 * value [0] EXPLICIT ANY DEFINED BY type-id }
628 *
629 * EDIPartyName ::= SEQUENCE {
630 * nameAssigner [0] DirectoryString OPTIONAL,
631 * partyName [1] DirectoryString }
632 *
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300633 * NOTE: we list all types, but only use dNSName and otherName
634 * of type HwModuleName, as defined in RFC 4108, at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200635 */
636static int x509_get_subject_alt_name( unsigned char **p,
637 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638 mbedtls_x509_sequence *subject_alt_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200639{
Janos Follath865b3eb2019-12-16 11:46:15 +0000640 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200641 size_t len, tag_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 mbedtls_asn1_buf *buf;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200643 unsigned char tag;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 mbedtls_asn1_sequence *cur = subject_alt_name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200645
646 /* Get main sequence tag */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
648 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100649 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200650
651 if( *p + len != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100652 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
653 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200654
655 while( *p < end )
656 {
Ron Eldordbbd9662019-05-15 15:46:03 +0300657 mbedtls_x509_subject_alternative_name dummy_san_buf;
658 memset( &dummy_san_buf, 0, sizeof( dummy_san_buf ) );
659
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200660 tag = **p;
661 (*p)++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 if( ( ret = mbedtls_asn1_get_len( p, end, &tag_len ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100663 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200664
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100665 if( ( tag & MBEDTLS_ASN1_TAG_CLASS_MASK ) !=
666 MBEDTLS_ASN1_CONTEXT_SPECIFIC )
667 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100668 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
669 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100670 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200671
Ron Eldordbbd9662019-05-15 15:46:03 +0300672 /*
irwir74aee1c2019-09-21 18:21:48 +0300673 * Check that the SAN is structured correctly.
Ron Eldordbbd9662019-05-15 15:46:03 +0300674 */
675 ret = mbedtls_x509_parse_subject_alt_name( &(cur->buf), &dummy_san_buf );
676 /*
677 * In case the extension is malformed, return an error,
678 * and clear the allocated sequences.
679 */
680 if( ret != 0 && ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
681 {
682 mbedtls_x509_sequence *seq_cur = subject_alt_name->next;
683 mbedtls_x509_sequence *seq_prv;
684 while( seq_cur != NULL )
685 {
686 seq_prv = seq_cur;
687 seq_cur = seq_cur->next;
688 mbedtls_platform_zeroize( seq_prv,
689 sizeof( mbedtls_x509_sequence ) );
690 mbedtls_free( seq_prv );
691 }
Ron Eldor5aebeeb2019-05-22 16:41:21 +0300692 subject_alt_name->next = NULL;
Ron Eldordbbd9662019-05-15 15:46:03 +0300693 return( ret );
694 }
695
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200696 /* Allocate and assign next pointer */
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200697 if( cur->buf.p != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200698 {
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100699 if( cur->next != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100701
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200702 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200703
704 if( cur->next == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100705 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
706 MBEDTLS_ERR_ASN1_ALLOC_FAILED ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200707
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200708 cur = cur->next;
709 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200710
711 buf = &(cur->buf);
712 buf->tag = tag;
713 buf->p = *p;
714 buf->len = tag_len;
715 *p += buf->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200716 }
717
718 /* Set final sequence entry's next pointer to NULL */
719 cur->next = NULL;
720
721 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100722 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
723 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200724
725 return( 0 );
726}
727
728/*
Ron Eldor74d9acc2019-03-21 14:00:03 +0200729 * id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 }
730 *
731 * anyPolicy OBJECT IDENTIFIER ::= { id-ce-certificatePolicies 0 }
732 *
733 * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
734 *
735 * PolicyInformation ::= SEQUENCE {
736 * policyIdentifier CertPolicyId,
737 * policyQualifiers SEQUENCE SIZE (1..MAX) OF
738 * PolicyQualifierInfo OPTIONAL }
739 *
740 * CertPolicyId ::= OBJECT IDENTIFIER
741 *
742 * PolicyQualifierInfo ::= SEQUENCE {
743 * policyQualifierId PolicyQualifierId,
744 * qualifier ANY DEFINED BY policyQualifierId }
745 *
746 * -- policyQualifierIds for Internet policy qualifiers
747 *
748 * id-qt OBJECT IDENTIFIER ::= { id-pkix 2 }
749 * id-qt-cps OBJECT IDENTIFIER ::= { id-qt 1 }
750 * id-qt-unotice OBJECT IDENTIFIER ::= { id-qt 2 }
751 *
752 * PolicyQualifierId ::= OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice )
753 *
754 * Qualifier ::= CHOICE {
755 * cPSuri CPSuri,
756 * userNotice UserNotice }
757 *
758 * CPSuri ::= IA5String
759 *
760 * UserNotice ::= SEQUENCE {
761 * noticeRef NoticeReference OPTIONAL,
762 * explicitText DisplayText OPTIONAL }
763 *
764 * NoticeReference ::= SEQUENCE {
765 * organization DisplayText,
766 * noticeNumbers SEQUENCE OF INTEGER }
767 *
768 * DisplayText ::= CHOICE {
769 * ia5String IA5String (SIZE (1..200)),
770 * visibleString VisibleString (SIZE (1..200)),
771 * bmpString BMPString (SIZE (1..200)),
772 * utf8String UTF8String (SIZE (1..200)) }
773 *
774 * NOTE: we only parse and use anyPolicy without qualifiers at this point
775 * as defined in RFC 5280.
776 */
777static int x509_get_certificate_policies( unsigned char **p,
778 const unsigned char *end,
779 mbedtls_x509_sequence *certificate_policies )
780{
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300781 int ret, parse_ret = 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200782 size_t len;
783 mbedtls_asn1_buf *buf;
784 mbedtls_asn1_sequence *cur = certificate_policies;
785
786 /* Get main sequence tag */
787 ret = mbedtls_asn1_get_tag( p, end, &len,
788 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
789 if( ret != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100790 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200791
792 if( *p + len != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100793 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
794 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200795
796 /*
797 * Cannot be an empty sequence.
798 */
799 if( len == 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100800 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
801 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200802
803 while( *p < end )
804 {
805 mbedtls_x509_buf policy_oid;
806 const unsigned char *policy_end;
807
808 /*
809 * Get the policy sequence
810 */
811 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
812 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100813 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200814
815 policy_end = *p + len;
816
Ron Eldor08063792019-05-13 16:38:39 +0300817 if( ( ret = mbedtls_asn1_get_tag( p, policy_end, &len,
Ron Eldor74d9acc2019-03-21 14:00:03 +0200818 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100819 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200820
821 policy_oid.tag = MBEDTLS_ASN1_OID;
822 policy_oid.len = len;
823 policy_oid.p = *p;
824
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300825 /*
826 * Only AnyPolicy is currently supported when enforcing policy.
827 */
828 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_POLICY, &policy_oid ) != 0 )
829 {
830 /*
831 * Set the parsing return code but continue parsing, in case this
TRodziewicz3ecb92e2021-05-11 18:22:05 +0200832 * extension is critical.
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300833 */
834 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
835 }
836
Ron Eldor74d9acc2019-03-21 14:00:03 +0200837 /* Allocate and assign next pointer */
838 if( cur->buf.p != NULL )
839 {
840 if( cur->next != NULL )
841 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
842
843 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
844
845 if( cur->next == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100846 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
847 MBEDTLS_ERR_ASN1_ALLOC_FAILED ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200848
849 cur = cur->next;
850 }
851
852 buf = &( cur->buf );
853 buf->tag = policy_oid.tag;
854 buf->p = policy_oid.p;
855 buf->len = policy_oid.len;
Ron Eldor08063792019-05-13 16:38:39 +0300856
857 *p += len;
858
859 /*
860 * If there is an optional qualifier, then *p < policy_end
861 * Check the Qualifier len to verify it doesn't exceed policy_end.
862 */
863 if( *p < policy_end )
864 {
865 if( ( ret = mbedtls_asn1_get_tag( p, policy_end, &len,
866 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100867 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor08063792019-05-13 16:38:39 +0300868 /*
869 * Skip the optional policy qualifiers.
870 */
871 *p += len;
872 }
873
874 if( *p != policy_end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100875 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
876 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200877 }
878
879 /* Set final sequence entry's next pointer to NULL */
880 cur->next = NULL;
881
882 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100883 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
884 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200885
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300886 return( parse_ret );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200887}
888
889/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200890 * X.509 v3 extensions
891 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200892 */
893static int x509_get_crt_ext( unsigned char **p,
894 const unsigned char *end,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200895 mbedtls_x509_crt *crt,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200896 mbedtls_x509_crt_ext_cb_t cb,
897 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200898{
Janos Follath865b3eb2019-12-16 11:46:15 +0000899 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200900 size_t len;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200901 unsigned char *end_ext_data, *start_ext_octet, *end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200902
Hanno Becker12f62fb2019-02-12 17:22:36 +0000903 if( *p == end )
904 return( 0 );
905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906 if( ( ret = mbedtls_x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200907 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200908
Hanno Becker12f62fb2019-02-12 17:22:36 +0000909 end = crt->v3_ext.p + crt->v3_ext.len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200910 while( *p < end )
911 {
912 /*
913 * Extension ::= SEQUENCE {
914 * extnID OBJECT IDENTIFIER,
915 * critical BOOLEAN DEFAULT FALSE,
916 * extnValue OCTET STRING }
917 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918 mbedtls_x509_buf extn_oid = {0, 0, NULL};
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200919 int is_critical = 0; /* DEFAULT FALSE */
920 int ext_type = 0;
921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200922 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
923 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100924 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200925
926 end_ext_data = *p + len;
927
928 /* Get extension ID */
k-stachowiakdcae78a2018-06-28 16:32:54 +0200929 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &extn_oid.len,
k-stachowiak463928a2018-07-24 12:50:59 +0200930 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100931 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200932
k-stachowiak463928a2018-07-24 12:50:59 +0200933 extn_oid.tag = MBEDTLS_ASN1_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200934 extn_oid.p = *p;
935 *p += extn_oid.len;
936
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200937 /* Get optional critical */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938 if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
939 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
Chris Jones9f7a6932021-04-14 12:12:09 +0100940 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200941
942 /* Data should be octet string type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
944 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100945 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200946
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200947 start_ext_octet = *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200948 end_ext_octet = *p + len;
949
950 if( end_ext_octet != end_ext_data )
Chris Jones9f7a6932021-04-14 12:12:09 +0100951 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
952 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200953
954 /*
955 * Detect supported extensions
956 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200958
959 if( ret != 0 )
960 {
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200961 /* Give the callback (if any) a chance to handle the extension */
Nicola Di Lieto2c3a9172020-05-28 17:20:42 +0200962 if( cb != NULL )
963 {
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200964 ret = cb( p_ctx, crt, &extn_oid, is_critical, *p, end_ext_octet );
Nicola Di Lieto565b52b2020-05-29 22:46:56 +0200965 if( ret != 0 && is_critical )
966 return( ret );
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200967 *p = end_ext_octet;
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200968 continue;
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200969 }
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200970
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200971 /* No parser found, skip extension */
972 *p = end_ext_octet;
973
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200974 if( is_critical )
975 {
976 /* Data is marked as critical: fail */
Chris Jones9f7a6932021-04-14 12:12:09 +0100977 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
978 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200979 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200980 continue;
981 }
982
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100983 /* Forbid repeated extensions */
984 if( ( crt->ext_types & ext_type ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200985 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100986
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200987 crt->ext_types |= ext_type;
988
989 switch( ext_type )
990 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100991 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200992 /* Parse basic constraints */
993 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
994 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200995 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200996 break;
997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200998 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200999 /* Parse key usage */
1000 if( ( ret = x509_get_key_usage( p, end_ext_octet,
1001 &crt->key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001002 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001003 break;
1004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001005 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001006 /* Parse extended key usage */
1007 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
1008 &crt->ext_key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001009 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001010 break;
1011
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001012 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001013 /* Parse subject alt name */
1014 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
1015 &crt->subject_alt_names ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001016 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001017 break;
1018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001020 /* Parse netscape certificate type */
1021 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
1022 &crt->ns_cert_type ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001023 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001024 break;
1025
Ron Eldor74d9acc2019-03-21 14:00:03 +02001026 case MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES:
1027 /* Parse certificate policies type */
1028 if( ( ret = x509_get_certificate_policies( p, end_ext_octet,
1029 &crt->certificate_policies ) ) != 0 )
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001030 {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +02001031 /* Give the callback (if any) a chance to handle the extension
1032 * if it contains unsupported policies */
1033 if( ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE && cb != NULL &&
1034 cb( p_ctx, crt, &extn_oid, is_critical,
1035 start_ext_octet, end_ext_octet ) == 0 )
1036 break;
1037
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001038 if( is_critical )
1039 return( ret );
1040 else
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001041 /*
Ron Eldora2913912019-05-16 16:17:38 +03001042 * If MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE is returned, then we
1043 * cannot interpret or enforce the policy. However, it is up to
1044 * the user to choose how to enforce the policies,
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001045 * unless the extension is critical.
1046 */
1047 if( ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1048 return( ret );
1049 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001050 break;
1051
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001052 default:
Ron Eldordf48efa2019-04-08 13:28:24 +03001053 /*
1054 * If this is a non-critical extension, which the oid layer
1055 * supports, but there isn't an x509 parser for it,
1056 * skip the extension.
1057 */
Ron Eldordf48efa2019-04-08 13:28:24 +03001058 if( is_critical )
1059 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1060 else
Ron Eldordf48efa2019-04-08 13:28:24 +03001061 *p = end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001062 }
1063 }
1064
1065 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +01001066 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1067 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001068
1069 return( 0 );
1070}
1071
1072/*
1073 * Parse and fill a single X.509 certificate in DER format
1074 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001075static int x509_crt_parse_der_core( mbedtls_x509_crt *crt,
1076 const unsigned char *buf,
1077 size_t buflen,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001078 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001079 mbedtls_x509_crt_ext_cb_t cb,
1080 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001081{
Janos Follath865b3eb2019-12-16 11:46:15 +00001082 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001083 size_t len;
1084 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087 memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) );
1088 memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) );
1089 memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001090
1091 /*
1092 * Check for valid input
1093 */
1094 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001095 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001096
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001097 /* Use the original buffer until we figure out actual length. */
Janos Follathcc0e49d2016-02-17 14:34:12 +00001098 p = (unsigned char*) buf;
1099 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001100 end = p + len;
1101
1102 /*
1103 * Certificate ::= SEQUENCE {
1104 * tbsCertificate TBSCertificate,
1105 * signatureAlgorithm AlgorithmIdentifier,
1106 * signatureValue BIT STRING }
1107 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1109 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001111 mbedtls_x509_crt_free( crt );
1112 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001113 }
1114
Janos Follathcc0e49d2016-02-17 14:34:12 +00001115 end = crt_end = p + len;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001116 crt->raw.len = crt_end - buf;
1117 if( make_copy != 0 )
1118 {
1119 /* Create and populate a new buffer for the raw field. */
1120 crt->raw.p = p = mbedtls_calloc( 1, crt->raw.len );
1121 if( crt->raw.p == NULL )
1122 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
1123
1124 memcpy( crt->raw.p, buf, crt->raw.len );
1125 crt->own_buffer = 1;
1126
1127 p += crt->raw.len - len;
1128 end = crt_end = p + len;
1129 }
1130 else
1131 {
1132 crt->raw.p = (unsigned char*) buf;
1133 crt->own_buffer = 0;
1134 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001135
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001136 /*
1137 * TBSCertificate ::= SEQUENCE {
1138 */
1139 crt->tbs.p = p;
1140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1142 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001145 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001146 }
1147
1148 end = p + len;
1149 crt->tbs.len = end - crt->tbs.p;
1150
1151 /*
1152 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1153 *
1154 * CertificateSerialNumber ::= INTEGER
1155 *
1156 * signature AlgorithmIdentifier
1157 */
1158 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001159 ( ret = mbedtls_x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1160 ( ret = mbedtls_x509_get_alg( &p, end, &crt->sig_oid,
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001161 &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001164 return( ret );
1165 }
1166
Andres AG7ca4a032017-03-09 16:16:11 +00001167 if( crt->version < 0 || crt->version > 2 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001168 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001169 mbedtls_x509_crt_free( crt );
1170 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001171 }
1172
Andres AG7ca4a032017-03-09 16:16:11 +00001173 crt->version++;
1174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175 if( ( ret = mbedtls_x509_get_sig_alg( &crt->sig_oid, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02001176 &crt->sig_md, &crt->sig_pk,
1177 &crt->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001178 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001180 return( ret );
1181 }
1182
1183 /*
1184 * issuer Name
1185 */
1186 crt->issuer_raw.p = p;
1187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001188 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1189 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001190 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001192 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001193 }
1194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195 if( ( ret = mbedtls_x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001196 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001197 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001198 return( ret );
1199 }
1200
1201 crt->issuer_raw.len = p - crt->issuer_raw.p;
1202
1203 /*
1204 * Validity ::= SEQUENCE {
1205 * notBefore Time,
1206 * notAfter Time }
1207 *
1208 */
1209 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1210 &crt->valid_to ) ) != 0 )
1211 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001212 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001213 return( ret );
1214 }
1215
1216 /*
1217 * subject Name
1218 */
1219 crt->subject_raw.p = p;
1220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001221 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1222 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001223 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001224 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001225 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001226 }
1227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001228 if( len && ( ret = mbedtls_x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001229 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001230 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001231 return( ret );
1232 }
1233
1234 crt->subject_raw.len = p - crt->subject_raw.p;
1235
1236 /*
1237 * SubjectPublicKeyInfo
1238 */
Hanno Becker494dd7a2019-02-06 16:13:41 +00001239 crt->pk_raw.p = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001240 if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001241 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001242 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001243 return( ret );
1244 }
Hanno Becker494dd7a2019-02-06 16:13:41 +00001245 crt->pk_raw.len = p - crt->pk_raw.p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001246
1247 /*
1248 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1249 * -- If present, version shall be v2 or v3
1250 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1251 * -- If present, version shall be v2 or v3
1252 * extensions [3] EXPLICIT Extensions OPTIONAL
1253 * -- If present, version shall be v3
1254 */
1255 if( crt->version == 2 || crt->version == 3 )
1256 {
1257 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1258 if( ret != 0 )
1259 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001260 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001261 return( ret );
1262 }
1263 }
1264
1265 if( crt->version == 2 || crt->version == 3 )
1266 {
1267 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1268 if( ret != 0 )
1269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001270 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001271 return( ret );
1272 }
1273 }
1274
1275 if( crt->version == 3 )
Manuel Pégourié-Gonnarda252af72015-03-27 16:15:55 +01001276 {
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001277 ret = x509_get_crt_ext( &p, end, crt, cb, p_ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001278 if( ret != 0 )
1279 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001280 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001281 return( ret );
1282 }
1283 }
1284
1285 if( p != end )
1286 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001288 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT,
1289 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001290 }
1291
1292 end = crt_end;
1293
1294 /*
1295 * }
1296 * -- end of TBSCertificate
1297 *
1298 * signatureAlgorithm AlgorithmIdentifier,
1299 * signatureValue BIT STRING
1300 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301 if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001302 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001304 return( ret );
1305 }
1306
Manuel Pégourié-Gonnard1022fed2015-03-27 16:30:47 +01001307 if( crt->sig_oid.len != sig_oid2.len ||
1308 memcmp( crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len ) != 0 ||
Paul Elliottca17ebf2020-11-24 17:30:18 +00001309 sig_params1.tag != sig_params2.tag ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001310 sig_params1.len != sig_params2.len ||
Manuel Pégourié-Gonnard159c5242015-04-30 11:15:22 +02001311 ( sig_params1.len != 0 &&
1312 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314 mbedtls_x509_crt_free( crt );
1315 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001316 }
1317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318 if( ( ret = mbedtls_x509_get_sig( &p, end, &crt->sig ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001319 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001320 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001321 return( ret );
1322 }
1323
1324 if( p != end )
1325 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001327 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT,
1328 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001329 }
1330
1331 return( 0 );
1332}
1333
1334/*
1335 * Parse one X.509 certificate in DER format from a buffer and add them to a
1336 * chained list
1337 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001338static int mbedtls_x509_crt_parse_der_internal( mbedtls_x509_crt *chain,
1339 const unsigned char *buf,
1340 size_t buflen,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001341 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001342 mbedtls_x509_crt_ext_cb_t cb,
1343 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001344{
Janos Follath865b3eb2019-12-16 11:46:15 +00001345 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001346 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001347
1348 /*
1349 * Check for valid input
1350 */
1351 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001352 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001353
1354 while( crt->version != 0 && crt->next != NULL )
1355 {
1356 prev = crt;
1357 crt = crt->next;
1358 }
1359
1360 /*
1361 * Add new certificate on the end of the chain if needed.
1362 */
Paul Bakker66d5d072014-06-17 16:39:18 +02001363 if( crt->version != 0 && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001364 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001365 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001366
1367 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001368 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001369
1370 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001371 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001372 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001373 }
1374
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001375 ret = x509_crt_parse_der_core( crt, buf, buflen, make_copy, cb, p_ctx );
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001376 if( ret != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001377 {
1378 if( prev )
1379 prev->next = NULL;
1380
1381 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001382 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001383
1384 return( ret );
1385 }
1386
1387 return( 0 );
1388}
1389
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001390int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
1391 const unsigned char *buf,
1392 size_t buflen )
1393{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001394 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 0, NULL, NULL ) );
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001395}
1396
Nicola Di Lietofde98f72020-05-28 08:34:33 +02001397int mbedtls_x509_crt_parse_der_with_ext_cb( mbedtls_x509_crt *chain,
1398 const unsigned char *buf,
1399 size_t buflen,
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +02001400 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001401 mbedtls_x509_crt_ext_cb_t cb,
1402 void *p_ctx )
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001403{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001404 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, make_copy, cb, p_ctx ) );
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001405}
1406
1407int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
1408 const unsigned char *buf,
1409 size_t buflen )
1410{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001411 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 1, NULL, NULL ) );
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001412}
1413
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001414/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001415 * Parse one or more PEM certificates from a buffer and add them to the chained
1416 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001417 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001418int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain,
1419 const unsigned char *buf,
1420 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001421{
Janos Follath98e28a72016-05-31 14:03:54 +01001422#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001423 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001425#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001426
1427 /*
1428 * Check for valid input
1429 */
1430 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001431 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001432
1433 /*
1434 * Determine buffer content. Buffer contains either one DER certificate or
1435 * one or more PEM certificates.
1436 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001438 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001439 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1440 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001442 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444 if( buf_format == MBEDTLS_X509_FORMAT_DER )
1445 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Janos Follath98e28a72016-05-31 14:03:54 +01001446#else
1447 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
1448#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450#if defined(MBEDTLS_PEM_PARSE_C)
1451 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001452 {
Janos Follath865b3eb2019-12-16 11:46:15 +00001453 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001455
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001456 /* 1 rather than 0 since the terminating NULL byte is counted in */
1457 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001458 {
1459 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001460 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001461
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001462 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001464 "-----BEGIN CERTIFICATE-----",
1465 "-----END CERTIFICATE-----",
1466 buf, NULL, 0, &use_len );
1467
1468 if( ret == 0 )
1469 {
1470 /*
1471 * Was PEM encoded
1472 */
1473 buflen -= use_len;
1474 buf += use_len;
1475 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001476 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001477 {
1478 return( ret );
1479 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001480 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001481 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001482 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001483
1484 /*
1485 * PEM header and footer were found
1486 */
1487 buflen -= use_len;
1488 buf += use_len;
1489
1490 if( first_error == 0 )
1491 first_error = ret;
1492
Paul Bakker5a5fa922014-09-26 14:53:04 +02001493 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001494 continue;
1495 }
1496 else
1497 break;
1498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001501 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001502
1503 if( ret != 0 )
1504 {
1505 /*
1506 * Quit parsing on a memory error
1507 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001508 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001509 return( ret );
1510
1511 if( first_error == 0 )
1512 first_error = ret;
1513
1514 total_failed++;
1515 continue;
1516 }
1517
1518 success = 1;
1519 }
1520 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001521
1522 if( success )
1523 return( total_failed );
1524 else if( first_error )
1525 return( first_error );
1526 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001527 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Janos Follath98e28a72016-05-31 14:03:54 +01001528#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001529}
1530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001531#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001532/*
1533 * Load one or more certificates and add them to the chained list
1534 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001536{
Janos Follath865b3eb2019-12-16 11:46:15 +00001537 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001538 size_t n;
1539 unsigned char *buf;
1540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001541 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001542 return( ret );
1543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001544 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001545
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001546 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001548
1549 return( ret );
1550}
1551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001552int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001553{
1554 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001555#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001556 int w_ret;
1557 WCHAR szDir[MAX_PATH];
1558 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001559 char *p;
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001560 size_t len = strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001561
Paul Bakker9af723c2014-05-01 13:03:14 +02001562 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001563 HANDLE hFind;
1564
1565 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001567
Paul Bakker9af723c2014-05-01 13:03:14 +02001568 memset( szDir, 0, sizeof(szDir) );
1569 memset( filename, 0, MAX_PATH );
1570 memcpy( filename, path, len );
1571 filename[len++] = '\\';
1572 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001573 filename[len++] = '*';
1574
Simon B3c6b18d2016-11-03 01:11:37 +00001575 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001576 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001577 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001579
1580 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001581 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001583
1584 len = MAX_PATH - len;
1585 do
1586 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001587 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001588
1589 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1590 continue;
1591
Paul Bakker9af723c2014-05-01 13:03:14 +02001592 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001593 lstrlenW( file_data.cFileName ),
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001594 p, (int) len - 1,
Paul Bakker9af723c2014-05-01 13:03:14 +02001595 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001596 if( w_ret == 0 )
Ron Eldor36d90422017-01-09 15:09:16 +02001597 {
1598 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1599 goto cleanup;
1600 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001603 if( w_ret < 0 )
1604 ret++;
1605 else
1606 ret += w_ret;
1607 }
1608 while( FindNextFileW( hFind, &file_data ) != 0 );
1609
Paul Bakker66d5d072014-06-17 16:39:18 +02001610 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001611 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001612
Ron Eldor36d90422017-01-09 15:09:16 +02001613cleanup:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001614 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001615#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001616 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001617 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001618 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001619 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001620 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001621 DIR *dir = opendir( path );
1622
Paul Bakker66d5d072014-06-17 16:39:18 +02001623 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001624 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001625
Ron Eldor63140682017-01-09 19:27:59 +02001626#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001627 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001628 {
1629 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001630 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001631 }
Ron Eldor63140682017-01-09 19:27:59 +02001632#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001633
Paul Elliottfb91a482021-03-05 14:17:51 +00001634 memset( &sb, 0, sizeof( sb ) );
1635
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001636 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001637 {
Andres AGf9113192016-09-02 14:06:04 +01001638 snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name,
1639 "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001640
Andres AGf9113192016-09-02 14:06:04 +01001641 if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001642 {
Andres AGf9113192016-09-02 14:06:04 +01001643 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1644 goto cleanup;
1645 }
1646 else if( stat( entry_name, &sb ) == -1 )
1647 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001648 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001649 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001650 }
1651
1652 if( !S_ISREG( sb.st_mode ) )
1653 continue;
1654
1655 // Ignore parse errors
1656 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001657 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001658 if( t_ret < 0 )
1659 ret++;
1660 else
1661 ret += t_ret;
1662 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001663
1664cleanup:
Andres AGf9113192016-09-02 14:06:04 +01001665 closedir( dir );
1666
Ron Eldor63140682017-01-09 19:27:59 +02001667#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001668 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001669 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Ron Eldor63140682017-01-09 19:27:59 +02001670#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001671
Paul Bakkerbe089b02013-10-14 15:51:50 +02001672#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001673
1674 return( ret );
1675}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001676#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001677
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001678/*
1679 * OtherName ::= SEQUENCE {
1680 * type-id OBJECT IDENTIFIER,
1681 * value [0] EXPLICIT ANY DEFINED BY type-id }
1682 *
1683 * HardwareModuleName ::= SEQUENCE {
1684 * hwType OBJECT IDENTIFIER,
1685 * hwSerialNum OCTET STRING }
1686 *
1687 * NOTE: we currently only parse and use otherName of type HwModuleName,
1688 * as defined in RFC 4108.
1689 */
1690static int x509_get_other_name( const mbedtls_x509_buf *subject_alt_name,
1691 mbedtls_x509_san_other_name *other_name )
1692{
Janos Follathab23cd12019-05-09 13:53:57 +01001693 int ret = 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001694 size_t len;
1695 unsigned char *p = subject_alt_name->p;
1696 const unsigned char *end = p + subject_alt_name->len;
1697 mbedtls_x509_buf cur_oid;
1698
1699 if( ( subject_alt_name->tag &
1700 ( MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK ) ) !=
1701 ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ) )
1702 {
1703 /*
1704 * The given subject alternative name is not of type "othername".
1705 */
1706 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
1707 }
1708
1709 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1710 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001711 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001712
1713 cur_oid.tag = MBEDTLS_ASN1_OID;
1714 cur_oid.p = p;
1715 cur_oid.len = len;
1716
1717 /*
1718 * Only HwModuleName is currently supported.
1719 */
1720 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid ) != 0 )
1721 {
1722 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1723 }
1724
Ron Eldor890819a2019-05-13 19:03:04 +03001725 if( p + len >= end )
1726 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001727 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001728 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1729 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001730 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001731 p += len;
1732 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1733 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001734 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001735
1736 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1737 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001738 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001739
1740 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001741 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001742
1743 other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID;
1744 other_name->value.hardware_module_name.oid.p = p;
1745 other_name->value.hardware_module_name.oid.len = len;
1746
Ron Eldor890819a2019-05-13 19:03:04 +03001747 if( p + len >= end )
1748 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001749 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001750 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1751 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001752 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001753 p += len;
1754 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1755 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001756 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001757
1758 other_name->value.hardware_module_name.val.tag = MBEDTLS_ASN1_OCTET_STRING;
1759 other_name->value.hardware_module_name.val.p = p;
1760 other_name->value.hardware_module_name.val.len = len;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001761 p += len;
1762 if( p != end )
1763 {
Ron Eldor890819a2019-05-13 19:03:04 +03001764 mbedtls_platform_zeroize( other_name,
Sébastien Duquette661d7252019-06-23 17:45:26 -04001765 sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001766 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1767 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001768 }
1769 return( 0 );
1770}
1771
Peter Kolbus9a969b62018-12-11 13:55:56 -06001772int mbedtls_x509_parse_subject_alt_name( const mbedtls_x509_buf *san_buf,
1773 mbedtls_x509_subject_alternative_name *san )
1774{
1775 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1776 switch( san_buf->tag &
1777 ( MBEDTLS_ASN1_TAG_CLASS_MASK |
1778 MBEDTLS_ASN1_TAG_VALUE_MASK ) )
1779 {
1780 /*
1781 * otherName
1782 */
1783 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ):
1784 {
1785 mbedtls_x509_san_other_name other_name;
1786
1787 ret = x509_get_other_name( san_buf, &other_name );
1788 if( ret != 0 )
1789 return( ret );
1790
1791 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1792 san->type = MBEDTLS_X509_SAN_OTHER_NAME;
1793 memcpy( &san->san.other_name,
1794 &other_name, sizeof( other_name ) );
1795
1796 }
1797 break;
1798
1799 /*
1800 * dNSName
1801 */
1802 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME ):
1803 {
1804 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1805 san->type = MBEDTLS_X509_SAN_DNS_NAME;
1806
1807 memcpy( &san->san.unstructured_name,
1808 san_buf, sizeof( *san_buf ) );
1809
1810 }
1811 break;
1812
1813 /*
1814 * Type not supported
1815 */
1816 default:
1817 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1818 }
1819 return( 0 );
1820}
1821
1822#if !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001823static int x509_info_subject_alt_name( char **buf, size_t *size,
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001824 const mbedtls_x509_sequence
1825 *subject_alt_name,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001826 const char *prefix )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001827{
Janos Follath865b3eb2019-12-16 11:46:15 +00001828 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001829 size_t n = *size;
1830 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001831 const mbedtls_x509_sequence *cur = subject_alt_name;
Ron Eldor890819a2019-05-13 19:03:04 +03001832 mbedtls_x509_subject_alternative_name san;
1833 int parse_ret;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001834
1835 while( cur != NULL )
1836 {
Ron Eldor890819a2019-05-13 19:03:04 +03001837 memset( &san, 0, sizeof( san ) );
1838 parse_ret = mbedtls_x509_parse_subject_alt_name( &cur->buf, &san );
1839 if( parse_ret != 0 )
1840 {
1841 if( parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1842 {
1843 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
1844 MBEDTLS_X509_SAFE_SNPRINTF;
1845 }
1846 else
1847 {
1848 ret = mbedtls_snprintf( p, n, "\n%s <malformed>", prefix );
1849 MBEDTLS_X509_SAFE_SNPRINTF;
1850 }
1851 cur = cur->next;
1852 continue;
1853 }
1854
1855 switch( san.type )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001856 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001857 /*
1858 * otherName
1859 */
Ron Eldora2913912019-05-16 16:17:38 +03001860 case MBEDTLS_X509_SAN_OTHER_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001861 {
1862 mbedtls_x509_san_other_name *other_name = &san.san.other_name;
1863
1864 ret = mbedtls_snprintf( p, n, "\n%s otherName :", prefix );
1865 MBEDTLS_X509_SAFE_SNPRINTF;
1866
1867 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME,
1868 &other_name->value.hardware_module_name.oid ) != 0 )
Janos Follath22f605f2019-05-10 10:37:17 +01001869 {
Ron Eldor890819a2019-05-13 19:03:04 +03001870 ret = mbedtls_snprintf( p, n, "\n%s hardware module name :", prefix );
1871 MBEDTLS_X509_SAFE_SNPRINTF;
1872 ret = mbedtls_snprintf( p, n, "\n%s hardware type : ", prefix );
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001873 MBEDTLS_X509_SAFE_SNPRINTF;
1874
Ron Eldor890819a2019-05-13 19:03:04 +03001875 ret = mbedtls_oid_get_numeric_string( p, n, &other_name->value.hardware_module_name.oid );
1876 MBEDTLS_X509_SAFE_SNPRINTF;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001877
Ron Eldor890819a2019-05-13 19:03:04 +03001878 ret = mbedtls_snprintf( p, n, "\n%s hardware serial number : ", prefix );
1879 MBEDTLS_X509_SAFE_SNPRINTF;
1880
1881 if( other_name->value.hardware_module_name.val.len >= n )
1882 {
1883 *p = '\0';
1884 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Janos Follath22f605f2019-05-10 10:37:17 +01001885 }
1886
Ron Eldora2913912019-05-16 16:17:38 +03001887 memcpy( p, other_name->value.hardware_module_name.val.p,
1888 other_name->value.hardware_module_name.val.len );
1889 p += other_name->value.hardware_module_name.val.len;
1890
Ron Eldor890819a2019-05-13 19:03:04 +03001891 n -= other_name->value.hardware_module_name.val.len;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001892
Ron Eldor890819a2019-05-13 19:03:04 +03001893 }/* MBEDTLS_OID_ON_HW_MODULE_NAME */
1894 }
1895 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001896
1897 /*
1898 * dNSName
1899 */
Ron Eldora2913912019-05-16 16:17:38 +03001900 case MBEDTLS_X509_SAN_DNS_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001901 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001902 ret = mbedtls_snprintf( p, n, "\n%s dNSName : ", prefix );
1903 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldor890819a2019-05-13 19:03:04 +03001904 if( san.san.unstructured_name.len >= n )
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001905 {
1906 *p = '\0';
1907 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
1908 }
Ron Eldora2913912019-05-16 16:17:38 +03001909
1910 memcpy( p, san.san.unstructured_name.p, san.san.unstructured_name.len );
1911 p += san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001912 n -= san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001913 }
1914 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001915
1916 /*
Ron Eldor890819a2019-05-13 19:03:04 +03001917 * Type not supported, skip item.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001918 */
1919 default:
Janos Follath22f605f2019-05-10 10:37:17 +01001920 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
1921 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001922 break;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001923 }
1924
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001925 cur = cur->next;
1926 }
1927
1928 *p = '\0';
1929
1930 *size = n;
1931 *buf = p;
1932
1933 return( 0 );
1934}
1935
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001936#define PRINT_ITEM(i) \
1937 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001938 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001939 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001940 sep = ", "; \
1941 }
1942
1943#define CERT_TYPE(type,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001944 if( ns_cert_type & (type) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001945 PRINT_ITEM( name );
1946
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001947static int x509_info_cert_type( char **buf, size_t *size,
1948 unsigned char ns_cert_type )
1949{
Janos Follath865b3eb2019-12-16 11:46:15 +00001950 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001951 size_t n = *size;
1952 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001953 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001955 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001956 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001957 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
1958 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1959 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001960 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
1961 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1962 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001963
1964 *size = n;
1965 *buf = p;
1966
1967 return( 0 );
1968}
1969
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001970#define KEY_USAGE(code,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001971 if( key_usage & (code) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001972 PRINT_ITEM( name );
1973
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001974static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001975 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001976{
Janos Follath865b3eb2019-12-16 11:46:15 +00001977 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001978 size_t n = *size;
1979 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001980 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001982 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
1983 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001984 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1985 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1986 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001987 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
1988 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001989 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
1990 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001991
1992 *size = n;
1993 *buf = p;
1994
1995 return( 0 );
1996}
1997
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001998static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001999 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002000{
Janos Follath865b3eb2019-12-16 11:46:15 +00002001 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002002 const char *desc;
2003 size_t n = *size;
2004 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002005 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002006 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002007
2008 while( cur != NULL )
2009 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002010 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002011 desc = "???";
2012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002013 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002014 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002015
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002016 sep = ", ";
2017
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002018 cur = cur->next;
2019 }
2020
2021 *size = n;
2022 *buf = p;
2023
2024 return( 0 );
2025}
2026
Ron Eldor74d9acc2019-03-21 14:00:03 +02002027static int x509_info_cert_policies( char **buf, size_t *size,
2028 const mbedtls_x509_sequence *certificate_policies )
2029{
Janos Follath865b3eb2019-12-16 11:46:15 +00002030 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor74d9acc2019-03-21 14:00:03 +02002031 const char *desc;
2032 size_t n = *size;
2033 char *p = *buf;
2034 const mbedtls_x509_sequence *cur = certificate_policies;
2035 const char *sep = "";
2036
2037 while( cur != NULL )
2038 {
2039 if( mbedtls_oid_get_certificate_policies( &cur->buf, &desc ) != 0 )
2040 desc = "???";
2041
2042 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
2043 MBEDTLS_X509_SAFE_SNPRINTF;
2044
2045 sep = ", ";
2046
2047 cur = cur->next;
2048 }
2049
2050 *size = n;
2051 *buf = p;
2052
2053 return( 0 );
2054}
2055
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002056/*
2057 * Return an informational string about the certificate.
2058 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002059#define BEFORE_COLON 18
2060#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002061int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
2062 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002063{
Janos Follath865b3eb2019-12-16 11:46:15 +00002064 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002065 size_t n;
2066 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002067 char key_size_str[BEFORE_COLON];
2068
2069 p = buf;
2070 n = size;
2071
Janos Follath98e28a72016-05-31 14:03:54 +01002072 if( NULL == crt )
2073 {
2074 ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" );
2075 MBEDTLS_X509_SAFE_SNPRINTF;
2076
2077 return( (int) ( size - n ) );
2078 }
2079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002080 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002081 prefix, crt->version );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002082 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002083 ret = mbedtls_snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002084 prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002085 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002087 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002088 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002090 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002091 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002092 ret = mbedtls_x509_dn_gets( p, n, &crt->issuer );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002093 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002095 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002096 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002097 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002098 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002100 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002101 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2102 crt->valid_from.year, crt->valid_from.mon,
2103 crt->valid_from.day, crt->valid_from.hour,
2104 crt->valid_from.min, crt->valid_from.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002105 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002107 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002108 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2109 crt->valid_to.year, crt->valid_to.mon,
2110 crt->valid_to.day, crt->valid_to.hour,
2111 crt->valid_to.min, crt->valid_to.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002112 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002114 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002115 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002117 ret = mbedtls_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02002118 crt->sig_md, crt->sig_opts );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002119 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002120
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002121 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002122 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
2123 mbedtls_pk_get_name( &crt->pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002124 {
2125 return( ret );
2126 }
2127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002128 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +02002129 (int) mbedtls_pk_get_bitlen( &crt->pk ) );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002130 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002131
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002132 /*
2133 * Optional extensions
2134 */
2135
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002136 if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002137 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002138 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002139 crt->ca_istrue ? "true" : "false" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002140 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002141
2142 if( crt->max_pathlen > 0 )
2143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002144 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002145 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002146 }
2147 }
2148
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002149 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002150 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002151 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name :", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002152 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002153
2154 if( ( ret = x509_info_subject_alt_name( &p, &n,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002155 &crt->subject_alt_names,
Ron Eldor6aeae9e2019-05-20 12:00:36 +03002156 prefix ) ) != 0 )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002157 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002158 }
2159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002160 if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002161 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002162 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002163 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002164
2165 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
2166 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002167 }
2168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002169 if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002171 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002172 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002173
2174 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
2175 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002176 }
2177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002178 if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002179 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002180 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002181 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002182
2183 if( ( ret = x509_info_ext_key_usage( &p, &n,
2184 &crt->ext_key_usage ) ) != 0 )
2185 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002186 }
2187
Ron Eldor74d9acc2019-03-21 14:00:03 +02002188 if( crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES )
2189 {
2190 ret = mbedtls_snprintf( p, n, "\n%scertificate policies : ", prefix );
2191 MBEDTLS_X509_SAFE_SNPRINTF;
2192
2193 if( ( ret = x509_info_cert_policies( &p, &n,
2194 &crt->certificate_policies ) ) != 0 )
2195 return( ret );
2196 }
2197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002198 ret = mbedtls_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002199 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002200
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002201 return( (int) ( size - n ) );
2202}
2203
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002204struct x509_crt_verify_string {
2205 int code;
2206 const char *string;
2207};
2208
Hanno Becker7ac83f92020-10-09 10:48:22 +01002209#define X509_CRT_ERROR_INFO( err, err_str, info ) { err, info },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002210static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Hanno Becker7ac83f92020-10-09 10:48:22 +01002211 MBEDTLS_X509_CRT_ERROR_INFO_LIST
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002212 { 0, NULL }
2213};
Hanno Becker7ac83f92020-10-09 10:48:22 +01002214#undef X509_CRT_ERROR_INFO
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002215
2216int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002217 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002218{
Janos Follath865b3eb2019-12-16 11:46:15 +00002219 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002220 const struct x509_crt_verify_string *cur;
2221 char *p = buf;
2222 size_t n = size;
2223
2224 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
2225 {
2226 if( ( flags & cur->code ) == 0 )
2227 continue;
2228
2229 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002230 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002231 flags ^= cur->code;
2232 }
2233
2234 if( flags != 0 )
2235 {
2236 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
2237 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002238 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002239 }
2240
2241 return( (int) ( size - n ) );
2242}
Hanno Becker612a2f12020-10-09 09:19:39 +01002243#endif /* MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002244
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002245int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
2246 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002247{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002248 unsigned int usage_must, usage_may;
2249 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
2250 | MBEDTLS_X509_KU_DECIPHER_ONLY;
2251
2252 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
2253 return( 0 );
2254
2255 usage_must = usage & ~may_mask;
2256
2257 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
2258 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
2259
2260 usage_may = usage & may_mask;
2261
2262 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002263 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002264
2265 return( 0 );
2266}
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002268int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002269 const char *usage_oid,
2270 size_t usage_len )
2271{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002272 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002273
2274 /* Extension is not mandatory, absent means no restriction */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002275 if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002276 return( 0 );
2277
2278 /*
2279 * Look for the requested usage (or wildcard ANY) in our list
2280 */
2281 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
2282 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002283 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002284
2285 if( cur_oid->len == usage_len &&
2286 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
2287 {
2288 return( 0 );
2289 }
2290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002291 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002292 return( 0 );
2293 }
2294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002295 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002296}
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002298#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002299/*
2300 * Return 1 if the certificate is revoked, or 0 otherwise.
2301 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002302int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002303{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002304 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002305
2306 while( cur != NULL && cur->serial.len != 0 )
2307 {
2308 if( crt->serial.len == cur->serial.len &&
2309 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
2310 {
Raoul Strackxa4e86142020-06-15 17:03:13 +02002311 return( 1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002312 }
2313
2314 cur = cur->next;
2315 }
2316
2317 return( 0 );
2318}
2319
2320/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002321 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002322 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002323 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002324static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002325 mbedtls_x509_crl *crl_list,
2326 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002327{
2328 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002329 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2330 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002331
2332 if( ca == NULL )
2333 return( flags );
2334
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002335 while( crl_list != NULL )
2336 {
2337 if( crl_list->version == 0 ||
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002338 x509_name_cmp( &crl_list->issuer, &ca->subject ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002339 {
2340 crl_list = crl_list->next;
2341 continue;
2342 }
2343
2344 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002345 * Check if the CA is configured to sign CRLs
2346 */
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002347 if( mbedtls_x509_crt_check_key_usage( ca,
2348 MBEDTLS_X509_KU_CRL_SIGN ) != 0 )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002350 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002351 break;
2352 }
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002353
2354 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002355 * Check if CRL is correctly signed by the trusted CA
2356 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002357 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
2358 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
2359
2360 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
2361 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
2362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002363 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002364 if( mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002365 {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002366 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002367 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002368 break;
2369 }
2370
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002371 if( x509_profile_check_key( profile, &ca->pk ) != 0 )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002372 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002374 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
2375 crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02002376 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002377 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002378 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002379 break;
2380 }
2381
2382 /*
2383 * Check for validity of CRL (Do not drop out)
2384 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002385 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002386 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002387
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002388 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002389 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002390
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002391 /*
2392 * Check if certificate is revoked
2393 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002394 if( mbedtls_x509_crt_is_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002395 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002396 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002397 break;
2398 }
2399
2400 crl_list = crl_list->next;
2401 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002402
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002403 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002404}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002405#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002406
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002407/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002408 * Check the signature of a certificate by its parent
2409 */
2410static int x509_crt_check_signature( const mbedtls_x509_crt *child,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002411 mbedtls_x509_crt *parent,
2412 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002413{
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002414 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002415 size_t hash_len;
2416#if !defined(MBEDTLS_USE_PSA_CRYPTO)
2417 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002418 md_info = mbedtls_md_info_from_type( child->sig_md );
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002419 hash_len = mbedtls_md_get_size( md_info );
Andrzej Kurek8b38ff52018-11-20 03:20:09 -05002420
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002421 /* Note: hash errors can happen only after an internal error */
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002422 if( mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash ) != 0 )
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002423 return( -1 );
2424#else
Jaeden Amero34973232019-02-20 10:32:28 +00002425 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002426 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( child->sig_md );
2427
2428 if( psa_hash_setup( &hash_operation, hash_alg ) != PSA_SUCCESS )
2429 return( -1 );
2430
2431 if( psa_hash_update( &hash_operation, child->tbs.p, child->tbs.len )
2432 != PSA_SUCCESS )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002433 {
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002434 return( -1 );
2435 }
2436
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002437 if( psa_hash_finish( &hash_operation, hash, sizeof( hash ), &hash_len )
2438 != PSA_SUCCESS )
2439 {
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002440 return( -1 );
2441 }
2442#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002443 /* Skip expensive computation on obvious mismatch */
2444 if( ! mbedtls_pk_can_do( &parent->pk, child->sig_pk ) )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002445 return( -1 );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002446
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002447#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002448 if( rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA )
2449 {
2450 return( mbedtls_pk_verify_restartable( &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002451 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002452 child->sig.p, child->sig.len, &rs_ctx->pk ) );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002453 }
2454#else
2455 (void) rs_ctx;
2456#endif
2457
2458 return( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002459 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002460 child->sig.p, child->sig.len ) );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002461}
2462
2463/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002464 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2465 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002466 *
2467 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002468 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002469static int x509_crt_check_parent( const mbedtls_x509_crt *child,
2470 const mbedtls_x509_crt *parent,
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002471 int top )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002472{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002473 int need_ca_bit;
2474
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002475 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02002476 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002477 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002478
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002479 /* Parent must have the basicConstraints CA bit set as a general rule */
2480 need_ca_bit = 1;
2481
2482 /* Exception: v1/v2 certificates that are locally trusted. */
2483 if( top && parent->version < 3 )
2484 need_ca_bit = 0;
2485
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002486 if( need_ca_bit && ! parent->ca_istrue )
2487 return( -1 );
2488
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002489 if( need_ca_bit &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002490 mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002491 {
2492 return( -1 );
2493 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002494
2495 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002496}
2497
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002498/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002499 * Find a suitable parent for child in candidates, or return NULL.
2500 *
2501 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002502 * 1. subject name matches child's issuer
2503 * 2. if necessary, the CA bit is set and key usage allows signing certs
2504 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002505 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002506 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002507 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002508 * If there's a suitable candidate which is also time-valid, return the first
2509 * such. Otherwise, return the first suitable candidate (or NULL if there is
2510 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002511 *
2512 * The rationale for this rule is that someone could have a list of trusted
2513 * roots with two versions on the same root with different validity periods.
2514 * (At least one user reported having such a list and wanted it to just work.)
2515 * The reason we don't just require time-validity is that generally there is
2516 * only one version, and if it's expired we want the flags to state that
2517 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002518 *
2519 * The rationale for rule 3 (signature for trusted roots) is that users might
2520 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002521 * way we select the correct one is by checking the signature (as we don't
2522 * rely on key identifier extensions). (This is one way users might choose to
2523 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002524 *
2525 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002526 * - [in] child: certificate for which we're looking for a parent
2527 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002528 * - [out] r_parent: parent found (or NULL)
2529 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002530 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2531 * of the chain, 0 otherwise
2532 * - [in] path_cnt: number of intermediates seen so far
2533 * - [in] self_cnt: number of self-signed intermediates seen so far
2534 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002535 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002536 *
2537 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002538 * - 0 on success
2539 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002540 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002541static int x509_crt_find_parent_in(
2542 mbedtls_x509_crt *child,
2543 mbedtls_x509_crt *candidates,
2544 mbedtls_x509_crt **r_parent,
2545 int *r_signature_is_good,
2546 int top,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002547 unsigned path_cnt,
2548 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002549 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002550{
Janos Follath865b3eb2019-12-16 11:46:15 +00002551 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002552 mbedtls_x509_crt *parent, *fallback_parent;
Benjamin Kier36050732019-05-30 14:49:17 -04002553 int signature_is_good = 0, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002554
2555#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002556 /* did we have something in progress? */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002557 if( rs_ctx != NULL && rs_ctx->parent != NULL )
2558 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002559 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002560 parent = rs_ctx->parent;
2561 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002562 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002563
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002564 /* clear saved state */
2565 rs_ctx->parent = NULL;
2566 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002567 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002568
2569 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002570 goto check_signature;
2571 }
2572#endif
2573
2574 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002575 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002576
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002577 for( parent = candidates; parent != NULL; parent = parent->next )
2578 {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002579 /* basic parenting skills (name, CA bit, key usage) */
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002580 if( x509_crt_check_parent( child, parent, top ) != 0 )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002581 continue;
2582
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002583 /* +1 because stored max_pathlen is 1 higher that the actual value */
2584 if( parent->max_pathlen > 0 &&
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002585 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt )
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002586 {
2587 continue;
2588 }
2589
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002590 /* Signature */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002591#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2592check_signature:
2593#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002594 ret = x509_crt_check_signature( child, parent, rs_ctx );
2595
2596#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002597 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2598 {
2599 /* save state */
2600 rs_ctx->parent = parent;
2601 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002602 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002603
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002604 return( ret );
2605 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002606#else
2607 (void) ret;
2608#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002609
2610 signature_is_good = ret == 0;
2611 if( top && ! signature_is_good )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002612 continue;
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002613
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002614 /* optional time check */
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002615 if( mbedtls_x509_time_is_past( &parent->valid_to ) ||
2616 mbedtls_x509_time_is_future( &parent->valid_from ) )
2617 {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002618 if( fallback_parent == NULL )
2619 {
2620 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002621 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002622 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002623
2624 continue;
2625 }
2626
Andy Gross1f627142019-01-30 10:25:53 -06002627 *r_parent = parent;
2628 *r_signature_is_good = signature_is_good;
2629
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002630 break;
2631 }
2632
Andy Gross1f627142019-01-30 10:25:53 -06002633 if( parent == NULL )
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002634 {
2635 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002636 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002637 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002638
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002639 return( 0 );
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002640}
2641
2642/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002643 * Find a parent in trusted CAs or the provided chain, or return NULL.
2644 *
2645 * Searches in trusted CAs first, and return the first suitable parent found
2646 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002647 *
2648 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002649 * - [in] child: certificate for which we're looking for a parent, followed
2650 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002651 * - [in] trust_ca: list of locally trusted certificates
2652 * - [out] parent: parent found (or NULL)
2653 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2654 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2655 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2656 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002657 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002658 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002659 *
2660 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002661 * - 0 on success
2662 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002663 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002664static int x509_crt_find_parent(
2665 mbedtls_x509_crt *child,
2666 mbedtls_x509_crt *trust_ca,
2667 mbedtls_x509_crt **parent,
2668 int *parent_is_trusted,
2669 int *signature_is_good,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002670 unsigned path_cnt,
2671 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002672 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002673{
Janos Follath865b3eb2019-12-16 11:46:15 +00002674 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002675 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002676
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002677 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002678
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002679#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002680 /* restore then clear saved state if we have some stored */
2681 if( rs_ctx != NULL && rs_ctx->parent_is_trusted != -1 )
2682 {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002683 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002684 rs_ctx->parent_is_trusted = -1;
2685 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002686#endif
2687
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002688 while( 1 ) {
2689 search_list = *parent_is_trusted ? trust_ca : child->next;
2690
2691 ret = x509_crt_find_parent_in( child, search_list,
2692 parent, signature_is_good,
2693 *parent_is_trusted,
2694 path_cnt, self_cnt, rs_ctx );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002695
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002696#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002697 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2698 {
2699 /* save state */
2700 rs_ctx->parent_is_trusted = *parent_is_trusted;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002701 return( ret );
2702 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002703#else
2704 (void) ret;
2705#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002706
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002707 /* stop here if found or already in second iteration */
2708 if( *parent != NULL || *parent_is_trusted == 0 )
2709 break;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002710
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002711 /* prepare second iteration */
2712 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002713 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002714
2715 /* extra precaution against mistakes in the caller */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002716 if( *parent == NULL )
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002717 {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002718 *parent_is_trusted = 0;
2719 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002720 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002721
2722 return( 0 );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002723}
2724
2725/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002726 * Check if an end-entity certificate is locally trusted
2727 *
2728 * Currently we require such certificates to be self-signed (actually only
2729 * check for self-issued as self-signatures are not checked)
2730 */
2731static int x509_crt_check_ee_locally_trusted(
2732 mbedtls_x509_crt *crt,
2733 mbedtls_x509_crt *trust_ca )
2734{
2735 mbedtls_x509_crt *cur;
2736
2737 /* must be self-issued */
2738 if( x509_name_cmp( &crt->issuer, &crt->subject ) != 0 )
2739 return( -1 );
2740
2741 /* look for an exact match with trusted cert */
2742 for( cur = trust_ca; cur != NULL; cur = cur->next )
2743 {
2744 if( crt->raw.len == cur->raw.len &&
2745 memcmp( crt->raw.p, cur->raw.p, crt->raw.len ) == 0 )
2746 {
2747 return( 0 );
2748 }
2749 }
2750
2751 /* too bad */
2752 return( -1 );
2753}
2754
2755/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002756 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002757 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002758 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2759 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002760 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002761 * such that every cert in the chain is a child of the next one,
2762 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002763 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002764 * Verify that chain and return it with flags for all issues found.
2765 *
2766 * Special cases:
2767 * - EE == Rj -> return a one-element list containing it
2768 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2769 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002770 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002771 * Tests for (aspects of) this function should include at least:
2772 * - trusted EE
2773 * - EE -> trusted root
Antonin Décimo36e89b52019-01-23 15:24:37 +01002774 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002775 * - if relevant: EE untrusted
2776 * - if relevant: EE -> intermediate, untrusted
2777 * with the aspect under test checked at each relevant level (EE, int, root).
2778 * For some aspects longer chains are required, but usually length 2 is
2779 * enough (but length 1 is not in general).
2780 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002781 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002782 * - [in] crt: the cert list EE, C1, ..., Cn
2783 * - [in] trust_ca: the trusted list R1, ..., Rp
2784 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002785 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002786 * Only valid when return value is 0, may contain garbage otherwise!
2787 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002788 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002789 *
2790 * Return value:
2791 * - non-zero if the chain could not be fully built and examined
2792 * - 0 is the chain was successfully built and examined,
2793 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002794 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002795static int x509_crt_verify_chain(
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002796 mbedtls_x509_crt *crt,
2797 mbedtls_x509_crt *trust_ca,
2798 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00002799 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2800 void *p_ca_cb,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002801 const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002802 mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002803 mbedtls_x509_crt_restart_ctx *rs_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002804{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002805 /* Don't initialize any of those variables here, so that the compiler can
2806 * catch potential issues with jumping ahead when restarting */
Janos Follath865b3eb2019-12-16 11:46:15 +00002807 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002808 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002809 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002810 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002811 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002812 int parent_is_trusted;
2813 int child_is_trusted;
2814 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002815 unsigned self_cnt;
Hanno Beckerf53893b2019-03-28 13:45:55 +00002816 mbedtls_x509_crt *cur_trust_ca = NULL;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002817
2818#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2819 /* resume if we had an operation in progress */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002820 if( rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent )
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002821 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002822 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002823 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002824 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002825
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002826 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002827 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002828 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002829 flags = &cur->flags;
2830
2831 goto find_parent;
2832 }
2833#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002834
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002835 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002836 self_cnt = 0;
2837 parent_is_trusted = 0;
2838 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002839
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002840 while( 1 ) {
2841 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002842 cur = &ver_chain->items[ver_chain->len];
2843 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002844 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002845 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002846 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002847
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002848 /* Check time-validity (all certificates) */
2849 if( mbedtls_x509_time_is_past( &child->valid_to ) )
2850 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002851
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002852 if( mbedtls_x509_time_is_future( &child->valid_from ) )
2853 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002854
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002855 /* Stop here for trusted roots (but not for trusted EE certs) */
2856 if( child_is_trusted )
2857 return( 0 );
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002858
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002859 /* Check signature algorithm: MD & PK algs */
2860 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
2861 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002862
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002863 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
2864 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002865
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002866 /* Special case: EE certs that are locally trusted */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002867 if( ver_chain->len == 1 &&
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002868 x509_crt_check_ee_locally_trusted( child, trust_ca ) == 0 )
2869 {
2870 return( 0 );
2871 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002872
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002873#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2874find_parent:
2875#endif
Hanno Beckerf53893b2019-03-28 13:45:55 +00002876
2877 /* Obtain list of potential trusted signers from CA callback,
2878 * or use statically provided list. */
2879#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
2880 if( f_ca_cb != NULL )
2881 {
2882 mbedtls_x509_crt_free( ver_chain->trust_ca_cb_result );
2883 mbedtls_free( ver_chain->trust_ca_cb_result );
2884 ver_chain->trust_ca_cb_result = NULL;
2885
2886 ret = f_ca_cb( p_ca_cb, child, &ver_chain->trust_ca_cb_result );
2887 if( ret != 0 )
2888 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2889
2890 cur_trust_ca = ver_chain->trust_ca_cb_result;
2891 }
2892 else
2893#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2894 {
2895 ((void) f_ca_cb);
2896 ((void) p_ca_cb);
2897 cur_trust_ca = trust_ca;
2898 }
2899
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002900 /* Look for a parent in trusted CAs or up the chain */
Hanno Beckerf53893b2019-03-28 13:45:55 +00002901 ret = x509_crt_find_parent( child, cur_trust_ca, &parent,
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002902 &parent_is_trusted, &signature_is_good,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002903 ver_chain->len - 1, self_cnt, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002904
2905#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002906 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2907 {
2908 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002909 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002910 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002911 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002912
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002913 return( ret );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002914 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002915#else
2916 (void) ret;
2917#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002918
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002919 /* No parent? We're done here */
2920 if( parent == NULL )
2921 {
2922 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2923 return( 0 );
2924 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002925
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002926 /* Count intermediate self-issued (not necessarily self-signed) certs.
2927 * These can occur with some strategies for key rollover, see [SIRO],
2928 * and should be excluded from max_pathlen checks. */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002929 if( ver_chain->len != 1 &&
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002930 x509_name_cmp( &child->issuer, &child->subject ) == 0 )
2931 {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002932 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002933 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002934
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002935 /* path_cnt is 0 for the first intermediate CA,
2936 * and if parent is trusted it's not an intermediate CA */
2937 if( ! parent_is_trusted &&
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002938 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002939 {
2940 /* return immediately to avoid overflow the chain array */
2941 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2942 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002943
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002944 /* signature was checked while searching parent */
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002945 if( ! signature_is_good )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002946 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2947
2948 /* check size of signing key */
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002949 if( x509_profile_check_key( profile, &parent->pk ) != 0 )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002950 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002952#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002953 /* Check trusted CA's CRL for the given crt */
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002954 *flags |= x509_crt_verifycrl( child, parent, ca_crl, profile );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002955#else
2956 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002957#endif
2958
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002959 /* prepare for next iteration */
2960 child = parent;
2961 parent = NULL;
2962 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002963 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002964 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002965}
2966
2967/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002968 * Check for CN match
2969 */
2970static int x509_crt_check_cn( const mbedtls_x509_buf *name,
2971 const char *cn, size_t cn_len )
2972{
2973 /* try exact match */
2974 if( name->len == cn_len &&
2975 x509_memcasecmp( cn, name->p, cn_len ) == 0 )
2976 {
2977 return( 0 );
2978 }
2979
2980 /* try wildcard match */
Manuel Pégourié-Gonnard900fba62017-10-18 14:28:11 +02002981 if( x509_check_wildcard( cn, name ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002982 {
2983 return( 0 );
2984 }
2985
2986 return( -1 );
2987}
2988
2989/*
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002990 * Check for SAN match, see RFC 5280 Section 4.2.1.6
2991 */
2992static int x509_crt_check_san( const mbedtls_x509_buf *name,
2993 const char *cn, size_t cn_len )
2994{
2995 const unsigned char san_type = (unsigned char) name->tag &
2996 MBEDTLS_ASN1_TAG_VALUE_MASK;
2997
2998 /* dNSName */
2999 if( san_type == MBEDTLS_X509_SAN_DNS_NAME )
3000 return( x509_crt_check_cn( name, cn, cn_len ) );
3001
3002 /* (We may handle other types here later.) */
3003
3004 /* Unrecognized type */
3005 return( -1 );
3006}
3007
3008/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003009 * Verify the requested CN - only call this if cn is not NULL!
3010 */
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003011static void x509_crt_verify_name( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003012 const char *cn,
3013 uint32_t *flags )
3014{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003015 const mbedtls_x509_name *name;
3016 const mbedtls_x509_sequence *cur;
3017 size_t cn_len = strlen( cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003018
3019 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
3020 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003021 for( cur = &crt->subject_alt_names; cur != NULL; cur = cur->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003022 {
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003023 if( x509_crt_check_san( &cur->buf, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003024 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003025 }
3026
3027 if( cur == NULL )
3028 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3029 }
3030 else
3031 {
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02003032 for( name = &crt->subject; name != NULL; name = name->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003033 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003034 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, &name->oid ) == 0 &&
3035 x509_crt_check_cn( &name->val, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003036 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003037 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003038 }
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003039 }
3040
3041 if( name == NULL )
3042 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3043 }
3044}
3045
3046/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003047 * Merge the flags for all certs in the chain, after calling callback
3048 */
3049static int x509_crt_merge_flags_with_cb(
3050 uint32_t *flags,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003051 const mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003052 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3053 void *p_vrfy )
3054{
Janos Follath865b3eb2019-12-16 11:46:15 +00003055 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003056 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003057 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003058 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003059
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003060 for( i = ver_chain->len; i != 0; --i )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003061 {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003062 cur = &ver_chain->items[i-1];
3063 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003064
3065 if( NULL != f_vrfy )
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003066 if( ( ret = f_vrfy( p_vrfy, cur->crt, (int) i-1, &cur_flags ) ) != 0 )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003067 return( ret );
3068
3069 *flags |= cur_flags;
3070 }
3071
3072 return( 0 );
3073}
3074
3075/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003076 * Verify the certificate validity, with profile, restartable version
3077 *
3078 * This function:
3079 * - checks the requested CN (if any)
3080 * - checks the type and size of the EE cert's key,
3081 * as that isn't done as part of chain building/verification currently
3082 * - builds and verifies the chain
3083 * - then calls the callback and merges the flags
Hanno Becker3116fb32019-03-28 13:34:42 +00003084 *
3085 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
3086 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
3087 * verification routine to search for trusted signers, and CRLs will
3088 * be disabled. Otherwise, `trust_ca` will be used as the static list
3089 * of trusted signers, and `ca_crl` will be use as the static list
3090 * of CRLs.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003091 */
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003092static int x509_crt_verify_restartable_ca_cb( mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003093 mbedtls_x509_crt *trust_ca,
3094 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003095 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3096 void *p_ca_cb,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003097 const mbedtls_x509_crt_profile *profile,
3098 const char *cn, uint32_t *flags,
3099 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3100 void *p_vrfy,
3101 mbedtls_x509_crt_restart_ctx *rs_ctx )
3102{
Janos Follath865b3eb2019-12-16 11:46:15 +00003103 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003104 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003105 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003106 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003107
3108 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003109 ee_flags = 0;
3110 x509_crt_verify_chain_reset( &ver_chain );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003111
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003112 if( profile == NULL )
3113 {
3114 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3115 goto exit;
3116 }
3117
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003118 /* check name if requested */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003119 if( cn != NULL )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003120 x509_crt_verify_name( crt, cn, &ee_flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003121
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003122 /* Check the type and size of the key */
3123 pk_type = mbedtls_pk_get_type( &crt->pk );
3124
3125 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003126 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003127
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02003128 if( x509_profile_check_key( profile, &crt->pk ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003129 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003130
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003131 /* Check the chain */
Hanno Becker3116fb32019-03-28 13:34:42 +00003132 ret = x509_crt_verify_chain( crt, trust_ca, ca_crl,
3133 f_ca_cb, p_ca_cb, profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003134 &ver_chain, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003135
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003136 if( ret != 0 )
3137 goto exit;
3138
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003139 /* Merge end-entity flags */
3140 ver_chain.items[0].flags |= ee_flags;
3141
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003142 /* Build final flags, calling callback on the way if any */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003143 ret = x509_crt_merge_flags_with_cb( flags, &ver_chain, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003144
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003145exit:
Hanno Beckerf53893b2019-03-28 13:45:55 +00003146
3147#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3148 mbedtls_x509_crt_free( ver_chain.trust_ca_cb_result );
3149 mbedtls_free( ver_chain.trust_ca_cb_result );
3150 ver_chain.trust_ca_cb_result = NULL;
3151#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3152
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003153#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3154 if( rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
3155 mbedtls_x509_crt_restart_free( rs_ctx );
3156#endif
3157
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003158 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3159 * the SSL module for authmode optional, but non-zero return from the
3160 * callback means a fatal error so it shouldn't be ignored */
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003161 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
3162 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3163
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003164 if( ret != 0 )
3165 {
3166 *flags = (uint32_t) -1;
3167 return( ret );
3168 }
3169
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003170 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003171 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003172
3173 return( 0 );
3174}
3175
Hanno Becker3116fb32019-03-28 13:34:42 +00003176
3177/*
3178 * Verify the certificate validity (default profile, not restartable)
3179 */
3180int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
3181 mbedtls_x509_crt *trust_ca,
3182 mbedtls_x509_crl *ca_crl,
3183 const char *cn, uint32_t *flags,
3184 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3185 void *p_vrfy )
3186{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003187 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003188 NULL, NULL,
3189 &mbedtls_x509_crt_profile_default,
3190 cn, flags,
3191 f_vrfy, p_vrfy, NULL ) );
3192}
3193
3194/*
3195 * Verify the certificate validity (user-chosen profile, not restartable)
3196 */
3197int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
3198 mbedtls_x509_crt *trust_ca,
3199 mbedtls_x509_crl *ca_crl,
3200 const mbedtls_x509_crt_profile *profile,
3201 const char *cn, uint32_t *flags,
3202 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3203 void *p_vrfy )
3204{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003205 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003206 NULL, NULL,
3207 profile, cn, flags,
3208 f_vrfy, p_vrfy, NULL ) );
3209}
3210
3211#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3212/*
3213 * Verify the certificate validity (user-chosen profile, CA callback,
3214 * not restartable).
3215 */
Jarno Lamsa31d9db62019-04-01 14:33:49 +03003216int mbedtls_x509_crt_verify_with_ca_cb( mbedtls_x509_crt *crt,
Hanno Becker3116fb32019-03-28 13:34:42 +00003217 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3218 void *p_ca_cb,
3219 const mbedtls_x509_crt_profile *profile,
3220 const char *cn, uint32_t *flags,
3221 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3222 void *p_vrfy )
3223{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003224 return( x509_crt_verify_restartable_ca_cb( crt, NULL, NULL,
Hanno Becker3116fb32019-03-28 13:34:42 +00003225 f_ca_cb, p_ca_cb,
3226 profile, cn, flags,
3227 f_vrfy, p_vrfy, NULL ) );
3228}
3229#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3230
3231int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
3232 mbedtls_x509_crt *trust_ca,
3233 mbedtls_x509_crl *ca_crl,
3234 const mbedtls_x509_crt_profile *profile,
3235 const char *cn, uint32_t *flags,
3236 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3237 void *p_vrfy,
3238 mbedtls_x509_crt_restart_ctx *rs_ctx )
3239{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003240 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003241 NULL, NULL,
3242 profile, cn, flags,
3243 f_vrfy, p_vrfy, rs_ctx ) );
3244}
3245
3246
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003247/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003248 * Initialize a certificate chain
3249 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003250void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02003251{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003252 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02003253}
3254
3255/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003256 * Unallocate all certificate data
3257 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003258void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003259{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003260 mbedtls_x509_crt *cert_cur = crt;
3261 mbedtls_x509_crt *cert_prv;
3262 mbedtls_x509_name *name_cur;
3263 mbedtls_x509_name *name_prv;
3264 mbedtls_x509_sequence *seq_cur;
3265 mbedtls_x509_sequence *seq_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003266
3267 if( crt == NULL )
3268 return;
3269
3270 do
3271 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003272 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003274#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
3275 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003276#endif
3277
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003278 name_cur = cert_cur->issuer.next;
3279 while( name_cur != NULL )
3280 {
3281 name_prv = name_cur;
3282 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003283 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003284 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003285 }
3286
3287 name_cur = cert_cur->subject.next;
3288 while( name_cur != NULL )
3289 {
3290 name_prv = name_cur;
3291 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003292 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003293 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003294 }
3295
3296 seq_cur = cert_cur->ext_key_usage.next;
3297 while( seq_cur != NULL )
3298 {
3299 seq_prv = seq_cur;
3300 seq_cur = seq_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003301 mbedtls_platform_zeroize( seq_prv,
3302 sizeof( mbedtls_x509_sequence ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003303 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003304 }
3305
3306 seq_cur = cert_cur->subject_alt_names.next;
3307 while( seq_cur != NULL )
3308 {
3309 seq_prv = seq_cur;
3310 seq_cur = seq_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003311 mbedtls_platform_zeroize( seq_prv,
3312 sizeof( mbedtls_x509_sequence ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003313 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003314 }
3315
Ron Eldor74d9acc2019-03-21 14:00:03 +02003316 seq_cur = cert_cur->certificate_policies.next;
3317 while( seq_cur != NULL )
3318 {
3319 seq_prv = seq_cur;
3320 seq_cur = seq_cur->next;
3321 mbedtls_platform_zeroize( seq_prv,
3322 sizeof( mbedtls_x509_sequence ) );
3323 mbedtls_free( seq_prv );
3324 }
3325
Hanno Becker1a65dcd2019-01-31 08:57:44 +00003326 if( cert_cur->raw.p != NULL && cert_cur->own_buffer )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003327 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003328 mbedtls_platform_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003329 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003330 }
3331
3332 cert_cur = cert_cur->next;
3333 }
3334 while( cert_cur != NULL );
3335
3336 cert_cur = crt;
3337 do
3338 {
3339 cert_prv = cert_cur;
3340 cert_cur = cert_cur->next;
3341
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003342 mbedtls_platform_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003343 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003344 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003345 }
3346 while( cert_cur != NULL );
3347}
3348
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003349#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3350/*
3351 * Initialize a restart context
3352 */
3353void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx )
3354{
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003355 mbedtls_pk_restart_init( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003356
3357 ctx->parent = NULL;
3358 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003359 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003360
3361 ctx->parent_is_trusted = -1;
3362
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003363 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003364 ctx->self_cnt = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003365 x509_crt_verify_chain_reset( &ctx->ver_chain );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003366}
3367
3368/*
3369 * Free the components of a restart context
3370 */
3371void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx )
3372{
3373 if( ctx == NULL )
3374 return;
3375
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003376 mbedtls_pk_restart_free( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003377 mbedtls_x509_crt_restart_init( ctx );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003378}
3379#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003381#endif /* MBEDTLS_X509_CRT_PARSE_C */