blob: 14bedddbbf416ac8c3613c6e809601ab8666689e [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"
pespacek7599a772022-02-07 14:40:55 +010050#endif /* MBEDTLS_USE_PSA_CRYPTO */
Andrzej Kurekd4a65532018-10-31 06:18:39 -040051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020054#else
Simon Butcherd2642582018-10-03 15:11:19 +010055#include <stdio.h>
Rich Evans00ab4702015-02-06 13:43:58 +000056#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#define mbedtls_free free
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020058#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#define mbedtls_snprintf snprintf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020060#endif
61
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000063#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010064#endif
65
Paul Bakkerfa6a6202013-10-28 18:48:30 +010066#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020067#include <windows.h>
68#else
69#include <time.h>
70#endif
71
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072#if defined(MBEDTLS_FS_IO)
Rich Evans00ab4702015-02-06 13:43:58 +000073#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020074#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020075#include <sys/types.h>
76#include <sys/stat.h>
Martino Facchin0ec05ec2021-05-04 11:47:36 +020077#if defined(__MBED__)
78#include <platform/mbed_retarget.h>
79#else
Paul Bakker7c6b2c32013-09-16 13:49:26 +020080#include <dirent.h>
Martino Facchin0ec05ec2021-05-04 11:47:36 +020081#endif /* __MBED__ */
Rich Evans00ab4702015-02-06 13:43:58 +000082#endif /* !_WIN32 || EFIX64 || EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020083#endif
84
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +020085/*
86 * Item in a verification chain: cert and flags for it
87 */
88typedef struct {
89 mbedtls_x509_crt *crt;
90 uint32_t flags;
91} x509_crt_verify_chain_item;
92
93/*
94 * Max size of verification chain: end-entity + intermediates + trusted root
95 */
96#define X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
Paul Bakker34617722014-06-13 17:20:13 +020097
Gilles Peskineffb92da2021-06-02 00:03:26 +020098/* Default profile. Do not remove items unless there are serious security
99 * concerns. */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200100const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
101{
Gilles Peskineae270bf2021-06-02 00:05:29 +0200102 /* Hashes from SHA-256 and above. Note that this selection
103 * should be aligned with ssl_preset_default_hashes in ssl_tls.c. */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200104 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
105 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
106 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
107 0xFFFFFFF, /* Any PK alg */
108#if defined(MBEDTLS_ECP_C)
Gilles Peskineae270bf2021-06-02 00:05:29 +0200109 /* Curves at or above 128-bit security level. Note that this selection
110 * should be aligned with ssl_preset_default_curves in ssl_tls.c. */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200111 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
112 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
113 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
114 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
115 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
116 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
Gilles Peskine39957502021-06-17 23:17:52 +0200117 0,
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200118#else
119 0,
120#endif
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200121 2048,
122};
123
Gilles Peskineffb92da2021-06-02 00:03:26 +0200124/* Next-generation profile. Currently identical to the default, but may
125 * be tightened at any time. */
126const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200127{
128 /* Hashes from SHA-256 and above. */
129 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
130 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
131 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
132 0xFFFFFFF, /* Any PK alg */
133#if defined(MBEDTLS_ECP_C)
134 /* Curves at or above 128-bit security level. */
135 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
136 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
137 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
138 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
139 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
140 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
141 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256K1 ),
142#else
143 0,
144#endif
145 2048,
146};
Gilles Peskineffb92da2021-06-02 00:03:26 +0200147
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200148/*
149 * NSA Suite B Profile
150 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200151const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
152{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200153 /* Only SHA-256 and 384 */
154 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
155 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ),
156 /* Only ECDSA */
Ron Eldor85e1dcf2018-02-06 15:59:38 +0200157 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECDSA ) |
158 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECKEY ),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200159#if defined(MBEDTLS_ECP_C)
160 /* Only NIST P-256 and P-384 */
161 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
162 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ),
163#else
164 0,
165#endif
166 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200167};
168
169/*
Manuel Pégourié-Gonnard9d4c2c42021-06-18 09:48:27 +0200170 * Empty / all-forbidden profile
171 */
172const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_none =
173{
174 0,
175 0,
176 0,
177 (uint32_t) -1,
178};
179
180/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200181 * Check md_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200182 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200183 */
184static int x509_profile_check_md_alg( const mbedtls_x509_crt_profile *profile,
185 mbedtls_md_type_t md_alg )
186{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200187 if( md_alg == MBEDTLS_MD_NONE )
188 return( -1 );
189
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200190 if( ( profile->allowed_mds & MBEDTLS_X509_ID_FLAG( md_alg ) ) != 0 )
191 return( 0 );
192
193 return( -1 );
194}
195
196/*
197 * Check pk_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200198 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200199 */
200static int x509_profile_check_pk_alg( const mbedtls_x509_crt_profile *profile,
201 mbedtls_pk_type_t pk_alg )
202{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200203 if( pk_alg == MBEDTLS_PK_NONE )
204 return( -1 );
205
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200206 if( ( profile->allowed_pks & MBEDTLS_X509_ID_FLAG( pk_alg ) ) != 0 )
207 return( 0 );
208
209 return( -1 );
210}
211
212/*
213 * Check key against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200214 * Return 0 if pk is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200215 */
216static int x509_profile_check_key( const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200217 const mbedtls_pk_context *pk )
218{
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200219 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type( pk );
Manuel Pégourié-Gonnard19773ff2017-10-24 10:51:26 +0200220
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200221#if defined(MBEDTLS_RSA_C)
222 if( pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS )
223 {
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200224 if( mbedtls_pk_get_bitlen( pk ) >= profile->rsa_min_bitlen )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200225 return( 0 );
226
227 return( -1 );
228 }
229#endif
230
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200231#if defined(MBEDTLS_ECP_C)
232 if( pk_alg == MBEDTLS_PK_ECDSA ||
233 pk_alg == MBEDTLS_PK_ECKEY ||
234 pk_alg == MBEDTLS_PK_ECKEY_DH )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200235 {
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200236 const mbedtls_ecp_group_id gid = mbedtls_pk_ec( *pk )->grp.id;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200237
Philippe Antoineb5b25432018-05-11 11:06:29 +0200238 if( gid == MBEDTLS_ECP_DP_NONE )
239 return( -1 );
240
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200241 if( ( profile->allowed_curves & MBEDTLS_X509_ID_FLAG( gid ) ) != 0 )
242 return( 0 );
243
244 return( -1 );
245 }
246#endif
247
248 return( -1 );
249}
250
251/*
Hanno Becker1f8527f2018-11-02 09:19:16 +0000252 * Like memcmp, but case-insensitive and always returns -1 if different
253 */
254static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
255{
256 size_t i;
257 unsigned char diff;
258 const unsigned char *n1 = s1, *n2 = s2;
259
260 for( i = 0; i < len; i++ )
261 {
262 diff = n1[i] ^ n2[i];
263
264 if( diff == 0 )
265 continue;
266
267 if( diff == 32 &&
268 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
269 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
270 {
271 continue;
272 }
273
274 return( -1 );
275 }
276
277 return( 0 );
278}
279
280/*
281 * Return 0 if name matches wildcard, -1 otherwise
282 */
283static int x509_check_wildcard( const char *cn, const mbedtls_x509_buf *name )
284{
285 size_t i;
286 size_t cn_idx = 0, cn_len = strlen( cn );
287
288 /* We can't have a match if there is no wildcard to match */
289 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
290 return( -1 );
291
292 for( i = 0; i < cn_len; ++i )
293 {
294 if( cn[i] == '.' )
295 {
296 cn_idx = i;
297 break;
298 }
299 }
300
301 if( cn_idx == 0 )
302 return( -1 );
303
304 if( cn_len - cn_idx == name->len - 1 &&
305 x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
306 {
307 return( 0 );
308 }
309
310 return( -1 );
311}
312
313/*
314 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
315 * variations (but not all).
316 *
317 * Return 0 if equal, -1 otherwise.
318 */
319static int x509_string_cmp( const mbedtls_x509_buf *a, const mbedtls_x509_buf *b )
320{
321 if( a->tag == b->tag &&
322 a->len == b->len &&
323 memcmp( a->p, b->p, b->len ) == 0 )
324 {
325 return( 0 );
326 }
327
328 if( ( a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
329 ( b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
330 a->len == b->len &&
331 x509_memcasecmp( a->p, b->p, b->len ) == 0 )
332 {
333 return( 0 );
334 }
335
336 return( -1 );
337}
338
339/*
340 * Compare two X.509 Names (aka rdnSequence).
341 *
342 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
343 * we sometimes return unequal when the full algorithm would return equal,
344 * but never the other way. (In particular, we don't do Unicode normalisation
345 * or space folding.)
346 *
347 * Return 0 if equal, -1 otherwise.
348 */
349static int x509_name_cmp( const mbedtls_x509_name *a, const mbedtls_x509_name *b )
350{
351 /* Avoid recursion, it might not be optimised by the compiler */
352 while( a != NULL || b != NULL )
353 {
354 if( a == NULL || b == NULL )
355 return( -1 );
356
357 /* type */
358 if( a->oid.tag != b->oid.tag ||
359 a->oid.len != b->oid.len ||
360 memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 )
361 {
362 return( -1 );
363 }
364
365 /* value */
366 if( x509_string_cmp( &a->val, &b->val ) != 0 )
367 return( -1 );
368
369 /* structure of the list of sets */
370 if( a->next_merged != b->next_merged )
371 return( -1 );
372
373 a = a->next;
374 b = b->next;
375 }
376
377 /* a == NULL == b */
378 return( 0 );
379}
380
381/*
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200382 * Reset (init or clear) a verify_chain
383 */
384static void x509_crt_verify_chain_reset(
385 mbedtls_x509_crt_verify_chain *ver_chain )
386{
387 size_t i;
388
389 for( i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++ )
390 {
391 ver_chain->items[i].crt = NULL;
Hanno Beckera9375b32019-01-10 09:19:26 +0000392 ver_chain->items[i].flags = (uint32_t) -1;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200393 }
394
395 ver_chain->len = 0;
Hanno Beckerf53893b2019-03-28 13:45:55 +0000396
397#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
398 ver_chain->trust_ca_cb_result = NULL;
399#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200400}
401
402/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200403 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
404 */
405static int x509_get_version( unsigned char **p,
406 const unsigned char *end,
407 int *ver )
408{
Janos Follath865b3eb2019-12-16 11:46:15 +0000409 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200410 size_t len;
411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
413 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200414 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200416 {
417 *ver = 0;
418 return( 0 );
419 }
420
Chris Jones9f7a6932021-04-14 12:12:09 +0100421 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200422 }
423
424 end = *p + len;
425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100427 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_VERSION, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200428
429 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100430 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_VERSION,
431 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200432
433 return( 0 );
434}
435
436/*
437 * Validity ::= SEQUENCE {
438 * notBefore Time,
439 * notAfter Time }
440 */
441static int x509_get_dates( unsigned char **p,
442 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443 mbedtls_x509_time *from,
444 mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200445{
Janos Follath865b3eb2019-12-16 11:46:15 +0000446 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200447 size_t len;
448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
450 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100451 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200452
453 end = *p + len;
454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455 if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200456 return( ret );
457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458 if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200459 return( ret );
460
461 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100462 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE,
463 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200464
465 return( 0 );
466}
467
468/*
469 * X.509 v2/v3 unique identifier (not parsed)
470 */
471static int x509_get_uid( unsigned char **p,
472 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473 mbedtls_x509_buf *uid, int n )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200474{
Janos Follath865b3eb2019-12-16 11:46:15 +0000475 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200476
477 if( *p == end )
478 return( 0 );
479
480 uid->tag = **p;
481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482 if( ( ret = mbedtls_asn1_get_tag( p, end, &uid->len,
483 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200484 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200486 return( 0 );
487
Chris Jones9f7a6932021-04-14 12:12:09 +0100488 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200489 }
490
491 uid->p = *p;
492 *p += uid->len;
493
494 return( 0 );
495}
496
497static int x509_get_basic_constraints( unsigned char **p,
498 const unsigned char *end,
499 int *ca_istrue,
500 int *max_pathlen )
501{
Janos Follath865b3eb2019-12-16 11:46:15 +0000502 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200503 size_t len;
504
505 /*
506 * BasicConstraints ::= SEQUENCE {
507 * cA BOOLEAN DEFAULT FALSE,
508 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
509 */
510 *ca_istrue = 0; /* DEFAULT FALSE */
511 *max_pathlen = 0; /* endless */
512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
514 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100515 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200516
517 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200518 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 if( ( ret = mbedtls_asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200521 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
523 ret = mbedtls_asn1_get_int( p, end, ca_istrue );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200524
525 if( ret != 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( *ca_istrue != 0 )
529 *ca_istrue = 1;
530 }
531
532 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200533 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100536 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200537
538 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100539 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
540 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200541
Andrzej Kurek16050742020-04-14 09:49:52 -0400542 /* Do not accept max_pathlen equal to INT_MAX to avoid a signed integer
543 * overflow, which is an undefined behavior. */
544 if( *max_pathlen == INT_MAX )
Chris Jones9f7a6932021-04-14 12:12:09 +0100545 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
546 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Andrzej Kurek16050742020-04-14 09:49:52 -0400547
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200548 (*max_pathlen)++;
549
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200550 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200551}
552
553static int x509_get_ns_cert_type( unsigned char **p,
554 const unsigned char *end,
555 unsigned char *ns_cert_type)
556{
Janos Follath865b3eb2019-12-16 11:46:15 +0000557 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100561 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200562
563 if( bs.len != 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100564 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
565 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200566
567 /* Get actual bitstring */
568 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200569 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200570}
571
572static int x509_get_key_usage( unsigned char **p,
573 const unsigned char *end,
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +0100574 unsigned int *key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200575{
Janos Follath865b3eb2019-12-16 11:46:15 +0000576 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200577 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100581 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200582
583 if( bs.len < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100584 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
585 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200586
587 /* Get actual bitstring */
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200588 *key_usage = 0;
589 for( i = 0; i < bs.len && i < sizeof( unsigned int ); i++ )
590 {
591 *key_usage |= (unsigned int) bs.p[i] << (8*i);
592 }
593
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200594 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200595}
596
597/*
598 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
599 *
600 * KeyPurposeId ::= OBJECT IDENTIFIER
601 */
602static int x509_get_ext_key_usage( unsigned char **p,
603 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200605{
Janos Follath865b3eb2019-12-16 11:46:15 +0000606 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 if( ( ret = mbedtls_asn1_get_sequence_of( p, end, ext_key_usage, MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100609 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200610
611 /* Sequence length must be >= 1 */
612 if( ext_key_usage->buf.p == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100613 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
614 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200615
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200616 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200617}
618
619/*
620 * SubjectAltName ::= GeneralNames
621 *
622 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
623 *
624 * GeneralName ::= CHOICE {
625 * otherName [0] OtherName,
626 * rfc822Name [1] IA5String,
627 * dNSName [2] IA5String,
628 * x400Address [3] ORAddress,
629 * directoryName [4] Name,
630 * ediPartyName [5] EDIPartyName,
631 * uniformResourceIdentifier [6] IA5String,
632 * iPAddress [7] OCTET STRING,
633 * registeredID [8] OBJECT IDENTIFIER }
634 *
635 * OtherName ::= SEQUENCE {
636 * type-id OBJECT IDENTIFIER,
637 * value [0] EXPLICIT ANY DEFINED BY type-id }
638 *
639 * EDIPartyName ::= SEQUENCE {
640 * nameAssigner [0] DirectoryString OPTIONAL,
641 * partyName [1] DirectoryString }
642 *
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300643 * NOTE: we list all types, but only use dNSName and otherName
644 * of type HwModuleName, as defined in RFC 4108, at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200645 */
646static int x509_get_subject_alt_name( unsigned char **p,
647 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 mbedtls_x509_sequence *subject_alt_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200649{
Janos Follath865b3eb2019-12-16 11:46:15 +0000650 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200651 size_t len, tag_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652 mbedtls_asn1_buf *buf;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200653 unsigned char tag;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654 mbedtls_asn1_sequence *cur = subject_alt_name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200655
656 /* Get main sequence tag */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
658 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100659 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200660
661 if( *p + len != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100662 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
663 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200664
665 while( *p < end )
666 {
Ron Eldordbbd9662019-05-15 15:46:03 +0300667 mbedtls_x509_subject_alternative_name dummy_san_buf;
668 memset( &dummy_san_buf, 0, sizeof( dummy_san_buf ) );
669
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200670 tag = **p;
671 (*p)++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672 if( ( ret = mbedtls_asn1_get_len( p, end, &tag_len ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100673 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200674
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100675 if( ( tag & MBEDTLS_ASN1_TAG_CLASS_MASK ) !=
676 MBEDTLS_ASN1_CONTEXT_SPECIFIC )
677 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100678 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
679 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100680 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200681
Ron Eldordbbd9662019-05-15 15:46:03 +0300682 /*
irwir74aee1c2019-09-21 18:21:48 +0300683 * Check that the SAN is structured correctly.
Ron Eldordbbd9662019-05-15 15:46:03 +0300684 */
685 ret = mbedtls_x509_parse_subject_alt_name( &(cur->buf), &dummy_san_buf );
686 /*
687 * In case the extension is malformed, return an error,
688 * and clear the allocated sequences.
689 */
690 if( ret != 0 && ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
691 {
692 mbedtls_x509_sequence *seq_cur = subject_alt_name->next;
693 mbedtls_x509_sequence *seq_prv;
694 while( seq_cur != NULL )
695 {
696 seq_prv = seq_cur;
697 seq_cur = seq_cur->next;
698 mbedtls_platform_zeroize( seq_prv,
699 sizeof( mbedtls_x509_sequence ) );
700 mbedtls_free( seq_prv );
701 }
Ron Eldor5aebeeb2019-05-22 16:41:21 +0300702 subject_alt_name->next = NULL;
Ron Eldordbbd9662019-05-15 15:46:03 +0300703 return( ret );
704 }
705
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200706 /* Allocate and assign next pointer */
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200707 if( cur->buf.p != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200708 {
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100709 if( cur->next != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100711
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200712 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200713
714 if( cur->next == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100715 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
716 MBEDTLS_ERR_ASN1_ALLOC_FAILED ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200717
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200718 cur = cur->next;
719 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200720
721 buf = &(cur->buf);
722 buf->tag = tag;
723 buf->p = *p;
724 buf->len = tag_len;
725 *p += buf->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200726 }
727
728 /* Set final sequence entry's next pointer to NULL */
729 cur->next = NULL;
730
731 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100732 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
733 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200734
735 return( 0 );
736}
737
738/*
Ron Eldor74d9acc2019-03-21 14:00:03 +0200739 * id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 }
740 *
741 * anyPolicy OBJECT IDENTIFIER ::= { id-ce-certificatePolicies 0 }
742 *
743 * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
744 *
745 * PolicyInformation ::= SEQUENCE {
746 * policyIdentifier CertPolicyId,
747 * policyQualifiers SEQUENCE SIZE (1..MAX) OF
748 * PolicyQualifierInfo OPTIONAL }
749 *
750 * CertPolicyId ::= OBJECT IDENTIFIER
751 *
752 * PolicyQualifierInfo ::= SEQUENCE {
753 * policyQualifierId PolicyQualifierId,
754 * qualifier ANY DEFINED BY policyQualifierId }
755 *
756 * -- policyQualifierIds for Internet policy qualifiers
757 *
758 * id-qt OBJECT IDENTIFIER ::= { id-pkix 2 }
759 * id-qt-cps OBJECT IDENTIFIER ::= { id-qt 1 }
760 * id-qt-unotice OBJECT IDENTIFIER ::= { id-qt 2 }
761 *
762 * PolicyQualifierId ::= OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice )
763 *
764 * Qualifier ::= CHOICE {
765 * cPSuri CPSuri,
766 * userNotice UserNotice }
767 *
768 * CPSuri ::= IA5String
769 *
770 * UserNotice ::= SEQUENCE {
771 * noticeRef NoticeReference OPTIONAL,
772 * explicitText DisplayText OPTIONAL }
773 *
774 * NoticeReference ::= SEQUENCE {
775 * organization DisplayText,
776 * noticeNumbers SEQUENCE OF INTEGER }
777 *
778 * DisplayText ::= CHOICE {
779 * ia5String IA5String (SIZE (1..200)),
780 * visibleString VisibleString (SIZE (1..200)),
781 * bmpString BMPString (SIZE (1..200)),
782 * utf8String UTF8String (SIZE (1..200)) }
783 *
784 * NOTE: we only parse and use anyPolicy without qualifiers at this point
785 * as defined in RFC 5280.
786 */
787static int x509_get_certificate_policies( unsigned char **p,
788 const unsigned char *end,
789 mbedtls_x509_sequence *certificate_policies )
790{
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300791 int ret, parse_ret = 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200792 size_t len;
793 mbedtls_asn1_buf *buf;
794 mbedtls_asn1_sequence *cur = certificate_policies;
795
796 /* Get main sequence tag */
797 ret = mbedtls_asn1_get_tag( p, end, &len,
798 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
799 if( ret != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100800 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200801
802 if( *p + len != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100803 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
804 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200805
806 /*
807 * Cannot be an empty sequence.
808 */
809 if( len == 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100810 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
811 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200812
813 while( *p < end )
814 {
815 mbedtls_x509_buf policy_oid;
816 const unsigned char *policy_end;
817
818 /*
819 * Get the policy sequence
820 */
821 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
822 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100823 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200824
825 policy_end = *p + len;
826
Ron Eldor08063792019-05-13 16:38:39 +0300827 if( ( ret = mbedtls_asn1_get_tag( p, policy_end, &len,
Ron Eldor74d9acc2019-03-21 14:00:03 +0200828 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100829 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200830
831 policy_oid.tag = MBEDTLS_ASN1_OID;
832 policy_oid.len = len;
833 policy_oid.p = *p;
834
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300835 /*
836 * Only AnyPolicy is currently supported when enforcing policy.
837 */
838 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_POLICY, &policy_oid ) != 0 )
839 {
840 /*
841 * Set the parsing return code but continue parsing, in case this
TRodziewicz3ecb92e2021-05-11 18:22:05 +0200842 * extension is critical.
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300843 */
844 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
845 }
846
Ron Eldor74d9acc2019-03-21 14:00:03 +0200847 /* Allocate and assign next pointer */
848 if( cur->buf.p != NULL )
849 {
850 if( cur->next != NULL )
851 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
852
853 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
854
855 if( cur->next == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100856 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
857 MBEDTLS_ERR_ASN1_ALLOC_FAILED ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200858
859 cur = cur->next;
860 }
861
862 buf = &( cur->buf );
863 buf->tag = policy_oid.tag;
864 buf->p = policy_oid.p;
865 buf->len = policy_oid.len;
Ron Eldor08063792019-05-13 16:38:39 +0300866
867 *p += len;
868
869 /*
870 * If there is an optional qualifier, then *p < policy_end
871 * Check the Qualifier len to verify it doesn't exceed policy_end.
872 */
873 if( *p < policy_end )
874 {
875 if( ( ret = mbedtls_asn1_get_tag( p, policy_end, &len,
876 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100877 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor08063792019-05-13 16:38:39 +0300878 /*
879 * Skip the optional policy qualifiers.
880 */
881 *p += len;
882 }
883
884 if( *p != policy_end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100885 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
886 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200887 }
888
889 /* Set final sequence entry's next pointer to NULL */
890 cur->next = NULL;
891
892 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100893 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
894 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200895
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300896 return( parse_ret );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200897}
898
899/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200900 * X.509 v3 extensions
901 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200902 */
903static int x509_get_crt_ext( unsigned char **p,
904 const unsigned char *end,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200905 mbedtls_x509_crt *crt,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200906 mbedtls_x509_crt_ext_cb_t cb,
907 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200908{
Janos Follath865b3eb2019-12-16 11:46:15 +0000909 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200910 size_t len;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200911 unsigned char *end_ext_data, *start_ext_octet, *end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200912
Hanno Becker12f62fb2019-02-12 17:22:36 +0000913 if( *p == end )
914 return( 0 );
915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916 if( ( ret = mbedtls_x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200917 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200918
Hanno Becker12f62fb2019-02-12 17:22:36 +0000919 end = crt->v3_ext.p + crt->v3_ext.len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200920 while( *p < end )
921 {
922 /*
923 * Extension ::= SEQUENCE {
924 * extnID OBJECT IDENTIFIER,
925 * critical BOOLEAN DEFAULT FALSE,
926 * extnValue OCTET STRING }
927 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200928 mbedtls_x509_buf extn_oid = {0, 0, NULL};
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200929 int is_critical = 0; /* DEFAULT FALSE */
930 int ext_type = 0;
931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
933 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100934 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200935
936 end_ext_data = *p + len;
937
938 /* Get extension ID */
k-stachowiakdcae78a2018-06-28 16:32:54 +0200939 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &extn_oid.len,
k-stachowiak463928a2018-07-24 12:50:59 +0200940 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100941 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200942
k-stachowiak463928a2018-07-24 12:50:59 +0200943 extn_oid.tag = MBEDTLS_ASN1_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200944 extn_oid.p = *p;
945 *p += extn_oid.len;
946
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200947 /* Get optional critical */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948 if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
949 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
Chris Jones9f7a6932021-04-14 12:12:09 +0100950 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200951
952 /* Data should be octet string type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
954 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100955 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200956
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200957 start_ext_octet = *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200958 end_ext_octet = *p + len;
959
960 if( end_ext_octet != end_ext_data )
Chris Jones9f7a6932021-04-14 12:12:09 +0100961 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
962 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200963
964 /*
965 * Detect supported extensions
966 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200967 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200968
969 if( ret != 0 )
970 {
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200971 /* Give the callback (if any) a chance to handle the extension */
Nicola Di Lieto2c3a9172020-05-28 17:20:42 +0200972 if( cb != NULL )
973 {
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200974 ret = cb( p_ctx, crt, &extn_oid, is_critical, *p, end_ext_octet );
Nicola Di Lieto565b52b2020-05-29 22:46:56 +0200975 if( ret != 0 && is_critical )
976 return( ret );
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200977 *p = end_ext_octet;
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200978 continue;
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200979 }
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200980
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200981 /* No parser found, skip extension */
982 *p = end_ext_octet;
983
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200984 if( is_critical )
985 {
986 /* Data is marked as critical: fail */
Chris Jones9f7a6932021-04-14 12:12:09 +0100987 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
988 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200989 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200990 continue;
991 }
992
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100993 /* Forbid repeated extensions */
994 if( ( crt->ext_types & ext_type ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200995 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100996
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200997 crt->ext_types |= ext_type;
998
999 switch( ext_type )
1000 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001001 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001002 /* Parse basic constraints */
1003 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
1004 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001005 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001006 break;
1007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001009 /* Parse key usage */
1010 if( ( ret = x509_get_key_usage( p, end_ext_octet,
1011 &crt->key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001012 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001013 break;
1014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001016 /* Parse extended key usage */
1017 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
1018 &crt->ext_key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001019 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001020 break;
1021
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001022 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001023 /* Parse subject alt name */
1024 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
1025 &crt->subject_alt_names ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001026 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001027 break;
1028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001029 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001030 /* Parse netscape certificate type */
1031 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
1032 &crt->ns_cert_type ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001033 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001034 break;
1035
Ron Eldor74d9acc2019-03-21 14:00:03 +02001036 case MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES:
1037 /* Parse certificate policies type */
1038 if( ( ret = x509_get_certificate_policies( p, end_ext_octet,
1039 &crt->certificate_policies ) ) != 0 )
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001040 {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +02001041 /* Give the callback (if any) a chance to handle the extension
1042 * if it contains unsupported policies */
1043 if( ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE && cb != NULL &&
1044 cb( p_ctx, crt, &extn_oid, is_critical,
1045 start_ext_octet, end_ext_octet ) == 0 )
1046 break;
1047
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001048 if( is_critical )
1049 return( ret );
1050 else
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001051 /*
Ron Eldora2913912019-05-16 16:17:38 +03001052 * If MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE is returned, then we
1053 * cannot interpret or enforce the policy. However, it is up to
1054 * the user to choose how to enforce the policies,
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001055 * unless the extension is critical.
1056 */
1057 if( ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1058 return( ret );
1059 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001060 break;
1061
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001062 default:
Ron Eldordf48efa2019-04-08 13:28:24 +03001063 /*
1064 * If this is a non-critical extension, which the oid layer
1065 * supports, but there isn't an x509 parser for it,
1066 * skip the extension.
1067 */
Ron Eldordf48efa2019-04-08 13:28:24 +03001068 if( is_critical )
1069 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1070 else
Ron Eldordf48efa2019-04-08 13:28:24 +03001071 *p = end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001072 }
1073 }
1074
1075 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +01001076 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1077 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001078
1079 return( 0 );
1080}
1081
1082/*
1083 * Parse and fill a single X.509 certificate in DER format
1084 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001085static int x509_crt_parse_der_core( mbedtls_x509_crt *crt,
1086 const unsigned char *buf,
1087 size_t buflen,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001088 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001089 mbedtls_x509_crt_ext_cb_t cb,
1090 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001091{
Janos Follath865b3eb2019-12-16 11:46:15 +00001092 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001093 size_t len;
1094 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001095 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097 memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) );
1098 memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) );
1099 memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001100
1101 /*
1102 * Check for valid input
1103 */
1104 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001106
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001107 /* Use the original buffer until we figure out actual length. */
Janos Follathcc0e49d2016-02-17 14:34:12 +00001108 p = (unsigned char*) buf;
1109 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001110 end = p + len;
1111
1112 /*
1113 * Certificate ::= SEQUENCE {
1114 * tbsCertificate TBSCertificate,
1115 * signatureAlgorithm AlgorithmIdentifier,
1116 * signatureValue BIT STRING }
1117 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001118 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1119 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001121 mbedtls_x509_crt_free( crt );
1122 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001123 }
1124
Janos Follathcc0e49d2016-02-17 14:34:12 +00001125 end = crt_end = p + len;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001126 crt->raw.len = crt_end - buf;
1127 if( make_copy != 0 )
1128 {
1129 /* Create and populate a new buffer for the raw field. */
1130 crt->raw.p = p = mbedtls_calloc( 1, crt->raw.len );
1131 if( crt->raw.p == NULL )
1132 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
1133
1134 memcpy( crt->raw.p, buf, crt->raw.len );
1135 crt->own_buffer = 1;
1136
1137 p += crt->raw.len - len;
1138 end = crt_end = p + len;
1139 }
1140 else
1141 {
1142 crt->raw.p = (unsigned char*) buf;
1143 crt->own_buffer = 0;
1144 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001145
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001146 /*
1147 * TBSCertificate ::= SEQUENCE {
1148 */
1149 crt->tbs.p = p;
1150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1152 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001153 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001155 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001156 }
1157
1158 end = p + len;
1159 crt->tbs.len = end - crt->tbs.p;
1160
1161 /*
1162 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1163 *
1164 * CertificateSerialNumber ::= INTEGER
1165 *
1166 * signature AlgorithmIdentifier
1167 */
1168 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001169 ( ret = mbedtls_x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1170 ( ret = mbedtls_x509_get_alg( &p, end, &crt->sig_oid,
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001171 &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001174 return( ret );
1175 }
1176
Andres AG7ca4a032017-03-09 16:16:11 +00001177 if( crt->version < 0 || crt->version > 2 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001178 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179 mbedtls_x509_crt_free( crt );
1180 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001181 }
1182
Andres AG7ca4a032017-03-09 16:16:11 +00001183 crt->version++;
1184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001185 if( ( ret = mbedtls_x509_get_sig_alg( &crt->sig_oid, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02001186 &crt->sig_md, &crt->sig_pk,
1187 &crt->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001188 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001190 return( ret );
1191 }
1192
1193 /*
1194 * issuer Name
1195 */
1196 crt->issuer_raw.p = p;
1197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001198 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1199 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001200 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001202 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001203 }
1204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001205 if( ( ret = mbedtls_x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001206 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001207 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001208 return( ret );
1209 }
1210
1211 crt->issuer_raw.len = p - crt->issuer_raw.p;
1212
1213 /*
1214 * Validity ::= SEQUENCE {
1215 * notBefore Time,
1216 * notAfter Time }
1217 *
1218 */
1219 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1220 &crt->valid_to ) ) != 0 )
1221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001222 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001223 return( ret );
1224 }
1225
1226 /*
1227 * subject Name
1228 */
1229 crt->subject_raw.p = p;
1230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001231 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1232 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001233 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001234 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001235 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001236 }
1237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001238 if( len && ( ret = mbedtls_x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001239 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001240 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001241 return( ret );
1242 }
1243
1244 crt->subject_raw.len = p - crt->subject_raw.p;
1245
1246 /*
1247 * SubjectPublicKeyInfo
1248 */
Hanno Becker494dd7a2019-02-06 16:13:41 +00001249 crt->pk_raw.p = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001250 if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001251 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001252 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001253 return( ret );
1254 }
Hanno Becker494dd7a2019-02-06 16:13:41 +00001255 crt->pk_raw.len = p - crt->pk_raw.p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001256
1257 /*
1258 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1259 * -- If present, version shall be v2 or v3
1260 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1261 * -- If present, version shall be v2 or v3
1262 * extensions [3] EXPLICIT Extensions OPTIONAL
1263 * -- If present, version shall be v3
1264 */
1265 if( crt->version == 2 || crt->version == 3 )
1266 {
1267 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
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 == 2 || crt->version == 3 )
1276 {
1277 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1278 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( crt->version == 3 )
Manuel Pégourié-Gonnarda252af72015-03-27 16:15:55 +01001286 {
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001287 ret = x509_get_crt_ext( &p, end, crt, cb, p_ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001288 if( ret != 0 )
1289 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001290 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001291 return( ret );
1292 }
1293 }
1294
1295 if( p != end )
1296 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001297 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001298 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT,
1299 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001300 }
1301
1302 end = crt_end;
1303
1304 /*
1305 * }
1306 * -- end of TBSCertificate
1307 *
1308 * signatureAlgorithm AlgorithmIdentifier,
1309 * signatureValue BIT STRING
1310 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311 if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001314 return( ret );
1315 }
1316
Manuel Pégourié-Gonnard1022fed2015-03-27 16:30:47 +01001317 if( crt->sig_oid.len != sig_oid2.len ||
1318 memcmp( crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len ) != 0 ||
Paul Elliottca17ebf2020-11-24 17:30:18 +00001319 sig_params1.tag != sig_params2.tag ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001320 sig_params1.len != sig_params2.len ||
Manuel Pégourié-Gonnard159c5242015-04-30 11:15:22 +02001321 ( sig_params1.len != 0 &&
1322 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001323 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324 mbedtls_x509_crt_free( crt );
1325 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001326 }
1327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328 if( ( ret = mbedtls_x509_get_sig( &p, end, &crt->sig ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001331 return( ret );
1332 }
1333
1334 if( p != end )
1335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001337 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT,
1338 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001339 }
1340
1341 return( 0 );
1342}
1343
1344/*
1345 * Parse one X.509 certificate in DER format from a buffer and add them to a
1346 * chained list
1347 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001348static int mbedtls_x509_crt_parse_der_internal( mbedtls_x509_crt *chain,
1349 const unsigned char *buf,
1350 size_t buflen,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001351 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001352 mbedtls_x509_crt_ext_cb_t cb,
1353 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001354{
Janos Follath865b3eb2019-12-16 11:46:15 +00001355 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001357
1358 /*
1359 * Check for valid input
1360 */
1361 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001363
1364 while( crt->version != 0 && crt->next != NULL )
1365 {
1366 prev = crt;
1367 crt = crt->next;
1368 }
1369
1370 /*
1371 * Add new certificate on the end of the chain if needed.
1372 */
Paul Bakker66d5d072014-06-17 16:39:18 +02001373 if( crt->version != 0 && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001374 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001375 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001376
1377 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001378 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001379
1380 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001381 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001382 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001383 }
1384
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001385 ret = x509_crt_parse_der_core( crt, buf, buflen, make_copy, cb, p_ctx );
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001386 if( ret != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001387 {
1388 if( prev )
1389 prev->next = NULL;
1390
1391 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001393
1394 return( ret );
1395 }
1396
1397 return( 0 );
1398}
1399
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001400int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
1401 const unsigned char *buf,
1402 size_t buflen )
1403{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001404 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 0, NULL, NULL ) );
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001405}
1406
Nicola Di Lietofde98f72020-05-28 08:34:33 +02001407int mbedtls_x509_crt_parse_der_with_ext_cb( mbedtls_x509_crt *chain,
1408 const unsigned char *buf,
1409 size_t buflen,
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +02001410 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001411 mbedtls_x509_crt_ext_cb_t cb,
1412 void *p_ctx )
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001413{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001414 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, make_copy, cb, p_ctx ) );
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001415}
1416
1417int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
1418 const unsigned char *buf,
1419 size_t buflen )
1420{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001421 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 1, NULL, NULL ) );
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001422}
1423
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001424/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001425 * Parse one or more PEM certificates from a buffer and add them to the chained
1426 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001427 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001428int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain,
1429 const unsigned char *buf,
1430 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001431{
Janos Follath98e28a72016-05-31 14:03:54 +01001432#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001433 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001434 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001435#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001436
1437 /*
1438 * Check for valid input
1439 */
1440 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001442
1443 /*
1444 * Determine buffer content. Buffer contains either one DER certificate or
1445 * one or more PEM certificates.
1446 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001447#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001448 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001449 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1450 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001451 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001452 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454 if( buf_format == MBEDTLS_X509_FORMAT_DER )
1455 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Janos Follath98e28a72016-05-31 14:03:54 +01001456#else
1457 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
1458#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001460#if defined(MBEDTLS_PEM_PARSE_C)
1461 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001462 {
Janos Follath865b3eb2019-12-16 11:46:15 +00001463 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001465
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001466 /* 1 rather than 0 since the terminating NULL byte is counted in */
1467 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001468 {
1469 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001470 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001471
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001472 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001474 "-----BEGIN CERTIFICATE-----",
1475 "-----END CERTIFICATE-----",
1476 buf, NULL, 0, &use_len );
1477
1478 if( ret == 0 )
1479 {
1480 /*
1481 * Was PEM encoded
1482 */
1483 buflen -= use_len;
1484 buf += use_len;
1485 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001486 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001487 {
1488 return( ret );
1489 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001490 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001491 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001493
1494 /*
1495 * PEM header and footer were found
1496 */
1497 buflen -= use_len;
1498 buf += use_len;
1499
1500 if( first_error == 0 )
1501 first_error = ret;
1502
Paul Bakker5a5fa922014-09-26 14:53:04 +02001503 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001504 continue;
1505 }
1506 else
1507 break;
1508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001509 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001511 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001512
1513 if( ret != 0 )
1514 {
1515 /*
1516 * Quit parsing on a memory error
1517 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001518 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001519 return( ret );
1520
1521 if( first_error == 0 )
1522 first_error = ret;
1523
1524 total_failed++;
1525 continue;
1526 }
1527
1528 success = 1;
1529 }
1530 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001531
1532 if( success )
1533 return( total_failed );
1534 else if( first_error )
1535 return( first_error );
1536 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001537 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Janos Follath98e28a72016-05-31 14:03:54 +01001538#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001539}
1540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001541#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001542/*
1543 * Load one or more certificates and add them to the chained list
1544 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001545int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001546{
Janos Follath865b3eb2019-12-16 11:46:15 +00001547 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001548 size_t n;
1549 unsigned char *buf;
1550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001552 return( ret );
1553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001555
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001556 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001557 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001558
1559 return( ret );
1560}
1561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001562int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001563{
1564 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001565#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001566 int w_ret;
1567 WCHAR szDir[MAX_PATH];
1568 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001569 char *p;
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001570 size_t len = strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001571
Paul Bakker9af723c2014-05-01 13:03:14 +02001572 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001573 HANDLE hFind;
1574
1575 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001577
Paul Bakker9af723c2014-05-01 13:03:14 +02001578 memset( szDir, 0, sizeof(szDir) );
1579 memset( filename, 0, MAX_PATH );
1580 memcpy( filename, path, len );
1581 filename[len++] = '\\';
1582 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001583 filename[len++] = '*';
1584
Simon B3c6b18d2016-11-03 01:11:37 +00001585 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001586 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001587 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001588 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001589
1590 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001591 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001592 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001593
1594 len = MAX_PATH - len;
1595 do
1596 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001597 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001598
1599 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1600 continue;
1601
Paul Bakker9af723c2014-05-01 13:03:14 +02001602 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001603 lstrlenW( file_data.cFileName ),
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001604 p, (int) len - 1,
Paul Bakker9af723c2014-05-01 13:03:14 +02001605 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001606 if( w_ret == 0 )
Ron Eldor36d90422017-01-09 15:09:16 +02001607 {
1608 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1609 goto cleanup;
1610 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001613 if( w_ret < 0 )
1614 ret++;
1615 else
1616 ret += w_ret;
1617 }
1618 while( FindNextFileW( hFind, &file_data ) != 0 );
1619
Paul Bakker66d5d072014-06-17 16:39:18 +02001620 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001621 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001622
Ron Eldor36d90422017-01-09 15:09:16 +02001623cleanup:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001624 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001625#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001626 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001627 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001628 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001629 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001630 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001631 DIR *dir = opendir( path );
1632
Paul Bakker66d5d072014-06-17 16:39:18 +02001633 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001634 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001635
Ron Eldor63140682017-01-09 19:27:59 +02001636#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001637 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001638 {
1639 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001640 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001641 }
Ron Eldor63140682017-01-09 19:27:59 +02001642#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001643
Paul Elliottfb91a482021-03-05 14:17:51 +00001644 memset( &sb, 0, sizeof( sb ) );
1645
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001646 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001647 {
Andres AGf9113192016-09-02 14:06:04 +01001648 snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name,
1649 "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001650
Andres AGf9113192016-09-02 14:06:04 +01001651 if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001652 {
Andres AGf9113192016-09-02 14:06:04 +01001653 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1654 goto cleanup;
1655 }
1656 else if( stat( entry_name, &sb ) == -1 )
1657 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001658 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001659 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001660 }
1661
1662 if( !S_ISREG( sb.st_mode ) )
1663 continue;
1664
1665 // Ignore parse errors
1666 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001667 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001668 if( t_ret < 0 )
1669 ret++;
1670 else
1671 ret += t_ret;
1672 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001673
1674cleanup:
Andres AGf9113192016-09-02 14:06:04 +01001675 closedir( dir );
1676
Ron Eldor63140682017-01-09 19:27:59 +02001677#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001678 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001679 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Ron Eldor63140682017-01-09 19:27:59 +02001680#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001681
Paul Bakkerbe089b02013-10-14 15:51:50 +02001682#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001683
1684 return( ret );
1685}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001686#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001687
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001688/*
1689 * OtherName ::= SEQUENCE {
1690 * type-id OBJECT IDENTIFIER,
1691 * value [0] EXPLICIT ANY DEFINED BY type-id }
1692 *
1693 * HardwareModuleName ::= SEQUENCE {
1694 * hwType OBJECT IDENTIFIER,
1695 * hwSerialNum OCTET STRING }
1696 *
1697 * NOTE: we currently only parse and use otherName of type HwModuleName,
1698 * as defined in RFC 4108.
1699 */
1700static int x509_get_other_name( const mbedtls_x509_buf *subject_alt_name,
1701 mbedtls_x509_san_other_name *other_name )
1702{
Janos Follathab23cd12019-05-09 13:53:57 +01001703 int ret = 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001704 size_t len;
1705 unsigned char *p = subject_alt_name->p;
1706 const unsigned char *end = p + subject_alt_name->len;
1707 mbedtls_x509_buf cur_oid;
1708
1709 if( ( subject_alt_name->tag &
1710 ( MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK ) ) !=
1711 ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ) )
1712 {
1713 /*
1714 * The given subject alternative name is not of type "othername".
1715 */
1716 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
1717 }
1718
1719 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1720 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001721 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001722
1723 cur_oid.tag = MBEDTLS_ASN1_OID;
1724 cur_oid.p = p;
1725 cur_oid.len = len;
1726
1727 /*
1728 * Only HwModuleName is currently supported.
1729 */
1730 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid ) != 0 )
1731 {
1732 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1733 }
1734
Ron Eldor890819a2019-05-13 19:03:04 +03001735 if( p + len >= end )
1736 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001737 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001738 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1739 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001740 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001741 p += len;
1742 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1743 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001744 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001745
1746 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1747 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001748 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001749
1750 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001751 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001752
1753 other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID;
1754 other_name->value.hardware_module_name.oid.p = p;
1755 other_name->value.hardware_module_name.oid.len = len;
1756
Ron Eldor890819a2019-05-13 19:03:04 +03001757 if( p + len >= end )
1758 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001759 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001760 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1761 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001762 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001763 p += len;
1764 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1765 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001766 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001767
1768 other_name->value.hardware_module_name.val.tag = MBEDTLS_ASN1_OCTET_STRING;
1769 other_name->value.hardware_module_name.val.p = p;
1770 other_name->value.hardware_module_name.val.len = len;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001771 p += len;
1772 if( p != end )
1773 {
Ron Eldor890819a2019-05-13 19:03:04 +03001774 mbedtls_platform_zeroize( other_name,
Sébastien Duquette661d7252019-06-23 17:45:26 -04001775 sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001776 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1777 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001778 }
1779 return( 0 );
1780}
1781
Peter Kolbus9a969b62018-12-11 13:55:56 -06001782int mbedtls_x509_parse_subject_alt_name( const mbedtls_x509_buf *san_buf,
1783 mbedtls_x509_subject_alternative_name *san )
1784{
1785 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1786 switch( san_buf->tag &
1787 ( MBEDTLS_ASN1_TAG_CLASS_MASK |
1788 MBEDTLS_ASN1_TAG_VALUE_MASK ) )
1789 {
1790 /*
1791 * otherName
1792 */
1793 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ):
1794 {
1795 mbedtls_x509_san_other_name other_name;
1796
1797 ret = x509_get_other_name( san_buf, &other_name );
1798 if( ret != 0 )
1799 return( ret );
1800
1801 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1802 san->type = MBEDTLS_X509_SAN_OTHER_NAME;
1803 memcpy( &san->san.other_name,
1804 &other_name, sizeof( other_name ) );
1805
1806 }
1807 break;
1808
1809 /*
1810 * dNSName
1811 */
1812 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME ):
1813 {
1814 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1815 san->type = MBEDTLS_X509_SAN_DNS_NAME;
1816
1817 memcpy( &san->san.unstructured_name,
1818 san_buf, sizeof( *san_buf ) );
1819
1820 }
1821 break;
1822
1823 /*
1824 * Type not supported
1825 */
1826 default:
1827 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1828 }
1829 return( 0 );
1830}
1831
1832#if !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001833static int x509_info_subject_alt_name( char **buf, size_t *size,
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001834 const mbedtls_x509_sequence
1835 *subject_alt_name,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001836 const char *prefix )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001837{
Janos Follath865b3eb2019-12-16 11:46:15 +00001838 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001839 size_t n = *size;
1840 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001841 const mbedtls_x509_sequence *cur = subject_alt_name;
Ron Eldor890819a2019-05-13 19:03:04 +03001842 mbedtls_x509_subject_alternative_name san;
1843 int parse_ret;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001844
1845 while( cur != NULL )
1846 {
Ron Eldor890819a2019-05-13 19:03:04 +03001847 memset( &san, 0, sizeof( san ) );
1848 parse_ret = mbedtls_x509_parse_subject_alt_name( &cur->buf, &san );
1849 if( parse_ret != 0 )
1850 {
1851 if( parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1852 {
1853 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
1854 MBEDTLS_X509_SAFE_SNPRINTF;
1855 }
1856 else
1857 {
1858 ret = mbedtls_snprintf( p, n, "\n%s <malformed>", prefix );
1859 MBEDTLS_X509_SAFE_SNPRINTF;
1860 }
1861 cur = cur->next;
1862 continue;
1863 }
1864
1865 switch( san.type )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001866 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001867 /*
1868 * otherName
1869 */
Ron Eldora2913912019-05-16 16:17:38 +03001870 case MBEDTLS_X509_SAN_OTHER_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001871 {
1872 mbedtls_x509_san_other_name *other_name = &san.san.other_name;
1873
1874 ret = mbedtls_snprintf( p, n, "\n%s otherName :", prefix );
1875 MBEDTLS_X509_SAFE_SNPRINTF;
1876
1877 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME,
1878 &other_name->value.hardware_module_name.oid ) != 0 )
Janos Follath22f605f2019-05-10 10:37:17 +01001879 {
Ron Eldor890819a2019-05-13 19:03:04 +03001880 ret = mbedtls_snprintf( p, n, "\n%s hardware module name :", prefix );
1881 MBEDTLS_X509_SAFE_SNPRINTF;
1882 ret = mbedtls_snprintf( p, n, "\n%s hardware type : ", prefix );
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001883 MBEDTLS_X509_SAFE_SNPRINTF;
1884
Ron Eldor890819a2019-05-13 19:03:04 +03001885 ret = mbedtls_oid_get_numeric_string( p, n, &other_name->value.hardware_module_name.oid );
1886 MBEDTLS_X509_SAFE_SNPRINTF;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001887
Ron Eldor890819a2019-05-13 19:03:04 +03001888 ret = mbedtls_snprintf( p, n, "\n%s hardware serial number : ", prefix );
1889 MBEDTLS_X509_SAFE_SNPRINTF;
1890
1891 if( other_name->value.hardware_module_name.val.len >= n )
1892 {
1893 *p = '\0';
1894 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Janos Follath22f605f2019-05-10 10:37:17 +01001895 }
1896
Ron Eldora2913912019-05-16 16:17:38 +03001897 memcpy( p, other_name->value.hardware_module_name.val.p,
1898 other_name->value.hardware_module_name.val.len );
1899 p += other_name->value.hardware_module_name.val.len;
1900
Ron Eldor890819a2019-05-13 19:03:04 +03001901 n -= other_name->value.hardware_module_name.val.len;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001902
Ron Eldor890819a2019-05-13 19:03:04 +03001903 }/* MBEDTLS_OID_ON_HW_MODULE_NAME */
1904 }
1905 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001906
1907 /*
1908 * dNSName
1909 */
Ron Eldora2913912019-05-16 16:17:38 +03001910 case MBEDTLS_X509_SAN_DNS_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001911 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001912 ret = mbedtls_snprintf( p, n, "\n%s dNSName : ", prefix );
1913 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldor890819a2019-05-13 19:03:04 +03001914 if( san.san.unstructured_name.len >= n )
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001915 {
1916 *p = '\0';
1917 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
1918 }
Ron Eldora2913912019-05-16 16:17:38 +03001919
1920 memcpy( p, san.san.unstructured_name.p, san.san.unstructured_name.len );
1921 p += san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001922 n -= san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001923 }
1924 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001925
1926 /*
Ron Eldor890819a2019-05-13 19:03:04 +03001927 * Type not supported, skip item.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001928 */
1929 default:
Janos Follath22f605f2019-05-10 10:37:17 +01001930 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
1931 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001932 break;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001933 }
1934
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001935 cur = cur->next;
1936 }
1937
1938 *p = '\0';
1939
1940 *size = n;
1941 *buf = p;
1942
1943 return( 0 );
1944}
1945
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001946#define PRINT_ITEM(i) \
1947 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001948 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001949 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001950 sep = ", "; \
1951 }
1952
1953#define CERT_TYPE(type,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001954 if( ns_cert_type & (type) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001955 PRINT_ITEM( name );
1956
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001957static int x509_info_cert_type( char **buf, size_t *size,
1958 unsigned char ns_cert_type )
1959{
Janos Follath865b3eb2019-12-16 11:46:15 +00001960 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001961 size_t n = *size;
1962 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001963 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001965 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001966 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001967 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
1968 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1969 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001970 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
1971 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1972 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001973
1974 *size = n;
1975 *buf = p;
1976
1977 return( 0 );
1978}
1979
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001980#define KEY_USAGE(code,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001981 if( key_usage & (code) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001982 PRINT_ITEM( name );
1983
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001984static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001985 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001986{
Janos Follath865b3eb2019-12-16 11:46:15 +00001987 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001988 size_t n = *size;
1989 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001990 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001992 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
1993 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001994 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1995 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1996 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001997 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
1998 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001999 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
2000 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002001
2002 *size = n;
2003 *buf = p;
2004
2005 return( 0 );
2006}
2007
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002008static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002009 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002010{
Janos Follath865b3eb2019-12-16 11:46:15 +00002011 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002012 const char *desc;
2013 size_t n = *size;
2014 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002015 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002016 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002017
2018 while( cur != NULL )
2019 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002020 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002021 desc = "???";
2022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002023 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002024 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002025
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002026 sep = ", ";
2027
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002028 cur = cur->next;
2029 }
2030
2031 *size = n;
2032 *buf = p;
2033
2034 return( 0 );
2035}
2036
Ron Eldor74d9acc2019-03-21 14:00:03 +02002037static int x509_info_cert_policies( char **buf, size_t *size,
2038 const mbedtls_x509_sequence *certificate_policies )
2039{
Janos Follath865b3eb2019-12-16 11:46:15 +00002040 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor74d9acc2019-03-21 14:00:03 +02002041 const char *desc;
2042 size_t n = *size;
2043 char *p = *buf;
2044 const mbedtls_x509_sequence *cur = certificate_policies;
2045 const char *sep = "";
2046
2047 while( cur != NULL )
2048 {
2049 if( mbedtls_oid_get_certificate_policies( &cur->buf, &desc ) != 0 )
2050 desc = "???";
2051
2052 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
2053 MBEDTLS_X509_SAFE_SNPRINTF;
2054
2055 sep = ", ";
2056
2057 cur = cur->next;
2058 }
2059
2060 *size = n;
2061 *buf = p;
2062
2063 return( 0 );
2064}
2065
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002066/*
2067 * Return an informational string about the certificate.
2068 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002069#define BEFORE_COLON 18
2070#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002071int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
2072 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002073{
Janos Follath865b3eb2019-12-16 11:46:15 +00002074 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002075 size_t n;
2076 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002077 char key_size_str[BEFORE_COLON];
2078
2079 p = buf;
2080 n = size;
2081
Janos Follath98e28a72016-05-31 14:03:54 +01002082 if( NULL == crt )
2083 {
2084 ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" );
2085 MBEDTLS_X509_SAFE_SNPRINTF;
2086
2087 return( (int) ( size - n ) );
2088 }
2089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002090 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002091 prefix, crt->version );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002092 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002093 ret = mbedtls_snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002094 prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002095 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002097 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
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%sissuer name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002101 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002102 ret = mbedtls_x509_dn_gets( p, n, &crt->issuer );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002103 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002105 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002106 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002107 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002108 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002110 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002111 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2112 crt->valid_from.year, crt->valid_from.mon,
2113 crt->valid_from.day, crt->valid_from.hour,
2114 crt->valid_from.min, crt->valid_from.sec );
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_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002118 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2119 crt->valid_to.year, crt->valid_to.mon,
2120 crt->valid_to.day, crt->valid_to.hour,
2121 crt->valid_to.min, crt->valid_to.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002122 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002124 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002125 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002127 ret = mbedtls_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02002128 crt->sig_md, crt->sig_opts );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002129 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002130
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002131 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002132 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
2133 mbedtls_pk_get_name( &crt->pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002134 {
2135 return( ret );
2136 }
2137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002138 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +02002139 (int) mbedtls_pk_get_bitlen( &crt->pk ) );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002140 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002141
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002142 /*
2143 * Optional extensions
2144 */
2145
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002146 if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002148 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002149 crt->ca_istrue ? "true" : "false" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002150 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002151
2152 if( crt->max_pathlen > 0 )
2153 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002154 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002155 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002156 }
2157 }
2158
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002159 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002160 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002161 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name :", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002162 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002163
2164 if( ( ret = x509_info_subject_alt_name( &p, &n,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002165 &crt->subject_alt_names,
Ron Eldor6aeae9e2019-05-20 12:00:36 +03002166 prefix ) ) != 0 )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002167 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002168 }
2169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002170 if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002171 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002172 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002173 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002174
2175 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
2176 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002177 }
2178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002179 if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002180 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002181 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002182 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002183
2184 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
2185 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002186 }
2187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002188 if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002189 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002190 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002191 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002192
2193 if( ( ret = x509_info_ext_key_usage( &p, &n,
2194 &crt->ext_key_usage ) ) != 0 )
2195 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002196 }
2197
Ron Eldor74d9acc2019-03-21 14:00:03 +02002198 if( crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES )
2199 {
2200 ret = mbedtls_snprintf( p, n, "\n%scertificate policies : ", prefix );
2201 MBEDTLS_X509_SAFE_SNPRINTF;
2202
2203 if( ( ret = x509_info_cert_policies( &p, &n,
2204 &crt->certificate_policies ) ) != 0 )
2205 return( ret );
2206 }
2207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002208 ret = mbedtls_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002209 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002210
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002211 return( (int) ( size - n ) );
2212}
2213
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002214struct x509_crt_verify_string {
2215 int code;
2216 const char *string;
2217};
2218
Hanno Becker7ac83f92020-10-09 10:48:22 +01002219#define X509_CRT_ERROR_INFO( err, err_str, info ) { err, info },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002220static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Hanno Becker7ac83f92020-10-09 10:48:22 +01002221 MBEDTLS_X509_CRT_ERROR_INFO_LIST
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002222 { 0, NULL }
2223};
Hanno Becker7ac83f92020-10-09 10:48:22 +01002224#undef X509_CRT_ERROR_INFO
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002225
2226int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002227 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002228{
Janos Follath865b3eb2019-12-16 11:46:15 +00002229 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002230 const struct x509_crt_verify_string *cur;
2231 char *p = buf;
2232 size_t n = size;
2233
2234 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
2235 {
2236 if( ( flags & cur->code ) == 0 )
2237 continue;
2238
2239 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002240 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002241 flags ^= cur->code;
2242 }
2243
2244 if( flags != 0 )
2245 {
2246 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
2247 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002248 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002249 }
2250
2251 return( (int) ( size - n ) );
2252}
Hanno Becker612a2f12020-10-09 09:19:39 +01002253#endif /* MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002254
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002255int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
2256 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002257{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002258 unsigned int usage_must, usage_may;
2259 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
2260 | MBEDTLS_X509_KU_DECIPHER_ONLY;
2261
2262 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
2263 return( 0 );
2264
2265 usage_must = usage & ~may_mask;
2266
2267 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
2268 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
2269
2270 usage_may = usage & may_mask;
2271
2272 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002273 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002274
2275 return( 0 );
2276}
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002278int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002279 const char *usage_oid,
2280 size_t usage_len )
2281{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002282 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002283
2284 /* Extension is not mandatory, absent means no restriction */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002285 if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002286 return( 0 );
2287
2288 /*
2289 * Look for the requested usage (or wildcard ANY) in our list
2290 */
2291 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
2292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002293 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002294
2295 if( cur_oid->len == usage_len &&
2296 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
2297 {
2298 return( 0 );
2299 }
2300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002301 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002302 return( 0 );
2303 }
2304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002305 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002306}
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002308#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002309/*
2310 * Return 1 if the certificate is revoked, or 0 otherwise.
2311 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002312int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002313{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002314 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002315
2316 while( cur != NULL && cur->serial.len != 0 )
2317 {
2318 if( crt->serial.len == cur->serial.len &&
2319 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
2320 {
Raoul Strackxa4e86142020-06-15 17:03:13 +02002321 return( 1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002322 }
2323
2324 cur = cur->next;
2325 }
2326
2327 return( 0 );
2328}
2329
2330/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002331 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002332 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002333 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002334static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002335 mbedtls_x509_crl *crl_list,
2336 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002337{
2338 int flags = 0;
pespacek7599a772022-02-07 14:40:55 +01002339#if defined(MBEDTLS_USE_PSA_CRYPTO)
2340 unsigned char hash[PSA_HASH_MAX_SIZE];
2341 psa_algorithm_t psa_algorithm;
2342#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002343 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2344 const mbedtls_md_info_t *md_info;
pespacek7599a772022-02-07 14:40:55 +01002345#endif /* MBEDTLS_USE_PSA_CRYPTO */
2346 size_t hash_length;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002347
2348 if( ca == NULL )
2349 return( flags );
2350
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002351 while( crl_list != NULL )
2352 {
2353 if( crl_list->version == 0 ||
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002354 x509_name_cmp( &crl_list->issuer, &ca->subject ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002355 {
2356 crl_list = crl_list->next;
2357 continue;
2358 }
2359
2360 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002361 * Check if the CA is configured to sign CRLs
2362 */
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002363 if( mbedtls_x509_crt_check_key_usage( ca,
2364 MBEDTLS_X509_KU_CRL_SIGN ) != 0 )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002365 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002366 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002367 break;
2368 }
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002369
2370 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002371 * Check if CRL is correctly signed by the trusted CA
2372 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002373 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
2374 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
2375
2376 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
2377 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
2378
pespacek7599a772022-02-07 14:40:55 +01002379#if defined(MBEDTLS_USE_PSA_CRYPTO)
2380 psa_algorithm = mbedtls_psa_translate_md( crl_list->sig_md );
2381 if(psa_hash_compute( psa_algorithm,
2382 crl_list->tbs.p,
2383 crl_list->tbs.len,
pespacekb9f07a72022-02-14 15:13:26 +01002384 hash,
2385 sizeof( hash ),
pespacek7599a772022-02-07 14:40:55 +01002386 &hash_length ) != PSA_SUCCESS )
pespaceka7a64692022-02-14 15:18:43 +01002387 {
2388 /* Note: this can't happen except after an internal error */
2389 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
2390 break;
2391 }
pespacek7599a772022-02-07 14:40:55 +01002392#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002393 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
pespacek7599a772022-02-07 14:40:55 +01002394 hash_length = mbedtls_md_get_size( md_info );
2395 if( mbedtls_md( md_info,
2396 crl_list->tbs.p,
2397 crl_list->tbs.len,
2398 hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002399 {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002400 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002401 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002402 break;
2403 }
pespaceka7a64692022-02-14 15:18:43 +01002404#endif /* MBEDTLS_USE_PSA_CRYPTO */
2405
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002406
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002407 if( x509_profile_check_key( profile, &ca->pk ) != 0 )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002408 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002410 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
pespacek7599a772022-02-07 14:40:55 +01002411 crl_list->sig_md, hash, hash_length,
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02002412 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002413 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002414 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002415 break;
2416 }
2417
2418 /*
2419 * Check for validity of CRL (Do not drop out)
2420 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002421 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002422 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002423
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002424 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002425 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002426
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002427 /*
2428 * Check if certificate is revoked
2429 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002430 if( mbedtls_x509_crt_is_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002432 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002433 break;
2434 }
2435
2436 crl_list = crl_list->next;
2437 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002438
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002439 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002440}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002441#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002442
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002443/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002444 * Check the signature of a certificate by its parent
2445 */
2446static int x509_crt_check_signature( const mbedtls_x509_crt *child,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002447 mbedtls_x509_crt *parent,
2448 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002449{
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002450 size_t hash_len;
2451#if !defined(MBEDTLS_USE_PSA_CRYPTO)
pespacek7599a772022-02-07 14:40:55 +01002452 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002453 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002454 md_info = mbedtls_md_info_from_type( child->sig_md );
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002455 hash_len = mbedtls_md_get_size( md_info );
Andrzej Kurek8b38ff52018-11-20 03:20:09 -05002456
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002457 /* Note: hash errors can happen only after an internal error */
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002458 if( mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash ) != 0 )
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002459 return( -1 );
2460#else
pespacek7599a772022-02-07 14:40:55 +01002461 unsigned char hash[PSA_HASH_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002462 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( child->sig_md );
pespacek7599a772022-02-07 14:40:55 +01002463 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002464
pespacek7599a772022-02-07 14:40:55 +01002465 status = psa_hash_compute( hash_alg,
2466 child->tbs.p,
2467 child->tbs.len,
2468 hash,
pespacekb9f07a72022-02-14 15:13:26 +01002469 sizeof( hash ),
pespacek7599a772022-02-07 14:40:55 +01002470 &hash_len );
2471 if( status != PSA_SUCCESS )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002472 {
pespacek3110c7b2022-02-14 15:07:41 +01002473 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002474 }
2475
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002476#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002477 /* Skip expensive computation on obvious mismatch */
2478 if( ! mbedtls_pk_can_do( &parent->pk, child->sig_pk ) )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002479 return( -1 );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002480
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002481#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002482 if( rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA )
2483 {
2484 return( mbedtls_pk_verify_restartable( &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002485 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002486 child->sig.p, child->sig.len, &rs_ctx->pk ) );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002487 }
2488#else
2489 (void) rs_ctx;
2490#endif
2491
2492 return( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002493 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002494 child->sig.p, child->sig.len ) );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002495}
2496
2497/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002498 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2499 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002500 *
2501 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002502 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002503static int x509_crt_check_parent( const mbedtls_x509_crt *child,
2504 const mbedtls_x509_crt *parent,
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002505 int top )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002506{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002507 int need_ca_bit;
2508
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002509 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02002510 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002511 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002512
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002513 /* Parent must have the basicConstraints CA bit set as a general rule */
2514 need_ca_bit = 1;
2515
2516 /* Exception: v1/v2 certificates that are locally trusted. */
2517 if( top && parent->version < 3 )
2518 need_ca_bit = 0;
2519
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002520 if( need_ca_bit && ! parent->ca_istrue )
2521 return( -1 );
2522
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002523 if( need_ca_bit &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002524 mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002525 {
2526 return( -1 );
2527 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002528
2529 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002530}
2531
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002532/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002533 * Find a suitable parent for child in candidates, or return NULL.
2534 *
2535 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002536 * 1. subject name matches child's issuer
2537 * 2. if necessary, the CA bit is set and key usage allows signing certs
2538 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002539 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002540 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002541 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002542 * If there's a suitable candidate which is also time-valid, return the first
2543 * such. Otherwise, return the first suitable candidate (or NULL if there is
2544 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002545 *
2546 * The rationale for this rule is that someone could have a list of trusted
2547 * roots with two versions on the same root with different validity periods.
2548 * (At least one user reported having such a list and wanted it to just work.)
2549 * The reason we don't just require time-validity is that generally there is
2550 * only one version, and if it's expired we want the flags to state that
2551 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002552 *
2553 * The rationale for rule 3 (signature for trusted roots) is that users might
2554 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002555 * way we select the correct one is by checking the signature (as we don't
2556 * rely on key identifier extensions). (This is one way users might choose to
2557 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002558 *
2559 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002560 * - [in] child: certificate for which we're looking for a parent
2561 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002562 * - [out] r_parent: parent found (or NULL)
2563 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002564 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2565 * of the chain, 0 otherwise
2566 * - [in] path_cnt: number of intermediates seen so far
2567 * - [in] self_cnt: number of self-signed intermediates seen so far
2568 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002569 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002570 *
2571 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002572 * - 0 on success
2573 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002574 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002575static int x509_crt_find_parent_in(
2576 mbedtls_x509_crt *child,
2577 mbedtls_x509_crt *candidates,
2578 mbedtls_x509_crt **r_parent,
2579 int *r_signature_is_good,
2580 int top,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002581 unsigned path_cnt,
2582 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002583 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002584{
Janos Follath865b3eb2019-12-16 11:46:15 +00002585 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002586 mbedtls_x509_crt *parent, *fallback_parent;
Benjamin Kier36050732019-05-30 14:49:17 -04002587 int signature_is_good = 0, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002588
2589#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002590 /* did we have something in progress? */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002591 if( rs_ctx != NULL && rs_ctx->parent != NULL )
2592 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002593 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002594 parent = rs_ctx->parent;
2595 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002596 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002597
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002598 /* clear saved state */
2599 rs_ctx->parent = NULL;
2600 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002601 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002602
2603 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002604 goto check_signature;
2605 }
2606#endif
2607
2608 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002609 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002610
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002611 for( parent = candidates; parent != NULL; parent = parent->next )
2612 {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002613 /* basic parenting skills (name, CA bit, key usage) */
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002614 if( x509_crt_check_parent( child, parent, top ) != 0 )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002615 continue;
2616
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002617 /* +1 because stored max_pathlen is 1 higher that the actual value */
2618 if( parent->max_pathlen > 0 &&
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002619 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt )
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002620 {
2621 continue;
2622 }
2623
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002624 /* Signature */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002625#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2626check_signature:
2627#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002628 ret = x509_crt_check_signature( child, parent, rs_ctx );
2629
2630#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002631 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2632 {
2633 /* save state */
2634 rs_ctx->parent = parent;
2635 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002636 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002637
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002638 return( ret );
2639 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002640#else
2641 (void) ret;
2642#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002643
2644 signature_is_good = ret == 0;
2645 if( top && ! signature_is_good )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002646 continue;
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002647
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002648 /* optional time check */
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002649 if( mbedtls_x509_time_is_past( &parent->valid_to ) ||
2650 mbedtls_x509_time_is_future( &parent->valid_from ) )
2651 {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002652 if( fallback_parent == NULL )
2653 {
2654 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002655 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002656 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002657
2658 continue;
2659 }
2660
Andy Gross1f627142019-01-30 10:25:53 -06002661 *r_parent = parent;
2662 *r_signature_is_good = signature_is_good;
2663
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002664 break;
2665 }
2666
Andy Gross1f627142019-01-30 10:25:53 -06002667 if( parent == NULL )
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002668 {
2669 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002670 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002671 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002672
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002673 return( 0 );
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002674}
2675
2676/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002677 * Find a parent in trusted CAs or the provided chain, or return NULL.
2678 *
2679 * Searches in trusted CAs first, and return the first suitable parent found
2680 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002681 *
2682 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002683 * - [in] child: certificate for which we're looking for a parent, followed
2684 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002685 * - [in] trust_ca: list of locally trusted certificates
2686 * - [out] parent: parent found (or NULL)
2687 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2688 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2689 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2690 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002691 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002692 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002693 *
2694 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002695 * - 0 on success
2696 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002697 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002698static int x509_crt_find_parent(
2699 mbedtls_x509_crt *child,
2700 mbedtls_x509_crt *trust_ca,
2701 mbedtls_x509_crt **parent,
2702 int *parent_is_trusted,
2703 int *signature_is_good,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002704 unsigned path_cnt,
2705 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002706 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002707{
Janos Follath865b3eb2019-12-16 11:46:15 +00002708 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002709 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002710
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002711 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002712
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002713#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002714 /* restore then clear saved state if we have some stored */
2715 if( rs_ctx != NULL && rs_ctx->parent_is_trusted != -1 )
2716 {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002717 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002718 rs_ctx->parent_is_trusted = -1;
2719 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002720#endif
2721
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002722 while( 1 ) {
2723 search_list = *parent_is_trusted ? trust_ca : child->next;
2724
2725 ret = x509_crt_find_parent_in( child, search_list,
2726 parent, signature_is_good,
2727 *parent_is_trusted,
2728 path_cnt, self_cnt, rs_ctx );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002729
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002730#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002731 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2732 {
2733 /* save state */
2734 rs_ctx->parent_is_trusted = *parent_is_trusted;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002735 return( ret );
2736 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002737#else
2738 (void) ret;
2739#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002740
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002741 /* stop here if found or already in second iteration */
2742 if( *parent != NULL || *parent_is_trusted == 0 )
2743 break;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002744
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002745 /* prepare second iteration */
2746 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002747 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002748
2749 /* extra precaution against mistakes in the caller */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002750 if( *parent == NULL )
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002751 {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002752 *parent_is_trusted = 0;
2753 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002754 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002755
2756 return( 0 );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002757}
2758
2759/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002760 * Check if an end-entity certificate is locally trusted
2761 *
2762 * Currently we require such certificates to be self-signed (actually only
2763 * check for self-issued as self-signatures are not checked)
2764 */
2765static int x509_crt_check_ee_locally_trusted(
2766 mbedtls_x509_crt *crt,
2767 mbedtls_x509_crt *trust_ca )
2768{
2769 mbedtls_x509_crt *cur;
2770
2771 /* must be self-issued */
2772 if( x509_name_cmp( &crt->issuer, &crt->subject ) != 0 )
2773 return( -1 );
2774
2775 /* look for an exact match with trusted cert */
2776 for( cur = trust_ca; cur != NULL; cur = cur->next )
2777 {
2778 if( crt->raw.len == cur->raw.len &&
2779 memcmp( crt->raw.p, cur->raw.p, crt->raw.len ) == 0 )
2780 {
2781 return( 0 );
2782 }
2783 }
2784
2785 /* too bad */
2786 return( -1 );
2787}
2788
2789/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002790 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002791 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002792 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2793 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002794 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002795 * such that every cert in the chain is a child of the next one,
2796 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002797 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002798 * Verify that chain and return it with flags for all issues found.
2799 *
2800 * Special cases:
2801 * - EE == Rj -> return a one-element list containing it
2802 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2803 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002804 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002805 * Tests for (aspects of) this function should include at least:
2806 * - trusted EE
2807 * - EE -> trusted root
Antonin Décimo36e89b52019-01-23 15:24:37 +01002808 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002809 * - if relevant: EE untrusted
2810 * - if relevant: EE -> intermediate, untrusted
2811 * with the aspect under test checked at each relevant level (EE, int, root).
2812 * For some aspects longer chains are required, but usually length 2 is
2813 * enough (but length 1 is not in general).
2814 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002815 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002816 * - [in] crt: the cert list EE, C1, ..., Cn
2817 * - [in] trust_ca: the trusted list R1, ..., Rp
2818 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002819 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002820 * Only valid when return value is 0, may contain garbage otherwise!
2821 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002822 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002823 *
2824 * Return value:
2825 * - non-zero if the chain could not be fully built and examined
2826 * - 0 is the chain was successfully built and examined,
2827 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002828 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002829static int x509_crt_verify_chain(
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002830 mbedtls_x509_crt *crt,
2831 mbedtls_x509_crt *trust_ca,
2832 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00002833 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2834 void *p_ca_cb,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002835 const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002836 mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002837 mbedtls_x509_crt_restart_ctx *rs_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002838{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002839 /* Don't initialize any of those variables here, so that the compiler can
2840 * catch potential issues with jumping ahead when restarting */
Janos Follath865b3eb2019-12-16 11:46:15 +00002841 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002842 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002843 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002844 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002845 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002846 int parent_is_trusted;
2847 int child_is_trusted;
2848 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002849 unsigned self_cnt;
Hanno Beckerf53893b2019-03-28 13:45:55 +00002850 mbedtls_x509_crt *cur_trust_ca = NULL;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002851
2852#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2853 /* resume if we had an operation in progress */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002854 if( rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent )
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002855 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002856 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002857 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002858 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002859
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002860 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002861 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002862 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002863 flags = &cur->flags;
2864
2865 goto find_parent;
2866 }
2867#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002868
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002869 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002870 self_cnt = 0;
2871 parent_is_trusted = 0;
2872 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002873
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002874 while( 1 ) {
2875 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002876 cur = &ver_chain->items[ver_chain->len];
2877 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002878 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002879 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002880 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002881
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002882 /* Check time-validity (all certificates) */
2883 if( mbedtls_x509_time_is_past( &child->valid_to ) )
2884 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002885
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002886 if( mbedtls_x509_time_is_future( &child->valid_from ) )
2887 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002888
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002889 /* Stop here for trusted roots (but not for trusted EE certs) */
2890 if( child_is_trusted )
2891 return( 0 );
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002892
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002893 /* Check signature algorithm: MD & PK algs */
2894 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
2895 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002896
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002897 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
2898 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002899
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002900 /* Special case: EE certs that are locally trusted */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002901 if( ver_chain->len == 1 &&
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002902 x509_crt_check_ee_locally_trusted( child, trust_ca ) == 0 )
2903 {
2904 return( 0 );
2905 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002906
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002907#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2908find_parent:
2909#endif
Hanno Beckerf53893b2019-03-28 13:45:55 +00002910
2911 /* Obtain list of potential trusted signers from CA callback,
2912 * or use statically provided list. */
2913#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
2914 if( f_ca_cb != NULL )
2915 {
2916 mbedtls_x509_crt_free( ver_chain->trust_ca_cb_result );
2917 mbedtls_free( ver_chain->trust_ca_cb_result );
2918 ver_chain->trust_ca_cb_result = NULL;
2919
2920 ret = f_ca_cb( p_ca_cb, child, &ver_chain->trust_ca_cb_result );
2921 if( ret != 0 )
2922 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2923
2924 cur_trust_ca = ver_chain->trust_ca_cb_result;
2925 }
2926 else
2927#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2928 {
2929 ((void) f_ca_cb);
2930 ((void) p_ca_cb);
2931 cur_trust_ca = trust_ca;
2932 }
2933
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002934 /* Look for a parent in trusted CAs or up the chain */
Hanno Beckerf53893b2019-03-28 13:45:55 +00002935 ret = x509_crt_find_parent( child, cur_trust_ca, &parent,
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002936 &parent_is_trusted, &signature_is_good,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002937 ver_chain->len - 1, self_cnt, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002938
2939#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002940 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2941 {
2942 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002943 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002944 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002945 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002946
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002947 return( ret );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002948 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002949#else
2950 (void) ret;
2951#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002952
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002953 /* No parent? We're done here */
2954 if( parent == NULL )
2955 {
2956 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2957 return( 0 );
2958 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002959
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002960 /* Count intermediate self-issued (not necessarily self-signed) certs.
2961 * These can occur with some strategies for key rollover, see [SIRO],
2962 * and should be excluded from max_pathlen checks. */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002963 if( ver_chain->len != 1 &&
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002964 x509_name_cmp( &child->issuer, &child->subject ) == 0 )
2965 {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002966 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002967 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002968
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002969 /* path_cnt is 0 for the first intermediate CA,
2970 * and if parent is trusted it's not an intermediate CA */
2971 if( ! parent_is_trusted &&
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002972 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002973 {
2974 /* return immediately to avoid overflow the chain array */
2975 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2976 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002977
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002978 /* signature was checked while searching parent */
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002979 if( ! signature_is_good )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002980 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2981
2982 /* check size of signing key */
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002983 if( x509_profile_check_key( profile, &parent->pk ) != 0 )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002984 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002986#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002987 /* Check trusted CA's CRL for the given crt */
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002988 *flags |= x509_crt_verifycrl( child, parent, ca_crl, profile );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002989#else
2990 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002991#endif
2992
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002993 /* prepare for next iteration */
2994 child = parent;
2995 parent = NULL;
2996 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002997 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002998 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002999}
3000
3001/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003002 * Check for CN match
3003 */
3004static int x509_crt_check_cn( const mbedtls_x509_buf *name,
3005 const char *cn, size_t cn_len )
3006{
3007 /* try exact match */
3008 if( name->len == cn_len &&
3009 x509_memcasecmp( cn, name->p, cn_len ) == 0 )
3010 {
3011 return( 0 );
3012 }
3013
3014 /* try wildcard match */
Manuel Pégourié-Gonnard900fba62017-10-18 14:28:11 +02003015 if( x509_check_wildcard( cn, name ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003016 {
3017 return( 0 );
3018 }
3019
3020 return( -1 );
3021}
3022
3023/*
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003024 * Check for SAN match, see RFC 5280 Section 4.2.1.6
3025 */
3026static int x509_crt_check_san( const mbedtls_x509_buf *name,
3027 const char *cn, size_t cn_len )
3028{
3029 const unsigned char san_type = (unsigned char) name->tag &
3030 MBEDTLS_ASN1_TAG_VALUE_MASK;
3031
3032 /* dNSName */
3033 if( san_type == MBEDTLS_X509_SAN_DNS_NAME )
3034 return( x509_crt_check_cn( name, cn, cn_len ) );
3035
3036 /* (We may handle other types here later.) */
3037
3038 /* Unrecognized type */
3039 return( -1 );
3040}
3041
3042/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003043 * Verify the requested CN - only call this if cn is not NULL!
3044 */
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003045static void x509_crt_verify_name( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003046 const char *cn,
3047 uint32_t *flags )
3048{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003049 const mbedtls_x509_name *name;
3050 const mbedtls_x509_sequence *cur;
3051 size_t cn_len = strlen( cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003052
3053 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
3054 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003055 for( cur = &crt->subject_alt_names; cur != NULL; cur = cur->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003056 {
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003057 if( x509_crt_check_san( &cur->buf, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003058 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003059 }
3060
3061 if( cur == NULL )
3062 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3063 }
3064 else
3065 {
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02003066 for( name = &crt->subject; name != NULL; name = name->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003067 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003068 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, &name->oid ) == 0 &&
3069 x509_crt_check_cn( &name->val, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003070 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003071 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003072 }
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003073 }
3074
3075 if( name == NULL )
3076 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3077 }
3078}
3079
3080/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003081 * Merge the flags for all certs in the chain, after calling callback
3082 */
3083static int x509_crt_merge_flags_with_cb(
3084 uint32_t *flags,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003085 const mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003086 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3087 void *p_vrfy )
3088{
Janos Follath865b3eb2019-12-16 11:46:15 +00003089 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003090 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003091 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003092 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003093
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003094 for( i = ver_chain->len; i != 0; --i )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003095 {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003096 cur = &ver_chain->items[i-1];
3097 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003098
3099 if( NULL != f_vrfy )
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003100 if( ( ret = f_vrfy( p_vrfy, cur->crt, (int) i-1, &cur_flags ) ) != 0 )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003101 return( ret );
3102
3103 *flags |= cur_flags;
3104 }
3105
3106 return( 0 );
3107}
3108
3109/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003110 * Verify the certificate validity, with profile, restartable version
3111 *
3112 * This function:
3113 * - checks the requested CN (if any)
3114 * - checks the type and size of the EE cert's key,
3115 * as that isn't done as part of chain building/verification currently
3116 * - builds and verifies the chain
3117 * - then calls the callback and merges the flags
Hanno Becker3116fb32019-03-28 13:34:42 +00003118 *
3119 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
3120 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
3121 * verification routine to search for trusted signers, and CRLs will
3122 * be disabled. Otherwise, `trust_ca` will be used as the static list
3123 * of trusted signers, and `ca_crl` will be use as the static list
3124 * of CRLs.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003125 */
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003126static int x509_crt_verify_restartable_ca_cb( mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003127 mbedtls_x509_crt *trust_ca,
3128 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003129 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3130 void *p_ca_cb,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003131 const mbedtls_x509_crt_profile *profile,
3132 const char *cn, uint32_t *flags,
3133 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3134 void *p_vrfy,
3135 mbedtls_x509_crt_restart_ctx *rs_ctx )
3136{
Janos Follath865b3eb2019-12-16 11:46:15 +00003137 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003138 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003139 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003140 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003141
3142 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003143 ee_flags = 0;
3144 x509_crt_verify_chain_reset( &ver_chain );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003145
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003146 if( profile == NULL )
3147 {
3148 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3149 goto exit;
3150 }
3151
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003152 /* check name if requested */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003153 if( cn != NULL )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003154 x509_crt_verify_name( crt, cn, &ee_flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003155
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003156 /* Check the type and size of the key */
3157 pk_type = mbedtls_pk_get_type( &crt->pk );
3158
3159 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003160 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003161
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02003162 if( x509_profile_check_key( profile, &crt->pk ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003163 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003164
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003165 /* Check the chain */
Hanno Becker3116fb32019-03-28 13:34:42 +00003166 ret = x509_crt_verify_chain( crt, trust_ca, ca_crl,
3167 f_ca_cb, p_ca_cb, profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003168 &ver_chain, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003169
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003170 if( ret != 0 )
3171 goto exit;
3172
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003173 /* Merge end-entity flags */
3174 ver_chain.items[0].flags |= ee_flags;
3175
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003176 /* Build final flags, calling callback on the way if any */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003177 ret = x509_crt_merge_flags_with_cb( flags, &ver_chain, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003178
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003179exit:
Hanno Beckerf53893b2019-03-28 13:45:55 +00003180
3181#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3182 mbedtls_x509_crt_free( ver_chain.trust_ca_cb_result );
3183 mbedtls_free( ver_chain.trust_ca_cb_result );
3184 ver_chain.trust_ca_cb_result = NULL;
3185#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3186
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003187#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3188 if( rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
3189 mbedtls_x509_crt_restart_free( rs_ctx );
3190#endif
3191
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003192 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3193 * the SSL module for authmode optional, but non-zero return from the
3194 * callback means a fatal error so it shouldn't be ignored */
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003195 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
3196 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3197
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003198 if( ret != 0 )
3199 {
3200 *flags = (uint32_t) -1;
3201 return( ret );
3202 }
3203
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003204 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003205 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003206
3207 return( 0 );
3208}
3209
Hanno Becker3116fb32019-03-28 13:34:42 +00003210
3211/*
3212 * Verify the certificate validity (default profile, not restartable)
3213 */
3214int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
3215 mbedtls_x509_crt *trust_ca,
3216 mbedtls_x509_crl *ca_crl,
3217 const char *cn, uint32_t *flags,
3218 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3219 void *p_vrfy )
3220{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003221 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003222 NULL, NULL,
3223 &mbedtls_x509_crt_profile_default,
3224 cn, flags,
3225 f_vrfy, p_vrfy, NULL ) );
3226}
3227
3228/*
3229 * Verify the certificate validity (user-chosen profile, not restartable)
3230 */
3231int mbedtls_x509_crt_verify_with_profile( 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{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003239 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003240 NULL, NULL,
3241 profile, cn, flags,
3242 f_vrfy, p_vrfy, NULL ) );
3243}
3244
3245#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3246/*
3247 * Verify the certificate validity (user-chosen profile, CA callback,
3248 * not restartable).
3249 */
Jarno Lamsa31d9db62019-04-01 14:33:49 +03003250int mbedtls_x509_crt_verify_with_ca_cb( mbedtls_x509_crt *crt,
Hanno Becker3116fb32019-03-28 13:34:42 +00003251 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3252 void *p_ca_cb,
3253 const mbedtls_x509_crt_profile *profile,
3254 const char *cn, uint32_t *flags,
3255 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3256 void *p_vrfy )
3257{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003258 return( x509_crt_verify_restartable_ca_cb( crt, NULL, NULL,
Hanno Becker3116fb32019-03-28 13:34:42 +00003259 f_ca_cb, p_ca_cb,
3260 profile, cn, flags,
3261 f_vrfy, p_vrfy, NULL ) );
3262}
3263#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3264
3265int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
3266 mbedtls_x509_crt *trust_ca,
3267 mbedtls_x509_crl *ca_crl,
3268 const mbedtls_x509_crt_profile *profile,
3269 const char *cn, uint32_t *flags,
3270 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3271 void *p_vrfy,
3272 mbedtls_x509_crt_restart_ctx *rs_ctx )
3273{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003274 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003275 NULL, NULL,
3276 profile, cn, flags,
3277 f_vrfy, p_vrfy, rs_ctx ) );
3278}
3279
3280
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003281/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003282 * Initialize a certificate chain
3283 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003284void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02003285{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003286 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02003287}
3288
3289/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003290 * Unallocate all certificate data
3291 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003292void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003293{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003294 mbedtls_x509_crt *cert_cur = crt;
3295 mbedtls_x509_crt *cert_prv;
3296 mbedtls_x509_name *name_cur;
3297 mbedtls_x509_name *name_prv;
3298 mbedtls_x509_sequence *seq_cur;
3299 mbedtls_x509_sequence *seq_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003300
3301 if( crt == NULL )
3302 return;
3303
3304 do
3305 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003306 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003308#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
3309 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003310#endif
3311
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003312 name_cur = cert_cur->issuer.next;
3313 while( name_cur != NULL )
3314 {
3315 name_prv = name_cur;
3316 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003317 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003318 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003319 }
3320
3321 name_cur = cert_cur->subject.next;
3322 while( name_cur != NULL )
3323 {
3324 name_prv = name_cur;
3325 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003326 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003327 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003328 }
3329
3330 seq_cur = cert_cur->ext_key_usage.next;
3331 while( seq_cur != NULL )
3332 {
3333 seq_prv = seq_cur;
3334 seq_cur = seq_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003335 mbedtls_platform_zeroize( seq_prv,
3336 sizeof( mbedtls_x509_sequence ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003337 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003338 }
3339
3340 seq_cur = cert_cur->subject_alt_names.next;
3341 while( seq_cur != NULL )
3342 {
3343 seq_prv = seq_cur;
3344 seq_cur = seq_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003345 mbedtls_platform_zeroize( seq_prv,
3346 sizeof( mbedtls_x509_sequence ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003347 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003348 }
3349
Ron Eldor74d9acc2019-03-21 14:00:03 +02003350 seq_cur = cert_cur->certificate_policies.next;
3351 while( seq_cur != NULL )
3352 {
3353 seq_prv = seq_cur;
3354 seq_cur = seq_cur->next;
3355 mbedtls_platform_zeroize( seq_prv,
3356 sizeof( mbedtls_x509_sequence ) );
3357 mbedtls_free( seq_prv );
3358 }
3359
Hanno Becker1a65dcd2019-01-31 08:57:44 +00003360 if( cert_cur->raw.p != NULL && cert_cur->own_buffer )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003361 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003362 mbedtls_platform_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003363 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003364 }
3365
3366 cert_cur = cert_cur->next;
3367 }
3368 while( cert_cur != NULL );
3369
3370 cert_cur = crt;
3371 do
3372 {
3373 cert_prv = cert_cur;
3374 cert_cur = cert_cur->next;
3375
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003376 mbedtls_platform_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003377 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003378 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003379 }
3380 while( cert_cur != NULL );
3381}
3382
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003383#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3384/*
3385 * Initialize a restart context
3386 */
3387void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx )
3388{
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003389 mbedtls_pk_restart_init( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003390
3391 ctx->parent = NULL;
3392 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003393 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003394
3395 ctx->parent_is_trusted = -1;
3396
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003397 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003398 ctx->self_cnt = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003399 x509_crt_verify_chain_reset( &ctx->ver_chain );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003400}
3401
3402/*
3403 * Free the components of a restart context
3404 */
3405void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx )
3406{
3407 if( ctx == NULL )
3408 return;
3409
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003410 mbedtls_pk_restart_free( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003411 mbedtls_x509_crt_restart_init( ctx );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003412}
3413#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003415#endif /* MBEDTLS_X509_CRT_PARSE_C */