2013-09-16 13:49:26 +02:00
|
|
|
/*
|
2014-06-12 22:34:55 +02:00
|
|
|
* X.509 Certidicate Revocation List (CRL) parsing
|
2013-09-16 13:49:26 +02:00
|
|
|
*
|
2015-01-23 10:45:19 +01:00
|
|
|
* Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
|
2013-09-16 13:49:26 +02:00
|
|
|
*
|
2015-03-06 14:17:10 +01:00
|
|
|
* This file is part of mbed TLS (https://tls.mbed.org)
|
2013-09-16 13:49:26 +02:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* The ITU-T X.509 standard defines a certificate format for PKI.
|
|
|
|
*
|
2014-06-12 22:34:55 +02:00
|
|
|
* http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
|
|
|
|
* http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
|
|
|
|
* http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
|
2013-09-16 13:49:26 +02:00
|
|
|
*
|
|
|
|
* http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
|
|
|
|
* http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
|
|
|
|
*/
|
|
|
|
|
2014-04-29 12:39:06 +02:00
|
|
|
#if !defined(POLARSSL_CONFIG_FILE)
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/config.h"
|
2014-04-29 12:39:06 +02:00
|
|
|
#else
|
|
|
|
#include POLARSSL_CONFIG_FILE
|
|
|
|
#endif
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
#if defined(POLARSSL_X509_CRL_PARSE_C)
|
|
|
|
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/x509_crl.h"
|
|
|
|
#include "mbedtls/oid.h"
|
2015-02-06 14:43:58 +01:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
#if defined(POLARSSL_PEM_PARSE_C)
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/pem.h"
|
2013-09-16 13:49:26 +02:00
|
|
|
#endif
|
|
|
|
|
2014-02-01 22:50:26 +01:00
|
|
|
#if defined(POLARSSL_PLATFORM_C)
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/platform.h"
|
2013-09-16 13:49:26 +02:00
|
|
|
#else
|
2015-02-06 14:43:58 +01:00
|
|
|
#include <stdlib.h>
|
2015-02-17 16:46:45 +01:00
|
|
|
#include <stdio.h>
|
2013-09-16 13:49:26 +02:00
|
|
|
#define polarssl_free free
|
2015-01-30 12:00:01 +01:00
|
|
|
#define polarssl_malloc malloc
|
|
|
|
#define polarssl_snprintf snprintf
|
2013-09-16 13:49:26 +02:00
|
|
|
#endif
|
|
|
|
|
2013-10-28 18:48:30 +01:00
|
|
|
#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
|
2013-09-16 13:49:26 +02:00
|
|
|
#include <windows.h>
|
|
|
|
#else
|
|
|
|
#include <time.h>
|
|
|
|
#endif
|
|
|
|
|
2013-10-28 18:48:30 +01:00
|
|
|
#if defined(POLARSSL_FS_IO) || defined(EFIX64) || defined(EFI32)
|
2013-09-16 13:49:26 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#endif
|
|
|
|
|
2014-06-13 17:20:13 +02:00
|
|
|
/* Implementation that should never be optimized out by the compiler */
|
|
|
|
static void polarssl_zeroize( void *v, size_t n ) {
|
|
|
|
volatile unsigned char *p = v; while( n-- ) *p++ = 0;
|
|
|
|
}
|
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
/*
|
|
|
|
* Version ::= INTEGER { v1(0), v2(1) }
|
|
|
|
*/
|
|
|
|
static int x509_crl_get_version( unsigned char **p,
|
|
|
|
const unsigned char *end,
|
|
|
|
int *ver )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
|
|
|
|
{
|
|
|
|
if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
|
|
|
|
{
|
|
|
|
*ver = 0;
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_VERSION + ret );
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* X.509 CRL v2 extensions (no extensions parsed yet.)
|
|
|
|
*/
|
|
|
|
static int x509_get_crl_ext( unsigned char **p,
|
|
|
|
const unsigned char *end,
|
|
|
|
x509_buf *ext )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t len = 0;
|
|
|
|
|
|
|
|
/* Get explicit tag */
|
|
|
|
if( ( ret = x509_get_ext( p, end, ext, 0) ) != 0 )
|
|
|
|
{
|
|
|
|
if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
|
|
|
|
return( 0 );
|
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
while( *p < end )
|
|
|
|
{
|
|
|
|
if( ( ret = asn1_get_tag( p, end, &len,
|
|
|
|
ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
*p += len;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( *p != end )
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
|
2013-09-16 13:49:26 +02:00
|
|
|
POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* X.509 CRL v2 entry extensions (no extensions parsed yet.)
|
|
|
|
*/
|
|
|
|
static int x509_get_crl_entry_ext( unsigned char **p,
|
|
|
|
const unsigned char *end,
|
|
|
|
x509_buf *ext )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t len = 0;
|
|
|
|
|
|
|
|
/* OPTIONAL */
|
2014-06-17 16:39:18 +02:00
|
|
|
if( end <= *p )
|
2013-09-16 13:49:26 +02:00
|
|
|
return( 0 );
|
|
|
|
|
|
|
|
ext->tag = **p;
|
|
|
|
ext->p = *p;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get CRL-entry extension sequence header
|
|
|
|
* crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
|
|
|
|
*/
|
|
|
|
if( ( ret = asn1_get_tag( p, end, &ext->len,
|
|
|
|
ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
|
|
|
|
{
|
|
|
|
if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
|
|
|
|
{
|
|
|
|
ext->p = NULL;
|
|
|
|
return( 0 );
|
|
|
|
}
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
2014-05-01 13:03:14 +02:00
|
|
|
end = *p + ext->len;
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
if( end != *p + ext->len )
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
|
2013-09-16 13:49:26 +02:00
|
|
|
POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
|
|
|
|
|
|
|
|
while( *p < end )
|
|
|
|
{
|
|
|
|
if( ( ret = asn1_get_tag( p, end, &len,
|
|
|
|
ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
*p += len;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( *p != end )
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
|
2013-09-16 13:49:26 +02:00
|
|
|
POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* X.509 CRL Entries
|
|
|
|
*/
|
|
|
|
static int x509_get_entries( unsigned char **p,
|
|
|
|
const unsigned char *end,
|
|
|
|
x509_crl_entry *entry )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t entry_len;
|
|
|
|
x509_crl_entry *cur_entry = entry;
|
|
|
|
|
|
|
|
if( *p == end )
|
|
|
|
return( 0 );
|
|
|
|
|
|
|
|
if( ( ret = asn1_get_tag( p, end, &entry_len,
|
|
|
|
ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
|
|
|
|
{
|
|
|
|
if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
|
|
|
|
return( 0 );
|
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
end = *p + entry_len;
|
|
|
|
|
|
|
|
while( *p < end )
|
|
|
|
{
|
|
|
|
size_t len2;
|
|
|
|
const unsigned char *end2;
|
|
|
|
|
|
|
|
if( ( ret = asn1_get_tag( p, end, &len2,
|
|
|
|
ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
|
|
|
|
{
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
cur_entry->raw.tag = **p;
|
|
|
|
cur_entry->raw.p = *p;
|
|
|
|
cur_entry->raw.len = len2;
|
|
|
|
end2 = *p + len2;
|
|
|
|
|
|
|
|
if( ( ret = x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
|
|
|
|
return( ret );
|
|
|
|
|
2014-05-01 14:18:25 +02:00
|
|
|
if( ( ret = x509_get_time( p, end2,
|
|
|
|
&cur_entry->revocation_date ) ) != 0 )
|
2013-09-16 13:49:26 +02:00
|
|
|
return( ret );
|
|
|
|
|
2014-05-01 14:18:25 +02:00
|
|
|
if( ( ret = x509_get_crl_entry_ext( p, end2,
|
|
|
|
&cur_entry->entry_ext ) ) != 0 )
|
2013-09-16 13:49:26 +02:00
|
|
|
return( ret );
|
|
|
|
|
2014-06-17 16:39:18 +02:00
|
|
|
if( *p < end )
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
|
|
|
cur_entry->next = polarssl_malloc( sizeof( x509_crl_entry ) );
|
|
|
|
|
|
|
|
if( cur_entry->next == NULL )
|
|
|
|
return( POLARSSL_ERR_X509_MALLOC_FAILED );
|
|
|
|
|
2014-11-12 22:27:42 +01:00
|
|
|
memset( cur_entry->next, 0, sizeof( x509_crl_entry ) );
|
2013-09-16 13:49:26 +02:00
|
|
|
cur_entry = cur_entry->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2014-11-19 16:58:28 +01:00
|
|
|
* Parse one CRLs in DER format and append it to the chained list
|
2013-09-16 13:49:26 +02:00
|
|
|
*/
|
2014-11-19 16:58:28 +01:00
|
|
|
int x509_crl_parse_der( x509_crl *chain,
|
|
|
|
const unsigned char *buf, size_t buflen )
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t len;
|
|
|
|
unsigned char *p, *end;
|
2014-06-05 17:02:24 +02:00
|
|
|
x509_buf sig_params1, sig_params2;
|
2014-11-19 16:58:28 +01:00
|
|
|
x509_crl *crl = chain;
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for valid input
|
|
|
|
*/
|
|
|
|
if( crl == NULL || buf == NULL )
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2014-11-19 16:58:28 +01:00
|
|
|
memset( &sig_params1, 0, sizeof( x509_buf ) );
|
|
|
|
memset( &sig_params2, 0, sizeof( x509_buf ) );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Add new CRL on the end of the chain if needed.
|
|
|
|
*/
|
2014-11-19 16:58:28 +01:00
|
|
|
while( crl->version != 0 && crl->next != NULL )
|
|
|
|
crl = crl->next;
|
|
|
|
|
2014-06-17 16:39:18 +02:00
|
|
|
if( crl->version != 0 && crl->next == NULL )
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
2015-02-15 23:35:38 +01:00
|
|
|
crl->next = polarssl_malloc( sizeof( x509_crl ) );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
if( crl->next == NULL )
|
|
|
|
{
|
|
|
|
x509_crl_free( crl );
|
|
|
|
return( POLARSSL_ERR_X509_MALLOC_FAILED );
|
|
|
|
}
|
|
|
|
|
2014-11-12 22:27:42 +01:00
|
|
|
x509_crl_init( crl->next );
|
2013-09-16 13:49:26 +02:00
|
|
|
crl = crl->next;
|
|
|
|
}
|
|
|
|
|
2014-11-19 16:58:28 +01:00
|
|
|
/*
|
|
|
|
* Copy raw DER-encoded CRL
|
|
|
|
*/
|
|
|
|
if( ( p = polarssl_malloc( buflen ) ) == NULL )
|
|
|
|
return( POLARSSL_ERR_X509_MALLOC_FAILED );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2014-11-19 16:58:28 +01:00
|
|
|
memcpy( p, buf, buflen );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
crl->raw.p = p;
|
2014-11-19 16:58:28 +01:00
|
|
|
crl->raw.len = buflen;
|
|
|
|
|
|
|
|
end = p + buflen;
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* CertificateList ::= SEQUENCE {
|
|
|
|
* tbsCertList TBSCertList,
|
|
|
|
* signatureAlgorithm AlgorithmIdentifier,
|
|
|
|
* signatureValue BIT STRING }
|
|
|
|
*/
|
|
|
|
if( ( ret = asn1_get_tag( &p, end, &len,
|
|
|
|
ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
|
|
|
|
{
|
|
|
|
x509_crl_free( crl );
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_FORMAT );
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if( len != (size_t) ( end - p ) )
|
|
|
|
{
|
|
|
|
x509_crl_free( crl );
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_FORMAT +
|
2013-09-16 13:49:26 +02:00
|
|
|
POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* TBSCertList ::= SEQUENCE {
|
|
|
|
*/
|
|
|
|
crl->tbs.p = p;
|
|
|
|
|
|
|
|
if( ( ret = asn1_get_tag( &p, end, &len,
|
|
|
|
ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
|
|
|
|
{
|
|
|
|
x509_crl_free( crl );
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
end = p + len;
|
|
|
|
crl->tbs.len = end - crl->tbs.p;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
|
|
|
|
* -- if present, MUST be v2
|
|
|
|
*
|
|
|
|
* signature AlgorithmIdentifier
|
|
|
|
*/
|
|
|
|
if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
|
2014-06-05 17:02:24 +02:00
|
|
|
( ret = x509_get_alg( &p, end, &crl->sig_oid1, &sig_params1 ) ) != 0 )
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
|
|
|
x509_crl_free( crl );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
crl->version++;
|
|
|
|
|
|
|
|
if( crl->version > 2 )
|
|
|
|
{
|
|
|
|
x509_crl_free( crl );
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_UNKNOWN_VERSION );
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
2014-06-05 17:02:24 +02:00
|
|
|
if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &sig_params1,
|
2014-06-05 15:14:28 +02:00
|
|
|
&crl->sig_md, &crl->sig_pk,
|
|
|
|
&crl->sig_opts ) ) != 0 )
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
|
|
|
x509_crl_free( crl );
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_UNKNOWN_SIG_ALG );
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* issuer Name
|
|
|
|
*/
|
|
|
|
crl->issuer_raw.p = p;
|
|
|
|
|
|
|
|
if( ( ret = asn1_get_tag( &p, end, &len,
|
|
|
|
ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
|
|
|
|
{
|
|
|
|
x509_crl_free( crl );
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
|
|
|
|
{
|
|
|
|
x509_crl_free( crl );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
crl->issuer_raw.len = p - crl->issuer_raw.p;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* thisUpdate Time
|
|
|
|
* nextUpdate Time OPTIONAL
|
|
|
|
*/
|
|
|
|
if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
|
|
|
|
{
|
|
|
|
x509_crl_free( crl );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
|
|
|
|
{
|
2014-06-17 16:39:18 +02:00
|
|
|
if( ret != ( POLARSSL_ERR_X509_INVALID_DATE +
|
2013-09-16 13:49:26 +02:00
|
|
|
POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
|
2014-06-17 16:39:18 +02:00
|
|
|
ret != ( POLARSSL_ERR_X509_INVALID_DATE +
|
2013-09-16 13:49:26 +02:00
|
|
|
POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
|
|
|
|
{
|
|
|
|
x509_crl_free( crl );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* revokedCertificates SEQUENCE OF SEQUENCE {
|
|
|
|
* userCertificate CertificateSerialNumber,
|
|
|
|
* revocationDate Time,
|
|
|
|
* crlEntryExtensions Extensions OPTIONAL
|
|
|
|
* -- if present, MUST be v2
|
|
|
|
* } OPTIONAL
|
|
|
|
*/
|
|
|
|
if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
|
|
|
|
{
|
|
|
|
x509_crl_free( crl );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* crlExtensions EXPLICIT Extensions OPTIONAL
|
|
|
|
* -- if present, MUST be v2
|
|
|
|
*/
|
|
|
|
if( crl->version == 2 )
|
|
|
|
{
|
|
|
|
ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
|
|
|
|
|
|
|
|
if( ret != 0 )
|
|
|
|
{
|
|
|
|
x509_crl_free( crl );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( p != end )
|
|
|
|
{
|
|
|
|
x509_crl_free( crl );
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_FORMAT +
|
2013-09-16 13:49:26 +02:00
|
|
|
POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
|
|
|
|
}
|
|
|
|
|
|
|
|
end = crl->raw.p + crl->raw.len;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* signatureAlgorithm AlgorithmIdentifier,
|
|
|
|
* signatureValue BIT STRING
|
|
|
|
*/
|
2014-06-05 17:02:24 +02:00
|
|
|
if( ( ret = x509_get_alg( &p, end, &crl->sig_oid2, &sig_params2 ) ) != 0 )
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
|
|
|
x509_crl_free( crl );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( crl->sig_oid1.len != crl->sig_oid2.len ||
|
2014-06-05 17:02:24 +02:00
|
|
|
memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 ||
|
|
|
|
sig_params1.len != sig_params2.len ||
|
2014-06-17 16:39:18 +02:00
|
|
|
memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 )
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
|
|
|
x509_crl_free( crl );
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_SIG_MISMATCH );
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
|
|
|
|
{
|
|
|
|
x509_crl_free( crl );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( p != end )
|
|
|
|
{
|
|
|
|
x509_crl_free( crl );
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_FORMAT +
|
2013-09-16 13:49:26 +02:00
|
|
|
POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
|
|
|
|
}
|
|
|
|
|
2014-11-19 16:58:28 +01:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse one or more CRLs and add them to the chained list
|
|
|
|
*/
|
|
|
|
int x509_crl_parse( x509_crl *chain, const unsigned char *buf, size_t buflen )
|
|
|
|
{
|
2014-11-19 19:05:03 +01:00
|
|
|
#if defined(POLARSSL_PEM_PARSE_C)
|
2014-11-19 16:58:28 +01:00
|
|
|
int ret;
|
2014-11-19 19:05:03 +01:00
|
|
|
size_t use_len;
|
|
|
|
pem_context pem;
|
|
|
|
int is_pem = 0;
|
2014-11-19 16:58:28 +01:00
|
|
|
|
|
|
|
if( chain == NULL || buf == NULL )
|
|
|
|
return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
|
|
|
|
|
2014-11-19 19:05:03 +01:00
|
|
|
do
|
|
|
|
{
|
|
|
|
pem_init( &pem );
|
|
|
|
ret = pem_read_buffer( &pem,
|
|
|
|
"-----BEGIN X509 CRL-----",
|
|
|
|
"-----END X509 CRL-----",
|
|
|
|
buf, NULL, 0, &use_len );
|
2014-11-19 16:58:28 +01:00
|
|
|
|
2014-11-19 19:05:03 +01:00
|
|
|
if( ret == 0 )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Was PEM encoded
|
|
|
|
*/
|
|
|
|
is_pem = 1;
|
2014-11-19 16:58:28 +01:00
|
|
|
|
2014-11-19 19:05:03 +01:00
|
|
|
buflen -= use_len;
|
|
|
|
buf += use_len;
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2014-11-19 19:05:03 +01:00
|
|
|
if( ( ret = x509_crl_parse_der( chain,
|
|
|
|
pem.buf, pem.buflen ) ) != 0 )
|
|
|
|
{
|
|
|
|
return( ret );
|
|
|
|
}
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2014-11-19 19:05:03 +01:00
|
|
|
pem_free( &pem );
|
|
|
|
}
|
|
|
|
else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
|
|
|
|
{
|
|
|
|
pem_free( &pem );
|
2014-11-19 16:58:28 +01:00
|
|
|
return( ret );
|
2014-11-19 19:05:03 +01:00
|
|
|
}
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
2014-11-19 19:05:03 +01:00
|
|
|
while( is_pem && buflen > 0 );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2014-11-19 19:05:03 +01:00
|
|
|
if( is_pem )
|
|
|
|
return( 0 );
|
|
|
|
else
|
|
|
|
#endif /* POLARSSL_PEM_PARSE_C */
|
|
|
|
return( x509_crl_parse_der( chain, buf, buflen ) );
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(POLARSSL_FS_IO)
|
|
|
|
/*
|
|
|
|
* Load one or more CRLs and add them to the chained list
|
|
|
|
*/
|
2013-09-18 13:46:23 +02:00
|
|
|
int x509_crl_parse_file( x509_crl *chain, const char *path )
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t n;
|
|
|
|
unsigned char *buf;
|
|
|
|
|
2014-11-21 09:49:43 +01:00
|
|
|
if( ( ret = pk_load_file( path, &buf, &n ) ) != 0 )
|
2013-09-16 13:49:26 +02:00
|
|
|
return( ret );
|
|
|
|
|
2013-09-18 13:46:23 +02:00
|
|
|
ret = x509_crl_parse( chain, buf, n );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2014-06-13 17:20:13 +02:00
|
|
|
polarssl_zeroize( buf, n + 1 );
|
2013-09-16 13:49:26 +02:00
|
|
|
polarssl_free( buf );
|
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
#endif /* POLARSSL_FS_IO */
|
|
|
|
|
2013-10-29 15:22:54 +01:00
|
|
|
#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
|
|
|
|
!defined(EFI32)
|
2013-09-16 13:49:26 +02:00
|
|
|
#include <stdarg.h>
|
|
|
|
|
|
|
|
#if !defined vsnprintf
|
|
|
|
#define vsnprintf _vsnprintf
|
|
|
|
#endif // vsnprintf
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Windows _snprintf and _vsnprintf are not compatible to linux versions.
|
|
|
|
* Result value is not size of buffer needed, but -1 if no fit is possible.
|
|
|
|
*
|
|
|
|
* This fuction tries to 'fix' this by at least suggesting enlarging the
|
|
|
|
* size by 20.
|
|
|
|
*/
|
2014-06-17 16:39:18 +02:00
|
|
|
static int compat_snprintf( char *str, size_t size, const char *format, ... )
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int res = -1;
|
|
|
|
|
|
|
|
va_start( ap, format );
|
|
|
|
|
|
|
|
res = vsnprintf( str, size, format, ap );
|
|
|
|
|
|
|
|
va_end( ap );
|
|
|
|
|
|
|
|
// No quick fix possible
|
2014-06-17 16:39:18 +02:00
|
|
|
if( res < 0 )
|
2013-09-16 13:49:26 +02:00
|
|
|
return( (int) size + 20 );
|
|
|
|
|
2014-06-17 14:06:49 +02:00
|
|
|
return( res );
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#define snprintf compat_snprintf
|
2014-05-01 13:03:14 +02:00
|
|
|
#endif /* _MSC_VER && !snprintf && !EFIX64 && !EFI32 */
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
|
|
|
|
|
2014-06-17 14:06:49 +02:00
|
|
|
#define SAFE_SNPRINTF() \
|
|
|
|
{ \
|
|
|
|
if( ret == -1 ) \
|
|
|
|
return( -1 ); \
|
|
|
|
\
|
2014-06-17 16:39:18 +02:00
|
|
|
if( (unsigned int) ret > n ) { \
|
2014-06-17 14:06:49 +02:00
|
|
|
p[n - 1] = '\0'; \
|
|
|
|
return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL ); \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
n -= (unsigned int) ret; \
|
|
|
|
p += (unsigned int) ret; \
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return an informational string about the certificate.
|
|
|
|
*/
|
|
|
|
#define BEFORE_COLON 14
|
|
|
|
#define BC "14"
|
|
|
|
/*
|
|
|
|
* Return an informational string about the CRL.
|
|
|
|
*/
|
2013-09-18 13:46:23 +02:00
|
|
|
int x509_crl_info( char *buf, size_t size, const char *prefix,
|
|
|
|
const x509_crl *crl )
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t n;
|
|
|
|
char *p;
|
|
|
|
const x509_crl_entry *entry;
|
|
|
|
|
|
|
|
p = buf;
|
|
|
|
n = size;
|
|
|
|
|
2015-01-30 12:00:01 +01:00
|
|
|
ret = polarssl_snprintf( p, n, "%sCRL version : %d",
|
2013-09-16 13:49:26 +02:00
|
|
|
prefix, crl->version );
|
|
|
|
SAFE_SNPRINTF();
|
|
|
|
|
2015-01-30 12:00:01 +01:00
|
|
|
ret = polarssl_snprintf( p, n, "\n%sissuer name : ", prefix );
|
2013-09-16 13:49:26 +02:00
|
|
|
SAFE_SNPRINTF();
|
2013-09-18 11:11:02 +02:00
|
|
|
ret = x509_dn_gets( p, n, &crl->issuer );
|
2013-09-16 13:49:26 +02:00
|
|
|
SAFE_SNPRINTF();
|
|
|
|
|
2015-01-30 12:00:01 +01:00
|
|
|
ret = polarssl_snprintf( p, n, "\n%sthis update : " \
|
2013-09-16 13:49:26 +02:00
|
|
|
"%04d-%02d-%02d %02d:%02d:%02d", prefix,
|
|
|
|
crl->this_update.year, crl->this_update.mon,
|
|
|
|
crl->this_update.day, crl->this_update.hour,
|
|
|
|
crl->this_update.min, crl->this_update.sec );
|
|
|
|
SAFE_SNPRINTF();
|
|
|
|
|
2015-01-30 12:00:01 +01:00
|
|
|
ret = polarssl_snprintf( p, n, "\n%snext update : " \
|
2013-09-16 13:49:26 +02:00
|
|
|
"%04d-%02d-%02d %02d:%02d:%02d", prefix,
|
|
|
|
crl->next_update.year, crl->next_update.mon,
|
|
|
|
crl->next_update.day, crl->next_update.hour,
|
|
|
|
crl->next_update.min, crl->next_update.sec );
|
|
|
|
SAFE_SNPRINTF();
|
|
|
|
|
|
|
|
entry = &crl->entry;
|
|
|
|
|
2015-01-30 12:00:01 +01:00
|
|
|
ret = polarssl_snprintf( p, n, "\n%sRevoked certificates:",
|
2013-09-16 13:49:26 +02:00
|
|
|
prefix );
|
|
|
|
SAFE_SNPRINTF();
|
|
|
|
|
|
|
|
while( entry != NULL && entry->raw.len != 0 )
|
|
|
|
{
|
2015-01-30 12:00:01 +01:00
|
|
|
ret = polarssl_snprintf( p, n, "\n%sserial number: ",
|
2013-09-16 13:49:26 +02:00
|
|
|
prefix );
|
|
|
|
SAFE_SNPRINTF();
|
|
|
|
|
2014-06-17 16:39:18 +02:00
|
|
|
ret = x509_serial_gets( p, n, &entry->serial );
|
2013-09-16 13:49:26 +02:00
|
|
|
SAFE_SNPRINTF();
|
|
|
|
|
2015-01-30 12:00:01 +01:00
|
|
|
ret = polarssl_snprintf( p, n, " revocation date: " \
|
2013-09-16 13:49:26 +02:00
|
|
|
"%04d-%02d-%02d %02d:%02d:%02d",
|
|
|
|
entry->revocation_date.year, entry->revocation_date.mon,
|
|
|
|
entry->revocation_date.day, entry->revocation_date.hour,
|
|
|
|
entry->revocation_date.min, entry->revocation_date.sec );
|
|
|
|
SAFE_SNPRINTF();
|
|
|
|
|
|
|
|
entry = entry->next;
|
|
|
|
}
|
|
|
|
|
2015-01-30 12:00:01 +01:00
|
|
|
ret = polarssl_snprintf( p, n, "\n%ssigned using : ", prefix );
|
2013-09-16 13:49:26 +02:00
|
|
|
SAFE_SNPRINTF();
|
|
|
|
|
2014-06-05 15:41:39 +02:00
|
|
|
ret = x509_sig_alg_gets( p, n, &crl->sig_oid1, crl->sig_pk, crl->sig_md,
|
2014-06-05 17:07:30 +02:00
|
|
|
crl->sig_opts );
|
2013-09-16 13:49:26 +02:00
|
|
|
SAFE_SNPRINTF();
|
|
|
|
|
2015-01-30 12:00:01 +01:00
|
|
|
ret = polarssl_snprintf( p, n, "\n" );
|
2013-09-16 13:49:26 +02:00
|
|
|
SAFE_SNPRINTF();
|
|
|
|
|
|
|
|
return( (int) ( size - n ) );
|
|
|
|
}
|
|
|
|
|
2013-09-18 11:58:25 +02:00
|
|
|
/*
|
|
|
|
* Initialize a CRL chain
|
|
|
|
*/
|
|
|
|
void x509_crl_init( x509_crl *crl )
|
|
|
|
{
|
|
|
|
memset( crl, 0, sizeof(x509_crl) );
|
|
|
|
}
|
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
/*
|
|
|
|
* Unallocate all CRL data
|
|
|
|
*/
|
|
|
|
void x509_crl_free( x509_crl *crl )
|
|
|
|
{
|
|
|
|
x509_crl *crl_cur = crl;
|
|
|
|
x509_crl *crl_prv;
|
|
|
|
x509_name *name_cur;
|
|
|
|
x509_name *name_prv;
|
|
|
|
x509_crl_entry *entry_cur;
|
|
|
|
x509_crl_entry *entry_prv;
|
|
|
|
|
|
|
|
if( crl == NULL )
|
|
|
|
return;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2014-06-06 16:42:37 +02:00
|
|
|
#if defined(POLARSSL_X509_RSASSA_PSS_SUPPORT)
|
2014-06-05 15:14:28 +02:00
|
|
|
polarssl_free( crl_cur->sig_opts );
|
|
|
|
#endif
|
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
name_cur = crl_cur->issuer.next;
|
|
|
|
while( name_cur != NULL )
|
|
|
|
{
|
|
|
|
name_prv = name_cur;
|
|
|
|
name_cur = name_cur->next;
|
2014-06-13 17:20:13 +02:00
|
|
|
polarssl_zeroize( name_prv, sizeof( x509_name ) );
|
2013-09-16 13:49:26 +02:00
|
|
|
polarssl_free( name_prv );
|
|
|
|
}
|
|
|
|
|
|
|
|
entry_cur = crl_cur->entry.next;
|
|
|
|
while( entry_cur != NULL )
|
|
|
|
{
|
|
|
|
entry_prv = entry_cur;
|
|
|
|
entry_cur = entry_cur->next;
|
2014-06-13 17:20:13 +02:00
|
|
|
polarssl_zeroize( entry_prv, sizeof( x509_crl_entry ) );
|
2013-09-16 13:49:26 +02:00
|
|
|
polarssl_free( entry_prv );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( crl_cur->raw.p != NULL )
|
|
|
|
{
|
2014-06-13 17:20:13 +02:00
|
|
|
polarssl_zeroize( crl_cur->raw.p, crl_cur->raw.len );
|
2013-09-16 13:49:26 +02:00
|
|
|
polarssl_free( crl_cur->raw.p );
|
|
|
|
}
|
|
|
|
|
|
|
|
crl_cur = crl_cur->next;
|
|
|
|
}
|
|
|
|
while( crl_cur != NULL );
|
|
|
|
|
|
|
|
crl_cur = crl;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
crl_prv = crl_cur;
|
|
|
|
crl_cur = crl_cur->next;
|
|
|
|
|
2014-06-13 17:20:13 +02:00
|
|
|
polarssl_zeroize( crl_prv, sizeof( x509_crl ) );
|
2013-09-16 13:49:26 +02:00
|
|
|
if( crl_prv != crl )
|
|
|
|
polarssl_free( crl_prv );
|
|
|
|
}
|
|
|
|
while( crl_cur != NULL );
|
|
|
|
}
|
|
|
|
|
2014-05-01 13:03:14 +02:00
|
|
|
#endif /* POLARSSL_X509_CRL_PARSE_C */
|