blob: a8f23c5fd5ac5bd774f10dbd208d95883547daae [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 }
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001659 else if( stat( entry_name, &sb ) == -1 )
Andres AGf9113192016-09-02 14:06:04 +01001660 {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001661 if( errno == ENOENT )
Eduardo Silvae1bfffc2019-04-25 10:43:26 -06001662 {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001663 /* Broken symbolic link - ignore this entry.
1664 stat(2) will return this error for either (a) a dangling
1665 symlink or (b) a missing file.
1666 Given that we have just obtained the filename from readdir,
1667 assume that it does exist and therefore treat this as a
1668 dangling symlink. */
1669 continue;
1670 }
1671 else
1672 {
1673 /* Some other file error; report the error. */
Eduardo Silvae1bfffc2019-04-25 10:43:26 -06001674 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1675 goto cleanup;
1676 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001677 }
1678
1679 if( !S_ISREG( sb.st_mode ) )
1680 continue;
1681
1682 // Ignore parse errors
1683 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001684 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001685 if( t_ret < 0 )
1686 ret++;
1687 else
1688 ret += t_ret;
1689 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001690
1691cleanup:
Andres AGf9113192016-09-02 14:06:04 +01001692 closedir( dir );
1693
Ron Eldor63140682017-01-09 19:27:59 +02001694#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001695 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001696 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Ron Eldor63140682017-01-09 19:27:59 +02001697#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001698
Paul Bakkerbe089b02013-10-14 15:51:50 +02001699#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001700
1701 return( ret );
1702}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001703#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001704
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001705/*
1706 * OtherName ::= SEQUENCE {
1707 * type-id OBJECT IDENTIFIER,
1708 * value [0] EXPLICIT ANY DEFINED BY type-id }
1709 *
1710 * HardwareModuleName ::= SEQUENCE {
1711 * hwType OBJECT IDENTIFIER,
1712 * hwSerialNum OCTET STRING }
1713 *
1714 * NOTE: we currently only parse and use otherName of type HwModuleName,
1715 * as defined in RFC 4108.
1716 */
1717static int x509_get_other_name( const mbedtls_x509_buf *subject_alt_name,
1718 mbedtls_x509_san_other_name *other_name )
1719{
Janos Follathab23cd12019-05-09 13:53:57 +01001720 int ret = 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001721 size_t len;
1722 unsigned char *p = subject_alt_name->p;
1723 const unsigned char *end = p + subject_alt_name->len;
1724 mbedtls_x509_buf cur_oid;
1725
1726 if( ( subject_alt_name->tag &
1727 ( MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK ) ) !=
1728 ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ) )
1729 {
1730 /*
1731 * The given subject alternative name is not of type "othername".
1732 */
1733 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
1734 }
1735
1736 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1737 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001738 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001739
1740 cur_oid.tag = MBEDTLS_ASN1_OID;
1741 cur_oid.p = p;
1742 cur_oid.len = len;
1743
1744 /*
1745 * Only HwModuleName is currently supported.
1746 */
1747 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid ) != 0 )
1748 {
1749 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1750 }
1751
Ron Eldor890819a2019-05-13 19:03:04 +03001752 if( p + len >= end )
1753 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001754 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001755 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1756 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001757 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001758 p += len;
1759 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1760 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001761 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001762
1763 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1764 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001765 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001766
1767 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001768 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001769
1770 other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID;
1771 other_name->value.hardware_module_name.oid.p = p;
1772 other_name->value.hardware_module_name.oid.len = len;
1773
Ron Eldor890819a2019-05-13 19:03:04 +03001774 if( p + len >= end )
1775 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001776 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001777 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1778 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001779 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001780 p += len;
1781 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1782 MBEDTLS_ASN1_OCTET_STRING ) ) != 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.val.tag = MBEDTLS_ASN1_OCTET_STRING;
1786 other_name->value.hardware_module_name.val.p = p;
1787 other_name->value.hardware_module_name.val.len = len;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001788 p += len;
1789 if( p != end )
1790 {
Ron Eldor890819a2019-05-13 19:03:04 +03001791 mbedtls_platform_zeroize( other_name,
Sébastien Duquette661d7252019-06-23 17:45:26 -04001792 sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001793 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1794 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001795 }
1796 return( 0 );
1797}
1798
Peter Kolbus9a969b62018-12-11 13:55:56 -06001799int mbedtls_x509_parse_subject_alt_name( const mbedtls_x509_buf *san_buf,
1800 mbedtls_x509_subject_alternative_name *san )
1801{
1802 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1803 switch( san_buf->tag &
1804 ( MBEDTLS_ASN1_TAG_CLASS_MASK |
1805 MBEDTLS_ASN1_TAG_VALUE_MASK ) )
1806 {
1807 /*
1808 * otherName
1809 */
1810 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ):
1811 {
1812 mbedtls_x509_san_other_name other_name;
1813
1814 ret = x509_get_other_name( san_buf, &other_name );
1815 if( ret != 0 )
1816 return( ret );
1817
1818 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1819 san->type = MBEDTLS_X509_SAN_OTHER_NAME;
1820 memcpy( &san->san.other_name,
1821 &other_name, sizeof( other_name ) );
1822
1823 }
1824 break;
1825
1826 /*
1827 * dNSName
1828 */
1829 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME ):
1830 {
1831 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1832 san->type = MBEDTLS_X509_SAN_DNS_NAME;
1833
1834 memcpy( &san->san.unstructured_name,
1835 san_buf, sizeof( *san_buf ) );
1836
1837 }
1838 break;
1839
1840 /*
1841 * Type not supported
1842 */
1843 default:
1844 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1845 }
1846 return( 0 );
1847}
1848
1849#if !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001850static int x509_info_subject_alt_name( char **buf, size_t *size,
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001851 const mbedtls_x509_sequence
1852 *subject_alt_name,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001853 const char *prefix )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001854{
Janos Follath865b3eb2019-12-16 11:46:15 +00001855 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001856 size_t n = *size;
1857 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001858 const mbedtls_x509_sequence *cur = subject_alt_name;
Ron Eldor890819a2019-05-13 19:03:04 +03001859 mbedtls_x509_subject_alternative_name san;
1860 int parse_ret;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001861
1862 while( cur != NULL )
1863 {
Ron Eldor890819a2019-05-13 19:03:04 +03001864 memset( &san, 0, sizeof( san ) );
1865 parse_ret = mbedtls_x509_parse_subject_alt_name( &cur->buf, &san );
1866 if( parse_ret != 0 )
1867 {
1868 if( parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1869 {
1870 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
1871 MBEDTLS_X509_SAFE_SNPRINTF;
1872 }
1873 else
1874 {
1875 ret = mbedtls_snprintf( p, n, "\n%s <malformed>", prefix );
1876 MBEDTLS_X509_SAFE_SNPRINTF;
1877 }
1878 cur = cur->next;
1879 continue;
1880 }
1881
1882 switch( san.type )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001883 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001884 /*
1885 * otherName
1886 */
Ron Eldora2913912019-05-16 16:17:38 +03001887 case MBEDTLS_X509_SAN_OTHER_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001888 {
1889 mbedtls_x509_san_other_name *other_name = &san.san.other_name;
1890
1891 ret = mbedtls_snprintf( p, n, "\n%s otherName :", prefix );
1892 MBEDTLS_X509_SAFE_SNPRINTF;
1893
1894 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME,
1895 &other_name->value.hardware_module_name.oid ) != 0 )
Janos Follath22f605f2019-05-10 10:37:17 +01001896 {
Ron Eldor890819a2019-05-13 19:03:04 +03001897 ret = mbedtls_snprintf( p, n, "\n%s hardware module name :", prefix );
1898 MBEDTLS_X509_SAFE_SNPRINTF;
1899 ret = mbedtls_snprintf( p, n, "\n%s hardware type : ", prefix );
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001900 MBEDTLS_X509_SAFE_SNPRINTF;
1901
Ron Eldor890819a2019-05-13 19:03:04 +03001902 ret = mbedtls_oid_get_numeric_string( p, n, &other_name->value.hardware_module_name.oid );
1903 MBEDTLS_X509_SAFE_SNPRINTF;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001904
Ron Eldor890819a2019-05-13 19:03:04 +03001905 ret = mbedtls_snprintf( p, n, "\n%s hardware serial number : ", prefix );
1906 MBEDTLS_X509_SAFE_SNPRINTF;
1907
1908 if( other_name->value.hardware_module_name.val.len >= n )
1909 {
1910 *p = '\0';
1911 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Janos Follath22f605f2019-05-10 10:37:17 +01001912 }
1913
Ron Eldora2913912019-05-16 16:17:38 +03001914 memcpy( p, other_name->value.hardware_module_name.val.p,
1915 other_name->value.hardware_module_name.val.len );
1916 p += other_name->value.hardware_module_name.val.len;
1917
Ron Eldor890819a2019-05-13 19:03:04 +03001918 n -= other_name->value.hardware_module_name.val.len;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001919
Ron Eldor890819a2019-05-13 19:03:04 +03001920 }/* MBEDTLS_OID_ON_HW_MODULE_NAME */
1921 }
1922 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001923
1924 /*
1925 * dNSName
1926 */
Ron Eldora2913912019-05-16 16:17:38 +03001927 case MBEDTLS_X509_SAN_DNS_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001928 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001929 ret = mbedtls_snprintf( p, n, "\n%s dNSName : ", prefix );
1930 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldor890819a2019-05-13 19:03:04 +03001931 if( san.san.unstructured_name.len >= n )
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001932 {
1933 *p = '\0';
1934 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
1935 }
Ron Eldora2913912019-05-16 16:17:38 +03001936
1937 memcpy( p, san.san.unstructured_name.p, san.san.unstructured_name.len );
1938 p += san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001939 n -= san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001940 }
1941 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001942
1943 /*
Ron Eldor890819a2019-05-13 19:03:04 +03001944 * Type not supported, skip item.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001945 */
1946 default:
Janos Follath22f605f2019-05-10 10:37:17 +01001947 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
1948 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001949 break;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001950 }
1951
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001952 cur = cur->next;
1953 }
1954
1955 *p = '\0';
1956
1957 *size = n;
1958 *buf = p;
1959
1960 return( 0 );
1961}
1962
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001963#define PRINT_ITEM(i) \
1964 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001965 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001966 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001967 sep = ", "; \
1968 }
1969
1970#define CERT_TYPE(type,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001971 if( ns_cert_type & (type) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001972 PRINT_ITEM( name );
1973
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001974static int x509_info_cert_type( char **buf, size_t *size,
1975 unsigned char ns_cert_type )
1976{
Janos Follath865b3eb2019-12-16 11:46:15 +00001977 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001978 size_t n = *size;
1979 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001980 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001982 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001983 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001984 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
1985 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1986 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001987 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
1988 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1989 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001990
1991 *size = n;
1992 *buf = p;
1993
1994 return( 0 );
1995}
1996
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001997#define KEY_USAGE(code,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001998 if( key_usage & (code) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001999 PRINT_ITEM( name );
2000
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002001static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02002002 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002003{
Janos Follath865b3eb2019-12-16 11:46:15 +00002004 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002005 size_t n = *size;
2006 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002007 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002009 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
2010 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002011 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
2012 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
2013 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002014 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
2015 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02002016 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
2017 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002018
2019 *size = n;
2020 *buf = p;
2021
2022 return( 0 );
2023}
2024
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002025static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002026 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002027{
Janos Follath865b3eb2019-12-16 11:46:15 +00002028 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002029 const char *desc;
2030 size_t n = *size;
2031 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002032 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002033 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002034
2035 while( cur != NULL )
2036 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002037 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002038 desc = "???";
2039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002040 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002041 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002042
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002043 sep = ", ";
2044
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002045 cur = cur->next;
2046 }
2047
2048 *size = n;
2049 *buf = p;
2050
2051 return( 0 );
2052}
2053
Ron Eldor74d9acc2019-03-21 14:00:03 +02002054static int x509_info_cert_policies( char **buf, size_t *size,
2055 const mbedtls_x509_sequence *certificate_policies )
2056{
Janos Follath865b3eb2019-12-16 11:46:15 +00002057 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor74d9acc2019-03-21 14:00:03 +02002058 const char *desc;
2059 size_t n = *size;
2060 char *p = *buf;
2061 const mbedtls_x509_sequence *cur = certificate_policies;
2062 const char *sep = "";
2063
2064 while( cur != NULL )
2065 {
2066 if( mbedtls_oid_get_certificate_policies( &cur->buf, &desc ) != 0 )
2067 desc = "???";
2068
2069 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
2070 MBEDTLS_X509_SAFE_SNPRINTF;
2071
2072 sep = ", ";
2073
2074 cur = cur->next;
2075 }
2076
2077 *size = n;
2078 *buf = p;
2079
2080 return( 0 );
2081}
2082
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002083/*
2084 * Return an informational string about the certificate.
2085 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002086#define BEFORE_COLON 18
2087#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002088int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
2089 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002090{
Janos Follath865b3eb2019-12-16 11:46:15 +00002091 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002092 size_t n;
2093 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002094 char key_size_str[BEFORE_COLON];
2095
2096 p = buf;
2097 n = size;
2098
Janos Follath98e28a72016-05-31 14:03:54 +01002099 if( NULL == crt )
2100 {
2101 ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" );
2102 MBEDTLS_X509_SAFE_SNPRINTF;
2103
2104 return( (int) ( size - n ) );
2105 }
2106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002107 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002108 prefix, crt->version );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002109 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002110 ret = mbedtls_snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002111 prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002112 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002114 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002115 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002117 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002118 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002119 ret = mbedtls_x509_dn_gets( p, n, &crt->issuer );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002120 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002122 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002123 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002124 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002125 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002127 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002128 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2129 crt->valid_from.year, crt->valid_from.mon,
2130 crt->valid_from.day, crt->valid_from.hour,
2131 crt->valid_from.min, crt->valid_from.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002132 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002134 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002135 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2136 crt->valid_to.year, crt->valid_to.mon,
2137 crt->valid_to.day, crt->valid_to.hour,
2138 crt->valid_to.min, crt->valid_to.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002139 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002141 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002142 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002144 ret = mbedtls_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02002145 crt->sig_md, crt->sig_opts );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002146 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002147
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002148 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002149 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
2150 mbedtls_pk_get_name( &crt->pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002151 {
2152 return( ret );
2153 }
2154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002155 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +02002156 (int) mbedtls_pk_get_bitlen( &crt->pk ) );
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é-Gonnardb28487d2014-04-01 12:19:09 +02002159 /*
2160 * Optional extensions
2161 */
2162
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002163 if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002165 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002166 crt->ca_istrue ? "true" : "false" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002167 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002168
2169 if( crt->max_pathlen > 0 )
2170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002171 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002172 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002173 }
2174 }
2175
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002176 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002177 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002178 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name :", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002179 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002180
2181 if( ( ret = x509_info_subject_alt_name( &p, &n,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002182 &crt->subject_alt_names,
Ron Eldor6aeae9e2019-05-20 12:00:36 +03002183 prefix ) ) != 0 )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002184 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002185 }
2186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002187 if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002188 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002189 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002190 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002191
2192 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
2193 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002194 }
2195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002196 if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002197 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002198 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002199 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002200
2201 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
2202 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002203 }
2204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002205 if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002206 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002207 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002208 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002209
2210 if( ( ret = x509_info_ext_key_usage( &p, &n,
2211 &crt->ext_key_usage ) ) != 0 )
2212 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002213 }
2214
Ron Eldor74d9acc2019-03-21 14:00:03 +02002215 if( crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES )
2216 {
2217 ret = mbedtls_snprintf( p, n, "\n%scertificate policies : ", prefix );
2218 MBEDTLS_X509_SAFE_SNPRINTF;
2219
2220 if( ( ret = x509_info_cert_policies( &p, &n,
2221 &crt->certificate_policies ) ) != 0 )
2222 return( ret );
2223 }
2224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002225 ret = mbedtls_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002226 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002227
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002228 return( (int) ( size - n ) );
2229}
2230
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002231struct x509_crt_verify_string {
2232 int code;
2233 const char *string;
2234};
2235
Hanno Becker7ac83f92020-10-09 10:48:22 +01002236#define X509_CRT_ERROR_INFO( err, err_str, info ) { err, info },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002237static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Hanno Becker7ac83f92020-10-09 10:48:22 +01002238 MBEDTLS_X509_CRT_ERROR_INFO_LIST
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002239 { 0, NULL }
2240};
Hanno Becker7ac83f92020-10-09 10:48:22 +01002241#undef X509_CRT_ERROR_INFO
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002242
2243int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002244 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002245{
Janos Follath865b3eb2019-12-16 11:46:15 +00002246 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002247 const struct x509_crt_verify_string *cur;
2248 char *p = buf;
2249 size_t n = size;
2250
2251 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
2252 {
2253 if( ( flags & cur->code ) == 0 )
2254 continue;
2255
2256 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002257 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002258 flags ^= cur->code;
2259 }
2260
2261 if( flags != 0 )
2262 {
2263 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
2264 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002265 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002266 }
2267
2268 return( (int) ( size - n ) );
2269}
Hanno Becker612a2f12020-10-09 09:19:39 +01002270#endif /* MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002271
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002272int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
2273 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002274{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002275 unsigned int usage_must, usage_may;
2276 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
2277 | MBEDTLS_X509_KU_DECIPHER_ONLY;
2278
2279 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
2280 return( 0 );
2281
2282 usage_must = usage & ~may_mask;
2283
2284 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
2285 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
2286
2287 usage_may = usage & may_mask;
2288
2289 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002290 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002291
2292 return( 0 );
2293}
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002295int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002296 const char *usage_oid,
2297 size_t usage_len )
2298{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002299 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002300
2301 /* Extension is not mandatory, absent means no restriction */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002302 if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002303 return( 0 );
2304
2305 /*
2306 * Look for the requested usage (or wildcard ANY) in our list
2307 */
2308 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
2309 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002310 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002311
2312 if( cur_oid->len == usage_len &&
2313 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
2314 {
2315 return( 0 );
2316 }
2317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002318 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002319 return( 0 );
2320 }
2321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002322 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002323}
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002325#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002326/*
2327 * Return 1 if the certificate is revoked, or 0 otherwise.
2328 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002329int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002330{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002331 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002332
2333 while( cur != NULL && cur->serial.len != 0 )
2334 {
2335 if( crt->serial.len == cur->serial.len &&
2336 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
2337 {
Raoul Strackxa4e86142020-06-15 17:03:13 +02002338 return( 1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002339 }
2340
2341 cur = cur->next;
2342 }
2343
2344 return( 0 );
2345}
2346
2347/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002348 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002349 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002350 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002351static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002352 mbedtls_x509_crl *crl_list,
2353 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002354{
2355 int flags = 0;
pespacek7599a772022-02-07 14:40:55 +01002356#if defined(MBEDTLS_USE_PSA_CRYPTO)
2357 unsigned char hash[PSA_HASH_MAX_SIZE];
2358 psa_algorithm_t psa_algorithm;
2359#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002360 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2361 const mbedtls_md_info_t *md_info;
pespacek7599a772022-02-07 14:40:55 +01002362#endif /* MBEDTLS_USE_PSA_CRYPTO */
2363 size_t hash_length;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002364
2365 if( ca == NULL )
2366 return( flags );
2367
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002368 while( crl_list != NULL )
2369 {
2370 if( crl_list->version == 0 ||
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002371 x509_name_cmp( &crl_list->issuer, &ca->subject ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002372 {
2373 crl_list = crl_list->next;
2374 continue;
2375 }
2376
2377 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002378 * Check if the CA is configured to sign CRLs
2379 */
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002380 if( mbedtls_x509_crt_check_key_usage( ca,
2381 MBEDTLS_X509_KU_CRL_SIGN ) != 0 )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002382 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002383 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002384 break;
2385 }
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002386
2387 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002388 * Check if CRL is correctly signed by the trusted CA
2389 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002390 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
2391 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
2392
2393 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
2394 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
2395
pespacek7599a772022-02-07 14:40:55 +01002396#if defined(MBEDTLS_USE_PSA_CRYPTO)
2397 psa_algorithm = mbedtls_psa_translate_md( crl_list->sig_md );
pespacekd924e552022-02-28 11:49:54 +01002398 if( psa_hash_compute( psa_algorithm,
2399 crl_list->tbs.p,
2400 crl_list->tbs.len,
2401 hash,
2402 sizeof( hash ),
2403 &hash_length ) != PSA_SUCCESS )
pespaceka7a64692022-02-14 15:18:43 +01002404 {
2405 /* Note: this can't happen except after an internal error */
2406 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
2407 break;
2408 }
pespacek7599a772022-02-07 14:40:55 +01002409#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002410 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
pespacek7599a772022-02-07 14:40:55 +01002411 hash_length = mbedtls_md_get_size( md_info );
2412 if( mbedtls_md( md_info,
2413 crl_list->tbs.p,
2414 crl_list->tbs.len,
2415 hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002416 {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002417 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002418 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002419 break;
2420 }
pespaceka7a64692022-02-14 15:18:43 +01002421#endif /* MBEDTLS_USE_PSA_CRYPTO */
2422
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002423 if( x509_profile_check_key( profile, &ca->pk ) != 0 )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002424 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002426 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
pespacek7599a772022-02-07 14:40:55 +01002427 crl_list->sig_md, hash, hash_length,
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02002428 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002429 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002430 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002431 break;
2432 }
2433
2434 /*
2435 * Check for validity of CRL (Do not drop out)
2436 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002437 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002438 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002439
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002440 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002441 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002442
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002443 /*
2444 * Check if certificate is revoked
2445 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002446 if( mbedtls_x509_crt_is_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002447 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002448 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002449 break;
2450 }
2451
2452 crl_list = crl_list->next;
2453 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002454
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002455 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002456}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002457#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002458
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002459/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002460 * Check the signature of a certificate by its parent
2461 */
2462static int x509_crt_check_signature( const mbedtls_x509_crt *child,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002463 mbedtls_x509_crt *parent,
2464 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002465{
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002466 size_t hash_len;
2467#if !defined(MBEDTLS_USE_PSA_CRYPTO)
pespacek7599a772022-02-07 14:40:55 +01002468 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002469 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002470 md_info = mbedtls_md_info_from_type( child->sig_md );
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002471 hash_len = mbedtls_md_get_size( md_info );
Andrzej Kurek8b38ff52018-11-20 03:20:09 -05002472
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002473 /* Note: hash errors can happen only after an internal error */
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002474 if( mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash ) != 0 )
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002475 return( -1 );
2476#else
pespacek7599a772022-02-07 14:40:55 +01002477 unsigned char hash[PSA_HASH_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002478 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( child->sig_md );
pespacek7599a772022-02-07 14:40:55 +01002479 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002480
pespacek7599a772022-02-07 14:40:55 +01002481 status = psa_hash_compute( hash_alg,
2482 child->tbs.p,
2483 child->tbs.len,
2484 hash,
pespacekb9f07a72022-02-14 15:13:26 +01002485 sizeof( hash ),
pespacek7599a772022-02-07 14:40:55 +01002486 &hash_len );
2487 if( status != PSA_SUCCESS )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002488 {
pespacek3110c7b2022-02-14 15:07:41 +01002489 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002490 }
2491
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002492#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002493 /* Skip expensive computation on obvious mismatch */
2494 if( ! mbedtls_pk_can_do( &parent->pk, child->sig_pk ) )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002495 return( -1 );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002496
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002497#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002498 if( rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA )
2499 {
2500 return( mbedtls_pk_verify_restartable( &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002501 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002502 child->sig.p, child->sig.len, &rs_ctx->pk ) );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002503 }
2504#else
2505 (void) rs_ctx;
2506#endif
2507
2508 return( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002509 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002510 child->sig.p, child->sig.len ) );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002511}
2512
2513/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002514 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2515 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002516 *
2517 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002518 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002519static int x509_crt_check_parent( const mbedtls_x509_crt *child,
2520 const mbedtls_x509_crt *parent,
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002521 int top )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002522{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002523 int need_ca_bit;
2524
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002525 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02002526 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002527 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002528
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002529 /* Parent must have the basicConstraints CA bit set as a general rule */
2530 need_ca_bit = 1;
2531
2532 /* Exception: v1/v2 certificates that are locally trusted. */
2533 if( top && parent->version < 3 )
2534 need_ca_bit = 0;
2535
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002536 if( need_ca_bit && ! parent->ca_istrue )
2537 return( -1 );
2538
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002539 if( need_ca_bit &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002540 mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002541 {
2542 return( -1 );
2543 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002544
2545 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002546}
2547
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002548/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002549 * Find a suitable parent for child in candidates, or return NULL.
2550 *
2551 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002552 * 1. subject name matches child's issuer
2553 * 2. if necessary, the CA bit is set and key usage allows signing certs
2554 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002555 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002556 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002557 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002558 * If there's a suitable candidate which is also time-valid, return the first
2559 * such. Otherwise, return the first suitable candidate (or NULL if there is
2560 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002561 *
2562 * The rationale for this rule is that someone could have a list of trusted
2563 * roots with two versions on the same root with different validity periods.
2564 * (At least one user reported having such a list and wanted it to just work.)
2565 * The reason we don't just require time-validity is that generally there is
2566 * only one version, and if it's expired we want the flags to state that
2567 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002568 *
2569 * The rationale for rule 3 (signature for trusted roots) is that users might
2570 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002571 * way we select the correct one is by checking the signature (as we don't
2572 * rely on key identifier extensions). (This is one way users might choose to
2573 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002574 *
2575 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002576 * - [in] child: certificate for which we're looking for a parent
2577 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002578 * - [out] r_parent: parent found (or NULL)
2579 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002580 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2581 * of the chain, 0 otherwise
2582 * - [in] path_cnt: number of intermediates seen so far
2583 * - [in] self_cnt: number of self-signed intermediates seen so far
2584 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002585 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002586 *
2587 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002588 * - 0 on success
2589 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002590 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002591static int x509_crt_find_parent_in(
2592 mbedtls_x509_crt *child,
2593 mbedtls_x509_crt *candidates,
2594 mbedtls_x509_crt **r_parent,
2595 int *r_signature_is_good,
2596 int top,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002597 unsigned path_cnt,
2598 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002599 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002600{
Janos Follath865b3eb2019-12-16 11:46:15 +00002601 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002602 mbedtls_x509_crt *parent, *fallback_parent;
Benjamin Kier36050732019-05-30 14:49:17 -04002603 int signature_is_good = 0, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002604
2605#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002606 /* did we have something in progress? */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002607 if( rs_ctx != NULL && rs_ctx->parent != NULL )
2608 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002609 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002610 parent = rs_ctx->parent;
2611 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002612 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002613
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002614 /* clear saved state */
2615 rs_ctx->parent = NULL;
2616 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002617 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002618
2619 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002620 goto check_signature;
2621 }
2622#endif
2623
2624 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002625 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002626
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002627 for( parent = candidates; parent != NULL; parent = parent->next )
2628 {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002629 /* basic parenting skills (name, CA bit, key usage) */
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002630 if( x509_crt_check_parent( child, parent, top ) != 0 )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002631 continue;
2632
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002633 /* +1 because stored max_pathlen is 1 higher that the actual value */
2634 if( parent->max_pathlen > 0 &&
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002635 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt )
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002636 {
2637 continue;
2638 }
2639
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002640 /* Signature */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002641#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2642check_signature:
2643#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002644 ret = x509_crt_check_signature( child, parent, rs_ctx );
2645
2646#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002647 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2648 {
2649 /* save state */
2650 rs_ctx->parent = parent;
2651 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002652 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002653
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002654 return( ret );
2655 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002656#else
2657 (void) ret;
2658#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002659
2660 signature_is_good = ret == 0;
2661 if( top && ! signature_is_good )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002662 continue;
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002663
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002664 /* optional time check */
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002665 if( mbedtls_x509_time_is_past( &parent->valid_to ) ||
2666 mbedtls_x509_time_is_future( &parent->valid_from ) )
2667 {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002668 if( fallback_parent == NULL )
2669 {
2670 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002671 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002672 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002673
2674 continue;
2675 }
2676
Andy Gross1f627142019-01-30 10:25:53 -06002677 *r_parent = parent;
2678 *r_signature_is_good = signature_is_good;
2679
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002680 break;
2681 }
2682
Andy Gross1f627142019-01-30 10:25:53 -06002683 if( parent == NULL )
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002684 {
2685 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002686 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002687 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002688
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002689 return( 0 );
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002690}
2691
2692/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002693 * Find a parent in trusted CAs or the provided chain, or return NULL.
2694 *
2695 * Searches in trusted CAs first, and return the first suitable parent found
2696 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002697 *
2698 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002699 * - [in] child: certificate for which we're looking for a parent, followed
2700 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002701 * - [in] trust_ca: list of locally trusted certificates
2702 * - [out] parent: parent found (or NULL)
2703 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2704 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2705 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2706 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002707 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002708 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002709 *
2710 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002711 * - 0 on success
2712 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002713 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002714static int x509_crt_find_parent(
2715 mbedtls_x509_crt *child,
2716 mbedtls_x509_crt *trust_ca,
2717 mbedtls_x509_crt **parent,
2718 int *parent_is_trusted,
2719 int *signature_is_good,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002720 unsigned path_cnt,
2721 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002722 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002723{
Janos Follath865b3eb2019-12-16 11:46:15 +00002724 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002725 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002726
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002727 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002728
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002729#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002730 /* restore then clear saved state if we have some stored */
2731 if( rs_ctx != NULL && rs_ctx->parent_is_trusted != -1 )
2732 {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002733 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002734 rs_ctx->parent_is_trusted = -1;
2735 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002736#endif
2737
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002738 while( 1 ) {
2739 search_list = *parent_is_trusted ? trust_ca : child->next;
2740
2741 ret = x509_crt_find_parent_in( child, search_list,
2742 parent, signature_is_good,
2743 *parent_is_trusted,
2744 path_cnt, self_cnt, rs_ctx );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002745
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002746#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002747 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2748 {
2749 /* save state */
2750 rs_ctx->parent_is_trusted = *parent_is_trusted;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002751 return( ret );
2752 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002753#else
2754 (void) ret;
2755#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002756
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002757 /* stop here if found or already in second iteration */
2758 if( *parent != NULL || *parent_is_trusted == 0 )
2759 break;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002760
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002761 /* prepare second iteration */
2762 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002763 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002764
2765 /* extra precaution against mistakes in the caller */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002766 if( *parent == NULL )
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002767 {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002768 *parent_is_trusted = 0;
2769 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002770 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002771
2772 return( 0 );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002773}
2774
2775/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002776 * Check if an end-entity certificate is locally trusted
2777 *
2778 * Currently we require such certificates to be self-signed (actually only
2779 * check for self-issued as self-signatures are not checked)
2780 */
2781static int x509_crt_check_ee_locally_trusted(
2782 mbedtls_x509_crt *crt,
2783 mbedtls_x509_crt *trust_ca )
2784{
2785 mbedtls_x509_crt *cur;
2786
2787 /* must be self-issued */
2788 if( x509_name_cmp( &crt->issuer, &crt->subject ) != 0 )
2789 return( -1 );
2790
2791 /* look for an exact match with trusted cert */
2792 for( cur = trust_ca; cur != NULL; cur = cur->next )
2793 {
2794 if( crt->raw.len == cur->raw.len &&
2795 memcmp( crt->raw.p, cur->raw.p, crt->raw.len ) == 0 )
2796 {
2797 return( 0 );
2798 }
2799 }
2800
2801 /* too bad */
2802 return( -1 );
2803}
2804
2805/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002806 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002807 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002808 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2809 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002810 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002811 * such that every cert in the chain is a child of the next one,
2812 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002813 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002814 * Verify that chain and return it with flags for all issues found.
2815 *
2816 * Special cases:
2817 * - EE == Rj -> return a one-element list containing it
2818 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2819 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002820 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002821 * Tests for (aspects of) this function should include at least:
2822 * - trusted EE
2823 * - EE -> trusted root
Antonin Décimo36e89b52019-01-23 15:24:37 +01002824 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002825 * - if relevant: EE untrusted
2826 * - if relevant: EE -> intermediate, untrusted
2827 * with the aspect under test checked at each relevant level (EE, int, root).
2828 * For some aspects longer chains are required, but usually length 2 is
2829 * enough (but length 1 is not in general).
2830 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002831 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002832 * - [in] crt: the cert list EE, C1, ..., Cn
2833 * - [in] trust_ca: the trusted list R1, ..., Rp
2834 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002835 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002836 * Only valid when return value is 0, may contain garbage otherwise!
2837 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002838 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002839 *
2840 * Return value:
2841 * - non-zero if the chain could not be fully built and examined
2842 * - 0 is the chain was successfully built and examined,
2843 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002844 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002845static int x509_crt_verify_chain(
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002846 mbedtls_x509_crt *crt,
2847 mbedtls_x509_crt *trust_ca,
2848 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00002849 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2850 void *p_ca_cb,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002851 const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002852 mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002853 mbedtls_x509_crt_restart_ctx *rs_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002854{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002855 /* Don't initialize any of those variables here, so that the compiler can
2856 * catch potential issues with jumping ahead when restarting */
Janos Follath865b3eb2019-12-16 11:46:15 +00002857 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002858 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002859 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002860 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002861 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002862 int parent_is_trusted;
2863 int child_is_trusted;
2864 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002865 unsigned self_cnt;
Hanno Beckerf53893b2019-03-28 13:45:55 +00002866 mbedtls_x509_crt *cur_trust_ca = NULL;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002867
2868#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2869 /* resume if we had an operation in progress */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002870 if( rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent )
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002871 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002872 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002873 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002874 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002875
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002876 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002877 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002878 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002879 flags = &cur->flags;
2880
2881 goto find_parent;
2882 }
2883#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002884
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002885 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002886 self_cnt = 0;
2887 parent_is_trusted = 0;
2888 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002889
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002890 while( 1 ) {
2891 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002892 cur = &ver_chain->items[ver_chain->len];
2893 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002894 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002895 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002896 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002897
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002898 /* Check time-validity (all certificates) */
2899 if( mbedtls_x509_time_is_past( &child->valid_to ) )
2900 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002901
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002902 if( mbedtls_x509_time_is_future( &child->valid_from ) )
2903 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002904
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002905 /* Stop here for trusted roots (but not for trusted EE certs) */
2906 if( child_is_trusted )
2907 return( 0 );
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002908
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002909 /* Check signature algorithm: MD & PK algs */
2910 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
2911 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002912
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002913 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
2914 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002915
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002916 /* Special case: EE certs that are locally trusted */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002917 if( ver_chain->len == 1 &&
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002918 x509_crt_check_ee_locally_trusted( child, trust_ca ) == 0 )
2919 {
2920 return( 0 );
2921 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002922
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002923#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2924find_parent:
2925#endif
Hanno Beckerf53893b2019-03-28 13:45:55 +00002926
2927 /* Obtain list of potential trusted signers from CA callback,
2928 * or use statically provided list. */
2929#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
2930 if( f_ca_cb != NULL )
2931 {
2932 mbedtls_x509_crt_free( ver_chain->trust_ca_cb_result );
2933 mbedtls_free( ver_chain->trust_ca_cb_result );
2934 ver_chain->trust_ca_cb_result = NULL;
2935
2936 ret = f_ca_cb( p_ca_cb, child, &ver_chain->trust_ca_cb_result );
2937 if( ret != 0 )
2938 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2939
2940 cur_trust_ca = ver_chain->trust_ca_cb_result;
2941 }
2942 else
2943#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2944 {
2945 ((void) f_ca_cb);
2946 ((void) p_ca_cb);
2947 cur_trust_ca = trust_ca;
2948 }
2949
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002950 /* Look for a parent in trusted CAs or up the chain */
Hanno Beckerf53893b2019-03-28 13:45:55 +00002951 ret = x509_crt_find_parent( child, cur_trust_ca, &parent,
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002952 &parent_is_trusted, &signature_is_good,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002953 ver_chain->len - 1, self_cnt, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002954
2955#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002956 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2957 {
2958 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002959 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002960 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002961 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002962
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002963 return( ret );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002964 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002965#else
2966 (void) ret;
2967#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002968
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002969 /* No parent? We're done here */
2970 if( parent == NULL )
2971 {
2972 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2973 return( 0 );
2974 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002975
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002976 /* Count intermediate self-issued (not necessarily self-signed) certs.
2977 * These can occur with some strategies for key rollover, see [SIRO],
2978 * and should be excluded from max_pathlen checks. */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002979 if( ver_chain->len != 1 &&
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002980 x509_name_cmp( &child->issuer, &child->subject ) == 0 )
2981 {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002982 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002983 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002984
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002985 /* path_cnt is 0 for the first intermediate CA,
2986 * and if parent is trusted it's not an intermediate CA */
2987 if( ! parent_is_trusted &&
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002988 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002989 {
2990 /* return immediately to avoid overflow the chain array */
2991 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2992 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002993
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002994 /* signature was checked while searching parent */
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002995 if( ! signature_is_good )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002996 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2997
2998 /* check size of signing key */
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002999 if( x509_profile_check_key( profile, &parent->pk ) != 0 )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003000 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003002#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003003 /* Check trusted CA's CRL for the given crt */
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02003004 *flags |= x509_crt_verifycrl( child, parent, ca_crl, profile );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003005#else
3006 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003007#endif
3008
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003009 /* prepare for next iteration */
3010 child = parent;
3011 parent = NULL;
3012 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003013 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003014 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003015}
3016
3017/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003018 * Check for CN match
3019 */
3020static int x509_crt_check_cn( const mbedtls_x509_buf *name,
3021 const char *cn, size_t cn_len )
3022{
3023 /* try exact match */
3024 if( name->len == cn_len &&
3025 x509_memcasecmp( cn, name->p, cn_len ) == 0 )
3026 {
3027 return( 0 );
3028 }
3029
3030 /* try wildcard match */
Manuel Pégourié-Gonnard900fba62017-10-18 14:28:11 +02003031 if( x509_check_wildcard( cn, name ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003032 {
3033 return( 0 );
3034 }
3035
3036 return( -1 );
3037}
3038
3039/*
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003040 * Check for SAN match, see RFC 5280 Section 4.2.1.6
3041 */
3042static int x509_crt_check_san( const mbedtls_x509_buf *name,
3043 const char *cn, size_t cn_len )
3044{
3045 const unsigned char san_type = (unsigned char) name->tag &
3046 MBEDTLS_ASN1_TAG_VALUE_MASK;
3047
3048 /* dNSName */
3049 if( san_type == MBEDTLS_X509_SAN_DNS_NAME )
3050 return( x509_crt_check_cn( name, cn, cn_len ) );
3051
3052 /* (We may handle other types here later.) */
3053
3054 /* Unrecognized type */
3055 return( -1 );
3056}
3057
3058/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003059 * Verify the requested CN - only call this if cn is not NULL!
3060 */
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003061static void x509_crt_verify_name( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003062 const char *cn,
3063 uint32_t *flags )
3064{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003065 const mbedtls_x509_name *name;
3066 const mbedtls_x509_sequence *cur;
3067 size_t cn_len = strlen( cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003068
3069 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
3070 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003071 for( cur = &crt->subject_alt_names; cur != NULL; cur = cur->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003072 {
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003073 if( x509_crt_check_san( &cur->buf, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003074 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003075 }
3076
3077 if( cur == NULL )
3078 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3079 }
3080 else
3081 {
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02003082 for( name = &crt->subject; name != NULL; name = name->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003083 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003084 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, &name->oid ) == 0 &&
3085 x509_crt_check_cn( &name->val, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003086 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003087 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003088 }
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003089 }
3090
3091 if( name == NULL )
3092 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3093 }
3094}
3095
3096/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003097 * Merge the flags for all certs in the chain, after calling callback
3098 */
3099static int x509_crt_merge_flags_with_cb(
3100 uint32_t *flags,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003101 const mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003102 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3103 void *p_vrfy )
3104{
Janos Follath865b3eb2019-12-16 11:46:15 +00003105 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003106 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003107 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003108 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003109
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003110 for( i = ver_chain->len; i != 0; --i )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003111 {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003112 cur = &ver_chain->items[i-1];
3113 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003114
3115 if( NULL != f_vrfy )
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003116 if( ( ret = f_vrfy( p_vrfy, cur->crt, (int) i-1, &cur_flags ) ) != 0 )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003117 return( ret );
3118
3119 *flags |= cur_flags;
3120 }
3121
3122 return( 0 );
3123}
3124
3125/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003126 * Verify the certificate validity, with profile, restartable version
3127 *
3128 * This function:
3129 * - checks the requested CN (if any)
3130 * - checks the type and size of the EE cert's key,
3131 * as that isn't done as part of chain building/verification currently
3132 * - builds and verifies the chain
3133 * - then calls the callback and merges the flags
Hanno Becker3116fb32019-03-28 13:34:42 +00003134 *
3135 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
3136 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
3137 * verification routine to search for trusted signers, and CRLs will
3138 * be disabled. Otherwise, `trust_ca` will be used as the static list
3139 * of trusted signers, and `ca_crl` will be use as the static list
3140 * of CRLs.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003141 */
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003142static int x509_crt_verify_restartable_ca_cb( mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003143 mbedtls_x509_crt *trust_ca,
3144 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003145 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3146 void *p_ca_cb,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003147 const mbedtls_x509_crt_profile *profile,
3148 const char *cn, uint32_t *flags,
3149 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3150 void *p_vrfy,
3151 mbedtls_x509_crt_restart_ctx *rs_ctx )
3152{
Janos Follath865b3eb2019-12-16 11:46:15 +00003153 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003154 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003155 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003156 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003157
3158 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003159 ee_flags = 0;
3160 x509_crt_verify_chain_reset( &ver_chain );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003161
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003162 if( profile == NULL )
3163 {
3164 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3165 goto exit;
3166 }
3167
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003168 /* check name if requested */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003169 if( cn != NULL )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003170 x509_crt_verify_name( crt, cn, &ee_flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003171
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003172 /* Check the type and size of the key */
3173 pk_type = mbedtls_pk_get_type( &crt->pk );
3174
3175 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003176 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003177
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02003178 if( x509_profile_check_key( profile, &crt->pk ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003179 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003180
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003181 /* Check the chain */
Hanno Becker3116fb32019-03-28 13:34:42 +00003182 ret = x509_crt_verify_chain( crt, trust_ca, ca_crl,
3183 f_ca_cb, p_ca_cb, profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003184 &ver_chain, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003185
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003186 if( ret != 0 )
3187 goto exit;
3188
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003189 /* Merge end-entity flags */
3190 ver_chain.items[0].flags |= ee_flags;
3191
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003192 /* Build final flags, calling callback on the way if any */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003193 ret = x509_crt_merge_flags_with_cb( flags, &ver_chain, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003194
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003195exit:
Hanno Beckerf53893b2019-03-28 13:45:55 +00003196
3197#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3198 mbedtls_x509_crt_free( ver_chain.trust_ca_cb_result );
3199 mbedtls_free( ver_chain.trust_ca_cb_result );
3200 ver_chain.trust_ca_cb_result = NULL;
3201#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3202
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003203#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3204 if( rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
3205 mbedtls_x509_crt_restart_free( rs_ctx );
3206#endif
3207
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003208 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3209 * the SSL module for authmode optional, but non-zero return from the
3210 * callback means a fatal error so it shouldn't be ignored */
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003211 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
3212 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3213
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003214 if( ret != 0 )
3215 {
3216 *flags = (uint32_t) -1;
3217 return( ret );
3218 }
3219
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003220 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003221 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003222
3223 return( 0 );
3224}
3225
Hanno Becker3116fb32019-03-28 13:34:42 +00003226
3227/*
3228 * Verify the certificate validity (default profile, not restartable)
3229 */
3230int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
3231 mbedtls_x509_crt *trust_ca,
3232 mbedtls_x509_crl *ca_crl,
3233 const char *cn, uint32_t *flags,
3234 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3235 void *p_vrfy )
3236{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003237 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003238 NULL, NULL,
3239 &mbedtls_x509_crt_profile_default,
3240 cn, flags,
3241 f_vrfy, p_vrfy, NULL ) );
3242}
3243
3244/*
3245 * Verify the certificate validity (user-chosen profile, not restartable)
3246 */
3247int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
3248 mbedtls_x509_crt *trust_ca,
3249 mbedtls_x509_crl *ca_crl,
3250 const mbedtls_x509_crt_profile *profile,
3251 const char *cn, uint32_t *flags,
3252 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3253 void *p_vrfy )
3254{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003255 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003256 NULL, NULL,
3257 profile, cn, flags,
3258 f_vrfy, p_vrfy, NULL ) );
3259}
3260
3261#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3262/*
3263 * Verify the certificate validity (user-chosen profile, CA callback,
3264 * not restartable).
3265 */
Jarno Lamsa31d9db62019-04-01 14:33:49 +03003266int mbedtls_x509_crt_verify_with_ca_cb( mbedtls_x509_crt *crt,
Hanno Becker3116fb32019-03-28 13:34:42 +00003267 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3268 void *p_ca_cb,
3269 const mbedtls_x509_crt_profile *profile,
3270 const char *cn, uint32_t *flags,
3271 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3272 void *p_vrfy )
3273{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003274 return( x509_crt_verify_restartable_ca_cb( crt, NULL, NULL,
Hanno Becker3116fb32019-03-28 13:34:42 +00003275 f_ca_cb, p_ca_cb,
3276 profile, cn, flags,
3277 f_vrfy, p_vrfy, NULL ) );
3278}
3279#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3280
3281int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
3282 mbedtls_x509_crt *trust_ca,
3283 mbedtls_x509_crl *ca_crl,
3284 const mbedtls_x509_crt_profile *profile,
3285 const char *cn, uint32_t *flags,
3286 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3287 void *p_vrfy,
3288 mbedtls_x509_crt_restart_ctx *rs_ctx )
3289{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003290 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003291 NULL, NULL,
3292 profile, cn, flags,
3293 f_vrfy, p_vrfy, rs_ctx ) );
3294}
3295
3296
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003297/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003298 * Initialize a certificate chain
3299 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003300void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02003301{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003302 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02003303}
3304
3305/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003306 * Unallocate all certificate data
3307 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003308void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003309{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003310 mbedtls_x509_crt *cert_cur = crt;
3311 mbedtls_x509_crt *cert_prv;
3312 mbedtls_x509_name *name_cur;
3313 mbedtls_x509_name *name_prv;
3314 mbedtls_x509_sequence *seq_cur;
3315 mbedtls_x509_sequence *seq_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003316
3317 if( crt == NULL )
3318 return;
3319
3320 do
3321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003322 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003324#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
3325 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003326#endif
3327
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003328 name_cur = cert_cur->issuer.next;
3329 while( name_cur != NULL )
3330 {
3331 name_prv = name_cur;
3332 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003333 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003334 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003335 }
3336
3337 name_cur = cert_cur->subject.next;
3338 while( name_cur != NULL )
3339 {
3340 name_prv = name_cur;
3341 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003342 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003343 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003344 }
3345
3346 seq_cur = cert_cur->ext_key_usage.next;
3347 while( seq_cur != NULL )
3348 {
3349 seq_prv = seq_cur;
3350 seq_cur = seq_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003351 mbedtls_platform_zeroize( seq_prv,
3352 sizeof( mbedtls_x509_sequence ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003353 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003354 }
3355
3356 seq_cur = cert_cur->subject_alt_names.next;
3357 while( seq_cur != NULL )
3358 {
3359 seq_prv = seq_cur;
3360 seq_cur = seq_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003361 mbedtls_platform_zeroize( seq_prv,
3362 sizeof( mbedtls_x509_sequence ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003363 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003364 }
3365
Ron Eldor74d9acc2019-03-21 14:00:03 +02003366 seq_cur = cert_cur->certificate_policies.next;
3367 while( seq_cur != NULL )
3368 {
3369 seq_prv = seq_cur;
3370 seq_cur = seq_cur->next;
3371 mbedtls_platform_zeroize( seq_prv,
3372 sizeof( mbedtls_x509_sequence ) );
3373 mbedtls_free( seq_prv );
3374 }
3375
Hanno Becker1a65dcd2019-01-31 08:57:44 +00003376 if( cert_cur->raw.p != NULL && cert_cur->own_buffer )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003377 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003378 mbedtls_platform_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003379 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003380 }
3381
3382 cert_cur = cert_cur->next;
3383 }
3384 while( cert_cur != NULL );
3385
3386 cert_cur = crt;
3387 do
3388 {
3389 cert_prv = cert_cur;
3390 cert_cur = cert_cur->next;
3391
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003392 mbedtls_platform_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003393 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003394 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003395 }
3396 while( cert_cur != NULL );
3397}
3398
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003399#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3400/*
3401 * Initialize a restart context
3402 */
3403void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx )
3404{
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003405 mbedtls_pk_restart_init( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003406
3407 ctx->parent = NULL;
3408 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003409 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003410
3411 ctx->parent_is_trusted = -1;
3412
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003413 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003414 ctx->self_cnt = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003415 x509_crt_verify_chain_reset( &ctx->ver_chain );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003416}
3417
3418/*
3419 * Free the components of a restart context
3420 */
3421void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx )
3422{
3423 if( ctx == NULL )
3424 return;
3425
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003426 mbedtls_pk_restart_free( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003427 mbedtls_x509_crt_restart_init( ctx );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003428}
3429#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003431#endif /* MBEDTLS_X509_CRT_PARSE_C */