blob: 3f00e438440e37fa9e6307a2e21463b131649943 [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/*
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +02002 * X.509 certificate parsing and verification
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker7c6b2c32013-09-16 13:49:26 +020018 */
19/*
20 * The ITU-T X.509 standard defines a certificate format for PKI.
21 *
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +020022 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
23 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
24 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020025 *
26 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
27 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +020028 *
29 * [SIRO] https://cabforum.org/wp-content/uploads/Chunghwatelecom201503cabforumV4.pdf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020030 */
31
Gilles Peskinedb09ef62020-06-03 01:43:33 +020032#include "common.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020035
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/x509_crt.h"
Janos Follath73c616b2019-12-18 15:07:04 +000037#include "mbedtls/error.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050039#include "mbedtls/platform_util.h"
Rich Evans00ab4702015-02-06 13:43:58 +000040
Rich Evans00ab4702015-02-06 13:43:58 +000041#include <string.h>
42
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020045#endif
46
Andrzej Kurekd4a65532018-10-31 06:18:39 -040047#if defined(MBEDTLS_USE_PSA_CRYPTO)
48#include "psa/crypto.h"
49#include "mbedtls/psa_util.h"
pespacek7599a772022-02-07 14:40:55 +010050#endif /* MBEDTLS_USE_PSA_CRYPTO */
Andrzej Kurekd4a65532018-10-31 06:18:39 -040051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020054#else
Simon Butcherd2642582018-10-03 15:11:19 +010055#include <stdio.h>
Rich Evans00ab4702015-02-06 13:43:58 +000056#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#define mbedtls_free free
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020058#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#define mbedtls_snprintf snprintf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020060#endif
61
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000063#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010064#endif
65
Daniel Axtensf0710242020-05-28 11:43:41 +100066#if defined(MBEDTLS_HAVE_TIME)
Paul Bakkerfa6a6202013-10-28 18:48:30 +010067#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020068#include <windows.h>
69#else
70#include <time.h>
71#endif
Daniel Axtensf0710242020-05-28 11:43:41 +100072#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074#if defined(MBEDTLS_FS_IO)
Rich Evans00ab4702015-02-06 13:43:58 +000075#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020076#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020077#include <sys/types.h>
78#include <sys/stat.h>
Martino Facchin0ec05ec2021-05-04 11:47:36 +020079#if defined(__MBED__)
80#include <platform/mbed_retarget.h>
81#else
Paul Bakker7c6b2c32013-09-16 13:49:26 +020082#include <dirent.h>
Martino Facchin0ec05ec2021-05-04 11:47:36 +020083#endif /* __MBED__ */
Eduardo Silvae1bfffc2019-04-25 10:43:26 -060084#include <errno.h>
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 }
Eduardo Silvae1bfffc2019-04-25 10:43:26 -06001659 else
Andres AGf9113192016-09-02 14:06:04 +01001660 {
Eduardo Silvae1bfffc2019-04-25 10:43:26 -06001661 /* determinate if the file entry could be a link, using lstat(2)
1662 * is safer than just stat(2), otherwise a broken link will
1663 * give us a false positive. */
1664 if( lstat( entry_name, &sb ) == -1 )
1665 {
1666 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1667 goto cleanup;
1668 }
1669
1670 /* if the file is a symbolic link, we need to validate the real
1671 * information using stat(2). */
1672 if( S_ISLNK( sb.st_mode ) )
1673 {
1674 /* if stat(2) fails it could be a broken link or a generic
1675 * error, if the link is broken, just report it as a
1676 * certificate that could not be processed, otherwise
1677 * just set a MBEDTLS_ERR_X509_FILE_IO_ERROR. */
1678 if( stat( entry_name, &sb ) == -1 )
1679 {
1680 if( errno == ENOENT )
1681 {
Dave Rodgman935154e2022-07-20 14:01:45 +01001682 /* Broken link - ignore this entry */
1683 continue;
Eduardo Silvae1bfffc2019-04-25 10:43:26 -06001684 }
1685 else
1686 {
1687 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1688 goto cleanup;
1689 }
1690 }
1691 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001692 }
1693
1694 if( !S_ISREG( sb.st_mode ) )
1695 continue;
1696
1697 // Ignore parse errors
1698 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001699 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001700 if( t_ret < 0 )
1701 ret++;
1702 else
1703 ret += t_ret;
1704 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001705
1706cleanup:
Andres AGf9113192016-09-02 14:06:04 +01001707 closedir( dir );
1708
Ron Eldor63140682017-01-09 19:27:59 +02001709#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001710 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001711 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Ron Eldor63140682017-01-09 19:27:59 +02001712#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001713
Paul Bakkerbe089b02013-10-14 15:51:50 +02001714#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001715
1716 return( ret );
1717}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001718#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001719
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001720/*
1721 * OtherName ::= SEQUENCE {
1722 * type-id OBJECT IDENTIFIER,
1723 * value [0] EXPLICIT ANY DEFINED BY type-id }
1724 *
1725 * HardwareModuleName ::= SEQUENCE {
1726 * hwType OBJECT IDENTIFIER,
1727 * hwSerialNum OCTET STRING }
1728 *
1729 * NOTE: we currently only parse and use otherName of type HwModuleName,
1730 * as defined in RFC 4108.
1731 */
1732static int x509_get_other_name( const mbedtls_x509_buf *subject_alt_name,
1733 mbedtls_x509_san_other_name *other_name )
1734{
Janos Follathab23cd12019-05-09 13:53:57 +01001735 int ret = 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001736 size_t len;
1737 unsigned char *p = subject_alt_name->p;
1738 const unsigned char *end = p + subject_alt_name->len;
1739 mbedtls_x509_buf cur_oid;
1740
1741 if( ( subject_alt_name->tag &
1742 ( MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK ) ) !=
1743 ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ) )
1744 {
1745 /*
1746 * The given subject alternative name is not of type "othername".
1747 */
1748 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
1749 }
1750
1751 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1752 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001753 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001754
1755 cur_oid.tag = MBEDTLS_ASN1_OID;
1756 cur_oid.p = p;
1757 cur_oid.len = len;
1758
1759 /*
1760 * Only HwModuleName is currently supported.
1761 */
1762 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid ) != 0 )
1763 {
1764 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1765 }
1766
Ron Eldor890819a2019-05-13 19:03:04 +03001767 if( p + len >= end )
1768 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001769 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001770 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1771 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001772 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001773 p += len;
1774 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1775 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001776 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001777
1778 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1779 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001780 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001781
1782 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001783 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001784
1785 other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID;
1786 other_name->value.hardware_module_name.oid.p = p;
1787 other_name->value.hardware_module_name.oid.len = len;
1788
Ron Eldor890819a2019-05-13 19:03:04 +03001789 if( p + len >= end )
1790 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001791 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001792 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1793 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001794 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001795 p += len;
1796 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1797 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001798 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001799
1800 other_name->value.hardware_module_name.val.tag = MBEDTLS_ASN1_OCTET_STRING;
1801 other_name->value.hardware_module_name.val.p = p;
1802 other_name->value.hardware_module_name.val.len = len;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001803 p += len;
1804 if( p != end )
1805 {
Ron Eldor890819a2019-05-13 19:03:04 +03001806 mbedtls_platform_zeroize( other_name,
Sébastien Duquette661d7252019-06-23 17:45:26 -04001807 sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001808 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1809 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001810 }
1811 return( 0 );
1812}
1813
Peter Kolbus9a969b62018-12-11 13:55:56 -06001814int mbedtls_x509_parse_subject_alt_name( const mbedtls_x509_buf *san_buf,
1815 mbedtls_x509_subject_alternative_name *san )
1816{
1817 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1818 switch( san_buf->tag &
1819 ( MBEDTLS_ASN1_TAG_CLASS_MASK |
1820 MBEDTLS_ASN1_TAG_VALUE_MASK ) )
1821 {
1822 /*
1823 * otherName
1824 */
1825 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ):
1826 {
1827 mbedtls_x509_san_other_name other_name;
1828
1829 ret = x509_get_other_name( san_buf, &other_name );
1830 if( ret != 0 )
1831 return( ret );
1832
1833 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1834 san->type = MBEDTLS_X509_SAN_OTHER_NAME;
1835 memcpy( &san->san.other_name,
1836 &other_name, sizeof( other_name ) );
1837
1838 }
1839 break;
1840
1841 /*
1842 * dNSName
1843 */
1844 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME ):
1845 {
1846 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1847 san->type = MBEDTLS_X509_SAN_DNS_NAME;
1848
1849 memcpy( &san->san.unstructured_name,
1850 san_buf, sizeof( *san_buf ) );
1851
1852 }
1853 break;
1854
1855 /*
1856 * Type not supported
1857 */
1858 default:
1859 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1860 }
1861 return( 0 );
1862}
1863
1864#if !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001865static int x509_info_subject_alt_name( char **buf, size_t *size,
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001866 const mbedtls_x509_sequence
1867 *subject_alt_name,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001868 const char *prefix )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001869{
Janos Follath865b3eb2019-12-16 11:46:15 +00001870 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001871 size_t n = *size;
1872 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001873 const mbedtls_x509_sequence *cur = subject_alt_name;
Ron Eldor890819a2019-05-13 19:03:04 +03001874 mbedtls_x509_subject_alternative_name san;
1875 int parse_ret;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001876
1877 while( cur != NULL )
1878 {
Ron Eldor890819a2019-05-13 19:03:04 +03001879 memset( &san, 0, sizeof( san ) );
1880 parse_ret = mbedtls_x509_parse_subject_alt_name( &cur->buf, &san );
1881 if( parse_ret != 0 )
1882 {
1883 if( parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1884 {
1885 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
1886 MBEDTLS_X509_SAFE_SNPRINTF;
1887 }
1888 else
1889 {
1890 ret = mbedtls_snprintf( p, n, "\n%s <malformed>", prefix );
1891 MBEDTLS_X509_SAFE_SNPRINTF;
1892 }
1893 cur = cur->next;
1894 continue;
1895 }
1896
1897 switch( san.type )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001898 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001899 /*
1900 * otherName
1901 */
Ron Eldora2913912019-05-16 16:17:38 +03001902 case MBEDTLS_X509_SAN_OTHER_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001903 {
1904 mbedtls_x509_san_other_name *other_name = &san.san.other_name;
1905
1906 ret = mbedtls_snprintf( p, n, "\n%s otherName :", prefix );
1907 MBEDTLS_X509_SAFE_SNPRINTF;
1908
1909 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME,
1910 &other_name->value.hardware_module_name.oid ) != 0 )
Janos Follath22f605f2019-05-10 10:37:17 +01001911 {
Ron Eldor890819a2019-05-13 19:03:04 +03001912 ret = mbedtls_snprintf( p, n, "\n%s hardware module name :", prefix );
1913 MBEDTLS_X509_SAFE_SNPRINTF;
1914 ret = mbedtls_snprintf( p, n, "\n%s hardware type : ", prefix );
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001915 MBEDTLS_X509_SAFE_SNPRINTF;
1916
Ron Eldor890819a2019-05-13 19:03:04 +03001917 ret = mbedtls_oid_get_numeric_string( p, n, &other_name->value.hardware_module_name.oid );
1918 MBEDTLS_X509_SAFE_SNPRINTF;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001919
Ron Eldor890819a2019-05-13 19:03:04 +03001920 ret = mbedtls_snprintf( p, n, "\n%s hardware serial number : ", prefix );
1921 MBEDTLS_X509_SAFE_SNPRINTF;
1922
1923 if( other_name->value.hardware_module_name.val.len >= n )
1924 {
1925 *p = '\0';
1926 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Janos Follath22f605f2019-05-10 10:37:17 +01001927 }
1928
Ron Eldora2913912019-05-16 16:17:38 +03001929 memcpy( p, other_name->value.hardware_module_name.val.p,
1930 other_name->value.hardware_module_name.val.len );
1931 p += other_name->value.hardware_module_name.val.len;
1932
Ron Eldor890819a2019-05-13 19:03:04 +03001933 n -= other_name->value.hardware_module_name.val.len;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001934
Ron Eldor890819a2019-05-13 19:03:04 +03001935 }/* MBEDTLS_OID_ON_HW_MODULE_NAME */
1936 }
1937 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001938
1939 /*
1940 * dNSName
1941 */
Ron Eldora2913912019-05-16 16:17:38 +03001942 case MBEDTLS_X509_SAN_DNS_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001943 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001944 ret = mbedtls_snprintf( p, n, "\n%s dNSName : ", prefix );
1945 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldor890819a2019-05-13 19:03:04 +03001946 if( san.san.unstructured_name.len >= n )
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001947 {
1948 *p = '\0';
1949 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
1950 }
Ron Eldora2913912019-05-16 16:17:38 +03001951
1952 memcpy( p, san.san.unstructured_name.p, san.san.unstructured_name.len );
1953 p += san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001954 n -= san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001955 }
1956 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001957
1958 /*
Ron Eldor890819a2019-05-13 19:03:04 +03001959 * Type not supported, skip item.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001960 */
1961 default:
Janos Follath22f605f2019-05-10 10:37:17 +01001962 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
1963 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001964 break;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001965 }
1966
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001967 cur = cur->next;
1968 }
1969
1970 *p = '\0';
1971
1972 *size = n;
1973 *buf = p;
1974
1975 return( 0 );
1976}
1977
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001978#define PRINT_ITEM(i) \
1979 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001980 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001981 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001982 sep = ", "; \
1983 }
1984
1985#define CERT_TYPE(type,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001986 if( ns_cert_type & (type) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001987 PRINT_ITEM( name );
1988
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001989static int x509_info_cert_type( char **buf, size_t *size,
1990 unsigned char ns_cert_type )
1991{
Janos Follath865b3eb2019-12-16 11:46:15 +00001992 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001993 size_t n = *size;
1994 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001995 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001997 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001998 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001999 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
2000 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
2001 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002002 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
2003 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
2004 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002005
2006 *size = n;
2007 *buf = p;
2008
2009 return( 0 );
2010}
2011
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002012#define KEY_USAGE(code,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01002013 if( key_usage & (code) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002014 PRINT_ITEM( name );
2015
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002016static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02002017 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002018{
Janos Follath865b3eb2019-12-16 11:46:15 +00002019 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002020 size_t n = *size;
2021 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002022 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002024 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
2025 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002026 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
2027 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
2028 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002029 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
2030 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02002031 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
2032 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002033
2034 *size = n;
2035 *buf = p;
2036
2037 return( 0 );
2038}
2039
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002040static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002041 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002042{
Janos Follath865b3eb2019-12-16 11:46:15 +00002043 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002044 const char *desc;
2045 size_t n = *size;
2046 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002047 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002048 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002049
2050 while( cur != NULL )
2051 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002052 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002053 desc = "???";
2054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002055 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002056 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002057
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002058 sep = ", ";
2059
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002060 cur = cur->next;
2061 }
2062
2063 *size = n;
2064 *buf = p;
2065
2066 return( 0 );
2067}
2068
Ron Eldor74d9acc2019-03-21 14:00:03 +02002069static int x509_info_cert_policies( char **buf, size_t *size,
2070 const mbedtls_x509_sequence *certificate_policies )
2071{
Janos Follath865b3eb2019-12-16 11:46:15 +00002072 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor74d9acc2019-03-21 14:00:03 +02002073 const char *desc;
2074 size_t n = *size;
2075 char *p = *buf;
2076 const mbedtls_x509_sequence *cur = certificate_policies;
2077 const char *sep = "";
2078
2079 while( cur != NULL )
2080 {
2081 if( mbedtls_oid_get_certificate_policies( &cur->buf, &desc ) != 0 )
2082 desc = "???";
2083
2084 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
2085 MBEDTLS_X509_SAFE_SNPRINTF;
2086
2087 sep = ", ";
2088
2089 cur = cur->next;
2090 }
2091
2092 *size = n;
2093 *buf = p;
2094
2095 return( 0 );
2096}
2097
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002098/*
2099 * Return an informational string about the certificate.
2100 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002101#define BEFORE_COLON 18
2102#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002103int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
2104 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002105{
Janos Follath865b3eb2019-12-16 11:46:15 +00002106 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002107 size_t n;
2108 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002109 char key_size_str[BEFORE_COLON];
2110
2111 p = buf;
2112 n = size;
2113
Janos Follath98e28a72016-05-31 14:03:54 +01002114 if( NULL == crt )
2115 {
2116 ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" );
2117 MBEDTLS_X509_SAFE_SNPRINTF;
2118
2119 return( (int) ( size - n ) );
2120 }
2121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002122 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002123 prefix, crt->version );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002124 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002125 ret = mbedtls_snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002126 prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002127 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002129 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002130 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002132 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002133 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002134 ret = mbedtls_x509_dn_gets( p, n, &crt->issuer );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002135 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002137 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002138 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002139 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002140 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002142 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002143 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2144 crt->valid_from.year, crt->valid_from.mon,
2145 crt->valid_from.day, crt->valid_from.hour,
2146 crt->valid_from.min, crt->valid_from.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002147 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002149 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002150 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2151 crt->valid_to.year, crt->valid_to.mon,
2152 crt->valid_to.day, crt->valid_to.hour,
2153 crt->valid_to.min, crt->valid_to.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002154 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002156 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002157 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002159 ret = mbedtls_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02002160 crt->sig_md, crt->sig_opts );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002161 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002162
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002163 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002164 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
2165 mbedtls_pk_get_name( &crt->pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002166 {
2167 return( ret );
2168 }
2169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002170 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +02002171 (int) mbedtls_pk_get_bitlen( &crt->pk ) );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002172 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002173
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002174 /*
2175 * Optional extensions
2176 */
2177
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002178 if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002179 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002180 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002181 crt->ca_istrue ? "true" : "false" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002182 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002183
2184 if( crt->max_pathlen > 0 )
2185 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002186 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002187 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002188 }
2189 }
2190
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002191 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002192 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002193 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name :", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002194 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002195
2196 if( ( ret = x509_info_subject_alt_name( &p, &n,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002197 &crt->subject_alt_names,
Ron Eldor6aeae9e2019-05-20 12:00:36 +03002198 prefix ) ) != 0 )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002199 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002200 }
2201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002202 if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002204 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002205 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002206
2207 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
2208 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002209 }
2210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002211 if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002213 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002214 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002215
2216 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
2217 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002218 }
2219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002220 if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002222 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002223 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002224
2225 if( ( ret = x509_info_ext_key_usage( &p, &n,
2226 &crt->ext_key_usage ) ) != 0 )
2227 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002228 }
2229
Ron Eldor74d9acc2019-03-21 14:00:03 +02002230 if( crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES )
2231 {
2232 ret = mbedtls_snprintf( p, n, "\n%scertificate policies : ", prefix );
2233 MBEDTLS_X509_SAFE_SNPRINTF;
2234
2235 if( ( ret = x509_info_cert_policies( &p, &n,
2236 &crt->certificate_policies ) ) != 0 )
2237 return( ret );
2238 }
2239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002240 ret = mbedtls_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002241 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002242
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002243 return( (int) ( size - n ) );
2244}
2245
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002246struct x509_crt_verify_string {
2247 int code;
2248 const char *string;
2249};
2250
Hanno Becker7ac83f92020-10-09 10:48:22 +01002251#define X509_CRT_ERROR_INFO( err, err_str, info ) { err, info },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002252static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Hanno Becker7ac83f92020-10-09 10:48:22 +01002253 MBEDTLS_X509_CRT_ERROR_INFO_LIST
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002254 { 0, NULL }
2255};
Hanno Becker7ac83f92020-10-09 10:48:22 +01002256#undef X509_CRT_ERROR_INFO
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002257
2258int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002259 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002260{
Janos Follath865b3eb2019-12-16 11:46:15 +00002261 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002262 const struct x509_crt_verify_string *cur;
2263 char *p = buf;
2264 size_t n = size;
2265
2266 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
2267 {
2268 if( ( flags & cur->code ) == 0 )
2269 continue;
2270
2271 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002272 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002273 flags ^= cur->code;
2274 }
2275
2276 if( flags != 0 )
2277 {
2278 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
2279 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002280 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002281 }
2282
2283 return( (int) ( size - n ) );
2284}
Hanno Becker612a2f12020-10-09 09:19:39 +01002285#endif /* MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002286
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002287int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
2288 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002289{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002290 unsigned int usage_must, usage_may;
2291 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
2292 | MBEDTLS_X509_KU_DECIPHER_ONLY;
2293
2294 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
2295 return( 0 );
2296
2297 usage_must = usage & ~may_mask;
2298
2299 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
2300 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
2301
2302 usage_may = usage & may_mask;
2303
2304 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002305 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002306
2307 return( 0 );
2308}
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002310int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002311 const char *usage_oid,
2312 size_t usage_len )
2313{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002314 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002315
2316 /* Extension is not mandatory, absent means no restriction */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002317 if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002318 return( 0 );
2319
2320 /*
2321 * Look for the requested usage (or wildcard ANY) in our list
2322 */
2323 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
2324 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002325 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002326
2327 if( cur_oid->len == usage_len &&
2328 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
2329 {
2330 return( 0 );
2331 }
2332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002333 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002334 return( 0 );
2335 }
2336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002337 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002338}
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002340#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002341/*
2342 * Return 1 if the certificate is revoked, or 0 otherwise.
2343 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002344int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002345{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002346 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002347
2348 while( cur != NULL && cur->serial.len != 0 )
2349 {
2350 if( crt->serial.len == cur->serial.len &&
2351 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
2352 {
Raoul Strackxa4e86142020-06-15 17:03:13 +02002353 return( 1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002354 }
2355
2356 cur = cur->next;
2357 }
2358
2359 return( 0 );
2360}
2361
2362/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002363 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002364 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002365 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002366static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002367 mbedtls_x509_crl *crl_list,
2368 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002369{
2370 int flags = 0;
pespacek7599a772022-02-07 14:40:55 +01002371#if defined(MBEDTLS_USE_PSA_CRYPTO)
2372 unsigned char hash[PSA_HASH_MAX_SIZE];
2373 psa_algorithm_t psa_algorithm;
2374#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002375 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2376 const mbedtls_md_info_t *md_info;
pespacek7599a772022-02-07 14:40:55 +01002377#endif /* MBEDTLS_USE_PSA_CRYPTO */
2378 size_t hash_length;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002379
2380 if( ca == NULL )
2381 return( flags );
2382
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002383 while( crl_list != NULL )
2384 {
2385 if( crl_list->version == 0 ||
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002386 x509_name_cmp( &crl_list->issuer, &ca->subject ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002387 {
2388 crl_list = crl_list->next;
2389 continue;
2390 }
2391
2392 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002393 * Check if the CA is configured to sign CRLs
2394 */
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002395 if( mbedtls_x509_crt_check_key_usage( ca,
2396 MBEDTLS_X509_KU_CRL_SIGN ) != 0 )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002397 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002398 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002399 break;
2400 }
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002401
2402 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002403 * Check if CRL is correctly signed by the trusted CA
2404 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002405 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
2406 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
2407
2408 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
2409 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
2410
pespacek7599a772022-02-07 14:40:55 +01002411#if defined(MBEDTLS_USE_PSA_CRYPTO)
2412 psa_algorithm = mbedtls_psa_translate_md( crl_list->sig_md );
pespacekd924e552022-02-28 11:49:54 +01002413 if( psa_hash_compute( psa_algorithm,
2414 crl_list->tbs.p,
2415 crl_list->tbs.len,
2416 hash,
2417 sizeof( hash ),
2418 &hash_length ) != PSA_SUCCESS )
pespaceka7a64692022-02-14 15:18:43 +01002419 {
2420 /* Note: this can't happen except after an internal error */
2421 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
2422 break;
2423 }
pespacek7599a772022-02-07 14:40:55 +01002424#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002425 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
pespacek7599a772022-02-07 14:40:55 +01002426 hash_length = mbedtls_md_get_size( md_info );
2427 if( mbedtls_md( md_info,
2428 crl_list->tbs.p,
2429 crl_list->tbs.len,
2430 hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002431 {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002432 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002433 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002434 break;
2435 }
pespaceka7a64692022-02-14 15:18:43 +01002436#endif /* MBEDTLS_USE_PSA_CRYPTO */
2437
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002438 if( x509_profile_check_key( profile, &ca->pk ) != 0 )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002439 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002441 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
pespacek7599a772022-02-07 14:40:55 +01002442 crl_list->sig_md, hash, hash_length,
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02002443 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002444 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002445 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002446 break;
2447 }
2448
2449 /*
2450 * Check for validity of CRL (Do not drop out)
2451 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002452 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002453 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002454
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002455 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002456 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002457
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002458 /*
2459 * Check if certificate is revoked
2460 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002461 if( mbedtls_x509_crt_is_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002462 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002463 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002464 break;
2465 }
2466
2467 crl_list = crl_list->next;
2468 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002469
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002470 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002471}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002472#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002473
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002474/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002475 * Check the signature of a certificate by its parent
2476 */
2477static int x509_crt_check_signature( const mbedtls_x509_crt *child,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002478 mbedtls_x509_crt *parent,
2479 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002480{
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002481 size_t hash_len;
2482#if !defined(MBEDTLS_USE_PSA_CRYPTO)
pespacek7599a772022-02-07 14:40:55 +01002483 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002484 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002485 md_info = mbedtls_md_info_from_type( child->sig_md );
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002486 hash_len = mbedtls_md_get_size( md_info );
Andrzej Kurek8b38ff52018-11-20 03:20:09 -05002487
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002488 /* Note: hash errors can happen only after an internal error */
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002489 if( mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash ) != 0 )
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002490 return( -1 );
2491#else
pespacek7599a772022-02-07 14:40:55 +01002492 unsigned char hash[PSA_HASH_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002493 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( child->sig_md );
pespacek7599a772022-02-07 14:40:55 +01002494 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002495
pespacek7599a772022-02-07 14:40:55 +01002496 status = psa_hash_compute( hash_alg,
2497 child->tbs.p,
2498 child->tbs.len,
2499 hash,
pespacekb9f07a72022-02-14 15:13:26 +01002500 sizeof( hash ),
pespacek7599a772022-02-07 14:40:55 +01002501 &hash_len );
2502 if( status != PSA_SUCCESS )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002503 {
pespacek3110c7b2022-02-14 15:07:41 +01002504 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002505 }
2506
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002507#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002508 /* Skip expensive computation on obvious mismatch */
2509 if( ! mbedtls_pk_can_do( &parent->pk, child->sig_pk ) )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002510 return( -1 );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002511
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002512#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002513 if( rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA )
2514 {
2515 return( mbedtls_pk_verify_restartable( &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002516 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002517 child->sig.p, child->sig.len, &rs_ctx->pk ) );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002518 }
2519#else
2520 (void) rs_ctx;
2521#endif
2522
2523 return( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002524 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002525 child->sig.p, child->sig.len ) );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002526}
2527
2528/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002529 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2530 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002531 *
2532 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002533 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002534static int x509_crt_check_parent( const mbedtls_x509_crt *child,
2535 const mbedtls_x509_crt *parent,
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002536 int top )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002537{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002538 int need_ca_bit;
2539
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002540 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02002541 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002542 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002543
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002544 /* Parent must have the basicConstraints CA bit set as a general rule */
2545 need_ca_bit = 1;
2546
2547 /* Exception: v1/v2 certificates that are locally trusted. */
2548 if( top && parent->version < 3 )
2549 need_ca_bit = 0;
2550
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002551 if( need_ca_bit && ! parent->ca_istrue )
2552 return( -1 );
2553
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002554 if( need_ca_bit &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002555 mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002556 {
2557 return( -1 );
2558 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002559
2560 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002561}
2562
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002563/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002564 * Find a suitable parent for child in candidates, or return NULL.
2565 *
2566 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002567 * 1. subject name matches child's issuer
2568 * 2. if necessary, the CA bit is set and key usage allows signing certs
2569 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002570 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002571 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002572 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002573 * If there's a suitable candidate which is also time-valid, return the first
2574 * such. Otherwise, return the first suitable candidate (or NULL if there is
2575 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002576 *
2577 * The rationale for this rule is that someone could have a list of trusted
2578 * roots with two versions on the same root with different validity periods.
2579 * (At least one user reported having such a list and wanted it to just work.)
2580 * The reason we don't just require time-validity is that generally there is
2581 * only one version, and if it's expired we want the flags to state that
2582 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002583 *
2584 * The rationale for rule 3 (signature for trusted roots) is that users might
2585 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002586 * way we select the correct one is by checking the signature (as we don't
2587 * rely on key identifier extensions). (This is one way users might choose to
2588 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002589 *
2590 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002591 * - [in] child: certificate for which we're looking for a parent
2592 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002593 * - [out] r_parent: parent found (or NULL)
2594 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002595 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2596 * of the chain, 0 otherwise
2597 * - [in] path_cnt: number of intermediates seen so far
2598 * - [in] self_cnt: number of self-signed intermediates seen so far
2599 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002600 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002601 *
2602 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002603 * - 0 on success
2604 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002605 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002606static int x509_crt_find_parent_in(
2607 mbedtls_x509_crt *child,
2608 mbedtls_x509_crt *candidates,
2609 mbedtls_x509_crt **r_parent,
2610 int *r_signature_is_good,
2611 int top,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002612 unsigned path_cnt,
2613 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002614 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002615{
Janos Follath865b3eb2019-12-16 11:46:15 +00002616 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002617 mbedtls_x509_crt *parent, *fallback_parent;
Benjamin Kier36050732019-05-30 14:49:17 -04002618 int signature_is_good = 0, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002619
2620#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002621 /* did we have something in progress? */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002622 if( rs_ctx != NULL && rs_ctx->parent != NULL )
2623 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002624 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002625 parent = rs_ctx->parent;
2626 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002627 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002628
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002629 /* clear saved state */
2630 rs_ctx->parent = NULL;
2631 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002632 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002633
2634 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002635 goto check_signature;
2636 }
2637#endif
2638
2639 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002640 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002641
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002642 for( parent = candidates; parent != NULL; parent = parent->next )
2643 {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002644 /* basic parenting skills (name, CA bit, key usage) */
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002645 if( x509_crt_check_parent( child, parent, top ) != 0 )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002646 continue;
2647
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002648 /* +1 because stored max_pathlen is 1 higher that the actual value */
2649 if( parent->max_pathlen > 0 &&
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002650 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt )
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002651 {
2652 continue;
2653 }
2654
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002655 /* Signature */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002656#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2657check_signature:
2658#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002659 ret = x509_crt_check_signature( child, parent, rs_ctx );
2660
2661#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002662 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2663 {
2664 /* save state */
2665 rs_ctx->parent = parent;
2666 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002667 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002668
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002669 return( ret );
2670 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002671#else
2672 (void) ret;
2673#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002674
2675 signature_is_good = ret == 0;
2676 if( top && ! signature_is_good )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002677 continue;
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002678
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002679 /* optional time check */
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002680 if( mbedtls_x509_time_is_past( &parent->valid_to ) ||
2681 mbedtls_x509_time_is_future( &parent->valid_from ) )
2682 {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002683 if( fallback_parent == NULL )
2684 {
2685 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002686 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002687 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002688
2689 continue;
2690 }
2691
Andy Gross1f627142019-01-30 10:25:53 -06002692 *r_parent = parent;
2693 *r_signature_is_good = signature_is_good;
2694
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002695 break;
2696 }
2697
Andy Gross1f627142019-01-30 10:25:53 -06002698 if( parent == NULL )
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002699 {
2700 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002701 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002702 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002703
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002704 return( 0 );
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002705}
2706
2707/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002708 * Find a parent in trusted CAs or the provided chain, or return NULL.
2709 *
2710 * Searches in trusted CAs first, and return the first suitable parent found
2711 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002712 *
2713 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002714 * - [in] child: certificate for which we're looking for a parent, followed
2715 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002716 * - [in] trust_ca: list of locally trusted certificates
2717 * - [out] parent: parent found (or NULL)
2718 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2719 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2720 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2721 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002722 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002723 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002724 *
2725 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002726 * - 0 on success
2727 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002728 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002729static int x509_crt_find_parent(
2730 mbedtls_x509_crt *child,
2731 mbedtls_x509_crt *trust_ca,
2732 mbedtls_x509_crt **parent,
2733 int *parent_is_trusted,
2734 int *signature_is_good,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002735 unsigned path_cnt,
2736 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002737 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002738{
Janos Follath865b3eb2019-12-16 11:46:15 +00002739 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002740 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002741
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002742 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002743
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002744#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002745 /* restore then clear saved state if we have some stored */
2746 if( rs_ctx != NULL && rs_ctx->parent_is_trusted != -1 )
2747 {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002748 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002749 rs_ctx->parent_is_trusted = -1;
2750 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002751#endif
2752
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002753 while( 1 ) {
2754 search_list = *parent_is_trusted ? trust_ca : child->next;
2755
2756 ret = x509_crt_find_parent_in( child, search_list,
2757 parent, signature_is_good,
2758 *parent_is_trusted,
2759 path_cnt, self_cnt, rs_ctx );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002760
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002761#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002762 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2763 {
2764 /* save state */
2765 rs_ctx->parent_is_trusted = *parent_is_trusted;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002766 return( ret );
2767 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002768#else
2769 (void) ret;
2770#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002771
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002772 /* stop here if found or already in second iteration */
2773 if( *parent != NULL || *parent_is_trusted == 0 )
2774 break;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002775
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002776 /* prepare second iteration */
2777 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002778 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002779
2780 /* extra precaution against mistakes in the caller */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002781 if( *parent == NULL )
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002782 {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002783 *parent_is_trusted = 0;
2784 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002785 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002786
2787 return( 0 );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002788}
2789
2790/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002791 * Check if an end-entity certificate is locally trusted
2792 *
2793 * Currently we require such certificates to be self-signed (actually only
2794 * check for self-issued as self-signatures are not checked)
2795 */
2796static int x509_crt_check_ee_locally_trusted(
2797 mbedtls_x509_crt *crt,
2798 mbedtls_x509_crt *trust_ca )
2799{
2800 mbedtls_x509_crt *cur;
2801
2802 /* must be self-issued */
2803 if( x509_name_cmp( &crt->issuer, &crt->subject ) != 0 )
2804 return( -1 );
2805
2806 /* look for an exact match with trusted cert */
2807 for( cur = trust_ca; cur != NULL; cur = cur->next )
2808 {
2809 if( crt->raw.len == cur->raw.len &&
2810 memcmp( crt->raw.p, cur->raw.p, crt->raw.len ) == 0 )
2811 {
2812 return( 0 );
2813 }
2814 }
2815
2816 /* too bad */
2817 return( -1 );
2818}
2819
2820/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002821 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002822 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002823 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2824 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002825 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002826 * such that every cert in the chain is a child of the next one,
2827 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002828 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002829 * Verify that chain and return it with flags for all issues found.
2830 *
2831 * Special cases:
2832 * - EE == Rj -> return a one-element list containing it
2833 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2834 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002835 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002836 * Tests for (aspects of) this function should include at least:
2837 * - trusted EE
2838 * - EE -> trusted root
Antonin Décimo36e89b52019-01-23 15:24:37 +01002839 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002840 * - if relevant: EE untrusted
2841 * - if relevant: EE -> intermediate, untrusted
2842 * with the aspect under test checked at each relevant level (EE, int, root).
2843 * For some aspects longer chains are required, but usually length 2 is
2844 * enough (but length 1 is not in general).
2845 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002846 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002847 * - [in] crt: the cert list EE, C1, ..., Cn
2848 * - [in] trust_ca: the trusted list R1, ..., Rp
2849 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002850 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002851 * Only valid when return value is 0, may contain garbage otherwise!
2852 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002853 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002854 *
2855 * Return value:
2856 * - non-zero if the chain could not be fully built and examined
2857 * - 0 is the chain was successfully built and examined,
2858 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002859 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002860static int x509_crt_verify_chain(
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002861 mbedtls_x509_crt *crt,
2862 mbedtls_x509_crt *trust_ca,
2863 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00002864 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2865 void *p_ca_cb,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002866 const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002867 mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002868 mbedtls_x509_crt_restart_ctx *rs_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002869{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002870 /* Don't initialize any of those variables here, so that the compiler can
2871 * catch potential issues with jumping ahead when restarting */
Janos Follath865b3eb2019-12-16 11:46:15 +00002872 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002873 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002874 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002875 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002876 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002877 int parent_is_trusted;
2878 int child_is_trusted;
2879 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002880 unsigned self_cnt;
Hanno Beckerf53893b2019-03-28 13:45:55 +00002881 mbedtls_x509_crt *cur_trust_ca = NULL;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002882
2883#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2884 /* resume if we had an operation in progress */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002885 if( rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent )
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002886 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002887 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002888 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002889 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002890
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002891 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002892 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002893 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002894 flags = &cur->flags;
2895
2896 goto find_parent;
2897 }
2898#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002899
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002900 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002901 self_cnt = 0;
2902 parent_is_trusted = 0;
2903 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002904
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002905 while( 1 ) {
2906 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002907 cur = &ver_chain->items[ver_chain->len];
2908 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002909 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002910 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002911 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002912
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002913 /* Check time-validity (all certificates) */
2914 if( mbedtls_x509_time_is_past( &child->valid_to ) )
2915 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002916
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002917 if( mbedtls_x509_time_is_future( &child->valid_from ) )
2918 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002919
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002920 /* Stop here for trusted roots (but not for trusted EE certs) */
2921 if( child_is_trusted )
2922 return( 0 );
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002923
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002924 /* Check signature algorithm: MD & PK algs */
2925 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
2926 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002927
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002928 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
2929 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002930
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002931 /* Special case: EE certs that are locally trusted */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002932 if( ver_chain->len == 1 &&
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002933 x509_crt_check_ee_locally_trusted( child, trust_ca ) == 0 )
2934 {
2935 return( 0 );
2936 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002937
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002938#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2939find_parent:
2940#endif
Hanno Beckerf53893b2019-03-28 13:45:55 +00002941
2942 /* Obtain list of potential trusted signers from CA callback,
2943 * or use statically provided list. */
2944#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
2945 if( f_ca_cb != NULL )
2946 {
2947 mbedtls_x509_crt_free( ver_chain->trust_ca_cb_result );
2948 mbedtls_free( ver_chain->trust_ca_cb_result );
2949 ver_chain->trust_ca_cb_result = NULL;
2950
2951 ret = f_ca_cb( p_ca_cb, child, &ver_chain->trust_ca_cb_result );
2952 if( ret != 0 )
2953 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2954
2955 cur_trust_ca = ver_chain->trust_ca_cb_result;
2956 }
2957 else
2958#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2959 {
2960 ((void) f_ca_cb);
2961 ((void) p_ca_cb);
2962 cur_trust_ca = trust_ca;
2963 }
2964
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002965 /* Look for a parent in trusted CAs or up the chain */
Hanno Beckerf53893b2019-03-28 13:45:55 +00002966 ret = x509_crt_find_parent( child, cur_trust_ca, &parent,
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002967 &parent_is_trusted, &signature_is_good,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002968 ver_chain->len - 1, self_cnt, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002969
2970#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002971 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2972 {
2973 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002974 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002975 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002976 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002977
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002978 return( ret );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002979 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002980#else
2981 (void) ret;
2982#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002983
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002984 /* No parent? We're done here */
2985 if( parent == NULL )
2986 {
2987 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2988 return( 0 );
2989 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002990
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002991 /* Count intermediate self-issued (not necessarily self-signed) certs.
2992 * These can occur with some strategies for key rollover, see [SIRO],
2993 * and should be excluded from max_pathlen checks. */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002994 if( ver_chain->len != 1 &&
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002995 x509_name_cmp( &child->issuer, &child->subject ) == 0 )
2996 {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002997 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002998 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002999
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003000 /* path_cnt is 0 for the first intermediate CA,
3001 * and if parent is trusted it's not an intermediate CA */
3002 if( ! parent_is_trusted &&
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003003 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003004 {
3005 /* return immediately to avoid overflow the chain array */
3006 return( MBEDTLS_ERR_X509_FATAL_ERROR );
3007 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003008
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02003009 /* signature was checked while searching parent */
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003010 if( ! signature_is_good )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003011 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
3012
3013 /* check size of signing key */
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02003014 if( x509_profile_check_key( profile, &parent->pk ) != 0 )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003015 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003017#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003018 /* Check trusted CA's CRL for the given crt */
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02003019 *flags |= x509_crt_verifycrl( child, parent, ca_crl, profile );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003020#else
3021 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003022#endif
3023
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003024 /* prepare for next iteration */
3025 child = parent;
3026 parent = NULL;
3027 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003028 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003029 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003030}
3031
3032/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003033 * Check for CN match
3034 */
3035static int x509_crt_check_cn( const mbedtls_x509_buf *name,
3036 const char *cn, size_t cn_len )
3037{
3038 /* try exact match */
3039 if( name->len == cn_len &&
3040 x509_memcasecmp( cn, name->p, cn_len ) == 0 )
3041 {
3042 return( 0 );
3043 }
3044
3045 /* try wildcard match */
Manuel Pégourié-Gonnard900fba62017-10-18 14:28:11 +02003046 if( x509_check_wildcard( cn, name ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003047 {
3048 return( 0 );
3049 }
3050
3051 return( -1 );
3052}
3053
3054/*
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003055 * Check for SAN match, see RFC 5280 Section 4.2.1.6
3056 */
3057static int x509_crt_check_san( const mbedtls_x509_buf *name,
3058 const char *cn, size_t cn_len )
3059{
3060 const unsigned char san_type = (unsigned char) name->tag &
3061 MBEDTLS_ASN1_TAG_VALUE_MASK;
3062
3063 /* dNSName */
3064 if( san_type == MBEDTLS_X509_SAN_DNS_NAME )
3065 return( x509_crt_check_cn( name, cn, cn_len ) );
3066
3067 /* (We may handle other types here later.) */
3068
3069 /* Unrecognized type */
3070 return( -1 );
3071}
3072
3073/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003074 * Verify the requested CN - only call this if cn is not NULL!
3075 */
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003076static void x509_crt_verify_name( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003077 const char *cn,
3078 uint32_t *flags )
3079{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003080 const mbedtls_x509_name *name;
3081 const mbedtls_x509_sequence *cur;
3082 size_t cn_len = strlen( cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003083
3084 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
3085 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003086 for( cur = &crt->subject_alt_names; cur != NULL; cur = cur->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003087 {
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003088 if( x509_crt_check_san( &cur->buf, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003089 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003090 }
3091
3092 if( cur == NULL )
3093 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3094 }
3095 else
3096 {
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02003097 for( name = &crt->subject; name != NULL; name = name->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003098 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003099 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, &name->oid ) == 0 &&
3100 x509_crt_check_cn( &name->val, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003101 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003102 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003103 }
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003104 }
3105
3106 if( name == NULL )
3107 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3108 }
3109}
3110
3111/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003112 * Merge the flags for all certs in the chain, after calling callback
3113 */
3114static int x509_crt_merge_flags_with_cb(
3115 uint32_t *flags,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003116 const mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003117 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3118 void *p_vrfy )
3119{
Janos Follath865b3eb2019-12-16 11:46:15 +00003120 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003121 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003122 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003123 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003124
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003125 for( i = ver_chain->len; i != 0; --i )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003126 {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003127 cur = &ver_chain->items[i-1];
3128 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003129
3130 if( NULL != f_vrfy )
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003131 if( ( ret = f_vrfy( p_vrfy, cur->crt, (int) i-1, &cur_flags ) ) != 0 )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003132 return( ret );
3133
3134 *flags |= cur_flags;
3135 }
3136
3137 return( 0 );
3138}
3139
3140/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003141 * Verify the certificate validity, with profile, restartable version
3142 *
3143 * This function:
3144 * - checks the requested CN (if any)
3145 * - checks the type and size of the EE cert's key,
3146 * as that isn't done as part of chain building/verification currently
3147 * - builds and verifies the chain
3148 * - then calls the callback and merges the flags
Hanno Becker3116fb32019-03-28 13:34:42 +00003149 *
3150 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
3151 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
3152 * verification routine to search for trusted signers, and CRLs will
3153 * be disabled. Otherwise, `trust_ca` will be used as the static list
3154 * of trusted signers, and `ca_crl` will be use as the static list
3155 * of CRLs.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003156 */
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003157static int x509_crt_verify_restartable_ca_cb( mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003158 mbedtls_x509_crt *trust_ca,
3159 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003160 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3161 void *p_ca_cb,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003162 const mbedtls_x509_crt_profile *profile,
3163 const char *cn, uint32_t *flags,
3164 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3165 void *p_vrfy,
3166 mbedtls_x509_crt_restart_ctx *rs_ctx )
3167{
Janos Follath865b3eb2019-12-16 11:46:15 +00003168 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003169 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003170 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003171 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003172
3173 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003174 ee_flags = 0;
3175 x509_crt_verify_chain_reset( &ver_chain );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003176
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003177 if( profile == NULL )
3178 {
3179 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3180 goto exit;
3181 }
3182
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003183 /* check name if requested */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003184 if( cn != NULL )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003185 x509_crt_verify_name( crt, cn, &ee_flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003186
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003187 /* Check the type and size of the key */
3188 pk_type = mbedtls_pk_get_type( &crt->pk );
3189
3190 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003191 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003192
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02003193 if( x509_profile_check_key( profile, &crt->pk ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003194 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003195
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003196 /* Check the chain */
Hanno Becker3116fb32019-03-28 13:34:42 +00003197 ret = x509_crt_verify_chain( crt, trust_ca, ca_crl,
3198 f_ca_cb, p_ca_cb, profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003199 &ver_chain, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003200
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003201 if( ret != 0 )
3202 goto exit;
3203
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003204 /* Merge end-entity flags */
3205 ver_chain.items[0].flags |= ee_flags;
3206
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003207 /* Build final flags, calling callback on the way if any */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003208 ret = x509_crt_merge_flags_with_cb( flags, &ver_chain, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003209
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003210exit:
Hanno Beckerf53893b2019-03-28 13:45:55 +00003211
3212#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3213 mbedtls_x509_crt_free( ver_chain.trust_ca_cb_result );
3214 mbedtls_free( ver_chain.trust_ca_cb_result );
3215 ver_chain.trust_ca_cb_result = NULL;
3216#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3217
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003218#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3219 if( rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
3220 mbedtls_x509_crt_restart_free( rs_ctx );
3221#endif
3222
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003223 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3224 * the SSL module for authmode optional, but non-zero return from the
3225 * callback means a fatal error so it shouldn't be ignored */
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003226 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
3227 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3228
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003229 if( ret != 0 )
3230 {
3231 *flags = (uint32_t) -1;
3232 return( ret );
3233 }
3234
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003235 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003236 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003237
3238 return( 0 );
3239}
3240
Hanno Becker3116fb32019-03-28 13:34:42 +00003241
3242/*
3243 * Verify the certificate validity (default profile, not restartable)
3244 */
3245int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
3246 mbedtls_x509_crt *trust_ca,
3247 mbedtls_x509_crl *ca_crl,
3248 const char *cn, uint32_t *flags,
3249 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3250 void *p_vrfy )
3251{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003252 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003253 NULL, NULL,
3254 &mbedtls_x509_crt_profile_default,
3255 cn, flags,
3256 f_vrfy, p_vrfy, NULL ) );
3257}
3258
3259/*
3260 * Verify the certificate validity (user-chosen profile, not restartable)
3261 */
3262int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
3263 mbedtls_x509_crt *trust_ca,
3264 mbedtls_x509_crl *ca_crl,
3265 const mbedtls_x509_crt_profile *profile,
3266 const char *cn, uint32_t *flags,
3267 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3268 void *p_vrfy )
3269{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003270 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003271 NULL, NULL,
3272 profile, cn, flags,
3273 f_vrfy, p_vrfy, NULL ) );
3274}
3275
3276#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3277/*
3278 * Verify the certificate validity (user-chosen profile, CA callback,
3279 * not restartable).
3280 */
Jarno Lamsa31d9db62019-04-01 14:33:49 +03003281int mbedtls_x509_crt_verify_with_ca_cb( mbedtls_x509_crt *crt,
Hanno Becker3116fb32019-03-28 13:34:42 +00003282 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3283 void *p_ca_cb,
3284 const mbedtls_x509_crt_profile *profile,
3285 const char *cn, uint32_t *flags,
3286 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3287 void *p_vrfy )
3288{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003289 return( x509_crt_verify_restartable_ca_cb( crt, NULL, NULL,
Hanno Becker3116fb32019-03-28 13:34:42 +00003290 f_ca_cb, p_ca_cb,
3291 profile, cn, flags,
3292 f_vrfy, p_vrfy, NULL ) );
3293}
3294#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3295
3296int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
3297 mbedtls_x509_crt *trust_ca,
3298 mbedtls_x509_crl *ca_crl,
3299 const mbedtls_x509_crt_profile *profile,
3300 const char *cn, uint32_t *flags,
3301 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3302 void *p_vrfy,
3303 mbedtls_x509_crt_restart_ctx *rs_ctx )
3304{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003305 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003306 NULL, NULL,
3307 profile, cn, flags,
3308 f_vrfy, p_vrfy, rs_ctx ) );
3309}
3310
3311
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003312/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003313 * Initialize a certificate chain
3314 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003315void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02003316{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003317 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02003318}
3319
3320/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003321 * Unallocate all certificate data
3322 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003323void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003324{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003325 mbedtls_x509_crt *cert_cur = crt;
3326 mbedtls_x509_crt *cert_prv;
3327 mbedtls_x509_name *name_cur;
3328 mbedtls_x509_name *name_prv;
3329 mbedtls_x509_sequence *seq_cur;
3330 mbedtls_x509_sequence *seq_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003331
3332 if( crt == NULL )
3333 return;
3334
3335 do
3336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003337 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003339#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
3340 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003341#endif
3342
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003343 name_cur = cert_cur->issuer.next;
3344 while( name_cur != NULL )
3345 {
3346 name_prv = name_cur;
3347 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003348 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003349 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003350 }
3351
3352 name_cur = cert_cur->subject.next;
3353 while( name_cur != NULL )
3354 {
3355 name_prv = name_cur;
3356 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003357 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003358 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003359 }
3360
3361 seq_cur = cert_cur->ext_key_usage.next;
3362 while( seq_cur != NULL )
3363 {
3364 seq_prv = seq_cur;
3365 seq_cur = seq_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003366 mbedtls_platform_zeroize( seq_prv,
3367 sizeof( mbedtls_x509_sequence ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003368 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003369 }
3370
3371 seq_cur = cert_cur->subject_alt_names.next;
3372 while( seq_cur != NULL )
3373 {
3374 seq_prv = seq_cur;
3375 seq_cur = seq_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003376 mbedtls_platform_zeroize( seq_prv,
3377 sizeof( mbedtls_x509_sequence ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003378 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003379 }
3380
Ron Eldor74d9acc2019-03-21 14:00:03 +02003381 seq_cur = cert_cur->certificate_policies.next;
3382 while( seq_cur != NULL )
3383 {
3384 seq_prv = seq_cur;
3385 seq_cur = seq_cur->next;
3386 mbedtls_platform_zeroize( seq_prv,
3387 sizeof( mbedtls_x509_sequence ) );
3388 mbedtls_free( seq_prv );
3389 }
3390
Hanno Becker1a65dcd2019-01-31 08:57:44 +00003391 if( cert_cur->raw.p != NULL && cert_cur->own_buffer )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003392 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003393 mbedtls_platform_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003394 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003395 }
3396
3397 cert_cur = cert_cur->next;
3398 }
3399 while( cert_cur != NULL );
3400
3401 cert_cur = crt;
3402 do
3403 {
3404 cert_prv = cert_cur;
3405 cert_cur = cert_cur->next;
3406
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003407 mbedtls_platform_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003408 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003409 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003410 }
3411 while( cert_cur != NULL );
3412}
3413
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003414#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3415/*
3416 * Initialize a restart context
3417 */
3418void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx )
3419{
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003420 mbedtls_pk_restart_init( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003421
3422 ctx->parent = NULL;
3423 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003424 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003425
3426 ctx->parent_is_trusted = -1;
3427
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003428 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003429 ctx->self_cnt = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003430 x509_crt_verify_chain_reset( &ctx->ver_chain );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003431}
3432
3433/*
3434 * Free the components of a restart context
3435 */
3436void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx )
3437{
3438 if( ctx == NULL )
3439 return;
3440
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003441 mbedtls_pk_restart_free( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003442 mbedtls_x509_crt_restart_init( ctx );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003443}
3444#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003446#endif /* MBEDTLS_X509_CRT_PARSE_C */