blob: 73116ea159e0fcdf5e983a508b90de992f010ccf [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/*
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +02002 * X.509 certificate parsing and verification
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker7c6b2c32013-09-16 13:49:26 +020018 */
19/*
20 * The ITU-T X.509 standard defines a certificate format for PKI.
21 *
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +020022 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
23 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
24 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020025 *
26 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
27 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +020028 *
29 * [SIRO] https://cabforum.org/wp-content/uploads/Chunghwatelecom201503cabforumV4.pdf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020030 */
31
Gilles Peskinedb09ef62020-06-03 01:43:33 +020032#include "common.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020035
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/x509_crt.h"
Janos Follath73c616b2019-12-18 15:07:04 +000037#include "mbedtls/error.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050039#include "mbedtls/platform_util.h"
Rich Evans00ab4702015-02-06 13:43:58 +000040
Rich Evans00ab4702015-02-06 13:43:58 +000041#include <string.h>
42
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020045#endif
46
Andrzej Kurekd4a65532018-10-31 06:18:39 -040047#if defined(MBEDTLS_USE_PSA_CRYPTO)
48#include "psa/crypto.h"
49#include "mbedtls/psa_util.h"
50#endif
51
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020054#else
Simon Butcherd2642582018-10-03 15:11:19 +010055#include <stdio.h>
Rich Evans00ab4702015-02-06 13:43:58 +000056#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#define mbedtls_free free
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020058#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#define mbedtls_snprintf snprintf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020060#endif
61
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000063#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010064#endif
65
Daniel Axtensf0710242020-05-28 11:43:41 +100066#if defined(MBEDTLS_HAVE_TIME)
Paul Bakkerfa6a6202013-10-28 18:48:30 +010067#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020068#include <windows.h>
69#else
70#include <time.h>
71#endif
Daniel Axtensf0710242020-05-28 11:43:41 +100072#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074#if defined(MBEDTLS_FS_IO)
Rich Evans00ab4702015-02-06 13:43:58 +000075#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020076#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020077#include <sys/types.h>
78#include <sys/stat.h>
Martino Facchin0ec05ec2021-05-04 11:47:36 +020079#if defined(__MBED__)
80#include <platform/mbed_retarget.h>
81#else
Paul Bakker7c6b2c32013-09-16 13:49:26 +020082#include <dirent.h>
Martino Facchin0ec05ec2021-05-04 11:47:36 +020083#endif /* __MBED__ */
Rich Evans00ab4702015-02-06 13:43:58 +000084#endif /* !_WIN32 || EFIX64 || EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020085#endif
86
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +020087/*
88 * Item in a verification chain: cert and flags for it
89 */
90typedef struct {
91 mbedtls_x509_crt *crt;
92 uint32_t flags;
93} x509_crt_verify_chain_item;
94
95/*
96 * Max size of verification chain: end-entity + intermediates + trusted root
97 */
98#define X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
Paul Bakker34617722014-06-13 17:20:13 +020099
Gilles Peskineffb92da2021-06-02 00:03:26 +0200100/* Default profile. Do not remove items unless there are serious security
101 * concerns. */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200102const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
103{
Gilles Peskineae270bf2021-06-02 00:05:29 +0200104 /* Hashes from SHA-256 and above. Note that this selection
105 * should be aligned with ssl_preset_default_hashes in ssl_tls.c. */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200106 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
107 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
108 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
109 0xFFFFFFF, /* Any PK alg */
110#if defined(MBEDTLS_ECP_C)
Gilles Peskineae270bf2021-06-02 00:05:29 +0200111 /* Curves at or above 128-bit security level. Note that this selection
112 * should be aligned with ssl_preset_default_curves in ssl_tls.c. */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200113 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
114 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
115 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
116 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
117 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
118 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
Gilles Peskine39957502021-06-17 23:17:52 +0200119 0,
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200120#else
121 0,
122#endif
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200123 2048,
124};
125
Gilles Peskineffb92da2021-06-02 00:03:26 +0200126/* Next-generation profile. Currently identical to the default, but may
127 * be tightened at any time. */
128const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200129{
130 /* Hashes from SHA-256 and above. */
131 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
132 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
133 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
134 0xFFFFFFF, /* Any PK alg */
135#if defined(MBEDTLS_ECP_C)
136 /* Curves at or above 128-bit security level. */
137 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
138 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
139 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
140 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
141 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
142 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
143 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256K1 ),
144#else
145 0,
146#endif
147 2048,
148};
Gilles Peskineffb92da2021-06-02 00:03:26 +0200149
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200150/*
151 * NSA Suite B Profile
152 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200153const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
154{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200155 /* Only SHA-256 and 384 */
156 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
157 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ),
158 /* Only ECDSA */
Ron Eldor85e1dcf2018-02-06 15:59:38 +0200159 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECDSA ) |
160 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECKEY ),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200161#if defined(MBEDTLS_ECP_C)
162 /* Only NIST P-256 and P-384 */
163 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
164 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ),
165#else
166 0,
167#endif
168 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200169};
170
171/*
Manuel Pégourié-Gonnard9d4c2c42021-06-18 09:48:27 +0200172 * Empty / all-forbidden profile
173 */
174const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_none =
175{
176 0,
177 0,
178 0,
179 (uint32_t) -1,
180};
181
182/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200183 * Check md_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200184 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200185 */
186static int x509_profile_check_md_alg( const mbedtls_x509_crt_profile *profile,
187 mbedtls_md_type_t md_alg )
188{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200189 if( md_alg == MBEDTLS_MD_NONE )
190 return( -1 );
191
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200192 if( ( profile->allowed_mds & MBEDTLS_X509_ID_FLAG( md_alg ) ) != 0 )
193 return( 0 );
194
195 return( -1 );
196}
197
198/*
199 * Check pk_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200200 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200201 */
202static int x509_profile_check_pk_alg( const mbedtls_x509_crt_profile *profile,
203 mbedtls_pk_type_t pk_alg )
204{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200205 if( pk_alg == MBEDTLS_PK_NONE )
206 return( -1 );
207
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200208 if( ( profile->allowed_pks & MBEDTLS_X509_ID_FLAG( pk_alg ) ) != 0 )
209 return( 0 );
210
211 return( -1 );
212}
213
214/*
215 * Check key against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200216 * Return 0 if pk is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200217 */
218static int x509_profile_check_key( const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200219 const mbedtls_pk_context *pk )
220{
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200221 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type( pk );
Manuel Pégourié-Gonnard19773ff2017-10-24 10:51:26 +0200222
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200223#if defined(MBEDTLS_RSA_C)
224 if( pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS )
225 {
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200226 if( mbedtls_pk_get_bitlen( pk ) >= profile->rsa_min_bitlen )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200227 return( 0 );
228
229 return( -1 );
230 }
231#endif
232
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200233#if defined(MBEDTLS_ECP_C)
234 if( pk_alg == MBEDTLS_PK_ECDSA ||
235 pk_alg == MBEDTLS_PK_ECKEY ||
236 pk_alg == MBEDTLS_PK_ECKEY_DH )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200237 {
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200238 const mbedtls_ecp_group_id gid = mbedtls_pk_ec( *pk )->grp.id;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200239
Philippe Antoineb5b25432018-05-11 11:06:29 +0200240 if( gid == MBEDTLS_ECP_DP_NONE )
241 return( -1 );
242
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200243 if( ( profile->allowed_curves & MBEDTLS_X509_ID_FLAG( gid ) ) != 0 )
244 return( 0 );
245
246 return( -1 );
247 }
248#endif
249
250 return( -1 );
251}
252
253/*
Hanno Becker1f8527f2018-11-02 09:19:16 +0000254 * Like memcmp, but case-insensitive and always returns -1 if different
255 */
256static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
257{
258 size_t i;
259 unsigned char diff;
260 const unsigned char *n1 = s1, *n2 = s2;
261
262 for( i = 0; i < len; i++ )
263 {
264 diff = n1[i] ^ n2[i];
265
266 if( diff == 0 )
267 continue;
268
269 if( diff == 32 &&
270 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
271 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
272 {
273 continue;
274 }
275
276 return( -1 );
277 }
278
279 return( 0 );
280}
281
282/*
283 * Return 0 if name matches wildcard, -1 otherwise
284 */
285static int x509_check_wildcard( const char *cn, const mbedtls_x509_buf *name )
286{
287 size_t i;
288 size_t cn_idx = 0, cn_len = strlen( cn );
289
290 /* We can't have a match if there is no wildcard to match */
291 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
292 return( -1 );
293
294 for( i = 0; i < cn_len; ++i )
295 {
296 if( cn[i] == '.' )
297 {
298 cn_idx = i;
299 break;
300 }
301 }
302
303 if( cn_idx == 0 )
304 return( -1 );
305
306 if( cn_len - cn_idx == name->len - 1 &&
307 x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
308 {
309 return( 0 );
310 }
311
312 return( -1 );
313}
314
315/*
316 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
317 * variations (but not all).
318 *
319 * Return 0 if equal, -1 otherwise.
320 */
321static int x509_string_cmp( const mbedtls_x509_buf *a, const mbedtls_x509_buf *b )
322{
323 if( a->tag == b->tag &&
324 a->len == b->len &&
325 memcmp( a->p, b->p, b->len ) == 0 )
326 {
327 return( 0 );
328 }
329
330 if( ( a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
331 ( b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
332 a->len == b->len &&
333 x509_memcasecmp( a->p, b->p, b->len ) == 0 )
334 {
335 return( 0 );
336 }
337
338 return( -1 );
339}
340
341/*
342 * Compare two X.509 Names (aka rdnSequence).
343 *
344 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
345 * we sometimes return unequal when the full algorithm would return equal,
346 * but never the other way. (In particular, we don't do Unicode normalisation
347 * or space folding.)
348 *
349 * Return 0 if equal, -1 otherwise.
350 */
351static int x509_name_cmp( const mbedtls_x509_name *a, const mbedtls_x509_name *b )
352{
353 /* Avoid recursion, it might not be optimised by the compiler */
354 while( a != NULL || b != NULL )
355 {
356 if( a == NULL || b == NULL )
357 return( -1 );
358
359 /* type */
360 if( a->oid.tag != b->oid.tag ||
361 a->oid.len != b->oid.len ||
362 memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 )
363 {
364 return( -1 );
365 }
366
367 /* value */
368 if( x509_string_cmp( &a->val, &b->val ) != 0 )
369 return( -1 );
370
371 /* structure of the list of sets */
372 if( a->next_merged != b->next_merged )
373 return( -1 );
374
375 a = a->next;
376 b = b->next;
377 }
378
379 /* a == NULL == b */
380 return( 0 );
381}
382
383/*
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200384 * Reset (init or clear) a verify_chain
385 */
386static void x509_crt_verify_chain_reset(
387 mbedtls_x509_crt_verify_chain *ver_chain )
388{
389 size_t i;
390
391 for( i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++ )
392 {
393 ver_chain->items[i].crt = NULL;
Hanno Beckera9375b32019-01-10 09:19:26 +0000394 ver_chain->items[i].flags = (uint32_t) -1;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200395 }
396
397 ver_chain->len = 0;
Hanno Beckerf53893b2019-03-28 13:45:55 +0000398
399#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
400 ver_chain->trust_ca_cb_result = NULL;
401#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200402}
403
404/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200405 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
406 */
407static int x509_get_version( unsigned char **p,
408 const unsigned char *end,
409 int *ver )
410{
Janos Follath865b3eb2019-12-16 11:46:15 +0000411 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200412 size_t len;
413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
415 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200416 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200418 {
419 *ver = 0;
420 return( 0 );
421 }
422
Chris Jones9f7a6932021-04-14 12:12:09 +0100423 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200424 }
425
426 end = *p + len;
427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100429 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_VERSION, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200430
431 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100432 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_VERSION,
433 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200434
435 return( 0 );
436}
437
438/*
439 * Validity ::= SEQUENCE {
440 * notBefore Time,
441 * notAfter Time }
442 */
443static int x509_get_dates( unsigned char **p,
444 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445 mbedtls_x509_time *from,
446 mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200447{
Janos Follath865b3eb2019-12-16 11:46:15 +0000448 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200449 size_t len;
450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
452 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100453 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200454
455 end = *p + len;
456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200458 return( ret );
459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200461 return( ret );
462
463 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100464 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE,
465 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200466
467 return( 0 );
468}
469
470/*
471 * X.509 v2/v3 unique identifier (not parsed)
472 */
473static int x509_get_uid( unsigned char **p,
474 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475 mbedtls_x509_buf *uid, int n )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200476{
Janos Follath865b3eb2019-12-16 11:46:15 +0000477 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200478
479 if( *p == end )
480 return( 0 );
481
482 uid->tag = **p;
483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 if( ( ret = mbedtls_asn1_get_tag( p, end, &uid->len,
485 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200486 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200488 return( 0 );
489
Chris Jones9f7a6932021-04-14 12:12:09 +0100490 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200491 }
492
493 uid->p = *p;
494 *p += uid->len;
495
496 return( 0 );
497}
498
499static int x509_get_basic_constraints( unsigned char **p,
500 const unsigned char *end,
501 int *ca_istrue,
502 int *max_pathlen )
503{
Janos Follath865b3eb2019-12-16 11:46:15 +0000504 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200505 size_t len;
506
507 /*
508 * BasicConstraints ::= SEQUENCE {
509 * cA BOOLEAN DEFAULT FALSE,
510 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
511 */
512 *ca_istrue = 0; /* DEFAULT FALSE */
513 *max_pathlen = 0; /* endless */
514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
516 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100517 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200518
519 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200520 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 if( ( ret = mbedtls_asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200523 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
525 ret = mbedtls_asn1_get_int( p, end, ca_istrue );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200526
527 if( ret != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100528 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200529
530 if( *ca_istrue != 0 )
531 *ca_istrue = 1;
532 }
533
534 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200535 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537 if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100538 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200539
540 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100541 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
542 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200543
Andrzej Kurek16050742020-04-14 09:49:52 -0400544 /* Do not accept max_pathlen equal to INT_MAX to avoid a signed integer
545 * overflow, which is an undefined behavior. */
546 if( *max_pathlen == INT_MAX )
Chris Jones9f7a6932021-04-14 12:12:09 +0100547 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
548 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Andrzej Kurek16050742020-04-14 09:49:52 -0400549
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200550 (*max_pathlen)++;
551
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200552 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200553}
554
555static int x509_get_ns_cert_type( unsigned char **p,
556 const unsigned char *end,
557 unsigned char *ns_cert_type)
558{
Janos Follath865b3eb2019-12-16 11:46:15 +0000559 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100563 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200564
565 if( bs.len != 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100566 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
567 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200568
569 /* Get actual bitstring */
570 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200571 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200572}
573
574static int x509_get_key_usage( unsigned char **p,
575 const unsigned char *end,
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +0100576 unsigned int *key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200577{
Janos Follath865b3eb2019-12-16 11:46:15 +0000578 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200579 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100583 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200584
585 if( bs.len < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100586 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
587 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200588
589 /* Get actual bitstring */
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200590 *key_usage = 0;
591 for( i = 0; i < bs.len && i < sizeof( unsigned int ); i++ )
592 {
593 *key_usage |= (unsigned int) bs.p[i] << (8*i);
594 }
595
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200596 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200597}
598
599/*
600 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
601 *
602 * KeyPurposeId ::= OBJECT IDENTIFIER
603 */
604static int x509_get_ext_key_usage( unsigned char **p,
605 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200607{
Janos Follath865b3eb2019-12-16 11:46:15 +0000608 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 if( ( ret = mbedtls_asn1_get_sequence_of( p, end, ext_key_usage, MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100611 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200612
613 /* Sequence length must be >= 1 */
614 if( ext_key_usage->buf.p == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100615 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
616 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200617
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200618 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200619}
620
621/*
622 * SubjectAltName ::= GeneralNames
623 *
624 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
625 *
626 * GeneralName ::= CHOICE {
627 * otherName [0] OtherName,
628 * rfc822Name [1] IA5String,
629 * dNSName [2] IA5String,
630 * x400Address [3] ORAddress,
631 * directoryName [4] Name,
632 * ediPartyName [5] EDIPartyName,
633 * uniformResourceIdentifier [6] IA5String,
634 * iPAddress [7] OCTET STRING,
635 * registeredID [8] OBJECT IDENTIFIER }
636 *
637 * OtherName ::= SEQUENCE {
638 * type-id OBJECT IDENTIFIER,
639 * value [0] EXPLICIT ANY DEFINED BY type-id }
640 *
641 * EDIPartyName ::= SEQUENCE {
642 * nameAssigner [0] DirectoryString OPTIONAL,
643 * partyName [1] DirectoryString }
644 *
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300645 * NOTE: we list all types, but only use dNSName and otherName
646 * of type HwModuleName, as defined in RFC 4108, at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200647 */
648static int x509_get_subject_alt_name( unsigned char **p,
649 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650 mbedtls_x509_sequence *subject_alt_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200651{
Janos Follath865b3eb2019-12-16 11:46:15 +0000652 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200653 size_t len, tag_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654 mbedtls_asn1_buf *buf;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200655 unsigned char tag;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 mbedtls_asn1_sequence *cur = subject_alt_name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200657
658 /* Get main sequence tag */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
660 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100661 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200662
663 if( *p + len != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100664 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
665 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200666
667 while( *p < end )
668 {
Ron Eldordbbd9662019-05-15 15:46:03 +0300669 mbedtls_x509_subject_alternative_name dummy_san_buf;
670 memset( &dummy_san_buf, 0, sizeof( dummy_san_buf ) );
671
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200672 tag = **p;
673 (*p)++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 if( ( ret = mbedtls_asn1_get_len( p, end, &tag_len ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100675 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200676
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100677 if( ( tag & MBEDTLS_ASN1_TAG_CLASS_MASK ) !=
678 MBEDTLS_ASN1_CONTEXT_SPECIFIC )
679 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100680 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
681 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100682 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200683
Ron Eldordbbd9662019-05-15 15:46:03 +0300684 /*
irwir74aee1c2019-09-21 18:21:48 +0300685 * Check that the SAN is structured correctly.
Ron Eldordbbd9662019-05-15 15:46:03 +0300686 */
687 ret = mbedtls_x509_parse_subject_alt_name( &(cur->buf), &dummy_san_buf );
688 /*
689 * In case the extension is malformed, return an error,
690 * and clear the allocated sequences.
691 */
692 if( ret != 0 && ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
693 {
694 mbedtls_x509_sequence *seq_cur = subject_alt_name->next;
695 mbedtls_x509_sequence *seq_prv;
696 while( seq_cur != NULL )
697 {
698 seq_prv = seq_cur;
699 seq_cur = seq_cur->next;
700 mbedtls_platform_zeroize( seq_prv,
701 sizeof( mbedtls_x509_sequence ) );
702 mbedtls_free( seq_prv );
703 }
Ron Eldor5aebeeb2019-05-22 16:41:21 +0300704 subject_alt_name->next = NULL;
Ron Eldordbbd9662019-05-15 15:46:03 +0300705 return( ret );
706 }
707
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200708 /* Allocate and assign next pointer */
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200709 if( cur->buf.p != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200710 {
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100711 if( cur->next != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100713
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200714 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200715
716 if( cur->next == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100717 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
718 MBEDTLS_ERR_ASN1_ALLOC_FAILED ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200719
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200720 cur = cur->next;
721 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200722
723 buf = &(cur->buf);
724 buf->tag = tag;
725 buf->p = *p;
726 buf->len = tag_len;
727 *p += buf->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200728 }
729
730 /* Set final sequence entry's next pointer to NULL */
731 cur->next = NULL;
732
733 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100734 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
735 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200736
737 return( 0 );
738}
739
740/*
Ron Eldor74d9acc2019-03-21 14:00:03 +0200741 * id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 }
742 *
743 * anyPolicy OBJECT IDENTIFIER ::= { id-ce-certificatePolicies 0 }
744 *
745 * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
746 *
747 * PolicyInformation ::= SEQUENCE {
748 * policyIdentifier CertPolicyId,
749 * policyQualifiers SEQUENCE SIZE (1..MAX) OF
750 * PolicyQualifierInfo OPTIONAL }
751 *
752 * CertPolicyId ::= OBJECT IDENTIFIER
753 *
754 * PolicyQualifierInfo ::= SEQUENCE {
755 * policyQualifierId PolicyQualifierId,
756 * qualifier ANY DEFINED BY policyQualifierId }
757 *
758 * -- policyQualifierIds for Internet policy qualifiers
759 *
760 * id-qt OBJECT IDENTIFIER ::= { id-pkix 2 }
761 * id-qt-cps OBJECT IDENTIFIER ::= { id-qt 1 }
762 * id-qt-unotice OBJECT IDENTIFIER ::= { id-qt 2 }
763 *
764 * PolicyQualifierId ::= OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice )
765 *
766 * Qualifier ::= CHOICE {
767 * cPSuri CPSuri,
768 * userNotice UserNotice }
769 *
770 * CPSuri ::= IA5String
771 *
772 * UserNotice ::= SEQUENCE {
773 * noticeRef NoticeReference OPTIONAL,
774 * explicitText DisplayText OPTIONAL }
775 *
776 * NoticeReference ::= SEQUENCE {
777 * organization DisplayText,
778 * noticeNumbers SEQUENCE OF INTEGER }
779 *
780 * DisplayText ::= CHOICE {
781 * ia5String IA5String (SIZE (1..200)),
782 * visibleString VisibleString (SIZE (1..200)),
783 * bmpString BMPString (SIZE (1..200)),
784 * utf8String UTF8String (SIZE (1..200)) }
785 *
786 * NOTE: we only parse and use anyPolicy without qualifiers at this point
787 * as defined in RFC 5280.
788 */
789static int x509_get_certificate_policies( unsigned char **p,
790 const unsigned char *end,
791 mbedtls_x509_sequence *certificate_policies )
792{
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300793 int ret, parse_ret = 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200794 size_t len;
795 mbedtls_asn1_buf *buf;
796 mbedtls_asn1_sequence *cur = certificate_policies;
797
798 /* Get main sequence tag */
799 ret = mbedtls_asn1_get_tag( p, end, &len,
800 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
801 if( ret != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100802 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200803
804 if( *p + len != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100805 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
806 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200807
808 /*
809 * Cannot be an empty sequence.
810 */
811 if( len == 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100812 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
813 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200814
815 while( *p < end )
816 {
817 mbedtls_x509_buf policy_oid;
818 const unsigned char *policy_end;
819
820 /*
821 * Get the policy sequence
822 */
823 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
824 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 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_end = *p + len;
828
Ron Eldor08063792019-05-13 16:38:39 +0300829 if( ( ret = mbedtls_asn1_get_tag( p, policy_end, &len,
Ron Eldor74d9acc2019-03-21 14:00:03 +0200830 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100831 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200832
833 policy_oid.tag = MBEDTLS_ASN1_OID;
834 policy_oid.len = len;
835 policy_oid.p = *p;
836
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300837 /*
838 * Only AnyPolicy is currently supported when enforcing policy.
839 */
840 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_POLICY, &policy_oid ) != 0 )
841 {
842 /*
843 * Set the parsing return code but continue parsing, in case this
TRodziewicz3ecb92e2021-05-11 18:22:05 +0200844 * extension is critical.
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300845 */
846 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
847 }
848
Ron Eldor74d9acc2019-03-21 14:00:03 +0200849 /* Allocate and assign next pointer */
850 if( cur->buf.p != NULL )
851 {
852 if( cur->next != NULL )
853 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
854
855 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
856
857 if( cur->next == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100858 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
859 MBEDTLS_ERR_ASN1_ALLOC_FAILED ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200860
861 cur = cur->next;
862 }
863
864 buf = &( cur->buf );
865 buf->tag = policy_oid.tag;
866 buf->p = policy_oid.p;
867 buf->len = policy_oid.len;
Ron Eldor08063792019-05-13 16:38:39 +0300868
869 *p += len;
870
871 /*
872 * If there is an optional qualifier, then *p < policy_end
873 * Check the Qualifier len to verify it doesn't exceed policy_end.
874 */
875 if( *p < policy_end )
876 {
877 if( ( ret = mbedtls_asn1_get_tag( p, policy_end, &len,
878 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100879 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor08063792019-05-13 16:38:39 +0300880 /*
881 * Skip the optional policy qualifiers.
882 */
883 *p += len;
884 }
885
886 if( *p != policy_end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100887 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
888 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200889 }
890
891 /* Set final sequence entry's next pointer to NULL */
892 cur->next = NULL;
893
894 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100895 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
896 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200897
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300898 return( parse_ret );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200899}
900
901/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200902 * X.509 v3 extensions
903 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200904 */
905static int x509_get_crt_ext( unsigned char **p,
906 const unsigned char *end,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200907 mbedtls_x509_crt *crt,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200908 mbedtls_x509_crt_ext_cb_t cb,
909 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200910{
Janos Follath865b3eb2019-12-16 11:46:15 +0000911 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200912 size_t len;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200913 unsigned char *end_ext_data, *start_ext_octet, *end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200914
Hanno Becker12f62fb2019-02-12 17:22:36 +0000915 if( *p == end )
916 return( 0 );
917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918 if( ( ret = mbedtls_x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200919 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200920
Hanno Becker12f62fb2019-02-12 17:22:36 +0000921 end = crt->v3_ext.p + crt->v3_ext.len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200922 while( *p < end )
923 {
924 /*
925 * Extension ::= SEQUENCE {
926 * extnID OBJECT IDENTIFIER,
927 * critical BOOLEAN DEFAULT FALSE,
928 * extnValue OCTET STRING }
929 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930 mbedtls_x509_buf extn_oid = {0, 0, NULL};
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200931 int is_critical = 0; /* DEFAULT FALSE */
932 int ext_type = 0;
933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200934 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
935 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100936 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200937
938 end_ext_data = *p + len;
939
940 /* Get extension ID */
k-stachowiakdcae78a2018-06-28 16:32:54 +0200941 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &extn_oid.len,
k-stachowiak463928a2018-07-24 12:50:59 +0200942 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100943 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200944
k-stachowiak463928a2018-07-24 12:50:59 +0200945 extn_oid.tag = MBEDTLS_ASN1_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200946 extn_oid.p = *p;
947 *p += extn_oid.len;
948
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200949 /* Get optional critical */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950 if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
951 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
Chris Jones9f7a6932021-04-14 12:12:09 +0100952 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200953
954 /* Data should be octet string type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
956 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100957 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200958
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200959 start_ext_octet = *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200960 end_ext_octet = *p + len;
961
962 if( end_ext_octet != end_ext_data )
Chris Jones9f7a6932021-04-14 12:12:09 +0100963 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
964 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200965
966 /*
967 * Detect supported extensions
968 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200970
971 if( ret != 0 )
972 {
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200973 /* Give the callback (if any) a chance to handle the extension */
Nicola Di Lieto2c3a9172020-05-28 17:20:42 +0200974 if( cb != NULL )
975 {
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200976 ret = cb( p_ctx, crt, &extn_oid, is_critical, *p, end_ext_octet );
Nicola Di Lieto565b52b2020-05-29 22:46:56 +0200977 if( ret != 0 && is_critical )
978 return( ret );
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200979 *p = end_ext_octet;
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200980 continue;
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200981 }
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200982
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200983 /* No parser found, skip extension */
984 *p = end_ext_octet;
985
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200986 if( is_critical )
987 {
988 /* Data is marked as critical: fail */
Chris Jones9f7a6932021-04-14 12:12:09 +0100989 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
990 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200991 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200992 continue;
993 }
994
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100995 /* Forbid repeated extensions */
996 if( ( crt->ext_types & ext_type ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100998
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200999 crt->ext_types |= ext_type;
1000
1001 switch( ext_type )
1002 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001003 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001004 /* Parse basic constraints */
1005 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
1006 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001007 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001008 break;
1009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001011 /* Parse key usage */
1012 if( ( ret = x509_get_key_usage( p, end_ext_octet,
1013 &crt->key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001014 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001015 break;
1016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001018 /* Parse extended key usage */
1019 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
1020 &crt->ext_key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001021 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001022 break;
1023
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001024 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001025 /* Parse subject alt name */
1026 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
1027 &crt->subject_alt_names ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001028 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001029 break;
1030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001032 /* Parse netscape certificate type */
1033 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
1034 &crt->ns_cert_type ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001035 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001036 break;
1037
Ron Eldor74d9acc2019-03-21 14:00:03 +02001038 case MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES:
1039 /* Parse certificate policies type */
1040 if( ( ret = x509_get_certificate_policies( p, end_ext_octet,
1041 &crt->certificate_policies ) ) != 0 )
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001042 {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +02001043 /* Give the callback (if any) a chance to handle the extension
1044 * if it contains unsupported policies */
1045 if( ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE && cb != NULL &&
1046 cb( p_ctx, crt, &extn_oid, is_critical,
1047 start_ext_octet, end_ext_octet ) == 0 )
1048 break;
1049
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001050 if( is_critical )
1051 return( ret );
1052 else
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001053 /*
Ron Eldora2913912019-05-16 16:17:38 +03001054 * If MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE is returned, then we
1055 * cannot interpret or enforce the policy. However, it is up to
1056 * the user to choose how to enforce the policies,
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001057 * unless the extension is critical.
1058 */
1059 if( ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1060 return( ret );
1061 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001062 break;
1063
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001064 default:
Ron Eldordf48efa2019-04-08 13:28:24 +03001065 /*
1066 * If this is a non-critical extension, which the oid layer
1067 * supports, but there isn't an x509 parser for it,
1068 * skip the extension.
1069 */
Ron Eldordf48efa2019-04-08 13:28:24 +03001070 if( is_critical )
1071 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1072 else
Ron Eldordf48efa2019-04-08 13:28:24 +03001073 *p = end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001074 }
1075 }
1076
1077 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +01001078 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1079 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001080
1081 return( 0 );
1082}
1083
1084/*
1085 * Parse and fill a single X.509 certificate in DER format
1086 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001087static int x509_crt_parse_der_core( mbedtls_x509_crt *crt,
1088 const unsigned char *buf,
1089 size_t buflen,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001090 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001091 mbedtls_x509_crt_ext_cb_t cb,
1092 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001093{
Janos Follath865b3eb2019-12-16 11:46:15 +00001094 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001095 size_t len;
1096 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001099 memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) );
1100 memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) );
1101 memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001102
1103 /*
1104 * Check for valid input
1105 */
1106 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001107 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001108
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001109 /* Use the original buffer until we figure out actual length. */
Janos Follathcc0e49d2016-02-17 14:34:12 +00001110 p = (unsigned char*) buf;
1111 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001112 end = p + len;
1113
1114 /*
1115 * Certificate ::= SEQUENCE {
1116 * tbsCertificate TBSCertificate,
1117 * signatureAlgorithm AlgorithmIdentifier,
1118 * signatureValue BIT STRING }
1119 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001120 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1121 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001122 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001123 mbedtls_x509_crt_free( crt );
1124 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001125 }
1126
Janos Follathcc0e49d2016-02-17 14:34:12 +00001127 end = crt_end = p + len;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001128 crt->raw.len = crt_end - buf;
1129 if( make_copy != 0 )
1130 {
1131 /* Create and populate a new buffer for the raw field. */
1132 crt->raw.p = p = mbedtls_calloc( 1, crt->raw.len );
1133 if( crt->raw.p == NULL )
1134 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
1135
1136 memcpy( crt->raw.p, buf, crt->raw.len );
1137 crt->own_buffer = 1;
1138
1139 p += crt->raw.len - len;
1140 end = crt_end = p + len;
1141 }
1142 else
1143 {
1144 crt->raw.p = (unsigned char*) buf;
1145 crt->own_buffer = 0;
1146 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001147
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001148 /*
1149 * TBSCertificate ::= SEQUENCE {
1150 */
1151 crt->tbs.p = p;
1152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001153 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1154 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001155 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001156 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001157 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001158 }
1159
1160 end = p + len;
1161 crt->tbs.len = end - crt->tbs.p;
1162
1163 /*
1164 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1165 *
1166 * CertificateSerialNumber ::= INTEGER
1167 *
1168 * signature AlgorithmIdentifier
1169 */
1170 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171 ( ret = mbedtls_x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1172 ( ret = mbedtls_x509_get_alg( &p, end, &crt->sig_oid,
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001173 &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001174 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001176 return( ret );
1177 }
1178
Andres AG7ca4a032017-03-09 16:16:11 +00001179 if( crt->version < 0 || crt->version > 2 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001180 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001181 mbedtls_x509_crt_free( crt );
1182 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001183 }
1184
Andres AG7ca4a032017-03-09 16:16:11 +00001185 crt->version++;
1186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187 if( ( ret = mbedtls_x509_get_sig_alg( &crt->sig_oid, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02001188 &crt->sig_md, &crt->sig_pk,
1189 &crt->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001190 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001192 return( ret );
1193 }
1194
1195 /*
1196 * issuer Name
1197 */
1198 crt->issuer_raw.p = p;
1199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001200 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1201 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001202 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001204 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001205 }
1206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001207 if( ( ret = mbedtls_x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001208 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001210 return( ret );
1211 }
1212
1213 crt->issuer_raw.len = p - crt->issuer_raw.p;
1214
1215 /*
1216 * Validity ::= SEQUENCE {
1217 * notBefore Time,
1218 * notAfter Time }
1219 *
1220 */
1221 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1222 &crt->valid_to ) ) != 0 )
1223 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001224 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001225 return( ret );
1226 }
1227
1228 /*
1229 * subject Name
1230 */
1231 crt->subject_raw.p = p;
1232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001233 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1234 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001235 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001236 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001237 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001238 }
1239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001240 if( len && ( ret = mbedtls_x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001241 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001242 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001243 return( ret );
1244 }
1245
1246 crt->subject_raw.len = p - crt->subject_raw.p;
1247
1248 /*
1249 * SubjectPublicKeyInfo
1250 */
Hanno Becker494dd7a2019-02-06 16:13:41 +00001251 crt->pk_raw.p = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001252 if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001255 return( ret );
1256 }
Hanno Becker494dd7a2019-02-06 16:13:41 +00001257 crt->pk_raw.len = p - crt->pk_raw.p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001258
1259 /*
1260 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1261 * -- If present, version shall be v2 or v3
1262 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1263 * -- If present, version shall be v2 or v3
1264 * extensions [3] EXPLICIT Extensions OPTIONAL
1265 * -- If present, version shall be v3
1266 */
1267 if( crt->version == 2 || crt->version == 3 )
1268 {
1269 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1270 if( ret != 0 )
1271 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001272 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001273 return( ret );
1274 }
1275 }
1276
1277 if( crt->version == 2 || crt->version == 3 )
1278 {
1279 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1280 if( ret != 0 )
1281 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001282 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001283 return( ret );
1284 }
1285 }
1286
1287 if( crt->version == 3 )
Manuel Pégourié-Gonnarda252af72015-03-27 16:15:55 +01001288 {
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001289 ret = x509_get_crt_ext( &p, end, crt, cb, p_ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001290 if( ret != 0 )
1291 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001293 return( ret );
1294 }
1295 }
1296
1297 if( p != end )
1298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001299 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001300 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT,
1301 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001302 }
1303
1304 end = crt_end;
1305
1306 /*
1307 * }
1308 * -- end of TBSCertificate
1309 *
1310 * signatureAlgorithm AlgorithmIdentifier,
1311 * signatureValue BIT STRING
1312 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313 if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001314 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001316 return( ret );
1317 }
1318
Manuel Pégourié-Gonnard1022fed2015-03-27 16:30:47 +01001319 if( crt->sig_oid.len != sig_oid2.len ||
1320 memcmp( crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len ) != 0 ||
Paul Elliottca17ebf2020-11-24 17:30:18 +00001321 sig_params1.tag != sig_params2.tag ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001322 sig_params1.len != sig_params2.len ||
Manuel Pégourié-Gonnard159c5242015-04-30 11:15:22 +02001323 ( sig_params1.len != 0 &&
1324 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001325 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326 mbedtls_x509_crt_free( crt );
1327 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001328 }
1329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330 if( ( ret = mbedtls_x509_get_sig( &p, end, &crt->sig ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001333 return( ret );
1334 }
1335
1336 if( p != end )
1337 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001338 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001339 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT,
1340 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001341 }
1342
1343 return( 0 );
1344}
1345
1346/*
1347 * Parse one X.509 certificate in DER format from a buffer and add them to a
1348 * chained list
1349 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001350static int mbedtls_x509_crt_parse_der_internal( mbedtls_x509_crt *chain,
1351 const unsigned char *buf,
1352 size_t buflen,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001353 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001354 mbedtls_x509_crt_ext_cb_t cb,
1355 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001356{
Janos Follath865b3eb2019-12-16 11:46:15 +00001357 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001358 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001359
1360 /*
1361 * Check for valid input
1362 */
1363 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001364 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001365
1366 while( crt->version != 0 && crt->next != NULL )
1367 {
1368 prev = crt;
1369 crt = crt->next;
1370 }
1371
1372 /*
1373 * Add new certificate on the end of the chain if needed.
1374 */
Paul Bakker66d5d072014-06-17 16:39:18 +02001375 if( crt->version != 0 && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001376 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001377 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001378
1379 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001380 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001381
1382 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001384 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001385 }
1386
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001387 ret = x509_crt_parse_der_core( crt, buf, buflen, make_copy, cb, p_ctx );
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001388 if( ret != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001389 {
1390 if( prev )
1391 prev->next = NULL;
1392
1393 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001394 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001395
1396 return( ret );
1397 }
1398
1399 return( 0 );
1400}
1401
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001402int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
1403 const unsigned char *buf,
1404 size_t buflen )
1405{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001406 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 0, NULL, NULL ) );
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001407}
1408
Nicola Di Lietofde98f72020-05-28 08:34:33 +02001409int mbedtls_x509_crt_parse_der_with_ext_cb( mbedtls_x509_crt *chain,
1410 const unsigned char *buf,
1411 size_t buflen,
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +02001412 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001413 mbedtls_x509_crt_ext_cb_t cb,
1414 void *p_ctx )
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001415{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001416 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, make_copy, cb, p_ctx ) );
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001417}
1418
1419int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
1420 const unsigned char *buf,
1421 size_t buflen )
1422{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001423 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 1, NULL, NULL ) );
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001424}
1425
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001426/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001427 * Parse one or more PEM certificates from a buffer and add them to the chained
1428 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001429 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001430int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain,
1431 const unsigned char *buf,
1432 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001433{
Janos Follath98e28a72016-05-31 14:03:54 +01001434#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001435 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001437#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001438
1439 /*
1440 * Check for valid input
1441 */
1442 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001443 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001444
1445 /*
1446 * Determine buffer content. Buffer contains either one DER certificate or
1447 * one or more PEM certificates.
1448 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001450 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001451 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1452 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001453 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001454 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456 if( buf_format == MBEDTLS_X509_FORMAT_DER )
1457 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Janos Follath98e28a72016-05-31 14:03:54 +01001458#else
1459 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
1460#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001462#if defined(MBEDTLS_PEM_PARSE_C)
1463 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001464 {
Janos Follath865b3eb2019-12-16 11:46:15 +00001465 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001467
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001468 /* 1 rather than 0 since the terminating NULL byte is counted in */
1469 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001470 {
1471 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001472 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001473
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001474 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001475 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001476 "-----BEGIN CERTIFICATE-----",
1477 "-----END CERTIFICATE-----",
1478 buf, NULL, 0, &use_len );
1479
1480 if( ret == 0 )
1481 {
1482 /*
1483 * Was PEM encoded
1484 */
1485 buflen -= use_len;
1486 buf += use_len;
1487 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001488 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001489 {
1490 return( ret );
1491 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001493 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001494 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001495
1496 /*
1497 * PEM header and footer were found
1498 */
1499 buflen -= use_len;
1500 buf += use_len;
1501
1502 if( first_error == 0 )
1503 first_error = ret;
1504
Paul Bakker5a5fa922014-09-26 14:53:04 +02001505 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001506 continue;
1507 }
1508 else
1509 break;
1510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001511 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001513 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001514
1515 if( ret != 0 )
1516 {
1517 /*
1518 * Quit parsing on a memory error
1519 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001520 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001521 return( ret );
1522
1523 if( first_error == 0 )
1524 first_error = ret;
1525
1526 total_failed++;
1527 continue;
1528 }
1529
1530 success = 1;
1531 }
1532 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001533
1534 if( success )
1535 return( total_failed );
1536 else if( first_error )
1537 return( first_error );
1538 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001539 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Janos Follath98e28a72016-05-31 14:03:54 +01001540#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001541}
1542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001543#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001544/*
1545 * Load one or more certificates and add them to the chained list
1546 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001548{
Janos Follath865b3eb2019-12-16 11:46:15 +00001549 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001550 size_t n;
1551 unsigned char *buf;
1552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001554 return( ret );
1555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001556 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001557
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001558 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001559 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001560
1561 return( ret );
1562}
1563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001564int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001565{
1566 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001567#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001568 int w_ret;
1569 WCHAR szDir[MAX_PATH];
1570 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001571 char *p;
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001572 size_t len = strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001573
Paul Bakker9af723c2014-05-01 13:03:14 +02001574 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001575 HANDLE hFind;
1576
1577 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001579
Paul Bakker9af723c2014-05-01 13:03:14 +02001580 memset( szDir, 0, sizeof(szDir) );
1581 memset( filename, 0, MAX_PATH );
1582 memcpy( filename, path, len );
1583 filename[len++] = '\\';
1584 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001585 filename[len++] = '*';
1586
Simon B3c6b18d2016-11-03 01:11:37 +00001587 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001588 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001589 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001590 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001591
1592 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001593 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001594 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001595
1596 len = MAX_PATH - len;
1597 do
1598 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001599 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001600
1601 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1602 continue;
1603
Paul Bakker9af723c2014-05-01 13:03:14 +02001604 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001605 lstrlenW( file_data.cFileName ),
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001606 p, (int) len - 1,
Paul Bakker9af723c2014-05-01 13:03:14 +02001607 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001608 if( w_ret == 0 )
Ron Eldor36d90422017-01-09 15:09:16 +02001609 {
1610 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1611 goto cleanup;
1612 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001614 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001615 if( w_ret < 0 )
1616 ret++;
1617 else
1618 ret += w_ret;
1619 }
1620 while( FindNextFileW( hFind, &file_data ) != 0 );
1621
Paul Bakker66d5d072014-06-17 16:39:18 +02001622 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001623 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001624
Ron Eldor36d90422017-01-09 15:09:16 +02001625cleanup:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001626 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001627#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001628 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001629 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001630 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001631 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001632 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001633 DIR *dir = opendir( path );
1634
Paul Bakker66d5d072014-06-17 16:39:18 +02001635 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001636 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001637
Ron Eldor63140682017-01-09 19:27:59 +02001638#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001639 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001640 {
1641 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001642 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001643 }
Ron Eldor63140682017-01-09 19:27:59 +02001644#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001645
Paul Elliottfb91a482021-03-05 14:17:51 +00001646 memset( &sb, 0, sizeof( sb ) );
1647
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001648 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001649 {
Andres AGf9113192016-09-02 14:06:04 +01001650 snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name,
1651 "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001652
Andres AGf9113192016-09-02 14:06:04 +01001653 if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001654 {
Andres AGf9113192016-09-02 14:06:04 +01001655 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1656 goto cleanup;
1657 }
1658 else if( stat( entry_name, &sb ) == -1 )
1659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001660 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001661 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001662 }
1663
1664 if( !S_ISREG( sb.st_mode ) )
1665 continue;
1666
1667 // Ignore parse errors
1668 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001669 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001670 if( t_ret < 0 )
1671 ret++;
1672 else
1673 ret += t_ret;
1674 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001675
1676cleanup:
Andres AGf9113192016-09-02 14:06:04 +01001677 closedir( dir );
1678
Ron Eldor63140682017-01-09 19:27:59 +02001679#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001680 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001681 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Ron Eldor63140682017-01-09 19:27:59 +02001682#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001683
Paul Bakkerbe089b02013-10-14 15:51:50 +02001684#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001685
1686 return( ret );
1687}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001688#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001689
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001690/*
1691 * OtherName ::= SEQUENCE {
1692 * type-id OBJECT IDENTIFIER,
1693 * value [0] EXPLICIT ANY DEFINED BY type-id }
1694 *
1695 * HardwareModuleName ::= SEQUENCE {
1696 * hwType OBJECT IDENTIFIER,
1697 * hwSerialNum OCTET STRING }
1698 *
1699 * NOTE: we currently only parse and use otherName of type HwModuleName,
1700 * as defined in RFC 4108.
1701 */
1702static int x509_get_other_name( const mbedtls_x509_buf *subject_alt_name,
1703 mbedtls_x509_san_other_name *other_name )
1704{
Janos Follathab23cd12019-05-09 13:53:57 +01001705 int ret = 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001706 size_t len;
1707 unsigned char *p = subject_alt_name->p;
1708 const unsigned char *end = p + subject_alt_name->len;
1709 mbedtls_x509_buf cur_oid;
1710
1711 if( ( subject_alt_name->tag &
1712 ( MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK ) ) !=
1713 ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ) )
1714 {
1715 /*
1716 * The given subject alternative name is not of type "othername".
1717 */
1718 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
1719 }
1720
1721 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1722 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001723 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001724
1725 cur_oid.tag = MBEDTLS_ASN1_OID;
1726 cur_oid.p = p;
1727 cur_oid.len = len;
1728
1729 /*
1730 * Only HwModuleName is currently supported.
1731 */
1732 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid ) != 0 )
1733 {
1734 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1735 }
1736
Ron Eldor890819a2019-05-13 19:03:04 +03001737 if( p + len >= end )
1738 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001739 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001740 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1741 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001742 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001743 p += len;
1744 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1745 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001746 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001747
1748 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1749 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001750 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001751
1752 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001753 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001754
1755 other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID;
1756 other_name->value.hardware_module_name.oid.p = p;
1757 other_name->value.hardware_module_name.oid.len = len;
1758
Ron Eldor890819a2019-05-13 19:03:04 +03001759 if( p + len >= end )
1760 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001761 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001762 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1763 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001764 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001765 p += len;
1766 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1767 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001768 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001769
1770 other_name->value.hardware_module_name.val.tag = MBEDTLS_ASN1_OCTET_STRING;
1771 other_name->value.hardware_module_name.val.p = p;
1772 other_name->value.hardware_module_name.val.len = len;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001773 p += len;
1774 if( p != end )
1775 {
Ron Eldor890819a2019-05-13 19:03:04 +03001776 mbedtls_platform_zeroize( other_name,
Sébastien Duquette661d7252019-06-23 17:45:26 -04001777 sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001778 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1779 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001780 }
1781 return( 0 );
1782}
1783
Peter Kolbus9a969b62018-12-11 13:55:56 -06001784int mbedtls_x509_parse_subject_alt_name( const mbedtls_x509_buf *san_buf,
1785 mbedtls_x509_subject_alternative_name *san )
1786{
1787 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1788 switch( san_buf->tag &
1789 ( MBEDTLS_ASN1_TAG_CLASS_MASK |
1790 MBEDTLS_ASN1_TAG_VALUE_MASK ) )
1791 {
1792 /*
1793 * otherName
1794 */
1795 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ):
1796 {
1797 mbedtls_x509_san_other_name other_name;
1798
1799 ret = x509_get_other_name( san_buf, &other_name );
1800 if( ret != 0 )
1801 return( ret );
1802
1803 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1804 san->type = MBEDTLS_X509_SAN_OTHER_NAME;
1805 memcpy( &san->san.other_name,
1806 &other_name, sizeof( other_name ) );
1807
1808 }
1809 break;
1810
1811 /*
1812 * dNSName
1813 */
1814 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME ):
1815 {
1816 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1817 san->type = MBEDTLS_X509_SAN_DNS_NAME;
1818
1819 memcpy( &san->san.unstructured_name,
1820 san_buf, sizeof( *san_buf ) );
1821
1822 }
1823 break;
1824
1825 /*
1826 * Type not supported
1827 */
1828 default:
1829 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1830 }
1831 return( 0 );
1832}
1833
1834#if !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001835static int x509_info_subject_alt_name( char **buf, size_t *size,
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001836 const mbedtls_x509_sequence
1837 *subject_alt_name,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001838 const char *prefix )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001839{
Janos Follath865b3eb2019-12-16 11:46:15 +00001840 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001841 size_t n = *size;
1842 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001843 const mbedtls_x509_sequence *cur = subject_alt_name;
Ron Eldor890819a2019-05-13 19:03:04 +03001844 mbedtls_x509_subject_alternative_name san;
1845 int parse_ret;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001846
1847 while( cur != NULL )
1848 {
Ron Eldor890819a2019-05-13 19:03:04 +03001849 memset( &san, 0, sizeof( san ) );
1850 parse_ret = mbedtls_x509_parse_subject_alt_name( &cur->buf, &san );
1851 if( parse_ret != 0 )
1852 {
1853 if( parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1854 {
1855 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
1856 MBEDTLS_X509_SAFE_SNPRINTF;
1857 }
1858 else
1859 {
1860 ret = mbedtls_snprintf( p, n, "\n%s <malformed>", prefix );
1861 MBEDTLS_X509_SAFE_SNPRINTF;
1862 }
1863 cur = cur->next;
1864 continue;
1865 }
1866
1867 switch( san.type )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001868 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001869 /*
1870 * otherName
1871 */
Ron Eldora2913912019-05-16 16:17:38 +03001872 case MBEDTLS_X509_SAN_OTHER_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001873 {
1874 mbedtls_x509_san_other_name *other_name = &san.san.other_name;
1875
1876 ret = mbedtls_snprintf( p, n, "\n%s otherName :", prefix );
1877 MBEDTLS_X509_SAFE_SNPRINTF;
1878
1879 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME,
1880 &other_name->value.hardware_module_name.oid ) != 0 )
Janos Follath22f605f2019-05-10 10:37:17 +01001881 {
Ron Eldor890819a2019-05-13 19:03:04 +03001882 ret = mbedtls_snprintf( p, n, "\n%s hardware module name :", prefix );
1883 MBEDTLS_X509_SAFE_SNPRINTF;
1884 ret = mbedtls_snprintf( p, n, "\n%s hardware type : ", prefix );
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001885 MBEDTLS_X509_SAFE_SNPRINTF;
1886
Ron Eldor890819a2019-05-13 19:03:04 +03001887 ret = mbedtls_oid_get_numeric_string( p, n, &other_name->value.hardware_module_name.oid );
1888 MBEDTLS_X509_SAFE_SNPRINTF;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001889
Ron Eldor890819a2019-05-13 19:03:04 +03001890 ret = mbedtls_snprintf( p, n, "\n%s hardware serial number : ", prefix );
1891 MBEDTLS_X509_SAFE_SNPRINTF;
1892
1893 if( other_name->value.hardware_module_name.val.len >= n )
1894 {
1895 *p = '\0';
1896 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Janos Follath22f605f2019-05-10 10:37:17 +01001897 }
1898
Ron Eldora2913912019-05-16 16:17:38 +03001899 memcpy( p, other_name->value.hardware_module_name.val.p,
1900 other_name->value.hardware_module_name.val.len );
1901 p += other_name->value.hardware_module_name.val.len;
1902
Ron Eldor890819a2019-05-13 19:03:04 +03001903 n -= other_name->value.hardware_module_name.val.len;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001904
Ron Eldor890819a2019-05-13 19:03:04 +03001905 }/* MBEDTLS_OID_ON_HW_MODULE_NAME */
1906 }
1907 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001908
1909 /*
1910 * dNSName
1911 */
Ron Eldora2913912019-05-16 16:17:38 +03001912 case MBEDTLS_X509_SAN_DNS_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001913 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001914 ret = mbedtls_snprintf( p, n, "\n%s dNSName : ", prefix );
1915 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldor890819a2019-05-13 19:03:04 +03001916 if( san.san.unstructured_name.len >= n )
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001917 {
1918 *p = '\0';
1919 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
1920 }
Ron Eldora2913912019-05-16 16:17:38 +03001921
1922 memcpy( p, san.san.unstructured_name.p, san.san.unstructured_name.len );
1923 p += san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001924 n -= san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001925 }
1926 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001927
1928 /*
Ron Eldor890819a2019-05-13 19:03:04 +03001929 * Type not supported, skip item.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001930 */
1931 default:
Janos Follath22f605f2019-05-10 10:37:17 +01001932 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
1933 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001934 break;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001935 }
1936
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001937 cur = cur->next;
1938 }
1939
1940 *p = '\0';
1941
1942 *size = n;
1943 *buf = p;
1944
1945 return( 0 );
1946}
1947
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001948#define PRINT_ITEM(i) \
1949 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001950 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001951 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001952 sep = ", "; \
1953 }
1954
1955#define CERT_TYPE(type,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001956 if( ns_cert_type & (type) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001957 PRINT_ITEM( name );
1958
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001959static int x509_info_cert_type( char **buf, size_t *size,
1960 unsigned char ns_cert_type )
1961{
Janos Follath865b3eb2019-12-16 11:46:15 +00001962 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001963 size_t n = *size;
1964 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001965 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001967 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001968 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001969 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
1970 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1971 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001972 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
1973 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1974 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001975
1976 *size = n;
1977 *buf = p;
1978
1979 return( 0 );
1980}
1981
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001982#define KEY_USAGE(code,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001983 if( key_usage & (code) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001984 PRINT_ITEM( name );
1985
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001986static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001987 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001988{
Janos Follath865b3eb2019-12-16 11:46:15 +00001989 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001990 size_t n = *size;
1991 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001992 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001994 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
1995 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001996 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1997 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1998 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001999 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
2000 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02002001 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
2002 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002003
2004 *size = n;
2005 *buf = p;
2006
2007 return( 0 );
2008}
2009
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002010static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002011 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002012{
Janos Follath865b3eb2019-12-16 11:46:15 +00002013 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002014 const char *desc;
2015 size_t n = *size;
2016 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002017 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002018 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002019
2020 while( cur != NULL )
2021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002022 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002023 desc = "???";
2024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002025 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002026 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002027
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002028 sep = ", ";
2029
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002030 cur = cur->next;
2031 }
2032
2033 *size = n;
2034 *buf = p;
2035
2036 return( 0 );
2037}
2038
Ron Eldor74d9acc2019-03-21 14:00:03 +02002039static int x509_info_cert_policies( char **buf, size_t *size,
2040 const mbedtls_x509_sequence *certificate_policies )
2041{
Janos Follath865b3eb2019-12-16 11:46:15 +00002042 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor74d9acc2019-03-21 14:00:03 +02002043 const char *desc;
2044 size_t n = *size;
2045 char *p = *buf;
2046 const mbedtls_x509_sequence *cur = certificate_policies;
2047 const char *sep = "";
2048
2049 while( cur != NULL )
2050 {
2051 if( mbedtls_oid_get_certificate_policies( &cur->buf, &desc ) != 0 )
2052 desc = "???";
2053
2054 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
2055 MBEDTLS_X509_SAFE_SNPRINTF;
2056
2057 sep = ", ";
2058
2059 cur = cur->next;
2060 }
2061
2062 *size = n;
2063 *buf = p;
2064
2065 return( 0 );
2066}
2067
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002068/*
2069 * Return an informational string about the certificate.
2070 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002071#define BEFORE_COLON 18
2072#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002073int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
2074 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002075{
Janos Follath865b3eb2019-12-16 11:46:15 +00002076 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002077 size_t n;
2078 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002079 char key_size_str[BEFORE_COLON];
2080
2081 p = buf;
2082 n = size;
2083
Janos Follath98e28a72016-05-31 14:03:54 +01002084 if( NULL == crt )
2085 {
2086 ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" );
2087 MBEDTLS_X509_SAFE_SNPRINTF;
2088
2089 return( (int) ( size - n ) );
2090 }
2091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002092 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002093 prefix, crt->version );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002094 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002095 ret = mbedtls_snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002096 prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002097 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002099 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002100 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002102 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002103 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002104 ret = mbedtls_x509_dn_gets( p, n, &crt->issuer );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002105 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002107 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002108 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002109 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002110 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002112 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002113 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2114 crt->valid_from.year, crt->valid_from.mon,
2115 crt->valid_from.day, crt->valid_from.hour,
2116 crt->valid_from.min, crt->valid_from.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002117 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002119 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002120 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2121 crt->valid_to.year, crt->valid_to.mon,
2122 crt->valid_to.day, crt->valid_to.hour,
2123 crt->valid_to.min, crt->valid_to.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002124 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002126 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002127 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002129 ret = mbedtls_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02002130 crt->sig_md, crt->sig_opts );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002131 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002132
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002133 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002134 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
2135 mbedtls_pk_get_name( &crt->pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002136 {
2137 return( ret );
2138 }
2139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002140 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +02002141 (int) mbedtls_pk_get_bitlen( &crt->pk ) );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002142 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002143
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002144 /*
2145 * Optional extensions
2146 */
2147
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002148 if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002149 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002150 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002151 crt->ca_istrue ? "true" : "false" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002152 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002153
2154 if( crt->max_pathlen > 0 )
2155 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002156 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002157 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002158 }
2159 }
2160
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002161 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002162 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002163 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name :", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002164 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002165
2166 if( ( ret = x509_info_subject_alt_name( &p, &n,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002167 &crt->subject_alt_names,
Ron Eldor6aeae9e2019-05-20 12:00:36 +03002168 prefix ) ) != 0 )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002169 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002170 }
2171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002172 if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002173 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002174 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002175 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002176
2177 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
2178 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002179 }
2180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002181 if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002182 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002183 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002184 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002185
2186 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
2187 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002188 }
2189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002190 if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002192 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002193 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002194
2195 if( ( ret = x509_info_ext_key_usage( &p, &n,
2196 &crt->ext_key_usage ) ) != 0 )
2197 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002198 }
2199
Ron Eldor74d9acc2019-03-21 14:00:03 +02002200 if( crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES )
2201 {
2202 ret = mbedtls_snprintf( p, n, "\n%scertificate policies : ", prefix );
2203 MBEDTLS_X509_SAFE_SNPRINTF;
2204
2205 if( ( ret = x509_info_cert_policies( &p, &n,
2206 &crt->certificate_policies ) ) != 0 )
2207 return( ret );
2208 }
2209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002210 ret = mbedtls_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002211 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002212
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002213 return( (int) ( size - n ) );
2214}
2215
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002216struct x509_crt_verify_string {
2217 int code;
2218 const char *string;
2219};
2220
Hanno Becker7ac83f92020-10-09 10:48:22 +01002221#define X509_CRT_ERROR_INFO( err, err_str, info ) { err, info },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002222static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Hanno Becker7ac83f92020-10-09 10:48:22 +01002223 MBEDTLS_X509_CRT_ERROR_INFO_LIST
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002224 { 0, NULL }
2225};
Hanno Becker7ac83f92020-10-09 10:48:22 +01002226#undef X509_CRT_ERROR_INFO
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002227
2228int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002229 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002230{
Janos Follath865b3eb2019-12-16 11:46:15 +00002231 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002232 const struct x509_crt_verify_string *cur;
2233 char *p = buf;
2234 size_t n = size;
2235
2236 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
2237 {
2238 if( ( flags & cur->code ) == 0 )
2239 continue;
2240
2241 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002242 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002243 flags ^= cur->code;
2244 }
2245
2246 if( flags != 0 )
2247 {
2248 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
2249 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002250 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002251 }
2252
2253 return( (int) ( size - n ) );
2254}
Hanno Becker612a2f12020-10-09 09:19:39 +01002255#endif /* MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002256
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002257int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
2258 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002259{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002260 unsigned int usage_must, usage_may;
2261 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
2262 | MBEDTLS_X509_KU_DECIPHER_ONLY;
2263
2264 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
2265 return( 0 );
2266
2267 usage_must = usage & ~may_mask;
2268
2269 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
2270 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
2271
2272 usage_may = usage & may_mask;
2273
2274 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002275 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002276
2277 return( 0 );
2278}
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002280int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002281 const char *usage_oid,
2282 size_t usage_len )
2283{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002284 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002285
2286 /* Extension is not mandatory, absent means no restriction */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002287 if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002288 return( 0 );
2289
2290 /*
2291 * Look for the requested usage (or wildcard ANY) in our list
2292 */
2293 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
2294 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002295 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002296
2297 if( cur_oid->len == usage_len &&
2298 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
2299 {
2300 return( 0 );
2301 }
2302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002303 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002304 return( 0 );
2305 }
2306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002307 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002308}
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002310#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002311/*
2312 * Return 1 if the certificate is revoked, or 0 otherwise.
2313 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002314int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002315{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002316 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002317
2318 while( cur != NULL && cur->serial.len != 0 )
2319 {
2320 if( crt->serial.len == cur->serial.len &&
2321 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
2322 {
Raoul Strackxa4e86142020-06-15 17:03:13 +02002323 return( 1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002324 }
2325
2326 cur = cur->next;
2327 }
2328
2329 return( 0 );
2330}
2331
2332/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002333 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002334 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002335 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002336static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002337 mbedtls_x509_crl *crl_list,
2338 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002339{
2340 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002341 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2342 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002343
2344 if( ca == NULL )
2345 return( flags );
2346
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002347 while( crl_list != NULL )
2348 {
2349 if( crl_list->version == 0 ||
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002350 x509_name_cmp( &crl_list->issuer, &ca->subject ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002351 {
2352 crl_list = crl_list->next;
2353 continue;
2354 }
2355
2356 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002357 * Check if the CA is configured to sign CRLs
2358 */
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002359 if( mbedtls_x509_crt_check_key_usage( ca,
2360 MBEDTLS_X509_KU_CRL_SIGN ) != 0 )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002361 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002362 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002363 break;
2364 }
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002365
2366 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002367 * Check if CRL is correctly signed by the trusted CA
2368 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002369 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
2370 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
2371
2372 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
2373 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
2374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002375 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002376 if( mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002377 {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002378 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002379 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002380 break;
2381 }
2382
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002383 if( x509_profile_check_key( profile, &ca->pk ) != 0 )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002384 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002386 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
2387 crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02002388 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002389 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002390 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002391 break;
2392 }
2393
2394 /*
2395 * Check for validity of CRL (Do not drop out)
2396 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002397 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002398 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002399
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002400 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002401 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002402
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002403 /*
2404 * Check if certificate is revoked
2405 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002406 if( mbedtls_x509_crt_is_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002407 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002408 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002409 break;
2410 }
2411
2412 crl_list = crl_list->next;
2413 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002414
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002415 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002416}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002417#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002418
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002419/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002420 * Check the signature of a certificate by its parent
2421 */
2422static int x509_crt_check_signature( const mbedtls_x509_crt *child,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002423 mbedtls_x509_crt *parent,
2424 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002425{
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002426 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002427 size_t hash_len;
2428#if !defined(MBEDTLS_USE_PSA_CRYPTO)
2429 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002430 md_info = mbedtls_md_info_from_type( child->sig_md );
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002431 hash_len = mbedtls_md_get_size( md_info );
Andrzej Kurek8b38ff52018-11-20 03:20:09 -05002432
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002433 /* Note: hash errors can happen only after an internal error */
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002434 if( mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash ) != 0 )
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002435 return( -1 );
2436#else
Jaeden Amero34973232019-02-20 10:32:28 +00002437 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002438 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( child->sig_md );
2439
2440 if( psa_hash_setup( &hash_operation, hash_alg ) != PSA_SUCCESS )
2441 return( -1 );
2442
2443 if( psa_hash_update( &hash_operation, child->tbs.p, child->tbs.len )
2444 != PSA_SUCCESS )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002445 {
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002446 return( -1 );
2447 }
2448
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002449 if( psa_hash_finish( &hash_operation, hash, sizeof( hash ), &hash_len )
2450 != PSA_SUCCESS )
2451 {
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002452 return( -1 );
2453 }
2454#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002455 /* Skip expensive computation on obvious mismatch */
2456 if( ! mbedtls_pk_can_do( &parent->pk, child->sig_pk ) )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002457 return( -1 );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002458
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002459#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002460 if( rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA )
2461 {
2462 return( mbedtls_pk_verify_restartable( &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002463 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002464 child->sig.p, child->sig.len, &rs_ctx->pk ) );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002465 }
2466#else
2467 (void) rs_ctx;
2468#endif
2469
2470 return( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002471 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002472 child->sig.p, child->sig.len ) );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002473}
2474
2475/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002476 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2477 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002478 *
2479 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002480 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002481static int x509_crt_check_parent( const mbedtls_x509_crt *child,
2482 const mbedtls_x509_crt *parent,
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002483 int top )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002484{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002485 int need_ca_bit;
2486
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002487 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02002488 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002489 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002490
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002491 /* Parent must have the basicConstraints CA bit set as a general rule */
2492 need_ca_bit = 1;
2493
2494 /* Exception: v1/v2 certificates that are locally trusted. */
2495 if( top && parent->version < 3 )
2496 need_ca_bit = 0;
2497
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002498 if( need_ca_bit && ! parent->ca_istrue )
2499 return( -1 );
2500
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002501 if( need_ca_bit &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002502 mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002503 {
2504 return( -1 );
2505 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002506
2507 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002508}
2509
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002510/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002511 * Find a suitable parent for child in candidates, or return NULL.
2512 *
2513 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002514 * 1. subject name matches child's issuer
2515 * 2. if necessary, the CA bit is set and key usage allows signing certs
2516 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002517 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002518 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002519 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002520 * If there's a suitable candidate which is also time-valid, return the first
2521 * such. Otherwise, return the first suitable candidate (or NULL if there is
2522 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002523 *
2524 * The rationale for this rule is that someone could have a list of trusted
2525 * roots with two versions on the same root with different validity periods.
2526 * (At least one user reported having such a list and wanted it to just work.)
2527 * The reason we don't just require time-validity is that generally there is
2528 * only one version, and if it's expired we want the flags to state that
2529 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002530 *
2531 * The rationale for rule 3 (signature for trusted roots) is that users might
2532 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002533 * way we select the correct one is by checking the signature (as we don't
2534 * rely on key identifier extensions). (This is one way users might choose to
2535 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002536 *
2537 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002538 * - [in] child: certificate for which we're looking for a parent
2539 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002540 * - [out] r_parent: parent found (or NULL)
2541 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002542 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2543 * of the chain, 0 otherwise
2544 * - [in] path_cnt: number of intermediates seen so far
2545 * - [in] self_cnt: number of self-signed intermediates seen so far
2546 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002547 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002548 *
2549 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002550 * - 0 on success
2551 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002552 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002553static int x509_crt_find_parent_in(
2554 mbedtls_x509_crt *child,
2555 mbedtls_x509_crt *candidates,
2556 mbedtls_x509_crt **r_parent,
2557 int *r_signature_is_good,
2558 int top,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002559 unsigned path_cnt,
2560 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002561 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002562{
Janos Follath865b3eb2019-12-16 11:46:15 +00002563 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002564 mbedtls_x509_crt *parent, *fallback_parent;
Benjamin Kier36050732019-05-30 14:49:17 -04002565 int signature_is_good = 0, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002566
2567#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002568 /* did we have something in progress? */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002569 if( rs_ctx != NULL && rs_ctx->parent != NULL )
2570 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002571 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002572 parent = rs_ctx->parent;
2573 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002574 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002575
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002576 /* clear saved state */
2577 rs_ctx->parent = NULL;
2578 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002579 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002580
2581 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002582 goto check_signature;
2583 }
2584#endif
2585
2586 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002587 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002588
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002589 for( parent = candidates; parent != NULL; parent = parent->next )
2590 {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002591 /* basic parenting skills (name, CA bit, key usage) */
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002592 if( x509_crt_check_parent( child, parent, top ) != 0 )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002593 continue;
2594
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002595 /* +1 because stored max_pathlen is 1 higher that the actual value */
2596 if( parent->max_pathlen > 0 &&
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002597 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt )
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002598 {
2599 continue;
2600 }
2601
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002602 /* Signature */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002603#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2604check_signature:
2605#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002606 ret = x509_crt_check_signature( child, parent, rs_ctx );
2607
2608#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002609 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2610 {
2611 /* save state */
2612 rs_ctx->parent = parent;
2613 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002614 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002615
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002616 return( ret );
2617 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002618#else
2619 (void) ret;
2620#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002621
2622 signature_is_good = ret == 0;
2623 if( top && ! signature_is_good )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002624 continue;
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002625
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002626 /* optional time check */
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002627 if( mbedtls_x509_time_is_past( &parent->valid_to ) ||
2628 mbedtls_x509_time_is_future( &parent->valid_from ) )
2629 {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002630 if( fallback_parent == NULL )
2631 {
2632 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002633 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002634 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002635
2636 continue;
2637 }
2638
Andy Gross1f627142019-01-30 10:25:53 -06002639 *r_parent = parent;
2640 *r_signature_is_good = signature_is_good;
2641
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002642 break;
2643 }
2644
Andy Gross1f627142019-01-30 10:25:53 -06002645 if( parent == NULL )
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002646 {
2647 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002648 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002649 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002650
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002651 return( 0 );
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002652}
2653
2654/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002655 * Find a parent in trusted CAs or the provided chain, or return NULL.
2656 *
2657 * Searches in trusted CAs first, and return the first suitable parent found
2658 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002659 *
2660 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002661 * - [in] child: certificate for which we're looking for a parent, followed
2662 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002663 * - [in] trust_ca: list of locally trusted certificates
2664 * - [out] parent: parent found (or NULL)
2665 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2666 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2667 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2668 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002669 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002670 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002671 *
2672 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002673 * - 0 on success
2674 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002675 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002676static int x509_crt_find_parent(
2677 mbedtls_x509_crt *child,
2678 mbedtls_x509_crt *trust_ca,
2679 mbedtls_x509_crt **parent,
2680 int *parent_is_trusted,
2681 int *signature_is_good,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002682 unsigned path_cnt,
2683 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002684 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002685{
Janos Follath865b3eb2019-12-16 11:46:15 +00002686 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002687 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002688
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002689 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002690
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002691#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002692 /* restore then clear saved state if we have some stored */
2693 if( rs_ctx != NULL && rs_ctx->parent_is_trusted != -1 )
2694 {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002695 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002696 rs_ctx->parent_is_trusted = -1;
2697 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002698#endif
2699
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002700 while( 1 ) {
2701 search_list = *parent_is_trusted ? trust_ca : child->next;
2702
2703 ret = x509_crt_find_parent_in( child, search_list,
2704 parent, signature_is_good,
2705 *parent_is_trusted,
2706 path_cnt, self_cnt, rs_ctx );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002707
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002708#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002709 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2710 {
2711 /* save state */
2712 rs_ctx->parent_is_trusted = *parent_is_trusted;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002713 return( ret );
2714 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002715#else
2716 (void) ret;
2717#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002718
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002719 /* stop here if found or already in second iteration */
2720 if( *parent != NULL || *parent_is_trusted == 0 )
2721 break;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002722
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002723 /* prepare second iteration */
2724 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002725 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002726
2727 /* extra precaution against mistakes in the caller */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002728 if( *parent == NULL )
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002729 {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002730 *parent_is_trusted = 0;
2731 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002732 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002733
2734 return( 0 );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002735}
2736
2737/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002738 * Check if an end-entity certificate is locally trusted
2739 *
2740 * Currently we require such certificates to be self-signed (actually only
2741 * check for self-issued as self-signatures are not checked)
2742 */
2743static int x509_crt_check_ee_locally_trusted(
2744 mbedtls_x509_crt *crt,
2745 mbedtls_x509_crt *trust_ca )
2746{
2747 mbedtls_x509_crt *cur;
2748
2749 /* must be self-issued */
2750 if( x509_name_cmp( &crt->issuer, &crt->subject ) != 0 )
2751 return( -1 );
2752
2753 /* look for an exact match with trusted cert */
2754 for( cur = trust_ca; cur != NULL; cur = cur->next )
2755 {
2756 if( crt->raw.len == cur->raw.len &&
2757 memcmp( crt->raw.p, cur->raw.p, crt->raw.len ) == 0 )
2758 {
2759 return( 0 );
2760 }
2761 }
2762
2763 /* too bad */
2764 return( -1 );
2765}
2766
2767/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002768 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002769 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002770 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2771 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002772 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002773 * such that every cert in the chain is a child of the next one,
2774 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002775 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002776 * Verify that chain and return it with flags for all issues found.
2777 *
2778 * Special cases:
2779 * - EE == Rj -> return a one-element list containing it
2780 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2781 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002782 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002783 * Tests for (aspects of) this function should include at least:
2784 * - trusted EE
2785 * - EE -> trusted root
Antonin Décimo36e89b52019-01-23 15:24:37 +01002786 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002787 * - if relevant: EE untrusted
2788 * - if relevant: EE -> intermediate, untrusted
2789 * with the aspect under test checked at each relevant level (EE, int, root).
2790 * For some aspects longer chains are required, but usually length 2 is
2791 * enough (but length 1 is not in general).
2792 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002793 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002794 * - [in] crt: the cert list EE, C1, ..., Cn
2795 * - [in] trust_ca: the trusted list R1, ..., Rp
2796 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002797 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002798 * Only valid when return value is 0, may contain garbage otherwise!
2799 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002800 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002801 *
2802 * Return value:
2803 * - non-zero if the chain could not be fully built and examined
2804 * - 0 is the chain was successfully built and examined,
2805 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002806 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002807static int x509_crt_verify_chain(
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002808 mbedtls_x509_crt *crt,
2809 mbedtls_x509_crt *trust_ca,
2810 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00002811 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2812 void *p_ca_cb,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002813 const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002814 mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002815 mbedtls_x509_crt_restart_ctx *rs_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002816{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002817 /* Don't initialize any of those variables here, so that the compiler can
2818 * catch potential issues with jumping ahead when restarting */
Janos Follath865b3eb2019-12-16 11:46:15 +00002819 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002820 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002821 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002822 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002823 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002824 int parent_is_trusted;
2825 int child_is_trusted;
2826 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002827 unsigned self_cnt;
Hanno Beckerf53893b2019-03-28 13:45:55 +00002828 mbedtls_x509_crt *cur_trust_ca = NULL;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002829
2830#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2831 /* resume if we had an operation in progress */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002832 if( rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent )
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002833 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002834 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002835 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002836 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002837
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002838 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002839 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002840 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002841 flags = &cur->flags;
2842
2843 goto find_parent;
2844 }
2845#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002846
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002847 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002848 self_cnt = 0;
2849 parent_is_trusted = 0;
2850 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002851
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002852 while( 1 ) {
2853 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002854 cur = &ver_chain->items[ver_chain->len];
2855 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002856 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002857 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002858 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002859
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002860 /* Check time-validity (all certificates) */
2861 if( mbedtls_x509_time_is_past( &child->valid_to ) )
2862 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002863
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002864 if( mbedtls_x509_time_is_future( &child->valid_from ) )
2865 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002866
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002867 /* Stop here for trusted roots (but not for trusted EE certs) */
2868 if( child_is_trusted )
2869 return( 0 );
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002870
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002871 /* Check signature algorithm: MD & PK algs */
2872 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
2873 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002874
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002875 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
2876 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002877
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002878 /* Special case: EE certs that are locally trusted */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002879 if( ver_chain->len == 1 &&
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002880 x509_crt_check_ee_locally_trusted( child, trust_ca ) == 0 )
2881 {
2882 return( 0 );
2883 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002884
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002885#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2886find_parent:
2887#endif
Hanno Beckerf53893b2019-03-28 13:45:55 +00002888
2889 /* Obtain list of potential trusted signers from CA callback,
2890 * or use statically provided list. */
2891#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
2892 if( f_ca_cb != NULL )
2893 {
2894 mbedtls_x509_crt_free( ver_chain->trust_ca_cb_result );
2895 mbedtls_free( ver_chain->trust_ca_cb_result );
2896 ver_chain->trust_ca_cb_result = NULL;
2897
2898 ret = f_ca_cb( p_ca_cb, child, &ver_chain->trust_ca_cb_result );
2899 if( ret != 0 )
2900 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2901
2902 cur_trust_ca = ver_chain->trust_ca_cb_result;
2903 }
2904 else
2905#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2906 {
2907 ((void) f_ca_cb);
2908 ((void) p_ca_cb);
2909 cur_trust_ca = trust_ca;
2910 }
2911
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002912 /* Look for a parent in trusted CAs or up the chain */
Hanno Beckerf53893b2019-03-28 13:45:55 +00002913 ret = x509_crt_find_parent( child, cur_trust_ca, &parent,
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002914 &parent_is_trusted, &signature_is_good,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002915 ver_chain->len - 1, self_cnt, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002916
2917#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002918 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2919 {
2920 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002921 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002922 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002923 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002924
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002925 return( ret );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002926 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002927#else
2928 (void) ret;
2929#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002930
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002931 /* No parent? We're done here */
2932 if( parent == NULL )
2933 {
2934 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2935 return( 0 );
2936 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002937
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002938 /* Count intermediate self-issued (not necessarily self-signed) certs.
2939 * These can occur with some strategies for key rollover, see [SIRO],
2940 * and should be excluded from max_pathlen checks. */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002941 if( ver_chain->len != 1 &&
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002942 x509_name_cmp( &child->issuer, &child->subject ) == 0 )
2943 {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002944 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002945 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002946
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002947 /* path_cnt is 0 for the first intermediate CA,
2948 * and if parent is trusted it's not an intermediate CA */
2949 if( ! parent_is_trusted &&
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002950 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002951 {
2952 /* return immediately to avoid overflow the chain array */
2953 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2954 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002955
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002956 /* signature was checked while searching parent */
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002957 if( ! signature_is_good )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002958 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2959
2960 /* check size of signing key */
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002961 if( x509_profile_check_key( profile, &parent->pk ) != 0 )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002962 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002964#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002965 /* Check trusted CA's CRL for the given crt */
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002966 *flags |= x509_crt_verifycrl( child, parent, ca_crl, profile );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002967#else
2968 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002969#endif
2970
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002971 /* prepare for next iteration */
2972 child = parent;
2973 parent = NULL;
2974 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002975 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002976 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002977}
2978
2979/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002980 * Check for CN match
2981 */
2982static int x509_crt_check_cn( const mbedtls_x509_buf *name,
2983 const char *cn, size_t cn_len )
2984{
2985 /* try exact match */
2986 if( name->len == cn_len &&
2987 x509_memcasecmp( cn, name->p, cn_len ) == 0 )
2988 {
2989 return( 0 );
2990 }
2991
2992 /* try wildcard match */
Manuel Pégourié-Gonnard900fba62017-10-18 14:28:11 +02002993 if( x509_check_wildcard( cn, name ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002994 {
2995 return( 0 );
2996 }
2997
2998 return( -1 );
2999}
3000
3001/*
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003002 * Check for SAN match, see RFC 5280 Section 4.2.1.6
3003 */
3004static int x509_crt_check_san( const mbedtls_x509_buf *name,
3005 const char *cn, size_t cn_len )
3006{
3007 const unsigned char san_type = (unsigned char) name->tag &
3008 MBEDTLS_ASN1_TAG_VALUE_MASK;
3009
3010 /* dNSName */
3011 if( san_type == MBEDTLS_X509_SAN_DNS_NAME )
3012 return( x509_crt_check_cn( name, cn, cn_len ) );
3013
3014 /* (We may handle other types here later.) */
3015
3016 /* Unrecognized type */
3017 return( -1 );
3018}
3019
3020/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003021 * Verify the requested CN - only call this if cn is not NULL!
3022 */
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003023static void x509_crt_verify_name( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003024 const char *cn,
3025 uint32_t *flags )
3026{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003027 const mbedtls_x509_name *name;
3028 const mbedtls_x509_sequence *cur;
3029 size_t cn_len = strlen( cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003030
3031 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
3032 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003033 for( cur = &crt->subject_alt_names; cur != NULL; cur = cur->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003034 {
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003035 if( x509_crt_check_san( &cur->buf, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003036 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003037 }
3038
3039 if( cur == NULL )
3040 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3041 }
3042 else
3043 {
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02003044 for( name = &crt->subject; name != NULL; name = name->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003045 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003046 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, &name->oid ) == 0 &&
3047 x509_crt_check_cn( &name->val, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003048 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003049 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003050 }
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003051 }
3052
3053 if( name == NULL )
3054 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3055 }
3056}
3057
3058/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003059 * Merge the flags for all certs in the chain, after calling callback
3060 */
3061static int x509_crt_merge_flags_with_cb(
3062 uint32_t *flags,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003063 const mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003064 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3065 void *p_vrfy )
3066{
Janos Follath865b3eb2019-12-16 11:46:15 +00003067 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003068 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003069 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003070 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003071
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003072 for( i = ver_chain->len; i != 0; --i )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003073 {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003074 cur = &ver_chain->items[i-1];
3075 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003076
3077 if( NULL != f_vrfy )
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003078 if( ( ret = f_vrfy( p_vrfy, cur->crt, (int) i-1, &cur_flags ) ) != 0 )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003079 return( ret );
3080
3081 *flags |= cur_flags;
3082 }
3083
3084 return( 0 );
3085}
3086
3087/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003088 * Verify the certificate validity, with profile, restartable version
3089 *
3090 * This function:
3091 * - checks the requested CN (if any)
3092 * - checks the type and size of the EE cert's key,
3093 * as that isn't done as part of chain building/verification currently
3094 * - builds and verifies the chain
3095 * - then calls the callback and merges the flags
Hanno Becker3116fb32019-03-28 13:34:42 +00003096 *
3097 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
3098 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
3099 * verification routine to search for trusted signers, and CRLs will
3100 * be disabled. Otherwise, `trust_ca` will be used as the static list
3101 * of trusted signers, and `ca_crl` will be use as the static list
3102 * of CRLs.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003103 */
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003104static int x509_crt_verify_restartable_ca_cb( mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003105 mbedtls_x509_crt *trust_ca,
3106 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003107 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3108 void *p_ca_cb,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003109 const mbedtls_x509_crt_profile *profile,
3110 const char *cn, uint32_t *flags,
3111 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3112 void *p_vrfy,
3113 mbedtls_x509_crt_restart_ctx *rs_ctx )
3114{
Janos Follath865b3eb2019-12-16 11:46:15 +00003115 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003116 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003117 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003118 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003119
3120 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003121 ee_flags = 0;
3122 x509_crt_verify_chain_reset( &ver_chain );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003123
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003124 if( profile == NULL )
3125 {
3126 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3127 goto exit;
3128 }
3129
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003130 /* check name if requested */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003131 if( cn != NULL )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003132 x509_crt_verify_name( crt, cn, &ee_flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003133
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003134 /* Check the type and size of the key */
3135 pk_type = mbedtls_pk_get_type( &crt->pk );
3136
3137 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003138 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003139
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02003140 if( x509_profile_check_key( profile, &crt->pk ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003141 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003142
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003143 /* Check the chain */
Hanno Becker3116fb32019-03-28 13:34:42 +00003144 ret = x509_crt_verify_chain( crt, trust_ca, ca_crl,
3145 f_ca_cb, p_ca_cb, profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003146 &ver_chain, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003147
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003148 if( ret != 0 )
3149 goto exit;
3150
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003151 /* Merge end-entity flags */
3152 ver_chain.items[0].flags |= ee_flags;
3153
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003154 /* Build final flags, calling callback on the way if any */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003155 ret = x509_crt_merge_flags_with_cb( flags, &ver_chain, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003156
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003157exit:
Hanno Beckerf53893b2019-03-28 13:45:55 +00003158
3159#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3160 mbedtls_x509_crt_free( ver_chain.trust_ca_cb_result );
3161 mbedtls_free( ver_chain.trust_ca_cb_result );
3162 ver_chain.trust_ca_cb_result = NULL;
3163#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3164
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003165#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3166 if( rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
3167 mbedtls_x509_crt_restart_free( rs_ctx );
3168#endif
3169
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003170 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3171 * the SSL module for authmode optional, but non-zero return from the
3172 * callback means a fatal error so it shouldn't be ignored */
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003173 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
3174 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3175
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003176 if( ret != 0 )
3177 {
3178 *flags = (uint32_t) -1;
3179 return( ret );
3180 }
3181
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003182 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003183 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003184
3185 return( 0 );
3186}
3187
Hanno Becker3116fb32019-03-28 13:34:42 +00003188
3189/*
3190 * Verify the certificate validity (default profile, not restartable)
3191 */
3192int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
3193 mbedtls_x509_crt *trust_ca,
3194 mbedtls_x509_crl *ca_crl,
3195 const char *cn, uint32_t *flags,
3196 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3197 void *p_vrfy )
3198{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003199 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003200 NULL, NULL,
3201 &mbedtls_x509_crt_profile_default,
3202 cn, flags,
3203 f_vrfy, p_vrfy, NULL ) );
3204}
3205
3206/*
3207 * Verify the certificate validity (user-chosen profile, not restartable)
3208 */
3209int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
3210 mbedtls_x509_crt *trust_ca,
3211 mbedtls_x509_crl *ca_crl,
3212 const mbedtls_x509_crt_profile *profile,
3213 const char *cn, uint32_t *flags,
3214 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3215 void *p_vrfy )
3216{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003217 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003218 NULL, NULL,
3219 profile, cn, flags,
3220 f_vrfy, p_vrfy, NULL ) );
3221}
3222
3223#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3224/*
3225 * Verify the certificate validity (user-chosen profile, CA callback,
3226 * not restartable).
3227 */
Jarno Lamsa31d9db62019-04-01 14:33:49 +03003228int mbedtls_x509_crt_verify_with_ca_cb( mbedtls_x509_crt *crt,
Hanno Becker3116fb32019-03-28 13:34:42 +00003229 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3230 void *p_ca_cb,
3231 const mbedtls_x509_crt_profile *profile,
3232 const char *cn, uint32_t *flags,
3233 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3234 void *p_vrfy )
3235{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003236 return( x509_crt_verify_restartable_ca_cb( crt, NULL, NULL,
Hanno Becker3116fb32019-03-28 13:34:42 +00003237 f_ca_cb, p_ca_cb,
3238 profile, cn, flags,
3239 f_vrfy, p_vrfy, NULL ) );
3240}
3241#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3242
3243int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
3244 mbedtls_x509_crt *trust_ca,
3245 mbedtls_x509_crl *ca_crl,
3246 const mbedtls_x509_crt_profile *profile,
3247 const char *cn, uint32_t *flags,
3248 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3249 void *p_vrfy,
3250 mbedtls_x509_crt_restart_ctx *rs_ctx )
3251{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003252 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003253 NULL, NULL,
3254 profile, cn, flags,
3255 f_vrfy, p_vrfy, rs_ctx ) );
3256}
3257
3258
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003259/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003260 * Initialize a certificate chain
3261 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003262void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02003263{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003264 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02003265}
3266
3267/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003268 * Unallocate all certificate data
3269 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003270void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003271{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003272 mbedtls_x509_crt *cert_cur = crt;
3273 mbedtls_x509_crt *cert_prv;
3274 mbedtls_x509_name *name_cur;
3275 mbedtls_x509_name *name_prv;
3276 mbedtls_x509_sequence *seq_cur;
3277 mbedtls_x509_sequence *seq_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003278
3279 if( crt == NULL )
3280 return;
3281
3282 do
3283 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003284 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003286#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
3287 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003288#endif
3289
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003290 name_cur = cert_cur->issuer.next;
3291 while( name_cur != NULL )
3292 {
3293 name_prv = name_cur;
3294 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003295 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003296 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003297 }
3298
3299 name_cur = cert_cur->subject.next;
3300 while( name_cur != NULL )
3301 {
3302 name_prv = name_cur;
3303 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003304 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003305 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003306 }
3307
3308 seq_cur = cert_cur->ext_key_usage.next;
3309 while( seq_cur != NULL )
3310 {
3311 seq_prv = seq_cur;
3312 seq_cur = seq_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003313 mbedtls_platform_zeroize( seq_prv,
3314 sizeof( mbedtls_x509_sequence ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003315 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003316 }
3317
3318 seq_cur = cert_cur->subject_alt_names.next;
3319 while( seq_cur != NULL )
3320 {
3321 seq_prv = seq_cur;
3322 seq_cur = seq_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003323 mbedtls_platform_zeroize( seq_prv,
3324 sizeof( mbedtls_x509_sequence ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003325 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003326 }
3327
Ron Eldor74d9acc2019-03-21 14:00:03 +02003328 seq_cur = cert_cur->certificate_policies.next;
3329 while( seq_cur != NULL )
3330 {
3331 seq_prv = seq_cur;
3332 seq_cur = seq_cur->next;
3333 mbedtls_platform_zeroize( seq_prv,
3334 sizeof( mbedtls_x509_sequence ) );
3335 mbedtls_free( seq_prv );
3336 }
3337
Hanno Becker1a65dcd2019-01-31 08:57:44 +00003338 if( cert_cur->raw.p != NULL && cert_cur->own_buffer )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003339 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003340 mbedtls_platform_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003341 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003342 }
3343
3344 cert_cur = cert_cur->next;
3345 }
3346 while( cert_cur != NULL );
3347
3348 cert_cur = crt;
3349 do
3350 {
3351 cert_prv = cert_cur;
3352 cert_cur = cert_cur->next;
3353
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003354 mbedtls_platform_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003355 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003356 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003357 }
3358 while( cert_cur != NULL );
3359}
3360
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003361#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3362/*
3363 * Initialize a restart context
3364 */
3365void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx )
3366{
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003367 mbedtls_pk_restart_init( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003368
3369 ctx->parent = NULL;
3370 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003371 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003372
3373 ctx->parent_is_trusted = -1;
3374
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003375 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003376 ctx->self_cnt = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003377 x509_crt_verify_chain_reset( &ctx->ver_chain );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003378}
3379
3380/*
3381 * Free the components of a restart context
3382 */
3383void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx )
3384{
3385 if( ctx == NULL )
3386 return;
3387
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003388 mbedtls_pk_restart_free( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003389 mbedtls_x509_crt_restart_init( ctx );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003390}
3391#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003393#endif /* MBEDTLS_X509_CRT_PARSE_C */