blob: 911644b7da9f401d49157fd055a70aa44daa22c0 [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/*
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +02002 * X.509 certificate parsing and verification
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker7c6b2c32013-09-16 13:49:26 +020018 */
19/*
20 * The ITU-T X.509 standard defines a certificate format for PKI.
21 *
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +020022 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
23 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
24 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020025 *
26 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
27 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +020028 *
29 * [SIRO] https://cabforum.org/wp-content/uploads/Chunghwatelecom201503cabforumV4.pdf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020030 */
31
Gilles Peskinedb09ef62020-06-03 01:43:33 +020032#include "common.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020035
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/x509_crt.h"
Janos Follath73c616b2019-12-18 15:07:04 +000037#include "mbedtls/error.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050039#include "mbedtls/platform_util.h"
Rich Evans00ab4702015-02-06 13:43:58 +000040
Rich Evans00ab4702015-02-06 13:43:58 +000041#include <string.h>
42
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020045#endif
46
Andrzej Kurekd4a65532018-10-31 06:18:39 -040047#if defined(MBEDTLS_USE_PSA_CRYPTO)
48#include "psa/crypto.h"
49#include "mbedtls/psa_util.h"
50#endif
51
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020054#else
Simon Butcherd2642582018-10-03 15:11:19 +010055#include <stdio.h>
Rich Evans00ab4702015-02-06 13:43:58 +000056#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#define mbedtls_free free
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020058#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#define mbedtls_snprintf snprintf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020060#endif
61
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000063#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010064#endif
65
Daniel Axtens301db662020-05-28 11:43:41 +100066#if defined(MBEDTLS_HAVE_TIME)
Paul Bakkerfa6a6202013-10-28 18:48:30 +010067#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020068#include <windows.h>
69#else
70#include <time.h>
71#endif
Daniel Axtens301db662020-05-28 11:43:41 +100072#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074#if defined(MBEDTLS_FS_IO)
Rich Evans00ab4702015-02-06 13:43:58 +000075#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020076#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020077#include <sys/types.h>
78#include <sys/stat.h>
79#include <dirent.h>
Rich Evans00ab4702015-02-06 13:43:58 +000080#endif /* !_WIN32 || EFIX64 || EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020081#endif
82
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +020083/*
84 * Item in a verification chain: cert and flags for it
85 */
86typedef struct {
87 mbedtls_x509_crt *crt;
88 uint32_t flags;
89} x509_crt_verify_chain_item;
90
91/*
92 * Max size of verification chain: end-entity + intermediates + trusted root
93 */
94#define X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
Paul Bakker34617722014-06-13 17:20:13 +020095
Gilles Peskine0ecd7192021-06-07 21:24:26 +020096/* Default profile. Do not remove items unless there are serious security
97 * concerns. */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020098const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
99{
Gilles Peskine5e79cb32017-05-04 16:17:21 +0200100 /* Only SHA-2 hashes */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200101 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) |
102 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
103 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
104 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
105 0xFFFFFFF, /* Any PK alg */
106 0xFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200107 2048,
108};
109
110/*
111 * Next-default profile
112 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200113const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
114{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200115 /* Hashes from SHA-256 and above */
116 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
117 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
118 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
119 0xFFFFFFF, /* Any PK alg */
120#if defined(MBEDTLS_ECP_C)
121 /* Curves at or above 128-bit security level */
122 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
123 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
124 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
125 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
126 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
127 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
128 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256K1 ),
129#else
130 0,
131#endif
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200132 2048,
133};
134
135/*
136 * NSA Suite B Profile
137 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200138const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
139{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200140 /* Only SHA-256 and 384 */
141 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
142 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ),
143 /* Only ECDSA */
Ron Eldor85e1dcf2018-02-06 15:59:38 +0200144 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECDSA ) |
145 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECKEY ),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200146#if defined(MBEDTLS_ECP_C)
147 /* Only NIST P-256 and P-384 */
148 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
149 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ),
150#else
151 0,
152#endif
153 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200154};
155
156/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200157 * Check md_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200158 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200159 */
160static int x509_profile_check_md_alg( const mbedtls_x509_crt_profile *profile,
161 mbedtls_md_type_t md_alg )
162{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200163 if( md_alg == MBEDTLS_MD_NONE )
164 return( -1 );
165
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200166 if( ( profile->allowed_mds & MBEDTLS_X509_ID_FLAG( md_alg ) ) != 0 )
167 return( 0 );
168
169 return( -1 );
170}
171
172/*
173 * Check pk_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200174 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200175 */
176static int x509_profile_check_pk_alg( const mbedtls_x509_crt_profile *profile,
177 mbedtls_pk_type_t pk_alg )
178{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200179 if( pk_alg == MBEDTLS_PK_NONE )
180 return( -1 );
181
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200182 if( ( profile->allowed_pks & MBEDTLS_X509_ID_FLAG( pk_alg ) ) != 0 )
183 return( 0 );
184
185 return( -1 );
186}
187
188/*
189 * Check key against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200190 * Return 0 if pk is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200191 */
192static int x509_profile_check_key( const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200193 const mbedtls_pk_context *pk )
194{
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200195 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type( pk );
Manuel Pégourié-Gonnard19773ff2017-10-24 10:51:26 +0200196
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200197#if defined(MBEDTLS_RSA_C)
198 if( pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS )
199 {
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200200 if( mbedtls_pk_get_bitlen( pk ) >= profile->rsa_min_bitlen )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200201 return( 0 );
202
203 return( -1 );
204 }
205#endif
206
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200207#if defined(MBEDTLS_ECP_C)
208 if( pk_alg == MBEDTLS_PK_ECDSA ||
209 pk_alg == MBEDTLS_PK_ECKEY ||
210 pk_alg == MBEDTLS_PK_ECKEY_DH )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200211 {
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200212 const mbedtls_ecp_group_id gid = mbedtls_pk_ec( *pk )->grp.id;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200213
Philippe Antoineb5b25432018-05-11 11:06:29 +0200214 if( gid == MBEDTLS_ECP_DP_NONE )
215 return( -1 );
216
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200217 if( ( profile->allowed_curves & MBEDTLS_X509_ID_FLAG( gid ) ) != 0 )
218 return( 0 );
219
220 return( -1 );
221 }
222#endif
223
224 return( -1 );
225}
226
227/*
Hanno Becker1f8527f2018-11-02 09:19:16 +0000228 * Like memcmp, but case-insensitive and always returns -1 if different
229 */
230static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
231{
232 size_t i;
233 unsigned char diff;
234 const unsigned char *n1 = s1, *n2 = s2;
235
236 for( i = 0; i < len; i++ )
237 {
238 diff = n1[i] ^ n2[i];
239
240 if( diff == 0 )
241 continue;
242
243 if( diff == 32 &&
244 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
245 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
246 {
247 continue;
248 }
249
250 return( -1 );
251 }
252
253 return( 0 );
254}
255
256/*
257 * Return 0 if name matches wildcard, -1 otherwise
258 */
259static int x509_check_wildcard( const char *cn, const mbedtls_x509_buf *name )
260{
261 size_t i;
262 size_t cn_idx = 0, cn_len = strlen( cn );
263
264 /* We can't have a match if there is no wildcard to match */
265 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
266 return( -1 );
267
268 for( i = 0; i < cn_len; ++i )
269 {
270 if( cn[i] == '.' )
271 {
272 cn_idx = i;
273 break;
274 }
275 }
276
277 if( cn_idx == 0 )
278 return( -1 );
279
280 if( cn_len - cn_idx == name->len - 1 &&
281 x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
282 {
283 return( 0 );
284 }
285
286 return( -1 );
287}
288
289/*
290 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
291 * variations (but not all).
292 *
293 * Return 0 if equal, -1 otherwise.
294 */
295static int x509_string_cmp( const mbedtls_x509_buf *a, const mbedtls_x509_buf *b )
296{
297 if( a->tag == b->tag &&
298 a->len == b->len &&
299 memcmp( a->p, b->p, b->len ) == 0 )
300 {
301 return( 0 );
302 }
303
304 if( ( a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
305 ( b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
306 a->len == b->len &&
307 x509_memcasecmp( a->p, b->p, b->len ) == 0 )
308 {
309 return( 0 );
310 }
311
312 return( -1 );
313}
314
315/*
316 * Compare two X.509 Names (aka rdnSequence).
317 *
318 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
319 * we sometimes return unequal when the full algorithm would return equal,
320 * but never the other way. (In particular, we don't do Unicode normalisation
321 * or space folding.)
322 *
323 * Return 0 if equal, -1 otherwise.
324 */
325static int x509_name_cmp( const mbedtls_x509_name *a, const mbedtls_x509_name *b )
326{
327 /* Avoid recursion, it might not be optimised by the compiler */
328 while( a != NULL || b != NULL )
329 {
330 if( a == NULL || b == NULL )
331 return( -1 );
332
333 /* type */
334 if( a->oid.tag != b->oid.tag ||
335 a->oid.len != b->oid.len ||
336 memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 )
337 {
338 return( -1 );
339 }
340
341 /* value */
342 if( x509_string_cmp( &a->val, &b->val ) != 0 )
343 return( -1 );
344
345 /* structure of the list of sets */
346 if( a->next_merged != b->next_merged )
347 return( -1 );
348
349 a = a->next;
350 b = b->next;
351 }
352
353 /* a == NULL == b */
354 return( 0 );
355}
356
357/*
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200358 * Reset (init or clear) a verify_chain
359 */
360static void x509_crt_verify_chain_reset(
361 mbedtls_x509_crt_verify_chain *ver_chain )
362{
363 size_t i;
364
365 for( i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++ )
366 {
367 ver_chain->items[i].crt = NULL;
Hanno Beckera9375b32019-01-10 09:19:26 +0000368 ver_chain->items[i].flags = (uint32_t) -1;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200369 }
370
371 ver_chain->len = 0;
Hanno Beckerf53893b2019-03-28 13:45:55 +0000372
373#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
374 ver_chain->trust_ca_cb_result = NULL;
375#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200376}
377
378/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200379 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
380 */
381static int x509_get_version( unsigned char **p,
382 const unsigned char *end,
383 int *ver )
384{
Janos Follath865b3eb2019-12-16 11:46:15 +0000385 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200386 size_t len;
387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
389 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200390 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200392 {
393 *ver = 0;
394 return( 0 );
395 }
396
Chris Jones9f7a6932021-04-14 12:12:09 +0100397 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200398 }
399
400 end = *p + len;
401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100403 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_VERSION, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200404
405 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100406 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_VERSION,
407 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200408
409 return( 0 );
410}
411
412/*
413 * Validity ::= SEQUENCE {
414 * notBefore Time,
415 * notAfter Time }
416 */
417static int x509_get_dates( unsigned char **p,
418 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419 mbedtls_x509_time *from,
420 mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200421{
Janos Follath865b3eb2019-12-16 11:46:15 +0000422 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200423 size_t len;
424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
426 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100427 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200428
429 end = *p + len;
430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431 if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200432 return( ret );
433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434 if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200435 return( ret );
436
437 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100438 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE,
439 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200440
441 return( 0 );
442}
443
444/*
445 * X.509 v2/v3 unique identifier (not parsed)
446 */
447static int x509_get_uid( unsigned char **p,
448 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449 mbedtls_x509_buf *uid, int n )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200450{
Janos Follath865b3eb2019-12-16 11:46:15 +0000451 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200452
453 if( *p == end )
454 return( 0 );
455
456 uid->tag = **p;
457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458 if( ( ret = mbedtls_asn1_get_tag( p, end, &uid->len,
459 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200462 return( 0 );
463
Chris Jones9f7a6932021-04-14 12:12:09 +0100464 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200465 }
466
467 uid->p = *p;
468 *p += uid->len;
469
470 return( 0 );
471}
472
473static int x509_get_basic_constraints( unsigned char **p,
474 const unsigned char *end,
475 int *ca_istrue,
476 int *max_pathlen )
477{
Janos Follath865b3eb2019-12-16 11:46:15 +0000478 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200479 size_t len;
480
481 /*
482 * BasicConstraints ::= SEQUENCE {
483 * cA BOOLEAN DEFAULT FALSE,
484 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
485 */
486 *ca_istrue = 0; /* DEFAULT FALSE */
487 *max_pathlen = 0; /* endless */
488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
490 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100491 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200492
493 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200494 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496 if( ( ret = mbedtls_asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200497 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
499 ret = mbedtls_asn1_get_int( p, end, ca_istrue );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200500
501 if( ret != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100502 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200503
504 if( *ca_istrue != 0 )
505 *ca_istrue = 1;
506 }
507
508 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200509 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511 if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100512 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200513
514 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100515 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
516 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200517
Andrzej Kurek16050742020-04-14 09:49:52 -0400518 /* Do not accept max_pathlen equal to INT_MAX to avoid a signed integer
519 * overflow, which is an undefined behavior. */
520 if( *max_pathlen == INT_MAX )
Chris Jones9f7a6932021-04-14 12:12:09 +0100521 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
522 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Andrzej Kurek16050742020-04-14 09:49:52 -0400523
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200524 (*max_pathlen)++;
525
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200526 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200527}
528
529static int x509_get_ns_cert_type( unsigned char **p,
530 const unsigned char *end,
531 unsigned char *ns_cert_type)
532{
Janos Follath865b3eb2019-12-16 11:46:15 +0000533 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 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( bs.len != 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100540 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
541 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200542
543 /* Get actual bitstring */
544 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200545 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200546}
547
548static int x509_get_key_usage( unsigned char **p,
549 const unsigned char *end,
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +0100550 unsigned int *key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200551{
Janos Follath865b3eb2019-12-16 11:46:15 +0000552 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200553 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100557 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200558
559 if( bs.len < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100560 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
561 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200562
563 /* Get actual bitstring */
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200564 *key_usage = 0;
565 for( i = 0; i < bs.len && i < sizeof( unsigned int ); i++ )
566 {
567 *key_usage |= (unsigned int) bs.p[i] << (8*i);
568 }
569
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200570 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200571}
572
573/*
574 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
575 *
576 * KeyPurposeId ::= OBJECT IDENTIFIER
577 */
578static int x509_get_ext_key_usage( unsigned char **p,
579 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200581{
Janos Follath865b3eb2019-12-16 11:46:15 +0000582 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584 if( ( ret = mbedtls_asn1_get_sequence_of( p, end, ext_key_usage, MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100585 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200586
587 /* Sequence length must be >= 1 */
588 if( ext_key_usage->buf.p == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100589 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
590 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200591
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200592 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200593}
594
595/*
596 * SubjectAltName ::= GeneralNames
597 *
598 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
599 *
600 * GeneralName ::= CHOICE {
601 * otherName [0] OtherName,
602 * rfc822Name [1] IA5String,
603 * dNSName [2] IA5String,
604 * x400Address [3] ORAddress,
605 * directoryName [4] Name,
606 * ediPartyName [5] EDIPartyName,
607 * uniformResourceIdentifier [6] IA5String,
608 * iPAddress [7] OCTET STRING,
609 * registeredID [8] OBJECT IDENTIFIER }
610 *
611 * OtherName ::= SEQUENCE {
612 * type-id OBJECT IDENTIFIER,
613 * value [0] EXPLICIT ANY DEFINED BY type-id }
614 *
615 * EDIPartyName ::= SEQUENCE {
616 * nameAssigner [0] DirectoryString OPTIONAL,
617 * partyName [1] DirectoryString }
618 *
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300619 * NOTE: we list all types, but only use dNSName and otherName
620 * of type HwModuleName, as defined in RFC 4108, at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200621 */
622static int x509_get_subject_alt_name( unsigned char **p,
623 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624 mbedtls_x509_sequence *subject_alt_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200625{
Janos Follath865b3eb2019-12-16 11:46:15 +0000626 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200627 size_t len, tag_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628 mbedtls_asn1_buf *buf;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200629 unsigned char tag;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630 mbedtls_asn1_sequence *cur = subject_alt_name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200631
632 /* Get main sequence tag */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
634 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100635 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200636
637 if( *p + len != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100638 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
639 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200640
641 while( *p < end )
642 {
Ron Eldordbbd9662019-05-15 15:46:03 +0300643 mbedtls_x509_subject_alternative_name dummy_san_buf;
644 memset( &dummy_san_buf, 0, sizeof( dummy_san_buf ) );
645
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200646 tag = **p;
647 (*p)++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 if( ( ret = mbedtls_asn1_get_len( p, end, &tag_len ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100649 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200650
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100651 if( ( tag & MBEDTLS_ASN1_TAG_CLASS_MASK ) !=
652 MBEDTLS_ASN1_CONTEXT_SPECIFIC )
653 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100654 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
655 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100656 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200657
Ron Eldordbbd9662019-05-15 15:46:03 +0300658 /*
irwir74aee1c2019-09-21 18:21:48 +0300659 * Check that the SAN is structured correctly.
Ron Eldordbbd9662019-05-15 15:46:03 +0300660 */
661 ret = mbedtls_x509_parse_subject_alt_name( &(cur->buf), &dummy_san_buf );
662 /*
663 * In case the extension is malformed, return an error,
664 * and clear the allocated sequences.
665 */
666 if( ret != 0 && ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
667 {
668 mbedtls_x509_sequence *seq_cur = subject_alt_name->next;
669 mbedtls_x509_sequence *seq_prv;
670 while( seq_cur != NULL )
671 {
672 seq_prv = seq_cur;
673 seq_cur = seq_cur->next;
674 mbedtls_platform_zeroize( seq_prv,
675 sizeof( mbedtls_x509_sequence ) );
676 mbedtls_free( seq_prv );
677 }
Ron Eldor5aebeeb2019-05-22 16:41:21 +0300678 subject_alt_name->next = NULL;
Ron Eldordbbd9662019-05-15 15:46:03 +0300679 return( ret );
680 }
681
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200682 /* Allocate and assign next pointer */
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200683 if( cur->buf.p != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200684 {
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100685 if( cur->next != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100687
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200688 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200689
690 if( cur->next == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100691 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
692 MBEDTLS_ERR_ASN1_ALLOC_FAILED ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200693
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200694 cur = cur->next;
695 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200696
697 buf = &(cur->buf);
698 buf->tag = tag;
699 buf->p = *p;
700 buf->len = tag_len;
701 *p += buf->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200702 }
703
704 /* Set final sequence entry's next pointer to NULL */
705 cur->next = NULL;
706
707 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100708 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
709 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200710
711 return( 0 );
712}
713
714/*
Ron Eldor74d9acc2019-03-21 14:00:03 +0200715 * id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 }
716 *
717 * anyPolicy OBJECT IDENTIFIER ::= { id-ce-certificatePolicies 0 }
718 *
719 * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
720 *
721 * PolicyInformation ::= SEQUENCE {
722 * policyIdentifier CertPolicyId,
723 * policyQualifiers SEQUENCE SIZE (1..MAX) OF
724 * PolicyQualifierInfo OPTIONAL }
725 *
726 * CertPolicyId ::= OBJECT IDENTIFIER
727 *
728 * PolicyQualifierInfo ::= SEQUENCE {
729 * policyQualifierId PolicyQualifierId,
730 * qualifier ANY DEFINED BY policyQualifierId }
731 *
732 * -- policyQualifierIds for Internet policy qualifiers
733 *
734 * id-qt OBJECT IDENTIFIER ::= { id-pkix 2 }
735 * id-qt-cps OBJECT IDENTIFIER ::= { id-qt 1 }
736 * id-qt-unotice OBJECT IDENTIFIER ::= { id-qt 2 }
737 *
738 * PolicyQualifierId ::= OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice )
739 *
740 * Qualifier ::= CHOICE {
741 * cPSuri CPSuri,
742 * userNotice UserNotice }
743 *
744 * CPSuri ::= IA5String
745 *
746 * UserNotice ::= SEQUENCE {
747 * noticeRef NoticeReference OPTIONAL,
748 * explicitText DisplayText OPTIONAL }
749 *
750 * NoticeReference ::= SEQUENCE {
751 * organization DisplayText,
752 * noticeNumbers SEQUENCE OF INTEGER }
753 *
754 * DisplayText ::= CHOICE {
755 * ia5String IA5String (SIZE (1..200)),
756 * visibleString VisibleString (SIZE (1..200)),
757 * bmpString BMPString (SIZE (1..200)),
758 * utf8String UTF8String (SIZE (1..200)) }
759 *
760 * NOTE: we only parse and use anyPolicy without qualifiers at this point
761 * as defined in RFC 5280.
762 */
763static int x509_get_certificate_policies( unsigned char **p,
764 const unsigned char *end,
765 mbedtls_x509_sequence *certificate_policies )
766{
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300767 int ret, parse_ret = 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200768 size_t len;
769 mbedtls_asn1_buf *buf;
770 mbedtls_asn1_sequence *cur = certificate_policies;
771
772 /* Get main sequence tag */
773 ret = mbedtls_asn1_get_tag( p, end, &len,
774 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
775 if( ret != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100776 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200777
778 if( *p + len != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100779 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
780 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200781
782 /*
783 * Cannot be an empty sequence.
784 */
785 if( len == 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100786 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
787 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200788
789 while( *p < end )
790 {
791 mbedtls_x509_buf policy_oid;
792 const unsigned char *policy_end;
793
794 /*
795 * Get the policy sequence
796 */
797 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
798 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100799 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200800
801 policy_end = *p + len;
802
Ron Eldor08063792019-05-13 16:38:39 +0300803 if( ( ret = mbedtls_asn1_get_tag( p, policy_end, &len,
Ron Eldor74d9acc2019-03-21 14:00:03 +0200804 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100805 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200806
807 policy_oid.tag = MBEDTLS_ASN1_OID;
808 policy_oid.len = len;
809 policy_oid.p = *p;
810
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300811 /*
812 * Only AnyPolicy is currently supported when enforcing policy.
813 */
814 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_POLICY, &policy_oid ) != 0 )
815 {
816 /*
817 * Set the parsing return code but continue parsing, in case this
818 * extension is critical and MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION
819 * is configured.
820 */
821 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
822 }
823
Ron Eldor74d9acc2019-03-21 14:00:03 +0200824 /* Allocate and assign next pointer */
825 if( cur->buf.p != NULL )
826 {
827 if( cur->next != NULL )
828 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
829
830 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
831
832 if( cur->next == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100833 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
834 MBEDTLS_ERR_ASN1_ALLOC_FAILED ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200835
836 cur = cur->next;
837 }
838
839 buf = &( cur->buf );
840 buf->tag = policy_oid.tag;
841 buf->p = policy_oid.p;
842 buf->len = policy_oid.len;
Ron Eldor08063792019-05-13 16:38:39 +0300843
844 *p += len;
845
846 /*
847 * If there is an optional qualifier, then *p < policy_end
848 * Check the Qualifier len to verify it doesn't exceed policy_end.
849 */
850 if( *p < policy_end )
851 {
852 if( ( ret = mbedtls_asn1_get_tag( p, policy_end, &len,
853 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100854 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor08063792019-05-13 16:38:39 +0300855 /*
856 * Skip the optional policy qualifiers.
857 */
858 *p += len;
859 }
860
861 if( *p != policy_end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100862 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
863 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200864 }
865
866 /* Set final sequence entry's next pointer to NULL */
867 cur->next = NULL;
868
869 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100870 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
871 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200872
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300873 return( parse_ret );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200874}
875
876/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200877 * X.509 v3 extensions
878 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200879 */
880static int x509_get_crt_ext( unsigned char **p,
881 const unsigned char *end,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200882 mbedtls_x509_crt *crt,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200883 mbedtls_x509_crt_ext_cb_t cb,
884 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200885{
Janos Follath865b3eb2019-12-16 11:46:15 +0000886 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200887 size_t len;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200888 unsigned char *end_ext_data, *start_ext_octet, *end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200889
Hanno Becker12f62fb2019-02-12 17:22:36 +0000890 if( *p == end )
891 return( 0 );
892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200893 if( ( ret = mbedtls_x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200894 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200895
Hanno Becker12f62fb2019-02-12 17:22:36 +0000896 end = crt->v3_ext.p + crt->v3_ext.len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200897 while( *p < end )
898 {
899 /*
900 * Extension ::= SEQUENCE {
901 * extnID OBJECT IDENTIFIER,
902 * critical BOOLEAN DEFAULT FALSE,
903 * extnValue OCTET STRING }
904 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200905 mbedtls_x509_buf extn_oid = {0, 0, NULL};
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200906 int is_critical = 0; /* DEFAULT FALSE */
907 int ext_type = 0;
908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
910 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100911 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200912
913 end_ext_data = *p + len;
914
915 /* Get extension ID */
k-stachowiakdcae78a2018-06-28 16:32:54 +0200916 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &extn_oid.len,
k-stachowiak463928a2018-07-24 12:50:59 +0200917 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100918 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200919
k-stachowiak463928a2018-07-24 12:50:59 +0200920 extn_oid.tag = MBEDTLS_ASN1_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200921 extn_oid.p = *p;
922 *p += extn_oid.len;
923
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200924 /* Get optional critical */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925 if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
926 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
Chris Jones9f7a6932021-04-14 12:12:09 +0100927 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200928
929 /* Data should be octet string type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
931 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100932 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200933
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200934 start_ext_octet = *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200935 end_ext_octet = *p + len;
936
937 if( end_ext_octet != end_ext_data )
Chris Jones9f7a6932021-04-14 12:12:09 +0100938 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
939 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200940
941 /*
942 * Detect supported extensions
943 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200945
946 if( ret != 0 )
947 {
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200948 /* Give the callback (if any) a chance to handle the extension */
Nicola Di Lieto2c3a9172020-05-28 17:20:42 +0200949 if( cb != NULL )
950 {
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200951 ret = cb( p_ctx, crt, &extn_oid, is_critical, *p, end_ext_octet );
Nicola Di Lieto565b52b2020-05-29 22:46:56 +0200952 if( ret != 0 && is_critical )
953 return( ret );
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200954 *p = end_ext_octet;
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200955 continue;
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200956 }
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200957
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200958 /* No parser found, skip extension */
959 *p = end_ext_octet;
960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200962 if( is_critical )
963 {
964 /* Data is marked as critical: fail */
Chris Jones9f7a6932021-04-14 12:12:09 +0100965 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
966 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200967 }
968#endif
969 continue;
970 }
971
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100972 /* Forbid repeated extensions */
973 if( ( crt->ext_types & ext_type ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200974 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100975
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200976 crt->ext_types |= ext_type;
977
978 switch( ext_type )
979 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100980 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200981 /* Parse basic constraints */
982 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
983 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200984 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200985 break;
986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200987 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200988 /* Parse key usage */
989 if( ( ret = x509_get_key_usage( p, end_ext_octet,
990 &crt->key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200991 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200992 break;
993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200995 /* Parse extended key usage */
996 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
997 &crt->ext_key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200998 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200999 break;
1000
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001001 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001002 /* Parse subject alt name */
1003 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
1004 &crt->subject_alt_names ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001005 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001006 break;
1007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001009 /* Parse netscape certificate type */
1010 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
1011 &crt->ns_cert_type ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001012 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001013 break;
1014
Ron Eldor74d9acc2019-03-21 14:00:03 +02001015 case MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES:
1016 /* Parse certificate policies type */
1017 if( ( ret = x509_get_certificate_policies( p, end_ext_octet,
1018 &crt->certificate_policies ) ) != 0 )
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001019 {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +02001020 /* Give the callback (if any) a chance to handle the extension
1021 * if it contains unsupported policies */
1022 if( ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE && cb != NULL &&
1023 cb( p_ctx, crt, &extn_oid, is_critical,
1024 start_ext_octet, end_ext_octet ) == 0 )
1025 break;
1026
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001027#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
1028 if( is_critical )
1029 return( ret );
1030 else
1031#endif
1032 /*
Ron Eldora2913912019-05-16 16:17:38 +03001033 * If MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE is returned, then we
1034 * cannot interpret or enforce the policy. However, it is up to
1035 * the user to choose how to enforce the policies,
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001036 * unless the extension is critical.
1037 */
1038 if( ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1039 return( ret );
1040 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001041 break;
1042
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001043 default:
Ron Eldordf48efa2019-04-08 13:28:24 +03001044 /*
1045 * If this is a non-critical extension, which the oid layer
1046 * supports, but there isn't an x509 parser for it,
1047 * skip the extension.
1048 */
1049#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
1050 if( is_critical )
1051 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1052 else
1053#endif
1054 *p = end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001055 }
1056 }
1057
1058 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +01001059 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1060 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001061
1062 return( 0 );
1063}
1064
1065/*
1066 * Parse and fill a single X.509 certificate in DER format
1067 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001068static int x509_crt_parse_der_core( mbedtls_x509_crt *crt,
1069 const unsigned char *buf,
1070 size_t buflen,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001071 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001072 mbedtls_x509_crt_ext_cb_t cb,
1073 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001074{
Janos Follath865b3eb2019-12-16 11:46:15 +00001075 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001076 size_t len;
1077 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080 memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) );
1081 memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) );
1082 memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001083
1084 /*
1085 * Check for valid input
1086 */
1087 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001089
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001090 /* Use the original buffer until we figure out actual length. */
Janos Follathcc0e49d2016-02-17 14:34:12 +00001091 p = (unsigned char*) buf;
1092 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001093 end = p + len;
1094
1095 /*
1096 * Certificate ::= SEQUENCE {
1097 * tbsCertificate TBSCertificate,
1098 * signatureAlgorithm AlgorithmIdentifier,
1099 * signatureValue BIT STRING }
1100 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1102 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001104 mbedtls_x509_crt_free( crt );
1105 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001106 }
1107
Janos Follathcc0e49d2016-02-17 14:34:12 +00001108 end = crt_end = p + len;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001109 crt->raw.len = crt_end - buf;
1110 if( make_copy != 0 )
1111 {
1112 /* Create and populate a new buffer for the raw field. */
1113 crt->raw.p = p = mbedtls_calloc( 1, crt->raw.len );
1114 if( crt->raw.p == NULL )
1115 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
1116
1117 memcpy( crt->raw.p, buf, crt->raw.len );
1118 crt->own_buffer = 1;
1119
1120 p += crt->raw.len - len;
1121 end = crt_end = p + len;
1122 }
1123 else
1124 {
1125 crt->raw.p = (unsigned char*) buf;
1126 crt->own_buffer = 0;
1127 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001128
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001129 /*
1130 * TBSCertificate ::= SEQUENCE {
1131 */
1132 crt->tbs.p = p;
1133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1135 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001136 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001138 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001139 }
1140
1141 end = p + len;
1142 crt->tbs.len = end - crt->tbs.p;
1143
1144 /*
1145 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1146 *
1147 * CertificateSerialNumber ::= INTEGER
1148 *
1149 * signature AlgorithmIdentifier
1150 */
1151 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152 ( ret = mbedtls_x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1153 ( ret = mbedtls_x509_get_alg( &p, end, &crt->sig_oid,
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001154 &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001155 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001156 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001157 return( ret );
1158 }
1159
Andres AG7ca4a032017-03-09 16:16:11 +00001160 if( crt->version < 0 || crt->version > 2 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001161 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162 mbedtls_x509_crt_free( crt );
1163 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001164 }
1165
Andres AG7ca4a032017-03-09 16:16:11 +00001166 crt->version++;
1167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168 if( ( ret = mbedtls_x509_get_sig_alg( &crt->sig_oid, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02001169 &crt->sig_md, &crt->sig_pk,
1170 &crt->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001171 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001173 return( ret );
1174 }
1175
1176 /*
1177 * issuer Name
1178 */
1179 crt->issuer_raw.p = p;
1180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001181 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1182 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001184 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001185 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001186 }
1187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001188 if( ( ret = mbedtls_x509_get_name( &p, p + len, &crt->issuer ) ) != 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 crt->issuer_raw.len = p - crt->issuer_raw.p;
1195
1196 /*
1197 * Validity ::= SEQUENCE {
1198 * notBefore Time,
1199 * notAfter Time }
1200 *
1201 */
1202 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1203 &crt->valid_to ) ) != 0 )
1204 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001205 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001206 return( ret );
1207 }
1208
1209 /*
1210 * subject Name
1211 */
1212 crt->subject_raw.p = p;
1213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001214 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1215 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001217 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001218 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001219 }
1220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001221 if( len && ( ret = mbedtls_x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001222 {
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 crt->subject_raw.len = p - crt->subject_raw.p;
1228
1229 /*
1230 * SubjectPublicKeyInfo
1231 */
Hanno Becker494dd7a2019-02-06 16:13:41 +00001232 crt->pk_raw.p = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001233 if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001234 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001235 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001236 return( ret );
1237 }
Hanno Becker494dd7a2019-02-06 16:13:41 +00001238 crt->pk_raw.len = p - crt->pk_raw.p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001239
1240 /*
1241 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1242 * -- If present, version shall be v2 or v3
1243 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1244 * -- If present, version shall be v2 or v3
1245 * extensions [3] EXPLICIT Extensions OPTIONAL
1246 * -- If present, version shall be v3
1247 */
1248 if( crt->version == 2 || crt->version == 3 )
1249 {
1250 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1251 if( ret != 0 )
1252 {
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 }
1256 }
1257
1258 if( crt->version == 2 || crt->version == 3 )
1259 {
1260 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1261 if( ret != 0 )
1262 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001263 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001264 return( ret );
1265 }
1266 }
1267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001268#if !defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001269 if( crt->version == 3 )
Paul Bakkerc27c4e22013-09-23 15:01:36 +02001270#endif
Manuel Pégourié-Gonnarda252af72015-03-27 16:15:55 +01001271 {
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001272 ret = x509_get_crt_ext( &p, end, crt, cb, p_ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001273 if( ret != 0 )
1274 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001275 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001276 return( ret );
1277 }
1278 }
1279
1280 if( p != end )
1281 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001282 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001283 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT,
1284 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001285 }
1286
1287 end = crt_end;
1288
1289 /*
1290 * }
1291 * -- end of TBSCertificate
1292 *
1293 * signatureAlgorithm AlgorithmIdentifier,
1294 * signatureValue BIT STRING
1295 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001296 if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001299 return( ret );
1300 }
1301
Manuel Pégourié-Gonnard1022fed2015-03-27 16:30:47 +01001302 if( crt->sig_oid.len != sig_oid2.len ||
1303 memcmp( crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len ) != 0 ||
Paul Elliottca17ebf2020-11-24 17:30:18 +00001304 sig_params1.tag != sig_params2.tag ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001305 sig_params1.len != sig_params2.len ||
Manuel Pégourié-Gonnard159c5242015-04-30 11:15:22 +02001306 ( sig_params1.len != 0 &&
1307 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001308 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001309 mbedtls_x509_crt_free( crt );
1310 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001311 }
1312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313 if( ( ret = mbedtls_x509_get_sig( &p, end, &crt->sig ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001314 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001316 return( ret );
1317 }
1318
1319 if( p != end )
1320 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001322 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT,
1323 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001324 }
1325
1326 return( 0 );
1327}
1328
1329/*
1330 * Parse one X.509 certificate in DER format from a buffer and add them to a
1331 * chained list
1332 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001333static int mbedtls_x509_crt_parse_der_internal( mbedtls_x509_crt *chain,
1334 const unsigned char *buf,
1335 size_t buflen,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001336 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001337 mbedtls_x509_crt_ext_cb_t cb,
1338 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001339{
Janos Follath865b3eb2019-12-16 11:46:15 +00001340 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001341 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001342
1343 /*
1344 * Check for valid input
1345 */
1346 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001347 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001348
1349 while( crt->version != 0 && crt->next != NULL )
1350 {
1351 prev = crt;
1352 crt = crt->next;
1353 }
1354
1355 /*
1356 * Add new certificate on the end of the chain if needed.
1357 */
Paul Bakker66d5d072014-06-17 16:39:18 +02001358 if( crt->version != 0 && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001359 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001360 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001361
1362 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001363 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001364
1365 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001367 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001368 }
1369
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001370 ret = x509_crt_parse_der_core( crt, buf, buflen, make_copy, cb, p_ctx );
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001371 if( ret != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001372 {
1373 if( prev )
1374 prev->next = NULL;
1375
1376 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001378
1379 return( ret );
1380 }
1381
1382 return( 0 );
1383}
1384
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001385int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
1386 const unsigned char *buf,
1387 size_t buflen )
1388{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001389 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 0, NULL, NULL ) );
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001390}
1391
Nicola Di Lietofde98f72020-05-28 08:34:33 +02001392int mbedtls_x509_crt_parse_der_with_ext_cb( mbedtls_x509_crt *chain,
1393 const unsigned char *buf,
1394 size_t buflen,
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +02001395 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001396 mbedtls_x509_crt_ext_cb_t cb,
1397 void *p_ctx )
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001398{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001399 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, make_copy, cb, p_ctx ) );
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001400}
1401
1402int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
1403 const unsigned char *buf,
1404 size_t buflen )
1405{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001406 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 1, NULL, NULL ) );
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001407}
1408
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001409/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001410 * Parse one or more PEM certificates from a buffer and add them to the chained
1411 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001412 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001413int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain,
1414 const unsigned char *buf,
1415 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001416{
Janos Follath98e28a72016-05-31 14:03:54 +01001417#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001418 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001420#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001421
1422 /*
1423 * Check for valid input
1424 */
1425 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001427
1428 /*
1429 * Determine buffer content. Buffer contains either one DER certificate or
1430 * one or more PEM certificates.
1431 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001433 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001434 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1435 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001437 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439 if( buf_format == MBEDTLS_X509_FORMAT_DER )
1440 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Janos Follath98e28a72016-05-31 14:03:54 +01001441#else
1442 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
1443#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001445#if defined(MBEDTLS_PEM_PARSE_C)
1446 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001447 {
Janos Follath865b3eb2019-12-16 11:46:15 +00001448 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001450
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001451 /* 1 rather than 0 since the terminating NULL byte is counted in */
1452 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001453 {
1454 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001455 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001456
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001457 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001459 "-----BEGIN CERTIFICATE-----",
1460 "-----END CERTIFICATE-----",
1461 buf, NULL, 0, &use_len );
1462
1463 if( ret == 0 )
1464 {
1465 /*
1466 * Was PEM encoded
1467 */
1468 buflen -= use_len;
1469 buf += use_len;
1470 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001472 {
1473 return( ret );
1474 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001475 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001476 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001478
1479 /*
1480 * PEM header and footer were found
1481 */
1482 buflen -= use_len;
1483 buf += use_len;
1484
1485 if( first_error == 0 )
1486 first_error = ret;
1487
Paul Bakker5a5fa922014-09-26 14:53:04 +02001488 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001489 continue;
1490 }
1491 else
1492 break;
1493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001494 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001496 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001497
1498 if( ret != 0 )
1499 {
1500 /*
1501 * Quit parsing on a memory error
1502 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001503 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001504 return( ret );
1505
1506 if( first_error == 0 )
1507 first_error = ret;
1508
1509 total_failed++;
1510 continue;
1511 }
1512
1513 success = 1;
1514 }
1515 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001516
1517 if( success )
1518 return( total_failed );
1519 else if( first_error )
1520 return( first_error );
1521 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001522 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Janos Follath98e28a72016-05-31 14:03:54 +01001523#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001524}
1525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001526#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001527/*
1528 * Load one or more certificates and add them to the chained list
1529 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001531{
Janos Follath865b3eb2019-12-16 11:46:15 +00001532 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001533 size_t n;
1534 unsigned char *buf;
1535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001536 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001537 return( ret );
1538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001539 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001540
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001541 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001542 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001543
1544 return( ret );
1545}
1546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001548{
1549 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001550#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001551 int w_ret;
1552 WCHAR szDir[MAX_PATH];
1553 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001554 char *p;
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001555 size_t len = strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001556
Paul Bakker9af723c2014-05-01 13:03:14 +02001557 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001558 HANDLE hFind;
1559
1560 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001561 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001562
Paul Bakker9af723c2014-05-01 13:03:14 +02001563 memset( szDir, 0, sizeof(szDir) );
1564 memset( filename, 0, MAX_PATH );
1565 memcpy( filename, path, len );
1566 filename[len++] = '\\';
1567 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001568 filename[len++] = '*';
1569
Simon B3c6b18d2016-11-03 01:11:37 +00001570 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001571 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001572 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001574
1575 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001576 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001577 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001578
1579 len = MAX_PATH - len;
1580 do
1581 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001582 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001583
1584 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1585 continue;
1586
Paul Bakker9af723c2014-05-01 13:03:14 +02001587 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001588 lstrlenW( file_data.cFileName ),
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001589 p, (int) len - 1,
Paul Bakker9af723c2014-05-01 13:03:14 +02001590 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001591 if( w_ret == 0 )
Ron Eldor36d90422017-01-09 15:09:16 +02001592 {
1593 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1594 goto cleanup;
1595 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001597 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001598 if( w_ret < 0 )
1599 ret++;
1600 else
1601 ret += w_ret;
1602 }
1603 while( FindNextFileW( hFind, &file_data ) != 0 );
1604
Paul Bakker66d5d072014-06-17 16:39:18 +02001605 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001606 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001607
Ron Eldor36d90422017-01-09 15:09:16 +02001608cleanup:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001609 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001610#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001611 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001612 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001613 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001614 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001615 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001616 DIR *dir = opendir( path );
1617
Paul Bakker66d5d072014-06-17 16:39:18 +02001618 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001619 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001620
Ron Eldor63140682017-01-09 19:27:59 +02001621#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001622 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001623 {
1624 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001625 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001626 }
Ron Eldor63140682017-01-09 19:27:59 +02001627#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001628
Paul Elliottfb91a482021-03-05 14:17:51 +00001629 memset( &sb, 0, sizeof( sb ) );
1630
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001631 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001632 {
Andres AGf9113192016-09-02 14:06:04 +01001633 snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name,
1634 "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001635
Andres AGf9113192016-09-02 14:06:04 +01001636 if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001637 {
Andres AGf9113192016-09-02 14:06:04 +01001638 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1639 goto cleanup;
1640 }
1641 else if( stat( entry_name, &sb ) == -1 )
1642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001644 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001645 }
1646
1647 if( !S_ISREG( sb.st_mode ) )
1648 continue;
1649
1650 // Ignore parse errors
1651 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001652 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001653 if( t_ret < 0 )
1654 ret++;
1655 else
1656 ret += t_ret;
1657 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001658
1659cleanup:
Andres AGf9113192016-09-02 14:06:04 +01001660 closedir( dir );
1661
Ron Eldor63140682017-01-09 19:27:59 +02001662#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001663 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001664 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Ron Eldor63140682017-01-09 19:27:59 +02001665#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001666
Paul Bakkerbe089b02013-10-14 15:51:50 +02001667#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001668
1669 return( ret );
1670}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001671#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001672
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001673/*
1674 * OtherName ::= SEQUENCE {
1675 * type-id OBJECT IDENTIFIER,
1676 * value [0] EXPLICIT ANY DEFINED BY type-id }
1677 *
1678 * HardwareModuleName ::= SEQUENCE {
1679 * hwType OBJECT IDENTIFIER,
1680 * hwSerialNum OCTET STRING }
1681 *
1682 * NOTE: we currently only parse and use otherName of type HwModuleName,
1683 * as defined in RFC 4108.
1684 */
1685static int x509_get_other_name( const mbedtls_x509_buf *subject_alt_name,
1686 mbedtls_x509_san_other_name *other_name )
1687{
Janos Follathab23cd12019-05-09 13:53:57 +01001688 int ret = 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001689 size_t len;
1690 unsigned char *p = subject_alt_name->p;
1691 const unsigned char *end = p + subject_alt_name->len;
1692 mbedtls_x509_buf cur_oid;
1693
1694 if( ( subject_alt_name->tag &
1695 ( MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK ) ) !=
1696 ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ) )
1697 {
1698 /*
1699 * The given subject alternative name is not of type "othername".
1700 */
1701 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
1702 }
1703
1704 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1705 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001706 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001707
1708 cur_oid.tag = MBEDTLS_ASN1_OID;
1709 cur_oid.p = p;
1710 cur_oid.len = len;
1711
1712 /*
1713 * Only HwModuleName is currently supported.
1714 */
1715 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid ) != 0 )
1716 {
1717 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1718 }
1719
Ron Eldor890819a2019-05-13 19:03:04 +03001720 if( p + len >= end )
1721 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001722 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001723 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1724 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001725 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001726 p += len;
1727 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1728 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001729 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001730
1731 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1732 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001733 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001734
1735 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001736 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001737
1738 other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID;
1739 other_name->value.hardware_module_name.oid.p = p;
1740 other_name->value.hardware_module_name.oid.len = len;
1741
Ron Eldor890819a2019-05-13 19:03:04 +03001742 if( p + len >= end )
1743 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001744 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001745 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1746 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001747 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001748 p += len;
1749 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1750 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001751 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001752
1753 other_name->value.hardware_module_name.val.tag = MBEDTLS_ASN1_OCTET_STRING;
1754 other_name->value.hardware_module_name.val.p = p;
1755 other_name->value.hardware_module_name.val.len = len;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001756 p += len;
1757 if( p != end )
1758 {
Ron Eldor890819a2019-05-13 19:03:04 +03001759 mbedtls_platform_zeroize( other_name,
Sébastien Duquette661d7252019-06-23 17:45:26 -04001760 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 Eldorb2dc3fa2019-03-21 13:40:13 +02001763 }
1764 return( 0 );
1765}
1766
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001767static int x509_info_subject_alt_name( char **buf, size_t *size,
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001768 const mbedtls_x509_sequence
1769 *subject_alt_name,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001770 const char *prefix )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001771{
Janos Follath865b3eb2019-12-16 11:46:15 +00001772 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001773 size_t n = *size;
1774 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001775 const mbedtls_x509_sequence *cur = subject_alt_name;
Ron Eldor890819a2019-05-13 19:03:04 +03001776 mbedtls_x509_subject_alternative_name san;
1777 int parse_ret;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001778
1779 while( cur != NULL )
1780 {
Ron Eldor890819a2019-05-13 19:03:04 +03001781 memset( &san, 0, sizeof( san ) );
1782 parse_ret = mbedtls_x509_parse_subject_alt_name( &cur->buf, &san );
1783 if( parse_ret != 0 )
1784 {
1785 if( parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1786 {
1787 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
1788 MBEDTLS_X509_SAFE_SNPRINTF;
1789 }
1790 else
1791 {
1792 ret = mbedtls_snprintf( p, n, "\n%s <malformed>", prefix );
1793 MBEDTLS_X509_SAFE_SNPRINTF;
1794 }
1795 cur = cur->next;
1796 continue;
1797 }
1798
1799 switch( san.type )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001800 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001801 /*
1802 * otherName
1803 */
Ron Eldora2913912019-05-16 16:17:38 +03001804 case MBEDTLS_X509_SAN_OTHER_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001805 {
1806 mbedtls_x509_san_other_name *other_name = &san.san.other_name;
1807
1808 ret = mbedtls_snprintf( p, n, "\n%s otherName :", prefix );
1809 MBEDTLS_X509_SAFE_SNPRINTF;
1810
1811 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME,
1812 &other_name->value.hardware_module_name.oid ) != 0 )
Janos Follath22f605f2019-05-10 10:37:17 +01001813 {
Ron Eldor890819a2019-05-13 19:03:04 +03001814 ret = mbedtls_snprintf( p, n, "\n%s hardware module name :", prefix );
1815 MBEDTLS_X509_SAFE_SNPRINTF;
1816 ret = mbedtls_snprintf( p, n, "\n%s hardware type : ", prefix );
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001817 MBEDTLS_X509_SAFE_SNPRINTF;
1818
Ron Eldor890819a2019-05-13 19:03:04 +03001819 ret = mbedtls_oid_get_numeric_string( p, n, &other_name->value.hardware_module_name.oid );
1820 MBEDTLS_X509_SAFE_SNPRINTF;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001821
Ron Eldor890819a2019-05-13 19:03:04 +03001822 ret = mbedtls_snprintf( p, n, "\n%s hardware serial number : ", prefix );
1823 MBEDTLS_X509_SAFE_SNPRINTF;
1824
1825 if( other_name->value.hardware_module_name.val.len >= n )
1826 {
1827 *p = '\0';
1828 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Janos Follath22f605f2019-05-10 10:37:17 +01001829 }
1830
Ron Eldora2913912019-05-16 16:17:38 +03001831 memcpy( p, other_name->value.hardware_module_name.val.p,
1832 other_name->value.hardware_module_name.val.len );
1833 p += other_name->value.hardware_module_name.val.len;
1834
Ron Eldor890819a2019-05-13 19:03:04 +03001835 n -= other_name->value.hardware_module_name.val.len;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001836
Ron Eldor890819a2019-05-13 19:03:04 +03001837 }/* MBEDTLS_OID_ON_HW_MODULE_NAME */
1838 }
1839 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001840
1841 /*
1842 * dNSName
1843 */
Ron Eldora2913912019-05-16 16:17:38 +03001844 case MBEDTLS_X509_SAN_DNS_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001845 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001846 ret = mbedtls_snprintf( p, n, "\n%s dNSName : ", prefix );
1847 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldor890819a2019-05-13 19:03:04 +03001848 if( san.san.unstructured_name.len >= n )
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001849 {
1850 *p = '\0';
1851 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
1852 }
Ron Eldora2913912019-05-16 16:17:38 +03001853
1854 memcpy( p, san.san.unstructured_name.p, san.san.unstructured_name.len );
1855 p += san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001856 n -= san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001857 }
1858 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001859
1860 /*
Ron Eldor890819a2019-05-13 19:03:04 +03001861 * Type not supported, skip item.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001862 */
1863 default:
Janos Follath22f605f2019-05-10 10:37:17 +01001864 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
1865 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001866 break;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001867 }
1868
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001869 cur = cur->next;
1870 }
1871
1872 *p = '\0';
1873
1874 *size = n;
1875 *buf = p;
1876
1877 return( 0 );
1878}
1879
Ron Eldor890819a2019-05-13 19:03:04 +03001880int mbedtls_x509_parse_subject_alt_name( const mbedtls_x509_buf *san_buf,
1881 mbedtls_x509_subject_alternative_name *san )
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001882{
Janos Follath865b3eb2019-12-16 11:46:15 +00001883 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor890819a2019-05-13 19:03:04 +03001884 switch( san_buf->tag &
1885 ( MBEDTLS_ASN1_TAG_CLASS_MASK |
1886 MBEDTLS_ASN1_TAG_VALUE_MASK ) )
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001887 {
Ron Eldor890819a2019-05-13 19:03:04 +03001888 /*
1889 * otherName
1890 */
1891 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ):
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001892 {
Ron Eldor890819a2019-05-13 19:03:04 +03001893 mbedtls_x509_san_other_name other_name;
1894
1895 ret = x509_get_other_name( san_buf, &other_name );
1896 if( ret != 0 )
1897 return( ret );
1898
1899 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1900 san->type = MBEDTLS_X509_SAN_OTHER_NAME;
1901 memcpy( &san->san.other_name,
1902 &other_name, sizeof( other_name ) );
1903
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001904 }
Ron Eldor890819a2019-05-13 19:03:04 +03001905 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001906
Ron Eldor890819a2019-05-13 19:03:04 +03001907 /*
1908 * dNSName
1909 */
1910 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME ):
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001911 {
Ron Eldor890819a2019-05-13 19:03:04 +03001912 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1913 san->type = MBEDTLS_X509_SAN_DNS_NAME;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001914
Ron Eldor890819a2019-05-13 19:03:04 +03001915 memcpy( &san->san.unstructured_name,
1916 san_buf, sizeof( *san_buf ) );
Janos Follath6c379b42019-05-10 14:17:16 +01001917
Ron Eldor890819a2019-05-13 19:03:04 +03001918 }
1919 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001920
Ron Eldor890819a2019-05-13 19:03:04 +03001921 /*
1922 * Type not supported
1923 */
1924 default:
1925 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1926 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001927 return( 0 );
1928}
1929
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001930#define PRINT_ITEM(i) \
1931 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001932 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001933 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001934 sep = ", "; \
1935 }
1936
1937#define CERT_TYPE(type,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001938 if( ns_cert_type & (type) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001939 PRINT_ITEM( name );
1940
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001941static int x509_info_cert_type( char **buf, size_t *size,
1942 unsigned char ns_cert_type )
1943{
Janos Follath865b3eb2019-12-16 11:46:15 +00001944 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001945 size_t n = *size;
1946 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001947 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001949 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001950 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001951 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
1952 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1953 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001954 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
1955 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1956 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001957
1958 *size = n;
1959 *buf = p;
1960
1961 return( 0 );
1962}
1963
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001964#define KEY_USAGE(code,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001965 if( key_usage & (code) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001966 PRINT_ITEM( name );
1967
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001968static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001969 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001970{
Janos Follath865b3eb2019-12-16 11:46:15 +00001971 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001972 size_t n = *size;
1973 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001974 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001976 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
1977 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001978 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1979 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1980 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001981 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
1982 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001983 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
1984 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001985
1986 *size = n;
1987 *buf = p;
1988
1989 return( 0 );
1990}
1991
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001992static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001993 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001994{
Janos Follath865b3eb2019-12-16 11:46:15 +00001995 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001996 const char *desc;
1997 size_t n = *size;
1998 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001999 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002000 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002001
2002 while( cur != NULL )
2003 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002004 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002005 desc = "???";
2006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002007 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002008 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002009
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002010 sep = ", ";
2011
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002012 cur = cur->next;
2013 }
2014
2015 *size = n;
2016 *buf = p;
2017
2018 return( 0 );
2019}
2020
Ron Eldor74d9acc2019-03-21 14:00:03 +02002021static int x509_info_cert_policies( char **buf, size_t *size,
2022 const mbedtls_x509_sequence *certificate_policies )
2023{
Janos Follath865b3eb2019-12-16 11:46:15 +00002024 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor74d9acc2019-03-21 14:00:03 +02002025 const char *desc;
2026 size_t n = *size;
2027 char *p = *buf;
2028 const mbedtls_x509_sequence *cur = certificate_policies;
2029 const char *sep = "";
2030
2031 while( cur != NULL )
2032 {
2033 if( mbedtls_oid_get_certificate_policies( &cur->buf, &desc ) != 0 )
2034 desc = "???";
2035
2036 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
2037 MBEDTLS_X509_SAFE_SNPRINTF;
2038
2039 sep = ", ";
2040
2041 cur = cur->next;
2042 }
2043
2044 *size = n;
2045 *buf = p;
2046
2047 return( 0 );
2048}
2049
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002050/*
2051 * Return an informational string about the certificate.
2052 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002053#define BEFORE_COLON 18
2054#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002055int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
2056 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002057{
Janos Follath865b3eb2019-12-16 11:46:15 +00002058 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002059 size_t n;
2060 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002061 char key_size_str[BEFORE_COLON];
2062
2063 p = buf;
2064 n = size;
2065
Janos Follath98e28a72016-05-31 14:03:54 +01002066 if( NULL == crt )
2067 {
2068 ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" );
2069 MBEDTLS_X509_SAFE_SNPRINTF;
2070
2071 return( (int) ( size - n ) );
2072 }
2073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002074 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002075 prefix, crt->version );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002076 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002077 ret = mbedtls_snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002078 prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002079 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002081 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002082 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002084 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002085 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002086 ret = mbedtls_x509_dn_gets( p, n, &crt->issuer );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002087 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002089 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002090 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002091 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002092 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002094 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002095 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2096 crt->valid_from.year, crt->valid_from.mon,
2097 crt->valid_from.day, crt->valid_from.hour,
2098 crt->valid_from.min, crt->valid_from.sec );
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%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002102 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2103 crt->valid_to.year, crt->valid_to.mon,
2104 crt->valid_to.day, crt->valid_to.hour,
2105 crt->valid_to.min, crt->valid_to.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002106 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002108 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
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_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02002112 crt->sig_md, crt->sig_opts );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002113 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002114
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002115 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002116 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
2117 mbedtls_pk_get_name( &crt->pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002118 {
2119 return( ret );
2120 }
2121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002122 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +02002123 (int) mbedtls_pk_get_bitlen( &crt->pk ) );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002124 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002125
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002126 /*
2127 * Optional extensions
2128 */
2129
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002130 if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002132 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002133 crt->ca_istrue ? "true" : "false" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002134 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002135
2136 if( crt->max_pathlen > 0 )
2137 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002138 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002139 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002140 }
2141 }
2142
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002143 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002144 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002145 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name :", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002146 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002147
2148 if( ( ret = x509_info_subject_alt_name( &p, &n,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002149 &crt->subject_alt_names,
Ron Eldor6aeae9e2019-05-20 12:00:36 +03002150 prefix ) ) != 0 )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002151 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002152 }
2153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002154 if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002155 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002156 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002157 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002158
2159 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
2160 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002161 }
2162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002163 if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002165 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002166 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002167
2168 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
2169 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002170 }
2171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002172 if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002173 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002174 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002175 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002176
2177 if( ( ret = x509_info_ext_key_usage( &p, &n,
2178 &crt->ext_key_usage ) ) != 0 )
2179 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002180 }
2181
Ron Eldor74d9acc2019-03-21 14:00:03 +02002182 if( crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES )
2183 {
2184 ret = mbedtls_snprintf( p, n, "\n%scertificate policies : ", prefix );
2185 MBEDTLS_X509_SAFE_SNPRINTF;
2186
2187 if( ( ret = x509_info_cert_policies( &p, &n,
2188 &crt->certificate_policies ) ) != 0 )
2189 return( ret );
2190 }
2191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002192 ret = mbedtls_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002193 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002194
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002195 return( (int) ( size - n ) );
2196}
2197
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002198struct x509_crt_verify_string {
2199 int code;
2200 const char *string;
2201};
2202
2203static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002204 { MBEDTLS_X509_BADCERT_EXPIRED, "The certificate validity has expired" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002205 { MBEDTLS_X509_BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" },
2206 { MBEDTLS_X509_BADCERT_CN_MISMATCH, "The certificate Common Name (CN) does not match with the expected CN" },
2207 { MBEDTLS_X509_BADCERT_NOT_TRUSTED, "The certificate is not correctly signed by the trusted CA" },
2208 { MBEDTLS_X509_BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" },
2209 { MBEDTLS_X509_BADCRL_EXPIRED, "The CRL is expired" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002210 { MBEDTLS_X509_BADCERT_MISSING, "Certificate was missing" },
2211 { MBEDTLS_X509_BADCERT_SKIP_VERIFY, "Certificate verification was skipped" },
2212 { MBEDTLS_X509_BADCERT_OTHER, "Other reason (can be used by verify callback)" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002213 { MBEDTLS_X509_BADCERT_FUTURE, "The certificate validity starts in the future" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002214 { MBEDTLS_X509_BADCRL_FUTURE, "The CRL is from the future" },
2215 { MBEDTLS_X509_BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" },
2216 { MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" },
2217 { MBEDTLS_X509_BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" },
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002218 { MBEDTLS_X509_BADCERT_BAD_MD, "The certificate is signed with an unacceptable hash." },
2219 { MBEDTLS_X509_BADCERT_BAD_PK, "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2220 { MBEDTLS_X509_BADCERT_BAD_KEY, "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." },
2221 { MBEDTLS_X509_BADCRL_BAD_MD, "The CRL is signed with an unacceptable hash." },
2222 { MBEDTLS_X509_BADCRL_BAD_PK, "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2223 { MBEDTLS_X509_BADCRL_BAD_KEY, "The CRL is signed with an unacceptable key (eg bad curve, RSA too short)." },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002224 { 0, NULL }
2225};
2226
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}
2254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002255#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
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}
2278#endif
2279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002280#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
2281int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002282 const char *usage_oid,
2283 size_t usage_len )
2284{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002285 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002286
2287 /* Extension is not mandatory, absent means no restriction */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002288 if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002289 return( 0 );
2290
2291 /*
2292 * Look for the requested usage (or wildcard ANY) in our list
2293 */
2294 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
2295 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002296 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002297
2298 if( cur_oid->len == usage_len &&
2299 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
2300 {
2301 return( 0 );
2302 }
2303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002304 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002305 return( 0 );
2306 }
2307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002308 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002309}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002310#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002312#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002313/*
2314 * Return 1 if the certificate is revoked, or 0 otherwise.
2315 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002316int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002317{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002318 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002319
2320 while( cur != NULL && cur->serial.len != 0 )
2321 {
2322 if( crt->serial.len == cur->serial.len &&
2323 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
2324 {
Raoul Strackxa4e86142020-06-15 17:03:13 +02002325 return( 1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002326 }
2327
2328 cur = cur->next;
2329 }
2330
2331 return( 0 );
2332}
2333
2334/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002335 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002336 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002337 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002338static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002339 mbedtls_x509_crl *crl_list,
2340 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002341{
2342 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002343 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2344 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002345
2346 if( ca == NULL )
2347 return( flags );
2348
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002349 while( crl_list != NULL )
2350 {
2351 if( crl_list->version == 0 ||
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002352 x509_name_cmp( &crl_list->issuer, &ca->subject ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002353 {
2354 crl_list = crl_list->next;
2355 continue;
2356 }
2357
2358 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002359 * Check if the CA is configured to sign CRLs
2360 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002361#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002362 if( mbedtls_x509_crt_check_key_usage( ca,
2363 MBEDTLS_X509_KU_CRL_SIGN ) != 0 )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002364 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002365 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002366 break;
2367 }
2368#endif
2369
2370 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002371 * Check if CRL is correctly signed by the trusted CA
2372 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002373 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
2374 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
2375
2376 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
2377 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
2378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002379 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002380 if( mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002381 {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002382 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002383 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002384 break;
2385 }
2386
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002387 if( x509_profile_check_key( profile, &ca->pk ) != 0 )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002388 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002390 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
2391 crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02002392 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002393 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002394 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002395 break;
2396 }
2397
2398 /*
2399 * Check for validity of CRL (Do not drop out)
2400 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002401 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002402 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002403
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002404 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002405 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002406
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002407 /*
2408 * Check if certificate is revoked
2409 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002410 if( mbedtls_x509_crt_is_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002411 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002412 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002413 break;
2414 }
2415
2416 crl_list = crl_list->next;
2417 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002418
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002419 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002420}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002421#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002422
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002423/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002424 * Check the signature of a certificate by its parent
2425 */
2426static int x509_crt_check_signature( const mbedtls_x509_crt *child,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002427 mbedtls_x509_crt *parent,
2428 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002429{
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002430 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002431 size_t hash_len;
2432#if !defined(MBEDTLS_USE_PSA_CRYPTO)
2433 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002434 md_info = mbedtls_md_info_from_type( child->sig_md );
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002435 hash_len = mbedtls_md_get_size( md_info );
Andrzej Kurek8b38ff52018-11-20 03:20:09 -05002436
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002437 /* Note: hash errors can happen only after an internal error */
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002438 if( mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash ) != 0 )
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002439 return( -1 );
2440#else
Jaeden Amero34973232019-02-20 10:32:28 +00002441 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002442 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( child->sig_md );
2443
2444 if( psa_hash_setup( &hash_operation, hash_alg ) != PSA_SUCCESS )
2445 return( -1 );
2446
2447 if( psa_hash_update( &hash_operation, child->tbs.p, child->tbs.len )
2448 != PSA_SUCCESS )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002449 {
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002450 return( -1 );
2451 }
2452
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002453 if( psa_hash_finish( &hash_operation, hash, sizeof( hash ), &hash_len )
2454 != PSA_SUCCESS )
2455 {
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002456 return( -1 );
2457 }
2458#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002459 /* Skip expensive computation on obvious mismatch */
2460 if( ! mbedtls_pk_can_do( &parent->pk, child->sig_pk ) )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002461 return( -1 );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002462
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002463#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002464 if( rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA )
2465 {
2466 return( mbedtls_pk_verify_restartable( &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002467 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002468 child->sig.p, child->sig.len, &rs_ctx->pk ) );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002469 }
2470#else
2471 (void) rs_ctx;
2472#endif
2473
2474 return( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002475 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002476 child->sig.p, child->sig.len ) );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002477}
2478
2479/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002480 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2481 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002482 *
2483 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002484 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002485static int x509_crt_check_parent( const mbedtls_x509_crt *child,
2486 const mbedtls_x509_crt *parent,
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002487 int top )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002488{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002489 int need_ca_bit;
2490
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002491 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02002492 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002493 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002494
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002495 /* Parent must have the basicConstraints CA bit set as a general rule */
2496 need_ca_bit = 1;
2497
2498 /* Exception: v1/v2 certificates that are locally trusted. */
2499 if( top && parent->version < 3 )
2500 need_ca_bit = 0;
2501
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002502 if( need_ca_bit && ! parent->ca_istrue )
2503 return( -1 );
2504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002505#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002506 if( need_ca_bit &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002507 mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002508 {
2509 return( -1 );
2510 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002511#endif
2512
2513 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002514}
2515
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002516/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002517 * Find a suitable parent for child in candidates, or return NULL.
2518 *
2519 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002520 * 1. subject name matches child's issuer
2521 * 2. if necessary, the CA bit is set and key usage allows signing certs
2522 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002523 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002524 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002525 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002526 * If there's a suitable candidate which is also time-valid, return the first
2527 * such. Otherwise, return the first suitable candidate (or NULL if there is
2528 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002529 *
2530 * The rationale for this rule is that someone could have a list of trusted
2531 * roots with two versions on the same root with different validity periods.
2532 * (At least one user reported having such a list and wanted it to just work.)
2533 * The reason we don't just require time-validity is that generally there is
2534 * only one version, and if it's expired we want the flags to state that
2535 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002536 *
2537 * The rationale for rule 3 (signature for trusted roots) is that users might
2538 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002539 * way we select the correct one is by checking the signature (as we don't
2540 * rely on key identifier extensions). (This is one way users might choose to
2541 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002542 *
2543 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002544 * - [in] child: certificate for which we're looking for a parent
2545 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002546 * - [out] r_parent: parent found (or NULL)
2547 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002548 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2549 * of the chain, 0 otherwise
2550 * - [in] path_cnt: number of intermediates seen so far
2551 * - [in] self_cnt: number of self-signed intermediates seen so far
2552 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002553 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002554 *
2555 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002556 * - 0 on success
2557 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002558 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002559static int x509_crt_find_parent_in(
2560 mbedtls_x509_crt *child,
2561 mbedtls_x509_crt *candidates,
2562 mbedtls_x509_crt **r_parent,
2563 int *r_signature_is_good,
2564 int top,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002565 unsigned path_cnt,
2566 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002567 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002568{
Janos Follath865b3eb2019-12-16 11:46:15 +00002569 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002570 mbedtls_x509_crt *parent, *fallback_parent;
Benjamin Kier36050732019-05-30 14:49:17 -04002571 int signature_is_good = 0, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002572
2573#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002574 /* did we have something in progress? */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002575 if( rs_ctx != NULL && rs_ctx->parent != NULL )
2576 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002577 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002578 parent = rs_ctx->parent;
2579 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002580 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002581
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002582 /* clear saved state */
2583 rs_ctx->parent = NULL;
2584 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002585 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002586
2587 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002588 goto check_signature;
2589 }
2590#endif
2591
2592 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002593 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002594
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002595 for( parent = candidates; parent != NULL; parent = parent->next )
2596 {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002597 /* basic parenting skills (name, CA bit, key usage) */
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002598 if( x509_crt_check_parent( child, parent, top ) != 0 )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002599 continue;
2600
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002601 /* +1 because stored max_pathlen is 1 higher that the actual value */
2602 if( parent->max_pathlen > 0 &&
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002603 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt )
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002604 {
2605 continue;
2606 }
2607
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002608 /* Signature */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002609#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2610check_signature:
2611#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002612 ret = x509_crt_check_signature( child, parent, rs_ctx );
2613
2614#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002615 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2616 {
2617 /* save state */
2618 rs_ctx->parent = parent;
2619 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002620 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002621
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002622 return( ret );
2623 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002624#else
2625 (void) ret;
2626#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002627
2628 signature_is_good = ret == 0;
2629 if( top && ! signature_is_good )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002630 continue;
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002631
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002632 /* optional time check */
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002633 if( mbedtls_x509_time_is_past( &parent->valid_to ) ||
2634 mbedtls_x509_time_is_future( &parent->valid_from ) )
2635 {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002636 if( fallback_parent == NULL )
2637 {
2638 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002639 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002640 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002641
2642 continue;
2643 }
2644
Andy Gross1f627142019-01-30 10:25:53 -06002645 *r_parent = parent;
2646 *r_signature_is_good = signature_is_good;
2647
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002648 break;
2649 }
2650
Andy Gross1f627142019-01-30 10:25:53 -06002651 if( parent == NULL )
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002652 {
2653 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002654 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002655 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002656
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002657 return( 0 );
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002658}
2659
2660/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002661 * Find a parent in trusted CAs or the provided chain, or return NULL.
2662 *
2663 * Searches in trusted CAs first, and return the first suitable parent found
2664 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002665 *
2666 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002667 * - [in] child: certificate for which we're looking for a parent, followed
2668 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002669 * - [in] trust_ca: list of locally trusted certificates
2670 * - [out] parent: parent found (or NULL)
2671 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2672 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2673 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2674 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002675 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002676 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002677 *
2678 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002679 * - 0 on success
2680 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002681 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002682static int x509_crt_find_parent(
2683 mbedtls_x509_crt *child,
2684 mbedtls_x509_crt *trust_ca,
2685 mbedtls_x509_crt **parent,
2686 int *parent_is_trusted,
2687 int *signature_is_good,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002688 unsigned path_cnt,
2689 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002690 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002691{
Janos Follath865b3eb2019-12-16 11:46:15 +00002692 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002693 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002694
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002695 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002696
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002697#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002698 /* restore then clear saved state if we have some stored */
2699 if( rs_ctx != NULL && rs_ctx->parent_is_trusted != -1 )
2700 {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002701 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002702 rs_ctx->parent_is_trusted = -1;
2703 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002704#endif
2705
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002706 while( 1 ) {
2707 search_list = *parent_is_trusted ? trust_ca : child->next;
2708
2709 ret = x509_crt_find_parent_in( child, search_list,
2710 parent, signature_is_good,
2711 *parent_is_trusted,
2712 path_cnt, self_cnt, rs_ctx );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002713
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002714#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002715 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2716 {
2717 /* save state */
2718 rs_ctx->parent_is_trusted = *parent_is_trusted;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002719 return( ret );
2720 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002721#else
2722 (void) ret;
2723#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002724
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002725 /* stop here if found or already in second iteration */
2726 if( *parent != NULL || *parent_is_trusted == 0 )
2727 break;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002728
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002729 /* prepare second iteration */
2730 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002731 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002732
2733 /* extra precaution against mistakes in the caller */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002734 if( *parent == NULL )
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002735 {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002736 *parent_is_trusted = 0;
2737 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002738 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002739
2740 return( 0 );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002741}
2742
2743/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002744 * Check if an end-entity certificate is locally trusted
2745 *
2746 * Currently we require such certificates to be self-signed (actually only
2747 * check for self-issued as self-signatures are not checked)
2748 */
2749static int x509_crt_check_ee_locally_trusted(
2750 mbedtls_x509_crt *crt,
2751 mbedtls_x509_crt *trust_ca )
2752{
2753 mbedtls_x509_crt *cur;
2754
2755 /* must be self-issued */
2756 if( x509_name_cmp( &crt->issuer, &crt->subject ) != 0 )
2757 return( -1 );
2758
2759 /* look for an exact match with trusted cert */
2760 for( cur = trust_ca; cur != NULL; cur = cur->next )
2761 {
2762 if( crt->raw.len == cur->raw.len &&
2763 memcmp( crt->raw.p, cur->raw.p, crt->raw.len ) == 0 )
2764 {
2765 return( 0 );
2766 }
2767 }
2768
2769 /* too bad */
2770 return( -1 );
2771}
2772
2773/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002774 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002775 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002776 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2777 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002778 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002779 * such that every cert in the chain is a child of the next one,
2780 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002781 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002782 * Verify that chain and return it with flags for all issues found.
2783 *
2784 * Special cases:
2785 * - EE == Rj -> return a one-element list containing it
2786 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2787 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002788 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002789 * Tests for (aspects of) this function should include at least:
2790 * - trusted EE
2791 * - EE -> trusted root
Antonin Décimo36e89b52019-01-23 15:24:37 +01002792 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002793 * - if relevant: EE untrusted
2794 * - if relevant: EE -> intermediate, untrusted
2795 * with the aspect under test checked at each relevant level (EE, int, root).
2796 * For some aspects longer chains are required, but usually length 2 is
2797 * enough (but length 1 is not in general).
2798 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002799 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002800 * - [in] crt: the cert list EE, C1, ..., Cn
2801 * - [in] trust_ca: the trusted list R1, ..., Rp
2802 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002803 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002804 * Only valid when return value is 0, may contain garbage otherwise!
2805 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002806 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002807 *
2808 * Return value:
2809 * - non-zero if the chain could not be fully built and examined
2810 * - 0 is the chain was successfully built and examined,
2811 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002812 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002813static int x509_crt_verify_chain(
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002814 mbedtls_x509_crt *crt,
2815 mbedtls_x509_crt *trust_ca,
2816 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00002817 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2818 void *p_ca_cb,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002819 const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002820 mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002821 mbedtls_x509_crt_restart_ctx *rs_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002822{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002823 /* Don't initialize any of those variables here, so that the compiler can
2824 * catch potential issues with jumping ahead when restarting */
Janos Follath865b3eb2019-12-16 11:46:15 +00002825 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002826 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002827 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002828 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002829 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002830 int parent_is_trusted;
2831 int child_is_trusted;
2832 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002833 unsigned self_cnt;
Hanno Beckerf53893b2019-03-28 13:45:55 +00002834 mbedtls_x509_crt *cur_trust_ca = NULL;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002835
2836#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2837 /* resume if we had an operation in progress */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002838 if( rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent )
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002839 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002840 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002841 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002842 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002843
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002844 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002845 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002846 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002847 flags = &cur->flags;
2848
2849 goto find_parent;
2850 }
2851#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002852
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002853 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002854 self_cnt = 0;
2855 parent_is_trusted = 0;
2856 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002857
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002858 while( 1 ) {
2859 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002860 cur = &ver_chain->items[ver_chain->len];
2861 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002862 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002863 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002864 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002865
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002866 /* Check time-validity (all certificates) */
2867 if( mbedtls_x509_time_is_past( &child->valid_to ) )
2868 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002869
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002870 if( mbedtls_x509_time_is_future( &child->valid_from ) )
2871 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002872
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002873 /* Stop here for trusted roots (but not for trusted EE certs) */
2874 if( child_is_trusted )
2875 return( 0 );
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002876
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002877 /* Check signature algorithm: MD & PK algs */
2878 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
2879 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002880
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002881 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
2882 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002883
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002884 /* Special case: EE certs that are locally trusted */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002885 if( ver_chain->len == 1 &&
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002886 x509_crt_check_ee_locally_trusted( child, trust_ca ) == 0 )
2887 {
2888 return( 0 );
2889 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002890
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002891#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2892find_parent:
2893#endif
Hanno Beckerf53893b2019-03-28 13:45:55 +00002894
2895 /* Obtain list of potential trusted signers from CA callback,
2896 * or use statically provided list. */
2897#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
2898 if( f_ca_cb != NULL )
2899 {
2900 mbedtls_x509_crt_free( ver_chain->trust_ca_cb_result );
2901 mbedtls_free( ver_chain->trust_ca_cb_result );
2902 ver_chain->trust_ca_cb_result = NULL;
2903
2904 ret = f_ca_cb( p_ca_cb, child, &ver_chain->trust_ca_cb_result );
2905 if( ret != 0 )
2906 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2907
2908 cur_trust_ca = ver_chain->trust_ca_cb_result;
2909 }
2910 else
2911#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2912 {
2913 ((void) f_ca_cb);
2914 ((void) p_ca_cb);
2915 cur_trust_ca = trust_ca;
2916 }
2917
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002918 /* Look for a parent in trusted CAs or up the chain */
Hanno Beckerf53893b2019-03-28 13:45:55 +00002919 ret = x509_crt_find_parent( child, cur_trust_ca, &parent,
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002920 &parent_is_trusted, &signature_is_good,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002921 ver_chain->len - 1, self_cnt, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002922
2923#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002924 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2925 {
2926 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002927 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002928 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002929 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002930
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002931 return( ret );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002932 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002933#else
2934 (void) ret;
2935#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002936
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002937 /* No parent? We're done here */
2938 if( parent == NULL )
2939 {
2940 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2941 return( 0 );
2942 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002943
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002944 /* Count intermediate self-issued (not necessarily self-signed) certs.
2945 * These can occur with some strategies for key rollover, see [SIRO],
2946 * and should be excluded from max_pathlen checks. */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002947 if( ver_chain->len != 1 &&
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002948 x509_name_cmp( &child->issuer, &child->subject ) == 0 )
2949 {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002950 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002951 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002952
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002953 /* path_cnt is 0 for the first intermediate CA,
2954 * and if parent is trusted it's not an intermediate CA */
2955 if( ! parent_is_trusted &&
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002956 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002957 {
2958 /* return immediately to avoid overflow the chain array */
2959 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2960 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002961
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002962 /* signature was checked while searching parent */
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002963 if( ! signature_is_good )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002964 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2965
2966 /* check size of signing key */
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002967 if( x509_profile_check_key( profile, &parent->pk ) != 0 )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002968 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002970#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002971 /* Check trusted CA's CRL for the given crt */
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002972 *flags |= x509_crt_verifycrl( child, parent, ca_crl, profile );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002973#else
2974 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002975#endif
2976
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002977 /* prepare for next iteration */
2978 child = parent;
2979 parent = NULL;
2980 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002981 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002982 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002983}
2984
2985/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002986 * Check for CN match
2987 */
2988static int x509_crt_check_cn( const mbedtls_x509_buf *name,
2989 const char *cn, size_t cn_len )
2990{
2991 /* try exact match */
2992 if( name->len == cn_len &&
2993 x509_memcasecmp( cn, name->p, cn_len ) == 0 )
2994 {
2995 return( 0 );
2996 }
2997
2998 /* try wildcard match */
Manuel Pégourié-Gonnard900fba62017-10-18 14:28:11 +02002999 if( x509_check_wildcard( cn, name ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003000 {
3001 return( 0 );
3002 }
3003
3004 return( -1 );
3005}
3006
3007/*
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003008 * Check for SAN match, see RFC 5280 Section 4.2.1.6
3009 */
3010static int x509_crt_check_san( const mbedtls_x509_buf *name,
3011 const char *cn, size_t cn_len )
3012{
3013 const unsigned char san_type = (unsigned char) name->tag &
3014 MBEDTLS_ASN1_TAG_VALUE_MASK;
3015
3016 /* dNSName */
3017 if( san_type == MBEDTLS_X509_SAN_DNS_NAME )
3018 return( x509_crt_check_cn( name, cn, cn_len ) );
3019
3020 /* (We may handle other types here later.) */
3021
3022 /* Unrecognized type */
3023 return( -1 );
3024}
3025
3026/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003027 * Verify the requested CN - only call this if cn is not NULL!
3028 */
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003029static void x509_crt_verify_name( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003030 const char *cn,
3031 uint32_t *flags )
3032{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003033 const mbedtls_x509_name *name;
3034 const mbedtls_x509_sequence *cur;
3035 size_t cn_len = strlen( cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003036
3037 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
3038 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003039 for( cur = &crt->subject_alt_names; cur != NULL; cur = cur->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003040 {
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003041 if( x509_crt_check_san( &cur->buf, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003042 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003043 }
3044
3045 if( cur == NULL )
3046 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3047 }
3048 else
3049 {
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02003050 for( name = &crt->subject; name != NULL; name = name->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003051 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003052 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, &name->oid ) == 0 &&
3053 x509_crt_check_cn( &name->val, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003054 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003055 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003056 }
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003057 }
3058
3059 if( name == NULL )
3060 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3061 }
3062}
3063
3064/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003065 * Merge the flags for all certs in the chain, after calling callback
3066 */
3067static int x509_crt_merge_flags_with_cb(
3068 uint32_t *flags,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003069 const mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003070 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3071 void *p_vrfy )
3072{
Janos Follath865b3eb2019-12-16 11:46:15 +00003073 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003074 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003075 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003076 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003077
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003078 for( i = ver_chain->len; i != 0; --i )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003079 {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003080 cur = &ver_chain->items[i-1];
3081 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003082
3083 if( NULL != f_vrfy )
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003084 if( ( ret = f_vrfy( p_vrfy, cur->crt, (int) i-1, &cur_flags ) ) != 0 )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003085 return( ret );
3086
3087 *flags |= cur_flags;
3088 }
3089
3090 return( 0 );
3091}
3092
3093/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003094 * Verify the certificate validity, with profile, restartable version
3095 *
3096 * This function:
3097 * - checks the requested CN (if any)
3098 * - checks the type and size of the EE cert's key,
3099 * as that isn't done as part of chain building/verification currently
3100 * - builds and verifies the chain
3101 * - then calls the callback and merges the flags
Hanno Becker3116fb32019-03-28 13:34:42 +00003102 *
3103 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
3104 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
3105 * verification routine to search for trusted signers, and CRLs will
3106 * be disabled. Otherwise, `trust_ca` will be used as the static list
3107 * of trusted signers, and `ca_crl` will be use as the static list
3108 * of CRLs.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003109 */
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003110static int x509_crt_verify_restartable_ca_cb( mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003111 mbedtls_x509_crt *trust_ca,
3112 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003113 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3114 void *p_ca_cb,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003115 const mbedtls_x509_crt_profile *profile,
3116 const char *cn, uint32_t *flags,
3117 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3118 void *p_vrfy,
3119 mbedtls_x509_crt_restart_ctx *rs_ctx )
3120{
Janos Follath865b3eb2019-12-16 11:46:15 +00003121 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003122 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003123 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003124 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003125
3126 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003127 ee_flags = 0;
3128 x509_crt_verify_chain_reset( &ver_chain );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003129
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003130 if( profile == NULL )
3131 {
3132 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3133 goto exit;
3134 }
3135
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003136 /* check name if requested */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003137 if( cn != NULL )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003138 x509_crt_verify_name( crt, cn, &ee_flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003139
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003140 /* Check the type and size of the key */
3141 pk_type = mbedtls_pk_get_type( &crt->pk );
3142
3143 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003144 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003145
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02003146 if( x509_profile_check_key( profile, &crt->pk ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003147 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003148
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003149 /* Check the chain */
Hanno Becker3116fb32019-03-28 13:34:42 +00003150 ret = x509_crt_verify_chain( crt, trust_ca, ca_crl,
3151 f_ca_cb, p_ca_cb, profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003152 &ver_chain, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003153
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003154 if( ret != 0 )
3155 goto exit;
3156
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003157 /* Merge end-entity flags */
3158 ver_chain.items[0].flags |= ee_flags;
3159
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003160 /* Build final flags, calling callback on the way if any */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003161 ret = x509_crt_merge_flags_with_cb( flags, &ver_chain, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003162
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003163exit:
Hanno Beckerf53893b2019-03-28 13:45:55 +00003164
3165#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3166 mbedtls_x509_crt_free( ver_chain.trust_ca_cb_result );
3167 mbedtls_free( ver_chain.trust_ca_cb_result );
3168 ver_chain.trust_ca_cb_result = NULL;
3169#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3170
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003171#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3172 if( rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
3173 mbedtls_x509_crt_restart_free( rs_ctx );
3174#endif
3175
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003176 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3177 * the SSL module for authmode optional, but non-zero return from the
3178 * callback means a fatal error so it shouldn't be ignored */
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003179 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
3180 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3181
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003182 if( ret != 0 )
3183 {
3184 *flags = (uint32_t) -1;
3185 return( ret );
3186 }
3187
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003188 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003189 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003190
3191 return( 0 );
3192}
3193
Hanno Becker3116fb32019-03-28 13:34:42 +00003194
3195/*
3196 * Verify the certificate validity (default profile, not restartable)
3197 */
3198int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
3199 mbedtls_x509_crt *trust_ca,
3200 mbedtls_x509_crl *ca_crl,
3201 const char *cn, uint32_t *flags,
3202 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3203 void *p_vrfy )
3204{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003205 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003206 NULL, NULL,
3207 &mbedtls_x509_crt_profile_default,
3208 cn, flags,
3209 f_vrfy, p_vrfy, NULL ) );
3210}
3211
3212/*
3213 * Verify the certificate validity (user-chosen profile, not restartable)
3214 */
3215int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
3216 mbedtls_x509_crt *trust_ca,
3217 mbedtls_x509_crl *ca_crl,
3218 const mbedtls_x509_crt_profile *profile,
3219 const char *cn, uint32_t *flags,
3220 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3221 void *p_vrfy )
3222{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003223 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003224 NULL, NULL,
3225 profile, cn, flags,
3226 f_vrfy, p_vrfy, NULL ) );
3227}
3228
3229#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3230/*
3231 * Verify the certificate validity (user-chosen profile, CA callback,
3232 * not restartable).
3233 */
Jarno Lamsa31d9db62019-04-01 14:33:49 +03003234int mbedtls_x509_crt_verify_with_ca_cb( mbedtls_x509_crt *crt,
Hanno Becker3116fb32019-03-28 13:34:42 +00003235 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3236 void *p_ca_cb,
3237 const mbedtls_x509_crt_profile *profile,
3238 const char *cn, uint32_t *flags,
3239 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3240 void *p_vrfy )
3241{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003242 return( x509_crt_verify_restartable_ca_cb( crt, NULL, NULL,
Hanno Becker3116fb32019-03-28 13:34:42 +00003243 f_ca_cb, p_ca_cb,
3244 profile, cn, flags,
3245 f_vrfy, p_vrfy, NULL ) );
3246}
3247#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3248
3249int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
3250 mbedtls_x509_crt *trust_ca,
3251 mbedtls_x509_crl *ca_crl,
3252 const mbedtls_x509_crt_profile *profile,
3253 const char *cn, uint32_t *flags,
3254 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3255 void *p_vrfy,
3256 mbedtls_x509_crt_restart_ctx *rs_ctx )
3257{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003258 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003259 NULL, NULL,
3260 profile, cn, flags,
3261 f_vrfy, p_vrfy, rs_ctx ) );
3262}
3263
3264
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003265/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003266 * Initialize a certificate chain
3267 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003268void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02003269{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003270 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02003271}
3272
3273/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003274 * Unallocate all certificate data
3275 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003276void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003277{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003278 mbedtls_x509_crt *cert_cur = crt;
3279 mbedtls_x509_crt *cert_prv;
3280 mbedtls_x509_name *name_cur;
3281 mbedtls_x509_name *name_prv;
3282 mbedtls_x509_sequence *seq_cur;
3283 mbedtls_x509_sequence *seq_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003284
3285 if( crt == NULL )
3286 return;
3287
3288 do
3289 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003290 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003292#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
3293 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003294#endif
3295
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003296 name_cur = cert_cur->issuer.next;
3297 while( name_cur != NULL )
3298 {
3299 name_prv = name_cur;
3300 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003301 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003302 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003303 }
3304
3305 name_cur = cert_cur->subject.next;
3306 while( name_cur != NULL )
3307 {
3308 name_prv = name_cur;
3309 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003310 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003311 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003312 }
3313
3314 seq_cur = cert_cur->ext_key_usage.next;
3315 while( seq_cur != NULL )
3316 {
3317 seq_prv = seq_cur;
3318 seq_cur = seq_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003319 mbedtls_platform_zeroize( seq_prv,
3320 sizeof( mbedtls_x509_sequence ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003321 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003322 }
3323
3324 seq_cur = cert_cur->subject_alt_names.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
Ron Eldor74d9acc2019-03-21 14:00:03 +02003334 seq_cur = cert_cur->certificate_policies.next;
3335 while( seq_cur != NULL )
3336 {
3337 seq_prv = seq_cur;
3338 seq_cur = seq_cur->next;
3339 mbedtls_platform_zeroize( seq_prv,
3340 sizeof( mbedtls_x509_sequence ) );
3341 mbedtls_free( seq_prv );
3342 }
3343
Hanno Becker1a65dcd2019-01-31 08:57:44 +00003344 if( cert_cur->raw.p != NULL && cert_cur->own_buffer )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003345 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003346 mbedtls_platform_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003347 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003348 }
3349
3350 cert_cur = cert_cur->next;
3351 }
3352 while( cert_cur != NULL );
3353
3354 cert_cur = crt;
3355 do
3356 {
3357 cert_prv = cert_cur;
3358 cert_cur = cert_cur->next;
3359
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003360 mbedtls_platform_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003361 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003362 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003363 }
3364 while( cert_cur != NULL );
3365}
3366
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003367#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3368/*
3369 * Initialize a restart context
3370 */
3371void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx )
3372{
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003373 mbedtls_pk_restart_init( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003374
3375 ctx->parent = NULL;
3376 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003377 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003378
3379 ctx->parent_is_trusted = -1;
3380
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003381 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003382 ctx->self_cnt = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003383 x509_crt_verify_chain_reset( &ctx->ver_chain );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003384}
3385
3386/*
3387 * Free the components of a restart context
3388 */
3389void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx )
3390{
3391 if( ctx == NULL )
3392 return;
3393
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003394 mbedtls_pk_restart_free( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003395 mbedtls_x509_crt_restart_init( ctx );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003396}
3397#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003399#endif /* MBEDTLS_X509_CRT_PARSE_C */