2 * X.509 Certidicate Revocation List (CRL) parsing
4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * This file is part of mbed TLS (https://tls.mbed.org)
22 * The ITU-T X.509 standard defines a certificate format for PKI.
24 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
25 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
26 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
28 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
29 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
32 #if !defined(MBEDTLS_CONFIG_FILE)
33 #include "mbedtls/config.h"
35 #include MBEDTLS_CONFIG_FILE
38 #if defined(MBEDTLS_X509_CRL_PARSE_C)
40 #include "mbedtls/x509_crl.h"
41 #include "mbedtls/oid.h"
42 #include "mbedtls/platform_util.h"
46 #if defined(MBEDTLS_PEM_PARSE_C)
47 #include "mbedtls/pem.h"
50 #if defined(MBEDTLS_PLATFORM_C)
51 #include "mbedtls/platform.h"
55 #define mbedtls_free free
56 #define mbedtls_calloc calloc
57 #define mbedtls_snprintf snprintf
60 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
66 #if defined(MBEDTLS_FS_IO) || defined(EFIX64) || defined(EFI32)
71 * Version ::= INTEGER { v1(0), v2(1) }
73 static int x509_crl_get_version( unsigned char **p,
74 const unsigned char *end,
79 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
81 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
87 return( MBEDTLS_ERR_X509_INVALID_VERSION + ret );
94 * X.509 CRL v2 extensions
96 * We currently don't parse any extension's content, but we do check that the
97 * list of extensions is well-formed and abort on critical extensions (that
98 * are unsupported as we don't support any extension so far)
100 static int x509_get_crl_ext( unsigned char **p,
101 const unsigned char *end,
102 mbedtls_x509_buf *ext )
107 * crlExtensions [0] EXPLICIT Extensions OPTIONAL
108 * -- if present, version MUST be v2
110 if( ( ret = mbedtls_x509_get_ext( p, end, ext, 0 ) ) != 0 )
112 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
121 * Extension ::= SEQUENCE {
122 * extnID OBJECT IDENTIFIER,
123 * critical BOOLEAN DEFAULT FALSE,
124 * extnValue OCTET STRING }
127 const unsigned char *end_ext_data;
130 /* Get enclosing sequence tag */
131 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
132 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
133 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
135 end_ext_data = *p + len;
137 /* Get OID (currently ignored) */
138 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
139 MBEDTLS_ASN1_OID ) ) != 0 )
141 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
145 /* Get optional critical */
146 if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data,
147 &is_critical ) ) != 0 &&
148 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
150 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
153 /* Data should be octet string type */
154 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
155 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
156 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
158 /* Ignore data so far and just check its length */
160 if( *p != end_ext_data )
161 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
162 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
164 /* Abort on (unsupported) critical extensions */
166 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
167 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
171 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
172 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
178 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
180 static int x509_get_crl_entry_ext( unsigned char **p,
181 const unsigned char *end,
182 mbedtls_x509_buf *ext )
195 * Get CRL-entry extension sequence header
196 * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
198 if( ( ret = mbedtls_asn1_get_tag( p, end, &ext->len,
199 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
201 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
206 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
211 if( end != *p + ext->len )
212 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
213 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
217 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
218 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
219 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
225 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
226 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
234 static int x509_get_entries( unsigned char **p,
235 const unsigned char *end,
236 mbedtls_x509_crl_entry *entry )
240 mbedtls_x509_crl_entry *cur_entry = entry;
245 if( ( ret = mbedtls_asn1_get_tag( p, end, &entry_len,
246 MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED ) ) != 0 )
248 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
254 end = *p + entry_len;
259 const unsigned char *end2;
261 if( ( ret = mbedtls_asn1_get_tag( p, end, &len2,
262 MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED ) ) != 0 )
267 cur_entry->raw.tag = **p;
268 cur_entry->raw.p = *p;
269 cur_entry->raw.len = len2;
272 if( ( ret = mbedtls_x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
275 if( ( ret = mbedtls_x509_get_time( p, end2,
276 &cur_entry->revocation_date ) ) != 0 )
279 if( ( ret = x509_get_crl_entry_ext( p, end2,
280 &cur_entry->entry_ext ) ) != 0 )
285 cur_entry->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crl_entry ) );
287 if( cur_entry->next == NULL )
288 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
290 cur_entry = cur_entry->next;
298 * Parse one CRLs in DER format and append it to the chained list
300 int mbedtls_x509_crl_parse_der( mbedtls_x509_crl *chain,
301 const unsigned char *buf, size_t buflen )
305 unsigned char *p = NULL, *end = NULL;
306 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
307 mbedtls_x509_crl *crl = chain;
310 * Check for valid input
312 if( crl == NULL || buf == NULL )
313 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
315 memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) );
316 memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) );
317 memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) );
320 * Add new CRL on the end of the chain if needed.
322 while( crl->version != 0 && crl->next != NULL )
325 if( crl->version != 0 && crl->next == NULL )
327 crl->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crl ) );
329 if( crl->next == NULL )
331 mbedtls_x509_crl_free( crl );
332 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
335 mbedtls_x509_crl_init( crl->next );
340 * Copy raw DER-encoded CRL
343 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
345 p = mbedtls_calloc( 1, buflen );
347 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
349 memcpy( p, buf, buflen );
352 crl->raw.len = buflen;
357 * CertificateList ::= SEQUENCE {
358 * tbsCertList TBSCertList,
359 * signatureAlgorithm AlgorithmIdentifier,
360 * signatureValue BIT STRING }
362 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
363 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
365 mbedtls_x509_crl_free( crl );
366 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
369 if( len != (size_t) ( end - p ) )
371 mbedtls_x509_crl_free( crl );
372 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
373 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
377 * TBSCertList ::= SEQUENCE {
381 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
382 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
384 mbedtls_x509_crl_free( crl );
385 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
389 crl->tbs.len = end - crl->tbs.p;
392 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
393 * -- if present, MUST be v2
395 * signature AlgorithmIdentifier
397 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
398 ( ret = mbedtls_x509_get_alg( &p, end, &crl->sig_oid, &sig_params1 ) ) != 0 )
400 mbedtls_x509_crl_free( crl );
404 if( crl->version < 0 || crl->version > 1 )
406 mbedtls_x509_crl_free( crl );
407 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
412 if( ( ret = mbedtls_x509_get_sig_alg( &crl->sig_oid, &sig_params1,
413 &crl->sig_md, &crl->sig_pk,
414 &crl->sig_opts ) ) != 0 )
416 mbedtls_x509_crl_free( crl );
417 return( MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG );
423 crl->issuer_raw.p = p;
425 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
426 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
428 mbedtls_x509_crl_free( crl );
429 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
432 if( ( ret = mbedtls_x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
434 mbedtls_x509_crl_free( crl );
438 crl->issuer_raw.len = p - crl->issuer_raw.p;
442 * nextUpdate Time OPTIONAL
444 if( ( ret = mbedtls_x509_get_time( &p, end, &crl->this_update ) ) != 0 )
446 mbedtls_x509_crl_free( crl );
450 if( ( ret = mbedtls_x509_get_time( &p, end, &crl->next_update ) ) != 0 )
452 if( ret != ( MBEDTLS_ERR_X509_INVALID_DATE +
453 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) &&
454 ret != ( MBEDTLS_ERR_X509_INVALID_DATE +
455 MBEDTLS_ERR_ASN1_OUT_OF_DATA ) )
457 mbedtls_x509_crl_free( crl );
463 * revokedCertificates SEQUENCE OF SEQUENCE {
464 * userCertificate CertificateSerialNumber,
465 * revocationDate Time,
466 * crlEntryExtensions Extensions OPTIONAL
467 * -- if present, MUST be v2
470 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
472 mbedtls_x509_crl_free( crl );
477 * crlExtensions EXPLICIT Extensions OPTIONAL
478 * -- if present, MUST be v2
480 if( crl->version == 2 )
482 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
486 mbedtls_x509_crl_free( crl );
493 mbedtls_x509_crl_free( crl );
494 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
495 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
498 end = crl->raw.p + crl->raw.len;
501 * signatureAlgorithm AlgorithmIdentifier,
502 * signatureValue BIT STRING
504 if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
506 mbedtls_x509_crl_free( crl );
510 if( crl->sig_oid.len != sig_oid2.len ||
511 memcmp( crl->sig_oid.p, sig_oid2.p, crl->sig_oid.len ) != 0 ||
512 sig_params1.len != sig_params2.len ||
513 ( sig_params1.len != 0 &&
514 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
516 mbedtls_x509_crl_free( crl );
517 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
520 if( ( ret = mbedtls_x509_get_sig( &p, end, &crl->sig ) ) != 0 )
522 mbedtls_x509_crl_free( crl );
528 mbedtls_x509_crl_free( crl );
529 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
530 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
537 * Parse one or more CRLs and add them to the chained list
539 int mbedtls_x509_crl_parse( mbedtls_x509_crl *chain, const unsigned char *buf, size_t buflen )
541 #if defined(MBEDTLS_PEM_PARSE_C)
544 mbedtls_pem_context pem;
547 if( chain == NULL || buf == NULL )
548 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
552 mbedtls_pem_init( &pem );
554 // Avoid calling mbedtls_pem_read_buffer() on non-null-terminated
556 if( buflen == 0 || buf[buflen - 1] != '\0' )
557 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
559 ret = mbedtls_pem_read_buffer( &pem,
560 "-----BEGIN X509 CRL-----",
561 "-----END X509 CRL-----",
562 buf, NULL, 0, &use_len );
574 if( ( ret = mbedtls_x509_crl_parse_der( chain,
575 pem.buf, pem.buflen ) ) != 0 )
577 mbedtls_pem_free( &pem );
583 mbedtls_pem_free( &pem );
587 mbedtls_pem_free( &pem );
589 /* In the PEM case, buflen is 1 at the end, for the terminated NULL byte.
590 * And a valid CRL cannot be less than 1 byte anyway. */
591 while( is_pem && buflen > 1 );
596 #endif /* MBEDTLS_PEM_PARSE_C */
597 return( mbedtls_x509_crl_parse_der( chain, buf, buflen ) );
600 #if defined(MBEDTLS_FS_IO)
602 * Load one or more CRLs and add them to the chained list
604 int mbedtls_x509_crl_parse_file( mbedtls_x509_crl *chain, const char *path )
610 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
613 ret = mbedtls_x509_crl_parse( chain, buf, n );
615 mbedtls_platform_zeroize( buf, n );
620 #endif /* MBEDTLS_FS_IO */
623 * Return an informational string about the certificate.
625 #define BEFORE_COLON 14
628 * Return an informational string about the CRL.
630 int mbedtls_x509_crl_info( char *buf, size_t size, const char *prefix,
631 const mbedtls_x509_crl *crl )
636 const mbedtls_x509_crl_entry *entry;
641 ret = mbedtls_snprintf( p, n, "%sCRL version : %d",
642 prefix, crl->version );
643 MBEDTLS_X509_SAFE_SNPRINTF;
645 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
646 MBEDTLS_X509_SAFE_SNPRINTF;
647 ret = mbedtls_x509_dn_gets( p, n, &crl->issuer );
648 MBEDTLS_X509_SAFE_SNPRINTF;
650 ret = mbedtls_snprintf( p, n, "\n%sthis update : " \
651 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
652 crl->this_update.year, crl->this_update.mon,
653 crl->this_update.day, crl->this_update.hour,
654 crl->this_update.min, crl->this_update.sec );
655 MBEDTLS_X509_SAFE_SNPRINTF;
657 ret = mbedtls_snprintf( p, n, "\n%snext update : " \
658 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
659 crl->next_update.year, crl->next_update.mon,
660 crl->next_update.day, crl->next_update.hour,
661 crl->next_update.min, crl->next_update.sec );
662 MBEDTLS_X509_SAFE_SNPRINTF;
666 ret = mbedtls_snprintf( p, n, "\n%sRevoked certificates:",
668 MBEDTLS_X509_SAFE_SNPRINTF;
670 while( entry != NULL && entry->raw.len != 0 )
672 ret = mbedtls_snprintf( p, n, "\n%sserial number: ",
674 MBEDTLS_X509_SAFE_SNPRINTF;
676 ret = mbedtls_x509_serial_gets( p, n, &entry->serial );
677 MBEDTLS_X509_SAFE_SNPRINTF;
679 ret = mbedtls_snprintf( p, n, " revocation date: " \
680 "%04d-%02d-%02d %02d:%02d:%02d",
681 entry->revocation_date.year, entry->revocation_date.mon,
682 entry->revocation_date.day, entry->revocation_date.hour,
683 entry->revocation_date.min, entry->revocation_date.sec );
684 MBEDTLS_X509_SAFE_SNPRINTF;
689 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
690 MBEDTLS_X509_SAFE_SNPRINTF;
692 ret = mbedtls_x509_sig_alg_gets( p, n, &crl->sig_oid, crl->sig_pk, crl->sig_md,
694 MBEDTLS_X509_SAFE_SNPRINTF;
696 ret = mbedtls_snprintf( p, n, "\n" );
697 MBEDTLS_X509_SAFE_SNPRINTF;
699 return( (int) ( size - n ) );
703 * Initialize a CRL chain
705 void mbedtls_x509_crl_init( mbedtls_x509_crl *crl )
707 memset( crl, 0, sizeof(mbedtls_x509_crl) );
711 * Unallocate all CRL data
713 void mbedtls_x509_crl_free( mbedtls_x509_crl *crl )
715 mbedtls_x509_crl *crl_cur = crl;
716 mbedtls_x509_crl *crl_prv;
717 mbedtls_x509_name *name_cur;
718 mbedtls_x509_name *name_prv;
719 mbedtls_x509_crl_entry *entry_cur;
720 mbedtls_x509_crl_entry *entry_prv;
727 #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
728 mbedtls_free( crl_cur->sig_opts );
731 name_cur = crl_cur->issuer.next;
732 while( name_cur != NULL )
735 name_cur = name_cur->next;
736 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
737 mbedtls_free( name_prv );
740 entry_cur = crl_cur->entry.next;
741 while( entry_cur != NULL )
743 entry_prv = entry_cur;
744 entry_cur = entry_cur->next;
745 mbedtls_platform_zeroize( entry_prv,
746 sizeof( mbedtls_x509_crl_entry ) );
747 mbedtls_free( entry_prv );
750 if( crl_cur->raw.p != NULL )
752 mbedtls_platform_zeroize( crl_cur->raw.p, crl_cur->raw.len );
753 mbedtls_free( crl_cur->raw.p );
756 crl_cur = crl_cur->next;
758 while( crl_cur != NULL );
764 crl_cur = crl_cur->next;
766 mbedtls_platform_zeroize( crl_prv, sizeof( mbedtls_x509_crl ) );
768 mbedtls_free( crl_prv );
770 while( crl_cur != NULL );
773 #endif /* MBEDTLS_X509_CRL_PARSE_C */