blob: b0c2d481a9612350cad5fcfb70d84a1a3f49bd2a [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#include "psa/crypto_values.h"
51#endif /* MBEDTLS_USE_PSA_CRYPTO */
Andrzej Kurekd4a65532018-10-31 06:18:39 -040052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020055#else
Simon Butcherd2642582018-10-03 15:11:19 +010056#include <stdio.h>
Rich Evans00ab4702015-02-06 13:43:58 +000057#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#define mbedtls_free free
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020059#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#define mbedtls_snprintf snprintf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020061#endif
62
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000064#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010065#endif
66
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
72
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073#if defined(MBEDTLS_FS_IO)
Rich Evans00ab4702015-02-06 13:43:58 +000074#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020075#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020076#include <sys/types.h>
77#include <sys/stat.h>
Martino Facchin0ec05ec2021-05-04 11:47:36 +020078#if defined(__MBED__)
79#include <platform/mbed_retarget.h>
80#else
Paul Bakker7c6b2c32013-09-16 13:49:26 +020081#include <dirent.h>
Martino Facchin0ec05ec2021-05-04 11:47:36 +020082#endif /* __MBED__ */
Rich Evans00ab4702015-02-06 13:43:58 +000083#endif /* !_WIN32 || EFIX64 || EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020084#endif
85
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +020086/*
87 * Item in a verification chain: cert and flags for it
88 */
89typedef struct {
90 mbedtls_x509_crt *crt;
91 uint32_t flags;
92} x509_crt_verify_chain_item;
93
94/*
95 * Max size of verification chain: end-entity + intermediates + trusted root
96 */
97#define X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
Paul Bakker34617722014-06-13 17:20:13 +020098
Gilles Peskineffb92da2021-06-02 00:03:26 +020099/* Default profile. Do not remove items unless there are serious security
100 * concerns. */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200101const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
102{
Gilles Peskineae270bf2021-06-02 00:05:29 +0200103 /* Hashes from SHA-256 and above. Note that this selection
104 * should be aligned with ssl_preset_default_hashes in ssl_tls.c. */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200105 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
106 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
107 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
108 0xFFFFFFF, /* Any PK alg */
109#if defined(MBEDTLS_ECP_C)
Gilles Peskineae270bf2021-06-02 00:05:29 +0200110 /* Curves at or above 128-bit security level. Note that this selection
111 * should be aligned with ssl_preset_default_curves in ssl_tls.c. */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200112 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
113 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
114 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
115 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
116 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
117 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
Gilles Peskine39957502021-06-17 23:17:52 +0200118 0,
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200119#else
120 0,
121#endif
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200122 2048,
123};
124
Gilles Peskineffb92da2021-06-02 00:03:26 +0200125/* Next-generation profile. Currently identical to the default, but may
126 * be tightened at any time. */
127const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200128{
129 /* Hashes from SHA-256 and above. */
130 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
131 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
132 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
133 0xFFFFFFF, /* Any PK alg */
134#if defined(MBEDTLS_ECP_C)
135 /* Curves at or above 128-bit security level. */
136 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
137 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
138 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
139 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
140 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
141 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
142 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256K1 ),
143#else
144 0,
145#endif
146 2048,
147};
Gilles Peskineffb92da2021-06-02 00:03:26 +0200148
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200149/*
150 * NSA Suite B Profile
151 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200152const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
153{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200154 /* Only SHA-256 and 384 */
155 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
156 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ),
157 /* Only ECDSA */
Ron Eldor85e1dcf2018-02-06 15:59:38 +0200158 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECDSA ) |
159 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECKEY ),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200160#if defined(MBEDTLS_ECP_C)
161 /* Only NIST P-256 and P-384 */
162 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
163 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ),
164#else
165 0,
166#endif
167 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200168};
169
170/*
Manuel Pégourié-Gonnard9d4c2c42021-06-18 09:48:27 +0200171 * Empty / all-forbidden profile
172 */
173const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_none =
174{
175 0,
176 0,
177 0,
178 (uint32_t) -1,
179};
180
181/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200182 * Check md_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200183 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200184 */
185static int x509_profile_check_md_alg( const mbedtls_x509_crt_profile *profile,
186 mbedtls_md_type_t md_alg )
187{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200188 if( md_alg == MBEDTLS_MD_NONE )
189 return( -1 );
190
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200191 if( ( profile->allowed_mds & MBEDTLS_X509_ID_FLAG( md_alg ) ) != 0 )
192 return( 0 );
193
194 return( -1 );
195}
196
197/*
198 * Check pk_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200199 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200200 */
201static int x509_profile_check_pk_alg( const mbedtls_x509_crt_profile *profile,
202 mbedtls_pk_type_t pk_alg )
203{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200204 if( pk_alg == MBEDTLS_PK_NONE )
205 return( -1 );
206
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200207 if( ( profile->allowed_pks & MBEDTLS_X509_ID_FLAG( pk_alg ) ) != 0 )
208 return( 0 );
209
210 return( -1 );
211}
212
213/*
214 * Check key against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200215 * Return 0 if pk is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200216 */
217static int x509_profile_check_key( const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200218 const mbedtls_pk_context *pk )
219{
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200220 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type( pk );
Manuel Pégourié-Gonnard19773ff2017-10-24 10:51:26 +0200221
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200222#if defined(MBEDTLS_RSA_C)
223 if( pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS )
224 {
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200225 if( mbedtls_pk_get_bitlen( pk ) >= profile->rsa_min_bitlen )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200226 return( 0 );
227
228 return( -1 );
229 }
230#endif
231
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200232#if defined(MBEDTLS_ECP_C)
233 if( pk_alg == MBEDTLS_PK_ECDSA ||
234 pk_alg == MBEDTLS_PK_ECKEY ||
235 pk_alg == MBEDTLS_PK_ECKEY_DH )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200236 {
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200237 const mbedtls_ecp_group_id gid = mbedtls_pk_ec( *pk )->grp.id;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200238
Philippe Antoineb5b25432018-05-11 11:06:29 +0200239 if( gid == MBEDTLS_ECP_DP_NONE )
240 return( -1 );
241
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200242 if( ( profile->allowed_curves & MBEDTLS_X509_ID_FLAG( gid ) ) != 0 )
243 return( 0 );
244
245 return( -1 );
246 }
247#endif
248
249 return( -1 );
250}
251
252/*
Hanno Becker1f8527f2018-11-02 09:19:16 +0000253 * Like memcmp, but case-insensitive and always returns -1 if different
254 */
255static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
256{
257 size_t i;
258 unsigned char diff;
259 const unsigned char *n1 = s1, *n2 = s2;
260
261 for( i = 0; i < len; i++ )
262 {
263 diff = n1[i] ^ n2[i];
264
265 if( diff == 0 )
266 continue;
267
268 if( diff == 32 &&
269 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
270 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
271 {
272 continue;
273 }
274
275 return( -1 );
276 }
277
278 return( 0 );
279}
280
281/*
282 * Return 0 if name matches wildcard, -1 otherwise
283 */
284static int x509_check_wildcard( const char *cn, const mbedtls_x509_buf *name )
285{
286 size_t i;
287 size_t cn_idx = 0, cn_len = strlen( cn );
288
289 /* We can't have a match if there is no wildcard to match */
290 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
291 return( -1 );
292
293 for( i = 0; i < cn_len; ++i )
294 {
295 if( cn[i] == '.' )
296 {
297 cn_idx = i;
298 break;
299 }
300 }
301
302 if( cn_idx == 0 )
303 return( -1 );
304
305 if( cn_len - cn_idx == name->len - 1 &&
306 x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
307 {
308 return( 0 );
309 }
310
311 return( -1 );
312}
313
314/*
315 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
316 * variations (but not all).
317 *
318 * Return 0 if equal, -1 otherwise.
319 */
320static int x509_string_cmp( const mbedtls_x509_buf *a, const mbedtls_x509_buf *b )
321{
322 if( a->tag == b->tag &&
323 a->len == b->len &&
324 memcmp( a->p, b->p, b->len ) == 0 )
325 {
326 return( 0 );
327 }
328
329 if( ( a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
330 ( b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
331 a->len == b->len &&
332 x509_memcasecmp( a->p, b->p, b->len ) == 0 )
333 {
334 return( 0 );
335 }
336
337 return( -1 );
338}
339
340/*
341 * Compare two X.509 Names (aka rdnSequence).
342 *
343 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
344 * we sometimes return unequal when the full algorithm would return equal,
345 * but never the other way. (In particular, we don't do Unicode normalisation
346 * or space folding.)
347 *
348 * Return 0 if equal, -1 otherwise.
349 */
350static int x509_name_cmp( const mbedtls_x509_name *a, const mbedtls_x509_name *b )
351{
352 /* Avoid recursion, it might not be optimised by the compiler */
353 while( a != NULL || b != NULL )
354 {
355 if( a == NULL || b == NULL )
356 return( -1 );
357
358 /* type */
359 if( a->oid.tag != b->oid.tag ||
360 a->oid.len != b->oid.len ||
361 memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 )
362 {
363 return( -1 );
364 }
365
366 /* value */
367 if( x509_string_cmp( &a->val, &b->val ) != 0 )
368 return( -1 );
369
370 /* structure of the list of sets */
371 if( a->next_merged != b->next_merged )
372 return( -1 );
373
374 a = a->next;
375 b = b->next;
376 }
377
378 /* a == NULL == b */
379 return( 0 );
380}
381
382/*
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200383 * Reset (init or clear) a verify_chain
384 */
385static void x509_crt_verify_chain_reset(
386 mbedtls_x509_crt_verify_chain *ver_chain )
387{
388 size_t i;
389
390 for( i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++ )
391 {
392 ver_chain->items[i].crt = NULL;
Hanno Beckera9375b32019-01-10 09:19:26 +0000393 ver_chain->items[i].flags = (uint32_t) -1;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200394 }
395
396 ver_chain->len = 0;
Hanno Beckerf53893b2019-03-28 13:45:55 +0000397
398#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
399 ver_chain->trust_ca_cb_result = NULL;
400#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200401}
402
403/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200404 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
405 */
406static int x509_get_version( unsigned char **p,
407 const unsigned char *end,
408 int *ver )
409{
Janos Follath865b3eb2019-12-16 11:46:15 +0000410 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200411 size_t len;
412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
414 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200415 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200417 {
418 *ver = 0;
419 return( 0 );
420 }
421
Chris Jones9f7a6932021-04-14 12:12:09 +0100422 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200423 }
424
425 end = *p + len;
426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100428 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_VERSION, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200429
430 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100431 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_VERSION,
432 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200433
434 return( 0 );
435}
436
437/*
438 * Validity ::= SEQUENCE {
439 * notBefore Time,
440 * notAfter Time }
441 */
442static int x509_get_dates( unsigned char **p,
443 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444 mbedtls_x509_time *from,
445 mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200446{
Janos Follath865b3eb2019-12-16 11:46:15 +0000447 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200448 size_t len;
449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
451 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100452 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200453
454 end = *p + len;
455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200457 return( ret );
458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200460 return( ret );
461
462 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100463 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE,
464 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200465
466 return( 0 );
467}
468
469/*
470 * X.509 v2/v3 unique identifier (not parsed)
471 */
472static int x509_get_uid( unsigned char **p,
473 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474 mbedtls_x509_buf *uid, int n )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200475{
Janos Follath865b3eb2019-12-16 11:46:15 +0000476 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200477
478 if( *p == end )
479 return( 0 );
480
481 uid->tag = **p;
482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 if( ( ret = mbedtls_asn1_get_tag( p, end, &uid->len,
484 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200485 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200487 return( 0 );
488
Chris Jones9f7a6932021-04-14 12:12:09 +0100489 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200490 }
491
492 uid->p = *p;
493 *p += uid->len;
494
495 return( 0 );
496}
497
498static int x509_get_basic_constraints( unsigned char **p,
499 const unsigned char *end,
500 int *ca_istrue,
501 int *max_pathlen )
502{
Janos Follath865b3eb2019-12-16 11:46:15 +0000503 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200504 size_t len;
505
506 /*
507 * BasicConstraints ::= SEQUENCE {
508 * cA BOOLEAN DEFAULT FALSE,
509 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
510 */
511 *ca_istrue = 0; /* DEFAULT FALSE */
512 *max_pathlen = 0; /* endless */
513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
515 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100516 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200517
518 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200519 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521 if( ( ret = mbedtls_asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200522 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
524 ret = mbedtls_asn1_get_int( p, end, ca_istrue );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200525
526 if( ret != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100527 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200528
529 if( *ca_istrue != 0 )
530 *ca_istrue = 1;
531 }
532
533 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200534 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100537 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200538
539 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100540 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
541 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200542
Andrzej Kurek16050742020-04-14 09:49:52 -0400543 /* Do not accept max_pathlen equal to INT_MAX to avoid a signed integer
544 * overflow, which is an undefined behavior. */
545 if( *max_pathlen == INT_MAX )
Chris Jones9f7a6932021-04-14 12:12:09 +0100546 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
547 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Andrzej Kurek16050742020-04-14 09:49:52 -0400548
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200549 (*max_pathlen)++;
550
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200551 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200552}
553
554static int x509_get_ns_cert_type( unsigned char **p,
555 const unsigned char *end,
556 unsigned char *ns_cert_type)
557{
Janos Follath865b3eb2019-12-16 11:46:15 +0000558 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100562 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200563
564 if( bs.len != 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100565 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
566 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200567
568 /* Get actual bitstring */
569 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200570 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200571}
572
573static int x509_get_key_usage( unsigned char **p,
574 const unsigned char *end,
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +0100575 unsigned int *key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200576{
Janos Follath865b3eb2019-12-16 11:46:15 +0000577 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200578 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100582 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200583
584 if( bs.len < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100585 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
586 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200587
588 /* Get actual bitstring */
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200589 *key_usage = 0;
590 for( i = 0; i < bs.len && i < sizeof( unsigned int ); i++ )
591 {
592 *key_usage |= (unsigned int) bs.p[i] << (8*i);
593 }
594
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200595 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200596}
597
598/*
599 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
600 *
601 * KeyPurposeId ::= OBJECT IDENTIFIER
602 */
603static int x509_get_ext_key_usage( unsigned char **p,
604 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200606{
Janos Follath865b3eb2019-12-16 11:46:15 +0000607 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 if( ( ret = mbedtls_asn1_get_sequence_of( p, end, ext_key_usage, MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100610 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200611
612 /* Sequence length must be >= 1 */
613 if( ext_key_usage->buf.p == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100614 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
615 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200616
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200617 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200618}
619
620/*
621 * SubjectAltName ::= GeneralNames
622 *
623 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
624 *
625 * GeneralName ::= CHOICE {
626 * otherName [0] OtherName,
627 * rfc822Name [1] IA5String,
628 * dNSName [2] IA5String,
629 * x400Address [3] ORAddress,
630 * directoryName [4] Name,
631 * ediPartyName [5] EDIPartyName,
632 * uniformResourceIdentifier [6] IA5String,
633 * iPAddress [7] OCTET STRING,
634 * registeredID [8] OBJECT IDENTIFIER }
635 *
636 * OtherName ::= SEQUENCE {
637 * type-id OBJECT IDENTIFIER,
638 * value [0] EXPLICIT ANY DEFINED BY type-id }
639 *
640 * EDIPartyName ::= SEQUENCE {
641 * nameAssigner [0] DirectoryString OPTIONAL,
642 * partyName [1] DirectoryString }
643 *
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300644 * NOTE: we list all types, but only use dNSName and otherName
645 * of type HwModuleName, as defined in RFC 4108, at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200646 */
647static int x509_get_subject_alt_name( unsigned char **p,
648 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 mbedtls_x509_sequence *subject_alt_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200650{
Janos Follath865b3eb2019-12-16 11:46:15 +0000651 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200652 size_t len, tag_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 mbedtls_asn1_buf *buf;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200654 unsigned char tag;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 mbedtls_asn1_sequence *cur = subject_alt_name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200656
657 /* Get main sequence tag */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
659 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100660 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200661
662 if( *p + len != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100663 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
664 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200665
666 while( *p < end )
667 {
Ron Eldordbbd9662019-05-15 15:46:03 +0300668 mbedtls_x509_subject_alternative_name dummy_san_buf;
669 memset( &dummy_san_buf, 0, sizeof( dummy_san_buf ) );
670
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200671 tag = **p;
672 (*p)++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 if( ( ret = mbedtls_asn1_get_len( p, end, &tag_len ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100674 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200675
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100676 if( ( tag & MBEDTLS_ASN1_TAG_CLASS_MASK ) !=
677 MBEDTLS_ASN1_CONTEXT_SPECIFIC )
678 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100679 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
680 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100681 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200682
Ron Eldordbbd9662019-05-15 15:46:03 +0300683 /*
irwir74aee1c2019-09-21 18:21:48 +0300684 * Check that the SAN is structured correctly.
Ron Eldordbbd9662019-05-15 15:46:03 +0300685 */
686 ret = mbedtls_x509_parse_subject_alt_name( &(cur->buf), &dummy_san_buf );
687 /*
688 * In case the extension is malformed, return an error,
689 * and clear the allocated sequences.
690 */
691 if( ret != 0 && ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
692 {
693 mbedtls_x509_sequence *seq_cur = subject_alt_name->next;
694 mbedtls_x509_sequence *seq_prv;
695 while( seq_cur != NULL )
696 {
697 seq_prv = seq_cur;
698 seq_cur = seq_cur->next;
699 mbedtls_platform_zeroize( seq_prv,
700 sizeof( mbedtls_x509_sequence ) );
701 mbedtls_free( seq_prv );
702 }
Ron Eldor5aebeeb2019-05-22 16:41:21 +0300703 subject_alt_name->next = NULL;
Ron Eldordbbd9662019-05-15 15:46:03 +0300704 return( ret );
705 }
706
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200707 /* Allocate and assign next pointer */
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200708 if( cur->buf.p != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200709 {
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100710 if( cur->next != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100712
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200713 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200714
715 if( cur->next == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100716 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
717 MBEDTLS_ERR_ASN1_ALLOC_FAILED ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200718
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200719 cur = cur->next;
720 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200721
722 buf = &(cur->buf);
723 buf->tag = tag;
724 buf->p = *p;
725 buf->len = tag_len;
726 *p += buf->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200727 }
728
729 /* Set final sequence entry's next pointer to NULL */
730 cur->next = NULL;
731
732 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100733 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
734 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200735
736 return( 0 );
737}
738
739/*
Ron Eldor74d9acc2019-03-21 14:00:03 +0200740 * id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 }
741 *
742 * anyPolicy OBJECT IDENTIFIER ::= { id-ce-certificatePolicies 0 }
743 *
744 * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
745 *
746 * PolicyInformation ::= SEQUENCE {
747 * policyIdentifier CertPolicyId,
748 * policyQualifiers SEQUENCE SIZE (1..MAX) OF
749 * PolicyQualifierInfo OPTIONAL }
750 *
751 * CertPolicyId ::= OBJECT IDENTIFIER
752 *
753 * PolicyQualifierInfo ::= SEQUENCE {
754 * policyQualifierId PolicyQualifierId,
755 * qualifier ANY DEFINED BY policyQualifierId }
756 *
757 * -- policyQualifierIds for Internet policy qualifiers
758 *
759 * id-qt OBJECT IDENTIFIER ::= { id-pkix 2 }
760 * id-qt-cps OBJECT IDENTIFIER ::= { id-qt 1 }
761 * id-qt-unotice OBJECT IDENTIFIER ::= { id-qt 2 }
762 *
763 * PolicyQualifierId ::= OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice )
764 *
765 * Qualifier ::= CHOICE {
766 * cPSuri CPSuri,
767 * userNotice UserNotice }
768 *
769 * CPSuri ::= IA5String
770 *
771 * UserNotice ::= SEQUENCE {
772 * noticeRef NoticeReference OPTIONAL,
773 * explicitText DisplayText OPTIONAL }
774 *
775 * NoticeReference ::= SEQUENCE {
776 * organization DisplayText,
777 * noticeNumbers SEQUENCE OF INTEGER }
778 *
779 * DisplayText ::= CHOICE {
780 * ia5String IA5String (SIZE (1..200)),
781 * visibleString VisibleString (SIZE (1..200)),
782 * bmpString BMPString (SIZE (1..200)),
783 * utf8String UTF8String (SIZE (1..200)) }
784 *
785 * NOTE: we only parse and use anyPolicy without qualifiers at this point
786 * as defined in RFC 5280.
787 */
788static int x509_get_certificate_policies( unsigned char **p,
789 const unsigned char *end,
790 mbedtls_x509_sequence *certificate_policies )
791{
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300792 int ret, parse_ret = 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200793 size_t len;
794 mbedtls_asn1_buf *buf;
795 mbedtls_asn1_sequence *cur = certificate_policies;
796
797 /* Get main sequence tag */
798 ret = mbedtls_asn1_get_tag( p, end, &len,
799 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
800 if( ret != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100801 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200802
803 if( *p + len != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100804 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
805 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200806
807 /*
808 * Cannot be an empty sequence.
809 */
810 if( len == 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100811 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
812 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200813
814 while( *p < end )
815 {
816 mbedtls_x509_buf policy_oid;
817 const unsigned char *policy_end;
818
819 /*
820 * Get the policy sequence
821 */
822 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
823 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100824 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200825
826 policy_end = *p + len;
827
Ron Eldor08063792019-05-13 16:38:39 +0300828 if( ( ret = mbedtls_asn1_get_tag( p, policy_end, &len,
Ron Eldor74d9acc2019-03-21 14:00:03 +0200829 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100830 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200831
832 policy_oid.tag = MBEDTLS_ASN1_OID;
833 policy_oid.len = len;
834 policy_oid.p = *p;
835
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300836 /*
837 * Only AnyPolicy is currently supported when enforcing policy.
838 */
839 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_POLICY, &policy_oid ) != 0 )
840 {
841 /*
842 * Set the parsing return code but continue parsing, in case this
TRodziewicz3ecb92e2021-05-11 18:22:05 +0200843 * extension is critical.
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300844 */
845 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
846 }
847
Ron Eldor74d9acc2019-03-21 14:00:03 +0200848 /* Allocate and assign next pointer */
849 if( cur->buf.p != NULL )
850 {
851 if( cur->next != NULL )
852 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
853
854 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
855
856 if( cur->next == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100857 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
858 MBEDTLS_ERR_ASN1_ALLOC_FAILED ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200859
860 cur = cur->next;
861 }
862
863 buf = &( cur->buf );
864 buf->tag = policy_oid.tag;
865 buf->p = policy_oid.p;
866 buf->len = policy_oid.len;
Ron Eldor08063792019-05-13 16:38:39 +0300867
868 *p += len;
869
870 /*
871 * If there is an optional qualifier, then *p < policy_end
872 * Check the Qualifier len to verify it doesn't exceed policy_end.
873 */
874 if( *p < policy_end )
875 {
876 if( ( ret = mbedtls_asn1_get_tag( p, policy_end, &len,
877 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100878 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor08063792019-05-13 16:38:39 +0300879 /*
880 * Skip the optional policy qualifiers.
881 */
882 *p += len;
883 }
884
885 if( *p != policy_end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100886 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
887 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200888 }
889
890 /* Set final sequence entry's next pointer to NULL */
891 cur->next = NULL;
892
893 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100894 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
895 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200896
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300897 return( parse_ret );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200898}
899
900/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200901 * X.509 v3 extensions
902 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200903 */
904static int x509_get_crt_ext( unsigned char **p,
905 const unsigned char *end,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200906 mbedtls_x509_crt *crt,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200907 mbedtls_x509_crt_ext_cb_t cb,
908 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200909{
Janos Follath865b3eb2019-12-16 11:46:15 +0000910 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200911 size_t len;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200912 unsigned char *end_ext_data, *start_ext_octet, *end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200913
Hanno Becker12f62fb2019-02-12 17:22:36 +0000914 if( *p == end )
915 return( 0 );
916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917 if( ( ret = mbedtls_x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200918 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200919
Hanno Becker12f62fb2019-02-12 17:22:36 +0000920 end = crt->v3_ext.p + crt->v3_ext.len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200921 while( *p < end )
922 {
923 /*
924 * Extension ::= SEQUENCE {
925 * extnID OBJECT IDENTIFIER,
926 * critical BOOLEAN DEFAULT FALSE,
927 * extnValue OCTET STRING }
928 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929 mbedtls_x509_buf extn_oid = {0, 0, NULL};
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200930 int is_critical = 0; /* DEFAULT FALSE */
931 int ext_type = 0;
932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
934 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100935 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200936
937 end_ext_data = *p + len;
938
939 /* Get extension ID */
k-stachowiakdcae78a2018-06-28 16:32:54 +0200940 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &extn_oid.len,
k-stachowiak463928a2018-07-24 12:50:59 +0200941 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100942 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200943
k-stachowiak463928a2018-07-24 12:50:59 +0200944 extn_oid.tag = MBEDTLS_ASN1_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200945 extn_oid.p = *p;
946 *p += extn_oid.len;
947
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200948 /* Get optional critical */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949 if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
950 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
Chris Jones9f7a6932021-04-14 12:12:09 +0100951 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200952
953 /* Data should be octet string type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
955 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100956 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200957
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200958 start_ext_octet = *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200959 end_ext_octet = *p + len;
960
961 if( end_ext_octet != end_ext_data )
Chris Jones9f7a6932021-04-14 12:12:09 +0100962 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
963 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200964
965 /*
966 * Detect supported extensions
967 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200968 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200969
970 if( ret != 0 )
971 {
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200972 /* Give the callback (if any) a chance to handle the extension */
Nicola Di Lieto2c3a9172020-05-28 17:20:42 +0200973 if( cb != NULL )
974 {
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200975 ret = cb( p_ctx, crt, &extn_oid, is_critical, *p, end_ext_octet );
Nicola Di Lieto565b52b2020-05-29 22:46:56 +0200976 if( ret != 0 && is_critical )
977 return( ret );
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200978 *p = end_ext_octet;
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200979 continue;
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200980 }
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200981
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200982 /* No parser found, skip extension */
983 *p = end_ext_octet;
984
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200985 if( is_critical )
986 {
987 /* Data is marked as critical: fail */
Chris Jones9f7a6932021-04-14 12:12:09 +0100988 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
989 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200990 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200991 continue;
992 }
993
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100994 /* Forbid repeated extensions */
995 if( ( crt->ext_types & ext_type ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100997
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200998 crt->ext_types |= ext_type;
999
1000 switch( ext_type )
1001 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001002 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001003 /* Parse basic constraints */
1004 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
1005 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001006 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001007 break;
1008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001010 /* Parse key usage */
1011 if( ( ret = x509_get_key_usage( p, end_ext_octet,
1012 &crt->key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001013 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001014 break;
1015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001017 /* Parse extended key usage */
1018 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
1019 &crt->ext_key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001020 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001021 break;
1022
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001023 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001024 /* Parse subject alt name */
1025 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
1026 &crt->subject_alt_names ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001027 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001028 break;
1029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001031 /* Parse netscape certificate type */
1032 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
1033 &crt->ns_cert_type ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001034 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001035 break;
1036
Ron Eldor74d9acc2019-03-21 14:00:03 +02001037 case MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES:
1038 /* Parse certificate policies type */
1039 if( ( ret = x509_get_certificate_policies( p, end_ext_octet,
1040 &crt->certificate_policies ) ) != 0 )
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001041 {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +02001042 /* Give the callback (if any) a chance to handle the extension
1043 * if it contains unsupported policies */
1044 if( ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE && cb != NULL &&
1045 cb( p_ctx, crt, &extn_oid, is_critical,
1046 start_ext_octet, end_ext_octet ) == 0 )
1047 break;
1048
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001049 if( is_critical )
1050 return( ret );
1051 else
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001052 /*
Ron Eldora2913912019-05-16 16:17:38 +03001053 * If MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE is returned, then we
1054 * cannot interpret or enforce the policy. However, it is up to
1055 * the user to choose how to enforce the policies,
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001056 * unless the extension is critical.
1057 */
1058 if( ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1059 return( ret );
1060 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001061 break;
1062
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001063 default:
Ron Eldordf48efa2019-04-08 13:28:24 +03001064 /*
1065 * If this is a non-critical extension, which the oid layer
1066 * supports, but there isn't an x509 parser for it,
1067 * skip the extension.
1068 */
Ron Eldordf48efa2019-04-08 13:28:24 +03001069 if( is_critical )
1070 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1071 else
Ron Eldordf48efa2019-04-08 13:28:24 +03001072 *p = end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001073 }
1074 }
1075
1076 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +01001077 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1078 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001079
1080 return( 0 );
1081}
1082
1083/*
1084 * Parse and fill a single X.509 certificate in DER format
1085 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001086static int x509_crt_parse_der_core( mbedtls_x509_crt *crt,
1087 const unsigned char *buf,
1088 size_t buflen,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001089 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001090 mbedtls_x509_crt_ext_cb_t cb,
1091 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001092{
Janos Follath865b3eb2019-12-16 11:46:15 +00001093 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001094 size_t len;
1095 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098 memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) );
1099 memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) );
1100 memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001101
1102 /*
1103 * Check for valid input
1104 */
1105 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001106 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001107
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001108 /* Use the original buffer until we figure out actual length. */
Janos Follathcc0e49d2016-02-17 14:34:12 +00001109 p = (unsigned char*) buf;
1110 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001111 end = p + len;
1112
1113 /*
1114 * Certificate ::= SEQUENCE {
1115 * tbsCertificate TBSCertificate,
1116 * signatureAlgorithm AlgorithmIdentifier,
1117 * signatureValue BIT STRING }
1118 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001119 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1120 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001121 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122 mbedtls_x509_crt_free( crt );
1123 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001124 }
1125
Janos Follathcc0e49d2016-02-17 14:34:12 +00001126 end = crt_end = p + len;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001127 crt->raw.len = crt_end - buf;
1128 if( make_copy != 0 )
1129 {
1130 /* Create and populate a new buffer for the raw field. */
1131 crt->raw.p = p = mbedtls_calloc( 1, crt->raw.len );
1132 if( crt->raw.p == NULL )
1133 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
1134
1135 memcpy( crt->raw.p, buf, crt->raw.len );
1136 crt->own_buffer = 1;
1137
1138 p += crt->raw.len - len;
1139 end = crt_end = p + len;
1140 }
1141 else
1142 {
1143 crt->raw.p = (unsigned char*) buf;
1144 crt->own_buffer = 0;
1145 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001146
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001147 /*
1148 * TBSCertificate ::= SEQUENCE {
1149 */
1150 crt->tbs.p = p;
1151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1153 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001156 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001157 }
1158
1159 end = p + len;
1160 crt->tbs.len = end - crt->tbs.p;
1161
1162 /*
1163 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1164 *
1165 * CertificateSerialNumber ::= INTEGER
1166 *
1167 * signature AlgorithmIdentifier
1168 */
1169 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001170 ( ret = mbedtls_x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1171 ( ret = mbedtls_x509_get_alg( &p, end, &crt->sig_oid,
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001172 &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001173 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001174 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001175 return( ret );
1176 }
1177
Andres AG7ca4a032017-03-09 16:16:11 +00001178 if( crt->version < 0 || crt->version > 2 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001179 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001180 mbedtls_x509_crt_free( crt );
1181 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001182 }
1183
Andres AG7ca4a032017-03-09 16:16:11 +00001184 crt->version++;
1185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001186 if( ( ret = mbedtls_x509_get_sig_alg( &crt->sig_oid, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02001187 &crt->sig_md, &crt->sig_pk,
1188 &crt->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001189 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001191 return( ret );
1192 }
1193
1194 /*
1195 * issuer Name
1196 */
1197 crt->issuer_raw.p = p;
1198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001199 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1200 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001201 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001202 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001203 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001204 }
1205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001206 if( ( ret = mbedtls_x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001207 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001209 return( ret );
1210 }
1211
1212 crt->issuer_raw.len = p - crt->issuer_raw.p;
1213
1214 /*
1215 * Validity ::= SEQUENCE {
1216 * notBefore Time,
1217 * notAfter Time }
1218 *
1219 */
1220 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1221 &crt->valid_to ) ) != 0 )
1222 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001223 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001224 return( ret );
1225 }
1226
1227 /*
1228 * subject Name
1229 */
1230 crt->subject_raw.p = p;
1231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001232 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1233 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001234 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001235 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001236 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001237 }
1238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001239 if( len && ( ret = mbedtls_x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001240 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001241 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001242 return( ret );
1243 }
1244
1245 crt->subject_raw.len = p - crt->subject_raw.p;
1246
1247 /*
1248 * SubjectPublicKeyInfo
1249 */
Hanno Becker494dd7a2019-02-06 16:13:41 +00001250 crt->pk_raw.p = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001251 if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001252 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001253 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001254 return( ret );
1255 }
Hanno Becker494dd7a2019-02-06 16:13:41 +00001256 crt->pk_raw.len = p - crt->pk_raw.p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001257
1258 /*
1259 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1260 * -- If present, version shall be v2 or v3
1261 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1262 * -- If present, version shall be v2 or v3
1263 * extensions [3] EXPLICIT Extensions OPTIONAL
1264 * -- If present, version shall be v3
1265 */
1266 if( crt->version == 2 || crt->version == 3 )
1267 {
1268 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1269 if( ret != 0 )
1270 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001271 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001272 return( ret );
1273 }
1274 }
1275
1276 if( crt->version == 2 || crt->version == 3 )
1277 {
1278 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1279 if( ret != 0 )
1280 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001281 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001282 return( ret );
1283 }
1284 }
1285
1286 if( crt->version == 3 )
Manuel Pégourié-Gonnarda252af72015-03-27 16:15:55 +01001287 {
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001288 ret = x509_get_crt_ext( &p, end, crt, cb, p_ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001289 if( ret != 0 )
1290 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001292 return( ret );
1293 }
1294 }
1295
1296 if( p != end )
1297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001299 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT,
1300 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001301 }
1302
1303 end = crt_end;
1304
1305 /*
1306 * }
1307 * -- end of TBSCertificate
1308 *
1309 * signatureAlgorithm AlgorithmIdentifier,
1310 * signatureValue BIT STRING
1311 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312 if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001315 return( ret );
1316 }
1317
Manuel Pégourié-Gonnard1022fed2015-03-27 16:30:47 +01001318 if( crt->sig_oid.len != sig_oid2.len ||
1319 memcmp( crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len ) != 0 ||
Paul Elliottca17ebf2020-11-24 17:30:18 +00001320 sig_params1.tag != sig_params2.tag ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001321 sig_params1.len != sig_params2.len ||
Manuel Pégourié-Gonnard159c5242015-04-30 11:15:22 +02001322 ( sig_params1.len != 0 &&
1323 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001324 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325 mbedtls_x509_crt_free( crt );
1326 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001327 }
1328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329 if( ( ret = mbedtls_x509_get_sig( &p, end, &crt->sig ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001330 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001332 return( ret );
1333 }
1334
1335 if( p != end )
1336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001337 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001338 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT,
1339 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001340 }
1341
1342 return( 0 );
1343}
1344
1345/*
1346 * Parse one X.509 certificate in DER format from a buffer and add them to a
1347 * chained list
1348 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001349static int mbedtls_x509_crt_parse_der_internal( mbedtls_x509_crt *chain,
1350 const unsigned char *buf,
1351 size_t buflen,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001352 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001353 mbedtls_x509_crt_ext_cb_t cb,
1354 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001355{
Janos Follath865b3eb2019-12-16 11:46:15 +00001356 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001357 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001358
1359 /*
1360 * Check for valid input
1361 */
1362 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001363 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001364
1365 while( crt->version != 0 && crt->next != NULL )
1366 {
1367 prev = crt;
1368 crt = crt->next;
1369 }
1370
1371 /*
1372 * Add new certificate on the end of the chain if needed.
1373 */
Paul Bakker66d5d072014-06-17 16:39:18 +02001374 if( crt->version != 0 && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001375 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001376 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001377
1378 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001379 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001380
1381 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001382 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001383 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001384 }
1385
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001386 ret = x509_crt_parse_der_core( crt, buf, buflen, make_copy, cb, p_ctx );
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001387 if( ret != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001388 {
1389 if( prev )
1390 prev->next = NULL;
1391
1392 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001393 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001394
1395 return( ret );
1396 }
1397
1398 return( 0 );
1399}
1400
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001401int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
1402 const unsigned char *buf,
1403 size_t buflen )
1404{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001405 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 0, NULL, NULL ) );
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001406}
1407
Nicola Di Lietofde98f72020-05-28 08:34:33 +02001408int mbedtls_x509_crt_parse_der_with_ext_cb( mbedtls_x509_crt *chain,
1409 const unsigned char *buf,
1410 size_t buflen,
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +02001411 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001412 mbedtls_x509_crt_ext_cb_t cb,
1413 void *p_ctx )
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001414{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001415 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, make_copy, cb, p_ctx ) );
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001416}
1417
1418int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
1419 const unsigned char *buf,
1420 size_t buflen )
1421{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001422 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 1, NULL, NULL ) );
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001423}
1424
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001425/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001426 * Parse one or more PEM certificates from a buffer and add them to the chained
1427 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001428 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001429int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain,
1430 const unsigned char *buf,
1431 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001432{
Janos Follath98e28a72016-05-31 14:03:54 +01001433#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001434 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001436#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001437
1438 /*
1439 * Check for valid input
1440 */
1441 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001442 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001443
1444 /*
1445 * Determine buffer content. Buffer contains either one DER certificate or
1446 * one or more PEM certificates.
1447 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001449 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001450 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1451 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001452 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001453 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001455 if( buf_format == MBEDTLS_X509_FORMAT_DER )
1456 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Janos Follath98e28a72016-05-31 14:03:54 +01001457#else
1458 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
1459#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001461#if defined(MBEDTLS_PEM_PARSE_C)
1462 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001463 {
Janos Follath865b3eb2019-12-16 11:46:15 +00001464 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001465 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001466
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001467 /* 1 rather than 0 since the terminating NULL byte is counted in */
1468 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001469 {
1470 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001472
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001473 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001475 "-----BEGIN CERTIFICATE-----",
1476 "-----END CERTIFICATE-----",
1477 buf, NULL, 0, &use_len );
1478
1479 if( ret == 0 )
1480 {
1481 /*
1482 * Was PEM encoded
1483 */
1484 buflen -= use_len;
1485 buf += use_len;
1486 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001487 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001488 {
1489 return( ret );
1490 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001491 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001492 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001493 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001494
1495 /*
1496 * PEM header and footer were found
1497 */
1498 buflen -= use_len;
1499 buf += use_len;
1500
1501 if( first_error == 0 )
1502 first_error = ret;
1503
Paul Bakker5a5fa922014-09-26 14:53:04 +02001504 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001505 continue;
1506 }
1507 else
1508 break;
1509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001510 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001512 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001513
1514 if( ret != 0 )
1515 {
1516 /*
1517 * Quit parsing on a memory error
1518 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001519 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001520 return( ret );
1521
1522 if( first_error == 0 )
1523 first_error = ret;
1524
1525 total_failed++;
1526 continue;
1527 }
1528
1529 success = 1;
1530 }
1531 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001532
1533 if( success )
1534 return( total_failed );
1535 else if( first_error )
1536 return( first_error );
1537 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001538 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Janos Follath98e28a72016-05-31 14:03:54 +01001539#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001540}
1541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001542#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001543/*
1544 * Load one or more certificates and add them to the chained list
1545 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001546int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001547{
Janos Follath865b3eb2019-12-16 11:46:15 +00001548 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001549 size_t n;
1550 unsigned char *buf;
1551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001552 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001553 return( ret );
1554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001555 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001556
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001557 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001558 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001559
1560 return( ret );
1561}
1562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001564{
1565 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001566#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001567 int w_ret;
1568 WCHAR szDir[MAX_PATH];
1569 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001570 char *p;
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001571 size_t len = strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001572
Paul Bakker9af723c2014-05-01 13:03:14 +02001573 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001574 HANDLE hFind;
1575
1576 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001577 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001578
Paul Bakker9af723c2014-05-01 13:03:14 +02001579 memset( szDir, 0, sizeof(szDir) );
1580 memset( filename, 0, MAX_PATH );
1581 memcpy( filename, path, len );
1582 filename[len++] = '\\';
1583 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001584 filename[len++] = '*';
1585
Simon B3c6b18d2016-11-03 01:11:37 +00001586 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001587 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001588 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001590
1591 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001592 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001594
1595 len = MAX_PATH - len;
1596 do
1597 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001598 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001599
1600 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1601 continue;
1602
Paul Bakker9af723c2014-05-01 13:03:14 +02001603 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001604 lstrlenW( file_data.cFileName ),
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001605 p, (int) len - 1,
Paul Bakker9af723c2014-05-01 13:03:14 +02001606 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001607 if( w_ret == 0 )
Ron Eldor36d90422017-01-09 15:09:16 +02001608 {
1609 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1610 goto cleanup;
1611 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001613 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001614 if( w_ret < 0 )
1615 ret++;
1616 else
1617 ret += w_ret;
1618 }
1619 while( FindNextFileW( hFind, &file_data ) != 0 );
1620
Paul Bakker66d5d072014-06-17 16:39:18 +02001621 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001622 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001623
Ron Eldor36d90422017-01-09 15:09:16 +02001624cleanup:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001625 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001626#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001627 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001628 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001629 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001630 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001631 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001632 DIR *dir = opendir( path );
1633
Paul Bakker66d5d072014-06-17 16:39:18 +02001634 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001635 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001636
Ron Eldor63140682017-01-09 19:27:59 +02001637#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001638 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001639 {
1640 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001641 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001642 }
Ron Eldor63140682017-01-09 19:27:59 +02001643#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001644
Paul Elliottfb91a482021-03-05 14:17:51 +00001645 memset( &sb, 0, sizeof( sb ) );
1646
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001647 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001648 {
Andres AGf9113192016-09-02 14:06:04 +01001649 snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name,
1650 "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001651
Andres AGf9113192016-09-02 14:06:04 +01001652 if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001653 {
Andres AGf9113192016-09-02 14:06:04 +01001654 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1655 goto cleanup;
1656 }
1657 else if( stat( entry_name, &sb ) == -1 )
1658 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001659 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001660 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001661 }
1662
1663 if( !S_ISREG( sb.st_mode ) )
1664 continue;
1665
1666 // Ignore parse errors
1667 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001668 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001669 if( t_ret < 0 )
1670 ret++;
1671 else
1672 ret += t_ret;
1673 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001674
1675cleanup:
Andres AGf9113192016-09-02 14:06:04 +01001676 closedir( dir );
1677
Ron Eldor63140682017-01-09 19:27:59 +02001678#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001679 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001680 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Ron Eldor63140682017-01-09 19:27:59 +02001681#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001682
Paul Bakkerbe089b02013-10-14 15:51:50 +02001683#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001684
1685 return( ret );
1686}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001687#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001688
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001689/*
1690 * OtherName ::= SEQUENCE {
1691 * type-id OBJECT IDENTIFIER,
1692 * value [0] EXPLICIT ANY DEFINED BY type-id }
1693 *
1694 * HardwareModuleName ::= SEQUENCE {
1695 * hwType OBJECT IDENTIFIER,
1696 * hwSerialNum OCTET STRING }
1697 *
1698 * NOTE: we currently only parse and use otherName of type HwModuleName,
1699 * as defined in RFC 4108.
1700 */
1701static int x509_get_other_name( const mbedtls_x509_buf *subject_alt_name,
1702 mbedtls_x509_san_other_name *other_name )
1703{
Janos Follathab23cd12019-05-09 13:53:57 +01001704 int ret = 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001705 size_t len;
1706 unsigned char *p = subject_alt_name->p;
1707 const unsigned char *end = p + subject_alt_name->len;
1708 mbedtls_x509_buf cur_oid;
1709
1710 if( ( subject_alt_name->tag &
1711 ( MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK ) ) !=
1712 ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ) )
1713 {
1714 /*
1715 * The given subject alternative name is not of type "othername".
1716 */
1717 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
1718 }
1719
1720 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1721 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001722 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001723
1724 cur_oid.tag = MBEDTLS_ASN1_OID;
1725 cur_oid.p = p;
1726 cur_oid.len = len;
1727
1728 /*
1729 * Only HwModuleName is currently supported.
1730 */
1731 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid ) != 0 )
1732 {
1733 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1734 }
1735
Ron Eldor890819a2019-05-13 19:03:04 +03001736 if( p + len >= end )
1737 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001738 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001739 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1740 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001741 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001742 p += len;
1743 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1744 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001745 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001746
1747 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1748 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001749 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001750
1751 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001752 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001753
1754 other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID;
1755 other_name->value.hardware_module_name.oid.p = p;
1756 other_name->value.hardware_module_name.oid.len = len;
1757
Ron Eldor890819a2019-05-13 19:03:04 +03001758 if( p + len >= end )
1759 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001760 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001761 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1762 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001763 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001764 p += len;
1765 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1766 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001767 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001768
1769 other_name->value.hardware_module_name.val.tag = MBEDTLS_ASN1_OCTET_STRING;
1770 other_name->value.hardware_module_name.val.p = p;
1771 other_name->value.hardware_module_name.val.len = len;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001772 p += len;
1773 if( p != end )
1774 {
Ron Eldor890819a2019-05-13 19:03:04 +03001775 mbedtls_platform_zeroize( other_name,
Sébastien Duquette661d7252019-06-23 17:45:26 -04001776 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 Eldorb2dc3fa2019-03-21 13:40:13 +02001779 }
1780 return( 0 );
1781}
1782
Peter Kolbus9a969b62018-12-11 13:55:56 -06001783int mbedtls_x509_parse_subject_alt_name( const mbedtls_x509_buf *san_buf,
1784 mbedtls_x509_subject_alternative_name *san )
1785{
1786 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1787 switch( san_buf->tag &
1788 ( MBEDTLS_ASN1_TAG_CLASS_MASK |
1789 MBEDTLS_ASN1_TAG_VALUE_MASK ) )
1790 {
1791 /*
1792 * otherName
1793 */
1794 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ):
1795 {
1796 mbedtls_x509_san_other_name other_name;
1797
1798 ret = x509_get_other_name( san_buf, &other_name );
1799 if( ret != 0 )
1800 return( ret );
1801
1802 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1803 san->type = MBEDTLS_X509_SAN_OTHER_NAME;
1804 memcpy( &san->san.other_name,
1805 &other_name, sizeof( other_name ) );
1806
1807 }
1808 break;
1809
1810 /*
1811 * dNSName
1812 */
1813 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME ):
1814 {
1815 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1816 san->type = MBEDTLS_X509_SAN_DNS_NAME;
1817
1818 memcpy( &san->san.unstructured_name,
1819 san_buf, sizeof( *san_buf ) );
1820
1821 }
1822 break;
1823
1824 /*
1825 * Type not supported
1826 */
1827 default:
1828 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1829 }
1830 return( 0 );
1831}
1832
1833#if !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001834static int x509_info_subject_alt_name( char **buf, size_t *size,
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001835 const mbedtls_x509_sequence
1836 *subject_alt_name,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001837 const char *prefix )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001838{
Janos Follath865b3eb2019-12-16 11:46:15 +00001839 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001840 size_t n = *size;
1841 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001842 const mbedtls_x509_sequence *cur = subject_alt_name;
Ron Eldor890819a2019-05-13 19:03:04 +03001843 mbedtls_x509_subject_alternative_name san;
1844 int parse_ret;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001845
1846 while( cur != NULL )
1847 {
Ron Eldor890819a2019-05-13 19:03:04 +03001848 memset( &san, 0, sizeof( san ) );
1849 parse_ret = mbedtls_x509_parse_subject_alt_name( &cur->buf, &san );
1850 if( parse_ret != 0 )
1851 {
1852 if( parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1853 {
1854 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
1855 MBEDTLS_X509_SAFE_SNPRINTF;
1856 }
1857 else
1858 {
1859 ret = mbedtls_snprintf( p, n, "\n%s <malformed>", prefix );
1860 MBEDTLS_X509_SAFE_SNPRINTF;
1861 }
1862 cur = cur->next;
1863 continue;
1864 }
1865
1866 switch( san.type )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001867 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001868 /*
1869 * otherName
1870 */
Ron Eldora2913912019-05-16 16:17:38 +03001871 case MBEDTLS_X509_SAN_OTHER_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001872 {
1873 mbedtls_x509_san_other_name *other_name = &san.san.other_name;
1874
1875 ret = mbedtls_snprintf( p, n, "\n%s otherName :", prefix );
1876 MBEDTLS_X509_SAFE_SNPRINTF;
1877
1878 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME,
1879 &other_name->value.hardware_module_name.oid ) != 0 )
Janos Follath22f605f2019-05-10 10:37:17 +01001880 {
Ron Eldor890819a2019-05-13 19:03:04 +03001881 ret = mbedtls_snprintf( p, n, "\n%s hardware module name :", prefix );
1882 MBEDTLS_X509_SAFE_SNPRINTF;
1883 ret = mbedtls_snprintf( p, n, "\n%s hardware type : ", prefix );
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001884 MBEDTLS_X509_SAFE_SNPRINTF;
1885
Ron Eldor890819a2019-05-13 19:03:04 +03001886 ret = mbedtls_oid_get_numeric_string( p, n, &other_name->value.hardware_module_name.oid );
1887 MBEDTLS_X509_SAFE_SNPRINTF;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001888
Ron Eldor890819a2019-05-13 19:03:04 +03001889 ret = mbedtls_snprintf( p, n, "\n%s hardware serial number : ", prefix );
1890 MBEDTLS_X509_SAFE_SNPRINTF;
1891
1892 if( other_name->value.hardware_module_name.val.len >= n )
1893 {
1894 *p = '\0';
1895 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Janos Follath22f605f2019-05-10 10:37:17 +01001896 }
1897
Ron Eldora2913912019-05-16 16:17:38 +03001898 memcpy( p, other_name->value.hardware_module_name.val.p,
1899 other_name->value.hardware_module_name.val.len );
1900 p += other_name->value.hardware_module_name.val.len;
1901
Ron Eldor890819a2019-05-13 19:03:04 +03001902 n -= other_name->value.hardware_module_name.val.len;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001903
Ron Eldor890819a2019-05-13 19:03:04 +03001904 }/* MBEDTLS_OID_ON_HW_MODULE_NAME */
1905 }
1906 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001907
1908 /*
1909 * dNSName
1910 */
Ron Eldora2913912019-05-16 16:17:38 +03001911 case MBEDTLS_X509_SAN_DNS_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001912 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001913 ret = mbedtls_snprintf( p, n, "\n%s dNSName : ", prefix );
1914 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldor890819a2019-05-13 19:03:04 +03001915 if( san.san.unstructured_name.len >= n )
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001916 {
1917 *p = '\0';
1918 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
1919 }
Ron Eldora2913912019-05-16 16:17:38 +03001920
1921 memcpy( p, san.san.unstructured_name.p, san.san.unstructured_name.len );
1922 p += san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001923 n -= san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001924 }
1925 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001926
1927 /*
Ron Eldor890819a2019-05-13 19:03:04 +03001928 * Type not supported, skip item.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001929 */
1930 default:
Janos Follath22f605f2019-05-10 10:37:17 +01001931 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
1932 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001933 break;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001934 }
1935
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001936 cur = cur->next;
1937 }
1938
1939 *p = '\0';
1940
1941 *size = n;
1942 *buf = p;
1943
1944 return( 0 );
1945}
1946
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001947#define PRINT_ITEM(i) \
1948 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001949 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001950 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001951 sep = ", "; \
1952 }
1953
1954#define CERT_TYPE(type,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001955 if( ns_cert_type & (type) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001956 PRINT_ITEM( name );
1957
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001958static int x509_info_cert_type( char **buf, size_t *size,
1959 unsigned char ns_cert_type )
1960{
Janos Follath865b3eb2019-12-16 11:46:15 +00001961 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001962 size_t n = *size;
1963 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001964 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001966 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001967 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001968 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
1969 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1970 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001971 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
1972 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1973 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001974
1975 *size = n;
1976 *buf = p;
1977
1978 return( 0 );
1979}
1980
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001981#define KEY_USAGE(code,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001982 if( key_usage & (code) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001983 PRINT_ITEM( name );
1984
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001985static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001986 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001987{
Janos Follath865b3eb2019-12-16 11:46:15 +00001988 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001989 size_t n = *size;
1990 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001991 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001993 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
1994 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001995 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1996 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1997 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001998 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
1999 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02002000 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
2001 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002002
2003 *size = n;
2004 *buf = p;
2005
2006 return( 0 );
2007}
2008
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002009static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002010 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002011{
Janos Follath865b3eb2019-12-16 11:46:15 +00002012 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002013 const char *desc;
2014 size_t n = *size;
2015 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002016 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002017 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002018
2019 while( cur != NULL )
2020 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002021 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002022 desc = "???";
2023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002024 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002025 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002026
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002027 sep = ", ";
2028
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002029 cur = cur->next;
2030 }
2031
2032 *size = n;
2033 *buf = p;
2034
2035 return( 0 );
2036}
2037
Ron Eldor74d9acc2019-03-21 14:00:03 +02002038static int x509_info_cert_policies( char **buf, size_t *size,
2039 const mbedtls_x509_sequence *certificate_policies )
2040{
Janos Follath865b3eb2019-12-16 11:46:15 +00002041 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor74d9acc2019-03-21 14:00:03 +02002042 const char *desc;
2043 size_t n = *size;
2044 char *p = *buf;
2045 const mbedtls_x509_sequence *cur = certificate_policies;
2046 const char *sep = "";
2047
2048 while( cur != NULL )
2049 {
2050 if( mbedtls_oid_get_certificate_policies( &cur->buf, &desc ) != 0 )
2051 desc = "???";
2052
2053 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
2054 MBEDTLS_X509_SAFE_SNPRINTF;
2055
2056 sep = ", ";
2057
2058 cur = cur->next;
2059 }
2060
2061 *size = n;
2062 *buf = p;
2063
2064 return( 0 );
2065}
2066
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002067/*
2068 * Return an informational string about the certificate.
2069 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002070#define BEFORE_COLON 18
2071#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002072int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
2073 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002074{
Janos Follath865b3eb2019-12-16 11:46:15 +00002075 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002076 size_t n;
2077 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002078 char key_size_str[BEFORE_COLON];
2079
2080 p = buf;
2081 n = size;
2082
Janos Follath98e28a72016-05-31 14:03:54 +01002083 if( NULL == crt )
2084 {
2085 ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" );
2086 MBEDTLS_X509_SAFE_SNPRINTF;
2087
2088 return( (int) ( size - n ) );
2089 }
2090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002091 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002092 prefix, crt->version );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002093 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002094 ret = mbedtls_snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002095 prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002096 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002098 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002099 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002101 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002102 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002103 ret = mbedtls_x509_dn_gets( p, n, &crt->issuer );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002104 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002106 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002107 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002108 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002109 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002111 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002112 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2113 crt->valid_from.year, crt->valid_from.mon,
2114 crt->valid_from.day, crt->valid_from.hour,
2115 crt->valid_from.min, crt->valid_from.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002116 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002118 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002119 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2120 crt->valid_to.year, crt->valid_to.mon,
2121 crt->valid_to.day, crt->valid_to.hour,
2122 crt->valid_to.min, crt->valid_to.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002123 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002125 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002126 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002128 ret = mbedtls_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02002129 crt->sig_md, crt->sig_opts );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002130 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002131
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002132 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002133 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
2134 mbedtls_pk_get_name( &crt->pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002135 {
2136 return( ret );
2137 }
2138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002139 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +02002140 (int) mbedtls_pk_get_bitlen( &crt->pk ) );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002141 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002142
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002143 /*
2144 * Optional extensions
2145 */
2146
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002147 if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002148 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002149 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002150 crt->ca_istrue ? "true" : "false" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002151 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002152
2153 if( crt->max_pathlen > 0 )
2154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002155 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002156 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002157 }
2158 }
2159
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002160 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002161 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002162 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name :", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002163 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002164
2165 if( ( ret = x509_info_subject_alt_name( &p, &n,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002166 &crt->subject_alt_names,
Ron Eldor6aeae9e2019-05-20 12:00:36 +03002167 prefix ) ) != 0 )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002168 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002169 }
2170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002171 if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002173 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002174 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002175
2176 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
2177 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002178 }
2179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002180 if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002181 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002182 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002183 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002184
2185 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
2186 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002187 }
2188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002189 if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002190 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002191 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002192 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002193
2194 if( ( ret = x509_info_ext_key_usage( &p, &n,
2195 &crt->ext_key_usage ) ) != 0 )
2196 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002197 }
2198
Ron Eldor74d9acc2019-03-21 14:00:03 +02002199 if( crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES )
2200 {
2201 ret = mbedtls_snprintf( p, n, "\n%scertificate policies : ", prefix );
2202 MBEDTLS_X509_SAFE_SNPRINTF;
2203
2204 if( ( ret = x509_info_cert_policies( &p, &n,
2205 &crt->certificate_policies ) ) != 0 )
2206 return( ret );
2207 }
2208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002209 ret = mbedtls_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002210 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002211
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002212 return( (int) ( size - n ) );
2213}
2214
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002215struct x509_crt_verify_string {
2216 int code;
2217 const char *string;
2218};
2219
Hanno Becker7ac83f92020-10-09 10:48:22 +01002220#define X509_CRT_ERROR_INFO( err, err_str, info ) { err, info },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002221static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Hanno Becker7ac83f92020-10-09 10:48:22 +01002222 MBEDTLS_X509_CRT_ERROR_INFO_LIST
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002223 { 0, NULL }
2224};
Hanno Becker7ac83f92020-10-09 10:48:22 +01002225#undef X509_CRT_ERROR_INFO
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002226
2227int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002228 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002229{
Janos Follath865b3eb2019-12-16 11:46:15 +00002230 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002231 const struct x509_crt_verify_string *cur;
2232 char *p = buf;
2233 size_t n = size;
2234
2235 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
2236 {
2237 if( ( flags & cur->code ) == 0 )
2238 continue;
2239
2240 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002241 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002242 flags ^= cur->code;
2243 }
2244
2245 if( flags != 0 )
2246 {
2247 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
2248 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002249 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002250 }
2251
2252 return( (int) ( size - n ) );
2253}
Hanno Becker612a2f12020-10-09 09:19:39 +01002254#endif /* MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002255
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002256int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
2257 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002258{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002259 unsigned int usage_must, usage_may;
2260 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
2261 | MBEDTLS_X509_KU_DECIPHER_ONLY;
2262
2263 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
2264 return( 0 );
2265
2266 usage_must = usage & ~may_mask;
2267
2268 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
2269 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
2270
2271 usage_may = usage & may_mask;
2272
2273 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002274 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002275
2276 return( 0 );
2277}
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002279int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002280 const char *usage_oid,
2281 size_t usage_len )
2282{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002283 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002284
2285 /* Extension is not mandatory, absent means no restriction */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002286 if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002287 return( 0 );
2288
2289 /*
2290 * Look for the requested usage (or wildcard ANY) in our list
2291 */
2292 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
2293 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002294 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002295
2296 if( cur_oid->len == usage_len &&
2297 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
2298 {
2299 return( 0 );
2300 }
2301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002302 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002303 return( 0 );
2304 }
2305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002306 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002307}
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002309#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002310/*
2311 * Return 1 if the certificate is revoked, or 0 otherwise.
2312 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002313int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002314{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002315 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002316
2317 while( cur != NULL && cur->serial.len != 0 )
2318 {
2319 if( crt->serial.len == cur->serial.len &&
2320 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
2321 {
Raoul Strackxa4e86142020-06-15 17:03:13 +02002322 return( 1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002323 }
2324
2325 cur = cur->next;
2326 }
2327
2328 return( 0 );
2329}
2330
2331/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002332 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002333 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002334 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002335static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002336 mbedtls_x509_crl *crl_list,
2337 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002338{
2339 int flags = 0;
pespacek7599a772022-02-07 14:40:55 +01002340#if defined(MBEDTLS_USE_PSA_CRYPTO)
2341 unsigned char hash[PSA_HASH_MAX_SIZE];
2342 psa_algorithm_t psa_algorithm;
2343#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002344 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2345 const mbedtls_md_info_t *md_info;
pespacek7599a772022-02-07 14:40:55 +01002346#endif /* MBEDTLS_USE_PSA_CRYPTO */
2347 size_t hash_length;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002348
2349 if( ca == NULL )
2350 return( flags );
2351
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002352 while( crl_list != NULL )
2353 {
2354 if( crl_list->version == 0 ||
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002355 x509_name_cmp( &crl_list->issuer, &ca->subject ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002356 {
2357 crl_list = crl_list->next;
2358 continue;
2359 }
2360
2361 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002362 * Check if the CA is configured to sign CRLs
2363 */
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002364 if( mbedtls_x509_crt_check_key_usage( ca,
2365 MBEDTLS_X509_KU_CRL_SIGN ) != 0 )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002366 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002367 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002368 break;
2369 }
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002370
2371 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002372 * Check if CRL is correctly signed by the trusted CA
2373 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002374 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
2375 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
2376
2377 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
2378 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
2379
pespacek7599a772022-02-07 14:40:55 +01002380#if defined(MBEDTLS_USE_PSA_CRYPTO)
2381 psa_algorithm = mbedtls_psa_translate_md( crl_list->sig_md );
2382 if(psa_hash_compute( psa_algorithm,
2383 crl_list->tbs.p,
2384 crl_list->tbs.len,
2385 hash,PSA_HASH_MAX_SIZE,
2386 &hash_length ) != PSA_SUCCESS )
2387#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002388 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
pespacek7599a772022-02-07 14:40:55 +01002389 hash_length = mbedtls_md_get_size( md_info );
2390 if( mbedtls_md( md_info,
2391 crl_list->tbs.p,
2392 crl_list->tbs.len,
2393 hash ) != 0 )
2394#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002395 {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002396 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002397 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002398 break;
2399 }
2400
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002401 if( x509_profile_check_key( profile, &ca->pk ) != 0 )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002402 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002404 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
pespacek7599a772022-02-07 14:40:55 +01002405 crl_list->sig_md, hash, hash_length,
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02002406 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002407 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002408 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002409 break;
2410 }
2411
2412 /*
2413 * Check for validity of CRL (Do not drop out)
2414 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002415 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002416 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002417
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002418 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002419 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002420
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002421 /*
2422 * Check if certificate is revoked
2423 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002424 if( mbedtls_x509_crt_is_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002425 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002426 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002427 break;
2428 }
2429
2430 crl_list = crl_list->next;
2431 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002432
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002433 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002434}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002435#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002436
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002437/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002438 * Check the signature of a certificate by its parent
2439 */
2440static int x509_crt_check_signature( const mbedtls_x509_crt *child,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002441 mbedtls_x509_crt *parent,
2442 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002443{
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002444 size_t hash_len;
2445#if !defined(MBEDTLS_USE_PSA_CRYPTO)
pespacek7599a772022-02-07 14:40:55 +01002446 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002447 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002448 md_info = mbedtls_md_info_from_type( child->sig_md );
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002449 hash_len = mbedtls_md_get_size( md_info );
Andrzej Kurek8b38ff52018-11-20 03:20:09 -05002450
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002451 /* Note: hash errors can happen only after an internal error */
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002452 if( mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash ) != 0 )
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002453 return( -1 );
2454#else
pespacek7599a772022-02-07 14:40:55 +01002455 unsigned char hash[PSA_HASH_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002456 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( child->sig_md );
pespacek7599a772022-02-07 14:40:55 +01002457 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002458
pespacek7599a772022-02-07 14:40:55 +01002459 status = psa_hash_compute( hash_alg,
2460 child->tbs.p,
2461 child->tbs.len,
2462 hash,
2463 PSA_HASH_MAX_SIZE,
2464 &hash_len );
2465 if( status != PSA_SUCCESS )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002466 {
pespacek3110c7b2022-02-14 15:07:41 +01002467 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002468 }
2469
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002470#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002471 /* Skip expensive computation on obvious mismatch */
2472 if( ! mbedtls_pk_can_do( &parent->pk, child->sig_pk ) )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002473 return( -1 );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002474
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002475#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002476 if( rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA )
2477 {
2478 return( mbedtls_pk_verify_restartable( &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002479 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002480 child->sig.p, child->sig.len, &rs_ctx->pk ) );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002481 }
2482#else
2483 (void) rs_ctx;
2484#endif
2485
2486 return( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002487 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002488 child->sig.p, child->sig.len ) );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002489}
2490
2491/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002492 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2493 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002494 *
2495 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002496 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002497static int x509_crt_check_parent( const mbedtls_x509_crt *child,
2498 const mbedtls_x509_crt *parent,
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002499 int top )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002500{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002501 int need_ca_bit;
2502
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002503 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02002504 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002505 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002506
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002507 /* Parent must have the basicConstraints CA bit set as a general rule */
2508 need_ca_bit = 1;
2509
2510 /* Exception: v1/v2 certificates that are locally trusted. */
2511 if( top && parent->version < 3 )
2512 need_ca_bit = 0;
2513
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002514 if( need_ca_bit && ! parent->ca_istrue )
2515 return( -1 );
2516
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002517 if( need_ca_bit &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002518 mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002519 {
2520 return( -1 );
2521 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002522
2523 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002524}
2525
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002526/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002527 * Find a suitable parent for child in candidates, or return NULL.
2528 *
2529 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002530 * 1. subject name matches child's issuer
2531 * 2. if necessary, the CA bit is set and key usage allows signing certs
2532 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002533 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002534 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002535 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002536 * If there's a suitable candidate which is also time-valid, return the first
2537 * such. Otherwise, return the first suitable candidate (or NULL if there is
2538 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002539 *
2540 * The rationale for this rule is that someone could have a list of trusted
2541 * roots with two versions on the same root with different validity periods.
2542 * (At least one user reported having such a list and wanted it to just work.)
2543 * The reason we don't just require time-validity is that generally there is
2544 * only one version, and if it's expired we want the flags to state that
2545 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002546 *
2547 * The rationale for rule 3 (signature for trusted roots) is that users might
2548 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002549 * way we select the correct one is by checking the signature (as we don't
2550 * rely on key identifier extensions). (This is one way users might choose to
2551 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002552 *
2553 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002554 * - [in] child: certificate for which we're looking for a parent
2555 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002556 * - [out] r_parent: parent found (or NULL)
2557 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002558 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2559 * of the chain, 0 otherwise
2560 * - [in] path_cnt: number of intermediates seen so far
2561 * - [in] self_cnt: number of self-signed intermediates seen so far
2562 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002563 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002564 *
2565 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002566 * - 0 on success
2567 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002568 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002569static int x509_crt_find_parent_in(
2570 mbedtls_x509_crt *child,
2571 mbedtls_x509_crt *candidates,
2572 mbedtls_x509_crt **r_parent,
2573 int *r_signature_is_good,
2574 int top,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002575 unsigned path_cnt,
2576 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002577 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002578{
Janos Follath865b3eb2019-12-16 11:46:15 +00002579 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002580 mbedtls_x509_crt *parent, *fallback_parent;
Benjamin Kier36050732019-05-30 14:49:17 -04002581 int signature_is_good = 0, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002582
2583#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002584 /* did we have something in progress? */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002585 if( rs_ctx != NULL && rs_ctx->parent != NULL )
2586 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002587 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002588 parent = rs_ctx->parent;
2589 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002590 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002591
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002592 /* clear saved state */
2593 rs_ctx->parent = NULL;
2594 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002595 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002596
2597 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002598 goto check_signature;
2599 }
2600#endif
2601
2602 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002603 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002604
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002605 for( parent = candidates; parent != NULL; parent = parent->next )
2606 {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002607 /* basic parenting skills (name, CA bit, key usage) */
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002608 if( x509_crt_check_parent( child, parent, top ) != 0 )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002609 continue;
2610
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002611 /* +1 because stored max_pathlen is 1 higher that the actual value */
2612 if( parent->max_pathlen > 0 &&
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002613 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt )
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002614 {
2615 continue;
2616 }
2617
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002618 /* Signature */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002619#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2620check_signature:
2621#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002622 ret = x509_crt_check_signature( child, parent, rs_ctx );
2623
2624#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002625 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2626 {
2627 /* save state */
2628 rs_ctx->parent = parent;
2629 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002630 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002631
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002632 return( ret );
2633 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002634#else
2635 (void) ret;
2636#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002637
2638 signature_is_good = ret == 0;
2639 if( top && ! signature_is_good )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002640 continue;
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002641
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002642 /* optional time check */
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002643 if( mbedtls_x509_time_is_past( &parent->valid_to ) ||
2644 mbedtls_x509_time_is_future( &parent->valid_from ) )
2645 {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002646 if( fallback_parent == NULL )
2647 {
2648 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002649 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002650 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002651
2652 continue;
2653 }
2654
Andy Gross1f627142019-01-30 10:25:53 -06002655 *r_parent = parent;
2656 *r_signature_is_good = signature_is_good;
2657
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002658 break;
2659 }
2660
Andy Gross1f627142019-01-30 10:25:53 -06002661 if( parent == NULL )
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002662 {
2663 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002664 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002665 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002666
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002667 return( 0 );
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002668}
2669
2670/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002671 * Find a parent in trusted CAs or the provided chain, or return NULL.
2672 *
2673 * Searches in trusted CAs first, and return the first suitable parent found
2674 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002675 *
2676 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002677 * - [in] child: certificate for which we're looking for a parent, followed
2678 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002679 * - [in] trust_ca: list of locally trusted certificates
2680 * - [out] parent: parent found (or NULL)
2681 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2682 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2683 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2684 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002685 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002686 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002687 *
2688 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002689 * - 0 on success
2690 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002691 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002692static int x509_crt_find_parent(
2693 mbedtls_x509_crt *child,
2694 mbedtls_x509_crt *trust_ca,
2695 mbedtls_x509_crt **parent,
2696 int *parent_is_trusted,
2697 int *signature_is_good,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002698 unsigned path_cnt,
2699 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002700 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002701{
Janos Follath865b3eb2019-12-16 11:46:15 +00002702 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002703 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002704
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002705 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002706
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002707#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002708 /* restore then clear saved state if we have some stored */
2709 if( rs_ctx != NULL && rs_ctx->parent_is_trusted != -1 )
2710 {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002711 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002712 rs_ctx->parent_is_trusted = -1;
2713 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002714#endif
2715
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002716 while( 1 ) {
2717 search_list = *parent_is_trusted ? trust_ca : child->next;
2718
2719 ret = x509_crt_find_parent_in( child, search_list,
2720 parent, signature_is_good,
2721 *parent_is_trusted,
2722 path_cnt, self_cnt, rs_ctx );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002723
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002724#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002725 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2726 {
2727 /* save state */
2728 rs_ctx->parent_is_trusted = *parent_is_trusted;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002729 return( ret );
2730 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002731#else
2732 (void) ret;
2733#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002734
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002735 /* stop here if found or already in second iteration */
2736 if( *parent != NULL || *parent_is_trusted == 0 )
2737 break;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002738
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002739 /* prepare second iteration */
2740 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002741 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002742
2743 /* extra precaution against mistakes in the caller */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002744 if( *parent == NULL )
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002745 {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002746 *parent_is_trusted = 0;
2747 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002748 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002749
2750 return( 0 );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002751}
2752
2753/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002754 * Check if an end-entity certificate is locally trusted
2755 *
2756 * Currently we require such certificates to be self-signed (actually only
2757 * check for self-issued as self-signatures are not checked)
2758 */
2759static int x509_crt_check_ee_locally_trusted(
2760 mbedtls_x509_crt *crt,
2761 mbedtls_x509_crt *trust_ca )
2762{
2763 mbedtls_x509_crt *cur;
2764
2765 /* must be self-issued */
2766 if( x509_name_cmp( &crt->issuer, &crt->subject ) != 0 )
2767 return( -1 );
2768
2769 /* look for an exact match with trusted cert */
2770 for( cur = trust_ca; cur != NULL; cur = cur->next )
2771 {
2772 if( crt->raw.len == cur->raw.len &&
2773 memcmp( crt->raw.p, cur->raw.p, crt->raw.len ) == 0 )
2774 {
2775 return( 0 );
2776 }
2777 }
2778
2779 /* too bad */
2780 return( -1 );
2781}
2782
2783/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002784 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002785 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002786 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2787 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002788 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002789 * such that every cert in the chain is a child of the next one,
2790 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002791 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002792 * Verify that chain and return it with flags for all issues found.
2793 *
2794 * Special cases:
2795 * - EE == Rj -> return a one-element list containing it
2796 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2797 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002798 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002799 * Tests for (aspects of) this function should include at least:
2800 * - trusted EE
2801 * - EE -> trusted root
Antonin Décimo36e89b52019-01-23 15:24:37 +01002802 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002803 * - if relevant: EE untrusted
2804 * - if relevant: EE -> intermediate, untrusted
2805 * with the aspect under test checked at each relevant level (EE, int, root).
2806 * For some aspects longer chains are required, but usually length 2 is
2807 * enough (but length 1 is not in general).
2808 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002809 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002810 * - [in] crt: the cert list EE, C1, ..., Cn
2811 * - [in] trust_ca: the trusted list R1, ..., Rp
2812 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002813 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002814 * Only valid when return value is 0, may contain garbage otherwise!
2815 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002816 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002817 *
2818 * Return value:
2819 * - non-zero if the chain could not be fully built and examined
2820 * - 0 is the chain was successfully built and examined,
2821 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002822 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002823static int x509_crt_verify_chain(
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002824 mbedtls_x509_crt *crt,
2825 mbedtls_x509_crt *trust_ca,
2826 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00002827 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2828 void *p_ca_cb,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002829 const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002830 mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002831 mbedtls_x509_crt_restart_ctx *rs_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002832{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002833 /* Don't initialize any of those variables here, so that the compiler can
2834 * catch potential issues with jumping ahead when restarting */
Janos Follath865b3eb2019-12-16 11:46:15 +00002835 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002836 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002837 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002838 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002839 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002840 int parent_is_trusted;
2841 int child_is_trusted;
2842 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002843 unsigned self_cnt;
Hanno Beckerf53893b2019-03-28 13:45:55 +00002844 mbedtls_x509_crt *cur_trust_ca = NULL;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002845
2846#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2847 /* resume if we had an operation in progress */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002848 if( rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent )
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002849 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002850 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002851 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002852 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002853
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002854 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002855 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002856 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002857 flags = &cur->flags;
2858
2859 goto find_parent;
2860 }
2861#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002862
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002863 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002864 self_cnt = 0;
2865 parent_is_trusted = 0;
2866 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002867
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002868 while( 1 ) {
2869 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002870 cur = &ver_chain->items[ver_chain->len];
2871 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002872 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002873 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002874 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002875
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002876 /* Check time-validity (all certificates) */
2877 if( mbedtls_x509_time_is_past( &child->valid_to ) )
2878 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002879
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002880 if( mbedtls_x509_time_is_future( &child->valid_from ) )
2881 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002882
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002883 /* Stop here for trusted roots (but not for trusted EE certs) */
2884 if( child_is_trusted )
2885 return( 0 );
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002886
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002887 /* Check signature algorithm: MD & PK algs */
2888 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
2889 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002890
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002891 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
2892 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002893
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002894 /* Special case: EE certs that are locally trusted */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002895 if( ver_chain->len == 1 &&
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002896 x509_crt_check_ee_locally_trusted( child, trust_ca ) == 0 )
2897 {
2898 return( 0 );
2899 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002900
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002901#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2902find_parent:
2903#endif
Hanno Beckerf53893b2019-03-28 13:45:55 +00002904
2905 /* Obtain list of potential trusted signers from CA callback,
2906 * or use statically provided list. */
2907#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
2908 if( f_ca_cb != NULL )
2909 {
2910 mbedtls_x509_crt_free( ver_chain->trust_ca_cb_result );
2911 mbedtls_free( ver_chain->trust_ca_cb_result );
2912 ver_chain->trust_ca_cb_result = NULL;
2913
2914 ret = f_ca_cb( p_ca_cb, child, &ver_chain->trust_ca_cb_result );
2915 if( ret != 0 )
2916 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2917
2918 cur_trust_ca = ver_chain->trust_ca_cb_result;
2919 }
2920 else
2921#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2922 {
2923 ((void) f_ca_cb);
2924 ((void) p_ca_cb);
2925 cur_trust_ca = trust_ca;
2926 }
2927
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002928 /* Look for a parent in trusted CAs or up the chain */
Hanno Beckerf53893b2019-03-28 13:45:55 +00002929 ret = x509_crt_find_parent( child, cur_trust_ca, &parent,
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002930 &parent_is_trusted, &signature_is_good,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002931 ver_chain->len - 1, self_cnt, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002932
2933#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002934 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2935 {
2936 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002937 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002938 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002939 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002940
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002941 return( ret );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002942 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002943#else
2944 (void) ret;
2945#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002946
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002947 /* No parent? We're done here */
2948 if( parent == NULL )
2949 {
2950 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2951 return( 0 );
2952 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002953
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002954 /* Count intermediate self-issued (not necessarily self-signed) certs.
2955 * These can occur with some strategies for key rollover, see [SIRO],
2956 * and should be excluded from max_pathlen checks. */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002957 if( ver_chain->len != 1 &&
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002958 x509_name_cmp( &child->issuer, &child->subject ) == 0 )
2959 {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002960 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002961 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002962
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002963 /* path_cnt is 0 for the first intermediate CA,
2964 * and if parent is trusted it's not an intermediate CA */
2965 if( ! parent_is_trusted &&
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002966 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002967 {
2968 /* return immediately to avoid overflow the chain array */
2969 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2970 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002971
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002972 /* signature was checked while searching parent */
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002973 if( ! signature_is_good )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002974 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2975
2976 /* check size of signing key */
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002977 if( x509_profile_check_key( profile, &parent->pk ) != 0 )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002978 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002980#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002981 /* Check trusted CA's CRL for the given crt */
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002982 *flags |= x509_crt_verifycrl( child, parent, ca_crl, profile );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002983#else
2984 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002985#endif
2986
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002987 /* prepare for next iteration */
2988 child = parent;
2989 parent = NULL;
2990 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002991 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002992 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002993}
2994
2995/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002996 * Check for CN match
2997 */
2998static int x509_crt_check_cn( const mbedtls_x509_buf *name,
2999 const char *cn, size_t cn_len )
3000{
3001 /* try exact match */
3002 if( name->len == cn_len &&
3003 x509_memcasecmp( cn, name->p, cn_len ) == 0 )
3004 {
3005 return( 0 );
3006 }
3007
3008 /* try wildcard match */
Manuel Pégourié-Gonnard900fba62017-10-18 14:28:11 +02003009 if( x509_check_wildcard( cn, name ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003010 {
3011 return( 0 );
3012 }
3013
3014 return( -1 );
3015}
3016
3017/*
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003018 * Check for SAN match, see RFC 5280 Section 4.2.1.6
3019 */
3020static int x509_crt_check_san( const mbedtls_x509_buf *name,
3021 const char *cn, size_t cn_len )
3022{
3023 const unsigned char san_type = (unsigned char) name->tag &
3024 MBEDTLS_ASN1_TAG_VALUE_MASK;
3025
3026 /* dNSName */
3027 if( san_type == MBEDTLS_X509_SAN_DNS_NAME )
3028 return( x509_crt_check_cn( name, cn, cn_len ) );
3029
3030 /* (We may handle other types here later.) */
3031
3032 /* Unrecognized type */
3033 return( -1 );
3034}
3035
3036/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003037 * Verify the requested CN - only call this if cn is not NULL!
3038 */
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003039static void x509_crt_verify_name( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003040 const char *cn,
3041 uint32_t *flags )
3042{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003043 const mbedtls_x509_name *name;
3044 const mbedtls_x509_sequence *cur;
3045 size_t cn_len = strlen( cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003046
3047 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
3048 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003049 for( cur = &crt->subject_alt_names; cur != NULL; cur = cur->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003050 {
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003051 if( x509_crt_check_san( &cur->buf, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003052 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003053 }
3054
3055 if( cur == NULL )
3056 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3057 }
3058 else
3059 {
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02003060 for( name = &crt->subject; name != NULL; name = name->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003061 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003062 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, &name->oid ) == 0 &&
3063 x509_crt_check_cn( &name->val, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003064 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003065 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003066 }
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003067 }
3068
3069 if( name == NULL )
3070 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3071 }
3072}
3073
3074/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003075 * Merge the flags for all certs in the chain, after calling callback
3076 */
3077static int x509_crt_merge_flags_with_cb(
3078 uint32_t *flags,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003079 const mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003080 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3081 void *p_vrfy )
3082{
Janos Follath865b3eb2019-12-16 11:46:15 +00003083 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003084 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003085 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003086 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003087
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003088 for( i = ver_chain->len; i != 0; --i )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003089 {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003090 cur = &ver_chain->items[i-1];
3091 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003092
3093 if( NULL != f_vrfy )
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003094 if( ( ret = f_vrfy( p_vrfy, cur->crt, (int) i-1, &cur_flags ) ) != 0 )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003095 return( ret );
3096
3097 *flags |= cur_flags;
3098 }
3099
3100 return( 0 );
3101}
3102
3103/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003104 * Verify the certificate validity, with profile, restartable version
3105 *
3106 * This function:
3107 * - checks the requested CN (if any)
3108 * - checks the type and size of the EE cert's key,
3109 * as that isn't done as part of chain building/verification currently
3110 * - builds and verifies the chain
3111 * - then calls the callback and merges the flags
Hanno Becker3116fb32019-03-28 13:34:42 +00003112 *
3113 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
3114 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
3115 * verification routine to search for trusted signers, and CRLs will
3116 * be disabled. Otherwise, `trust_ca` will be used as the static list
3117 * of trusted signers, and `ca_crl` will be use as the static list
3118 * of CRLs.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003119 */
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003120static int x509_crt_verify_restartable_ca_cb( mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003121 mbedtls_x509_crt *trust_ca,
3122 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003123 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3124 void *p_ca_cb,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003125 const mbedtls_x509_crt_profile *profile,
3126 const char *cn, uint32_t *flags,
3127 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3128 void *p_vrfy,
3129 mbedtls_x509_crt_restart_ctx *rs_ctx )
3130{
Janos Follath865b3eb2019-12-16 11:46:15 +00003131 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003132 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003133 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003134 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003135
3136 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003137 ee_flags = 0;
3138 x509_crt_verify_chain_reset( &ver_chain );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003139
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003140 if( profile == NULL )
3141 {
3142 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3143 goto exit;
3144 }
3145
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003146 /* check name if requested */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003147 if( cn != NULL )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003148 x509_crt_verify_name( crt, cn, &ee_flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003149
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003150 /* Check the type and size of the key */
3151 pk_type = mbedtls_pk_get_type( &crt->pk );
3152
3153 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003154 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003155
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02003156 if( x509_profile_check_key( profile, &crt->pk ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003157 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003158
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003159 /* Check the chain */
Hanno Becker3116fb32019-03-28 13:34:42 +00003160 ret = x509_crt_verify_chain( crt, trust_ca, ca_crl,
3161 f_ca_cb, p_ca_cb, profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003162 &ver_chain, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003163
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003164 if( ret != 0 )
3165 goto exit;
3166
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003167 /* Merge end-entity flags */
3168 ver_chain.items[0].flags |= ee_flags;
3169
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003170 /* Build final flags, calling callback on the way if any */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003171 ret = x509_crt_merge_flags_with_cb( flags, &ver_chain, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003172
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003173exit:
Hanno Beckerf53893b2019-03-28 13:45:55 +00003174
3175#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3176 mbedtls_x509_crt_free( ver_chain.trust_ca_cb_result );
3177 mbedtls_free( ver_chain.trust_ca_cb_result );
3178 ver_chain.trust_ca_cb_result = NULL;
3179#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3180
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003181#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3182 if( rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
3183 mbedtls_x509_crt_restart_free( rs_ctx );
3184#endif
3185
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003186 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3187 * the SSL module for authmode optional, but non-zero return from the
3188 * callback means a fatal error so it shouldn't be ignored */
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003189 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
3190 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3191
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003192 if( ret != 0 )
3193 {
3194 *flags = (uint32_t) -1;
3195 return( ret );
3196 }
3197
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003198 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003199 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003200
3201 return( 0 );
3202}
3203
Hanno Becker3116fb32019-03-28 13:34:42 +00003204
3205/*
3206 * Verify the certificate validity (default profile, not restartable)
3207 */
3208int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
3209 mbedtls_x509_crt *trust_ca,
3210 mbedtls_x509_crl *ca_crl,
3211 const char *cn, uint32_t *flags,
3212 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3213 void *p_vrfy )
3214{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003215 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003216 NULL, NULL,
3217 &mbedtls_x509_crt_profile_default,
3218 cn, flags,
3219 f_vrfy, p_vrfy, NULL ) );
3220}
3221
3222/*
3223 * Verify the certificate validity (user-chosen profile, not restartable)
3224 */
3225int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
3226 mbedtls_x509_crt *trust_ca,
3227 mbedtls_x509_crl *ca_crl,
3228 const mbedtls_x509_crt_profile *profile,
3229 const char *cn, uint32_t *flags,
3230 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3231 void *p_vrfy )
3232{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003233 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003234 NULL, NULL,
3235 profile, cn, flags,
3236 f_vrfy, p_vrfy, NULL ) );
3237}
3238
3239#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3240/*
3241 * Verify the certificate validity (user-chosen profile, CA callback,
3242 * not restartable).
3243 */
Jarno Lamsa31d9db62019-04-01 14:33:49 +03003244int mbedtls_x509_crt_verify_with_ca_cb( mbedtls_x509_crt *crt,
Hanno Becker3116fb32019-03-28 13:34:42 +00003245 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3246 void *p_ca_cb,
3247 const mbedtls_x509_crt_profile *profile,
3248 const char *cn, uint32_t *flags,
3249 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3250 void *p_vrfy )
3251{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003252 return( x509_crt_verify_restartable_ca_cb( crt, NULL, NULL,
Hanno Becker3116fb32019-03-28 13:34:42 +00003253 f_ca_cb, p_ca_cb,
3254 profile, cn, flags,
3255 f_vrfy, p_vrfy, NULL ) );
3256}
3257#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3258
3259int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
3260 mbedtls_x509_crt *trust_ca,
3261 mbedtls_x509_crl *ca_crl,
3262 const mbedtls_x509_crt_profile *profile,
3263 const char *cn, uint32_t *flags,
3264 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3265 void *p_vrfy,
3266 mbedtls_x509_crt_restart_ctx *rs_ctx )
3267{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003268 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003269 NULL, NULL,
3270 profile, cn, flags,
3271 f_vrfy, p_vrfy, rs_ctx ) );
3272}
3273
3274
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003275/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003276 * Initialize a certificate chain
3277 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003278void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02003279{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003280 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02003281}
3282
3283/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003284 * Unallocate all certificate data
3285 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003286void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003287{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003288 mbedtls_x509_crt *cert_cur = crt;
3289 mbedtls_x509_crt *cert_prv;
3290 mbedtls_x509_name *name_cur;
3291 mbedtls_x509_name *name_prv;
3292 mbedtls_x509_sequence *seq_cur;
3293 mbedtls_x509_sequence *seq_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003294
3295 if( crt == NULL )
3296 return;
3297
3298 do
3299 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003300 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003302#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
3303 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003304#endif
3305
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003306 name_cur = cert_cur->issuer.next;
3307 while( name_cur != NULL )
3308 {
3309 name_prv = name_cur;
3310 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003311 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003312 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003313 }
3314
3315 name_cur = cert_cur->subject.next;
3316 while( name_cur != NULL )
3317 {
3318 name_prv = name_cur;
3319 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003320 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003321 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003322 }
3323
3324 seq_cur = cert_cur->ext_key_usage.next;
3325 while( seq_cur != NULL )
3326 {
3327 seq_prv = seq_cur;
3328 seq_cur = seq_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003329 mbedtls_platform_zeroize( seq_prv,
3330 sizeof( mbedtls_x509_sequence ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003331 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003332 }
3333
3334 seq_cur = cert_cur->subject_alt_names.next;
3335 while( seq_cur != NULL )
3336 {
3337 seq_prv = seq_cur;
3338 seq_cur = seq_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003339 mbedtls_platform_zeroize( seq_prv,
3340 sizeof( mbedtls_x509_sequence ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003341 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003342 }
3343
Ron Eldor74d9acc2019-03-21 14:00:03 +02003344 seq_cur = cert_cur->certificate_policies.next;
3345 while( seq_cur != NULL )
3346 {
3347 seq_prv = seq_cur;
3348 seq_cur = seq_cur->next;
3349 mbedtls_platform_zeroize( seq_prv,
3350 sizeof( mbedtls_x509_sequence ) );
3351 mbedtls_free( seq_prv );
3352 }
3353
Hanno Becker1a65dcd2019-01-31 08:57:44 +00003354 if( cert_cur->raw.p != NULL && cert_cur->own_buffer )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003355 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003356 mbedtls_platform_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003357 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003358 }
3359
3360 cert_cur = cert_cur->next;
3361 }
3362 while( cert_cur != NULL );
3363
3364 cert_cur = crt;
3365 do
3366 {
3367 cert_prv = cert_cur;
3368 cert_cur = cert_cur->next;
3369
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003370 mbedtls_platform_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003371 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003372 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003373 }
3374 while( cert_cur != NULL );
3375}
3376
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003377#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3378/*
3379 * Initialize a restart context
3380 */
3381void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx )
3382{
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003383 mbedtls_pk_restart_init( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003384
3385 ctx->parent = NULL;
3386 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003387 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003388
3389 ctx->parent_is_trusted = -1;
3390
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003391 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003392 ctx->self_cnt = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003393 x509_crt_verify_chain_reset( &ctx->ver_chain );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003394}
3395
3396/*
3397 * Free the components of a restart context
3398 */
3399void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx )
3400{
3401 if( ctx == NULL )
3402 return;
3403
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003404 mbedtls_pk_restart_free( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003405 mbedtls_x509_crt_restart_init( ctx );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003406}
3407#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003409#endif /* MBEDTLS_X509_CRT_PARSE_C */