blob: d96abb28f4a52d793e85d49f388e7ab3924b93bf [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 */
Przemek Stekiel40afdd22022-09-06 13:08:28 +020051#include "hash_info.h"
Andrzej Kurekd4a65532018-10-31 06:18:39 -040052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020055#else
Simon Butcherd2642582018-10-03 15:11:19 +010056#include <stdio.h>
Rich Evans00ab4702015-02-06 13:43:58 +000057#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#define mbedtls_free free
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020059#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#define mbedtls_snprintf snprintf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020061#endif
62
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000064#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010065#endif
66
Daniel Axtensf0710242020-05-28 11:43:41 +100067#if defined(MBEDTLS_HAVE_TIME)
Paul Bakkerfa6a6202013-10-28 18:48:30 +010068#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020069#include <windows.h>
70#else
71#include <time.h>
72#endif
Daniel Axtensf0710242020-05-28 11:43:41 +100073#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075#if defined(MBEDTLS_FS_IO)
Rich Evans00ab4702015-02-06 13:43:58 +000076#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020077#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020078#include <sys/types.h>
79#include <sys/stat.h>
Martino Facchin0ec05ec2021-05-04 11:47:36 +020080#if defined(__MBED__)
81#include <platform/mbed_retarget.h>
82#else
Paul Bakker7c6b2c32013-09-16 13:49:26 +020083#include <dirent.h>
Martino Facchin0ec05ec2021-05-04 11:47:36 +020084#endif /* __MBED__ */
Eduardo Silvae1bfffc2019-04-25 10:43:26 -060085#include <errno.h>
Rich Evans00ab4702015-02-06 13:43:58 +000086#endif /* !_WIN32 || EFIX64 || EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020087#endif
88
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +020089/*
90 * Item in a verification chain: cert and flags for it
91 */
92typedef struct {
93 mbedtls_x509_crt *crt;
94 uint32_t flags;
95} x509_crt_verify_chain_item;
96
97/*
98 * Max size of verification chain: end-entity + intermediates + trusted root
99 */
100#define X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
Paul Bakker34617722014-06-13 17:20:13 +0200101
Gilles Peskineffb92da2021-06-02 00:03:26 +0200102/* Default profile. Do not remove items unless there are serious security
103 * concerns. */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200104const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
105{
Gilles Peskineae270bf2021-06-02 00:05:29 +0200106 /* Hashes from SHA-256 and above. Note that this selection
107 * should be aligned with ssl_preset_default_hashes in ssl_tls.c. */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200108 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
109 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
110 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
111 0xFFFFFFF, /* Any PK alg */
112#if defined(MBEDTLS_ECP_C)
Gilles Peskineae270bf2021-06-02 00:05:29 +0200113 /* Curves at or above 128-bit security level. Note that this selection
114 * should be aligned with ssl_preset_default_curves in ssl_tls.c. */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200115 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
116 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
117 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
118 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
119 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
120 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
Gilles Peskine39957502021-06-17 23:17:52 +0200121 0,
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200122#else
123 0,
124#endif
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200125 2048,
126};
127
Gilles Peskineffb92da2021-06-02 00:03:26 +0200128/* Next-generation profile. Currently identical to the default, but may
129 * be tightened at any time. */
130const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200131{
132 /* Hashes from SHA-256 and above. */
133 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
134 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
135 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
136 0xFFFFFFF, /* Any PK alg */
137#if defined(MBEDTLS_ECP_C)
138 /* Curves at or above 128-bit security level. */
139 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
140 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
141 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
142 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
143 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
144 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
145 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256K1 ),
146#else
147 0,
148#endif
149 2048,
150};
Gilles Peskineffb92da2021-06-02 00:03:26 +0200151
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200152/*
153 * NSA Suite B Profile
154 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200155const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
156{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200157 /* Only SHA-256 and 384 */
158 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
159 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ),
160 /* Only ECDSA */
Ron Eldor85e1dcf2018-02-06 15:59:38 +0200161 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECDSA ) |
162 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECKEY ),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200163#if defined(MBEDTLS_ECP_C)
164 /* Only NIST P-256 and P-384 */
165 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
166 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ),
167#else
168 0,
169#endif
170 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200171};
172
173/*
Manuel Pégourié-Gonnard9d4c2c42021-06-18 09:48:27 +0200174 * Empty / all-forbidden profile
175 */
176const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_none =
177{
178 0,
179 0,
180 0,
181 (uint32_t) -1,
182};
183
184/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200185 * Check md_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200186 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200187 */
188static int x509_profile_check_md_alg( const mbedtls_x509_crt_profile *profile,
189 mbedtls_md_type_t md_alg )
190{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200191 if( md_alg == MBEDTLS_MD_NONE )
192 return( -1 );
193
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200194 if( ( profile->allowed_mds & MBEDTLS_X509_ID_FLAG( md_alg ) ) != 0 )
195 return( 0 );
196
197 return( -1 );
198}
199
200/*
201 * Check pk_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200202 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200203 */
204static int x509_profile_check_pk_alg( const mbedtls_x509_crt_profile *profile,
205 mbedtls_pk_type_t pk_alg )
206{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200207 if( pk_alg == MBEDTLS_PK_NONE )
208 return( -1 );
209
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200210 if( ( profile->allowed_pks & MBEDTLS_X509_ID_FLAG( pk_alg ) ) != 0 )
211 return( 0 );
212
213 return( -1 );
214}
215
216/*
217 * Check key against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200218 * Return 0 if pk is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200219 */
220static int x509_profile_check_key( const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200221 const mbedtls_pk_context *pk )
222{
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200223 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type( pk );
Manuel Pégourié-Gonnard19773ff2017-10-24 10:51:26 +0200224
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200225#if defined(MBEDTLS_RSA_C)
226 if( pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS )
227 {
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200228 if( mbedtls_pk_get_bitlen( pk ) >= profile->rsa_min_bitlen )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200229 return( 0 );
230
231 return( -1 );
232 }
233#endif
234
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200235#if defined(MBEDTLS_ECP_C)
236 if( pk_alg == MBEDTLS_PK_ECDSA ||
237 pk_alg == MBEDTLS_PK_ECKEY ||
238 pk_alg == MBEDTLS_PK_ECKEY_DH )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200239 {
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200240 const mbedtls_ecp_group_id gid = mbedtls_pk_ec( *pk )->grp.id;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200241
Philippe Antoineb5b25432018-05-11 11:06:29 +0200242 if( gid == MBEDTLS_ECP_DP_NONE )
243 return( -1 );
244
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200245 if( ( profile->allowed_curves & MBEDTLS_X509_ID_FLAG( gid ) ) != 0 )
246 return( 0 );
247
248 return( -1 );
249 }
250#endif
251
252 return( -1 );
253}
254
255/*
Hanno Becker1f8527f2018-11-02 09:19:16 +0000256 * Like memcmp, but case-insensitive and always returns -1 if different
257 */
258static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
259{
260 size_t i;
261 unsigned char diff;
262 const unsigned char *n1 = s1, *n2 = s2;
263
264 for( i = 0; i < len; i++ )
265 {
266 diff = n1[i] ^ n2[i];
267
268 if( diff == 0 )
269 continue;
270
271 if( diff == 32 &&
272 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
273 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
274 {
275 continue;
276 }
277
278 return( -1 );
279 }
280
281 return( 0 );
282}
283
284/*
285 * Return 0 if name matches wildcard, -1 otherwise
286 */
287static int x509_check_wildcard( const char *cn, const mbedtls_x509_buf *name )
288{
289 size_t i;
290 size_t cn_idx = 0, cn_len = strlen( cn );
291
292 /* We can't have a match if there is no wildcard to match */
293 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
294 return( -1 );
295
296 for( i = 0; i < cn_len; ++i )
297 {
298 if( cn[i] == '.' )
299 {
300 cn_idx = i;
301 break;
302 }
303 }
304
305 if( cn_idx == 0 )
306 return( -1 );
307
308 if( cn_len - cn_idx == name->len - 1 &&
309 x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
310 {
311 return( 0 );
312 }
313
314 return( -1 );
315}
316
317/*
318 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
319 * variations (but not all).
320 *
321 * Return 0 if equal, -1 otherwise.
322 */
323static int x509_string_cmp( const mbedtls_x509_buf *a, const mbedtls_x509_buf *b )
324{
325 if( a->tag == b->tag &&
326 a->len == b->len &&
327 memcmp( a->p, b->p, b->len ) == 0 )
328 {
329 return( 0 );
330 }
331
332 if( ( a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
333 ( b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
334 a->len == b->len &&
335 x509_memcasecmp( a->p, b->p, b->len ) == 0 )
336 {
337 return( 0 );
338 }
339
340 return( -1 );
341}
342
343/*
344 * Compare two X.509 Names (aka rdnSequence).
345 *
346 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
347 * we sometimes return unequal when the full algorithm would return equal,
348 * but never the other way. (In particular, we don't do Unicode normalisation
349 * or space folding.)
350 *
351 * Return 0 if equal, -1 otherwise.
352 */
353static int x509_name_cmp( const mbedtls_x509_name *a, const mbedtls_x509_name *b )
354{
355 /* Avoid recursion, it might not be optimised by the compiler */
356 while( a != NULL || b != NULL )
357 {
358 if( a == NULL || b == NULL )
359 return( -1 );
360
361 /* type */
362 if( a->oid.tag != b->oid.tag ||
363 a->oid.len != b->oid.len ||
364 memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 )
365 {
366 return( -1 );
367 }
368
369 /* value */
370 if( x509_string_cmp( &a->val, &b->val ) != 0 )
371 return( -1 );
372
373 /* structure of the list of sets */
374 if( a->next_merged != b->next_merged )
375 return( -1 );
376
377 a = a->next;
378 b = b->next;
379 }
380
381 /* a == NULL == b */
382 return( 0 );
383}
384
385/*
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200386 * Reset (init or clear) a verify_chain
387 */
388static void x509_crt_verify_chain_reset(
389 mbedtls_x509_crt_verify_chain *ver_chain )
390{
391 size_t i;
392
393 for( i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++ )
394 {
395 ver_chain->items[i].crt = NULL;
Hanno Beckera9375b32019-01-10 09:19:26 +0000396 ver_chain->items[i].flags = (uint32_t) -1;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200397 }
398
399 ver_chain->len = 0;
Hanno Beckerf53893b2019-03-28 13:45:55 +0000400
401#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
402 ver_chain->trust_ca_cb_result = NULL;
403#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200404}
405
406/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200407 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
408 */
409static int x509_get_version( unsigned char **p,
410 const unsigned char *end,
411 int *ver )
412{
Janos Follath865b3eb2019-12-16 11:46:15 +0000413 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200414 size_t len;
415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
417 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200418 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200420 {
421 *ver = 0;
422 return( 0 );
423 }
424
Chris Jones9f7a6932021-04-14 12:12:09 +0100425 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200426 }
427
428 end = *p + len;
429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100431 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_VERSION, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200432
433 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100434 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_VERSION,
435 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200436
437 return( 0 );
438}
439
440/*
441 * Validity ::= SEQUENCE {
442 * notBefore Time,
443 * notAfter Time }
444 */
445static int x509_get_dates( unsigned char **p,
446 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447 mbedtls_x509_time *from,
448 mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200449{
Janos Follath865b3eb2019-12-16 11:46:15 +0000450 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200451 size_t len;
452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
454 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100455 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200456
457 end = *p + len;
458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200460 return( ret );
461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200463 return( ret );
464
465 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100466 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE,
467 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200468
469 return( 0 );
470}
471
472/*
473 * X.509 v2/v3 unique identifier (not parsed)
474 */
475static int x509_get_uid( unsigned char **p,
476 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 mbedtls_x509_buf *uid, int n )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200478{
Janos Follath865b3eb2019-12-16 11:46:15 +0000479 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200480
481 if( *p == end )
482 return( 0 );
483
484 uid->tag = **p;
485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486 if( ( ret = mbedtls_asn1_get_tag( p, end, &uid->len,
487 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200488 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200490 return( 0 );
491
Chris Jones9f7a6932021-04-14 12:12:09 +0100492 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200493 }
494
495 uid->p = *p;
496 *p += uid->len;
497
498 return( 0 );
499}
500
501static int x509_get_basic_constraints( unsigned char **p,
502 const unsigned char *end,
503 int *ca_istrue,
504 int *max_pathlen )
505{
Janos Follath865b3eb2019-12-16 11:46:15 +0000506 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200507 size_t len;
508
509 /*
510 * BasicConstraints ::= SEQUENCE {
511 * cA BOOLEAN DEFAULT FALSE,
512 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
513 */
514 *ca_istrue = 0; /* DEFAULT FALSE */
515 *max_pathlen = 0; /* endless */
516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
518 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100519 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200520
521 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200522 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 if( ( ret = mbedtls_asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
527 ret = mbedtls_asn1_get_int( p, end, ca_istrue );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200528
529 if( ret != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100530 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200531
532 if( *ca_istrue != 0 )
533 *ca_istrue = 1;
534 }
535
536 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200537 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100540 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200541
542 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100543 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
544 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200545
Andrzej Kurek16050742020-04-14 09:49:52 -0400546 /* Do not accept max_pathlen equal to INT_MAX to avoid a signed integer
547 * overflow, which is an undefined behavior. */
548 if( *max_pathlen == INT_MAX )
Chris Jones9f7a6932021-04-14 12:12:09 +0100549 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
550 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Andrzej Kurek16050742020-04-14 09:49:52 -0400551
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200552 (*max_pathlen)++;
553
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200554 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200555}
556
557static int x509_get_ns_cert_type( unsigned char **p,
558 const unsigned char *end,
559 unsigned char *ns_cert_type)
560{
Janos Follath865b3eb2019-12-16 11:46:15 +0000561 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100565 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200566
567 if( bs.len != 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100568 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
569 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200570
571 /* Get actual bitstring */
572 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200573 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200574}
575
576static int x509_get_key_usage( unsigned char **p,
577 const unsigned char *end,
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +0100578 unsigned int *key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200579{
Janos Follath865b3eb2019-12-16 11:46:15 +0000580 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200581 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100585 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200586
587 if( bs.len < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100588 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
589 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200590
591 /* Get actual bitstring */
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200592 *key_usage = 0;
593 for( i = 0; i < bs.len && i < sizeof( unsigned int ); i++ )
594 {
595 *key_usage |= (unsigned int) bs.p[i] << (8*i);
596 }
597
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200598 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200599}
600
601/*
602 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
603 *
604 * KeyPurposeId ::= OBJECT IDENTIFIER
605 */
606static int x509_get_ext_key_usage( unsigned char **p,
607 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200609{
Janos Follath865b3eb2019-12-16 11:46:15 +0000610 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 if( ( ret = mbedtls_asn1_get_sequence_of( p, end, ext_key_usage, MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100613 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200614
615 /* Sequence length must be >= 1 */
616 if( ext_key_usage->buf.p == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100617 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
618 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200619
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200620 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200621}
622
623/*
624 * SubjectAltName ::= GeneralNames
625 *
626 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
627 *
628 * GeneralName ::= CHOICE {
629 * otherName [0] OtherName,
630 * rfc822Name [1] IA5String,
631 * dNSName [2] IA5String,
632 * x400Address [3] ORAddress,
633 * directoryName [4] Name,
634 * ediPartyName [5] EDIPartyName,
635 * uniformResourceIdentifier [6] IA5String,
636 * iPAddress [7] OCTET STRING,
637 * registeredID [8] OBJECT IDENTIFIER }
638 *
639 * OtherName ::= SEQUENCE {
640 * type-id OBJECT IDENTIFIER,
641 * value [0] EXPLICIT ANY DEFINED BY type-id }
642 *
643 * EDIPartyName ::= SEQUENCE {
644 * nameAssigner [0] DirectoryString OPTIONAL,
645 * partyName [1] DirectoryString }
646 *
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300647 * NOTE: we list all types, but only use dNSName and otherName
648 * of type HwModuleName, as defined in RFC 4108, at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200649 */
650static int x509_get_subject_alt_name( unsigned char **p,
651 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652 mbedtls_x509_sequence *subject_alt_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200653{
Janos Follath865b3eb2019-12-16 11:46:15 +0000654 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200655 size_t len, tag_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 mbedtls_asn1_buf *buf;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200657 unsigned char tag;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658 mbedtls_asn1_sequence *cur = subject_alt_name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200659
660 /* Get main sequence tag */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
662 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100663 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200664
665 if( *p + len != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100666 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
667 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200668
669 while( *p < end )
670 {
Ron Eldordbbd9662019-05-15 15:46:03 +0300671 mbedtls_x509_subject_alternative_name dummy_san_buf;
672 memset( &dummy_san_buf, 0, sizeof( dummy_san_buf ) );
673
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200674 tag = **p;
675 (*p)++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676 if( ( ret = mbedtls_asn1_get_len( p, end, &tag_len ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100677 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200678
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100679 if( ( tag & MBEDTLS_ASN1_TAG_CLASS_MASK ) !=
680 MBEDTLS_ASN1_CONTEXT_SPECIFIC )
681 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100682 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
683 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100684 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200685
Ron Eldordbbd9662019-05-15 15:46:03 +0300686 /*
irwir74aee1c2019-09-21 18:21:48 +0300687 * Check that the SAN is structured correctly.
Ron Eldordbbd9662019-05-15 15:46:03 +0300688 */
689 ret = mbedtls_x509_parse_subject_alt_name( &(cur->buf), &dummy_san_buf );
690 /*
691 * In case the extension is malformed, return an error,
692 * and clear the allocated sequences.
693 */
694 if( ret != 0 && ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
695 {
696 mbedtls_x509_sequence *seq_cur = subject_alt_name->next;
697 mbedtls_x509_sequence *seq_prv;
698 while( seq_cur != NULL )
699 {
700 seq_prv = seq_cur;
701 seq_cur = seq_cur->next;
702 mbedtls_platform_zeroize( seq_prv,
703 sizeof( mbedtls_x509_sequence ) );
704 mbedtls_free( seq_prv );
705 }
Ron Eldor5aebeeb2019-05-22 16:41:21 +0300706 subject_alt_name->next = NULL;
Ron Eldordbbd9662019-05-15 15:46:03 +0300707 return( ret );
708 }
709
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200710 /* Allocate and assign next pointer */
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200711 if( cur->buf.p != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200712 {
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100713 if( cur->next != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100715
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200716 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200717
718 if( cur->next == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100719 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
720 MBEDTLS_ERR_ASN1_ALLOC_FAILED ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200721
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200722 cur = cur->next;
723 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200724
725 buf = &(cur->buf);
726 buf->tag = tag;
727 buf->p = *p;
728 buf->len = tag_len;
729 *p += buf->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200730 }
731
732 /* Set final sequence entry's next pointer to NULL */
733 cur->next = NULL;
734
735 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100736 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
737 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200738
739 return( 0 );
740}
741
742/*
Ron Eldor74d9acc2019-03-21 14:00:03 +0200743 * id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 }
744 *
745 * anyPolicy OBJECT IDENTIFIER ::= { id-ce-certificatePolicies 0 }
746 *
747 * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
748 *
749 * PolicyInformation ::= SEQUENCE {
750 * policyIdentifier CertPolicyId,
751 * policyQualifiers SEQUENCE SIZE (1..MAX) OF
752 * PolicyQualifierInfo OPTIONAL }
753 *
754 * CertPolicyId ::= OBJECT IDENTIFIER
755 *
756 * PolicyQualifierInfo ::= SEQUENCE {
757 * policyQualifierId PolicyQualifierId,
758 * qualifier ANY DEFINED BY policyQualifierId }
759 *
760 * -- policyQualifierIds for Internet policy qualifiers
761 *
762 * id-qt OBJECT IDENTIFIER ::= { id-pkix 2 }
763 * id-qt-cps OBJECT IDENTIFIER ::= { id-qt 1 }
764 * id-qt-unotice OBJECT IDENTIFIER ::= { id-qt 2 }
765 *
766 * PolicyQualifierId ::= OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice )
767 *
768 * Qualifier ::= CHOICE {
769 * cPSuri CPSuri,
770 * userNotice UserNotice }
771 *
772 * CPSuri ::= IA5String
773 *
774 * UserNotice ::= SEQUENCE {
775 * noticeRef NoticeReference OPTIONAL,
776 * explicitText DisplayText OPTIONAL }
777 *
778 * NoticeReference ::= SEQUENCE {
779 * organization DisplayText,
780 * noticeNumbers SEQUENCE OF INTEGER }
781 *
782 * DisplayText ::= CHOICE {
783 * ia5String IA5String (SIZE (1..200)),
784 * visibleString VisibleString (SIZE (1..200)),
785 * bmpString BMPString (SIZE (1..200)),
786 * utf8String UTF8String (SIZE (1..200)) }
787 *
788 * NOTE: we only parse and use anyPolicy without qualifiers at this point
789 * as defined in RFC 5280.
790 */
791static int x509_get_certificate_policies( unsigned char **p,
792 const unsigned char *end,
793 mbedtls_x509_sequence *certificate_policies )
794{
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300795 int ret, parse_ret = 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200796 size_t len;
797 mbedtls_asn1_buf *buf;
798 mbedtls_asn1_sequence *cur = certificate_policies;
799
800 /* Get main sequence tag */
801 ret = mbedtls_asn1_get_tag( p, end, &len,
802 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
803 if( ret != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100804 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200805
806 if( *p + len != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100807 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
808 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200809
810 /*
811 * Cannot be an empty sequence.
812 */
813 if( len == 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100814 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
815 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200816
817 while( *p < end )
818 {
819 mbedtls_x509_buf policy_oid;
820 const unsigned char *policy_end;
821
822 /*
823 * Get the policy sequence
824 */
825 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
826 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100827 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200828
829 policy_end = *p + len;
830
Ron Eldor08063792019-05-13 16:38:39 +0300831 if( ( ret = mbedtls_asn1_get_tag( p, policy_end, &len,
Ron Eldor74d9acc2019-03-21 14:00:03 +0200832 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100833 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200834
835 policy_oid.tag = MBEDTLS_ASN1_OID;
836 policy_oid.len = len;
837 policy_oid.p = *p;
838
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300839 /*
840 * Only AnyPolicy is currently supported when enforcing policy.
841 */
842 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_POLICY, &policy_oid ) != 0 )
843 {
844 /*
845 * Set the parsing return code but continue parsing, in case this
TRodziewicz3ecb92e2021-05-11 18:22:05 +0200846 * extension is critical.
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300847 */
848 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
849 }
850
Ron Eldor74d9acc2019-03-21 14:00:03 +0200851 /* Allocate and assign next pointer */
852 if( cur->buf.p != NULL )
853 {
854 if( cur->next != NULL )
855 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
856
857 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
858
859 if( cur->next == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100860 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
861 MBEDTLS_ERR_ASN1_ALLOC_FAILED ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200862
863 cur = cur->next;
864 }
865
866 buf = &( cur->buf );
867 buf->tag = policy_oid.tag;
868 buf->p = policy_oid.p;
869 buf->len = policy_oid.len;
Ron Eldor08063792019-05-13 16:38:39 +0300870
871 *p += len;
872
873 /*
874 * If there is an optional qualifier, then *p < policy_end
875 * Check the Qualifier len to verify it doesn't exceed policy_end.
876 */
877 if( *p < policy_end )
878 {
879 if( ( ret = mbedtls_asn1_get_tag( p, policy_end, &len,
880 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100881 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor08063792019-05-13 16:38:39 +0300882 /*
883 * Skip the optional policy qualifiers.
884 */
885 *p += len;
886 }
887
888 if( *p != policy_end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100889 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
890 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200891 }
892
893 /* Set final sequence entry's next pointer to NULL */
894 cur->next = NULL;
895
896 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100897 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
898 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200899
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300900 return( parse_ret );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200901}
902
903/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200904 * X.509 v3 extensions
905 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200906 */
907static int x509_get_crt_ext( unsigned char **p,
908 const unsigned char *end,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200909 mbedtls_x509_crt *crt,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200910 mbedtls_x509_crt_ext_cb_t cb,
911 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200912{
Janos Follath865b3eb2019-12-16 11:46:15 +0000913 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200914 size_t len;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200915 unsigned char *end_ext_data, *start_ext_octet, *end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200916
Hanno Becker12f62fb2019-02-12 17:22:36 +0000917 if( *p == end )
918 return( 0 );
919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200920 if( ( ret = mbedtls_x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200921 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200922
Hanno Becker12f62fb2019-02-12 17:22:36 +0000923 end = crt->v3_ext.p + crt->v3_ext.len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200924 while( *p < end )
925 {
926 /*
927 * Extension ::= SEQUENCE {
928 * extnID OBJECT IDENTIFIER,
929 * critical BOOLEAN DEFAULT FALSE,
930 * extnValue OCTET STRING }
931 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932 mbedtls_x509_buf extn_oid = {0, 0, NULL};
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200933 int is_critical = 0; /* DEFAULT FALSE */
934 int ext_type = 0;
935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
937 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100938 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200939
940 end_ext_data = *p + len;
941
942 /* Get extension ID */
k-stachowiakdcae78a2018-06-28 16:32:54 +0200943 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &extn_oid.len,
k-stachowiak463928a2018-07-24 12:50:59 +0200944 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100945 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200946
k-stachowiak463928a2018-07-24 12:50:59 +0200947 extn_oid.tag = MBEDTLS_ASN1_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200948 extn_oid.p = *p;
949 *p += extn_oid.len;
950
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200951 /* Get optional critical */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200952 if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
953 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
Chris Jones9f7a6932021-04-14 12:12:09 +0100954 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200955
956 /* Data should be octet string type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
958 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100959 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200960
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200961 start_ext_octet = *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200962 end_ext_octet = *p + len;
963
964 if( end_ext_octet != end_ext_data )
Chris Jones9f7a6932021-04-14 12:12:09 +0100965 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
966 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200967
968 /*
969 * Detect supported extensions
970 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200972
973 if( ret != 0 )
974 {
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200975 /* Give the callback (if any) a chance to handle the extension */
Nicola Di Lieto2c3a9172020-05-28 17:20:42 +0200976 if( cb != NULL )
977 {
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200978 ret = cb( p_ctx, crt, &extn_oid, is_critical, *p, end_ext_octet );
Nicola Di Lieto565b52b2020-05-29 22:46:56 +0200979 if( ret != 0 && is_critical )
980 return( ret );
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200981 *p = end_ext_octet;
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200982 continue;
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200983 }
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200984
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200985 /* No parser found, skip extension */
986 *p = end_ext_octet;
987
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200988 if( is_critical )
989 {
990 /* Data is marked as critical: fail */
Chris Jones9f7a6932021-04-14 12:12:09 +0100991 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
992 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200993 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200994 continue;
995 }
996
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100997 /* Forbid repeated extensions */
998 if( ( crt->ext_types & ext_type ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200999 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +01001000
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001001 crt->ext_types |= ext_type;
1002
1003 switch( ext_type )
1004 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001005 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001006 /* Parse basic constraints */
1007 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
1008 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001009 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001010 break;
1011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001013 /* Parse key usage */
1014 if( ( ret = x509_get_key_usage( p, end_ext_octet,
1015 &crt->key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001016 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001017 break;
1018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001020 /* Parse extended key usage */
1021 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
1022 &crt->ext_key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001023 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001024 break;
1025
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001026 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001027 /* Parse subject alt name */
1028 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
1029 &crt->subject_alt_names ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001030 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001031 break;
1032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001034 /* Parse netscape certificate type */
1035 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
1036 &crt->ns_cert_type ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001037 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001038 break;
1039
Ron Eldor74d9acc2019-03-21 14:00:03 +02001040 case MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES:
1041 /* Parse certificate policies type */
1042 if( ( ret = x509_get_certificate_policies( p, end_ext_octet,
1043 &crt->certificate_policies ) ) != 0 )
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001044 {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +02001045 /* Give the callback (if any) a chance to handle the extension
1046 * if it contains unsupported policies */
1047 if( ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE && cb != NULL &&
1048 cb( p_ctx, crt, &extn_oid, is_critical,
1049 start_ext_octet, end_ext_octet ) == 0 )
1050 break;
1051
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001052 if( is_critical )
1053 return( ret );
1054 else
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001055 /*
Ron Eldora2913912019-05-16 16:17:38 +03001056 * If MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE is returned, then we
1057 * cannot interpret or enforce the policy. However, it is up to
1058 * the user to choose how to enforce the policies,
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001059 * unless the extension is critical.
1060 */
1061 if( ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1062 return( ret );
1063 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001064 break;
1065
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001066 default:
Ron Eldordf48efa2019-04-08 13:28:24 +03001067 /*
1068 * If this is a non-critical extension, which the oid layer
1069 * supports, but there isn't an x509 parser for it,
1070 * skip the extension.
1071 */
Ron Eldordf48efa2019-04-08 13:28:24 +03001072 if( is_critical )
1073 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1074 else
Ron Eldordf48efa2019-04-08 13:28:24 +03001075 *p = end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001076 }
1077 }
1078
1079 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +01001080 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1081 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001082
1083 return( 0 );
1084}
1085
1086/*
1087 * Parse and fill a single X.509 certificate in DER format
1088 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001089static int x509_crt_parse_der_core( mbedtls_x509_crt *crt,
1090 const unsigned char *buf,
1091 size_t buflen,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001092 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001093 mbedtls_x509_crt_ext_cb_t cb,
1094 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001095{
Janos Follath865b3eb2019-12-16 11:46:15 +00001096 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001097 size_t len;
1098 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001099 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101 memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) );
1102 memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) );
1103 memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001104
1105 /*
1106 * Check for valid input
1107 */
1108 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001110
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001111 /* Use the original buffer until we figure out actual length. */
Janos Follathcc0e49d2016-02-17 14:34:12 +00001112 p = (unsigned char*) buf;
1113 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001114 end = p + len;
1115
1116 /*
1117 * Certificate ::= SEQUENCE {
1118 * tbsCertificate TBSCertificate,
1119 * signatureAlgorithm AlgorithmIdentifier,
1120 * signatureValue BIT STRING }
1121 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1123 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001124 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001125 mbedtls_x509_crt_free( crt );
1126 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001127 }
1128
Janos Follathcc0e49d2016-02-17 14:34:12 +00001129 end = crt_end = p + len;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001130 crt->raw.len = crt_end - buf;
1131 if( make_copy != 0 )
1132 {
1133 /* Create and populate a new buffer for the raw field. */
1134 crt->raw.p = p = mbedtls_calloc( 1, crt->raw.len );
1135 if( crt->raw.p == NULL )
1136 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
1137
1138 memcpy( crt->raw.p, buf, crt->raw.len );
1139 crt->own_buffer = 1;
1140
1141 p += crt->raw.len - len;
1142 end = crt_end = p + len;
1143 }
1144 else
1145 {
1146 crt->raw.p = (unsigned char*) buf;
1147 crt->own_buffer = 0;
1148 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001149
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001150 /*
1151 * TBSCertificate ::= SEQUENCE {
1152 */
1153 crt->tbs.p = p;
1154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1156 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001157 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001159 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001160 }
1161
1162 end = p + len;
1163 crt->tbs.len = end - crt->tbs.p;
1164
1165 /*
1166 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1167 *
1168 * CertificateSerialNumber ::= INTEGER
1169 *
1170 * signature AlgorithmIdentifier
1171 */
1172 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173 ( ret = mbedtls_x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1174 ( ret = mbedtls_x509_get_alg( &p, end, &crt->sig_oid,
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001175 &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001176 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001178 return( ret );
1179 }
1180
Andres AG7ca4a032017-03-09 16:16:11 +00001181 if( crt->version < 0 || crt->version > 2 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001182 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183 mbedtls_x509_crt_free( crt );
1184 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001185 }
1186
Andres AG7ca4a032017-03-09 16:16:11 +00001187 crt->version++;
1188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189 if( ( ret = mbedtls_x509_get_sig_alg( &crt->sig_oid, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02001190 &crt->sig_md, &crt->sig_pk,
1191 &crt->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001194 return( ret );
1195 }
1196
1197 /*
1198 * issuer Name
1199 */
1200 crt->issuer_raw.p = p;
1201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001202 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1203 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001204 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001205 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001206 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001207 }
1208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209 if( ( ret = mbedtls_x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001210 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001211 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001212 return( ret );
1213 }
1214
1215 crt->issuer_raw.len = p - crt->issuer_raw.p;
1216
1217 /*
1218 * Validity ::= SEQUENCE {
1219 * notBefore Time,
1220 * notAfter Time }
1221 *
1222 */
1223 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1224 &crt->valid_to ) ) != 0 )
1225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001226 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001227 return( ret );
1228 }
1229
1230 /*
1231 * subject Name
1232 */
1233 crt->subject_raw.p = p;
1234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001235 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1236 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001238 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001239 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001240 }
1241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001242 if( len && ( ret = mbedtls_x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001243 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001245 return( ret );
1246 }
1247
1248 crt->subject_raw.len = p - crt->subject_raw.p;
1249
1250 /*
1251 * SubjectPublicKeyInfo
1252 */
Hanno Becker494dd7a2019-02-06 16:13:41 +00001253 crt->pk_raw.p = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254 if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001255 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001256 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001257 return( ret );
1258 }
Hanno Becker494dd7a2019-02-06 16:13:41 +00001259 crt->pk_raw.len = p - crt->pk_raw.p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001260
1261 /*
1262 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1263 * -- If present, version shall be v2 or v3
1264 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1265 * -- If present, version shall be v2 or v3
1266 * extensions [3] EXPLICIT Extensions OPTIONAL
1267 * -- If present, version shall be v3
1268 */
1269 if( crt->version == 2 || crt->version == 3 )
1270 {
1271 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1272 if( ret != 0 )
1273 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001275 return( ret );
1276 }
1277 }
1278
1279 if( crt->version == 2 || crt->version == 3 )
1280 {
1281 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1282 if( ret != 0 )
1283 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001284 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001285 return( ret );
1286 }
1287 }
1288
1289 if( crt->version == 3 )
Manuel Pégourié-Gonnarda252af72015-03-27 16:15:55 +01001290 {
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001291 ret = x509_get_crt_ext( &p, end, crt, cb, p_ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001292 if( ret != 0 )
1293 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001294 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001295 return( ret );
1296 }
1297 }
1298
1299 if( p != end )
1300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001302 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT,
1303 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001304 }
1305
1306 end = crt_end;
1307
1308 /*
1309 * }
1310 * -- end of TBSCertificate
1311 *
1312 * signatureAlgorithm AlgorithmIdentifier,
1313 * signatureValue BIT STRING
1314 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315 if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001318 return( ret );
1319 }
1320
Manuel Pégourié-Gonnard1022fed2015-03-27 16:30:47 +01001321 if( crt->sig_oid.len != sig_oid2.len ||
1322 memcmp( crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len ) != 0 ||
Paul Elliottca17ebf2020-11-24 17:30:18 +00001323 sig_params1.tag != sig_params2.tag ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001324 sig_params1.len != sig_params2.len ||
Manuel Pégourié-Gonnard159c5242015-04-30 11:15:22 +02001325 ( sig_params1.len != 0 &&
1326 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328 mbedtls_x509_crt_free( crt );
1329 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001330 }
1331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332 if( ( ret = mbedtls_x509_get_sig( &p, end, &crt->sig ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001333 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001335 return( ret );
1336 }
1337
1338 if( p != end )
1339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001341 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT,
1342 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001343 }
1344
1345 return( 0 );
1346}
1347
1348/*
1349 * Parse one X.509 certificate in DER format from a buffer and add them to a
1350 * chained list
1351 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001352static int mbedtls_x509_crt_parse_der_internal( mbedtls_x509_crt *chain,
1353 const unsigned char *buf,
1354 size_t buflen,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001355 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001356 mbedtls_x509_crt_ext_cb_t cb,
1357 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001358{
Janos Follath865b3eb2019-12-16 11:46:15 +00001359 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001361
1362 /*
1363 * Check for valid input
1364 */
1365 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001367
1368 while( crt->version != 0 && crt->next != NULL )
1369 {
1370 prev = crt;
1371 crt = crt->next;
1372 }
1373
1374 /*
1375 * Add new certificate on the end of the chain if needed.
1376 */
Paul Bakker66d5d072014-06-17 16:39:18 +02001377 if( crt->version != 0 && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001378 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001379 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001380
1381 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001382 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001383
1384 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001386 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001387 }
1388
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001389 ret = x509_crt_parse_der_core( crt, buf, buflen, make_copy, cb, p_ctx );
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001390 if( ret != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001391 {
1392 if( prev )
1393 prev->next = NULL;
1394
1395 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001397
1398 return( ret );
1399 }
1400
1401 return( 0 );
1402}
1403
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001404int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
1405 const unsigned char *buf,
1406 size_t buflen )
1407{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001408 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 0, NULL, NULL ) );
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001409}
1410
Nicola Di Lietofde98f72020-05-28 08:34:33 +02001411int mbedtls_x509_crt_parse_der_with_ext_cb( mbedtls_x509_crt *chain,
1412 const unsigned char *buf,
1413 size_t buflen,
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +02001414 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001415 mbedtls_x509_crt_ext_cb_t cb,
1416 void *p_ctx )
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001417{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001418 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, make_copy, cb, p_ctx ) );
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001419}
1420
1421int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
1422 const unsigned char *buf,
1423 size_t buflen )
1424{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001425 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 1, NULL, NULL ) );
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001426}
1427
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001428/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001429 * Parse one or more PEM certificates from a buffer and add them to the chained
1430 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001431 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001432int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain,
1433 const unsigned char *buf,
1434 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001435{
Janos Follath98e28a72016-05-31 14:03:54 +01001436#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001437 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001439#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001440
1441 /*
1442 * Check for valid input
1443 */
1444 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001445 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001446
1447 /*
1448 * Determine buffer content. Buffer contains either one DER certificate or
1449 * one or more PEM certificates.
1450 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001451#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001452 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001453 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1454 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001455 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001456 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458 if( buf_format == MBEDTLS_X509_FORMAT_DER )
1459 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Janos Follath98e28a72016-05-31 14:03:54 +01001460#else
1461 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
1462#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464#if defined(MBEDTLS_PEM_PARSE_C)
1465 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001466 {
Janos Follath865b3eb2019-12-16 11:46:15 +00001467 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001469
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001470 /* 1 rather than 0 since the terminating NULL byte is counted in */
1471 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001472 {
1473 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001475
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001476 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001478 "-----BEGIN CERTIFICATE-----",
1479 "-----END CERTIFICATE-----",
1480 buf, NULL, 0, &use_len );
1481
1482 if( ret == 0 )
1483 {
1484 /*
1485 * Was PEM encoded
1486 */
1487 buflen -= use_len;
1488 buf += use_len;
1489 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001490 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001491 {
1492 return( ret );
1493 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001494 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001495 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001496 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001497
1498 /*
1499 * PEM header and footer were found
1500 */
1501 buflen -= use_len;
1502 buf += use_len;
1503
1504 if( first_error == 0 )
1505 first_error = ret;
1506
Paul Bakker5a5fa922014-09-26 14:53:04 +02001507 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001508 continue;
1509 }
1510 else
1511 break;
1512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001513 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001515 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001516
1517 if( ret != 0 )
1518 {
1519 /*
1520 * Quit parsing on a memory error
1521 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001522 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001523 return( ret );
1524
1525 if( first_error == 0 )
1526 first_error = ret;
1527
1528 total_failed++;
1529 continue;
1530 }
1531
1532 success = 1;
1533 }
1534 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001535
1536 if( success )
1537 return( total_failed );
1538 else if( first_error )
1539 return( first_error );
1540 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001541 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Janos Follath98e28a72016-05-31 14:03:54 +01001542#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001543}
1544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001545#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001546/*
1547 * Load one or more certificates and add them to the chained list
1548 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001549int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001550{
Janos Follath865b3eb2019-12-16 11:46:15 +00001551 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001552 size_t n;
1553 unsigned char *buf;
1554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001555 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001556 return( ret );
1557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001558 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001559
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001560 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001561 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001562
1563 return( ret );
1564}
1565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001567{
1568 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001569#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001570 int w_ret;
1571 WCHAR szDir[MAX_PATH];
1572 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001573 char *p;
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001574 size_t len = strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001575
Paul Bakker9af723c2014-05-01 13:03:14 +02001576 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001577 HANDLE hFind;
1578
1579 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001580 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001581
Paul Bakker9af723c2014-05-01 13:03:14 +02001582 memset( szDir, 0, sizeof(szDir) );
1583 memset( filename, 0, MAX_PATH );
1584 memcpy( filename, path, len );
1585 filename[len++] = '\\';
1586 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001587 filename[len++] = '*';
1588
Simon B3c6b18d2016-11-03 01:11:37 +00001589 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001590 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001591 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001592 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001593
1594 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001595 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001597
1598 len = MAX_PATH - len;
1599 do
1600 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001601 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001602
1603 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1604 continue;
1605
Paul Bakker9af723c2014-05-01 13:03:14 +02001606 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001607 lstrlenW( file_data.cFileName ),
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001608 p, (int) len - 1,
Paul Bakker9af723c2014-05-01 13:03:14 +02001609 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001610 if( w_ret == 0 )
Ron Eldor36d90422017-01-09 15:09:16 +02001611 {
1612 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1613 goto cleanup;
1614 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001616 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001617 if( w_ret < 0 )
1618 ret++;
1619 else
1620 ret += w_ret;
1621 }
1622 while( FindNextFileW( hFind, &file_data ) != 0 );
1623
Paul Bakker66d5d072014-06-17 16:39:18 +02001624 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001625 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001626
Ron Eldor36d90422017-01-09 15:09:16 +02001627cleanup:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001628 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001629#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001630 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001631 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001632 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001633 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001634 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001635 DIR *dir = opendir( path );
1636
Paul Bakker66d5d072014-06-17 16:39:18 +02001637 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001638 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001639
Ron Eldor63140682017-01-09 19:27:59 +02001640#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001641 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001642 {
1643 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001644 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001645 }
Ron Eldor63140682017-01-09 19:27:59 +02001646#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001647
Paul Elliottfb91a482021-03-05 14:17:51 +00001648 memset( &sb, 0, sizeof( sb ) );
1649
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001650 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001651 {
Andres AGf9113192016-09-02 14:06:04 +01001652 snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name,
1653 "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001654
Andres AGf9113192016-09-02 14:06:04 +01001655 if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001656 {
Andres AGf9113192016-09-02 14:06:04 +01001657 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1658 goto cleanup;
1659 }
1660 else if( stat( entry_name, &sb ) == -1 )
1661 {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001662 if( errno == ENOENT )
Eduardo Silvae1bfffc2019-04-25 10:43:26 -06001663 {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001664 /* Broken symbolic link - ignore this entry.
1665 stat(2) will return this error for either (a) a dangling
1666 symlink or (b) a missing file.
1667 Given that we have just obtained the filename from readdir,
1668 assume that it does exist and therefore treat this as a
1669 dangling symlink. */
1670 continue;
1671 }
1672 else
1673 {
1674 /* Some other file error; report the error. */
Eduardo Silvae1bfffc2019-04-25 10:43:26 -06001675 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1676 goto cleanup;
1677 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001678 }
1679
1680 if( !S_ISREG( sb.st_mode ) )
1681 continue;
1682
1683 // Ignore parse errors
1684 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001685 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001686 if( t_ret < 0 )
1687 ret++;
1688 else
1689 ret += t_ret;
1690 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001691
1692cleanup:
Andres AGf9113192016-09-02 14:06:04 +01001693 closedir( dir );
1694
Ron Eldor63140682017-01-09 19:27:59 +02001695#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001696 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001697 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Ron Eldor63140682017-01-09 19:27:59 +02001698#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001699
Paul Bakkerbe089b02013-10-14 15:51:50 +02001700#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001701
1702 return( ret );
1703}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001704#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001705
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001706/*
1707 * OtherName ::= SEQUENCE {
1708 * type-id OBJECT IDENTIFIER,
1709 * value [0] EXPLICIT ANY DEFINED BY type-id }
1710 *
1711 * HardwareModuleName ::= SEQUENCE {
1712 * hwType OBJECT IDENTIFIER,
1713 * hwSerialNum OCTET STRING }
1714 *
1715 * NOTE: we currently only parse and use otherName of type HwModuleName,
1716 * as defined in RFC 4108.
1717 */
1718static int x509_get_other_name( const mbedtls_x509_buf *subject_alt_name,
1719 mbedtls_x509_san_other_name *other_name )
1720{
Janos Follathab23cd12019-05-09 13:53:57 +01001721 int ret = 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001722 size_t len;
1723 unsigned char *p = subject_alt_name->p;
1724 const unsigned char *end = p + subject_alt_name->len;
1725 mbedtls_x509_buf cur_oid;
1726
1727 if( ( subject_alt_name->tag &
1728 ( MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK ) ) !=
1729 ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ) )
1730 {
1731 /*
1732 * The given subject alternative name is not of type "othername".
1733 */
1734 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
1735 }
1736
1737 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1738 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001739 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001740
1741 cur_oid.tag = MBEDTLS_ASN1_OID;
1742 cur_oid.p = p;
1743 cur_oid.len = len;
1744
1745 /*
1746 * Only HwModuleName is currently supported.
1747 */
1748 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid ) != 0 )
1749 {
1750 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1751 }
1752
Ron Eldor890819a2019-05-13 19:03:04 +03001753 if( p + len >= end )
1754 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001755 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001756 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1757 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001758 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001759 p += len;
1760 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1761 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001762 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001763
1764 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1765 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 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 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001769 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001770
1771 other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID;
1772 other_name->value.hardware_module_name.oid.p = p;
1773 other_name->value.hardware_module_name.oid.len = len;
1774
Ron Eldor890819a2019-05-13 19:03:04 +03001775 if( p + len >= end )
1776 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001777 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001778 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1779 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001780 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001781 p += len;
1782 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1783 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001784 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001785
1786 other_name->value.hardware_module_name.val.tag = MBEDTLS_ASN1_OCTET_STRING;
1787 other_name->value.hardware_module_name.val.p = p;
1788 other_name->value.hardware_module_name.val.len = len;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001789 p += len;
1790 if( p != end )
1791 {
Ron Eldor890819a2019-05-13 19:03:04 +03001792 mbedtls_platform_zeroize( other_name,
Sébastien Duquette661d7252019-06-23 17:45:26 -04001793 sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001794 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1795 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001796 }
1797 return( 0 );
1798}
1799
Peter Kolbus9a969b62018-12-11 13:55:56 -06001800int mbedtls_x509_parse_subject_alt_name( const mbedtls_x509_buf *san_buf,
1801 mbedtls_x509_subject_alternative_name *san )
1802{
1803 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1804 switch( san_buf->tag &
1805 ( MBEDTLS_ASN1_TAG_CLASS_MASK |
1806 MBEDTLS_ASN1_TAG_VALUE_MASK ) )
1807 {
1808 /*
1809 * otherName
1810 */
1811 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ):
1812 {
1813 mbedtls_x509_san_other_name other_name;
1814
1815 ret = x509_get_other_name( san_buf, &other_name );
1816 if( ret != 0 )
1817 return( ret );
1818
1819 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1820 san->type = MBEDTLS_X509_SAN_OTHER_NAME;
1821 memcpy( &san->san.other_name,
1822 &other_name, sizeof( other_name ) );
1823
1824 }
1825 break;
1826
1827 /*
1828 * dNSName
1829 */
1830 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME ):
1831 {
1832 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1833 san->type = MBEDTLS_X509_SAN_DNS_NAME;
1834
1835 memcpy( &san->san.unstructured_name,
1836 san_buf, sizeof( *san_buf ) );
1837
1838 }
1839 break;
1840
1841 /*
1842 * Type not supported
1843 */
1844 default:
1845 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1846 }
1847 return( 0 );
1848}
1849
1850#if !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001851static int x509_info_subject_alt_name( char **buf, size_t *size,
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001852 const mbedtls_x509_sequence
1853 *subject_alt_name,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001854 const char *prefix )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001855{
Janos Follath865b3eb2019-12-16 11:46:15 +00001856 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001857 size_t n = *size;
1858 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001859 const mbedtls_x509_sequence *cur = subject_alt_name;
Ron Eldor890819a2019-05-13 19:03:04 +03001860 mbedtls_x509_subject_alternative_name san;
1861 int parse_ret;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001862
1863 while( cur != NULL )
1864 {
Ron Eldor890819a2019-05-13 19:03:04 +03001865 memset( &san, 0, sizeof( san ) );
1866 parse_ret = mbedtls_x509_parse_subject_alt_name( &cur->buf, &san );
1867 if( parse_ret != 0 )
1868 {
1869 if( parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1870 {
1871 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
1872 MBEDTLS_X509_SAFE_SNPRINTF;
1873 }
1874 else
1875 {
1876 ret = mbedtls_snprintf( p, n, "\n%s <malformed>", prefix );
1877 MBEDTLS_X509_SAFE_SNPRINTF;
1878 }
1879 cur = cur->next;
1880 continue;
1881 }
1882
1883 switch( san.type )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001884 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001885 /*
1886 * otherName
1887 */
Ron Eldora2913912019-05-16 16:17:38 +03001888 case MBEDTLS_X509_SAN_OTHER_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001889 {
1890 mbedtls_x509_san_other_name *other_name = &san.san.other_name;
1891
1892 ret = mbedtls_snprintf( p, n, "\n%s otherName :", prefix );
1893 MBEDTLS_X509_SAFE_SNPRINTF;
1894
1895 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME,
1896 &other_name->value.hardware_module_name.oid ) != 0 )
Janos Follath22f605f2019-05-10 10:37:17 +01001897 {
Ron Eldor890819a2019-05-13 19:03:04 +03001898 ret = mbedtls_snprintf( p, n, "\n%s hardware module name :", prefix );
1899 MBEDTLS_X509_SAFE_SNPRINTF;
1900 ret = mbedtls_snprintf( p, n, "\n%s hardware type : ", prefix );
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001901 MBEDTLS_X509_SAFE_SNPRINTF;
1902
Ron Eldor890819a2019-05-13 19:03:04 +03001903 ret = mbedtls_oid_get_numeric_string( p, n, &other_name->value.hardware_module_name.oid );
1904 MBEDTLS_X509_SAFE_SNPRINTF;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001905
Ron Eldor890819a2019-05-13 19:03:04 +03001906 ret = mbedtls_snprintf( p, n, "\n%s hardware serial number : ", prefix );
1907 MBEDTLS_X509_SAFE_SNPRINTF;
1908
1909 if( other_name->value.hardware_module_name.val.len >= n )
1910 {
1911 *p = '\0';
1912 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Janos Follath22f605f2019-05-10 10:37:17 +01001913 }
1914
Ron Eldora2913912019-05-16 16:17:38 +03001915 memcpy( p, other_name->value.hardware_module_name.val.p,
1916 other_name->value.hardware_module_name.val.len );
1917 p += other_name->value.hardware_module_name.val.len;
1918
Ron Eldor890819a2019-05-13 19:03:04 +03001919 n -= other_name->value.hardware_module_name.val.len;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001920
Ron Eldor890819a2019-05-13 19:03:04 +03001921 }/* MBEDTLS_OID_ON_HW_MODULE_NAME */
1922 }
1923 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001924
1925 /*
1926 * dNSName
1927 */
Ron Eldora2913912019-05-16 16:17:38 +03001928 case MBEDTLS_X509_SAN_DNS_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001929 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001930 ret = mbedtls_snprintf( p, n, "\n%s dNSName : ", prefix );
1931 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldor890819a2019-05-13 19:03:04 +03001932 if( san.san.unstructured_name.len >= n )
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001933 {
1934 *p = '\0';
1935 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
1936 }
Ron Eldora2913912019-05-16 16:17:38 +03001937
1938 memcpy( p, san.san.unstructured_name.p, san.san.unstructured_name.len );
1939 p += san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001940 n -= san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001941 }
1942 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001943
1944 /*
Ron Eldor890819a2019-05-13 19:03:04 +03001945 * Type not supported, skip item.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001946 */
1947 default:
Janos Follath22f605f2019-05-10 10:37:17 +01001948 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
1949 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001950 break;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001951 }
1952
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001953 cur = cur->next;
1954 }
1955
1956 *p = '\0';
1957
1958 *size = n;
1959 *buf = p;
1960
1961 return( 0 );
1962}
1963
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001964#define PRINT_ITEM(i) \
1965 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001966 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001967 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001968 sep = ", "; \
1969 }
1970
1971#define CERT_TYPE(type,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001972 if( ns_cert_type & (type) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001973 PRINT_ITEM( name );
1974
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001975static int x509_info_cert_type( char **buf, size_t *size,
1976 unsigned char ns_cert_type )
1977{
Janos Follath865b3eb2019-12-16 11:46:15 +00001978 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001979 size_t n = *size;
1980 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001981 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001983 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001984 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001985 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
1986 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1987 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001988 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
1989 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1990 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001991
1992 *size = n;
1993 *buf = p;
1994
1995 return( 0 );
1996}
1997
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001998#define KEY_USAGE(code,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001999 if( key_usage & (code) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002000 PRINT_ITEM( name );
2001
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002002static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02002003 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002004{
Janos Follath865b3eb2019-12-16 11:46:15 +00002005 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002006 size_t n = *size;
2007 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002008 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002010 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
2011 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002012 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
2013 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
2014 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002015 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
2016 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02002017 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
2018 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002019
2020 *size = n;
2021 *buf = p;
2022
2023 return( 0 );
2024}
2025
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002026static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002027 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002028{
Janos Follath865b3eb2019-12-16 11:46:15 +00002029 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002030 const char *desc;
2031 size_t n = *size;
2032 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002033 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002034 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002035
2036 while( cur != NULL )
2037 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002038 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002039 desc = "???";
2040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002041 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002042 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002043
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002044 sep = ", ";
2045
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002046 cur = cur->next;
2047 }
2048
2049 *size = n;
2050 *buf = p;
2051
2052 return( 0 );
2053}
2054
Ron Eldor74d9acc2019-03-21 14:00:03 +02002055static int x509_info_cert_policies( char **buf, size_t *size,
2056 const mbedtls_x509_sequence *certificate_policies )
2057{
Janos Follath865b3eb2019-12-16 11:46:15 +00002058 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor74d9acc2019-03-21 14:00:03 +02002059 const char *desc;
2060 size_t n = *size;
2061 char *p = *buf;
2062 const mbedtls_x509_sequence *cur = certificate_policies;
2063 const char *sep = "";
2064
2065 while( cur != NULL )
2066 {
2067 if( mbedtls_oid_get_certificate_policies( &cur->buf, &desc ) != 0 )
2068 desc = "???";
2069
2070 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
2071 MBEDTLS_X509_SAFE_SNPRINTF;
2072
2073 sep = ", ";
2074
2075 cur = cur->next;
2076 }
2077
2078 *size = n;
2079 *buf = p;
2080
2081 return( 0 );
2082}
2083
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002084/*
2085 * Return an informational string about the certificate.
2086 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002087#define BEFORE_COLON 18
2088#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002089int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
2090 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002091{
Janos Follath865b3eb2019-12-16 11:46:15 +00002092 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002093 size_t n;
2094 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002095 char key_size_str[BEFORE_COLON];
2096
2097 p = buf;
2098 n = size;
2099
Janos Follath98e28a72016-05-31 14:03:54 +01002100 if( NULL == crt )
2101 {
2102 ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" );
2103 MBEDTLS_X509_SAFE_SNPRINTF;
2104
2105 return( (int) ( size - n ) );
2106 }
2107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002108 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002109 prefix, crt->version );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002110 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002111 ret = mbedtls_snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002112 prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002113 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002115 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002116 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002118 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002119 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002120 ret = mbedtls_x509_dn_gets( p, n, &crt->issuer );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002121 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002123 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002124 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002125 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002126 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002128 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002129 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2130 crt->valid_from.year, crt->valid_from.mon,
2131 crt->valid_from.day, crt->valid_from.hour,
2132 crt->valid_from.min, crt->valid_from.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002133 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002135 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002136 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2137 crt->valid_to.year, crt->valid_to.mon,
2138 crt->valid_to.day, crt->valid_to.hour,
2139 crt->valid_to.min, crt->valid_to.sec );
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é-Gonnard2cf5a7c2015-04-08 12:49:31 +02002142 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002143 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002145 ret = mbedtls_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02002146 crt->sig_md, crt->sig_opts );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002147 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002148
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002149 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002150 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
2151 mbedtls_pk_get_name( &crt->pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002152 {
2153 return( ret );
2154 }
2155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002156 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +02002157 (int) mbedtls_pk_get_bitlen( &crt->pk ) );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002158 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002159
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002160 /*
2161 * Optional extensions
2162 */
2163
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002164 if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002165 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002166 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002167 crt->ca_istrue ? "true" : "false" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002168 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002169
2170 if( crt->max_pathlen > 0 )
2171 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002172 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002173 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002174 }
2175 }
2176
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002177 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002178 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002179 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name :", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002180 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002181
2182 if( ( ret = x509_info_subject_alt_name( &p, &n,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002183 &crt->subject_alt_names,
Ron Eldor6aeae9e2019-05-20 12:00:36 +03002184 prefix ) ) != 0 )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002185 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_NS_CERT_TYPE )
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%scert. type : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002191 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002192
2193 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
2194 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002195 }
2196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002197 if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002198 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002199 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002200 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002201
2202 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
2203 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002204 }
2205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002206 if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002207 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002208 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002209 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002210
2211 if( ( ret = x509_info_ext_key_usage( &p, &n,
2212 &crt->ext_key_usage ) ) != 0 )
2213 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002214 }
2215
Ron Eldor74d9acc2019-03-21 14:00:03 +02002216 if( crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES )
2217 {
2218 ret = mbedtls_snprintf( p, n, "\n%scertificate policies : ", prefix );
2219 MBEDTLS_X509_SAFE_SNPRINTF;
2220
2221 if( ( ret = x509_info_cert_policies( &p, &n,
2222 &crt->certificate_policies ) ) != 0 )
2223 return( ret );
2224 }
2225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002226 ret = mbedtls_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002227 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002228
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002229 return( (int) ( size - n ) );
2230}
2231
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002232struct x509_crt_verify_string {
2233 int code;
2234 const char *string;
2235};
2236
Hanno Becker7ac83f92020-10-09 10:48:22 +01002237#define X509_CRT_ERROR_INFO( err, err_str, info ) { err, info },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002238static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Hanno Becker7ac83f92020-10-09 10:48:22 +01002239 MBEDTLS_X509_CRT_ERROR_INFO_LIST
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002240 { 0, NULL }
2241};
Hanno Becker7ac83f92020-10-09 10:48:22 +01002242#undef X509_CRT_ERROR_INFO
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002243
2244int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002245 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002246{
Janos Follath865b3eb2019-12-16 11:46:15 +00002247 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002248 const struct x509_crt_verify_string *cur;
2249 char *p = buf;
2250 size_t n = size;
2251
2252 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
2253 {
2254 if( ( flags & cur->code ) == 0 )
2255 continue;
2256
2257 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002258 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002259 flags ^= cur->code;
2260 }
2261
2262 if( flags != 0 )
2263 {
2264 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
2265 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002266 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002267 }
2268
2269 return( (int) ( size - n ) );
2270}
Hanno Becker612a2f12020-10-09 09:19:39 +01002271#endif /* MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002272
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002273int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
2274 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002275{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002276 unsigned int usage_must, usage_may;
2277 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
2278 | MBEDTLS_X509_KU_DECIPHER_ONLY;
2279
2280 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
2281 return( 0 );
2282
2283 usage_must = usage & ~may_mask;
2284
2285 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
2286 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
2287
2288 usage_may = usage & may_mask;
2289
2290 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002291 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002292
2293 return( 0 );
2294}
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002296int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002297 const char *usage_oid,
2298 size_t usage_len )
2299{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002300 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002301
2302 /* Extension is not mandatory, absent means no restriction */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002303 if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002304 return( 0 );
2305
2306 /*
2307 * Look for the requested usage (or wildcard ANY) in our list
2308 */
2309 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
2310 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002311 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002312
2313 if( cur_oid->len == usage_len &&
2314 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
2315 {
2316 return( 0 );
2317 }
2318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002319 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002320 return( 0 );
2321 }
2322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002323 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002324}
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002326#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002327/*
2328 * Return 1 if the certificate is revoked, or 0 otherwise.
2329 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002330int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002331{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002332 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002333
2334 while( cur != NULL && cur->serial.len != 0 )
2335 {
2336 if( crt->serial.len == cur->serial.len &&
2337 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
2338 {
Raoul Strackxa4e86142020-06-15 17:03:13 +02002339 return( 1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002340 }
2341
2342 cur = cur->next;
2343 }
2344
2345 return( 0 );
2346}
2347
2348/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002349 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002350 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002351 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002352static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002353 mbedtls_x509_crl *crl_list,
2354 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002355{
2356 int flags = 0;
Przemek Stekiel40afdd22022-09-06 13:08:28 +02002357 unsigned char hash[MBEDTLS_HASH_MAX_SIZE];
pespacek7599a772022-02-07 14:40:55 +01002358#if defined(MBEDTLS_USE_PSA_CRYPTO)
pespacek7599a772022-02-07 14:40:55 +01002359 psa_algorithm_t psa_algorithm;
2360#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002361 const mbedtls_md_info_t *md_info;
pespacek7599a772022-02-07 14:40:55 +01002362#endif /* MBEDTLS_USE_PSA_CRYPTO */
2363 size_t hash_length;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002364
2365 if( ca == NULL )
2366 return( flags );
2367
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002368 while( crl_list != NULL )
2369 {
2370 if( crl_list->version == 0 ||
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002371 x509_name_cmp( &crl_list->issuer, &ca->subject ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002372 {
2373 crl_list = crl_list->next;
2374 continue;
2375 }
2376
2377 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002378 * Check if the CA is configured to sign CRLs
2379 */
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002380 if( mbedtls_x509_crt_check_key_usage( ca,
2381 MBEDTLS_X509_KU_CRL_SIGN ) != 0 )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002382 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002383 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002384 break;
2385 }
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002386
2387 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002388 * Check if CRL is correctly signed by the trusted CA
2389 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002390 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
2391 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
2392
2393 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
2394 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
2395
pespacek7599a772022-02-07 14:40:55 +01002396#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02002397 psa_algorithm = mbedtls_hash_info_psa_from_md( crl_list->sig_md );
pespacekd924e552022-02-28 11:49:54 +01002398 if( psa_hash_compute( psa_algorithm,
2399 crl_list->tbs.p,
2400 crl_list->tbs.len,
2401 hash,
2402 sizeof( hash ),
2403 &hash_length ) != PSA_SUCCESS )
pespaceka7a64692022-02-14 15:18:43 +01002404 {
2405 /* Note: this can't happen except after an internal error */
2406 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
2407 break;
2408 }
pespacek7599a772022-02-07 14:40:55 +01002409#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002410 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
pespacek7599a772022-02-07 14:40:55 +01002411 hash_length = mbedtls_md_get_size( md_info );
2412 if( mbedtls_md( md_info,
2413 crl_list->tbs.p,
2414 crl_list->tbs.len,
2415 hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002416 {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002417 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002418 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002419 break;
2420 }
pespaceka7a64692022-02-14 15:18:43 +01002421#endif /* MBEDTLS_USE_PSA_CRYPTO */
2422
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002423 if( x509_profile_check_key( profile, &ca->pk ) != 0 )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002424 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002426 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
pespacek7599a772022-02-07 14:40:55 +01002427 crl_list->sig_md, hash, hash_length,
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02002428 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002429 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002430 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002431 break;
2432 }
2433
2434 /*
2435 * Check for validity of CRL (Do not drop out)
2436 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002437 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002438 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002439
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002440 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002441 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002442
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002443 /*
2444 * Check if certificate is revoked
2445 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002446 if( mbedtls_x509_crt_is_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002447 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002448 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002449 break;
2450 }
2451
2452 crl_list = crl_list->next;
2453 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002454
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002455 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002456}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002457#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002458
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002459/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002460 * Check the signature of a certificate by its parent
2461 */
2462static int x509_crt_check_signature( const mbedtls_x509_crt *child,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002463 mbedtls_x509_crt *parent,
2464 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002465{
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002466 size_t hash_len;
2467#if !defined(MBEDTLS_USE_PSA_CRYPTO)
pespacek7599a772022-02-07 14:40:55 +01002468 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002469 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002470 md_info = mbedtls_md_info_from_type( child->sig_md );
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002471 hash_len = mbedtls_md_get_size( md_info );
Andrzej Kurek8b38ff52018-11-20 03:20:09 -05002472
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002473 /* Note: hash errors can happen only after an internal error */
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002474 if( mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash ) != 0 )
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002475 return( -1 );
2476#else
pespacek7599a772022-02-07 14:40:55 +01002477 unsigned char hash[PSA_HASH_MAX_SIZE];
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02002478 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md( child->sig_md );
pespacek7599a772022-02-07 14:40:55 +01002479 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002480
pespacek7599a772022-02-07 14:40:55 +01002481 status = psa_hash_compute( hash_alg,
2482 child->tbs.p,
2483 child->tbs.len,
2484 hash,
pespacekb9f07a72022-02-14 15:13:26 +01002485 sizeof( hash ),
pespacek7599a772022-02-07 14:40:55 +01002486 &hash_len );
2487 if( status != PSA_SUCCESS )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002488 {
pespacek3110c7b2022-02-14 15:07:41 +01002489 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002490 }
2491
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002492#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002493 /* Skip expensive computation on obvious mismatch */
2494 if( ! mbedtls_pk_can_do( &parent->pk, child->sig_pk ) )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002495 return( -1 );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002496
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002497#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002498 if( rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA )
2499 {
2500 return( mbedtls_pk_verify_restartable( &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002501 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002502 child->sig.p, child->sig.len, &rs_ctx->pk ) );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002503 }
2504#else
2505 (void) rs_ctx;
2506#endif
2507
2508 return( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002509 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002510 child->sig.p, child->sig.len ) );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002511}
2512
2513/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002514 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2515 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002516 *
2517 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002518 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002519static int x509_crt_check_parent( const mbedtls_x509_crt *child,
2520 const mbedtls_x509_crt *parent,
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002521 int top )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002522{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002523 int need_ca_bit;
2524
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002525 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02002526 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002527 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002528
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002529 /* Parent must have the basicConstraints CA bit set as a general rule */
2530 need_ca_bit = 1;
2531
2532 /* Exception: v1/v2 certificates that are locally trusted. */
2533 if( top && parent->version < 3 )
2534 need_ca_bit = 0;
2535
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002536 if( need_ca_bit && ! parent->ca_istrue )
2537 return( -1 );
2538
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002539 if( need_ca_bit &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002540 mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002541 {
2542 return( -1 );
2543 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002544
2545 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002546}
2547
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002548/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002549 * Find a suitable parent for child in candidates, or return NULL.
2550 *
2551 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002552 * 1. subject name matches child's issuer
2553 * 2. if necessary, the CA bit is set and key usage allows signing certs
2554 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002555 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002556 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002557 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002558 * If there's a suitable candidate which is also time-valid, return the first
2559 * such. Otherwise, return the first suitable candidate (or NULL if there is
2560 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002561 *
2562 * The rationale for this rule is that someone could have a list of trusted
2563 * roots with two versions on the same root with different validity periods.
2564 * (At least one user reported having such a list and wanted it to just work.)
2565 * The reason we don't just require time-validity is that generally there is
2566 * only one version, and if it's expired we want the flags to state that
2567 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002568 *
2569 * The rationale for rule 3 (signature for trusted roots) is that users might
2570 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002571 * way we select the correct one is by checking the signature (as we don't
2572 * rely on key identifier extensions). (This is one way users might choose to
2573 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002574 *
2575 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002576 * - [in] child: certificate for which we're looking for a parent
2577 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002578 * - [out] r_parent: parent found (or NULL)
2579 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002580 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2581 * of the chain, 0 otherwise
2582 * - [in] path_cnt: number of intermediates seen so far
2583 * - [in] self_cnt: number of self-signed intermediates seen so far
2584 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002585 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002586 *
2587 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002588 * - 0 on success
2589 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002590 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002591static int x509_crt_find_parent_in(
2592 mbedtls_x509_crt *child,
2593 mbedtls_x509_crt *candidates,
2594 mbedtls_x509_crt **r_parent,
2595 int *r_signature_is_good,
2596 int top,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002597 unsigned path_cnt,
2598 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002599 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002600{
Janos Follath865b3eb2019-12-16 11:46:15 +00002601 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002602 mbedtls_x509_crt *parent, *fallback_parent;
Benjamin Kier36050732019-05-30 14:49:17 -04002603 int signature_is_good = 0, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002604
2605#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002606 /* did we have something in progress? */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002607 if( rs_ctx != NULL && rs_ctx->parent != NULL )
2608 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002609 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002610 parent = rs_ctx->parent;
2611 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002612 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002613
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002614 /* clear saved state */
2615 rs_ctx->parent = NULL;
2616 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002617 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002618
2619 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002620 goto check_signature;
2621 }
2622#endif
2623
2624 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002625 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002626
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002627 for( parent = candidates; parent != NULL; parent = parent->next )
2628 {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002629 /* basic parenting skills (name, CA bit, key usage) */
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002630 if( x509_crt_check_parent( child, parent, top ) != 0 )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002631 continue;
2632
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002633 /* +1 because stored max_pathlen is 1 higher that the actual value */
2634 if( parent->max_pathlen > 0 &&
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002635 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt )
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002636 {
2637 continue;
2638 }
2639
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002640 /* Signature */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002641#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2642check_signature:
2643#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002644 ret = x509_crt_check_signature( child, parent, rs_ctx );
2645
2646#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002647 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2648 {
2649 /* save state */
2650 rs_ctx->parent = parent;
2651 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002652 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002653
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002654 return( ret );
2655 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002656#else
2657 (void) ret;
2658#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002659
2660 signature_is_good = ret == 0;
2661 if( top && ! signature_is_good )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002662 continue;
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002663
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002664 /* optional time check */
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002665 if( mbedtls_x509_time_is_past( &parent->valid_to ) ||
2666 mbedtls_x509_time_is_future( &parent->valid_from ) )
2667 {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002668 if( fallback_parent == NULL )
2669 {
2670 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002671 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002672 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002673
2674 continue;
2675 }
2676
Andy Gross1f627142019-01-30 10:25:53 -06002677 *r_parent = parent;
2678 *r_signature_is_good = signature_is_good;
2679
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002680 break;
2681 }
2682
Andy Gross1f627142019-01-30 10:25:53 -06002683 if( parent == NULL )
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002684 {
2685 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002686 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002687 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002688
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002689 return( 0 );
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002690}
2691
2692/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002693 * Find a parent in trusted CAs or the provided chain, or return NULL.
2694 *
2695 * Searches in trusted CAs first, and return the first suitable parent found
2696 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002697 *
2698 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002699 * - [in] child: certificate for which we're looking for a parent, followed
2700 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002701 * - [in] trust_ca: list of locally trusted certificates
2702 * - [out] parent: parent found (or NULL)
2703 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2704 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2705 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2706 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002707 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002708 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002709 *
2710 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002711 * - 0 on success
2712 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002713 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002714static int x509_crt_find_parent(
2715 mbedtls_x509_crt *child,
2716 mbedtls_x509_crt *trust_ca,
2717 mbedtls_x509_crt **parent,
2718 int *parent_is_trusted,
2719 int *signature_is_good,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002720 unsigned path_cnt,
2721 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002722 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002723{
Janos Follath865b3eb2019-12-16 11:46:15 +00002724 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002725 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002726
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002727 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002728
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002729#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002730 /* restore then clear saved state if we have some stored */
2731 if( rs_ctx != NULL && rs_ctx->parent_is_trusted != -1 )
2732 {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002733 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002734 rs_ctx->parent_is_trusted = -1;
2735 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002736#endif
2737
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002738 while( 1 ) {
2739 search_list = *parent_is_trusted ? trust_ca : child->next;
2740
2741 ret = x509_crt_find_parent_in( child, search_list,
2742 parent, signature_is_good,
2743 *parent_is_trusted,
2744 path_cnt, self_cnt, rs_ctx );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002745
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002746#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002747 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2748 {
2749 /* save state */
2750 rs_ctx->parent_is_trusted = *parent_is_trusted;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002751 return( ret );
2752 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002753#else
2754 (void) ret;
2755#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002756
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002757 /* stop here if found or already in second iteration */
2758 if( *parent != NULL || *parent_is_trusted == 0 )
2759 break;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002760
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002761 /* prepare second iteration */
2762 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002763 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002764
2765 /* extra precaution against mistakes in the caller */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002766 if( *parent == NULL )
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002767 {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002768 *parent_is_trusted = 0;
2769 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002770 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002771
2772 return( 0 );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002773}
2774
2775/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002776 * Check if an end-entity certificate is locally trusted
2777 *
2778 * Currently we require such certificates to be self-signed (actually only
2779 * check for self-issued as self-signatures are not checked)
2780 */
2781static int x509_crt_check_ee_locally_trusted(
2782 mbedtls_x509_crt *crt,
2783 mbedtls_x509_crt *trust_ca )
2784{
2785 mbedtls_x509_crt *cur;
2786
2787 /* must be self-issued */
2788 if( x509_name_cmp( &crt->issuer, &crt->subject ) != 0 )
2789 return( -1 );
2790
2791 /* look for an exact match with trusted cert */
2792 for( cur = trust_ca; cur != NULL; cur = cur->next )
2793 {
2794 if( crt->raw.len == cur->raw.len &&
2795 memcmp( crt->raw.p, cur->raw.p, crt->raw.len ) == 0 )
2796 {
2797 return( 0 );
2798 }
2799 }
2800
2801 /* too bad */
2802 return( -1 );
2803}
2804
2805/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002806 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002807 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002808 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2809 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002810 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002811 * such that every cert in the chain is a child of the next one,
2812 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002813 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002814 * Verify that chain and return it with flags for all issues found.
2815 *
2816 * Special cases:
2817 * - EE == Rj -> return a one-element list containing it
2818 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2819 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002820 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002821 * Tests for (aspects of) this function should include at least:
2822 * - trusted EE
2823 * - EE -> trusted root
Antonin Décimo36e89b52019-01-23 15:24:37 +01002824 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002825 * - if relevant: EE untrusted
2826 * - if relevant: EE -> intermediate, untrusted
2827 * with the aspect under test checked at each relevant level (EE, int, root).
2828 * For some aspects longer chains are required, but usually length 2 is
2829 * enough (but length 1 is not in general).
2830 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002831 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002832 * - [in] crt: the cert list EE, C1, ..., Cn
2833 * - [in] trust_ca: the trusted list R1, ..., Rp
2834 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002835 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002836 * Only valid when return value is 0, may contain garbage otherwise!
2837 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002838 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002839 *
2840 * Return value:
2841 * - non-zero if the chain could not be fully built and examined
2842 * - 0 is the chain was successfully built and examined,
2843 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002844 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002845static int x509_crt_verify_chain(
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002846 mbedtls_x509_crt *crt,
2847 mbedtls_x509_crt *trust_ca,
2848 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00002849 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2850 void *p_ca_cb,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002851 const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002852 mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002853 mbedtls_x509_crt_restart_ctx *rs_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002854{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002855 /* Don't initialize any of those variables here, so that the compiler can
2856 * catch potential issues with jumping ahead when restarting */
Janos Follath865b3eb2019-12-16 11:46:15 +00002857 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002858 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002859 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002860 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002861 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002862 int parent_is_trusted;
2863 int child_is_trusted;
2864 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002865 unsigned self_cnt;
Hanno Beckerf53893b2019-03-28 13:45:55 +00002866 mbedtls_x509_crt *cur_trust_ca = NULL;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002867
2868#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2869 /* resume if we had an operation in progress */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002870 if( rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent )
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002871 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002872 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002873 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002874 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002875
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002876 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002877 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002878 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002879 flags = &cur->flags;
2880
2881 goto find_parent;
2882 }
2883#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002884
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002885 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002886 self_cnt = 0;
2887 parent_is_trusted = 0;
2888 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002889
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002890 while( 1 ) {
2891 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002892 cur = &ver_chain->items[ver_chain->len];
2893 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002894 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002895 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002896 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002897
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002898 /* Check time-validity (all certificates) */
2899 if( mbedtls_x509_time_is_past( &child->valid_to ) )
2900 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002901
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002902 if( mbedtls_x509_time_is_future( &child->valid_from ) )
2903 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002904
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002905 /* Stop here for trusted roots (but not for trusted EE certs) */
2906 if( child_is_trusted )
2907 return( 0 );
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002908
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002909 /* Check signature algorithm: MD & PK algs */
2910 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
2911 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002912
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002913 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
2914 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002915
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002916 /* Special case: EE certs that are locally trusted */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002917 if( ver_chain->len == 1 &&
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002918 x509_crt_check_ee_locally_trusted( child, trust_ca ) == 0 )
2919 {
2920 return( 0 );
2921 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002922
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002923#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2924find_parent:
2925#endif
Hanno Beckerf53893b2019-03-28 13:45:55 +00002926
2927 /* Obtain list of potential trusted signers from CA callback,
2928 * or use statically provided list. */
2929#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
2930 if( f_ca_cb != NULL )
2931 {
2932 mbedtls_x509_crt_free( ver_chain->trust_ca_cb_result );
2933 mbedtls_free( ver_chain->trust_ca_cb_result );
2934 ver_chain->trust_ca_cb_result = NULL;
2935
2936 ret = f_ca_cb( p_ca_cb, child, &ver_chain->trust_ca_cb_result );
2937 if( ret != 0 )
2938 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2939
2940 cur_trust_ca = ver_chain->trust_ca_cb_result;
2941 }
2942 else
2943#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2944 {
2945 ((void) f_ca_cb);
2946 ((void) p_ca_cb);
2947 cur_trust_ca = trust_ca;
2948 }
2949
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002950 /* Look for a parent in trusted CAs or up the chain */
Hanno Beckerf53893b2019-03-28 13:45:55 +00002951 ret = x509_crt_find_parent( child, cur_trust_ca, &parent,
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002952 &parent_is_trusted, &signature_is_good,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002953 ver_chain->len - 1, self_cnt, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002954
2955#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002956 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2957 {
2958 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002959 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002960 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002961 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002962
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002963 return( ret );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002964 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002965#else
2966 (void) ret;
2967#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002968
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002969 /* No parent? We're done here */
2970 if( parent == NULL )
2971 {
2972 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2973 return( 0 );
2974 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002975
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002976 /* Count intermediate self-issued (not necessarily self-signed) certs.
2977 * These can occur with some strategies for key rollover, see [SIRO],
2978 * and should be excluded from max_pathlen checks. */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002979 if( ver_chain->len != 1 &&
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002980 x509_name_cmp( &child->issuer, &child->subject ) == 0 )
2981 {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002982 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002983 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002984
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002985 /* path_cnt is 0 for the first intermediate CA,
2986 * and if parent is trusted it's not an intermediate CA */
2987 if( ! parent_is_trusted &&
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002988 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002989 {
2990 /* return immediately to avoid overflow the chain array */
2991 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2992 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002993
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002994 /* signature was checked while searching parent */
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002995 if( ! signature_is_good )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002996 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2997
2998 /* check size of signing key */
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002999 if( x509_profile_check_key( profile, &parent->pk ) != 0 )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003000 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003002#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003003 /* Check trusted CA's CRL for the given crt */
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02003004 *flags |= x509_crt_verifycrl( child, parent, ca_crl, profile );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003005#else
3006 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003007#endif
3008
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003009 /* prepare for next iteration */
3010 child = parent;
3011 parent = NULL;
3012 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003013 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003014 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003015}
3016
3017/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003018 * Check for CN match
3019 */
3020static int x509_crt_check_cn( const mbedtls_x509_buf *name,
3021 const char *cn, size_t cn_len )
3022{
3023 /* try exact match */
3024 if( name->len == cn_len &&
3025 x509_memcasecmp( cn, name->p, cn_len ) == 0 )
3026 {
3027 return( 0 );
3028 }
3029
3030 /* try wildcard match */
Manuel Pégourié-Gonnard900fba62017-10-18 14:28:11 +02003031 if( x509_check_wildcard( cn, name ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003032 {
3033 return( 0 );
3034 }
3035
3036 return( -1 );
3037}
3038
3039/*
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003040 * Check for SAN match, see RFC 5280 Section 4.2.1.6
3041 */
3042static int x509_crt_check_san( const mbedtls_x509_buf *name,
3043 const char *cn, size_t cn_len )
3044{
3045 const unsigned char san_type = (unsigned char) name->tag &
3046 MBEDTLS_ASN1_TAG_VALUE_MASK;
3047
3048 /* dNSName */
3049 if( san_type == MBEDTLS_X509_SAN_DNS_NAME )
3050 return( x509_crt_check_cn( name, cn, cn_len ) );
3051
3052 /* (We may handle other types here later.) */
3053
3054 /* Unrecognized type */
3055 return( -1 );
3056}
3057
3058/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003059 * Verify the requested CN - only call this if cn is not NULL!
3060 */
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003061static void x509_crt_verify_name( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003062 const char *cn,
3063 uint32_t *flags )
3064{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003065 const mbedtls_x509_name *name;
3066 const mbedtls_x509_sequence *cur;
3067 size_t cn_len = strlen( cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003068
3069 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
3070 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003071 for( cur = &crt->subject_alt_names; cur != NULL; cur = cur->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003072 {
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003073 if( x509_crt_check_san( &cur->buf, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003074 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003075 }
3076
3077 if( cur == NULL )
3078 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3079 }
3080 else
3081 {
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02003082 for( name = &crt->subject; name != NULL; name = name->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003083 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003084 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, &name->oid ) == 0 &&
3085 x509_crt_check_cn( &name->val, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003086 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003087 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003088 }
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003089 }
3090
3091 if( name == NULL )
3092 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3093 }
3094}
3095
3096/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003097 * Merge the flags for all certs in the chain, after calling callback
3098 */
3099static int x509_crt_merge_flags_with_cb(
3100 uint32_t *flags,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003101 const mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003102 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3103 void *p_vrfy )
3104{
Janos Follath865b3eb2019-12-16 11:46:15 +00003105 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003106 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003107 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003108 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003109
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003110 for( i = ver_chain->len; i != 0; --i )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003111 {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003112 cur = &ver_chain->items[i-1];
3113 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003114
3115 if( NULL != f_vrfy )
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003116 if( ( ret = f_vrfy( p_vrfy, cur->crt, (int) i-1, &cur_flags ) ) != 0 )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003117 return( ret );
3118
3119 *flags |= cur_flags;
3120 }
3121
3122 return( 0 );
3123}
3124
3125/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003126 * Verify the certificate validity, with profile, restartable version
3127 *
3128 * This function:
3129 * - checks the requested CN (if any)
3130 * - checks the type and size of the EE cert's key,
3131 * as that isn't done as part of chain building/verification currently
3132 * - builds and verifies the chain
3133 * - then calls the callback and merges the flags
Hanno Becker3116fb32019-03-28 13:34:42 +00003134 *
3135 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
3136 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
3137 * verification routine to search for trusted signers, and CRLs will
3138 * be disabled. Otherwise, `trust_ca` will be used as the static list
3139 * of trusted signers, and `ca_crl` will be use as the static list
3140 * of CRLs.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003141 */
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003142static int x509_crt_verify_restartable_ca_cb( mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003143 mbedtls_x509_crt *trust_ca,
3144 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003145 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3146 void *p_ca_cb,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003147 const mbedtls_x509_crt_profile *profile,
3148 const char *cn, uint32_t *flags,
3149 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3150 void *p_vrfy,
3151 mbedtls_x509_crt_restart_ctx *rs_ctx )
3152{
Janos Follath865b3eb2019-12-16 11:46:15 +00003153 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003154 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003155 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003156 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003157
3158 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003159 ee_flags = 0;
3160 x509_crt_verify_chain_reset( &ver_chain );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003161
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003162 if( profile == NULL )
3163 {
3164 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3165 goto exit;
3166 }
3167
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003168 /* check name if requested */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003169 if( cn != NULL )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003170 x509_crt_verify_name( crt, cn, &ee_flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003171
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003172 /* Check the type and size of the key */
3173 pk_type = mbedtls_pk_get_type( &crt->pk );
3174
3175 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003176 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003177
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02003178 if( x509_profile_check_key( profile, &crt->pk ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003179 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003180
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003181 /* Check the chain */
Hanno Becker3116fb32019-03-28 13:34:42 +00003182 ret = x509_crt_verify_chain( crt, trust_ca, ca_crl,
3183 f_ca_cb, p_ca_cb, profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003184 &ver_chain, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003185
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003186 if( ret != 0 )
3187 goto exit;
3188
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003189 /* Merge end-entity flags */
3190 ver_chain.items[0].flags |= ee_flags;
3191
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003192 /* Build final flags, calling callback on the way if any */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003193 ret = x509_crt_merge_flags_with_cb( flags, &ver_chain, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003194
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003195exit:
Hanno Beckerf53893b2019-03-28 13:45:55 +00003196
3197#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3198 mbedtls_x509_crt_free( ver_chain.trust_ca_cb_result );
3199 mbedtls_free( ver_chain.trust_ca_cb_result );
3200 ver_chain.trust_ca_cb_result = NULL;
3201#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3202
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003203#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3204 if( rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
3205 mbedtls_x509_crt_restart_free( rs_ctx );
3206#endif
3207
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003208 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3209 * the SSL module for authmode optional, but non-zero return from the
3210 * callback means a fatal error so it shouldn't be ignored */
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003211 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
3212 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3213
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003214 if( ret != 0 )
3215 {
3216 *flags = (uint32_t) -1;
3217 return( ret );
3218 }
3219
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003220 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003221 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003222
3223 return( 0 );
3224}
3225
Hanno Becker3116fb32019-03-28 13:34:42 +00003226
3227/*
3228 * Verify the certificate validity (default profile, not restartable)
3229 */
3230int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
3231 mbedtls_x509_crt *trust_ca,
3232 mbedtls_x509_crl *ca_crl,
3233 const char *cn, uint32_t *flags,
3234 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3235 void *p_vrfy )
3236{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003237 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003238 NULL, NULL,
3239 &mbedtls_x509_crt_profile_default,
3240 cn, flags,
3241 f_vrfy, p_vrfy, NULL ) );
3242}
3243
3244/*
3245 * Verify the certificate validity (user-chosen profile, not restartable)
3246 */
3247int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
3248 mbedtls_x509_crt *trust_ca,
3249 mbedtls_x509_crl *ca_crl,
3250 const mbedtls_x509_crt_profile *profile,
3251 const char *cn, uint32_t *flags,
3252 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3253 void *p_vrfy )
3254{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003255 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003256 NULL, NULL,
3257 profile, cn, flags,
3258 f_vrfy, p_vrfy, NULL ) );
3259}
3260
3261#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3262/*
3263 * Verify the certificate validity (user-chosen profile, CA callback,
3264 * not restartable).
3265 */
Jarno Lamsa31d9db62019-04-01 14:33:49 +03003266int mbedtls_x509_crt_verify_with_ca_cb( mbedtls_x509_crt *crt,
Hanno Becker3116fb32019-03-28 13:34:42 +00003267 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3268 void *p_ca_cb,
3269 const mbedtls_x509_crt_profile *profile,
3270 const char *cn, uint32_t *flags,
3271 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3272 void *p_vrfy )
3273{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003274 return( x509_crt_verify_restartable_ca_cb( crt, NULL, NULL,
Hanno Becker3116fb32019-03-28 13:34:42 +00003275 f_ca_cb, p_ca_cb,
3276 profile, cn, flags,
3277 f_vrfy, p_vrfy, NULL ) );
3278}
3279#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3280
3281int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
3282 mbedtls_x509_crt *trust_ca,
3283 mbedtls_x509_crl *ca_crl,
3284 const mbedtls_x509_crt_profile *profile,
3285 const char *cn, uint32_t *flags,
3286 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3287 void *p_vrfy,
3288 mbedtls_x509_crt_restart_ctx *rs_ctx )
3289{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003290 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003291 NULL, NULL,
3292 profile, cn, flags,
3293 f_vrfy, p_vrfy, rs_ctx ) );
3294}
3295
3296
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003297/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003298 * Initialize a certificate chain
3299 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003300void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02003301{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003302 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02003303}
3304
3305/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003306 * Unallocate all certificate data
3307 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003308void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003309{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003310 mbedtls_x509_crt *cert_cur = crt;
3311 mbedtls_x509_crt *cert_prv;
3312 mbedtls_x509_name *name_cur;
3313 mbedtls_x509_name *name_prv;
3314 mbedtls_x509_sequence *seq_cur;
3315 mbedtls_x509_sequence *seq_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003316
3317 if( crt == NULL )
3318 return;
3319
3320 do
3321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003322 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003324#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
3325 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003326#endif
3327
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003328 name_cur = cert_cur->issuer.next;
3329 while( name_cur != NULL )
3330 {
3331 name_prv = name_cur;
3332 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003333 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003334 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003335 }
3336
3337 name_cur = cert_cur->subject.next;
3338 while( name_cur != NULL )
3339 {
3340 name_prv = name_cur;
3341 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003342 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003343 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003344 }
3345
3346 seq_cur = cert_cur->ext_key_usage.next;
3347 while( seq_cur != NULL )
3348 {
3349 seq_prv = seq_cur;
3350 seq_cur = seq_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003351 mbedtls_platform_zeroize( seq_prv,
3352 sizeof( mbedtls_x509_sequence ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003353 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003354 }
3355
3356 seq_cur = cert_cur->subject_alt_names.next;
3357 while( seq_cur != NULL )
3358 {
3359 seq_prv = seq_cur;
3360 seq_cur = seq_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003361 mbedtls_platform_zeroize( seq_prv,
3362 sizeof( mbedtls_x509_sequence ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003363 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003364 }
3365
Ron Eldor74d9acc2019-03-21 14:00:03 +02003366 seq_cur = cert_cur->certificate_policies.next;
3367 while( seq_cur != NULL )
3368 {
3369 seq_prv = seq_cur;
3370 seq_cur = seq_cur->next;
3371 mbedtls_platform_zeroize( seq_prv,
3372 sizeof( mbedtls_x509_sequence ) );
3373 mbedtls_free( seq_prv );
3374 }
3375
Hanno Becker1a65dcd2019-01-31 08:57:44 +00003376 if( cert_cur->raw.p != NULL && cert_cur->own_buffer )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003377 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003378 mbedtls_platform_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003379 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003380 }
3381
3382 cert_cur = cert_cur->next;
3383 }
3384 while( cert_cur != NULL );
3385
3386 cert_cur = crt;
3387 do
3388 {
3389 cert_prv = cert_cur;
3390 cert_cur = cert_cur->next;
3391
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003392 mbedtls_platform_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003393 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003394 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003395 }
3396 while( cert_cur != NULL );
3397}
3398
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003399#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3400/*
3401 * Initialize a restart context
3402 */
3403void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx )
3404{
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003405 mbedtls_pk_restart_init( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003406
3407 ctx->parent = NULL;
3408 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003409 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003410
3411 ctx->parent_is_trusted = -1;
3412
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003413 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003414 ctx->self_cnt = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003415 x509_crt_verify_chain_reset( &ctx->ver_chain );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003416}
3417
3418/*
3419 * Free the components of a restart context
3420 */
3421void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx )
3422{
3423 if( ctx == NULL )
3424 return;
3425
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003426 mbedtls_pk_restart_free( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003427 mbedtls_x509_crt_restart_init( ctx );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003428}
3429#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003431#endif /* MBEDTLS_X509_CRT_PARSE_C */