blob: 5677a5c576540d443c7bbb6e31c27d921e3dd735 [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"
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +020050#include "hash_info.h"
pespacek7599a772022-02-07 14:40:55 +010051#endif /* MBEDTLS_USE_PSA_CRYPTO */
Andrzej Kurekd4a65532018-10-31 06:18:39 -040052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020055#else
Simon Butcherd2642582018-10-03 15:11:19 +010056#include <stdio.h>
Rich Evans00ab4702015-02-06 13:43:58 +000057#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#define mbedtls_free free
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020059#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#define mbedtls_snprintf snprintf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020061#endif
62
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000064#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010065#endif
66
Daniel Axtensf0710242020-05-28 11:43:41 +100067#if defined(MBEDTLS_HAVE_TIME)
Paul Bakkerfa6a6202013-10-28 18:48:30 +010068#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020069#include <windows.h>
70#else
71#include <time.h>
72#endif
Daniel Axtensf0710242020-05-28 11:43:41 +100073#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075#if defined(MBEDTLS_FS_IO)
Rich Evans00ab4702015-02-06 13:43:58 +000076#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020077#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020078#include <sys/types.h>
79#include <sys/stat.h>
Martino Facchin0ec05ec2021-05-04 11:47:36 +020080#if defined(__MBED__)
81#include <platform/mbed_retarget.h>
82#else
Paul Bakker7c6b2c32013-09-16 13:49:26 +020083#include <dirent.h>
Martino Facchin0ec05ec2021-05-04 11:47:36 +020084#endif /* __MBED__ */
Rich Evans00ab4702015-02-06 13:43:58 +000085#endif /* !_WIN32 || EFIX64 || EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020086#endif
87
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +020088/*
89 * Item in a verification chain: cert and flags for it
90 */
91typedef struct {
92 mbedtls_x509_crt *crt;
93 uint32_t flags;
94} x509_crt_verify_chain_item;
95
96/*
97 * Max size of verification chain: end-entity + intermediates + trusted root
98 */
99#define X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
Paul Bakker34617722014-06-13 17:20:13 +0200100
Gilles Peskineffb92da2021-06-02 00:03:26 +0200101/* Default profile. Do not remove items unless there are serious security
102 * concerns. */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200103const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
104{
Gilles Peskineae270bf2021-06-02 00:05:29 +0200105 /* Hashes from SHA-256 and above. Note that this selection
106 * should be aligned with ssl_preset_default_hashes in ssl_tls.c. */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200107 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
108 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
109 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
110 0xFFFFFFF, /* Any PK alg */
111#if defined(MBEDTLS_ECP_C)
Gilles Peskineae270bf2021-06-02 00:05:29 +0200112 /* Curves at or above 128-bit security level. Note that this selection
113 * should be aligned with ssl_preset_default_curves in ssl_tls.c. */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200114 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
115 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
116 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
117 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
118 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
119 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
Gilles Peskine39957502021-06-17 23:17:52 +0200120 0,
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200121#else
122 0,
123#endif
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200124 2048,
125};
126
Gilles Peskineffb92da2021-06-02 00:03:26 +0200127/* Next-generation profile. Currently identical to the default, but may
128 * be tightened at any time. */
129const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200130{
131 /* Hashes from SHA-256 and above. */
132 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
133 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
134 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
135 0xFFFFFFF, /* Any PK alg */
136#if defined(MBEDTLS_ECP_C)
137 /* Curves at or above 128-bit security level. */
138 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
139 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
140 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
141 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
142 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
143 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
144 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256K1 ),
145#else
146 0,
147#endif
148 2048,
149};
Gilles Peskineffb92da2021-06-02 00:03:26 +0200150
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200151/*
152 * NSA Suite B Profile
153 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200154const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
155{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200156 /* Only SHA-256 and 384 */
157 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
158 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ),
159 /* Only ECDSA */
Ron Eldor85e1dcf2018-02-06 15:59:38 +0200160 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECDSA ) |
161 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECKEY ),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200162#if defined(MBEDTLS_ECP_C)
163 /* Only NIST P-256 and P-384 */
164 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
165 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ),
166#else
167 0,
168#endif
169 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200170};
171
172/*
Manuel Pégourié-Gonnard9d4c2c42021-06-18 09:48:27 +0200173 * Empty / all-forbidden profile
174 */
175const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_none =
176{
177 0,
178 0,
179 0,
180 (uint32_t) -1,
181};
182
183/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200184 * Check md_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200185 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200186 */
187static int x509_profile_check_md_alg( const mbedtls_x509_crt_profile *profile,
188 mbedtls_md_type_t md_alg )
189{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200190 if( md_alg == MBEDTLS_MD_NONE )
191 return( -1 );
192
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200193 if( ( profile->allowed_mds & MBEDTLS_X509_ID_FLAG( md_alg ) ) != 0 )
194 return( 0 );
195
196 return( -1 );
197}
198
199/*
200 * Check pk_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200201 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200202 */
203static int x509_profile_check_pk_alg( const mbedtls_x509_crt_profile *profile,
204 mbedtls_pk_type_t pk_alg )
205{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200206 if( pk_alg == MBEDTLS_PK_NONE )
207 return( -1 );
208
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200209 if( ( profile->allowed_pks & MBEDTLS_X509_ID_FLAG( pk_alg ) ) != 0 )
210 return( 0 );
211
212 return( -1 );
213}
214
215/*
216 * Check key against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200217 * Return 0 if pk is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200218 */
219static int x509_profile_check_key( const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200220 const mbedtls_pk_context *pk )
221{
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200222 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type( pk );
Manuel Pégourié-Gonnard19773ff2017-10-24 10:51:26 +0200223
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200224#if defined(MBEDTLS_RSA_C)
225 if( pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS )
226 {
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200227 if( mbedtls_pk_get_bitlen( pk ) >= profile->rsa_min_bitlen )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200228 return( 0 );
229
230 return( -1 );
231 }
232#endif
233
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200234#if defined(MBEDTLS_ECP_C)
235 if( pk_alg == MBEDTLS_PK_ECDSA ||
236 pk_alg == MBEDTLS_PK_ECKEY ||
237 pk_alg == MBEDTLS_PK_ECKEY_DH )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200238 {
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200239 const mbedtls_ecp_group_id gid = mbedtls_pk_ec( *pk )->grp.id;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200240
Philippe Antoineb5b25432018-05-11 11:06:29 +0200241 if( gid == MBEDTLS_ECP_DP_NONE )
242 return( -1 );
243
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200244 if( ( profile->allowed_curves & MBEDTLS_X509_ID_FLAG( gid ) ) != 0 )
245 return( 0 );
246
247 return( -1 );
248 }
249#endif
250
251 return( -1 );
252}
253
254/*
Hanno Becker1f8527f2018-11-02 09:19:16 +0000255 * Like memcmp, but case-insensitive and always returns -1 if different
256 */
257static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
258{
259 size_t i;
260 unsigned char diff;
261 const unsigned char *n1 = s1, *n2 = s2;
262
263 for( i = 0; i < len; i++ )
264 {
265 diff = n1[i] ^ n2[i];
266
267 if( diff == 0 )
268 continue;
269
270 if( diff == 32 &&
271 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
272 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
273 {
274 continue;
275 }
276
277 return( -1 );
278 }
279
280 return( 0 );
281}
282
283/*
284 * Return 0 if name matches wildcard, -1 otherwise
285 */
286static int x509_check_wildcard( const char *cn, const mbedtls_x509_buf *name )
287{
288 size_t i;
289 size_t cn_idx = 0, cn_len = strlen( cn );
290
291 /* We can't have a match if there is no wildcard to match */
292 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
293 return( -1 );
294
295 for( i = 0; i < cn_len; ++i )
296 {
297 if( cn[i] == '.' )
298 {
299 cn_idx = i;
300 break;
301 }
302 }
303
304 if( cn_idx == 0 )
305 return( -1 );
306
307 if( cn_len - cn_idx == name->len - 1 &&
308 x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
309 {
310 return( 0 );
311 }
312
313 return( -1 );
314}
315
316/*
317 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
318 * variations (but not all).
319 *
320 * Return 0 if equal, -1 otherwise.
321 */
322static int x509_string_cmp( const mbedtls_x509_buf *a, const mbedtls_x509_buf *b )
323{
324 if( a->tag == b->tag &&
325 a->len == b->len &&
326 memcmp( a->p, b->p, b->len ) == 0 )
327 {
328 return( 0 );
329 }
330
331 if( ( a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
332 ( b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
333 a->len == b->len &&
334 x509_memcasecmp( a->p, b->p, b->len ) == 0 )
335 {
336 return( 0 );
337 }
338
339 return( -1 );
340}
341
342/*
343 * Compare two X.509 Names (aka rdnSequence).
344 *
345 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
346 * we sometimes return unequal when the full algorithm would return equal,
347 * but never the other way. (In particular, we don't do Unicode normalisation
348 * or space folding.)
349 *
350 * Return 0 if equal, -1 otherwise.
351 */
352static int x509_name_cmp( const mbedtls_x509_name *a, const mbedtls_x509_name *b )
353{
354 /* Avoid recursion, it might not be optimised by the compiler */
355 while( a != NULL || b != NULL )
356 {
357 if( a == NULL || b == NULL )
358 return( -1 );
359
360 /* type */
361 if( a->oid.tag != b->oid.tag ||
362 a->oid.len != b->oid.len ||
363 memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 )
364 {
365 return( -1 );
366 }
367
368 /* value */
369 if( x509_string_cmp( &a->val, &b->val ) != 0 )
370 return( -1 );
371
372 /* structure of the list of sets */
373 if( a->next_merged != b->next_merged )
374 return( -1 );
375
376 a = a->next;
377 b = b->next;
378 }
379
380 /* a == NULL == b */
381 return( 0 );
382}
383
384/*
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200385 * Reset (init or clear) a verify_chain
386 */
387static void x509_crt_verify_chain_reset(
388 mbedtls_x509_crt_verify_chain *ver_chain )
389{
390 size_t i;
391
392 for( i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++ )
393 {
394 ver_chain->items[i].crt = NULL;
Hanno Beckera9375b32019-01-10 09:19:26 +0000395 ver_chain->items[i].flags = (uint32_t) -1;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200396 }
397
398 ver_chain->len = 0;
Hanno Beckerf53893b2019-03-28 13:45:55 +0000399
400#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
401 ver_chain->trust_ca_cb_result = NULL;
402#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200403}
404
405/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200406 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
407 */
408static int x509_get_version( unsigned char **p,
409 const unsigned char *end,
410 int *ver )
411{
Janos Follath865b3eb2019-12-16 11:46:15 +0000412 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200413 size_t len;
414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
416 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200417 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200419 {
420 *ver = 0;
421 return( 0 );
422 }
423
Chris Jones9f7a6932021-04-14 12:12:09 +0100424 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200425 }
426
427 end = *p + len;
428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100430 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_VERSION, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200431
432 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100433 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_VERSION,
434 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200435
436 return( 0 );
437}
438
439/*
440 * Validity ::= SEQUENCE {
441 * notBefore Time,
442 * notAfter Time }
443 */
444static int x509_get_dates( unsigned char **p,
445 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446 mbedtls_x509_time *from,
447 mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200448{
Janos Follath865b3eb2019-12-16 11:46:15 +0000449 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200450 size_t len;
451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
453 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100454 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200455
456 end = *p + len;
457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458 if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200459 return( ret );
460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200462 return( ret );
463
464 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100465 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE,
466 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200467
468 return( 0 );
469}
470
471/*
472 * X.509 v2/v3 unique identifier (not parsed)
473 */
474static int x509_get_uid( unsigned char **p,
475 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476 mbedtls_x509_buf *uid, int n )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200477{
Janos Follath865b3eb2019-12-16 11:46:15 +0000478 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200479
480 if( *p == end )
481 return( 0 );
482
483 uid->tag = **p;
484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 if( ( ret = mbedtls_asn1_get_tag( p, end, &uid->len,
486 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200487 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200489 return( 0 );
490
Chris Jones9f7a6932021-04-14 12:12:09 +0100491 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200492 }
493
494 uid->p = *p;
495 *p += uid->len;
496
497 return( 0 );
498}
499
500static int x509_get_basic_constraints( unsigned char **p,
501 const unsigned char *end,
502 int *ca_istrue,
503 int *max_pathlen )
504{
Janos Follath865b3eb2019-12-16 11:46:15 +0000505 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200506 size_t len;
507
508 /*
509 * BasicConstraints ::= SEQUENCE {
510 * cA BOOLEAN DEFAULT FALSE,
511 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
512 */
513 *ca_istrue = 0; /* DEFAULT FALSE */
514 *max_pathlen = 0; /* endless */
515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
517 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100518 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200519
520 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200521 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 if( ( ret = mbedtls_asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200524 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
526 ret = mbedtls_asn1_get_int( p, end, ca_istrue );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200527
528 if( ret != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100529 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200530
531 if( *ca_istrue != 0 )
532 *ca_istrue = 1;
533 }
534
535 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200536 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538 if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100539 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200540
541 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100542 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
543 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200544
Andrzej Kurek16050742020-04-14 09:49:52 -0400545 /* Do not accept max_pathlen equal to INT_MAX to avoid a signed integer
546 * overflow, which is an undefined behavior. */
547 if( *max_pathlen == INT_MAX )
Chris Jones9f7a6932021-04-14 12:12:09 +0100548 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
549 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Andrzej Kurek16050742020-04-14 09:49:52 -0400550
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200551 (*max_pathlen)++;
552
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200553 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200554}
555
556static int x509_get_ns_cert_type( unsigned char **p,
557 const unsigned char *end,
558 unsigned char *ns_cert_type)
559{
Janos Follath865b3eb2019-12-16 11:46:15 +0000560 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100564 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200565
566 if( bs.len != 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100567 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
568 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200569
570 /* Get actual bitstring */
571 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200572 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200573}
574
575static int x509_get_key_usage( unsigned char **p,
576 const unsigned char *end,
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +0100577 unsigned int *key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200578{
Janos Follath865b3eb2019-12-16 11:46:15 +0000579 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200580 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100584 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200585
586 if( bs.len < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100587 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
588 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200589
590 /* Get actual bitstring */
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200591 *key_usage = 0;
592 for( i = 0; i < bs.len && i < sizeof( unsigned int ); i++ )
593 {
594 *key_usage |= (unsigned int) bs.p[i] << (8*i);
595 }
596
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200597 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200598}
599
600/*
601 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
602 *
603 * KeyPurposeId ::= OBJECT IDENTIFIER
604 */
605static int x509_get_ext_key_usage( unsigned char **p,
606 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200608{
Janos Follath865b3eb2019-12-16 11:46:15 +0000609 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611 if( ( ret = mbedtls_asn1_get_sequence_of( p, end, ext_key_usage, MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100612 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200613
614 /* Sequence length must be >= 1 */
615 if( ext_key_usage->buf.p == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100616 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
617 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200618
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200619 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200620}
621
622/*
623 * SubjectAltName ::= GeneralNames
624 *
625 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
626 *
627 * GeneralName ::= CHOICE {
628 * otherName [0] OtherName,
629 * rfc822Name [1] IA5String,
630 * dNSName [2] IA5String,
631 * x400Address [3] ORAddress,
632 * directoryName [4] Name,
633 * ediPartyName [5] EDIPartyName,
634 * uniformResourceIdentifier [6] IA5String,
635 * iPAddress [7] OCTET STRING,
636 * registeredID [8] OBJECT IDENTIFIER }
637 *
638 * OtherName ::= SEQUENCE {
639 * type-id OBJECT IDENTIFIER,
640 * value [0] EXPLICIT ANY DEFINED BY type-id }
641 *
642 * EDIPartyName ::= SEQUENCE {
643 * nameAssigner [0] DirectoryString OPTIONAL,
644 * partyName [1] DirectoryString }
645 *
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300646 * NOTE: we list all types, but only use dNSName and otherName
647 * of type HwModuleName, as defined in RFC 4108, at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200648 */
649static int x509_get_subject_alt_name( unsigned char **p,
650 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 mbedtls_x509_sequence *subject_alt_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200652{
Janos Follath865b3eb2019-12-16 11:46:15 +0000653 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200654 size_t len, tag_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 mbedtls_asn1_buf *buf;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200656 unsigned char tag;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 mbedtls_asn1_sequence *cur = subject_alt_name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200658
659 /* Get main sequence tag */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
661 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100662 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200663
664 if( *p + len != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100665 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
666 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200667
668 while( *p < end )
669 {
Ron Eldordbbd9662019-05-15 15:46:03 +0300670 mbedtls_x509_subject_alternative_name dummy_san_buf;
671 memset( &dummy_san_buf, 0, sizeof( dummy_san_buf ) );
672
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200673 tag = **p;
674 (*p)++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 if( ( ret = mbedtls_asn1_get_len( p, end, &tag_len ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100676 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200677
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100678 if( ( tag & MBEDTLS_ASN1_TAG_CLASS_MASK ) !=
679 MBEDTLS_ASN1_CONTEXT_SPECIFIC )
680 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100681 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
682 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100683 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200684
Ron Eldordbbd9662019-05-15 15:46:03 +0300685 /*
irwir74aee1c2019-09-21 18:21:48 +0300686 * Check that the SAN is structured correctly.
Ron Eldordbbd9662019-05-15 15:46:03 +0300687 */
688 ret = mbedtls_x509_parse_subject_alt_name( &(cur->buf), &dummy_san_buf );
689 /*
690 * In case the extension is malformed, return an error,
691 * and clear the allocated sequences.
692 */
693 if( ret != 0 && ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
694 {
695 mbedtls_x509_sequence *seq_cur = subject_alt_name->next;
696 mbedtls_x509_sequence *seq_prv;
697 while( seq_cur != NULL )
698 {
699 seq_prv = seq_cur;
700 seq_cur = seq_cur->next;
701 mbedtls_platform_zeroize( seq_prv,
702 sizeof( mbedtls_x509_sequence ) );
703 mbedtls_free( seq_prv );
704 }
Ron Eldor5aebeeb2019-05-22 16:41:21 +0300705 subject_alt_name->next = NULL;
Ron Eldordbbd9662019-05-15 15:46:03 +0300706 return( ret );
707 }
708
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200709 /* Allocate and assign next pointer */
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200710 if( cur->buf.p != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200711 {
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100712 if( cur->next != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100714
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200715 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200716
717 if( cur->next == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100718 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
719 MBEDTLS_ERR_ASN1_ALLOC_FAILED ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200720
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200721 cur = cur->next;
722 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200723
724 buf = &(cur->buf);
725 buf->tag = tag;
726 buf->p = *p;
727 buf->len = tag_len;
728 *p += buf->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200729 }
730
731 /* Set final sequence entry's next pointer to NULL */
732 cur->next = NULL;
733
734 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100735 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
736 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200737
738 return( 0 );
739}
740
741/*
Ron Eldor74d9acc2019-03-21 14:00:03 +0200742 * id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 }
743 *
744 * anyPolicy OBJECT IDENTIFIER ::= { id-ce-certificatePolicies 0 }
745 *
746 * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
747 *
748 * PolicyInformation ::= SEQUENCE {
749 * policyIdentifier CertPolicyId,
750 * policyQualifiers SEQUENCE SIZE (1..MAX) OF
751 * PolicyQualifierInfo OPTIONAL }
752 *
753 * CertPolicyId ::= OBJECT IDENTIFIER
754 *
755 * PolicyQualifierInfo ::= SEQUENCE {
756 * policyQualifierId PolicyQualifierId,
757 * qualifier ANY DEFINED BY policyQualifierId }
758 *
759 * -- policyQualifierIds for Internet policy qualifiers
760 *
761 * id-qt OBJECT IDENTIFIER ::= { id-pkix 2 }
762 * id-qt-cps OBJECT IDENTIFIER ::= { id-qt 1 }
763 * id-qt-unotice OBJECT IDENTIFIER ::= { id-qt 2 }
764 *
765 * PolicyQualifierId ::= OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice )
766 *
767 * Qualifier ::= CHOICE {
768 * cPSuri CPSuri,
769 * userNotice UserNotice }
770 *
771 * CPSuri ::= IA5String
772 *
773 * UserNotice ::= SEQUENCE {
774 * noticeRef NoticeReference OPTIONAL,
775 * explicitText DisplayText OPTIONAL }
776 *
777 * NoticeReference ::= SEQUENCE {
778 * organization DisplayText,
779 * noticeNumbers SEQUENCE OF INTEGER }
780 *
781 * DisplayText ::= CHOICE {
782 * ia5String IA5String (SIZE (1..200)),
783 * visibleString VisibleString (SIZE (1..200)),
784 * bmpString BMPString (SIZE (1..200)),
785 * utf8String UTF8String (SIZE (1..200)) }
786 *
787 * NOTE: we only parse and use anyPolicy without qualifiers at this point
788 * as defined in RFC 5280.
789 */
790static int x509_get_certificate_policies( unsigned char **p,
791 const unsigned char *end,
792 mbedtls_x509_sequence *certificate_policies )
793{
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300794 int ret, parse_ret = 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200795 size_t len;
796 mbedtls_asn1_buf *buf;
797 mbedtls_asn1_sequence *cur = certificate_policies;
798
799 /* Get main sequence tag */
800 ret = mbedtls_asn1_get_tag( p, end, &len,
801 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
802 if( ret != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100803 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200804
805 if( *p + len != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100806 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
807 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200808
809 /*
810 * Cannot be an empty sequence.
811 */
812 if( len == 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100813 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
814 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200815
816 while( *p < end )
817 {
818 mbedtls_x509_buf policy_oid;
819 const unsigned char *policy_end;
820
821 /*
822 * Get the policy sequence
823 */
824 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
825 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100826 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200827
828 policy_end = *p + len;
829
Ron Eldor08063792019-05-13 16:38:39 +0300830 if( ( ret = mbedtls_asn1_get_tag( p, policy_end, &len,
Ron Eldor74d9acc2019-03-21 14:00:03 +0200831 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100832 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200833
834 policy_oid.tag = MBEDTLS_ASN1_OID;
835 policy_oid.len = len;
836 policy_oid.p = *p;
837
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300838 /*
839 * Only AnyPolicy is currently supported when enforcing policy.
840 */
841 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_POLICY, &policy_oid ) != 0 )
842 {
843 /*
844 * Set the parsing return code but continue parsing, in case this
TRodziewicz3ecb92e2021-05-11 18:22:05 +0200845 * extension is critical.
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300846 */
847 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
848 }
849
Ron Eldor74d9acc2019-03-21 14:00:03 +0200850 /* Allocate and assign next pointer */
851 if( cur->buf.p != NULL )
852 {
853 if( cur->next != NULL )
854 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
855
856 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
857
858 if( cur->next == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100859 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
860 MBEDTLS_ERR_ASN1_ALLOC_FAILED ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200861
862 cur = cur->next;
863 }
864
865 buf = &( cur->buf );
866 buf->tag = policy_oid.tag;
867 buf->p = policy_oid.p;
868 buf->len = policy_oid.len;
Ron Eldor08063792019-05-13 16:38:39 +0300869
870 *p += len;
871
872 /*
873 * If there is an optional qualifier, then *p < policy_end
874 * Check the Qualifier len to verify it doesn't exceed policy_end.
875 */
876 if( *p < policy_end )
877 {
878 if( ( ret = mbedtls_asn1_get_tag( p, policy_end, &len,
879 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100880 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor08063792019-05-13 16:38:39 +0300881 /*
882 * Skip the optional policy qualifiers.
883 */
884 *p += len;
885 }
886
887 if( *p != policy_end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100888 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
889 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200890 }
891
892 /* Set final sequence entry's next pointer to NULL */
893 cur->next = NULL;
894
895 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100896 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
897 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200898
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300899 return( parse_ret );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200900}
901
902/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200903 * X.509 v3 extensions
904 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200905 */
906static int x509_get_crt_ext( unsigned char **p,
907 const unsigned char *end,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200908 mbedtls_x509_crt *crt,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200909 mbedtls_x509_crt_ext_cb_t cb,
910 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200911{
Janos Follath865b3eb2019-12-16 11:46:15 +0000912 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200913 size_t len;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200914 unsigned char *end_ext_data, *start_ext_octet, *end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200915
Hanno Becker12f62fb2019-02-12 17:22:36 +0000916 if( *p == end )
917 return( 0 );
918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200919 if( ( ret = mbedtls_x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200920 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200921
Hanno Becker12f62fb2019-02-12 17:22:36 +0000922 end = crt->v3_ext.p + crt->v3_ext.len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200923 while( *p < end )
924 {
925 /*
926 * Extension ::= SEQUENCE {
927 * extnID OBJECT IDENTIFIER,
928 * critical BOOLEAN DEFAULT FALSE,
929 * extnValue OCTET STRING }
930 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200931 mbedtls_x509_buf extn_oid = {0, 0, NULL};
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200932 int is_critical = 0; /* DEFAULT FALSE */
933 int ext_type = 0;
934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
936 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100937 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200938
939 end_ext_data = *p + len;
940
941 /* Get extension ID */
k-stachowiakdcae78a2018-06-28 16:32:54 +0200942 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &extn_oid.len,
k-stachowiak463928a2018-07-24 12:50:59 +0200943 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100944 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200945
k-stachowiak463928a2018-07-24 12:50:59 +0200946 extn_oid.tag = MBEDTLS_ASN1_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200947 extn_oid.p = *p;
948 *p += extn_oid.len;
949
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200950 /* Get optional critical */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200951 if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
952 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
Chris Jones9f7a6932021-04-14 12:12:09 +0100953 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200954
955 /* Data should be octet string type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
957 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100958 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200959
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200960 start_ext_octet = *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200961 end_ext_octet = *p + len;
962
963 if( end_ext_octet != end_ext_data )
Chris Jones9f7a6932021-04-14 12:12:09 +0100964 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
965 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200966
967 /*
968 * Detect supported extensions
969 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200970 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200971
972 if( ret != 0 )
973 {
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200974 /* Give the callback (if any) a chance to handle the extension */
Nicola Di Lieto2c3a9172020-05-28 17:20:42 +0200975 if( cb != NULL )
976 {
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200977 ret = cb( p_ctx, crt, &extn_oid, is_critical, *p, end_ext_octet );
Nicola Di Lieto565b52b2020-05-29 22:46:56 +0200978 if( ret != 0 && is_critical )
979 return( ret );
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200980 *p = end_ext_octet;
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200981 continue;
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200982 }
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200983
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200984 /* No parser found, skip extension */
985 *p = end_ext_octet;
986
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200987 if( is_critical )
988 {
989 /* Data is marked as critical: fail */
Chris Jones9f7a6932021-04-14 12:12:09 +0100990 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
991 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200992 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200993 continue;
994 }
995
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100996 /* Forbid repeated extensions */
997 if( ( crt->ext_types & ext_type ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200998 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100999
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001000 crt->ext_types |= ext_type;
1001
1002 switch( ext_type )
1003 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001004 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001005 /* Parse basic constraints */
1006 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
1007 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001008 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001009 break;
1010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001012 /* Parse key usage */
1013 if( ( ret = x509_get_key_usage( p, end_ext_octet,
1014 &crt->key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001015 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001016 break;
1017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001019 /* Parse extended key usage */
1020 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
1021 &crt->ext_key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001022 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001023 break;
1024
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001025 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001026 /* Parse subject alt name */
1027 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
1028 &crt->subject_alt_names ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001029 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001030 break;
1031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001033 /* Parse netscape certificate type */
1034 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
1035 &crt->ns_cert_type ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001036 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001037 break;
1038
Ron Eldor74d9acc2019-03-21 14:00:03 +02001039 case MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES:
1040 /* Parse certificate policies type */
1041 if( ( ret = x509_get_certificate_policies( p, end_ext_octet,
1042 &crt->certificate_policies ) ) != 0 )
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001043 {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +02001044 /* Give the callback (if any) a chance to handle the extension
1045 * if it contains unsupported policies */
1046 if( ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE && cb != NULL &&
1047 cb( p_ctx, crt, &extn_oid, is_critical,
1048 start_ext_octet, end_ext_octet ) == 0 )
1049 break;
1050
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001051 if( is_critical )
1052 return( ret );
1053 else
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001054 /*
Ron Eldora2913912019-05-16 16:17:38 +03001055 * If MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE is returned, then we
1056 * cannot interpret or enforce the policy. However, it is up to
1057 * the user to choose how to enforce the policies,
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001058 * unless the extension is critical.
1059 */
1060 if( ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1061 return( ret );
1062 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001063 break;
1064
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001065 default:
Ron Eldordf48efa2019-04-08 13:28:24 +03001066 /*
1067 * If this is a non-critical extension, which the oid layer
1068 * supports, but there isn't an x509 parser for it,
1069 * skip the extension.
1070 */
Ron Eldordf48efa2019-04-08 13:28:24 +03001071 if( is_critical )
1072 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1073 else
Ron Eldordf48efa2019-04-08 13:28:24 +03001074 *p = end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001075 }
1076 }
1077
1078 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +01001079 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1080 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001081
1082 return( 0 );
1083}
1084
1085/*
1086 * Parse and fill a single X.509 certificate in DER format
1087 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001088static int x509_crt_parse_der_core( mbedtls_x509_crt *crt,
1089 const unsigned char *buf,
1090 size_t buflen,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001091 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001092 mbedtls_x509_crt_ext_cb_t cb,
1093 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001094{
Janos Follath865b3eb2019-12-16 11:46:15 +00001095 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001096 size_t len;
1097 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001100 memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) );
1101 memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) );
1102 memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001103
1104 /*
1105 * Check for valid input
1106 */
1107 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001109
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001110 /* Use the original buffer until we figure out actual length. */
Janos Follathcc0e49d2016-02-17 14:34:12 +00001111 p = (unsigned char*) buf;
1112 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001113 end = p + len;
1114
1115 /*
1116 * Certificate ::= SEQUENCE {
1117 * tbsCertificate TBSCertificate,
1118 * signatureAlgorithm AlgorithmIdentifier,
1119 * signatureValue BIT STRING }
1120 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001121 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1122 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001123 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124 mbedtls_x509_crt_free( crt );
1125 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001126 }
1127
Janos Follathcc0e49d2016-02-17 14:34:12 +00001128 end = crt_end = p + len;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001129 crt->raw.len = crt_end - buf;
1130 if( make_copy != 0 )
1131 {
1132 /* Create and populate a new buffer for the raw field. */
1133 crt->raw.p = p = mbedtls_calloc( 1, crt->raw.len );
1134 if( crt->raw.p == NULL )
1135 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
1136
1137 memcpy( crt->raw.p, buf, crt->raw.len );
1138 crt->own_buffer = 1;
1139
1140 p += crt->raw.len - len;
1141 end = crt_end = p + len;
1142 }
1143 else
1144 {
1145 crt->raw.p = (unsigned char*) buf;
1146 crt->own_buffer = 0;
1147 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001148
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001149 /*
1150 * TBSCertificate ::= SEQUENCE {
1151 */
1152 crt->tbs.p = p;
1153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1155 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001156 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001158 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001159 }
1160
1161 end = p + len;
1162 crt->tbs.len = end - crt->tbs.p;
1163
1164 /*
1165 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1166 *
1167 * CertificateSerialNumber ::= INTEGER
1168 *
1169 * signature AlgorithmIdentifier
1170 */
1171 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172 ( ret = mbedtls_x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1173 ( ret = mbedtls_x509_get_alg( &p, end, &crt->sig_oid,
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001174 &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001175 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001176 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001177 return( ret );
1178 }
1179
Andres AG7ca4a032017-03-09 16:16:11 +00001180 if( crt->version < 0 || crt->version > 2 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001181 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001182 mbedtls_x509_crt_free( crt );
1183 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001184 }
1185
Andres AG7ca4a032017-03-09 16:16:11 +00001186 crt->version++;
1187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001188 if( ( ret = mbedtls_x509_get_sig_alg( &crt->sig_oid, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02001189 &crt->sig_md, &crt->sig_pk,
1190 &crt->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001192 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001193 return( ret );
1194 }
1195
1196 /*
1197 * issuer Name
1198 */
1199 crt->issuer_raw.p = p;
1200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1202 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001204 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001205 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001206 }
1207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208 if( ( ret = mbedtls_x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001209 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001211 return( ret );
1212 }
1213
1214 crt->issuer_raw.len = p - crt->issuer_raw.p;
1215
1216 /*
1217 * Validity ::= SEQUENCE {
1218 * notBefore Time,
1219 * notAfter Time }
1220 *
1221 */
1222 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1223 &crt->valid_to ) ) != 0 )
1224 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001225 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001226 return( ret );
1227 }
1228
1229 /*
1230 * subject Name
1231 */
1232 crt->subject_raw.p = p;
1233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001234 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1235 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001236 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001237 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001238 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001239 }
1240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001241 if( len && ( ret = mbedtls_x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001242 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001243 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001244 return( ret );
1245 }
1246
1247 crt->subject_raw.len = p - crt->subject_raw.p;
1248
1249 /*
1250 * SubjectPublicKeyInfo
1251 */
Hanno Becker494dd7a2019-02-06 16:13:41 +00001252 crt->pk_raw.p = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001253 if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001254 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001255 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001256 return( ret );
1257 }
Hanno Becker494dd7a2019-02-06 16:13:41 +00001258 crt->pk_raw.len = p - crt->pk_raw.p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001259
1260 /*
1261 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1262 * -- If present, version shall be v2 or v3
1263 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1264 * -- If present, version shall be v2 or v3
1265 * extensions [3] EXPLICIT Extensions OPTIONAL
1266 * -- If present, version shall be v3
1267 */
1268 if( crt->version == 2 || crt->version == 3 )
1269 {
1270 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1271 if( ret != 0 )
1272 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001273 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001274 return( ret );
1275 }
1276 }
1277
1278 if( crt->version == 2 || crt->version == 3 )
1279 {
1280 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1281 if( ret != 0 )
1282 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001284 return( ret );
1285 }
1286 }
1287
1288 if( crt->version == 3 )
Manuel Pégourié-Gonnarda252af72015-03-27 16:15:55 +01001289 {
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001290 ret = x509_get_crt_ext( &p, end, crt, cb, p_ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001291 if( ret != 0 )
1292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001294 return( ret );
1295 }
1296 }
1297
1298 if( p != end )
1299 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001301 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT,
1302 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001303 }
1304
1305 end = crt_end;
1306
1307 /*
1308 * }
1309 * -- end of TBSCertificate
1310 *
1311 * signatureAlgorithm AlgorithmIdentifier,
1312 * signatureValue BIT STRING
1313 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314 if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001315 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001317 return( ret );
1318 }
1319
Manuel Pégourié-Gonnard1022fed2015-03-27 16:30:47 +01001320 if( crt->sig_oid.len != sig_oid2.len ||
1321 memcmp( crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len ) != 0 ||
Paul Elliottca17ebf2020-11-24 17:30:18 +00001322 sig_params1.tag != sig_params2.tag ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001323 sig_params1.len != sig_params2.len ||
Manuel Pégourié-Gonnard159c5242015-04-30 11:15:22 +02001324 ( sig_params1.len != 0 &&
1325 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001326 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001327 mbedtls_x509_crt_free( crt );
1328 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001329 }
1330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331 if( ( ret = mbedtls_x509_get_sig( &p, end, &crt->sig ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001332 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001334 return( ret );
1335 }
1336
1337 if( p != end )
1338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001340 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT,
1341 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001342 }
1343
1344 return( 0 );
1345}
1346
1347/*
1348 * Parse one X.509 certificate in DER format from a buffer and add them to a
1349 * chained list
1350 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001351static int mbedtls_x509_crt_parse_der_internal( mbedtls_x509_crt *chain,
1352 const unsigned char *buf,
1353 size_t buflen,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001354 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001355 mbedtls_x509_crt_ext_cb_t cb,
1356 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001357{
Janos Follath865b3eb2019-12-16 11:46:15 +00001358 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001360
1361 /*
1362 * Check for valid input
1363 */
1364 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001366
1367 while( crt->version != 0 && crt->next != NULL )
1368 {
1369 prev = crt;
1370 crt = crt->next;
1371 }
1372
1373 /*
1374 * Add new certificate on the end of the chain if needed.
1375 */
Paul Bakker66d5d072014-06-17 16:39:18 +02001376 if( crt->version != 0 && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001377 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001378 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001379
1380 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001381 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001382
1383 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001384 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001385 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001386 }
1387
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001388 ret = x509_crt_parse_der_core( crt, buf, buflen, make_copy, cb, p_ctx );
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001389 if( ret != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001390 {
1391 if( prev )
1392 prev->next = NULL;
1393
1394 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001395 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001396
1397 return( ret );
1398 }
1399
1400 return( 0 );
1401}
1402
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001403int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
1404 const unsigned char *buf,
1405 size_t buflen )
1406{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001407 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 0, NULL, NULL ) );
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001408}
1409
Nicola Di Lietofde98f72020-05-28 08:34:33 +02001410int mbedtls_x509_crt_parse_der_with_ext_cb( mbedtls_x509_crt *chain,
1411 const unsigned char *buf,
1412 size_t buflen,
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +02001413 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001414 mbedtls_x509_crt_ext_cb_t cb,
1415 void *p_ctx )
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001416{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001417 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, make_copy, cb, p_ctx ) );
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001418}
1419
1420int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
1421 const unsigned char *buf,
1422 size_t buflen )
1423{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001424 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 1, NULL, NULL ) );
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001425}
1426
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001427/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001428 * Parse one or more PEM certificates from a buffer and add them to the chained
1429 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001430 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001431int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain,
1432 const unsigned char *buf,
1433 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001434{
Janos Follath98e28a72016-05-31 14:03:54 +01001435#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001436 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001438#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001439
1440 /*
1441 * Check for valid input
1442 */
1443 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001445
1446 /*
1447 * Determine buffer content. Buffer contains either one DER certificate or
1448 * one or more PEM certificates.
1449 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001451 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001452 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1453 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001455 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457 if( buf_format == MBEDTLS_X509_FORMAT_DER )
1458 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Janos Follath98e28a72016-05-31 14:03:54 +01001459#else
1460 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
1461#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463#if defined(MBEDTLS_PEM_PARSE_C)
1464 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001465 {
Janos Follath865b3eb2019-12-16 11:46:15 +00001466 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001467 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001468
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001469 /* 1 rather than 0 since the terminating NULL byte is counted in */
1470 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001471 {
1472 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001474
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001475 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001476 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001477 "-----BEGIN CERTIFICATE-----",
1478 "-----END CERTIFICATE-----",
1479 buf, NULL, 0, &use_len );
1480
1481 if( ret == 0 )
1482 {
1483 /*
1484 * Was PEM encoded
1485 */
1486 buflen -= use_len;
1487 buf += use_len;
1488 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001489 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001490 {
1491 return( ret );
1492 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001493 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001494 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001495 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001496
1497 /*
1498 * PEM header and footer were found
1499 */
1500 buflen -= use_len;
1501 buf += use_len;
1502
1503 if( first_error == 0 )
1504 first_error = ret;
1505
Paul Bakker5a5fa922014-09-26 14:53:04 +02001506 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001507 continue;
1508 }
1509 else
1510 break;
1511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001512 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001514 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001515
1516 if( ret != 0 )
1517 {
1518 /*
1519 * Quit parsing on a memory error
1520 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001521 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001522 return( ret );
1523
1524 if( first_error == 0 )
1525 first_error = ret;
1526
1527 total_failed++;
1528 continue;
1529 }
1530
1531 success = 1;
1532 }
1533 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001534
1535 if( success )
1536 return( total_failed );
1537 else if( first_error )
1538 return( first_error );
1539 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001540 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Janos Follath98e28a72016-05-31 14:03:54 +01001541#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001542}
1543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001544#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001545/*
1546 * Load one or more certificates and add them to the chained list
1547 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001548int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001549{
Janos Follath865b3eb2019-12-16 11:46:15 +00001550 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001551 size_t n;
1552 unsigned char *buf;
1553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001555 return( ret );
1556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001557 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001558
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001559 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001560 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001561
1562 return( ret );
1563}
1564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001565int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001566{
1567 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001568#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001569 int w_ret;
1570 WCHAR szDir[MAX_PATH];
1571 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001572 char *p;
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001573 size_t len = strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001574
Paul Bakker9af723c2014-05-01 13:03:14 +02001575 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001576 HANDLE hFind;
1577
1578 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001579 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001580
Paul Bakker9af723c2014-05-01 13:03:14 +02001581 memset( szDir, 0, sizeof(szDir) );
1582 memset( filename, 0, MAX_PATH );
1583 memcpy( filename, path, len );
1584 filename[len++] = '\\';
1585 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001586 filename[len++] = '*';
1587
Simon B3c6b18d2016-11-03 01:11:37 +00001588 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001589 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001590 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001591 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001592
1593 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001594 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001595 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001596
1597 len = MAX_PATH - len;
1598 do
1599 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001600 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001601
1602 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1603 continue;
1604
Paul Bakker9af723c2014-05-01 13:03:14 +02001605 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001606 lstrlenW( file_data.cFileName ),
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001607 p, (int) len - 1,
Paul Bakker9af723c2014-05-01 13:03:14 +02001608 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001609 if( w_ret == 0 )
Ron Eldor36d90422017-01-09 15:09:16 +02001610 {
1611 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1612 goto cleanup;
1613 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001616 if( w_ret < 0 )
1617 ret++;
1618 else
1619 ret += w_ret;
1620 }
1621 while( FindNextFileW( hFind, &file_data ) != 0 );
1622
Paul Bakker66d5d072014-06-17 16:39:18 +02001623 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001624 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001625
Ron Eldor36d90422017-01-09 15:09:16 +02001626cleanup:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001627 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001628#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001629 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001630 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001631 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001632 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001633 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001634 DIR *dir = opendir( path );
1635
Paul Bakker66d5d072014-06-17 16:39:18 +02001636 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001637 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001638
Ron Eldor63140682017-01-09 19:27:59 +02001639#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001640 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001641 {
1642 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001643 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001644 }
Ron Eldor63140682017-01-09 19:27:59 +02001645#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001646
Paul Elliottfb91a482021-03-05 14:17:51 +00001647 memset( &sb, 0, sizeof( sb ) );
1648
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001649 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001650 {
Andres AGf9113192016-09-02 14:06:04 +01001651 snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name,
1652 "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001653
Andres AGf9113192016-09-02 14:06:04 +01001654 if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001655 {
Andres AGf9113192016-09-02 14:06:04 +01001656 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1657 goto cleanup;
1658 }
1659 else if( stat( entry_name, &sb ) == -1 )
1660 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001661 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001662 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001663 }
1664
1665 if( !S_ISREG( sb.st_mode ) )
1666 continue;
1667
1668 // Ignore parse errors
1669 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001670 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001671 if( t_ret < 0 )
1672 ret++;
1673 else
1674 ret += t_ret;
1675 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001676
1677cleanup:
Andres AGf9113192016-09-02 14:06:04 +01001678 closedir( dir );
1679
Ron Eldor63140682017-01-09 19:27:59 +02001680#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001681 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001682 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Ron Eldor63140682017-01-09 19:27:59 +02001683#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001684
Paul Bakkerbe089b02013-10-14 15:51:50 +02001685#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001686
1687 return( ret );
1688}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001689#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001690
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001691/*
1692 * OtherName ::= SEQUENCE {
1693 * type-id OBJECT IDENTIFIER,
1694 * value [0] EXPLICIT ANY DEFINED BY type-id }
1695 *
1696 * HardwareModuleName ::= SEQUENCE {
1697 * hwType OBJECT IDENTIFIER,
1698 * hwSerialNum OCTET STRING }
1699 *
1700 * NOTE: we currently only parse and use otherName of type HwModuleName,
1701 * as defined in RFC 4108.
1702 */
1703static int x509_get_other_name( const mbedtls_x509_buf *subject_alt_name,
1704 mbedtls_x509_san_other_name *other_name )
1705{
Janos Follathab23cd12019-05-09 13:53:57 +01001706 int ret = 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001707 size_t len;
1708 unsigned char *p = subject_alt_name->p;
1709 const unsigned char *end = p + subject_alt_name->len;
1710 mbedtls_x509_buf cur_oid;
1711
1712 if( ( subject_alt_name->tag &
1713 ( MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK ) ) !=
1714 ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ) )
1715 {
1716 /*
1717 * The given subject alternative name is not of type "othername".
1718 */
1719 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
1720 }
1721
1722 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1723 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001724 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001725
1726 cur_oid.tag = MBEDTLS_ASN1_OID;
1727 cur_oid.p = p;
1728 cur_oid.len = len;
1729
1730 /*
1731 * Only HwModuleName is currently supported.
1732 */
1733 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid ) != 0 )
1734 {
1735 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1736 }
1737
Ron Eldor890819a2019-05-13 19:03:04 +03001738 if( p + len >= end )
1739 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001740 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001741 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1742 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001743 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001744 p += len;
1745 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1746 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001747 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001748
1749 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1750 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001751 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001752
1753 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001754 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001755
1756 other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID;
1757 other_name->value.hardware_module_name.oid.p = p;
1758 other_name->value.hardware_module_name.oid.len = len;
1759
Ron Eldor890819a2019-05-13 19:03:04 +03001760 if( p + len >= end )
1761 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001762 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001763 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1764 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001765 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001766 p += len;
1767 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1768 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001769 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001770
1771 other_name->value.hardware_module_name.val.tag = MBEDTLS_ASN1_OCTET_STRING;
1772 other_name->value.hardware_module_name.val.p = p;
1773 other_name->value.hardware_module_name.val.len = len;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001774 p += len;
1775 if( p != end )
1776 {
Ron Eldor890819a2019-05-13 19:03:04 +03001777 mbedtls_platform_zeroize( other_name,
Sébastien Duquette661d7252019-06-23 17:45:26 -04001778 sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001779 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1780 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001781 }
1782 return( 0 );
1783}
1784
Peter Kolbus9a969b62018-12-11 13:55:56 -06001785int mbedtls_x509_parse_subject_alt_name( const mbedtls_x509_buf *san_buf,
1786 mbedtls_x509_subject_alternative_name *san )
1787{
1788 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1789 switch( san_buf->tag &
1790 ( MBEDTLS_ASN1_TAG_CLASS_MASK |
1791 MBEDTLS_ASN1_TAG_VALUE_MASK ) )
1792 {
1793 /*
1794 * otherName
1795 */
1796 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ):
1797 {
1798 mbedtls_x509_san_other_name other_name;
1799
1800 ret = x509_get_other_name( san_buf, &other_name );
1801 if( ret != 0 )
1802 return( ret );
1803
1804 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1805 san->type = MBEDTLS_X509_SAN_OTHER_NAME;
1806 memcpy( &san->san.other_name,
1807 &other_name, sizeof( other_name ) );
1808
1809 }
1810 break;
1811
1812 /*
1813 * dNSName
1814 */
1815 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME ):
1816 {
1817 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1818 san->type = MBEDTLS_X509_SAN_DNS_NAME;
1819
1820 memcpy( &san->san.unstructured_name,
1821 san_buf, sizeof( *san_buf ) );
1822
1823 }
1824 break;
1825
1826 /*
1827 * Type not supported
1828 */
1829 default:
1830 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1831 }
1832 return( 0 );
1833}
1834
1835#if !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001836static int x509_info_subject_alt_name( char **buf, size_t *size,
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001837 const mbedtls_x509_sequence
1838 *subject_alt_name,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001839 const char *prefix )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001840{
Janos Follath865b3eb2019-12-16 11:46:15 +00001841 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001842 size_t n = *size;
1843 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001844 const mbedtls_x509_sequence *cur = subject_alt_name;
Ron Eldor890819a2019-05-13 19:03:04 +03001845 mbedtls_x509_subject_alternative_name san;
1846 int parse_ret;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001847
1848 while( cur != NULL )
1849 {
Ron Eldor890819a2019-05-13 19:03:04 +03001850 memset( &san, 0, sizeof( san ) );
1851 parse_ret = mbedtls_x509_parse_subject_alt_name( &cur->buf, &san );
1852 if( parse_ret != 0 )
1853 {
1854 if( parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1855 {
1856 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
1857 MBEDTLS_X509_SAFE_SNPRINTF;
1858 }
1859 else
1860 {
1861 ret = mbedtls_snprintf( p, n, "\n%s <malformed>", prefix );
1862 MBEDTLS_X509_SAFE_SNPRINTF;
1863 }
1864 cur = cur->next;
1865 continue;
1866 }
1867
1868 switch( san.type )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001869 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001870 /*
1871 * otherName
1872 */
Ron Eldora2913912019-05-16 16:17:38 +03001873 case MBEDTLS_X509_SAN_OTHER_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001874 {
1875 mbedtls_x509_san_other_name *other_name = &san.san.other_name;
1876
1877 ret = mbedtls_snprintf( p, n, "\n%s otherName :", prefix );
1878 MBEDTLS_X509_SAFE_SNPRINTF;
1879
1880 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME,
1881 &other_name->value.hardware_module_name.oid ) != 0 )
Janos Follath22f605f2019-05-10 10:37:17 +01001882 {
Ron Eldor890819a2019-05-13 19:03:04 +03001883 ret = mbedtls_snprintf( p, n, "\n%s hardware module name :", prefix );
1884 MBEDTLS_X509_SAFE_SNPRINTF;
1885 ret = mbedtls_snprintf( p, n, "\n%s hardware type : ", prefix );
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001886 MBEDTLS_X509_SAFE_SNPRINTF;
1887
Ron Eldor890819a2019-05-13 19:03:04 +03001888 ret = mbedtls_oid_get_numeric_string( p, n, &other_name->value.hardware_module_name.oid );
1889 MBEDTLS_X509_SAFE_SNPRINTF;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001890
Ron Eldor890819a2019-05-13 19:03:04 +03001891 ret = mbedtls_snprintf( p, n, "\n%s hardware serial number : ", prefix );
1892 MBEDTLS_X509_SAFE_SNPRINTF;
1893
1894 if( other_name->value.hardware_module_name.val.len >= n )
1895 {
1896 *p = '\0';
1897 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Janos Follath22f605f2019-05-10 10:37:17 +01001898 }
1899
Ron Eldora2913912019-05-16 16:17:38 +03001900 memcpy( p, other_name->value.hardware_module_name.val.p,
1901 other_name->value.hardware_module_name.val.len );
1902 p += other_name->value.hardware_module_name.val.len;
1903
Ron Eldor890819a2019-05-13 19:03:04 +03001904 n -= other_name->value.hardware_module_name.val.len;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001905
Ron Eldor890819a2019-05-13 19:03:04 +03001906 }/* MBEDTLS_OID_ON_HW_MODULE_NAME */
1907 }
1908 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001909
1910 /*
1911 * dNSName
1912 */
Ron Eldora2913912019-05-16 16:17:38 +03001913 case MBEDTLS_X509_SAN_DNS_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001914 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001915 ret = mbedtls_snprintf( p, n, "\n%s dNSName : ", prefix );
1916 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldor890819a2019-05-13 19:03:04 +03001917 if( san.san.unstructured_name.len >= n )
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001918 {
1919 *p = '\0';
1920 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
1921 }
Ron Eldora2913912019-05-16 16:17:38 +03001922
1923 memcpy( p, san.san.unstructured_name.p, san.san.unstructured_name.len );
1924 p += san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001925 n -= san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001926 }
1927 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001928
1929 /*
Ron Eldor890819a2019-05-13 19:03:04 +03001930 * Type not supported, skip item.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001931 */
1932 default:
Janos Follath22f605f2019-05-10 10:37:17 +01001933 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
1934 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001935 break;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001936 }
1937
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001938 cur = cur->next;
1939 }
1940
1941 *p = '\0';
1942
1943 *size = n;
1944 *buf = p;
1945
1946 return( 0 );
1947}
1948
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001949#define PRINT_ITEM(i) \
1950 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001951 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001952 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001953 sep = ", "; \
1954 }
1955
1956#define CERT_TYPE(type,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001957 if( ns_cert_type & (type) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001958 PRINT_ITEM( name );
1959
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001960static int x509_info_cert_type( char **buf, size_t *size,
1961 unsigned char ns_cert_type )
1962{
Janos Follath865b3eb2019-12-16 11:46:15 +00001963 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001964 size_t n = *size;
1965 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001966 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001968 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001969 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001970 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
1971 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1972 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001973 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
1974 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1975 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001976
1977 *size = n;
1978 *buf = p;
1979
1980 return( 0 );
1981}
1982
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001983#define KEY_USAGE(code,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001984 if( key_usage & (code) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001985 PRINT_ITEM( name );
1986
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001987static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001988 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001989{
Janos Follath865b3eb2019-12-16 11:46:15 +00001990 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001991 size_t n = *size;
1992 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001993 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001995 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
1996 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001997 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1998 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1999 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002000 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
2001 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02002002 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
2003 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002004
2005 *size = n;
2006 *buf = p;
2007
2008 return( 0 );
2009}
2010
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002011static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002012 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002013{
Janos Follath865b3eb2019-12-16 11:46:15 +00002014 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002015 const char *desc;
2016 size_t n = *size;
2017 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002018 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002019 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002020
2021 while( cur != NULL )
2022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002023 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002024 desc = "???";
2025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002026 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002027 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002028
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002029 sep = ", ";
2030
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002031 cur = cur->next;
2032 }
2033
2034 *size = n;
2035 *buf = p;
2036
2037 return( 0 );
2038}
2039
Ron Eldor74d9acc2019-03-21 14:00:03 +02002040static int x509_info_cert_policies( char **buf, size_t *size,
2041 const mbedtls_x509_sequence *certificate_policies )
2042{
Janos Follath865b3eb2019-12-16 11:46:15 +00002043 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor74d9acc2019-03-21 14:00:03 +02002044 const char *desc;
2045 size_t n = *size;
2046 char *p = *buf;
2047 const mbedtls_x509_sequence *cur = certificate_policies;
2048 const char *sep = "";
2049
2050 while( cur != NULL )
2051 {
2052 if( mbedtls_oid_get_certificate_policies( &cur->buf, &desc ) != 0 )
2053 desc = "???";
2054
2055 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
2056 MBEDTLS_X509_SAFE_SNPRINTF;
2057
2058 sep = ", ";
2059
2060 cur = cur->next;
2061 }
2062
2063 *size = n;
2064 *buf = p;
2065
2066 return( 0 );
2067}
2068
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002069/*
2070 * Return an informational string about the certificate.
2071 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002072#define BEFORE_COLON 18
2073#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002074int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
2075 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002076{
Janos Follath865b3eb2019-12-16 11:46:15 +00002077 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002078 size_t n;
2079 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002080 char key_size_str[BEFORE_COLON];
2081
2082 p = buf;
2083 n = size;
2084
Janos Follath98e28a72016-05-31 14:03:54 +01002085 if( NULL == crt )
2086 {
2087 ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" );
2088 MBEDTLS_X509_SAFE_SNPRINTF;
2089
2090 return( (int) ( size - n ) );
2091 }
2092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002093 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002094 prefix, crt->version );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002095 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002096 ret = mbedtls_snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002097 prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002098 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002100 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002101 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002103 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002104 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002105 ret = mbedtls_x509_dn_gets( p, n, &crt->issuer );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002106 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002108 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002109 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002110 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002111 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002113 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002114 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2115 crt->valid_from.year, crt->valid_from.mon,
2116 crt->valid_from.day, crt->valid_from.hour,
2117 crt->valid_from.min, crt->valid_from.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002118 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002120 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002121 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2122 crt->valid_to.year, crt->valid_to.mon,
2123 crt->valid_to.day, crt->valid_to.hour,
2124 crt->valid_to.min, crt->valid_to.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002125 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002127 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002128 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002130 ret = mbedtls_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02002131 crt->sig_md, crt->sig_opts );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002132 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002133
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002134 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002135 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
2136 mbedtls_pk_get_name( &crt->pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002137 {
2138 return( ret );
2139 }
2140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002141 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +02002142 (int) mbedtls_pk_get_bitlen( &crt->pk ) );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002143 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002144
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002145 /*
2146 * Optional extensions
2147 */
2148
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002149 if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002150 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002151 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002152 crt->ca_istrue ? "true" : "false" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002153 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002154
2155 if( crt->max_pathlen > 0 )
2156 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002157 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002158 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002159 }
2160 }
2161
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002162 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002163 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002164 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name :", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002165 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002166
2167 if( ( ret = x509_info_subject_alt_name( &p, &n,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002168 &crt->subject_alt_names,
Ron Eldor6aeae9e2019-05-20 12:00:36 +03002169 prefix ) ) != 0 )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002170 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002171 }
2172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002173 if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002174 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002175 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002176 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002177
2178 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
2179 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002180 }
2181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002182 if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002184 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002185 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002186
2187 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
2188 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002189 }
2190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002191 if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002193 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002194 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002195
2196 if( ( ret = x509_info_ext_key_usage( &p, &n,
2197 &crt->ext_key_usage ) ) != 0 )
2198 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002199 }
2200
Ron Eldor74d9acc2019-03-21 14:00:03 +02002201 if( crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES )
2202 {
2203 ret = mbedtls_snprintf( p, n, "\n%scertificate policies : ", prefix );
2204 MBEDTLS_X509_SAFE_SNPRINTF;
2205
2206 if( ( ret = x509_info_cert_policies( &p, &n,
2207 &crt->certificate_policies ) ) != 0 )
2208 return( ret );
2209 }
2210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002211 ret = mbedtls_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002212 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002213
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002214 return( (int) ( size - n ) );
2215}
2216
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002217struct x509_crt_verify_string {
2218 int code;
2219 const char *string;
2220};
2221
Hanno Becker7ac83f92020-10-09 10:48:22 +01002222#define X509_CRT_ERROR_INFO( err, err_str, info ) { err, info },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002223static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Hanno Becker7ac83f92020-10-09 10:48:22 +01002224 MBEDTLS_X509_CRT_ERROR_INFO_LIST
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002225 { 0, NULL }
2226};
Hanno Becker7ac83f92020-10-09 10:48:22 +01002227#undef X509_CRT_ERROR_INFO
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002228
2229int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002230 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002231{
Janos Follath865b3eb2019-12-16 11:46:15 +00002232 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002233 const struct x509_crt_verify_string *cur;
2234 char *p = buf;
2235 size_t n = size;
2236
2237 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
2238 {
2239 if( ( flags & cur->code ) == 0 )
2240 continue;
2241
2242 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002243 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002244 flags ^= cur->code;
2245 }
2246
2247 if( flags != 0 )
2248 {
2249 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
2250 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002251 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002252 }
2253
2254 return( (int) ( size - n ) );
2255}
Hanno Becker612a2f12020-10-09 09:19:39 +01002256#endif /* MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002257
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002258int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
2259 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002260{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002261 unsigned int usage_must, usage_may;
2262 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
2263 | MBEDTLS_X509_KU_DECIPHER_ONLY;
2264
2265 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
2266 return( 0 );
2267
2268 usage_must = usage & ~may_mask;
2269
2270 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
2271 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
2272
2273 usage_may = usage & may_mask;
2274
2275 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002276 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002277
2278 return( 0 );
2279}
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002281int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002282 const char *usage_oid,
2283 size_t usage_len )
2284{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002285 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002286
2287 /* Extension is not mandatory, absent means no restriction */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002288 if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002289 return( 0 );
2290
2291 /*
2292 * Look for the requested usage (or wildcard ANY) in our list
2293 */
2294 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
2295 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002296 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002297
2298 if( cur_oid->len == usage_len &&
2299 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
2300 {
2301 return( 0 );
2302 }
2303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002304 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002305 return( 0 );
2306 }
2307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002308 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002309}
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002311#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002312/*
2313 * Return 1 if the certificate is revoked, or 0 otherwise.
2314 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002315int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002316{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002317 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002318
2319 while( cur != NULL && cur->serial.len != 0 )
2320 {
2321 if( crt->serial.len == cur->serial.len &&
2322 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
2323 {
Raoul Strackxa4e86142020-06-15 17:03:13 +02002324 return( 1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002325 }
2326
2327 cur = cur->next;
2328 }
2329
2330 return( 0 );
2331}
2332
2333/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002334 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002335 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002336 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002337static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002338 mbedtls_x509_crl *crl_list,
2339 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002340{
2341 int flags = 0;
pespacek7599a772022-02-07 14:40:55 +01002342#if defined(MBEDTLS_USE_PSA_CRYPTO)
2343 unsigned char hash[PSA_HASH_MAX_SIZE];
2344 psa_algorithm_t psa_algorithm;
2345#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002346 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2347 const mbedtls_md_info_t *md_info;
pespacek7599a772022-02-07 14:40:55 +01002348#endif /* MBEDTLS_USE_PSA_CRYPTO */
2349 size_t hash_length;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002350
2351 if( ca == NULL )
2352 return( flags );
2353
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002354 while( crl_list != NULL )
2355 {
2356 if( crl_list->version == 0 ||
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002357 x509_name_cmp( &crl_list->issuer, &ca->subject ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002358 {
2359 crl_list = crl_list->next;
2360 continue;
2361 }
2362
2363 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002364 * Check if the CA is configured to sign CRLs
2365 */
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002366 if( mbedtls_x509_crt_check_key_usage( ca,
2367 MBEDTLS_X509_KU_CRL_SIGN ) != 0 )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002368 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002369 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002370 break;
2371 }
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002372
2373 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002374 * Check if CRL is correctly signed by the trusted CA
2375 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002376 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
2377 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
2378
2379 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
2380 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
2381
pespacek7599a772022-02-07 14:40:55 +01002382#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02002383 psa_algorithm = mbedtls_hash_info_psa_from_md( crl_list->sig_md );
pespacekd924e552022-02-28 11:49:54 +01002384 if( psa_hash_compute( psa_algorithm,
2385 crl_list->tbs.p,
2386 crl_list->tbs.len,
2387 hash,
2388 sizeof( hash ),
2389 &hash_length ) != PSA_SUCCESS )
pespaceka7a64692022-02-14 15:18:43 +01002390 {
2391 /* Note: this can't happen except after an internal error */
2392 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
2393 break;
2394 }
pespacek7599a772022-02-07 14:40:55 +01002395#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002396 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
pespacek7599a772022-02-07 14:40:55 +01002397 hash_length = mbedtls_md_get_size( md_info );
2398 if( mbedtls_md( md_info,
2399 crl_list->tbs.p,
2400 crl_list->tbs.len,
2401 hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002402 {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002403 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002404 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002405 break;
2406 }
pespaceka7a64692022-02-14 15:18:43 +01002407#endif /* MBEDTLS_USE_PSA_CRYPTO */
2408
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002409 if( x509_profile_check_key( profile, &ca->pk ) != 0 )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002410 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002412 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
pespacek7599a772022-02-07 14:40:55 +01002413 crl_list->sig_md, hash, hash_length,
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02002414 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002415 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002416 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002417 break;
2418 }
2419
2420 /*
2421 * Check for validity of CRL (Do not drop out)
2422 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002423 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002424 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002425
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002426 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002427 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002428
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002429 /*
2430 * Check if certificate is revoked
2431 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002432 if( mbedtls_x509_crt_is_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002433 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002434 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002435 break;
2436 }
2437
2438 crl_list = crl_list->next;
2439 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002440
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002441 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002442}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002443#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002444
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002445/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002446 * Check the signature of a certificate by its parent
2447 */
2448static int x509_crt_check_signature( const mbedtls_x509_crt *child,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002449 mbedtls_x509_crt *parent,
2450 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002451{
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002452 size_t hash_len;
2453#if !defined(MBEDTLS_USE_PSA_CRYPTO)
pespacek7599a772022-02-07 14:40:55 +01002454 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002455 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002456 md_info = mbedtls_md_info_from_type( child->sig_md );
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002457 hash_len = mbedtls_md_get_size( md_info );
Andrzej Kurek8b38ff52018-11-20 03:20:09 -05002458
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002459 /* Note: hash errors can happen only after an internal error */
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002460 if( mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash ) != 0 )
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002461 return( -1 );
2462#else
pespacek7599a772022-02-07 14:40:55 +01002463 unsigned char hash[PSA_HASH_MAX_SIZE];
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02002464 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md( child->sig_md );
pespacek7599a772022-02-07 14:40:55 +01002465 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002466
pespacek7599a772022-02-07 14:40:55 +01002467 status = psa_hash_compute( hash_alg,
2468 child->tbs.p,
2469 child->tbs.len,
2470 hash,
pespacekb9f07a72022-02-14 15:13:26 +01002471 sizeof( hash ),
pespacek7599a772022-02-07 14:40:55 +01002472 &hash_len );
2473 if( status != PSA_SUCCESS )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002474 {
pespacek3110c7b2022-02-14 15:07:41 +01002475 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002476 }
2477
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002478#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002479 /* Skip expensive computation on obvious mismatch */
2480 if( ! mbedtls_pk_can_do( &parent->pk, child->sig_pk ) )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002481 return( -1 );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002482
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002483#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002484 if( rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA )
2485 {
2486 return( mbedtls_pk_verify_restartable( &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002487 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002488 child->sig.p, child->sig.len, &rs_ctx->pk ) );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002489 }
2490#else
2491 (void) rs_ctx;
2492#endif
2493
2494 return( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002495 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002496 child->sig.p, child->sig.len ) );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002497}
2498
2499/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002500 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2501 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002502 *
2503 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002504 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002505static int x509_crt_check_parent( const mbedtls_x509_crt *child,
2506 const mbedtls_x509_crt *parent,
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002507 int top )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002508{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002509 int need_ca_bit;
2510
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002511 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02002512 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002513 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002514
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002515 /* Parent must have the basicConstraints CA bit set as a general rule */
2516 need_ca_bit = 1;
2517
2518 /* Exception: v1/v2 certificates that are locally trusted. */
2519 if( top && parent->version < 3 )
2520 need_ca_bit = 0;
2521
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002522 if( need_ca_bit && ! parent->ca_istrue )
2523 return( -1 );
2524
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002525 if( need_ca_bit &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002526 mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002527 {
2528 return( -1 );
2529 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002530
2531 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002532}
2533
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002534/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002535 * Find a suitable parent for child in candidates, or return NULL.
2536 *
2537 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002538 * 1. subject name matches child's issuer
2539 * 2. if necessary, the CA bit is set and key usage allows signing certs
2540 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002541 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002542 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002543 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002544 * If there's a suitable candidate which is also time-valid, return the first
2545 * such. Otherwise, return the first suitable candidate (or NULL if there is
2546 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002547 *
2548 * The rationale for this rule is that someone could have a list of trusted
2549 * roots with two versions on the same root with different validity periods.
2550 * (At least one user reported having such a list and wanted it to just work.)
2551 * The reason we don't just require time-validity is that generally there is
2552 * only one version, and if it's expired we want the flags to state that
2553 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002554 *
2555 * The rationale for rule 3 (signature for trusted roots) is that users might
2556 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002557 * way we select the correct one is by checking the signature (as we don't
2558 * rely on key identifier extensions). (This is one way users might choose to
2559 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002560 *
2561 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002562 * - [in] child: certificate for which we're looking for a parent
2563 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002564 * - [out] r_parent: parent found (or NULL)
2565 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002566 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2567 * of the chain, 0 otherwise
2568 * - [in] path_cnt: number of intermediates seen so far
2569 * - [in] self_cnt: number of self-signed intermediates seen so far
2570 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002571 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002572 *
2573 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002574 * - 0 on success
2575 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002576 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002577static int x509_crt_find_parent_in(
2578 mbedtls_x509_crt *child,
2579 mbedtls_x509_crt *candidates,
2580 mbedtls_x509_crt **r_parent,
2581 int *r_signature_is_good,
2582 int top,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002583 unsigned path_cnt,
2584 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002585 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002586{
Janos Follath865b3eb2019-12-16 11:46:15 +00002587 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002588 mbedtls_x509_crt *parent, *fallback_parent;
Benjamin Kier36050732019-05-30 14:49:17 -04002589 int signature_is_good = 0, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002590
2591#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002592 /* did we have something in progress? */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002593 if( rs_ctx != NULL && rs_ctx->parent != NULL )
2594 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002595 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002596 parent = rs_ctx->parent;
2597 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002598 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002599
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002600 /* clear saved state */
2601 rs_ctx->parent = NULL;
2602 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002603 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002604
2605 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002606 goto check_signature;
2607 }
2608#endif
2609
2610 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002611 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002612
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002613 for( parent = candidates; parent != NULL; parent = parent->next )
2614 {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002615 /* basic parenting skills (name, CA bit, key usage) */
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002616 if( x509_crt_check_parent( child, parent, top ) != 0 )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002617 continue;
2618
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002619 /* +1 because stored max_pathlen is 1 higher that the actual value */
2620 if( parent->max_pathlen > 0 &&
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002621 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt )
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002622 {
2623 continue;
2624 }
2625
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002626 /* Signature */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002627#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2628check_signature:
2629#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002630 ret = x509_crt_check_signature( child, parent, rs_ctx );
2631
2632#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002633 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2634 {
2635 /* save state */
2636 rs_ctx->parent = parent;
2637 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002638 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002639
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002640 return( ret );
2641 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002642#else
2643 (void) ret;
2644#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002645
2646 signature_is_good = ret == 0;
2647 if( top && ! signature_is_good )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002648 continue;
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002649
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002650 /* optional time check */
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002651 if( mbedtls_x509_time_is_past( &parent->valid_to ) ||
2652 mbedtls_x509_time_is_future( &parent->valid_from ) )
2653 {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002654 if( fallback_parent == NULL )
2655 {
2656 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002657 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002658 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002659
2660 continue;
2661 }
2662
Andy Gross1f627142019-01-30 10:25:53 -06002663 *r_parent = parent;
2664 *r_signature_is_good = signature_is_good;
2665
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002666 break;
2667 }
2668
Andy Gross1f627142019-01-30 10:25:53 -06002669 if( parent == NULL )
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002670 {
2671 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002672 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002673 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002674
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002675 return( 0 );
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002676}
2677
2678/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002679 * Find a parent in trusted CAs or the provided chain, or return NULL.
2680 *
2681 * Searches in trusted CAs first, and return the first suitable parent found
2682 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002683 *
2684 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002685 * - [in] child: certificate for which we're looking for a parent, followed
2686 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002687 * - [in] trust_ca: list of locally trusted certificates
2688 * - [out] parent: parent found (or NULL)
2689 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2690 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2691 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2692 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002693 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002694 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002695 *
2696 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002697 * - 0 on success
2698 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002699 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002700static int x509_crt_find_parent(
2701 mbedtls_x509_crt *child,
2702 mbedtls_x509_crt *trust_ca,
2703 mbedtls_x509_crt **parent,
2704 int *parent_is_trusted,
2705 int *signature_is_good,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002706 unsigned path_cnt,
2707 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002708 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002709{
Janos Follath865b3eb2019-12-16 11:46:15 +00002710 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002711 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002712
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002713 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002714
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002715#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002716 /* restore then clear saved state if we have some stored */
2717 if( rs_ctx != NULL && rs_ctx->parent_is_trusted != -1 )
2718 {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002719 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002720 rs_ctx->parent_is_trusted = -1;
2721 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002722#endif
2723
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002724 while( 1 ) {
2725 search_list = *parent_is_trusted ? trust_ca : child->next;
2726
2727 ret = x509_crt_find_parent_in( child, search_list,
2728 parent, signature_is_good,
2729 *parent_is_trusted,
2730 path_cnt, self_cnt, rs_ctx );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002731
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002732#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002733 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2734 {
2735 /* save state */
2736 rs_ctx->parent_is_trusted = *parent_is_trusted;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002737 return( ret );
2738 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002739#else
2740 (void) ret;
2741#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002742
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002743 /* stop here if found or already in second iteration */
2744 if( *parent != NULL || *parent_is_trusted == 0 )
2745 break;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002746
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002747 /* prepare second iteration */
2748 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002749 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002750
2751 /* extra precaution against mistakes in the caller */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002752 if( *parent == NULL )
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002753 {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002754 *parent_is_trusted = 0;
2755 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002756 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002757
2758 return( 0 );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002759}
2760
2761/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002762 * Check if an end-entity certificate is locally trusted
2763 *
2764 * Currently we require such certificates to be self-signed (actually only
2765 * check for self-issued as self-signatures are not checked)
2766 */
2767static int x509_crt_check_ee_locally_trusted(
2768 mbedtls_x509_crt *crt,
2769 mbedtls_x509_crt *trust_ca )
2770{
2771 mbedtls_x509_crt *cur;
2772
2773 /* must be self-issued */
2774 if( x509_name_cmp( &crt->issuer, &crt->subject ) != 0 )
2775 return( -1 );
2776
2777 /* look for an exact match with trusted cert */
2778 for( cur = trust_ca; cur != NULL; cur = cur->next )
2779 {
2780 if( crt->raw.len == cur->raw.len &&
2781 memcmp( crt->raw.p, cur->raw.p, crt->raw.len ) == 0 )
2782 {
2783 return( 0 );
2784 }
2785 }
2786
2787 /* too bad */
2788 return( -1 );
2789}
2790
2791/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002792 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002793 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002794 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2795 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002796 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002797 * such that every cert in the chain is a child of the next one,
2798 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002799 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002800 * Verify that chain and return it with flags for all issues found.
2801 *
2802 * Special cases:
2803 * - EE == Rj -> return a one-element list containing it
2804 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2805 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002806 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002807 * Tests for (aspects of) this function should include at least:
2808 * - trusted EE
2809 * - EE -> trusted root
Antonin Décimo36e89b52019-01-23 15:24:37 +01002810 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002811 * - if relevant: EE untrusted
2812 * - if relevant: EE -> intermediate, untrusted
2813 * with the aspect under test checked at each relevant level (EE, int, root).
2814 * For some aspects longer chains are required, but usually length 2 is
2815 * enough (but length 1 is not in general).
2816 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002817 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002818 * - [in] crt: the cert list EE, C1, ..., Cn
2819 * - [in] trust_ca: the trusted list R1, ..., Rp
2820 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002821 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002822 * Only valid when return value is 0, may contain garbage otherwise!
2823 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002824 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002825 *
2826 * Return value:
2827 * - non-zero if the chain could not be fully built and examined
2828 * - 0 is the chain was successfully built and examined,
2829 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002830 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002831static int x509_crt_verify_chain(
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002832 mbedtls_x509_crt *crt,
2833 mbedtls_x509_crt *trust_ca,
2834 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00002835 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2836 void *p_ca_cb,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002837 const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002838 mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002839 mbedtls_x509_crt_restart_ctx *rs_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002840{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002841 /* Don't initialize any of those variables here, so that the compiler can
2842 * catch potential issues with jumping ahead when restarting */
Janos Follath865b3eb2019-12-16 11:46:15 +00002843 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002844 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002845 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002846 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002847 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002848 int parent_is_trusted;
2849 int child_is_trusted;
2850 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002851 unsigned self_cnt;
Hanno Beckerf53893b2019-03-28 13:45:55 +00002852 mbedtls_x509_crt *cur_trust_ca = NULL;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002853
2854#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2855 /* resume if we had an operation in progress */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002856 if( rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent )
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002857 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002858 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002859 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002860 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002861
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002862 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002863 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002864 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002865 flags = &cur->flags;
2866
2867 goto find_parent;
2868 }
2869#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002870
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002871 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002872 self_cnt = 0;
2873 parent_is_trusted = 0;
2874 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002875
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002876 while( 1 ) {
2877 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002878 cur = &ver_chain->items[ver_chain->len];
2879 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002880 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002881 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002882 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002883
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002884 /* Check time-validity (all certificates) */
2885 if( mbedtls_x509_time_is_past( &child->valid_to ) )
2886 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002887
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002888 if( mbedtls_x509_time_is_future( &child->valid_from ) )
2889 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002890
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002891 /* Stop here for trusted roots (but not for trusted EE certs) */
2892 if( child_is_trusted )
2893 return( 0 );
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002894
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002895 /* Check signature algorithm: MD & PK algs */
2896 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
2897 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002898
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002899 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
2900 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002901
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002902 /* Special case: EE certs that are locally trusted */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002903 if( ver_chain->len == 1 &&
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002904 x509_crt_check_ee_locally_trusted( child, trust_ca ) == 0 )
2905 {
2906 return( 0 );
2907 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002908
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002909#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2910find_parent:
2911#endif
Hanno Beckerf53893b2019-03-28 13:45:55 +00002912
2913 /* Obtain list of potential trusted signers from CA callback,
2914 * or use statically provided list. */
2915#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
2916 if( f_ca_cb != NULL )
2917 {
2918 mbedtls_x509_crt_free( ver_chain->trust_ca_cb_result );
2919 mbedtls_free( ver_chain->trust_ca_cb_result );
2920 ver_chain->trust_ca_cb_result = NULL;
2921
2922 ret = f_ca_cb( p_ca_cb, child, &ver_chain->trust_ca_cb_result );
2923 if( ret != 0 )
2924 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2925
2926 cur_trust_ca = ver_chain->trust_ca_cb_result;
2927 }
2928 else
2929#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2930 {
2931 ((void) f_ca_cb);
2932 ((void) p_ca_cb);
2933 cur_trust_ca = trust_ca;
2934 }
2935
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002936 /* Look for a parent in trusted CAs or up the chain */
Hanno Beckerf53893b2019-03-28 13:45:55 +00002937 ret = x509_crt_find_parent( child, cur_trust_ca, &parent,
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002938 &parent_is_trusted, &signature_is_good,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002939 ver_chain->len - 1, self_cnt, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002940
2941#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002942 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2943 {
2944 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002945 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002946 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002947 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002948
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002949 return( ret );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002950 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002951#else
2952 (void) ret;
2953#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002954
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002955 /* No parent? We're done here */
2956 if( parent == NULL )
2957 {
2958 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2959 return( 0 );
2960 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002961
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002962 /* Count intermediate self-issued (not necessarily self-signed) certs.
2963 * These can occur with some strategies for key rollover, see [SIRO],
2964 * and should be excluded from max_pathlen checks. */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002965 if( ver_chain->len != 1 &&
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002966 x509_name_cmp( &child->issuer, &child->subject ) == 0 )
2967 {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002968 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002969 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002970
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002971 /* path_cnt is 0 for the first intermediate CA,
2972 * and if parent is trusted it's not an intermediate CA */
2973 if( ! parent_is_trusted &&
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002974 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002975 {
2976 /* return immediately to avoid overflow the chain array */
2977 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2978 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002979
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002980 /* signature was checked while searching parent */
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002981 if( ! signature_is_good )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002982 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2983
2984 /* check size of signing key */
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002985 if( x509_profile_check_key( profile, &parent->pk ) != 0 )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002986 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002988#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002989 /* Check trusted CA's CRL for the given crt */
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002990 *flags |= x509_crt_verifycrl( child, parent, ca_crl, profile );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002991#else
2992 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002993#endif
2994
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002995 /* prepare for next iteration */
2996 child = parent;
2997 parent = NULL;
2998 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002999 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003000 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003001}
3002
3003/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003004 * Check for CN match
3005 */
3006static int x509_crt_check_cn( const mbedtls_x509_buf *name,
3007 const char *cn, size_t cn_len )
3008{
3009 /* try exact match */
3010 if( name->len == cn_len &&
3011 x509_memcasecmp( cn, name->p, cn_len ) == 0 )
3012 {
3013 return( 0 );
3014 }
3015
3016 /* try wildcard match */
Manuel Pégourié-Gonnard900fba62017-10-18 14:28:11 +02003017 if( x509_check_wildcard( cn, name ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003018 {
3019 return( 0 );
3020 }
3021
3022 return( -1 );
3023}
3024
3025/*
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003026 * Check for SAN match, see RFC 5280 Section 4.2.1.6
3027 */
3028static int x509_crt_check_san( const mbedtls_x509_buf *name,
3029 const char *cn, size_t cn_len )
3030{
3031 const unsigned char san_type = (unsigned char) name->tag &
3032 MBEDTLS_ASN1_TAG_VALUE_MASK;
3033
3034 /* dNSName */
3035 if( san_type == MBEDTLS_X509_SAN_DNS_NAME )
3036 return( x509_crt_check_cn( name, cn, cn_len ) );
3037
3038 /* (We may handle other types here later.) */
3039
3040 /* Unrecognized type */
3041 return( -1 );
3042}
3043
3044/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003045 * Verify the requested CN - only call this if cn is not NULL!
3046 */
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003047static void x509_crt_verify_name( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003048 const char *cn,
3049 uint32_t *flags )
3050{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003051 const mbedtls_x509_name *name;
3052 const mbedtls_x509_sequence *cur;
3053 size_t cn_len = strlen( cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003054
3055 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
3056 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003057 for( cur = &crt->subject_alt_names; cur != NULL; cur = cur->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003058 {
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003059 if( x509_crt_check_san( &cur->buf, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003060 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003061 }
3062
3063 if( cur == NULL )
3064 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3065 }
3066 else
3067 {
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02003068 for( name = &crt->subject; name != NULL; name = name->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003069 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003070 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, &name->oid ) == 0 &&
3071 x509_crt_check_cn( &name->val, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003072 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003073 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003074 }
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003075 }
3076
3077 if( name == NULL )
3078 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3079 }
3080}
3081
3082/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003083 * Merge the flags for all certs in the chain, after calling callback
3084 */
3085static int x509_crt_merge_flags_with_cb(
3086 uint32_t *flags,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003087 const mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003088 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3089 void *p_vrfy )
3090{
Janos Follath865b3eb2019-12-16 11:46:15 +00003091 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003092 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003093 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003094 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003095
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003096 for( i = ver_chain->len; i != 0; --i )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003097 {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003098 cur = &ver_chain->items[i-1];
3099 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003100
3101 if( NULL != f_vrfy )
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003102 if( ( ret = f_vrfy( p_vrfy, cur->crt, (int) i-1, &cur_flags ) ) != 0 )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003103 return( ret );
3104
3105 *flags |= cur_flags;
3106 }
3107
3108 return( 0 );
3109}
3110
3111/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003112 * Verify the certificate validity, with profile, restartable version
3113 *
3114 * This function:
3115 * - checks the requested CN (if any)
3116 * - checks the type and size of the EE cert's key,
3117 * as that isn't done as part of chain building/verification currently
3118 * - builds and verifies the chain
3119 * - then calls the callback and merges the flags
Hanno Becker3116fb32019-03-28 13:34:42 +00003120 *
3121 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
3122 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
3123 * verification routine to search for trusted signers, and CRLs will
3124 * be disabled. Otherwise, `trust_ca` will be used as the static list
3125 * of trusted signers, and `ca_crl` will be use as the static list
3126 * of CRLs.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003127 */
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003128static int x509_crt_verify_restartable_ca_cb( mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003129 mbedtls_x509_crt *trust_ca,
3130 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003131 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3132 void *p_ca_cb,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003133 const mbedtls_x509_crt_profile *profile,
3134 const char *cn, uint32_t *flags,
3135 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3136 void *p_vrfy,
3137 mbedtls_x509_crt_restart_ctx *rs_ctx )
3138{
Janos Follath865b3eb2019-12-16 11:46:15 +00003139 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003140 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003141 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003142 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003143
3144 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003145 ee_flags = 0;
3146 x509_crt_verify_chain_reset( &ver_chain );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003147
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003148 if( profile == NULL )
3149 {
3150 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3151 goto exit;
3152 }
3153
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003154 /* check name if requested */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003155 if( cn != NULL )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003156 x509_crt_verify_name( crt, cn, &ee_flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003157
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003158 /* Check the type and size of the key */
3159 pk_type = mbedtls_pk_get_type( &crt->pk );
3160
3161 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003162 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003163
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02003164 if( x509_profile_check_key( profile, &crt->pk ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003165 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003166
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003167 /* Check the chain */
Hanno Becker3116fb32019-03-28 13:34:42 +00003168 ret = x509_crt_verify_chain( crt, trust_ca, ca_crl,
3169 f_ca_cb, p_ca_cb, profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003170 &ver_chain, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003171
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003172 if( ret != 0 )
3173 goto exit;
3174
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003175 /* Merge end-entity flags */
3176 ver_chain.items[0].flags |= ee_flags;
3177
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003178 /* Build final flags, calling callback on the way if any */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003179 ret = x509_crt_merge_flags_with_cb( flags, &ver_chain, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003180
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003181exit:
Hanno Beckerf53893b2019-03-28 13:45:55 +00003182
3183#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3184 mbedtls_x509_crt_free( ver_chain.trust_ca_cb_result );
3185 mbedtls_free( ver_chain.trust_ca_cb_result );
3186 ver_chain.trust_ca_cb_result = NULL;
3187#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3188
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003189#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3190 if( rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
3191 mbedtls_x509_crt_restart_free( rs_ctx );
3192#endif
3193
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003194 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3195 * the SSL module for authmode optional, but non-zero return from the
3196 * callback means a fatal error so it shouldn't be ignored */
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003197 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
3198 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3199
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003200 if( ret != 0 )
3201 {
3202 *flags = (uint32_t) -1;
3203 return( ret );
3204 }
3205
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003206 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003207 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003208
3209 return( 0 );
3210}
3211
Hanno Becker3116fb32019-03-28 13:34:42 +00003212
3213/*
3214 * Verify the certificate validity (default profile, not restartable)
3215 */
3216int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
3217 mbedtls_x509_crt *trust_ca,
3218 mbedtls_x509_crl *ca_crl,
3219 const char *cn, uint32_t *flags,
3220 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3221 void *p_vrfy )
3222{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003223 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003224 NULL, NULL,
3225 &mbedtls_x509_crt_profile_default,
3226 cn, flags,
3227 f_vrfy, p_vrfy, NULL ) );
3228}
3229
3230/*
3231 * Verify the certificate validity (user-chosen profile, not restartable)
3232 */
3233int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
3234 mbedtls_x509_crt *trust_ca,
3235 mbedtls_x509_crl *ca_crl,
3236 const mbedtls_x509_crt_profile *profile,
3237 const char *cn, uint32_t *flags,
3238 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3239 void *p_vrfy )
3240{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003241 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003242 NULL, NULL,
3243 profile, cn, flags,
3244 f_vrfy, p_vrfy, NULL ) );
3245}
3246
3247#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3248/*
3249 * Verify the certificate validity (user-chosen profile, CA callback,
3250 * not restartable).
3251 */
Jarno Lamsa31d9db62019-04-01 14:33:49 +03003252int mbedtls_x509_crt_verify_with_ca_cb( mbedtls_x509_crt *crt,
Hanno Becker3116fb32019-03-28 13:34:42 +00003253 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3254 void *p_ca_cb,
3255 const mbedtls_x509_crt_profile *profile,
3256 const char *cn, uint32_t *flags,
3257 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3258 void *p_vrfy )
3259{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003260 return( x509_crt_verify_restartable_ca_cb( crt, NULL, NULL,
Hanno Becker3116fb32019-03-28 13:34:42 +00003261 f_ca_cb, p_ca_cb,
3262 profile, cn, flags,
3263 f_vrfy, p_vrfy, NULL ) );
3264}
3265#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3266
3267int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
3268 mbedtls_x509_crt *trust_ca,
3269 mbedtls_x509_crl *ca_crl,
3270 const mbedtls_x509_crt_profile *profile,
3271 const char *cn, uint32_t *flags,
3272 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3273 void *p_vrfy,
3274 mbedtls_x509_crt_restart_ctx *rs_ctx )
3275{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003276 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003277 NULL, NULL,
3278 profile, cn, flags,
3279 f_vrfy, p_vrfy, rs_ctx ) );
3280}
3281
3282
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003283/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003284 * Initialize a certificate chain
3285 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003286void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02003287{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003288 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02003289}
3290
3291/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003292 * Unallocate all certificate data
3293 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003294void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003295{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003296 mbedtls_x509_crt *cert_cur = crt;
3297 mbedtls_x509_crt *cert_prv;
3298 mbedtls_x509_name *name_cur;
3299 mbedtls_x509_name *name_prv;
3300 mbedtls_x509_sequence *seq_cur;
3301 mbedtls_x509_sequence *seq_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003302
3303 if( crt == NULL )
3304 return;
3305
3306 do
3307 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003308 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003310#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
3311 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003312#endif
3313
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003314 name_cur = cert_cur->issuer.next;
3315 while( name_cur != NULL )
3316 {
3317 name_prv = name_cur;
3318 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003319 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003320 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003321 }
3322
3323 name_cur = cert_cur->subject.next;
3324 while( name_cur != NULL )
3325 {
3326 name_prv = name_cur;
3327 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003328 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003329 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003330 }
3331
3332 seq_cur = cert_cur->ext_key_usage.next;
3333 while( seq_cur != NULL )
3334 {
3335 seq_prv = seq_cur;
3336 seq_cur = seq_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003337 mbedtls_platform_zeroize( seq_prv,
3338 sizeof( mbedtls_x509_sequence ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003339 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003340 }
3341
3342 seq_cur = cert_cur->subject_alt_names.next;
3343 while( seq_cur != NULL )
3344 {
3345 seq_prv = seq_cur;
3346 seq_cur = seq_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003347 mbedtls_platform_zeroize( seq_prv,
3348 sizeof( mbedtls_x509_sequence ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003349 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003350 }
3351
Ron Eldor74d9acc2019-03-21 14:00:03 +02003352 seq_cur = cert_cur->certificate_policies.next;
3353 while( seq_cur != NULL )
3354 {
3355 seq_prv = seq_cur;
3356 seq_cur = seq_cur->next;
3357 mbedtls_platform_zeroize( seq_prv,
3358 sizeof( mbedtls_x509_sequence ) );
3359 mbedtls_free( seq_prv );
3360 }
3361
Hanno Becker1a65dcd2019-01-31 08:57:44 +00003362 if( cert_cur->raw.p != NULL && cert_cur->own_buffer )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003363 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003364 mbedtls_platform_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003365 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003366 }
3367
3368 cert_cur = cert_cur->next;
3369 }
3370 while( cert_cur != NULL );
3371
3372 cert_cur = crt;
3373 do
3374 {
3375 cert_prv = cert_cur;
3376 cert_cur = cert_cur->next;
3377
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003378 mbedtls_platform_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003379 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003380 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003381 }
3382 while( cert_cur != NULL );
3383}
3384
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003385#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3386/*
3387 * Initialize a restart context
3388 */
3389void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx )
3390{
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003391 mbedtls_pk_restart_init( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003392
3393 ctx->parent = NULL;
3394 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003395 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003396
3397 ctx->parent_is_trusted = -1;
3398
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003399 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003400 ctx->self_cnt = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003401 x509_crt_verify_chain_reset( &ctx->ver_chain );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003402}
3403
3404/*
3405 * Free the components of a restart context
3406 */
3407void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx )
3408{
3409 if( ctx == NULL )
3410 return;
3411
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003412 mbedtls_pk_restart_free( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003413 mbedtls_x509_crt_restart_init( ctx );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003414}
3415#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003417#endif /* MBEDTLS_X509_CRT_PARSE_C */