blob: c4f97bbe2bfa1fc2b24bfc976cc2884635ad84d6 [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é-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000056#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010057#endif
58
Daniel Axtensf0710242020-05-28 11:43:41 +100059#if defined(MBEDTLS_HAVE_TIME)
Paul Bakkerfa6a6202013-10-28 18:48:30 +010060#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020061#include <windows.h>
62#else
63#include <time.h>
64#endif
Daniel Axtensf0710242020-05-28 11:43:41 +100065#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067#if defined(MBEDTLS_FS_IO)
Rich Evans00ab4702015-02-06 13:43:58 +000068#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020069#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020070#include <sys/types.h>
71#include <sys/stat.h>
Martino Facchin0ec05ec2021-05-04 11:47:36 +020072#if defined(__MBED__)
73#include <platform/mbed_retarget.h>
74#else
Paul Bakker7c6b2c32013-09-16 13:49:26 +020075#include <dirent.h>
Martino Facchin0ec05ec2021-05-04 11:47:36 +020076#endif /* __MBED__ */
Eduardo Silvae1bfffc2019-04-25 10:43:26 -060077#include <errno.h>
Rich Evans00ab4702015-02-06 13:43:58 +000078#endif /* !_WIN32 || EFIX64 || EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020079#endif
80
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +020081/*
82 * Item in a verification chain: cert and flags for it
83 */
84typedef struct {
85 mbedtls_x509_crt *crt;
86 uint32_t flags;
87} x509_crt_verify_chain_item;
88
89/*
90 * Max size of verification chain: end-entity + intermediates + trusted root
91 */
92#define X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
Paul Bakker34617722014-06-13 17:20:13 +020093
Gilles Peskineffb92da2021-06-02 00:03:26 +020094/* Default profile. Do not remove items unless there are serious security
95 * concerns. */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020096const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
97{
Gilles Peskineae270bf2021-06-02 00:05:29 +020098 /* Hashes from SHA-256 and above. Note that this selection
99 * should be aligned with ssl_preset_default_hashes in ssl_tls.c. */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200100 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
101 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
102 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
103 0xFFFFFFF, /* Any PK alg */
104#if defined(MBEDTLS_ECP_C)
Gilles Peskineae270bf2021-06-02 00:05:29 +0200105 /* Curves at or above 128-bit security level. Note that this selection
106 * should be aligned with ssl_preset_default_curves in ssl_tls.c. */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200107 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
108 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
109 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
110 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
111 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
112 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
Gilles Peskine39957502021-06-17 23:17:52 +0200113 0,
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200114#else
115 0,
116#endif
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200117 2048,
118};
119
Gilles Peskineffb92da2021-06-02 00:03:26 +0200120/* Next-generation profile. Currently identical to the default, but may
121 * be tightened at any time. */
122const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200123{
124 /* Hashes from SHA-256 and above. */
125 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
126 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
127 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
128 0xFFFFFFF, /* Any PK alg */
129#if defined(MBEDTLS_ECP_C)
130 /* Curves at or above 128-bit security level. */
131 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
132 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
133 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
134 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
135 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
136 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
137 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256K1 ),
138#else
139 0,
140#endif
141 2048,
142};
Gilles Peskineffb92da2021-06-02 00:03:26 +0200143
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200144/*
145 * NSA Suite B Profile
146 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200147const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
148{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200149 /* Only SHA-256 and 384 */
150 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
151 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ),
152 /* Only ECDSA */
Ron Eldor85e1dcf2018-02-06 15:59:38 +0200153 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECDSA ) |
154 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECKEY ),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200155#if defined(MBEDTLS_ECP_C)
156 /* Only NIST P-256 and P-384 */
157 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
158 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ),
159#else
160 0,
161#endif
162 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200163};
164
165/*
Manuel Pégourié-Gonnard9d4c2c42021-06-18 09:48:27 +0200166 * Empty / all-forbidden profile
167 */
168const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_none =
169{
170 0,
171 0,
172 0,
173 (uint32_t) -1,
174};
175
176/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200177 * Check md_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200178 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200179 */
180static int x509_profile_check_md_alg( const mbedtls_x509_crt_profile *profile,
181 mbedtls_md_type_t md_alg )
182{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200183 if( md_alg == MBEDTLS_MD_NONE )
184 return( -1 );
185
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200186 if( ( profile->allowed_mds & MBEDTLS_X509_ID_FLAG( md_alg ) ) != 0 )
187 return( 0 );
188
189 return( -1 );
190}
191
192/*
193 * Check pk_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200194 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200195 */
196static int x509_profile_check_pk_alg( const mbedtls_x509_crt_profile *profile,
197 mbedtls_pk_type_t pk_alg )
198{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200199 if( pk_alg == MBEDTLS_PK_NONE )
200 return( -1 );
201
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200202 if( ( profile->allowed_pks & MBEDTLS_X509_ID_FLAG( pk_alg ) ) != 0 )
203 return( 0 );
204
205 return( -1 );
206}
207
208/*
209 * Check key against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200210 * Return 0 if pk is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200211 */
212static int x509_profile_check_key( const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200213 const mbedtls_pk_context *pk )
214{
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200215 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type( pk );
Manuel Pégourié-Gonnard19773ff2017-10-24 10:51:26 +0200216
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200217#if defined(MBEDTLS_RSA_C)
218 if( pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS )
219 {
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200220 if( mbedtls_pk_get_bitlen( pk ) >= profile->rsa_min_bitlen )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200221 return( 0 );
222
223 return( -1 );
224 }
225#endif
226
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200227#if defined(MBEDTLS_ECP_C)
228 if( pk_alg == MBEDTLS_PK_ECDSA ||
229 pk_alg == MBEDTLS_PK_ECKEY ||
230 pk_alg == MBEDTLS_PK_ECKEY_DH )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200231 {
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200232 const mbedtls_ecp_group_id gid = mbedtls_pk_ec( *pk )->grp.id;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200233
Philippe Antoineb5b25432018-05-11 11:06:29 +0200234 if( gid == MBEDTLS_ECP_DP_NONE )
235 return( -1 );
236
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200237 if( ( profile->allowed_curves & MBEDTLS_X509_ID_FLAG( gid ) ) != 0 )
238 return( 0 );
239
240 return( -1 );
241 }
242#endif
243
244 return( -1 );
245}
246
247/*
Hanno Becker1f8527f2018-11-02 09:19:16 +0000248 * Like memcmp, but case-insensitive and always returns -1 if different
249 */
250static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
251{
252 size_t i;
253 unsigned char diff;
254 const unsigned char *n1 = s1, *n2 = s2;
255
256 for( i = 0; i < len; i++ )
257 {
258 diff = n1[i] ^ n2[i];
259
260 if( diff == 0 )
261 continue;
262
263 if( diff == 32 &&
264 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
265 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
266 {
267 continue;
268 }
269
270 return( -1 );
271 }
272
273 return( 0 );
274}
275
276/*
277 * Return 0 if name matches wildcard, -1 otherwise
278 */
279static int x509_check_wildcard( const char *cn, const mbedtls_x509_buf *name )
280{
281 size_t i;
282 size_t cn_idx = 0, cn_len = strlen( cn );
283
284 /* We can't have a match if there is no wildcard to match */
285 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
286 return( -1 );
287
288 for( i = 0; i < cn_len; ++i )
289 {
290 if( cn[i] == '.' )
291 {
292 cn_idx = i;
293 break;
294 }
295 }
296
297 if( cn_idx == 0 )
298 return( -1 );
299
300 if( cn_len - cn_idx == name->len - 1 &&
301 x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
302 {
303 return( 0 );
304 }
305
306 return( -1 );
307}
308
309/*
310 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
311 * variations (but not all).
312 *
313 * Return 0 if equal, -1 otherwise.
314 */
315static int x509_string_cmp( const mbedtls_x509_buf *a, const mbedtls_x509_buf *b )
316{
317 if( a->tag == b->tag &&
318 a->len == b->len &&
319 memcmp( a->p, b->p, b->len ) == 0 )
320 {
321 return( 0 );
322 }
323
324 if( ( a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
325 ( b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
326 a->len == b->len &&
327 x509_memcasecmp( a->p, b->p, b->len ) == 0 )
328 {
329 return( 0 );
330 }
331
332 return( -1 );
333}
334
335/*
336 * Compare two X.509 Names (aka rdnSequence).
337 *
338 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
339 * we sometimes return unequal when the full algorithm would return equal,
340 * but never the other way. (In particular, we don't do Unicode normalisation
341 * or space folding.)
342 *
343 * Return 0 if equal, -1 otherwise.
344 */
345static int x509_name_cmp( const mbedtls_x509_name *a, const mbedtls_x509_name *b )
346{
347 /* Avoid recursion, it might not be optimised by the compiler */
348 while( a != NULL || b != NULL )
349 {
350 if( a == NULL || b == NULL )
351 return( -1 );
352
353 /* type */
354 if( a->oid.tag != b->oid.tag ||
355 a->oid.len != b->oid.len ||
356 memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 )
357 {
358 return( -1 );
359 }
360
361 /* value */
362 if( x509_string_cmp( &a->val, &b->val ) != 0 )
363 return( -1 );
364
365 /* structure of the list of sets */
366 if( a->next_merged != b->next_merged )
367 return( -1 );
368
369 a = a->next;
370 b = b->next;
371 }
372
373 /* a == NULL == b */
374 return( 0 );
375}
376
377/*
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200378 * Reset (init or clear) a verify_chain
379 */
380static void x509_crt_verify_chain_reset(
381 mbedtls_x509_crt_verify_chain *ver_chain )
382{
383 size_t i;
384
385 for( i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++ )
386 {
387 ver_chain->items[i].crt = NULL;
Hanno Beckera9375b32019-01-10 09:19:26 +0000388 ver_chain->items[i].flags = (uint32_t) -1;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200389 }
390
391 ver_chain->len = 0;
Hanno Beckerf53893b2019-03-28 13:45:55 +0000392
393#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
394 ver_chain->trust_ca_cb_result = NULL;
395#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200396}
397
398/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200399 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
400 */
401static int x509_get_version( unsigned char **p,
402 const unsigned char *end,
403 int *ver )
404{
Janos Follath865b3eb2019-12-16 11:46:15 +0000405 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200406 size_t len;
407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
409 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200410 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200412 {
413 *ver = 0;
414 return( 0 );
415 }
416
Chris Jones9f7a6932021-04-14 12:12:09 +0100417 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200418 }
419
420 end = *p + len;
421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100423 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_VERSION, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200424
425 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100426 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_VERSION,
427 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200428
429 return( 0 );
430}
431
432/*
433 * Validity ::= SEQUENCE {
434 * notBefore Time,
435 * notAfter Time }
436 */
437static int x509_get_dates( unsigned char **p,
438 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 mbedtls_x509_time *from,
440 mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200441{
Janos Follath865b3eb2019-12-16 11:46:15 +0000442 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200443 size_t len;
444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
446 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100447 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200448
449 end = *p + len;
450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451 if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200452 return( ret );
453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200455 return( ret );
456
457 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100458 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE,
459 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200460
461 return( 0 );
462}
463
464/*
465 * X.509 v2/v3 unique identifier (not parsed)
466 */
467static int x509_get_uid( unsigned char **p,
468 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469 mbedtls_x509_buf *uid, int n )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200470{
Janos Follath865b3eb2019-12-16 11:46:15 +0000471 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200472
473 if( *p == end )
474 return( 0 );
475
476 uid->tag = **p;
477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 if( ( ret = mbedtls_asn1_get_tag( p, end, &uid->len,
479 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200480 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200482 return( 0 );
483
Chris Jones9f7a6932021-04-14 12:12:09 +0100484 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200485 }
486
487 uid->p = *p;
488 *p += uid->len;
489
490 return( 0 );
491}
492
493static int x509_get_basic_constraints( unsigned char **p,
494 const unsigned char *end,
495 int *ca_istrue,
496 int *max_pathlen )
497{
Janos Follath865b3eb2019-12-16 11:46:15 +0000498 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200499 size_t len;
500
501 /*
502 * BasicConstraints ::= SEQUENCE {
503 * cA BOOLEAN DEFAULT FALSE,
504 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
505 */
506 *ca_istrue = 0; /* DEFAULT FALSE */
507 *max_pathlen = 0; /* endless */
508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
510 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100511 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200512
513 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200514 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516 if( ( ret = mbedtls_asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200517 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
519 ret = mbedtls_asn1_get_int( p, end, ca_istrue );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200520
521 if( ret != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100522 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200523
524 if( *ca_istrue != 0 )
525 *ca_istrue = 1;
526 }
527
528 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200529 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531 if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100532 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200533
534 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100535 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
536 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200537
Andrzej Kurek16050742020-04-14 09:49:52 -0400538 /* Do not accept max_pathlen equal to INT_MAX to avoid a signed integer
539 * overflow, which is an undefined behavior. */
540 if( *max_pathlen == INT_MAX )
Chris Jones9f7a6932021-04-14 12:12:09 +0100541 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
542 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Andrzej Kurek16050742020-04-14 09:49:52 -0400543
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200544 (*max_pathlen)++;
545
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200546 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200547}
548
549static int x509_get_ns_cert_type( unsigned char **p,
550 const unsigned char *end,
551 unsigned char *ns_cert_type)
552{
Janos Follath865b3eb2019-12-16 11:46:15 +0000553 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100557 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200558
559 if( bs.len != 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100560 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
561 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200562
563 /* Get actual bitstring */
564 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200565 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200566}
567
568static int x509_get_key_usage( unsigned char **p,
569 const unsigned char *end,
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +0100570 unsigned int *key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200571{
Janos Follath865b3eb2019-12-16 11:46:15 +0000572 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200573 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100577 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200578
579 if( bs.len < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100580 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
581 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200582
583 /* Get actual bitstring */
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200584 *key_usage = 0;
585 for( i = 0; i < bs.len && i < sizeof( unsigned int ); i++ )
586 {
587 *key_usage |= (unsigned int) bs.p[i] << (8*i);
588 }
589
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200590 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200591}
592
593/*
594 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
595 *
596 * KeyPurposeId ::= OBJECT IDENTIFIER
597 */
598static int x509_get_ext_key_usage( unsigned char **p,
599 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200601{
Janos Follath865b3eb2019-12-16 11:46:15 +0000602 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 if( ( ret = mbedtls_asn1_get_sequence_of( p, end, ext_key_usage, MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100605 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200606
607 /* Sequence length must be >= 1 */
608 if( ext_key_usage->buf.p == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100609 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
610 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200611
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200612 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200613}
614
615/*
616 * SubjectAltName ::= GeneralNames
617 *
618 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
619 *
620 * GeneralName ::= CHOICE {
621 * otherName [0] OtherName,
622 * rfc822Name [1] IA5String,
623 * dNSName [2] IA5String,
624 * x400Address [3] ORAddress,
625 * directoryName [4] Name,
626 * ediPartyName [5] EDIPartyName,
627 * uniformResourceIdentifier [6] IA5String,
628 * iPAddress [7] OCTET STRING,
629 * registeredID [8] OBJECT IDENTIFIER }
630 *
631 * OtherName ::= SEQUENCE {
632 * type-id OBJECT IDENTIFIER,
633 * value [0] EXPLICIT ANY DEFINED BY type-id }
634 *
635 * EDIPartyName ::= SEQUENCE {
636 * nameAssigner [0] DirectoryString OPTIONAL,
637 * partyName [1] DirectoryString }
638 *
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300639 * NOTE: we list all types, but only use dNSName and otherName
640 * of type HwModuleName, as defined in RFC 4108, at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200641 */
642static int x509_get_subject_alt_name( unsigned char **p,
643 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 mbedtls_x509_sequence *subject_alt_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200645{
Janos Follath865b3eb2019-12-16 11:46:15 +0000646 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200647 size_t len, tag_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 mbedtls_asn1_buf *buf;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200649 unsigned char tag;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650 mbedtls_asn1_sequence *cur = subject_alt_name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200651
652 /* Get main sequence tag */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
654 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100655 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200656
657 if( *p + len != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100658 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
659 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200660
661 while( *p < end )
662 {
Ron Eldordbbd9662019-05-15 15:46:03 +0300663 mbedtls_x509_subject_alternative_name dummy_san_buf;
664 memset( &dummy_san_buf, 0, sizeof( dummy_san_buf ) );
665
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200666 tag = **p;
667 (*p)++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 if( ( ret = mbedtls_asn1_get_len( p, end, &tag_len ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100669 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200670
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100671 if( ( tag & MBEDTLS_ASN1_TAG_CLASS_MASK ) !=
672 MBEDTLS_ASN1_CONTEXT_SPECIFIC )
673 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100674 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
675 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100676 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200677
Ron Eldordbbd9662019-05-15 15:46:03 +0300678 /*
irwir74aee1c2019-09-21 18:21:48 +0300679 * Check that the SAN is structured correctly.
Ron Eldordbbd9662019-05-15 15:46:03 +0300680 */
681 ret = mbedtls_x509_parse_subject_alt_name( &(cur->buf), &dummy_san_buf );
682 /*
683 * In case the extension is malformed, return an error,
684 * and clear the allocated sequences.
685 */
686 if( ret != 0 && ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
687 {
688 mbedtls_x509_sequence *seq_cur = subject_alt_name->next;
689 mbedtls_x509_sequence *seq_prv;
690 while( seq_cur != NULL )
691 {
692 seq_prv = seq_cur;
693 seq_cur = seq_cur->next;
694 mbedtls_platform_zeroize( seq_prv,
695 sizeof( mbedtls_x509_sequence ) );
696 mbedtls_free( seq_prv );
697 }
Ron Eldor5aebeeb2019-05-22 16:41:21 +0300698 subject_alt_name->next = NULL;
Ron Eldordbbd9662019-05-15 15:46:03 +0300699 return( ret );
700 }
701
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200702 /* Allocate and assign next pointer */
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200703 if( cur->buf.p != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200704 {
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100705 if( cur->next != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100707
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200708 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200709
710 if( cur->next == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100711 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
712 MBEDTLS_ERR_ASN1_ALLOC_FAILED ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200713
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200714 cur = cur->next;
715 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200716
717 buf = &(cur->buf);
718 buf->tag = tag;
719 buf->p = *p;
720 buf->len = tag_len;
721 *p += buf->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200722 }
723
724 /* Set final sequence entry's next pointer to NULL */
725 cur->next = NULL;
726
727 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100728 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
729 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200730
731 return( 0 );
732}
733
734/*
Ron Eldor74d9acc2019-03-21 14:00:03 +0200735 * id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 }
736 *
737 * anyPolicy OBJECT IDENTIFIER ::= { id-ce-certificatePolicies 0 }
738 *
739 * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
740 *
741 * PolicyInformation ::= SEQUENCE {
742 * policyIdentifier CertPolicyId,
743 * policyQualifiers SEQUENCE SIZE (1..MAX) OF
744 * PolicyQualifierInfo OPTIONAL }
745 *
746 * CertPolicyId ::= OBJECT IDENTIFIER
747 *
748 * PolicyQualifierInfo ::= SEQUENCE {
749 * policyQualifierId PolicyQualifierId,
750 * qualifier ANY DEFINED BY policyQualifierId }
751 *
752 * -- policyQualifierIds for Internet policy qualifiers
753 *
754 * id-qt OBJECT IDENTIFIER ::= { id-pkix 2 }
755 * id-qt-cps OBJECT IDENTIFIER ::= { id-qt 1 }
756 * id-qt-unotice OBJECT IDENTIFIER ::= { id-qt 2 }
757 *
758 * PolicyQualifierId ::= OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice )
759 *
760 * Qualifier ::= CHOICE {
761 * cPSuri CPSuri,
762 * userNotice UserNotice }
763 *
764 * CPSuri ::= IA5String
765 *
766 * UserNotice ::= SEQUENCE {
767 * noticeRef NoticeReference OPTIONAL,
768 * explicitText DisplayText OPTIONAL }
769 *
770 * NoticeReference ::= SEQUENCE {
771 * organization DisplayText,
772 * noticeNumbers SEQUENCE OF INTEGER }
773 *
774 * DisplayText ::= CHOICE {
775 * ia5String IA5String (SIZE (1..200)),
776 * visibleString VisibleString (SIZE (1..200)),
777 * bmpString BMPString (SIZE (1..200)),
778 * utf8String UTF8String (SIZE (1..200)) }
779 *
780 * NOTE: we only parse and use anyPolicy without qualifiers at this point
781 * as defined in RFC 5280.
782 */
783static int x509_get_certificate_policies( unsigned char **p,
784 const unsigned char *end,
785 mbedtls_x509_sequence *certificate_policies )
786{
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300787 int ret, parse_ret = 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200788 size_t len;
789 mbedtls_asn1_buf *buf;
790 mbedtls_asn1_sequence *cur = certificate_policies;
791
792 /* Get main sequence tag */
793 ret = mbedtls_asn1_get_tag( p, end, &len,
794 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
795 if( ret != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100796 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200797
798 if( *p + len != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100799 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
800 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200801
802 /*
803 * Cannot be an empty sequence.
804 */
805 if( len == 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100806 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
807 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200808
809 while( *p < end )
810 {
811 mbedtls_x509_buf policy_oid;
812 const unsigned char *policy_end;
813
814 /*
815 * Get the policy sequence
816 */
817 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
818 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100819 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200820
821 policy_end = *p + len;
822
Ron Eldor08063792019-05-13 16:38:39 +0300823 if( ( ret = mbedtls_asn1_get_tag( p, policy_end, &len,
Ron Eldor74d9acc2019-03-21 14:00:03 +0200824 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100825 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200826
827 policy_oid.tag = MBEDTLS_ASN1_OID;
828 policy_oid.len = len;
829 policy_oid.p = *p;
830
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300831 /*
832 * Only AnyPolicy is currently supported when enforcing policy.
833 */
834 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_POLICY, &policy_oid ) != 0 )
835 {
836 /*
837 * Set the parsing return code but continue parsing, in case this
TRodziewicz3ecb92e2021-05-11 18:22:05 +0200838 * extension is critical.
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300839 */
840 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
841 }
842
Ron Eldor74d9acc2019-03-21 14:00:03 +0200843 /* Allocate and assign next pointer */
844 if( cur->buf.p != NULL )
845 {
846 if( cur->next != NULL )
847 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
848
849 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
850
851 if( cur->next == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100852 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
853 MBEDTLS_ERR_ASN1_ALLOC_FAILED ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200854
855 cur = cur->next;
856 }
857
858 buf = &( cur->buf );
859 buf->tag = policy_oid.tag;
860 buf->p = policy_oid.p;
861 buf->len = policy_oid.len;
Ron Eldor08063792019-05-13 16:38:39 +0300862
863 *p += len;
864
865 /*
866 * If there is an optional qualifier, then *p < policy_end
867 * Check the Qualifier len to verify it doesn't exceed policy_end.
868 */
869 if( *p < policy_end )
870 {
871 if( ( ret = mbedtls_asn1_get_tag( p, policy_end, &len,
872 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100873 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor08063792019-05-13 16:38:39 +0300874 /*
875 * Skip the optional policy qualifiers.
876 */
877 *p += len;
878 }
879
880 if( *p != policy_end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100881 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
882 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200883 }
884
885 /* Set final sequence entry's next pointer to NULL */
886 cur->next = NULL;
887
888 if( *p != 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
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300892 return( parse_ret );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200893}
894
895/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200896 * X.509 v3 extensions
897 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200898 */
899static int x509_get_crt_ext( unsigned char **p,
900 const unsigned char *end,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200901 mbedtls_x509_crt *crt,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200902 mbedtls_x509_crt_ext_cb_t cb,
903 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200904{
Janos Follath865b3eb2019-12-16 11:46:15 +0000905 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200906 size_t len;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200907 unsigned char *end_ext_data, *start_ext_octet, *end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200908
Hanno Becker12f62fb2019-02-12 17:22:36 +0000909 if( *p == end )
910 return( 0 );
911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912 if( ( ret = mbedtls_x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200913 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200914
Hanno Becker12f62fb2019-02-12 17:22:36 +0000915 end = crt->v3_ext.p + crt->v3_ext.len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200916 while( *p < end )
917 {
918 /*
919 * Extension ::= SEQUENCE {
920 * extnID OBJECT IDENTIFIER,
921 * critical BOOLEAN DEFAULT FALSE,
922 * extnValue OCTET STRING }
923 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200924 mbedtls_x509_buf extn_oid = {0, 0, NULL};
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200925 int is_critical = 0; /* DEFAULT FALSE */
926 int ext_type = 0;
927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200928 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
929 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100930 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200931
932 end_ext_data = *p + len;
933
934 /* Get extension ID */
k-stachowiakdcae78a2018-06-28 16:32:54 +0200935 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &extn_oid.len,
k-stachowiak463928a2018-07-24 12:50:59 +0200936 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100937 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200938
k-stachowiak463928a2018-07-24 12:50:59 +0200939 extn_oid.tag = MBEDTLS_ASN1_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200940 extn_oid.p = *p;
941 *p += extn_oid.len;
942
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200943 /* Get optional critical */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944 if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
945 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
Chris Jones9f7a6932021-04-14 12:12:09 +0100946 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200947
948 /* Data should be octet string type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
950 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100951 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200952
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200953 start_ext_octet = *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200954 end_ext_octet = *p + len;
955
956 if( end_ext_octet != end_ext_data )
Chris Jones9f7a6932021-04-14 12:12:09 +0100957 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
958 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200959
960 /*
961 * Detect supported extensions
962 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200963 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200964
965 if( ret != 0 )
966 {
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200967 /* Give the callback (if any) a chance to handle the extension */
Nicola Di Lieto2c3a9172020-05-28 17:20:42 +0200968 if( cb != NULL )
969 {
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200970 ret = cb( p_ctx, crt, &extn_oid, is_critical, *p, end_ext_octet );
Nicola Di Lieto565b52b2020-05-29 22:46:56 +0200971 if( ret != 0 && is_critical )
972 return( ret );
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200973 *p = end_ext_octet;
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200974 continue;
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200975 }
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200976
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200977 /* No parser found, skip extension */
978 *p = end_ext_octet;
979
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200980 if( is_critical )
981 {
982 /* Data is marked as critical: fail */
Chris Jones9f7a6932021-04-14 12:12:09 +0100983 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
984 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200985 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200986 continue;
987 }
988
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100989 /* Forbid repeated extensions */
990 if( ( crt->ext_types & ext_type ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200991 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100992
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200993 crt->ext_types |= ext_type;
994
995 switch( ext_type )
996 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100997 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200998 /* Parse basic constraints */
999 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
1000 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001001 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001002 break;
1003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001005 /* Parse key usage */
1006 if( ( ret = x509_get_key_usage( p, end_ext_octet,
1007 &crt->key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001008 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001009 break;
1010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001012 /* Parse extended key usage */
1013 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
1014 &crt->ext_key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001015 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001016 break;
1017
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001018 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001019 /* Parse subject alt name */
1020 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
1021 &crt->subject_alt_names ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001022 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001023 break;
1024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001026 /* Parse netscape certificate type */
1027 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
1028 &crt->ns_cert_type ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001029 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001030 break;
1031
Ron Eldor74d9acc2019-03-21 14:00:03 +02001032 case MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES:
1033 /* Parse certificate policies type */
1034 if( ( ret = x509_get_certificate_policies( p, end_ext_octet,
1035 &crt->certificate_policies ) ) != 0 )
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001036 {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +02001037 /* Give the callback (if any) a chance to handle the extension
1038 * if it contains unsupported policies */
1039 if( ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE && cb != NULL &&
1040 cb( p_ctx, crt, &extn_oid, is_critical,
1041 start_ext_octet, end_ext_octet ) == 0 )
1042 break;
1043
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001044 if( is_critical )
1045 return( ret );
1046 else
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001047 /*
Ron Eldora2913912019-05-16 16:17:38 +03001048 * If MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE is returned, then we
1049 * cannot interpret or enforce the policy. However, it is up to
1050 * the user to choose how to enforce the policies,
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001051 * unless the extension is critical.
1052 */
1053 if( ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1054 return( ret );
1055 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001056 break;
1057
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001058 default:
Ron Eldordf48efa2019-04-08 13:28:24 +03001059 /*
1060 * If this is a non-critical extension, which the oid layer
1061 * supports, but there isn't an x509 parser for it,
1062 * skip the extension.
1063 */
Ron Eldordf48efa2019-04-08 13:28:24 +03001064 if( is_critical )
1065 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1066 else
Ron Eldordf48efa2019-04-08 13:28:24 +03001067 *p = end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001068 }
1069 }
1070
1071 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +01001072 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1073 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001074
1075 return( 0 );
1076}
1077
1078/*
1079 * Parse and fill a single X.509 certificate in DER format
1080 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001081static int x509_crt_parse_der_core( mbedtls_x509_crt *crt,
1082 const unsigned char *buf,
1083 size_t buflen,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001084 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001085 mbedtls_x509_crt_ext_cb_t cb,
1086 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001087{
Janos Follath865b3eb2019-12-16 11:46:15 +00001088 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001089 size_t len;
1090 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001091 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093 memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) );
1094 memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) );
1095 memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001096
1097 /*
1098 * Check for valid input
1099 */
1100 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001102
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001103 /* Use the original buffer until we figure out actual length. */
Janos Follathcc0e49d2016-02-17 14:34:12 +00001104 p = (unsigned char*) buf;
1105 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001106 end = p + len;
1107
1108 /*
1109 * Certificate ::= SEQUENCE {
1110 * tbsCertificate TBSCertificate,
1111 * signatureAlgorithm AlgorithmIdentifier,
1112 * signatureValue BIT STRING }
1113 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1115 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001116 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001117 mbedtls_x509_crt_free( crt );
1118 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001119 }
1120
Janos Follathcc0e49d2016-02-17 14:34:12 +00001121 end = crt_end = p + len;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001122 crt->raw.len = crt_end - buf;
1123 if( make_copy != 0 )
1124 {
1125 /* Create and populate a new buffer for the raw field. */
1126 crt->raw.p = p = mbedtls_calloc( 1, crt->raw.len );
1127 if( crt->raw.p == NULL )
1128 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
1129
1130 memcpy( crt->raw.p, buf, crt->raw.len );
1131 crt->own_buffer = 1;
1132
1133 p += crt->raw.len - len;
1134 end = crt_end = p + len;
1135 }
1136 else
1137 {
1138 crt->raw.p = (unsigned char*) buf;
1139 crt->own_buffer = 0;
1140 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001141
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001142 /*
1143 * TBSCertificate ::= SEQUENCE {
1144 */
1145 crt->tbs.p = p;
1146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001147 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1148 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001149 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001151 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001152 }
1153
1154 end = p + len;
1155 crt->tbs.len = end - crt->tbs.p;
1156
1157 /*
1158 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1159 *
1160 * CertificateSerialNumber ::= INTEGER
1161 *
1162 * signature AlgorithmIdentifier
1163 */
1164 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001165 ( ret = mbedtls_x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1166 ( ret = mbedtls_x509_get_alg( &p, end, &crt->sig_oid,
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001167 &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001168 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001169 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001170 return( ret );
1171 }
1172
Andres AG7ca4a032017-03-09 16:16:11 +00001173 if( crt->version < 0 || crt->version > 2 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001174 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175 mbedtls_x509_crt_free( crt );
1176 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001177 }
1178
Andres AG7ca4a032017-03-09 16:16:11 +00001179 crt->version++;
1180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001181 if( ( ret = mbedtls_x509_get_sig_alg( &crt->sig_oid, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02001182 &crt->sig_md, &crt->sig_pk,
1183 &crt->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001184 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001185 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001186 return( ret );
1187 }
1188
1189 /*
1190 * issuer Name
1191 */
1192 crt->issuer_raw.p = p;
1193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001194 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1195 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001196 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001197 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001198 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001199 }
1200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201 if( ( ret = mbedtls_x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001202 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001204 return( ret );
1205 }
1206
1207 crt->issuer_raw.len = p - crt->issuer_raw.p;
1208
1209 /*
1210 * Validity ::= SEQUENCE {
1211 * notBefore Time,
1212 * notAfter Time }
1213 *
1214 */
1215 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1216 &crt->valid_to ) ) != 0 )
1217 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001218 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001219 return( ret );
1220 }
1221
1222 /*
1223 * subject Name
1224 */
1225 crt->subject_raw.p = p;
1226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001227 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1228 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001229 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001230 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001231 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001232 }
1233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001234 if( len && ( ret = mbedtls_x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001235 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001236 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001237 return( ret );
1238 }
1239
1240 crt->subject_raw.len = p - crt->subject_raw.p;
1241
1242 /*
1243 * SubjectPublicKeyInfo
1244 */
Hanno Becker494dd7a2019-02-06 16:13:41 +00001245 crt->pk_raw.p = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246 if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001247 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001248 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001249 return( ret );
1250 }
Hanno Becker494dd7a2019-02-06 16:13:41 +00001251 crt->pk_raw.len = p - crt->pk_raw.p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001252
1253 /*
1254 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1255 * -- If present, version shall be v2 or v3
1256 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1257 * -- If present, version shall be v2 or v3
1258 * extensions [3] EXPLICIT Extensions OPTIONAL
1259 * -- If present, version shall be v3
1260 */
1261 if( crt->version == 2 || crt->version == 3 )
1262 {
1263 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1264 if( ret != 0 )
1265 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001266 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001267 return( ret );
1268 }
1269 }
1270
1271 if( crt->version == 2 || crt->version == 3 )
1272 {
1273 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1274 if( ret != 0 )
1275 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001276 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001277 return( ret );
1278 }
1279 }
1280
1281 if( crt->version == 3 )
Manuel Pégourié-Gonnarda252af72015-03-27 16:15:55 +01001282 {
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001283 ret = x509_get_crt_ext( &p, end, crt, cb, p_ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001284 if( ret != 0 )
1285 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001286 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001287 return( ret );
1288 }
1289 }
1290
1291 if( p != end )
1292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001294 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT,
1295 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001296 }
1297
1298 end = crt_end;
1299
1300 /*
1301 * }
1302 * -- end of TBSCertificate
1303 *
1304 * signatureAlgorithm AlgorithmIdentifier,
1305 * signatureValue BIT STRING
1306 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307 if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001308 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001309 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001310 return( ret );
1311 }
1312
Manuel Pégourié-Gonnard1022fed2015-03-27 16:30:47 +01001313 if( crt->sig_oid.len != sig_oid2.len ||
1314 memcmp( crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len ) != 0 ||
Paul Elliottca17ebf2020-11-24 17:30:18 +00001315 sig_params1.tag != sig_params2.tag ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001316 sig_params1.len != sig_params2.len ||
Manuel Pégourié-Gonnard159c5242015-04-30 11:15:22 +02001317 ( sig_params1.len != 0 &&
1318 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001319 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001320 mbedtls_x509_crt_free( crt );
1321 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001322 }
1323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324 if( ( ret = mbedtls_x509_get_sig( &p, end, &crt->sig ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001325 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001327 return( ret );
1328 }
1329
1330 if( p != end )
1331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001333 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT,
1334 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001335 }
1336
1337 return( 0 );
1338}
1339
1340/*
1341 * Parse one X.509 certificate in DER format from a buffer and add them to a
1342 * chained list
1343 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001344static int mbedtls_x509_crt_parse_der_internal( mbedtls_x509_crt *chain,
1345 const unsigned char *buf,
1346 size_t buflen,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001347 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001348 mbedtls_x509_crt_ext_cb_t cb,
1349 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001350{
Janos Follath865b3eb2019-12-16 11:46:15 +00001351 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001352 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001353
1354 /*
1355 * Check for valid input
1356 */
1357 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001358 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001359
1360 while( crt->version != 0 && crt->next != NULL )
1361 {
1362 prev = crt;
1363 crt = crt->next;
1364 }
1365
1366 /*
1367 * Add new certificate on the end of the chain if needed.
1368 */
Paul Bakker66d5d072014-06-17 16:39:18 +02001369 if( crt->version != 0 && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001370 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001371 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001372
1373 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001374 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001375
1376 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001378 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001379 }
1380
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001381 ret = x509_crt_parse_der_core( crt, buf, buflen, make_copy, cb, p_ctx );
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001382 if( ret != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001383 {
1384 if( prev )
1385 prev->next = NULL;
1386
1387 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001389
1390 return( ret );
1391 }
1392
1393 return( 0 );
1394}
1395
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001396int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
1397 const unsigned char *buf,
1398 size_t buflen )
1399{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001400 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 0, NULL, NULL ) );
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001401}
1402
Nicola Di Lietofde98f72020-05-28 08:34:33 +02001403int mbedtls_x509_crt_parse_der_with_ext_cb( mbedtls_x509_crt *chain,
1404 const unsigned char *buf,
1405 size_t buflen,
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +02001406 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001407 mbedtls_x509_crt_ext_cb_t cb,
1408 void *p_ctx )
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001409{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001410 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, make_copy, cb, p_ctx ) );
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001411}
1412
1413int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
1414 const unsigned char *buf,
1415 size_t buflen )
1416{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001417 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 1, NULL, NULL ) );
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001418}
1419
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001420/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001421 * Parse one or more PEM certificates from a buffer and add them to the chained
1422 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001423 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001424int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain,
1425 const unsigned char *buf,
1426 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001427{
Janos Follath98e28a72016-05-31 14:03:54 +01001428#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001429 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001431#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001432
1433 /*
1434 * Check for valid input
1435 */
1436 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001438
1439 /*
1440 * Determine buffer content. Buffer contains either one DER certificate or
1441 * one or more PEM certificates.
1442 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001443#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001444 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001445 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1446 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001447 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001448 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450 if( buf_format == MBEDTLS_X509_FORMAT_DER )
1451 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Janos Follath98e28a72016-05-31 14:03:54 +01001452#else
1453 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
1454#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456#if defined(MBEDTLS_PEM_PARSE_C)
1457 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001458 {
Janos Follath865b3eb2019-12-16 11:46:15 +00001459 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001460 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001461
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001462 /* 1 rather than 0 since the terminating NULL byte is counted in */
1463 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001464 {
1465 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001467
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001468 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001469 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001470 "-----BEGIN CERTIFICATE-----",
1471 "-----END CERTIFICATE-----",
1472 buf, NULL, 0, &use_len );
1473
1474 if( ret == 0 )
1475 {
1476 /*
1477 * Was PEM encoded
1478 */
1479 buflen -= use_len;
1480 buf += use_len;
1481 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001482 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001483 {
1484 return( ret );
1485 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001486 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001487 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001488 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001489
1490 /*
1491 * PEM header and footer were found
1492 */
1493 buflen -= use_len;
1494 buf += use_len;
1495
1496 if( first_error == 0 )
1497 first_error = ret;
1498
Paul Bakker5a5fa922014-09-26 14:53:04 +02001499 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001500 continue;
1501 }
1502 else
1503 break;
1504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001505 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001507 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001508
1509 if( ret != 0 )
1510 {
1511 /*
1512 * Quit parsing on a memory error
1513 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001514 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001515 return( ret );
1516
1517 if( first_error == 0 )
1518 first_error = ret;
1519
1520 total_failed++;
1521 continue;
1522 }
1523
1524 success = 1;
1525 }
1526 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001527
1528 if( success )
1529 return( total_failed );
1530 else if( first_error )
1531 return( first_error );
1532 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001533 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Janos Follath98e28a72016-05-31 14:03:54 +01001534#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001535}
1536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001537#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001538/*
1539 * Load one or more certificates and add them to the chained list
1540 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001541int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001542{
Janos Follath865b3eb2019-12-16 11:46:15 +00001543 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001544 size_t n;
1545 unsigned char *buf;
1546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001548 return( ret );
1549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001550 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001551
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001552 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001554
1555 return( ret );
1556}
1557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001558int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001559{
1560 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001561#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001562 int w_ret;
1563 WCHAR szDir[MAX_PATH];
1564 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001565 char *p;
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001566 size_t len = strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001567
Paul Bakker9af723c2014-05-01 13:03:14 +02001568 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001569 HANDLE hFind;
1570
1571 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001572 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001573
Paul Bakker9af723c2014-05-01 13:03:14 +02001574 memset( szDir, 0, sizeof(szDir) );
1575 memset( filename, 0, MAX_PATH );
1576 memcpy( filename, path, len );
1577 filename[len++] = '\\';
1578 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001579 filename[len++] = '*';
1580
Simon B3c6b18d2016-11-03 01:11:37 +00001581 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001582 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001583 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001585
1586 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001587 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001588 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001589
1590 len = MAX_PATH - len;
1591 do
1592 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001593 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001594
1595 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1596 continue;
1597
Paul Bakker9af723c2014-05-01 13:03:14 +02001598 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001599 lstrlenW( file_data.cFileName ),
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001600 p, (int) len - 1,
Paul Bakker9af723c2014-05-01 13:03:14 +02001601 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001602 if( w_ret == 0 )
Ron Eldor36d90422017-01-09 15:09:16 +02001603 {
1604 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1605 goto cleanup;
1606 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001608 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001609 if( w_ret < 0 )
1610 ret++;
1611 else
1612 ret += w_ret;
1613 }
1614 while( FindNextFileW( hFind, &file_data ) != 0 );
1615
Paul Bakker66d5d072014-06-17 16:39:18 +02001616 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001617 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001618
Ron Eldor36d90422017-01-09 15:09:16 +02001619cleanup:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001620 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001621#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001622 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001623 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001624 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001625 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001626 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001627 DIR *dir = opendir( path );
1628
Paul Bakker66d5d072014-06-17 16:39:18 +02001629 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001630 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001631
Ron Eldor63140682017-01-09 19:27:59 +02001632#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001633 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001634 {
1635 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001636 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001637 }
Ron Eldor63140682017-01-09 19:27:59 +02001638#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001639
Paul Elliottfb91a482021-03-05 14:17:51 +00001640 memset( &sb, 0, sizeof( sb ) );
1641
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001642 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001643 {
Andres AGf9113192016-09-02 14:06:04 +01001644 snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name,
1645 "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001646
Andres AGf9113192016-09-02 14:06:04 +01001647 if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001648 {
Andres AGf9113192016-09-02 14:06:04 +01001649 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1650 goto cleanup;
1651 }
1652 else if( stat( entry_name, &sb ) == -1 )
1653 {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001654 if( errno == ENOENT )
Eduardo Silvae1bfffc2019-04-25 10:43:26 -06001655 {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001656 /* Broken symbolic link - ignore this entry.
1657 stat(2) will return this error for either (a) a dangling
1658 symlink or (b) a missing file.
1659 Given that we have just obtained the filename from readdir,
1660 assume that it does exist and therefore treat this as a
1661 dangling symlink. */
1662 continue;
1663 }
1664 else
1665 {
1666 /* Some other file error; report the error. */
Eduardo Silvae1bfffc2019-04-25 10:43:26 -06001667 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1668 goto cleanup;
1669 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001670 }
1671
1672 if( !S_ISREG( sb.st_mode ) )
1673 continue;
1674
1675 // Ignore parse errors
1676 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001677 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001678 if( t_ret < 0 )
1679 ret++;
1680 else
1681 ret += t_ret;
1682 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001683
1684cleanup:
Andres AGf9113192016-09-02 14:06:04 +01001685 closedir( dir );
1686
Ron Eldor63140682017-01-09 19:27:59 +02001687#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001688 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001689 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Ron Eldor63140682017-01-09 19:27:59 +02001690#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001691
Paul Bakkerbe089b02013-10-14 15:51:50 +02001692#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001693
1694 return( ret );
1695}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001696#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001697
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001698/*
1699 * OtherName ::= SEQUENCE {
1700 * type-id OBJECT IDENTIFIER,
1701 * value [0] EXPLICIT ANY DEFINED BY type-id }
1702 *
1703 * HardwareModuleName ::= SEQUENCE {
1704 * hwType OBJECT IDENTIFIER,
1705 * hwSerialNum OCTET STRING }
1706 *
1707 * NOTE: we currently only parse and use otherName of type HwModuleName,
1708 * as defined in RFC 4108.
1709 */
1710static int x509_get_other_name( const mbedtls_x509_buf *subject_alt_name,
1711 mbedtls_x509_san_other_name *other_name )
1712{
Janos Follathab23cd12019-05-09 13:53:57 +01001713 int ret = 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001714 size_t len;
1715 unsigned char *p = subject_alt_name->p;
1716 const unsigned char *end = p + subject_alt_name->len;
1717 mbedtls_x509_buf cur_oid;
1718
1719 if( ( subject_alt_name->tag &
1720 ( MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK ) ) !=
1721 ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ) )
1722 {
1723 /*
1724 * The given subject alternative name is not of type "othername".
1725 */
1726 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
1727 }
1728
1729 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1730 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001731 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001732
1733 cur_oid.tag = MBEDTLS_ASN1_OID;
1734 cur_oid.p = p;
1735 cur_oid.len = len;
1736
1737 /*
1738 * Only HwModuleName is currently supported.
1739 */
1740 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid ) != 0 )
1741 {
1742 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1743 }
1744
Ron Eldor890819a2019-05-13 19:03:04 +03001745 if( p + len >= end )
1746 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001747 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001748 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1749 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001750 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001751 p += len;
1752 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1753 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001754 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001755
1756 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1757 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001758 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001759
1760 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001761 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001762
1763 other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID;
1764 other_name->value.hardware_module_name.oid.p = p;
1765 other_name->value.hardware_module_name.oid.len = len;
1766
Ron Eldor890819a2019-05-13 19:03:04 +03001767 if( p + len >= end )
1768 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001769 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001770 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1771 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001772 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001773 p += len;
1774 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1775 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001776 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001777
1778 other_name->value.hardware_module_name.val.tag = MBEDTLS_ASN1_OCTET_STRING;
1779 other_name->value.hardware_module_name.val.p = p;
1780 other_name->value.hardware_module_name.val.len = len;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001781 p += len;
1782 if( p != end )
1783 {
Ron Eldor890819a2019-05-13 19:03:04 +03001784 mbedtls_platform_zeroize( other_name,
Sébastien Duquette661d7252019-06-23 17:45:26 -04001785 sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001786 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1787 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001788 }
1789 return( 0 );
1790}
1791
Peter Kolbus9a969b62018-12-11 13:55:56 -06001792int mbedtls_x509_parse_subject_alt_name( const mbedtls_x509_buf *san_buf,
1793 mbedtls_x509_subject_alternative_name *san )
1794{
1795 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1796 switch( san_buf->tag &
1797 ( MBEDTLS_ASN1_TAG_CLASS_MASK |
1798 MBEDTLS_ASN1_TAG_VALUE_MASK ) )
1799 {
1800 /*
1801 * otherName
1802 */
1803 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ):
1804 {
1805 mbedtls_x509_san_other_name other_name;
1806
1807 ret = x509_get_other_name( san_buf, &other_name );
1808 if( ret != 0 )
1809 return( ret );
1810
1811 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1812 san->type = MBEDTLS_X509_SAN_OTHER_NAME;
1813 memcpy( &san->san.other_name,
1814 &other_name, sizeof( other_name ) );
1815
1816 }
1817 break;
1818
1819 /*
1820 * dNSName
1821 */
1822 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME ):
1823 {
1824 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1825 san->type = MBEDTLS_X509_SAN_DNS_NAME;
1826
1827 memcpy( &san->san.unstructured_name,
1828 san_buf, sizeof( *san_buf ) );
1829
1830 }
1831 break;
1832
1833 /*
1834 * Type not supported
1835 */
1836 default:
1837 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1838 }
1839 return( 0 );
1840}
1841
1842#if !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001843static int x509_info_subject_alt_name( char **buf, size_t *size,
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001844 const mbedtls_x509_sequence
1845 *subject_alt_name,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001846 const char *prefix )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001847{
Janos Follath865b3eb2019-12-16 11:46:15 +00001848 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001849 size_t n = *size;
1850 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001851 const mbedtls_x509_sequence *cur = subject_alt_name;
Ron Eldor890819a2019-05-13 19:03:04 +03001852 mbedtls_x509_subject_alternative_name san;
1853 int parse_ret;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001854
1855 while( cur != NULL )
1856 {
Ron Eldor890819a2019-05-13 19:03:04 +03001857 memset( &san, 0, sizeof( san ) );
1858 parse_ret = mbedtls_x509_parse_subject_alt_name( &cur->buf, &san );
1859 if( parse_ret != 0 )
1860 {
1861 if( parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1862 {
1863 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
1864 MBEDTLS_X509_SAFE_SNPRINTF;
1865 }
1866 else
1867 {
1868 ret = mbedtls_snprintf( p, n, "\n%s <malformed>", prefix );
1869 MBEDTLS_X509_SAFE_SNPRINTF;
1870 }
1871 cur = cur->next;
1872 continue;
1873 }
1874
1875 switch( san.type )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001876 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001877 /*
1878 * otherName
1879 */
Ron Eldora2913912019-05-16 16:17:38 +03001880 case MBEDTLS_X509_SAN_OTHER_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001881 {
1882 mbedtls_x509_san_other_name *other_name = &san.san.other_name;
1883
1884 ret = mbedtls_snprintf( p, n, "\n%s otherName :", prefix );
1885 MBEDTLS_X509_SAFE_SNPRINTF;
1886
1887 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME,
1888 &other_name->value.hardware_module_name.oid ) != 0 )
Janos Follath22f605f2019-05-10 10:37:17 +01001889 {
Ron Eldor890819a2019-05-13 19:03:04 +03001890 ret = mbedtls_snprintf( p, n, "\n%s hardware module name :", prefix );
1891 MBEDTLS_X509_SAFE_SNPRINTF;
1892 ret = mbedtls_snprintf( p, n, "\n%s hardware type : ", prefix );
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001893 MBEDTLS_X509_SAFE_SNPRINTF;
1894
Ron Eldor890819a2019-05-13 19:03:04 +03001895 ret = mbedtls_oid_get_numeric_string( p, n, &other_name->value.hardware_module_name.oid );
1896 MBEDTLS_X509_SAFE_SNPRINTF;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001897
Ron Eldor890819a2019-05-13 19:03:04 +03001898 ret = mbedtls_snprintf( p, n, "\n%s hardware serial number : ", prefix );
1899 MBEDTLS_X509_SAFE_SNPRINTF;
1900
1901 if( other_name->value.hardware_module_name.val.len >= n )
1902 {
1903 *p = '\0';
1904 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Janos Follath22f605f2019-05-10 10:37:17 +01001905 }
1906
Ron Eldora2913912019-05-16 16:17:38 +03001907 memcpy( p, other_name->value.hardware_module_name.val.p,
1908 other_name->value.hardware_module_name.val.len );
1909 p += other_name->value.hardware_module_name.val.len;
1910
Ron Eldor890819a2019-05-13 19:03:04 +03001911 n -= other_name->value.hardware_module_name.val.len;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001912
Ron Eldor890819a2019-05-13 19:03:04 +03001913 }/* MBEDTLS_OID_ON_HW_MODULE_NAME */
1914 }
1915 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001916
1917 /*
1918 * dNSName
1919 */
Ron Eldora2913912019-05-16 16:17:38 +03001920 case MBEDTLS_X509_SAN_DNS_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001921 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001922 ret = mbedtls_snprintf( p, n, "\n%s dNSName : ", prefix );
1923 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldor890819a2019-05-13 19:03:04 +03001924 if( san.san.unstructured_name.len >= n )
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001925 {
1926 *p = '\0';
1927 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
1928 }
Ron Eldora2913912019-05-16 16:17:38 +03001929
1930 memcpy( p, san.san.unstructured_name.p, san.san.unstructured_name.len );
1931 p += san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001932 n -= san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001933 }
1934 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001935
1936 /*
Ron Eldor890819a2019-05-13 19:03:04 +03001937 * Type not supported, skip item.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001938 */
1939 default:
Janos Follath22f605f2019-05-10 10:37:17 +01001940 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
1941 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001942 break;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001943 }
1944
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001945 cur = cur->next;
1946 }
1947
1948 *p = '\0';
1949
1950 *size = n;
1951 *buf = p;
1952
1953 return( 0 );
1954}
1955
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001956#define PRINT_ITEM(i) \
1957 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001958 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001959 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001960 sep = ", "; \
1961 }
1962
1963#define CERT_TYPE(type,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001964 if( ns_cert_type & (type) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001965 PRINT_ITEM( name );
1966
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001967static int x509_info_cert_type( char **buf, size_t *size,
1968 unsigned char ns_cert_type )
1969{
Janos Follath865b3eb2019-12-16 11:46:15 +00001970 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001971 size_t n = *size;
1972 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001973 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001975 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001976 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001977 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
1978 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1979 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001980 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
1981 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1982 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001983
1984 *size = n;
1985 *buf = p;
1986
1987 return( 0 );
1988}
1989
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001990#define KEY_USAGE(code,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001991 if( key_usage & (code) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001992 PRINT_ITEM( name );
1993
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001994static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001995 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001996{
Janos Follath865b3eb2019-12-16 11:46:15 +00001997 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001998 size_t n = *size;
1999 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002000 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002002 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
2003 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002004 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
2005 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
2006 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002007 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
2008 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02002009 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
2010 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002011
2012 *size = n;
2013 *buf = p;
2014
2015 return( 0 );
2016}
2017
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002018static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002019 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002020{
Janos Follath865b3eb2019-12-16 11:46:15 +00002021 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002022 const char *desc;
2023 size_t n = *size;
2024 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002025 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002026 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002027
2028 while( cur != NULL )
2029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002030 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002031 desc = "???";
2032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002033 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002034 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002035
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002036 sep = ", ";
2037
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002038 cur = cur->next;
2039 }
2040
2041 *size = n;
2042 *buf = p;
2043
2044 return( 0 );
2045}
2046
Ron Eldor74d9acc2019-03-21 14:00:03 +02002047static int x509_info_cert_policies( char **buf, size_t *size,
2048 const mbedtls_x509_sequence *certificate_policies )
2049{
Janos Follath865b3eb2019-12-16 11:46:15 +00002050 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor74d9acc2019-03-21 14:00:03 +02002051 const char *desc;
2052 size_t n = *size;
2053 char *p = *buf;
2054 const mbedtls_x509_sequence *cur = certificate_policies;
2055 const char *sep = "";
2056
2057 while( cur != NULL )
2058 {
2059 if( mbedtls_oid_get_certificate_policies( &cur->buf, &desc ) != 0 )
2060 desc = "???";
2061
2062 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
2063 MBEDTLS_X509_SAFE_SNPRINTF;
2064
2065 sep = ", ";
2066
2067 cur = cur->next;
2068 }
2069
2070 *size = n;
2071 *buf = p;
2072
2073 return( 0 );
2074}
2075
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002076/*
2077 * Return an informational string about the certificate.
2078 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002079#define BEFORE_COLON 18
2080#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002081int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
2082 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002083{
Janos Follath865b3eb2019-12-16 11:46:15 +00002084 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002085 size_t n;
2086 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002087 char key_size_str[BEFORE_COLON];
2088
2089 p = buf;
2090 n = size;
2091
Janos Follath98e28a72016-05-31 14:03:54 +01002092 if( NULL == crt )
2093 {
2094 ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" );
2095 MBEDTLS_X509_SAFE_SNPRINTF;
2096
2097 return( (int) ( size - n ) );
2098 }
2099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002100 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002101 prefix, crt->version );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002102 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002103 ret = mbedtls_snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002104 prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002105 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002107 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002108 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002110 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002111 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002112 ret = mbedtls_x509_dn_gets( p, n, &crt->issuer );
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_snprintf( p, n, "\n%ssubject name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002116 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002117 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002118 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002120 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002121 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2122 crt->valid_from.year, crt->valid_from.mon,
2123 crt->valid_from.day, crt->valid_from.hour,
2124 crt->valid_from.min, crt->valid_from.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002125 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002127 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002128 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2129 crt->valid_to.year, crt->valid_to.mon,
2130 crt->valid_to.day, crt->valid_to.hour,
2131 crt->valid_to.min, crt->valid_to.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002132 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002134 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002135 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002137 ret = mbedtls_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02002138 crt->sig_md, crt->sig_opts );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002139 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002140
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002141 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002142 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
2143 mbedtls_pk_get_name( &crt->pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002144 {
2145 return( ret );
2146 }
2147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002148 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +02002149 (int) mbedtls_pk_get_bitlen( &crt->pk ) );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002150 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002151
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002152 /*
2153 * Optional extensions
2154 */
2155
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002156 if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002157 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002158 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002159 crt->ca_istrue ? "true" : "false" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002160 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002161
2162 if( crt->max_pathlen > 0 )
2163 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002164 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002165 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002166 }
2167 }
2168
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002169 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002170 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002171 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name :", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002172 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002173
2174 if( ( ret = x509_info_subject_alt_name( &p, &n,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002175 &crt->subject_alt_names,
Ron Eldor6aeae9e2019-05-20 12:00:36 +03002176 prefix ) ) != 0 )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002177 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002178 }
2179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002180 if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002181 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002182 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002183 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002184
2185 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
2186 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002187 }
2188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002189 if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002190 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002191 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002192 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002193
2194 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
2195 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002196 }
2197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002198 if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002199 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002200 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002201 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002202
2203 if( ( ret = x509_info_ext_key_usage( &p, &n,
2204 &crt->ext_key_usage ) ) != 0 )
2205 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002206 }
2207
Ron Eldor74d9acc2019-03-21 14:00:03 +02002208 if( crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES )
2209 {
2210 ret = mbedtls_snprintf( p, n, "\n%scertificate policies : ", prefix );
2211 MBEDTLS_X509_SAFE_SNPRINTF;
2212
2213 if( ( ret = x509_info_cert_policies( &p, &n,
2214 &crt->certificate_policies ) ) != 0 )
2215 return( ret );
2216 }
2217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002218 ret = mbedtls_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002219 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002220
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002221 return( (int) ( size - n ) );
2222}
2223
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002224struct x509_crt_verify_string {
2225 int code;
2226 const char *string;
2227};
2228
Hanno Becker7ac83f92020-10-09 10:48:22 +01002229#define X509_CRT_ERROR_INFO( err, err_str, info ) { err, info },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002230static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Hanno Becker7ac83f92020-10-09 10:48:22 +01002231 MBEDTLS_X509_CRT_ERROR_INFO_LIST
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002232 { 0, NULL }
2233};
Hanno Becker7ac83f92020-10-09 10:48:22 +01002234#undef X509_CRT_ERROR_INFO
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002235
2236int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002237 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002238{
Janos Follath865b3eb2019-12-16 11:46:15 +00002239 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002240 const struct x509_crt_verify_string *cur;
2241 char *p = buf;
2242 size_t n = size;
2243
2244 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
2245 {
2246 if( ( flags & cur->code ) == 0 )
2247 continue;
2248
2249 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002250 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002251 flags ^= cur->code;
2252 }
2253
2254 if( flags != 0 )
2255 {
2256 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
2257 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002258 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002259 }
2260
2261 return( (int) ( size - n ) );
2262}
Hanno Becker612a2f12020-10-09 09:19:39 +01002263#endif /* MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002264
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002265int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
2266 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002267{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002268 unsigned int usage_must, usage_may;
2269 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
2270 | MBEDTLS_X509_KU_DECIPHER_ONLY;
2271
2272 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
2273 return( 0 );
2274
2275 usage_must = usage & ~may_mask;
2276
2277 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
2278 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
2279
2280 usage_may = usage & may_mask;
2281
2282 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002283 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002284
2285 return( 0 );
2286}
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002288int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002289 const char *usage_oid,
2290 size_t usage_len )
2291{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002292 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002293
2294 /* Extension is not mandatory, absent means no restriction */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002295 if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002296 return( 0 );
2297
2298 /*
2299 * Look for the requested usage (or wildcard ANY) in our list
2300 */
2301 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
2302 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002303 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002304
2305 if( cur_oid->len == usage_len &&
2306 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
2307 {
2308 return( 0 );
2309 }
2310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002311 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002312 return( 0 );
2313 }
2314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002315 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002316}
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002318#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002319/*
2320 * Return 1 if the certificate is revoked, or 0 otherwise.
2321 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002322int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002323{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002324 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002325
2326 while( cur != NULL && cur->serial.len != 0 )
2327 {
2328 if( crt->serial.len == cur->serial.len &&
2329 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
2330 {
Raoul Strackxa4e86142020-06-15 17:03:13 +02002331 return( 1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002332 }
2333
2334 cur = cur->next;
2335 }
2336
2337 return( 0 );
2338}
2339
2340/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002341 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002342 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002343 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002344static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002345 mbedtls_x509_crl *crl_list,
2346 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002347{
2348 int flags = 0;
Przemek Stekiel40afdd22022-09-06 13:08:28 +02002349 unsigned char hash[MBEDTLS_HASH_MAX_SIZE];
pespacek7599a772022-02-07 14:40:55 +01002350#if defined(MBEDTLS_USE_PSA_CRYPTO)
pespacek7599a772022-02-07 14:40:55 +01002351 psa_algorithm_t psa_algorithm;
2352#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002353 const mbedtls_md_info_t *md_info;
pespacek7599a772022-02-07 14:40:55 +01002354#endif /* MBEDTLS_USE_PSA_CRYPTO */
2355 size_t hash_length;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002356
2357 if( ca == NULL )
2358 return( flags );
2359
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002360 while( crl_list != NULL )
2361 {
2362 if( crl_list->version == 0 ||
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002363 x509_name_cmp( &crl_list->issuer, &ca->subject ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002364 {
2365 crl_list = crl_list->next;
2366 continue;
2367 }
2368
2369 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002370 * Check if the CA is configured to sign CRLs
2371 */
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002372 if( mbedtls_x509_crt_check_key_usage( ca,
2373 MBEDTLS_X509_KU_CRL_SIGN ) != 0 )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002374 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002375 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002376 break;
2377 }
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002378
2379 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002380 * Check if CRL is correctly signed by the trusted CA
2381 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002382 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
2383 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
2384
2385 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
2386 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
2387
pespacek7599a772022-02-07 14:40:55 +01002388#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02002389 psa_algorithm = mbedtls_hash_info_psa_from_md( crl_list->sig_md );
pespacekd924e552022-02-28 11:49:54 +01002390 if( psa_hash_compute( psa_algorithm,
2391 crl_list->tbs.p,
2392 crl_list->tbs.len,
2393 hash,
2394 sizeof( hash ),
2395 &hash_length ) != PSA_SUCCESS )
pespaceka7a64692022-02-14 15:18:43 +01002396 {
2397 /* Note: this can't happen except after an internal error */
2398 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
2399 break;
2400 }
pespacek7599a772022-02-07 14:40:55 +01002401#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002402 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
pespacek7599a772022-02-07 14:40:55 +01002403 hash_length = mbedtls_md_get_size( md_info );
2404 if( mbedtls_md( md_info,
2405 crl_list->tbs.p,
2406 crl_list->tbs.len,
2407 hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002408 {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002409 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002410 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002411 break;
2412 }
pespaceka7a64692022-02-14 15:18:43 +01002413#endif /* MBEDTLS_USE_PSA_CRYPTO */
2414
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002415 if( x509_profile_check_key( profile, &ca->pk ) != 0 )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002416 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002418 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
pespacek7599a772022-02-07 14:40:55 +01002419 crl_list->sig_md, hash, hash_length,
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02002420 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002421 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002422 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002423 break;
2424 }
2425
2426 /*
2427 * Check for validity of CRL (Do not drop out)
2428 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002429 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002430 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002431
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002432 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002433 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002434
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002435 /*
2436 * Check if certificate is revoked
2437 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002438 if( mbedtls_x509_crt_is_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002439 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002440 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002441 break;
2442 }
2443
2444 crl_list = crl_list->next;
2445 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002446
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002447 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002448}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002449#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002450
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002451/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002452 * Check the signature of a certificate by its parent
2453 */
2454static int x509_crt_check_signature( const mbedtls_x509_crt *child,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002455 mbedtls_x509_crt *parent,
2456 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002457{
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002458 size_t hash_len;
Przemek Stekiel51669542022-09-13 12:57:05 +02002459 unsigned char hash[MBEDTLS_HASH_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002460#if !defined(MBEDTLS_USE_PSA_CRYPTO)
2461 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002462 md_info = mbedtls_md_info_from_type( child->sig_md );
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002463 hash_len = mbedtls_md_get_size( md_info );
Andrzej Kurek8b38ff52018-11-20 03:20:09 -05002464
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002465 /* Note: hash errors can happen only after an internal error */
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002466 if( mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash ) != 0 )
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002467 return( -1 );
2468#else
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02002469 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md( child->sig_md );
pespacek7599a772022-02-07 14:40:55 +01002470 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002471
pespacek7599a772022-02-07 14:40:55 +01002472 status = psa_hash_compute( hash_alg,
2473 child->tbs.p,
2474 child->tbs.len,
2475 hash,
pespacekb9f07a72022-02-14 15:13:26 +01002476 sizeof( hash ),
pespacek7599a772022-02-07 14:40:55 +01002477 &hash_len );
2478 if( status != PSA_SUCCESS )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002479 {
pespacek3110c7b2022-02-14 15:07:41 +01002480 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002481 }
2482
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002483#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002484 /* Skip expensive computation on obvious mismatch */
2485 if( ! mbedtls_pk_can_do( &parent->pk, child->sig_pk ) )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002486 return( -1 );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002487
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002488#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002489 if( rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA )
2490 {
2491 return( mbedtls_pk_verify_restartable( &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002492 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002493 child->sig.p, child->sig.len, &rs_ctx->pk ) );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002494 }
2495#else
2496 (void) rs_ctx;
2497#endif
2498
2499 return( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002500 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002501 child->sig.p, child->sig.len ) );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002502}
2503
2504/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002505 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2506 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002507 *
2508 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002509 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002510static int x509_crt_check_parent( const mbedtls_x509_crt *child,
2511 const mbedtls_x509_crt *parent,
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002512 int top )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002513{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002514 int need_ca_bit;
2515
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002516 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02002517 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002518 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002519
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002520 /* Parent must have the basicConstraints CA bit set as a general rule */
2521 need_ca_bit = 1;
2522
2523 /* Exception: v1/v2 certificates that are locally trusted. */
2524 if( top && parent->version < 3 )
2525 need_ca_bit = 0;
2526
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002527 if( need_ca_bit && ! parent->ca_istrue )
2528 return( -1 );
2529
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002530 if( need_ca_bit &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002531 mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002532 {
2533 return( -1 );
2534 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002535
2536 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002537}
2538
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002539/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002540 * Find a suitable parent for child in candidates, or return NULL.
2541 *
2542 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002543 * 1. subject name matches child's issuer
2544 * 2. if necessary, the CA bit is set and key usage allows signing certs
2545 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002546 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002547 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002548 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002549 * If there's a suitable candidate which is also time-valid, return the first
2550 * such. Otherwise, return the first suitable candidate (or NULL if there is
2551 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002552 *
2553 * The rationale for this rule is that someone could have a list of trusted
2554 * roots with two versions on the same root with different validity periods.
2555 * (At least one user reported having such a list and wanted it to just work.)
2556 * The reason we don't just require time-validity is that generally there is
2557 * only one version, and if it's expired we want the flags to state that
2558 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002559 *
2560 * The rationale for rule 3 (signature for trusted roots) is that users might
2561 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002562 * way we select the correct one is by checking the signature (as we don't
2563 * rely on key identifier extensions). (This is one way users might choose to
2564 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002565 *
2566 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002567 * - [in] child: certificate for which we're looking for a parent
2568 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002569 * - [out] r_parent: parent found (or NULL)
2570 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002571 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2572 * of the chain, 0 otherwise
2573 * - [in] path_cnt: number of intermediates seen so far
2574 * - [in] self_cnt: number of self-signed intermediates seen so far
2575 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002576 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002577 *
2578 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002579 * - 0 on success
2580 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002581 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002582static int x509_crt_find_parent_in(
2583 mbedtls_x509_crt *child,
2584 mbedtls_x509_crt *candidates,
2585 mbedtls_x509_crt **r_parent,
2586 int *r_signature_is_good,
2587 int top,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002588 unsigned path_cnt,
2589 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002590 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002591{
Janos Follath865b3eb2019-12-16 11:46:15 +00002592 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002593 mbedtls_x509_crt *parent, *fallback_parent;
Benjamin Kier36050732019-05-30 14:49:17 -04002594 int signature_is_good = 0, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002595
2596#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002597 /* did we have something in progress? */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002598 if( rs_ctx != NULL && rs_ctx->parent != NULL )
2599 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002600 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002601 parent = rs_ctx->parent;
2602 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002603 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002604
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002605 /* clear saved state */
2606 rs_ctx->parent = NULL;
2607 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002608 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002609
2610 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002611 goto check_signature;
2612 }
2613#endif
2614
2615 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002616 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002617
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002618 for( parent = candidates; parent != NULL; parent = parent->next )
2619 {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002620 /* basic parenting skills (name, CA bit, key usage) */
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002621 if( x509_crt_check_parent( child, parent, top ) != 0 )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002622 continue;
2623
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002624 /* +1 because stored max_pathlen is 1 higher that the actual value */
2625 if( parent->max_pathlen > 0 &&
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002626 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt )
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002627 {
2628 continue;
2629 }
2630
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002631 /* Signature */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002632#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2633check_signature:
2634#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002635 ret = x509_crt_check_signature( child, parent, rs_ctx );
2636
2637#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002638 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2639 {
2640 /* save state */
2641 rs_ctx->parent = parent;
2642 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002643 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002644
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002645 return( ret );
2646 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002647#else
2648 (void) ret;
2649#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002650
2651 signature_is_good = ret == 0;
2652 if( top && ! signature_is_good )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002653 continue;
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002654
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002655 /* optional time check */
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002656 if( mbedtls_x509_time_is_past( &parent->valid_to ) ||
2657 mbedtls_x509_time_is_future( &parent->valid_from ) )
2658 {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002659 if( fallback_parent == NULL )
2660 {
2661 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002662 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002663 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002664
2665 continue;
2666 }
2667
Andy Gross1f627142019-01-30 10:25:53 -06002668 *r_parent = parent;
2669 *r_signature_is_good = signature_is_good;
2670
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002671 break;
2672 }
2673
Andy Gross1f627142019-01-30 10:25:53 -06002674 if( parent == NULL )
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002675 {
2676 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002677 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002678 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002679
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002680 return( 0 );
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002681}
2682
2683/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002684 * Find a parent in trusted CAs or the provided chain, or return NULL.
2685 *
2686 * Searches in trusted CAs first, and return the first suitable parent found
2687 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002688 *
2689 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002690 * - [in] child: certificate for which we're looking for a parent, followed
2691 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002692 * - [in] trust_ca: list of locally trusted certificates
2693 * - [out] parent: parent found (or NULL)
2694 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2695 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2696 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2697 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002698 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002699 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002700 *
2701 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002702 * - 0 on success
2703 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002704 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002705static int x509_crt_find_parent(
2706 mbedtls_x509_crt *child,
2707 mbedtls_x509_crt *trust_ca,
2708 mbedtls_x509_crt **parent,
2709 int *parent_is_trusted,
2710 int *signature_is_good,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002711 unsigned path_cnt,
2712 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002713 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002714{
Janos Follath865b3eb2019-12-16 11:46:15 +00002715 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002716 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002717
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002718 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002719
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002720#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002721 /* restore then clear saved state if we have some stored */
2722 if( rs_ctx != NULL && rs_ctx->parent_is_trusted != -1 )
2723 {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002724 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002725 rs_ctx->parent_is_trusted = -1;
2726 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002727#endif
2728
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002729 while( 1 ) {
2730 search_list = *parent_is_trusted ? trust_ca : child->next;
2731
2732 ret = x509_crt_find_parent_in( child, search_list,
2733 parent, signature_is_good,
2734 *parent_is_trusted,
2735 path_cnt, self_cnt, rs_ctx );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002736
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002737#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002738 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2739 {
2740 /* save state */
2741 rs_ctx->parent_is_trusted = *parent_is_trusted;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002742 return( ret );
2743 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002744#else
2745 (void) ret;
2746#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002747
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002748 /* stop here if found or already in second iteration */
2749 if( *parent != NULL || *parent_is_trusted == 0 )
2750 break;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002751
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002752 /* prepare second iteration */
2753 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002754 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002755
2756 /* extra precaution against mistakes in the caller */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002757 if( *parent == NULL )
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002758 {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002759 *parent_is_trusted = 0;
2760 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002761 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002762
2763 return( 0 );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002764}
2765
2766/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002767 * Check if an end-entity certificate is locally trusted
2768 *
2769 * Currently we require such certificates to be self-signed (actually only
2770 * check for self-issued as self-signatures are not checked)
2771 */
2772static int x509_crt_check_ee_locally_trusted(
2773 mbedtls_x509_crt *crt,
2774 mbedtls_x509_crt *trust_ca )
2775{
2776 mbedtls_x509_crt *cur;
2777
2778 /* must be self-issued */
2779 if( x509_name_cmp( &crt->issuer, &crt->subject ) != 0 )
2780 return( -1 );
2781
2782 /* look for an exact match with trusted cert */
2783 for( cur = trust_ca; cur != NULL; cur = cur->next )
2784 {
2785 if( crt->raw.len == cur->raw.len &&
2786 memcmp( crt->raw.p, cur->raw.p, crt->raw.len ) == 0 )
2787 {
2788 return( 0 );
2789 }
2790 }
2791
2792 /* too bad */
2793 return( -1 );
2794}
2795
2796/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002797 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002798 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002799 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2800 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002801 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002802 * such that every cert in the chain is a child of the next one,
2803 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002804 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002805 * Verify that chain and return it with flags for all issues found.
2806 *
2807 * Special cases:
2808 * - EE == Rj -> return a one-element list containing it
2809 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2810 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002811 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002812 * Tests for (aspects of) this function should include at least:
2813 * - trusted EE
2814 * - EE -> trusted root
Antonin Décimo36e89b52019-01-23 15:24:37 +01002815 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002816 * - if relevant: EE untrusted
2817 * - if relevant: EE -> intermediate, untrusted
2818 * with the aspect under test checked at each relevant level (EE, int, root).
2819 * For some aspects longer chains are required, but usually length 2 is
2820 * enough (but length 1 is not in general).
2821 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002822 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002823 * - [in] crt: the cert list EE, C1, ..., Cn
2824 * - [in] trust_ca: the trusted list R1, ..., Rp
2825 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002826 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002827 * Only valid when return value is 0, may contain garbage otherwise!
2828 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002829 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002830 *
2831 * Return value:
2832 * - non-zero if the chain could not be fully built and examined
2833 * - 0 is the chain was successfully built and examined,
2834 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002835 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002836static int x509_crt_verify_chain(
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002837 mbedtls_x509_crt *crt,
2838 mbedtls_x509_crt *trust_ca,
2839 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00002840 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2841 void *p_ca_cb,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002842 const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002843 mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002844 mbedtls_x509_crt_restart_ctx *rs_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002845{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002846 /* Don't initialize any of those variables here, so that the compiler can
2847 * catch potential issues with jumping ahead when restarting */
Janos Follath865b3eb2019-12-16 11:46:15 +00002848 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002849 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002850 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002851 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002852 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002853 int parent_is_trusted;
2854 int child_is_trusted;
2855 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002856 unsigned self_cnt;
Hanno Beckerf53893b2019-03-28 13:45:55 +00002857 mbedtls_x509_crt *cur_trust_ca = NULL;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002858
2859#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2860 /* resume if we had an operation in progress */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002861 if( rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent )
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002862 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002863 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002864 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002865 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002866
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002867 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002868 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002869 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002870 flags = &cur->flags;
2871
2872 goto find_parent;
2873 }
2874#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002875
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002876 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002877 self_cnt = 0;
2878 parent_is_trusted = 0;
2879 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002880
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002881 while( 1 ) {
2882 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002883 cur = &ver_chain->items[ver_chain->len];
2884 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002885 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002886 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002887 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002888
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002889 /* Check time-validity (all certificates) */
2890 if( mbedtls_x509_time_is_past( &child->valid_to ) )
2891 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002892
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002893 if( mbedtls_x509_time_is_future( &child->valid_from ) )
2894 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002895
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002896 /* Stop here for trusted roots (but not for trusted EE certs) */
2897 if( child_is_trusted )
2898 return( 0 );
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002899
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002900 /* Check signature algorithm: MD & PK algs */
2901 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
2902 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002903
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002904 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
2905 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002906
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002907 /* Special case: EE certs that are locally trusted */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002908 if( ver_chain->len == 1 &&
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002909 x509_crt_check_ee_locally_trusted( child, trust_ca ) == 0 )
2910 {
2911 return( 0 );
2912 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002913
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002914#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2915find_parent:
2916#endif
Hanno Beckerf53893b2019-03-28 13:45:55 +00002917
2918 /* Obtain list of potential trusted signers from CA callback,
2919 * or use statically provided list. */
2920#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
2921 if( f_ca_cb != NULL )
2922 {
2923 mbedtls_x509_crt_free( ver_chain->trust_ca_cb_result );
2924 mbedtls_free( ver_chain->trust_ca_cb_result );
2925 ver_chain->trust_ca_cb_result = NULL;
2926
2927 ret = f_ca_cb( p_ca_cb, child, &ver_chain->trust_ca_cb_result );
2928 if( ret != 0 )
2929 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2930
2931 cur_trust_ca = ver_chain->trust_ca_cb_result;
2932 }
2933 else
2934#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2935 {
2936 ((void) f_ca_cb);
2937 ((void) p_ca_cb);
2938 cur_trust_ca = trust_ca;
2939 }
2940
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002941 /* Look for a parent in trusted CAs or up the chain */
Hanno Beckerf53893b2019-03-28 13:45:55 +00002942 ret = x509_crt_find_parent( child, cur_trust_ca, &parent,
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002943 &parent_is_trusted, &signature_is_good,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002944 ver_chain->len - 1, self_cnt, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002945
2946#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002947 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2948 {
2949 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002950 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002951 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002952 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002953
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002954 return( ret );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002955 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002956#else
2957 (void) ret;
2958#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002959
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002960 /* No parent? We're done here */
2961 if( parent == NULL )
2962 {
2963 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2964 return( 0 );
2965 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002966
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002967 /* Count intermediate self-issued (not necessarily self-signed) certs.
2968 * These can occur with some strategies for key rollover, see [SIRO],
2969 * and should be excluded from max_pathlen checks. */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002970 if( ver_chain->len != 1 &&
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002971 x509_name_cmp( &child->issuer, &child->subject ) == 0 )
2972 {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002973 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002974 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002975
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002976 /* path_cnt is 0 for the first intermediate CA,
2977 * and if parent is trusted it's not an intermediate CA */
2978 if( ! parent_is_trusted &&
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002979 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002980 {
2981 /* return immediately to avoid overflow the chain array */
2982 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2983 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002984
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002985 /* signature was checked while searching parent */
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002986 if( ! signature_is_good )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002987 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2988
2989 /* check size of signing key */
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002990 if( x509_profile_check_key( profile, &parent->pk ) != 0 )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002991 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002993#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002994 /* Check trusted CA's CRL for the given crt */
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002995 *flags |= x509_crt_verifycrl( child, parent, ca_crl, profile );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002996#else
2997 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002998#endif
2999
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003000 /* prepare for next iteration */
3001 child = parent;
3002 parent = NULL;
3003 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003004 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003005 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003006}
3007
3008/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003009 * Check for CN match
3010 */
3011static int x509_crt_check_cn( const mbedtls_x509_buf *name,
3012 const char *cn, size_t cn_len )
3013{
3014 /* try exact match */
3015 if( name->len == cn_len &&
3016 x509_memcasecmp( cn, name->p, cn_len ) == 0 )
3017 {
3018 return( 0 );
3019 }
3020
3021 /* try wildcard match */
Manuel Pégourié-Gonnard900fba62017-10-18 14:28:11 +02003022 if( x509_check_wildcard( cn, name ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003023 {
3024 return( 0 );
3025 }
3026
3027 return( -1 );
3028}
3029
3030/*
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003031 * Check for SAN match, see RFC 5280 Section 4.2.1.6
3032 */
3033static int x509_crt_check_san( const mbedtls_x509_buf *name,
3034 const char *cn, size_t cn_len )
3035{
3036 const unsigned char san_type = (unsigned char) name->tag &
3037 MBEDTLS_ASN1_TAG_VALUE_MASK;
3038
3039 /* dNSName */
3040 if( san_type == MBEDTLS_X509_SAN_DNS_NAME )
3041 return( x509_crt_check_cn( name, cn, cn_len ) );
3042
3043 /* (We may handle other types here later.) */
3044
3045 /* Unrecognized type */
3046 return( -1 );
3047}
3048
3049/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003050 * Verify the requested CN - only call this if cn is not NULL!
3051 */
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003052static void x509_crt_verify_name( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003053 const char *cn,
3054 uint32_t *flags )
3055{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003056 const mbedtls_x509_name *name;
3057 const mbedtls_x509_sequence *cur;
3058 size_t cn_len = strlen( cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003059
3060 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
3061 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003062 for( cur = &crt->subject_alt_names; cur != NULL; cur = cur->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003063 {
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003064 if( x509_crt_check_san( &cur->buf, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003065 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003066 }
3067
3068 if( cur == NULL )
3069 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3070 }
3071 else
3072 {
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02003073 for( name = &crt->subject; name != NULL; name = name->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003074 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003075 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, &name->oid ) == 0 &&
3076 x509_crt_check_cn( &name->val, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003077 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003078 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003079 }
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003080 }
3081
3082 if( name == NULL )
3083 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3084 }
3085}
3086
3087/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003088 * Merge the flags for all certs in the chain, after calling callback
3089 */
3090static int x509_crt_merge_flags_with_cb(
3091 uint32_t *flags,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003092 const mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003093 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3094 void *p_vrfy )
3095{
Janos Follath865b3eb2019-12-16 11:46:15 +00003096 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003097 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003098 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003099 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003100
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003101 for( i = ver_chain->len; i != 0; --i )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003102 {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003103 cur = &ver_chain->items[i-1];
3104 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003105
3106 if( NULL != f_vrfy )
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003107 if( ( ret = f_vrfy( p_vrfy, cur->crt, (int) i-1, &cur_flags ) ) != 0 )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003108 return( ret );
3109
3110 *flags |= cur_flags;
3111 }
3112
3113 return( 0 );
3114}
3115
3116/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003117 * Verify the certificate validity, with profile, restartable version
3118 *
3119 * This function:
3120 * - checks the requested CN (if any)
3121 * - checks the type and size of the EE cert's key,
3122 * as that isn't done as part of chain building/verification currently
3123 * - builds and verifies the chain
3124 * - then calls the callback and merges the flags
Hanno Becker3116fb32019-03-28 13:34:42 +00003125 *
3126 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
3127 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
3128 * verification routine to search for trusted signers, and CRLs will
3129 * be disabled. Otherwise, `trust_ca` will be used as the static list
3130 * of trusted signers, and `ca_crl` will be use as the static list
3131 * of CRLs.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003132 */
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003133static int x509_crt_verify_restartable_ca_cb( mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003134 mbedtls_x509_crt *trust_ca,
3135 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003136 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3137 void *p_ca_cb,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003138 const mbedtls_x509_crt_profile *profile,
3139 const char *cn, uint32_t *flags,
3140 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3141 void *p_vrfy,
3142 mbedtls_x509_crt_restart_ctx *rs_ctx )
3143{
Janos Follath865b3eb2019-12-16 11:46:15 +00003144 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003145 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003146 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003147 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003148
3149 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003150 ee_flags = 0;
3151 x509_crt_verify_chain_reset( &ver_chain );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003152
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003153 if( profile == NULL )
3154 {
3155 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3156 goto exit;
3157 }
3158
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003159 /* check name if requested */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003160 if( cn != NULL )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003161 x509_crt_verify_name( crt, cn, &ee_flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003162
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003163 /* Check the type and size of the key */
3164 pk_type = mbedtls_pk_get_type( &crt->pk );
3165
3166 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003167 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003168
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02003169 if( x509_profile_check_key( profile, &crt->pk ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003170 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003171
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003172 /* Check the chain */
Hanno Becker3116fb32019-03-28 13:34:42 +00003173 ret = x509_crt_verify_chain( crt, trust_ca, ca_crl,
3174 f_ca_cb, p_ca_cb, profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003175 &ver_chain, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003176
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003177 if( ret != 0 )
3178 goto exit;
3179
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003180 /* Merge end-entity flags */
3181 ver_chain.items[0].flags |= ee_flags;
3182
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003183 /* Build final flags, calling callback on the way if any */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003184 ret = x509_crt_merge_flags_with_cb( flags, &ver_chain, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003185
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003186exit:
Hanno Beckerf53893b2019-03-28 13:45:55 +00003187
3188#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3189 mbedtls_x509_crt_free( ver_chain.trust_ca_cb_result );
3190 mbedtls_free( ver_chain.trust_ca_cb_result );
3191 ver_chain.trust_ca_cb_result = NULL;
3192#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3193
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003194#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3195 if( rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
3196 mbedtls_x509_crt_restart_free( rs_ctx );
3197#endif
3198
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003199 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3200 * the SSL module for authmode optional, but non-zero return from the
3201 * callback means a fatal error so it shouldn't be ignored */
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003202 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
3203 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3204
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003205 if( ret != 0 )
3206 {
3207 *flags = (uint32_t) -1;
3208 return( ret );
3209 }
3210
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003211 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003212 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003213
3214 return( 0 );
3215}
3216
Hanno Becker3116fb32019-03-28 13:34:42 +00003217
3218/*
3219 * Verify the certificate validity (default profile, not restartable)
3220 */
3221int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
3222 mbedtls_x509_crt *trust_ca,
3223 mbedtls_x509_crl *ca_crl,
3224 const char *cn, uint32_t *flags,
3225 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3226 void *p_vrfy )
3227{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003228 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003229 NULL, NULL,
3230 &mbedtls_x509_crt_profile_default,
3231 cn, flags,
3232 f_vrfy, p_vrfy, NULL ) );
3233}
3234
3235/*
3236 * Verify the certificate validity (user-chosen profile, not restartable)
3237 */
3238int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
3239 mbedtls_x509_crt *trust_ca,
3240 mbedtls_x509_crl *ca_crl,
3241 const mbedtls_x509_crt_profile *profile,
3242 const char *cn, uint32_t *flags,
3243 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3244 void *p_vrfy )
3245{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003246 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003247 NULL, NULL,
3248 profile, cn, flags,
3249 f_vrfy, p_vrfy, NULL ) );
3250}
3251
3252#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3253/*
3254 * Verify the certificate validity (user-chosen profile, CA callback,
3255 * not restartable).
3256 */
Jarno Lamsa31d9db62019-04-01 14:33:49 +03003257int mbedtls_x509_crt_verify_with_ca_cb( mbedtls_x509_crt *crt,
Hanno Becker3116fb32019-03-28 13:34:42 +00003258 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3259 void *p_ca_cb,
3260 const mbedtls_x509_crt_profile *profile,
3261 const char *cn, uint32_t *flags,
3262 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3263 void *p_vrfy )
3264{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003265 return( x509_crt_verify_restartable_ca_cb( crt, NULL, NULL,
Hanno Becker3116fb32019-03-28 13:34:42 +00003266 f_ca_cb, p_ca_cb,
3267 profile, cn, flags,
3268 f_vrfy, p_vrfy, NULL ) );
3269}
3270#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3271
3272int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
3273 mbedtls_x509_crt *trust_ca,
3274 mbedtls_x509_crl *ca_crl,
3275 const mbedtls_x509_crt_profile *profile,
3276 const char *cn, uint32_t *flags,
3277 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3278 void *p_vrfy,
3279 mbedtls_x509_crt_restart_ctx *rs_ctx )
3280{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003281 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003282 NULL, NULL,
3283 profile, cn, flags,
3284 f_vrfy, p_vrfy, rs_ctx ) );
3285}
3286
3287
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003288/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003289 * Initialize a certificate chain
3290 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003291void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02003292{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003293 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02003294}
3295
3296/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003297 * Unallocate all certificate data
3298 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003299void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003300{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003301 mbedtls_x509_crt *cert_cur = crt;
3302 mbedtls_x509_crt *cert_prv;
3303 mbedtls_x509_name *name_cur;
3304 mbedtls_x509_name *name_prv;
3305 mbedtls_x509_sequence *seq_cur;
3306 mbedtls_x509_sequence *seq_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003307
3308 if( crt == NULL )
3309 return;
3310
3311 do
3312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003313 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003315#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
3316 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003317#endif
3318
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003319 name_cur = cert_cur->issuer.next;
3320 while( name_cur != NULL )
3321 {
3322 name_prv = name_cur;
3323 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003324 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003325 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003326 }
3327
3328 name_cur = cert_cur->subject.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 seq_cur = cert_cur->ext_key_usage.next;
3338 while( seq_cur != NULL )
3339 {
3340 seq_prv = seq_cur;
3341 seq_cur = seq_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003342 mbedtls_platform_zeroize( seq_prv,
3343 sizeof( mbedtls_x509_sequence ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003344 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003345 }
3346
3347 seq_cur = cert_cur->subject_alt_names.next;
3348 while( seq_cur != NULL )
3349 {
3350 seq_prv = seq_cur;
3351 seq_cur = seq_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003352 mbedtls_platform_zeroize( seq_prv,
3353 sizeof( mbedtls_x509_sequence ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003354 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003355 }
3356
Ron Eldor74d9acc2019-03-21 14:00:03 +02003357 seq_cur = cert_cur->certificate_policies.next;
3358 while( seq_cur != NULL )
3359 {
3360 seq_prv = seq_cur;
3361 seq_cur = seq_cur->next;
3362 mbedtls_platform_zeroize( seq_prv,
3363 sizeof( mbedtls_x509_sequence ) );
3364 mbedtls_free( seq_prv );
3365 }
3366
Hanno Becker1a65dcd2019-01-31 08:57:44 +00003367 if( cert_cur->raw.p != NULL && cert_cur->own_buffer )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003368 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003369 mbedtls_platform_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003370 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003371 }
3372
3373 cert_cur = cert_cur->next;
3374 }
3375 while( cert_cur != NULL );
3376
3377 cert_cur = crt;
3378 do
3379 {
3380 cert_prv = cert_cur;
3381 cert_cur = cert_cur->next;
3382
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003383 mbedtls_platform_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003384 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003385 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003386 }
3387 while( cert_cur != NULL );
3388}
3389
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003390#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3391/*
3392 * Initialize a restart context
3393 */
3394void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx )
3395{
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003396 mbedtls_pk_restart_init( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003397
3398 ctx->parent = NULL;
3399 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003400 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003401
3402 ctx->parent_is_trusted = -1;
3403
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003404 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003405 ctx->self_cnt = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003406 x509_crt_verify_chain_reset( &ctx->ver_chain );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003407}
3408
3409/*
3410 * Free the components of a restart context
3411 */
3412void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx )
3413{
3414 if( ctx == NULL )
3415 return;
3416
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003417 mbedtls_pk_restart_free( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003418 mbedtls_x509_crt_restart_init( ctx );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003419}
3420#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003422#endif /* MBEDTLS_X509_CRT_PARSE_C */