2009-01-03 22:22:43 +01:00
|
|
|
/*
|
2014-06-12 22:34:55 +02:00
|
|
|
* X.509 certificate parsing and verification
|
2009-01-03 22:22:43 +01:00
|
|
|
*
|
2014-02-01 22:50:26 +01:00
|
|
|
* Copyright (C) 2006-2014, Brainspark B.V.
|
2010-07-18 22:36:00 +02:00
|
|
|
*
|
|
|
|
* This file is part of PolarSSL (http://www.polarssl.org)
|
2010-07-18 12:13:04 +02:00
|
|
|
* Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
|
2010-07-18 22:36:00 +02:00
|
|
|
*
|
2009-07-28 19:23:11 +02:00
|
|
|
* All rights reserved.
|
2009-01-04 17:27:10 +01:00
|
|
|
*
|
2009-01-03 22:22:43 +01: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.
|
|
|
|
*/
|
|
|
|
/*
|
2012-02-16 16:28:14 +01:00
|
|
|
* The ITU-T X.509 standard defines a certificate format for PKI.
|
2009-01-03 22:22:43 +01:00
|
|
|
*
|
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)
|
2009-01-03 22:22:43 +01: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)
|
2009-01-03 22:51:57 +01:00
|
|
|
#include "polarssl/config.h"
|
2014-04-29 12:39:06 +02:00
|
|
|
#else
|
|
|
|
#include POLARSSL_CONFIG_FILE
|
|
|
|
#endif
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
#if defined(POLARSSL_X509_CRT_PARSE_C)
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
#include "polarssl/x509_crt.h"
|
2013-04-07 22:00:46 +02:00
|
|
|
#include "polarssl/oid.h"
|
2013-09-15 20:43:33 +02:00
|
|
|
#if defined(POLARSSL_PEM_PARSE_C)
|
2011-02-12 15:30:57 +01:00
|
|
|
#include "polarssl/pem.h"
|
2013-09-15 20:43:33 +02:00
|
|
|
#endif
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2014-02-01 22:50:26 +01:00
|
|
|
#if defined(POLARSSL_PLATFORM_C)
|
|
|
|
#include "polarssl/platform.h"
|
2013-07-03 13:37:05 +02:00
|
|
|
#else
|
|
|
|
#define polarssl_malloc malloc
|
|
|
|
#define polarssl_free free
|
|
|
|
#endif
|
|
|
|
|
2013-11-28 17:11:54 +01:00
|
|
|
#if defined(POLARSSL_THREADING_C)
|
|
|
|
#include "polarssl/threading.h"
|
|
|
|
#endif
|
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2013-10-28 18:48:30 +01:00
|
|
|
#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
|
2011-11-18 15:26:47 +01:00
|
|
|
#include <windows.h>
|
|
|
|
#else
|
2009-01-03 22:22:43 +01:00
|
|
|
#include <time.h>
|
2011-11-18 15:26:47 +01:00
|
|
|
#endif
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2013-10-28 18:48:30 +01:00
|
|
|
#if defined(EFIX64) || defined(EFI32)
|
|
|
|
#include <stdio.h>
|
|
|
|
#endif
|
|
|
|
|
2011-04-25 17:28:35 +02:00
|
|
|
#if defined(POLARSSL_FS_IO)
|
|
|
|
#include <stdio.h>
|
2014-04-04 15:08:20 +02:00
|
|
|
#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
|
2012-06-04 14:46:42 +02:00
|
|
|
#include <sys/types.h>
|
2013-06-24 19:22:42 +02:00
|
|
|
#include <sys/stat.h>
|
2012-06-04 14:46:42 +02:00
|
|
|
#include <dirent.h>
|
|
|
|
#endif
|
2011-04-25 17:28:35 +02:00
|
|
|
#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;
|
|
|
|
}
|
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
|
|
|
* Version ::= INTEGER { v1(0), v2(1), v3(2) }
|
|
|
|
*/
|
|
|
|
static int x509_get_version( unsigned char **p,
|
2010-03-16 22:09:09 +01:00
|
|
|
const unsigned char *end,
|
2009-01-03 22:22:43 +01:00
|
|
|
int *ver )
|
|
|
|
{
|
2011-04-24 10:57:21 +02:00
|
|
|
int ret;
|
|
|
|
size_t len;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
if( ( ret = asn1_get_tag( p, end, &len,
|
|
|
|
ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) != 0 )
|
|
|
|
{
|
2009-01-03 22:51:57 +01:00
|
|
|
if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
|
2011-10-19 16:15:17 +02:00
|
|
|
{
|
|
|
|
*ver = 0;
|
|
|
|
return( 0 );
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
end = *p + len;
|
|
|
|
|
|
|
|
if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_VERSION + ret );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
if( *p != end )
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_VERSION +
|
2009-01-03 22:51:57 +01:00
|
|
|
POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2009-05-02 17:13:40 +02:00
|
|
|
/*
|
|
|
|
* Validity ::= SEQUENCE {
|
|
|
|
* notBefore Time,
|
|
|
|
* notAfter Time }
|
|
|
|
*/
|
|
|
|
static int x509_get_dates( unsigned char **p,
|
2010-03-16 22:09:09 +01:00
|
|
|
const unsigned char *end,
|
2009-05-02 17:13:40 +02:00
|
|
|
x509_time *from,
|
|
|
|
x509_time *to )
|
|
|
|
{
|
2011-04-24 10:57:21 +02:00
|
|
|
int ret;
|
|
|
|
size_t len;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2009-05-02 17:13:40 +02:00
|
|
|
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_DATE + ret );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2009-05-02 17:13:40 +02:00
|
|
|
end = *p + len;
|
|
|
|
|
2010-02-18 22:26:15 +01:00
|
|
|
if( ( ret = x509_get_time( p, end, from ) ) != 0 )
|
2009-05-02 17:13:40 +02:00
|
|
|
return( ret );
|
|
|
|
|
2010-02-18 22:26:15 +01:00
|
|
|
if( ( ret = x509_get_time( p, end, to ) ) != 0 )
|
2009-05-02 17:13:40 +02:00
|
|
|
return( ret );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
if( *p != end )
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_DATE +
|
2009-01-03 22:51:57 +01:00
|
|
|
POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* X.509 v2/v3 unique identifier (not parsed)
|
|
|
|
*/
|
|
|
|
static int x509_get_uid( unsigned char **p,
|
2010-03-16 22:09:09 +01:00
|
|
|
const unsigned char *end,
|
2009-01-03 22:22:43 +01:00
|
|
|
x509_buf *uid, int n )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if( *p == end )
|
|
|
|
return( 0 );
|
|
|
|
|
|
|
|
uid->tag = **p;
|
|
|
|
|
|
|
|
if( ( ret = asn1_get_tag( p, end, &uid->len,
|
|
|
|
ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
|
|
|
|
{
|
2009-01-03 22:51:57 +01:00
|
|
|
if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
|
2009-01-03 22:22:43 +01:00
|
|
|
return( 0 );
|
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
uid->p = *p;
|
|
|
|
*p += uid->len;
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
static int x509_get_basic_constraints( unsigned char **p,
|
|
|
|
const unsigned char *end,
|
|
|
|
int *ca_istrue,
|
|
|
|
int *max_pathlen )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2011-04-24 10:57:21 +02:00
|
|
|
int ret;
|
|
|
|
size_t len;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
/*
|
2013-09-16 13:49:26 +02:00
|
|
|
* BasicConstraints ::= SEQUENCE {
|
|
|
|
* cA BOOLEAN DEFAULT FALSE,
|
|
|
|
* pathLenConstraint INTEGER (0..MAX) OPTIONAL }
|
2009-01-03 22:22:43 +01:00
|
|
|
*/
|
2013-09-16 13:49:26 +02:00
|
|
|
*ca_istrue = 0; /* DEFAULT FALSE */
|
|
|
|
*max_pathlen = 0; /* endless */
|
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
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 );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
if( *p == end )
|
2014-06-17 14:06:49 +02:00
|
|
|
return( 0 );
|
2009-05-02 17:13:40 +02:00
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
|
2009-05-02 17:13:40 +02:00
|
|
|
{
|
|
|
|
if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
|
2013-09-16 13:49:26 +02:00
|
|
|
ret = asn1_get_int( p, end, ca_istrue );
|
2009-05-02 17:13:40 +02:00
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
if( ret != 0 )
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
|
2009-05-02 17:13:40 +02:00
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
if( *ca_istrue != 0 )
|
|
|
|
*ca_istrue = 1;
|
2009-05-02 17:13:40 +02:00
|
|
|
}
|
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
if( *p == end )
|
2014-06-17 14:06:49 +02:00
|
|
|
return( 0 );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2009-05-02 17:13:40 +02:00
|
|
|
if( *p != end )
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
|
2009-05-02 17:13:40 +02:00
|
|
|
POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
|
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
(*max_pathlen)++;
|
|
|
|
|
2014-06-17 14:06:49 +02:00
|
|
|
return( 0 );
|
2009-05-02 17:13:40 +02:00
|
|
|
}
|
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
static int x509_get_ns_cert_type( unsigned char **p,
|
|
|
|
const unsigned char *end,
|
|
|
|
unsigned char *ns_cert_type)
|
2011-10-12 11:58:41 +02:00
|
|
|
{
|
|
|
|
int ret;
|
2013-09-16 13:49:26 +02:00
|
|
|
x509_bitstring bs = { 0, 0, NULL };
|
2011-10-12 11:58:41 +02:00
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
|
2011-10-12 11:58:41 +02:00
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
if( bs.len != 1 )
|
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_INVALID_LENGTH );
|
2011-01-15 17:57:55 +01:00
|
|
|
|
|
|
|
/* Get actual bitstring */
|
|
|
|
*ns_cert_type = *bs.p;
|
2014-06-17 14:06:49 +02:00
|
|
|
return( 0 );
|
2011-01-15 17:57:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int x509_get_key_usage( unsigned char **p,
|
|
|
|
const unsigned char *end,
|
|
|
|
unsigned char *key_usage)
|
|
|
|
{
|
|
|
|
int ret;
|
2011-01-18 17:17:47 +01:00
|
|
|
x509_bitstring bs = { 0, 0, NULL };
|
2011-01-15 17:57:55 +01:00
|
|
|
|
|
|
|
if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
|
2011-01-15 17:57:55 +01:00
|
|
|
|
2012-08-23 15:03:52 +02:00
|
|
|
if( bs.len < 1 )
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
|
2011-01-15 17:57:55 +01:00
|
|
|
POLARSSL_ERR_ASN1_INVALID_LENGTH );
|
|
|
|
|
|
|
|
/* Get actual bitstring */
|
|
|
|
*key_usage = *bs.p;
|
2014-06-17 14:06:49 +02:00
|
|
|
return( 0 );
|
2011-01-15 17:57:55 +01:00
|
|
|
}
|
|
|
|
|
2009-05-02 17:13:40 +02:00
|
|
|
/*
|
2011-01-15 17:57:55 +01:00
|
|
|
* ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
|
|
|
|
*
|
|
|
|
* KeyPurposeId ::= OBJECT IDENTIFIER
|
|
|
|
*/
|
|
|
|
static int x509_get_ext_key_usage( unsigned char **p,
|
|
|
|
const unsigned char *end,
|
|
|
|
x509_sequence *ext_key_usage)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
|
2011-01-15 17:57:55 +01:00
|
|
|
|
|
|
|
/* Sequence length must be >= 1 */
|
|
|
|
if( ext_key_usage->buf.p == NULL )
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
|
2011-01-15 17:57:55 +01:00
|
|
|
POLARSSL_ERR_ASN1_INVALID_LENGTH );
|
|
|
|
|
2014-06-17 14:06:49 +02:00
|
|
|
return( 0 );
|
2011-01-15 17:57:55 +01:00
|
|
|
}
|
|
|
|
|
2012-02-11 17:09:32 +01:00
|
|
|
/*
|
|
|
|
* SubjectAltName ::= GeneralNames
|
|
|
|
*
|
|
|
|
* GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
|
|
|
|
*
|
|
|
|
* GeneralName ::= CHOICE {
|
|
|
|
* otherName [0] OtherName,
|
|
|
|
* rfc822Name [1] IA5String,
|
|
|
|
* dNSName [2] IA5String,
|
|
|
|
* x400Address [3] ORAddress,
|
|
|
|
* directoryName [4] Name,
|
|
|
|
* ediPartyName [5] EDIPartyName,
|
|
|
|
* uniformResourceIdentifier [6] IA5String,
|
|
|
|
* iPAddress [7] OCTET STRING,
|
|
|
|
* registeredID [8] OBJECT IDENTIFIER }
|
|
|
|
*
|
|
|
|
* OtherName ::= SEQUENCE {
|
|
|
|
* type-id OBJECT IDENTIFIER,
|
|
|
|
* value [0] EXPLICIT ANY DEFINED BY type-id }
|
|
|
|
*
|
|
|
|
* EDIPartyName ::= SEQUENCE {
|
|
|
|
* nameAssigner [0] DirectoryString OPTIONAL,
|
|
|
|
* partyName [1] DirectoryString }
|
|
|
|
*
|
|
|
|
* NOTE: PolarSSL only parses and uses dNSName at this point.
|
|
|
|
*/
|
|
|
|
static int x509_get_subject_alt_name( unsigned char **p,
|
|
|
|
const unsigned char *end,
|
|
|
|
x509_sequence *subject_alt_name )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t len, tag_len;
|
|
|
|
asn1_buf *buf;
|
|
|
|
unsigned char tag;
|
|
|
|
asn1_sequence *cur = subject_alt_name;
|
|
|
|
|
|
|
|
/* Get main sequence tag */
|
|
|
|
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 );
|
2012-02-11 17:09:32 +01:00
|
|
|
|
|
|
|
if( *p + len != end )
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
|
2012-02-11 17:09:32 +01:00
|
|
|
POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
|
|
|
|
|
|
|
|
while( *p < end )
|
|
|
|
{
|
|
|
|
if( ( end - *p ) < 1 )
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
|
2012-02-11 17:09:32 +01:00
|
|
|
POLARSSL_ERR_ASN1_OUT_OF_DATA );
|
|
|
|
|
|
|
|
tag = **p;
|
|
|
|
(*p)++;
|
|
|
|
if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
|
2012-02-11 17:09:32 +01:00
|
|
|
|
|
|
|
if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
|
2012-02-11 17:09:32 +01:00
|
|
|
POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
|
|
|
|
|
2014-04-01 13:43:28 +02:00
|
|
|
/* Skip everything but DNS name */
|
2012-02-11 17:09:32 +01:00
|
|
|
if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
|
|
|
|
{
|
|
|
|
*p += tag_len;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate and assign next pointer */
|
2014-04-01 13:43:28 +02:00
|
|
|
if( cur->buf.p != NULL )
|
2012-02-11 17:09:32 +01:00
|
|
|
{
|
2013-07-03 13:37:05 +02:00
|
|
|
cur->next = (asn1_sequence *) polarssl_malloc(
|
2012-02-11 17:09:32 +01:00
|
|
|
sizeof( asn1_sequence ) );
|
|
|
|
|
|
|
|
if( cur->next == NULL )
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
|
2012-02-11 17:09:32 +01:00
|
|
|
POLARSSL_ERR_ASN1_MALLOC_FAILED );
|
|
|
|
|
2012-08-23 12:49:55 +02:00
|
|
|
memset( cur->next, 0, sizeof( asn1_sequence ) );
|
2012-02-11 17:09:32 +01:00
|
|
|
cur = cur->next;
|
|
|
|
}
|
2014-04-01 13:43:28 +02:00
|
|
|
|
|
|
|
buf = &(cur->buf);
|
|
|
|
buf->tag = tag;
|
|
|
|
buf->p = *p;
|
|
|
|
buf->len = tag_len;
|
|
|
|
*p += buf->len;
|
2012-02-11 17:09:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set final sequence entry's next pointer to NULL */
|
|
|
|
cur->next = NULL;
|
|
|
|
|
|
|
|
if( *p != end )
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
|
2012-02-11 17:09:32 +01:00
|
|
|
POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2011-01-15 17:57:55 +01:00
|
|
|
/*
|
|
|
|
* X.509 v3 extensions
|
|
|
|
*
|
|
|
|
* TODO: Perform all of the basic constraints tests required by the RFC
|
|
|
|
* TODO: Set values for undetected extensions to a sane default?
|
|
|
|
*
|
2009-05-02 17:13:40 +02:00
|
|
|
*/
|
|
|
|
static int x509_get_crt_ext( unsigned char **p,
|
2010-03-16 22:09:09 +01:00
|
|
|
const unsigned char *end,
|
2013-09-18 14:13:26 +02:00
|
|
|
x509_crt *crt )
|
2009-05-02 17:13:40 +02:00
|
|
|
{
|
2011-04-24 10:57:21 +02:00
|
|
|
int ret;
|
|
|
|
size_t len;
|
2009-07-27 23:34:45 +02:00
|
|
|
unsigned char *end_ext_data, *end_ext_octet;
|
2009-05-02 17:13:40 +02:00
|
|
|
|
2011-10-12 11:56:41 +02:00
|
|
|
if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
|
2009-05-02 17:13:40 +02:00
|
|
|
{
|
|
|
|
if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
|
|
|
|
return( 0 );
|
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
while( *p < end )
|
|
|
|
{
|
2011-01-15 17:57:55 +01:00
|
|
|
/*
|
|
|
|
* Extension ::= SEQUENCE {
|
|
|
|
* extnID OBJECT IDENTIFIER,
|
|
|
|
* critical BOOLEAN DEFAULT FALSE,
|
|
|
|
* extnValue OCTET STRING }
|
|
|
|
*/
|
|
|
|
x509_buf extn_oid = {0, 0, NULL};
|
|
|
|
int is_critical = 0; /* DEFAULT FALSE */
|
2013-04-07 22:00:46 +02:00
|
|
|
int ext_type = 0;
|
2011-01-15 17:57:55 +01:00
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
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 );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2009-07-27 23:34:45 +02:00
|
|
|
end_ext_data = *p + len;
|
|
|
|
|
2011-01-15 17:57:55 +01:00
|
|
|
/* Get extension ID */
|
|
|
|
extn_oid.tag = **p;
|
|
|
|
|
|
|
|
if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2011-01-15 17:57:55 +01:00
|
|
|
extn_oid.p = *p;
|
|
|
|
*p += extn_oid.len;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2011-01-15 17:57:55 +01:00
|
|
|
if( ( end - *p ) < 1 )
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
|
2011-01-15 17:57:55 +01:00
|
|
|
POLARSSL_ERR_ASN1_OUT_OF_DATA );
|
|
|
|
|
|
|
|
/* Get optional critical */
|
2009-07-27 23:34:45 +02:00
|
|
|
if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
|
2009-01-03 22:51:57 +01:00
|
|
|
( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2011-01-15 17:57:55 +01:00
|
|
|
/* Data should be octet string type */
|
2009-07-27 23:34:45 +02:00
|
|
|
if( ( ret = asn1_get_tag( p, end_ext_data, &len,
|
2009-01-03 22:22:43 +01:00
|
|
|
ASN1_OCTET_STRING ) ) != 0 )
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2009-07-27 23:34:45 +02:00
|
|
|
end_ext_octet = *p + len;
|
2010-03-16 22:09:09 +01:00
|
|
|
|
2009-07-27 23:34:45 +02:00
|
|
|
if( end_ext_octet != end_ext_data )
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
|
2009-07-27 23:34:45 +02:00
|
|
|
POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2011-01-15 17:57:55 +01:00
|
|
|
/*
|
|
|
|
* Detect supported extensions
|
|
|
|
*/
|
2013-04-07 22:00:46 +02:00
|
|
|
ret = oid_get_x509_ext_type( &extn_oid, &ext_type );
|
|
|
|
|
|
|
|
if( ret != 0 )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2013-04-07 22:00:46 +02:00
|
|
|
/* No parser found, skip extension */
|
|
|
|
*p = end_ext_octet;
|
|
|
|
|
|
|
|
#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
|
|
|
|
if( is_critical )
|
|
|
|
{
|
|
|
|
/* Data is marked as critical: fail */
|
2014-06-17 14:06:49 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
|
2013-04-07 22:00:46 +02:00
|
|
|
POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
crt->ext_types |= ext_type;
|
|
|
|
|
|
|
|
switch( ext_type )
|
|
|
|
{
|
|
|
|
case EXT_BASIC_CONSTRAINTS:
|
2011-01-15 17:57:55 +01:00
|
|
|
/* Parse basic constraints */
|
|
|
|
if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
|
2011-01-16 22:46:31 +01:00
|
|
|
&crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
|
2014-06-17 14:06:49 +02:00
|
|
|
return( ret );
|
2013-04-07 22:00:46 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case EXT_KEY_USAGE:
|
2011-01-15 17:57:55 +01:00
|
|
|
/* Parse key usage */
|
|
|
|
if( ( ret = x509_get_key_usage( p, end_ext_octet,
|
|
|
|
&crt->key_usage ) ) != 0 )
|
2014-06-17 14:06:49 +02:00
|
|
|
return( ret );
|
2013-04-07 22:00:46 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case EXT_EXTENDED_KEY_USAGE:
|
2011-01-15 17:57:55 +01:00
|
|
|
/* Parse extended key usage */
|
|
|
|
if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
|
|
|
|
&crt->ext_key_usage ) ) != 0 )
|
2014-06-17 14:06:49 +02:00
|
|
|
return( ret );
|
2013-04-07 22:00:46 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case EXT_SUBJECT_ALT_NAME:
|
|
|
|
/* Parse subject alt name */
|
2012-02-11 17:09:32 +01:00
|
|
|
if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
|
|
|
|
&crt->subject_alt_names ) ) != 0 )
|
2014-06-17 14:06:49 +02:00
|
|
|
return( ret );
|
2013-04-07 22:00:46 +02:00
|
|
|
break;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2013-04-07 22:00:46 +02:00
|
|
|
case EXT_NS_CERT_TYPE:
|
|
|
|
/* Parse netscape certificate type */
|
|
|
|
if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
|
|
|
|
&crt->ns_cert_type ) ) != 0 )
|
2014-06-17 14:06:49 +02:00
|
|
|
return( ret );
|
2013-04-07 22:00:46 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
|
2011-01-15 17:57:55 +01:00
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if( *p != end )
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
|
2009-01-03 22:51:57 +01:00
|
|
|
POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2011-12-04 13:24:18 +01:00
|
|
|
* Parse and fill a single X.509 certificate in DER format
|
2009-01-03 22:22:43 +01:00
|
|
|
*/
|
2013-09-18 14:13:26 +02:00
|
|
|
static int x509_crt_parse_der_core( x509_crt *crt, const unsigned char *buf,
|
2013-09-18 13:46:23 +02:00
|
|
|
size_t buflen )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2011-04-24 10:57:21 +02:00
|
|
|
int ret;
|
2011-05-26 15:16:06 +02:00
|
|
|
size_t len;
|
2012-09-25 14:10:00 +02:00
|
|
|
unsigned char *p, *end, *crt_end;
|
2014-06-05 17:02:24 +02:00
|
|
|
x509_buf sig_params1, sig_params2;
|
2014-01-22 10:12:57 +01:00
|
|
|
|
2014-06-05 17:02:24 +02:00
|
|
|
memset( &sig_params1, 0, sizeof( x509_buf ) );
|
|
|
|
memset( &sig_params2, 0, sizeof( x509_buf ) );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2009-03-28 19:52:39 +01:00
|
|
|
/*
|
|
|
|
* Check for valid input
|
|
|
|
*/
|
|
|
|
if( crt == NULL || buf == NULL )
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
|
2009-03-28 19:52:39 +01:00
|
|
|
|
2013-07-03 13:37:05 +02:00
|
|
|
p = (unsigned char *) polarssl_malloc( len = buflen );
|
2011-02-12 15:30:57 +01:00
|
|
|
|
|
|
|
if( p == NULL )
|
2011-12-10 22:55:01 +01:00
|
|
|
return( POLARSSL_ERR_X509_MALLOC_FAILED );
|
2011-02-12 15:30:57 +01:00
|
|
|
|
|
|
|
memcpy( p, buf, buflen );
|
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
crt->raw.p = p;
|
|
|
|
crt->raw.len = len;
|
|
|
|
end = p + len;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Certificate ::= SEQUENCE {
|
|
|
|
* tbsCertificate TBSCertificate,
|
|
|
|
* signatureAlgorithm AlgorithmIdentifier,
|
|
|
|
* signatureValue BIT STRING }
|
|
|
|
*/
|
|
|
|
if( ( ret = asn1_get_tag( &p, end, &len,
|
|
|
|
ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
|
|
|
|
{
|
2013-09-16 13:49:26 +02:00
|
|
|
x509_crt_free( crt );
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_FORMAT );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2012-09-25 14:10:00 +02:00
|
|
|
if( len > (size_t) ( end - p ) )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2013-09-16 13:49:26 +02:00
|
|
|
x509_crt_free( crt );
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_FORMAT +
|
2009-01-03 22:51:57 +01:00
|
|
|
POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
2012-09-25 14:10:00 +02:00
|
|
|
crt_end = p + len;
|
2013-06-24 19:21:59 +02:00
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
|
|
|
* TBSCertificate ::= SEQUENCE {
|
|
|
|
*/
|
|
|
|
crt->tbs.p = p;
|
|
|
|
|
|
|
|
if( ( ret = asn1_get_tag( &p, end, &len,
|
|
|
|
ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
|
|
|
|
{
|
2013-09-16 13:49:26 +02:00
|
|
|
x509_crt_free( crt );
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
end = p + len;
|
|
|
|
crt->tbs.len = end - crt->tbs.p;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Version ::= INTEGER { v1(0), v2(1), v3(2) }
|
|
|
|
*
|
|
|
|
* CertificateSerialNumber ::= INTEGER
|
|
|
|
*
|
|
|
|
* signature AlgorithmIdentifier
|
|
|
|
*/
|
2013-07-10 13:18:41 +02:00
|
|
|
if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
|
|
|
|
( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
|
2014-01-22 10:12:57 +01:00
|
|
|
( ret = x509_get_alg( &p, end, &crt->sig_oid1,
|
2014-06-05 17:02:24 +02:00
|
|
|
&sig_params1 ) ) != 0 )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2013-09-16 13:49:26 +02:00
|
|
|
x509_crt_free( crt );
|
2009-01-03 22:22:43 +01:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
crt->version++;
|
|
|
|
|
|
|
|
if( crt->version > 3 )
|
|
|
|
{
|
2013-09-16 13:49:26 +02:00
|
|
|
x509_crt_free( crt );
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_UNKNOWN_VERSION );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2014-06-05 17:02:24 +02:00
|
|
|
if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &sig_params1,
|
2014-06-05 15:14:28 +02:00
|
|
|
&crt->sig_md, &crt->sig_pk,
|
|
|
|
&crt->sig_opts ) ) != 0 )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2013-09-16 13:49:26 +02:00
|
|
|
x509_crt_free( crt );
|
2010-03-17 07:56:01 +01:00
|
|
|
return( ret );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* issuer Name
|
|
|
|
*/
|
|
|
|
crt->issuer_raw.p = p;
|
|
|
|
|
|
|
|
if( ( ret = asn1_get_tag( &p, end, &len,
|
|
|
|
ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
|
|
|
|
{
|
2013-09-16 13:49:26 +02:00
|
|
|
x509_crt_free( crt );
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
|
|
|
|
{
|
2013-09-16 13:49:26 +02:00
|
|
|
x509_crt_free( crt );
|
2009-01-03 22:22:43 +01:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
crt->issuer_raw.len = p - crt->issuer_raw.p;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Validity ::= SEQUENCE {
|
|
|
|
* notBefore Time,
|
|
|
|
* notAfter Time }
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
|
|
|
|
&crt->valid_to ) ) != 0 )
|
|
|
|
{
|
2013-09-16 13:49:26 +02:00
|
|
|
x509_crt_free( crt );
|
2009-01-03 22:22:43 +01:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* subject Name
|
|
|
|
*/
|
|
|
|
crt->subject_raw.p = p;
|
|
|
|
|
|
|
|
if( ( ret = asn1_get_tag( &p, end, &len,
|
|
|
|
ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
|
|
|
|
{
|
2013-09-16 13:49:26 +02:00
|
|
|
x509_crt_free( crt );
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2012-06-27 13:51:09 +02:00
|
|
|
if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2013-09-16 13:49:26 +02:00
|
|
|
x509_crt_free( crt );
|
2009-01-03 22:22:43 +01:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
crt->subject_raw.len = p - crt->subject_raw.p;
|
|
|
|
|
|
|
|
/*
|
2013-07-09 12:13:24 +02:00
|
|
|
* SubjectPublicKeyInfo
|
2009-01-03 22:22:43 +01:00
|
|
|
*/
|
2013-09-16 22:45:03 +02:00
|
|
|
if( ( ret = pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2013-09-16 13:49:26 +02:00
|
|
|
x509_crt_free( crt );
|
2009-01-03 22:22:43 +01:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
|
|
|
|
* -- If present, version shall be v2 or v3
|
|
|
|
* subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
|
|
|
|
* -- If present, version shall be v2 or v3
|
|
|
|
* extensions [3] EXPLICIT Extensions OPTIONAL
|
|
|
|
* -- If present, version shall be v3
|
|
|
|
*/
|
|
|
|
if( crt->version == 2 || crt->version == 3 )
|
|
|
|
{
|
|
|
|
ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
|
|
|
|
if( ret != 0 )
|
|
|
|
{
|
2013-09-16 13:49:26 +02:00
|
|
|
x509_crt_free( crt );
|
2009-01-03 22:22:43 +01:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( crt->version == 2 || crt->version == 3 )
|
|
|
|
{
|
|
|
|
ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
|
|
|
|
if( ret != 0 )
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
|
|
|
x509_crt_free( crt );
|
|
|
|
return( ret );
|
|
|
|
}
|
2009-05-02 17:13:40 +02:00
|
|
|
}
|
|
|
|
|
2013-09-23 15:01:36 +02:00
|
|
|
#if !defined(POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3)
|
2013-09-16 13:49:26 +02:00
|
|
|
if( crt->version == 3 )
|
2009-05-02 17:13:40 +02:00
|
|
|
{
|
2013-09-23 15:01:36 +02:00
|
|
|
#endif
|
2014-06-17 16:39:18 +02:00
|
|
|
ret = x509_get_crt_ext( &p, end, crt );
|
2009-05-02 17:13:40 +02:00
|
|
|
if( ret != 0 )
|
|
|
|
{
|
2013-09-16 13:49:26 +02:00
|
|
|
x509_crt_free( crt );
|
2009-05-02 17:13:40 +02:00
|
|
|
return( ret );
|
|
|
|
}
|
2013-09-23 15:01:36 +02:00
|
|
|
#if !defined(POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3)
|
2009-05-02 17:13:40 +02:00
|
|
|
}
|
2013-09-23 15:01:36 +02:00
|
|
|
#endif
|
2009-05-02 17:13:40 +02:00
|
|
|
|
|
|
|
if( p != end )
|
|
|
|
{
|
2013-09-16 13:49:26 +02:00
|
|
|
x509_crt_free( crt );
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_FORMAT +
|
2009-05-02 17:13:40 +02:00
|
|
|
POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
|
|
|
|
}
|
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
end = crt_end;
|
2009-05-02 17:13:40 +02:00
|
|
|
|
|
|
|
/*
|
2013-09-16 13:49:26 +02:00
|
|
|
* }
|
|
|
|
* -- end of TBSCertificate
|
|
|
|
*
|
2009-05-02 17:13:40 +02:00
|
|
|
* signatureAlgorithm AlgorithmIdentifier,
|
|
|
|
* signatureValue BIT STRING
|
|
|
|
*/
|
2014-06-05 17:02:24 +02:00
|
|
|
if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2, &sig_params2 ) ) != 0 )
|
2009-05-02 17:13:40 +02:00
|
|
|
{
|
2013-09-16 13:49:26 +02:00
|
|
|
x509_crt_free( crt );
|
2009-05-02 17:13:40 +02:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
if( crt->sig_oid1.len != crt->sig_oid2.len ||
|
2014-06-05 17:02:24 +02:00
|
|
|
memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->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 )
|
2009-05-02 17:13:40 +02:00
|
|
|
{
|
2013-09-16 13:49:26 +02:00
|
|
|
x509_crt_free( crt );
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_SIG_MISMATCH );
|
2009-05-02 17:13:40 +02:00
|
|
|
}
|
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
|
2009-05-02 17:13:40 +02:00
|
|
|
{
|
2013-09-16 13:49:26 +02:00
|
|
|
x509_crt_free( crt );
|
2009-05-02 17:13:40 +02:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( p != end )
|
|
|
|
{
|
2013-09-16 13:49:26 +02:00
|
|
|
x509_crt_free( crt );
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_FORMAT +
|
2009-05-02 17:13:40 +02:00
|
|
|
POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
|
|
|
|
}
|
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse one X.509 certificate in DER format from a buffer and add them to a
|
|
|
|
* chained list
|
|
|
|
*/
|
2013-09-18 14:13:26 +02:00
|
|
|
int x509_crt_parse_der( x509_crt *chain, const unsigned char *buf,
|
2013-09-18 13:46:23 +02:00
|
|
|
size_t buflen )
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
|
|
|
int ret;
|
2013-09-18 14:13:26 +02:00
|
|
|
x509_crt *crt = chain, *prev = NULL;
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for valid input
|
|
|
|
*/
|
|
|
|
if( crt == 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
|
|
|
|
|
|
|
while( crt->version != 0 && crt->next != NULL )
|
|
|
|
{
|
|
|
|
prev = crt;
|
|
|
|
crt = crt->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add new certificate on the end of the chain if needed.
|
|
|
|
*/
|
2014-06-17 16:39:18 +02:00
|
|
|
if( crt->version != 0 && crt->next == NULL )
|
2009-05-02 17:13:40 +02:00
|
|
|
{
|
2013-09-18 14:13:26 +02:00
|
|
|
crt->next = (x509_crt *) polarssl_malloc( sizeof( x509_crt ) );
|
2009-05-02 17:13:40 +02:00
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
if( crt->next == NULL )
|
2011-12-10 22:55:01 +01:00
|
|
|
return( POLARSSL_ERR_X509_MALLOC_FAILED );
|
2009-05-02 17:13:40 +02:00
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
prev = crt;
|
|
|
|
crt = crt->next;
|
2013-09-18 11:58:25 +02:00
|
|
|
x509_crt_init( crt );
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
2013-09-18 13:46:23 +02:00
|
|
|
if( ( ret = x509_crt_parse_der_core( crt, buf, buflen ) ) != 0 )
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
|
|
|
if( prev )
|
|
|
|
prev->next = NULL;
|
|
|
|
|
|
|
|
if( crt != chain )
|
|
|
|
polarssl_free( crt );
|
2009-05-02 17:13:40 +02:00
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
return( ret );
|
2009-05-02 17:13:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2009-04-19 20:44:26 +02:00
|
|
|
/*
|
2014-05-01 14:18:25 +02:00
|
|
|
* Parse one or more PEM certificates from a buffer and add them to the chained
|
|
|
|
* list
|
2009-04-19 20:44:26 +02:00
|
|
|
*/
|
2013-09-18 14:13:26 +02:00
|
|
|
int x509_crt_parse( x509_crt *chain, const unsigned char *buf, size_t buflen )
|
2009-04-19 20:44:26 +02:00
|
|
|
{
|
2013-09-16 13:49:26 +02:00
|
|
|
int success = 0, first_error = 0, total_failed = 0;
|
|
|
|
int buf_format = X509_FORMAT_DER;
|
2009-04-19 20:44:26 +02:00
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
/*
|
|
|
|
* Check for valid input
|
|
|
|
*/
|
|
|
|
if( chain == NULL || buf == NULL )
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
|
2009-04-19 20:44:26 +02:00
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
/*
|
|
|
|
* Determine buffer content. Buffer contains either one DER certificate or
|
|
|
|
* one or more PEM certificates.
|
|
|
|
*/
|
|
|
|
#if defined(POLARSSL_PEM_PARSE_C)
|
|
|
|
if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
|
|
|
|
buf_format = X509_FORMAT_PEM;
|
|
|
|
#endif
|
2009-04-19 20:44:26 +02:00
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
if( buf_format == X509_FORMAT_DER )
|
2013-09-18 13:46:23 +02:00
|
|
|
return x509_crt_parse_der( chain, buf, buflen );
|
2013-08-19 14:29:31 +02:00
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
#if defined(POLARSSL_PEM_PARSE_C)
|
|
|
|
if( buf_format == X509_FORMAT_PEM )
|
2013-05-14 13:26:51 +02:00
|
|
|
{
|
2013-09-16 13:49:26 +02:00
|
|
|
int ret;
|
|
|
|
pem_context pem;
|
2009-04-19 20:44:26 +02:00
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
while( buflen > 0 )
|
|
|
|
{
|
|
|
|
size_t use_len;
|
|
|
|
pem_init( &pem );
|
|
|
|
|
|
|
|
ret = pem_read_buffer( &pem,
|
|
|
|
"-----BEGIN CERTIFICATE-----",
|
|
|
|
"-----END CERTIFICATE-----",
|
|
|
|
buf, NULL, 0, &use_len );
|
|
|
|
|
|
|
|
if( ret == 0 )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Was PEM encoded
|
|
|
|
*/
|
|
|
|
buflen -= use_len;
|
|
|
|
buf += use_len;
|
|
|
|
}
|
|
|
|
else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
|
|
|
|
{
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
|
|
|
|
{
|
|
|
|
pem_free( &pem );
|
2009-04-19 20:44:26 +02:00
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
/*
|
|
|
|
* PEM header and footer were found
|
|
|
|
*/
|
|
|
|
buflen -= use_len;
|
|
|
|
buf += use_len;
|
2009-04-19 20:44:26 +02:00
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
if( first_error == 0 )
|
|
|
|
first_error = ret;
|
2009-04-19 20:44:26 +02:00
|
|
|
|
2014-09-26 14:53:04 +02:00
|
|
|
total_failed++;
|
2013-09-16 13:49:26 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
|
2013-09-18 13:46:23 +02:00
|
|
|
ret = x509_crt_parse_der( chain, pem.buf, pem.buflen );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
pem_free( &pem );
|
|
|
|
|
|
|
|
if( ret != 0 )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Quit parsing on a memory error
|
|
|
|
*/
|
|
|
|
if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
|
|
|
|
return( ret );
|
|
|
|
|
|
|
|
if( first_error == 0 )
|
|
|
|
first_error = ret;
|
|
|
|
|
|
|
|
total_failed++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
success = 1;
|
|
|
|
}
|
|
|
|
}
|
2014-05-01 13:03:14 +02:00
|
|
|
#endif /* POLARSSL_PEM_PARSE_C */
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
if( success )
|
|
|
|
return( total_failed );
|
|
|
|
else if( first_error )
|
|
|
|
return( first_error );
|
|
|
|
else
|
|
|
|
return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
|
2009-04-19 20:44:26 +02:00
|
|
|
}
|
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
#if defined(POLARSSL_FS_IO)
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
|
|
|
* Load one or more certificates and add them to the chained list
|
|
|
|
*/
|
2013-09-18 14:13:26 +02:00
|
|
|
int x509_crt_parse_file( x509_crt *chain, const char *path )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t n;
|
|
|
|
unsigned char *buf;
|
|
|
|
|
2014-06-17 16:39:18 +02:00
|
|
|
if( ( ret = x509_load_file( path, &buf, &n ) ) != 0 )
|
2011-12-10 22:55:01 +01:00
|
|
|
return( ret );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2013-09-18 13:46:23 +02:00
|
|
|
ret = x509_crt_parse( chain, buf, n );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2014-06-13 17:20:13 +02:00
|
|
|
polarssl_zeroize( buf, n + 1 );
|
2013-07-03 13:37:05 +02:00
|
|
|
polarssl_free( buf );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2014-02-12 09:29:05 +01:00
|
|
|
#if defined(POLARSSL_THREADING_PTHREAD)
|
2013-11-28 17:11:54 +01:00
|
|
|
static threading_mutex_t readdir_mutex = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
#endif
|
|
|
|
|
2013-09-18 14:13:26 +02:00
|
|
|
int x509_crt_parse_path( x509_crt *chain, const char *path )
|
2012-06-04 14:46:42 +02:00
|
|
|
{
|
|
|
|
int ret = 0;
|
2013-10-28 18:48:30 +01:00
|
|
|
#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
|
2012-10-01 23:13:10 +02:00
|
|
|
int w_ret;
|
|
|
|
WCHAR szDir[MAX_PATH];
|
|
|
|
char filename[MAX_PATH];
|
2014-05-01 13:03:14 +02:00
|
|
|
char *p;
|
2013-10-11 18:58:55 +02:00
|
|
|
int len = (int) strlen( path );
|
2012-10-01 23:13:10 +02:00
|
|
|
|
2014-05-01 13:03:14 +02:00
|
|
|
WIN32_FIND_DATAW file_data;
|
2012-06-04 14:46:42 +02:00
|
|
|
HANDLE hFind;
|
2012-11-02 12:06:08 +01:00
|
|
|
|
|
|
|
if( len > MAX_PATH - 3 )
|
2013-09-23 15:10:16 +02:00
|
|
|
return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
|
2012-06-04 14:46:42 +02:00
|
|
|
|
2014-05-01 13:03:14 +02:00
|
|
|
memset( szDir, 0, sizeof(szDir) );
|
|
|
|
memset( filename, 0, MAX_PATH );
|
|
|
|
memcpy( filename, path, len );
|
|
|
|
filename[len++] = '\\';
|
|
|
|
p = filename + len;
|
2012-11-02 12:06:08 +01:00
|
|
|
filename[len++] = '*';
|
2012-10-01 23:13:10 +02:00
|
|
|
|
2014-05-01 14:18:25 +02:00
|
|
|
w_ret = MultiByteToWideChar( CP_ACP, 0, filename, len, szDir,
|
|
|
|
MAX_PATH - 3 );
|
2012-06-04 14:46:42 +02:00
|
|
|
|
2012-11-02 13:53:26 +01:00
|
|
|
hFind = FindFirstFileW( szDir, &file_data );
|
2014-06-17 16:39:18 +02:00
|
|
|
if( hFind == INVALID_HANDLE_VALUE )
|
2012-06-04 14:46:42 +02:00
|
|
|
return( POLARSSL_ERR_X509_FILE_IO_ERROR );
|
|
|
|
|
2012-11-02 12:06:08 +01:00
|
|
|
len = MAX_PATH - len;
|
2012-06-04 14:46:42 +02:00
|
|
|
do
|
|
|
|
{
|
2014-05-01 13:03:14 +02:00
|
|
|
memset( p, 0, len );
|
2012-10-01 23:13:10 +02:00
|
|
|
|
2012-06-04 23:29:15 +02:00
|
|
|
if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
|
2012-06-04 14:46:42 +02:00
|
|
|
continue;
|
|
|
|
|
2014-05-01 13:03:14 +02:00
|
|
|
w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
|
2014-06-17 16:39:18 +02:00
|
|
|
lstrlenW( file_data.cFileName ),
|
2014-05-01 13:03:14 +02:00
|
|
|
p, len - 1,
|
|
|
|
NULL, NULL );
|
2012-06-04 14:46:42 +02:00
|
|
|
|
2013-09-18 13:46:23 +02:00
|
|
|
w_ret = x509_crt_parse_file( chain, filename );
|
2012-10-01 23:13:10 +02:00
|
|
|
if( w_ret < 0 )
|
2013-06-24 19:22:42 +02:00
|
|
|
ret++;
|
|
|
|
else
|
|
|
|
ret += w_ret;
|
2012-06-04 14:46:42 +02:00
|
|
|
}
|
2012-11-02 13:53:26 +01:00
|
|
|
while( FindNextFileW( hFind, &file_data ) != 0 );
|
2012-06-04 14:46:42 +02:00
|
|
|
|
2014-06-17 16:39:18 +02:00
|
|
|
if( GetLastError() != ERROR_NO_MORE_FILES )
|
2012-11-02 12:06:08 +01:00
|
|
|
ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
|
2012-06-04 14:46:42 +02:00
|
|
|
|
|
|
|
FindClose( hFind );
|
2013-10-14 15:51:50 +02:00
|
|
|
#else /* _WIN32 */
|
2013-11-26 16:47:11 +01:00
|
|
|
int t_ret;
|
2013-06-24 19:22:42 +02:00
|
|
|
struct stat sb;
|
2013-11-26 16:47:11 +01:00
|
|
|
struct dirent *entry;
|
2012-06-04 14:46:42 +02:00
|
|
|
char entry_name[255];
|
|
|
|
DIR *dir = opendir( path );
|
|
|
|
|
2014-06-17 16:39:18 +02:00
|
|
|
if( dir == NULL )
|
2012-06-04 14:46:42 +02:00
|
|
|
return( POLARSSL_ERR_X509_FILE_IO_ERROR );
|
|
|
|
|
2014-02-12 09:29:05 +01:00
|
|
|
#if defined(POLARSSL_THREADING_PTHREAD)
|
2013-11-28 17:11:54 +01:00
|
|
|
if( ( ret = polarssl_mutex_lock( &readdir_mutex ) ) != 0 )
|
|
|
|
return( ret );
|
|
|
|
#endif
|
|
|
|
|
2013-11-26 16:47:11 +01:00
|
|
|
while( ( entry = readdir( dir ) ) != NULL )
|
2012-06-04 14:46:42 +02:00
|
|
|
{
|
2013-11-26 16:47:11 +01:00
|
|
|
snprintf( entry_name, sizeof entry_name, "%s/%s", path, entry->d_name );
|
2013-06-24 19:22:42 +02:00
|
|
|
|
2013-11-26 16:47:11 +01:00
|
|
|
if( stat( entry_name, &sb ) == -1 )
|
2013-09-09 17:26:14 +02:00
|
|
|
{
|
|
|
|
closedir( dir );
|
2013-11-28 17:11:54 +01:00
|
|
|
ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
|
|
|
|
goto cleanup;
|
2013-09-09 17:26:14 +02:00
|
|
|
}
|
2013-06-24 19:22:42 +02:00
|
|
|
|
|
|
|
if( !S_ISREG( sb.st_mode ) )
|
2012-06-04 14:46:42 +02:00
|
|
|
continue;
|
|
|
|
|
2013-06-24 19:22:42 +02:00
|
|
|
// Ignore parse errors
|
|
|
|
//
|
2013-09-18 13:46:23 +02:00
|
|
|
t_ret = x509_crt_parse_file( chain, entry_name );
|
2012-06-04 14:46:42 +02:00
|
|
|
if( t_ret < 0 )
|
2013-06-24 19:22:42 +02:00
|
|
|
ret++;
|
2013-09-16 13:49:26 +02:00
|
|
|
else
|
|
|
|
ret += t_ret;
|
|
|
|
}
|
|
|
|
closedir( dir );
|
2013-11-28 17:11:54 +01:00
|
|
|
|
|
|
|
cleanup:
|
2014-02-12 09:29:05 +01:00
|
|
|
#if defined(POLARSSL_THREADING_PTHREAD)
|
2013-11-28 17:11:54 +01:00
|
|
|
if( polarssl_mutex_unlock( &readdir_mutex ) != 0 )
|
|
|
|
ret = POLARSSL_ERR_THREADING_MUTEX_ERROR;
|
|
|
|
#endif
|
|
|
|
|
2013-10-14 15:51:50 +02:00
|
|
|
#endif /* _WIN32 */
|
2013-08-14 13:39:57 +02:00
|
|
|
|
|
|
|
return( ret );
|
2013-07-11 16:17:23 +02:00
|
|
|
}
|
2013-09-16 13:49:26 +02:00
|
|
|
#endif /* POLARSSL_FS_IO */
|
2013-07-11 16:17:23 +02:00
|
|
|
|
2013-10-29 15:22:54 +01:00
|
|
|
#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
|
|
|
|
!defined(EFI32)
|
2009-05-02 17:13:40 +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, ... )
|
2009-05-02 17:13:40 +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 )
|
2011-04-24 10:57:21 +02:00
|
|
|
return( (int) size + 20 );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2014-06-17 14:06:49 +02:00
|
|
|
return( res );
|
2009-05-02 17:13:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#define snprintf compat_snprintf
|
2014-05-01 13:03:14 +02:00
|
|
|
#endif /* _MSC_VER && !snprintf && !EFIX64 && !EFI32 */
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2009-05-02 17:13:40 +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; \
|
2009-05-02 17:13:40 +02:00
|
|
|
}
|
|
|
|
|
2014-04-01 13:43:28 +02:00
|
|
|
static int x509_info_subject_alt_name( char **buf, size_t *size,
|
|
|
|
const x509_sequence *subject_alt_name )
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
size_t n = *size;
|
|
|
|
char *p = *buf;
|
|
|
|
const x509_sequence *cur = subject_alt_name;
|
2014-04-01 18:00:07 +02:00
|
|
|
const char *sep = "";
|
|
|
|
size_t sep_len = 0;
|
2014-04-01 13:43:28 +02:00
|
|
|
|
|
|
|
while( cur != NULL )
|
|
|
|
{
|
2014-04-01 18:00:07 +02:00
|
|
|
if( cur->buf.len + sep_len >= n )
|
2014-04-01 13:43:28 +02:00
|
|
|
{
|
|
|
|
*p = '\0';
|
|
|
|
return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL );
|
|
|
|
}
|
|
|
|
|
2014-04-01 18:00:07 +02:00
|
|
|
n -= cur->buf.len + sep_len;
|
|
|
|
for( i = 0; i < sep_len; i++ )
|
|
|
|
*p++ = sep[i];
|
2014-04-01 13:43:28 +02:00
|
|
|
for( i = 0; i < cur->buf.len; i++ )
|
|
|
|
*p++ = cur->buf.p[i];
|
|
|
|
|
2014-04-01 18:00:07 +02:00
|
|
|
sep = ", ";
|
|
|
|
sep_len = 2;
|
|
|
|
|
2014-04-01 13:43:28 +02:00
|
|
|
cur = cur->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
*p = '\0';
|
|
|
|
|
|
|
|
*size = n;
|
|
|
|
*buf = p;
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2014-04-01 18:12:24 +02:00
|
|
|
#define PRINT_ITEM(i) \
|
|
|
|
{ \
|
|
|
|
ret = snprintf( p, n, "%s" i, sep ); \
|
|
|
|
SAFE_SNPRINTF(); \
|
|
|
|
sep = ", "; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define CERT_TYPE(type,name) \
|
|
|
|
if( ns_cert_type & type ) \
|
|
|
|
PRINT_ITEM( name );
|
|
|
|
|
2014-04-01 13:01:11 +02:00
|
|
|
static int x509_info_cert_type( char **buf, size_t *size,
|
|
|
|
unsigned char ns_cert_type )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t n = *size;
|
|
|
|
char *p = *buf;
|
2014-04-01 18:00:07 +02:00
|
|
|
const char *sep = "";
|
2014-04-01 13:01:11 +02:00
|
|
|
|
2014-04-01 18:12:24 +02:00
|
|
|
CERT_TYPE( NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
|
|
|
|
CERT_TYPE( NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
|
|
|
|
CERT_TYPE( NS_CERT_TYPE_EMAIL, "Email" );
|
|
|
|
CERT_TYPE( NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
|
|
|
|
CERT_TYPE( NS_CERT_TYPE_RESERVED, "Reserved" );
|
|
|
|
CERT_TYPE( NS_CERT_TYPE_SSL_CA, "SSL CA" );
|
|
|
|
CERT_TYPE( NS_CERT_TYPE_EMAIL_CA, "Email CA" );
|
|
|
|
CERT_TYPE( NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
|
2014-04-01 13:01:11 +02:00
|
|
|
|
|
|
|
*size = n;
|
|
|
|
*buf = p;
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2014-04-01 18:12:24 +02:00
|
|
|
#define KEY_USAGE(code,name) \
|
|
|
|
if( key_usage & code ) \
|
|
|
|
PRINT_ITEM( name );
|
|
|
|
|
2014-04-01 14:12:11 +02:00
|
|
|
static int x509_info_key_usage( char **buf, size_t *size,
|
|
|
|
unsigned char key_usage )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t n = *size;
|
|
|
|
char *p = *buf;
|
2014-04-01 18:00:07 +02:00
|
|
|
const char *sep = "";
|
2014-04-01 14:12:11 +02:00
|
|
|
|
2014-04-01 18:12:24 +02:00
|
|
|
KEY_USAGE( KU_DIGITAL_SIGNATURE, "Digital Signature" );
|
|
|
|
KEY_USAGE( KU_NON_REPUDIATION, "Non Repudiation" );
|
|
|
|
KEY_USAGE( KU_KEY_ENCIPHERMENT, "Key Encipherment" );
|
|
|
|
KEY_USAGE( KU_DATA_ENCIPHERMENT, "Data Encipherment" );
|
|
|
|
KEY_USAGE( KU_KEY_AGREEMENT, "Key Agreement" );
|
|
|
|
KEY_USAGE( KU_KEY_CERT_SIGN, "Key Cert Sign" );
|
|
|
|
KEY_USAGE( KU_CRL_SIGN, "CRL Sign" );
|
2014-04-01 14:12:11 +02:00
|
|
|
|
|
|
|
*size = n;
|
|
|
|
*buf = p;
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2014-04-01 17:32:44 +02:00
|
|
|
static int x509_info_ext_key_usage( char **buf, size_t *size,
|
|
|
|
const x509_sequence *extended_key_usage )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
const char *desc;
|
|
|
|
size_t n = *size;
|
|
|
|
char *p = *buf;
|
|
|
|
const x509_sequence *cur = extended_key_usage;
|
2014-04-01 18:00:07 +02:00
|
|
|
const char *sep = "";
|
2014-04-01 17:32:44 +02:00
|
|
|
|
|
|
|
while( cur != NULL )
|
|
|
|
{
|
|
|
|
if( oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
|
|
|
|
desc = "???";
|
|
|
|
|
2014-04-01 18:00:07 +02:00
|
|
|
ret = snprintf( p, n, "%s%s", sep, desc );
|
2014-04-01 17:32:44 +02:00
|
|
|
SAFE_SNPRINTF();
|
|
|
|
|
2014-04-01 18:00:07 +02:00
|
|
|
sep = ", ";
|
|
|
|
|
2014-04-01 17:32:44 +02:00
|
|
|
cur = cur->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
*size = n;
|
|
|
|
*buf = p;
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
2009-05-02 17:13:40 +02:00
|
|
|
* Return an informational string about the certificate.
|
2009-01-03 22:22:43 +01:00
|
|
|
*/
|
2014-04-01 12:19:09 +02:00
|
|
|
#define BEFORE_COLON 18
|
|
|
|
#define BC "18"
|
2013-09-18 13:46:23 +02:00
|
|
|
int x509_crt_info( char *buf, size_t size, const char *prefix,
|
2013-09-18 14:13:26 +02:00
|
|
|
const x509_crt *crt )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2011-04-24 10:57:21 +02:00
|
|
|
int ret;
|
|
|
|
size_t n;
|
2009-05-02 17:13:40 +02:00
|
|
|
char *p;
|
2013-08-12 19:45:32 +02:00
|
|
|
char key_size_str[BEFORE_COLON];
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
p = buf;
|
2009-05-02 17:13:40 +02:00
|
|
|
n = size;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2014-04-01 12:19:09 +02:00
|
|
|
ret = snprintf( p, n, "%scert. version : %d\n",
|
2009-01-03 22:22:43 +01:00
|
|
|
prefix, crt->version );
|
2009-05-02 17:13:40 +02:00
|
|
|
SAFE_SNPRINTF();
|
2014-04-01 12:19:09 +02:00
|
|
|
ret = snprintf( p, n, "%sserial number : ",
|
2009-01-03 22:22:43 +01:00
|
|
|
prefix );
|
2009-05-02 17:13:40 +02:00
|
|
|
SAFE_SNPRINTF();
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2014-06-17 16:39:18 +02:00
|
|
|
ret = x509_serial_gets( p, n, &crt->serial );
|
2011-01-16 22:34:59 +01:00
|
|
|
SAFE_SNPRINTF();
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2014-04-01 12:19:09 +02:00
|
|
|
ret = snprintf( p, n, "\n%sissuer name : ", prefix );
|
2009-05-02 17:13:40 +02:00
|
|
|
SAFE_SNPRINTF();
|
2013-09-18 11:11:02 +02:00
|
|
|
ret = x509_dn_gets( p, n, &crt->issuer );
|
2009-05-02 17:13:40 +02:00
|
|
|
SAFE_SNPRINTF();
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2014-04-01 12:19:09 +02:00
|
|
|
ret = snprintf( p, n, "\n%ssubject name : ", prefix );
|
2009-05-02 17:13:40 +02:00
|
|
|
SAFE_SNPRINTF();
|
2013-09-18 11:11:02 +02:00
|
|
|
ret = x509_dn_gets( p, n, &crt->subject );
|
2009-05-02 17:13:40 +02:00
|
|
|
SAFE_SNPRINTF();
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2014-04-01 12:19:09 +02:00
|
|
|
ret = snprintf( p, n, "\n%sissued on : " \
|
2009-01-03 22:22:43 +01:00
|
|
|
"%04d-%02d-%02d %02d:%02d:%02d", prefix,
|
|
|
|
crt->valid_from.year, crt->valid_from.mon,
|
|
|
|
crt->valid_from.day, crt->valid_from.hour,
|
|
|
|
crt->valid_from.min, crt->valid_from.sec );
|
2009-05-02 17:13:40 +02:00
|
|
|
SAFE_SNPRINTF();
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2014-04-01 12:19:09 +02:00
|
|
|
ret = snprintf( p, n, "\n%sexpires on : " \
|
2009-01-03 22:22:43 +01:00
|
|
|
"%04d-%02d-%02d %02d:%02d:%02d", prefix,
|
|
|
|
crt->valid_to.year, crt->valid_to.mon,
|
|
|
|
crt->valid_to.day, crt->valid_to.hour,
|
|
|
|
crt->valid_to.min, crt->valid_to.sec );
|
2009-05-02 17:13:40 +02:00
|
|
|
SAFE_SNPRINTF();
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2014-04-01 12:19:09 +02:00
|
|
|
ret = snprintf( p, n, "\n%ssigned using : ", prefix );
|
2009-05-02 17:13:40 +02:00
|
|
|
SAFE_SNPRINTF();
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2014-06-05 15:41:39 +02:00
|
|
|
ret = x509_sig_alg_gets( p, n, &crt->sig_oid1, crt->sig_pk,
|
2014-06-05 17:07:30 +02:00
|
|
|
crt->sig_md, crt->sig_opts );
|
2009-05-02 17:13:40 +02:00
|
|
|
SAFE_SNPRINTF();
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2014-04-01 12:19:09 +02:00
|
|
|
/* Key size */
|
2013-08-12 19:45:32 +02:00
|
|
|
if( ( ret = x509_key_size_helper( key_size_str, BEFORE_COLON,
|
2013-08-14 18:26:41 +02:00
|
|
|
pk_get_name( &crt->pk ) ) ) != 0 )
|
2013-08-12 19:45:32 +02:00
|
|
|
{
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2014-04-01 12:19:09 +02:00
|
|
|
ret = snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
|
2013-08-14 15:56:19 +02:00
|
|
|
(int) pk_get_size( &crt->pk ) );
|
2009-05-02 17:13:40 +02:00
|
|
|
SAFE_SNPRINTF();
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2014-04-01 12:19:09 +02:00
|
|
|
/*
|
|
|
|
* Optional extensions
|
|
|
|
*/
|
|
|
|
|
|
|
|
if( crt->ext_types & EXT_BASIC_CONSTRAINTS )
|
|
|
|
{
|
|
|
|
ret = snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
|
|
|
|
crt->ca_istrue ? "true" : "false" );
|
|
|
|
SAFE_SNPRINTF();
|
|
|
|
|
|
|
|
if( crt->max_pathlen > 0 )
|
|
|
|
{
|
|
|
|
ret = snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
|
|
|
|
SAFE_SNPRINTF();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
|
|
|
|
{
|
2014-04-01 18:00:07 +02:00
|
|
|
ret = snprintf( p, n, "\n%ssubject alt name : ", prefix );
|
2014-04-01 12:19:09 +02:00
|
|
|
SAFE_SNPRINTF();
|
2014-04-01 13:43:28 +02:00
|
|
|
|
|
|
|
if( ( ret = x509_info_subject_alt_name( &p, &n,
|
|
|
|
&crt->subject_alt_names ) ) != 0 )
|
|
|
|
return( ret );
|
2014-04-01 12:19:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if( crt->ext_types & EXT_NS_CERT_TYPE )
|
|
|
|
{
|
2014-04-01 18:00:07 +02:00
|
|
|
ret = snprintf( p, n, "\n%scert. type : ", prefix );
|
2014-04-01 12:19:09 +02:00
|
|
|
SAFE_SNPRINTF();
|
2014-04-01 13:01:11 +02:00
|
|
|
|
|
|
|
if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
|
|
|
|
return( ret );
|
2014-04-01 12:19:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if( crt->ext_types & EXT_KEY_USAGE )
|
|
|
|
{
|
2014-04-01 18:00:07 +02:00
|
|
|
ret = snprintf( p, n, "\n%skey usage : ", prefix );
|
2014-04-01 12:19:09 +02:00
|
|
|
SAFE_SNPRINTF();
|
2014-04-01 14:12:11 +02:00
|
|
|
|
|
|
|
if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
|
|
|
|
return( ret );
|
2014-04-01 12:19:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if( crt->ext_types & EXT_EXTENDED_KEY_USAGE )
|
|
|
|
{
|
2014-04-01 18:00:07 +02:00
|
|
|
ret = snprintf( p, n, "\n%sext key usage : ", prefix );
|
2014-04-01 12:19:09 +02:00
|
|
|
SAFE_SNPRINTF();
|
2014-04-01 17:32:44 +02:00
|
|
|
|
|
|
|
if( ( ret = x509_info_ext_key_usage( &p, &n,
|
|
|
|
&crt->ext_key_usage ) ) != 0 )
|
|
|
|
return( ret );
|
2014-04-01 12:19:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = snprintf( p, n, "\n" );
|
|
|
|
SAFE_SNPRINTF();
|
|
|
|
|
2011-04-24 10:57:21 +02:00
|
|
|
return( (int) ( size - n ) );
|
2009-05-02 17:13:40 +02:00
|
|
|
}
|
|
|
|
|
2014-04-09 09:50:03 +02:00
|
|
|
#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
|
|
|
|
int x509_crt_check_key_usage( const x509_crt *crt, int usage )
|
|
|
|
{
|
|
|
|
if( ( crt->ext_types & EXT_KEY_USAGE ) != 0 &&
|
|
|
|
( crt->key_usage & usage ) != usage )
|
|
|
|
return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-04-10 17:53:56 +02:00
|
|
|
#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
|
|
|
|
int x509_crt_check_extended_key_usage( const x509_crt *crt,
|
|
|
|
const char *usage_oid,
|
|
|
|
size_t usage_len )
|
|
|
|
{
|
|
|
|
const x509_sequence *cur;
|
|
|
|
|
|
|
|
/* Extension is not mandatory, absent means no restriction */
|
|
|
|
if( ( crt->ext_types & EXT_EXTENDED_KEY_USAGE ) == 0 )
|
|
|
|
return( 0 );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Look for the requested usage (or wildcard ANY) in our list
|
|
|
|
*/
|
|
|
|
for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
|
|
|
|
{
|
|
|
|
const x509_buf *cur_oid = &cur->buf;
|
|
|
|
|
|
|
|
if( cur_oid->len == usage_len &&
|
|
|
|
memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
|
|
|
|
{
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( OID_CMP( OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) )
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
|
|
|
|
}
|
2014-05-01 13:03:14 +02:00
|
|
|
#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
|
2014-04-10 17:53:56 +02:00
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
#if defined(POLARSSL_X509_CRL_PARSE_C)
|
2009-05-03 12:18:48 +02:00
|
|
|
/*
|
|
|
|
* Return 1 if the certificate is revoked, or 0 otherwise.
|
|
|
|
*/
|
2013-09-18 14:13:26 +02:00
|
|
|
int x509_crt_revoked( const x509_crt *crt, const x509_crl *crl )
|
2009-05-03 12:18:48 +02:00
|
|
|
{
|
2010-03-16 22:09:09 +01:00
|
|
|
const x509_crl_entry *cur = &crl->entry;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2009-05-03 12:18:48 +02:00
|
|
|
while( cur != NULL && cur->serial.len != 0 )
|
|
|
|
{
|
2011-01-16 22:38:35 +01:00
|
|
|
if( crt->serial.len == cur->serial.len &&
|
|
|
|
memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
|
2009-05-03 12:18:48 +02:00
|
|
|
{
|
2013-09-18 11:11:02 +02:00
|
|
|
if( x509_time_expired( &cur->revocation_date ) )
|
2009-05-03 12:18:48 +02:00
|
|
|
return( 1 );
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2009-05-03 12:18:48 +02:00
|
|
|
cur = cur->next;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2011-01-16 22:12:10 +01:00
|
|
|
/*
|
2013-10-29 10:02:51 +01:00
|
|
|
* Check that the given certificate is valid according to the CRL.
|
2011-01-16 22:12:10 +01:00
|
|
|
*/
|
2013-09-18 14:13:26 +02:00
|
|
|
static int x509_crt_verifycrl( x509_crt *crt, x509_crt *ca,
|
2013-09-18 13:46:23 +02:00
|
|
|
x509_crl *crl_list)
|
2011-01-16 22:12:10 +01:00
|
|
|
{
|
|
|
|
int flags = 0;
|
2013-04-07 22:00:46 +02:00
|
|
|
unsigned char hash[POLARSSL_MD_MAX_SIZE];
|
|
|
|
const md_info_t *md_info;
|
2011-01-16 22:12:10 +01:00
|
|
|
|
2012-09-28 09:10:55 +02:00
|
|
|
if( ca == NULL )
|
|
|
|
return( flags );
|
|
|
|
|
2011-01-16 22:12:10 +01:00
|
|
|
/*
|
|
|
|
* TODO: What happens if no CRL is present?
|
|
|
|
* Suggestion: Revocation state should be unknown if no CRL is present.
|
|
|
|
* For backwards compatibility this is not yet implemented.
|
|
|
|
*/
|
|
|
|
|
2012-09-28 09:10:55 +02:00
|
|
|
while( crl_list != NULL )
|
2011-01-16 22:12:10 +01:00
|
|
|
{
|
2012-09-28 09:10:55 +02:00
|
|
|
if( crl_list->version == 0 ||
|
|
|
|
crl_list->issuer_raw.len != ca->subject_raw.len ||
|
2011-01-16 22:12:10 +01:00
|
|
|
memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
|
|
|
|
crl_list->issuer_raw.len ) != 0 )
|
|
|
|
{
|
|
|
|
crl_list = crl_list->next;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-04-08 15:10:07 +02:00
|
|
|
/*
|
|
|
|
* Check if the CA is configured to sign CRLs
|
|
|
|
*/
|
|
|
|
#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
|
|
|
|
if( x509_crt_check_key_usage( ca, KU_CRL_SIGN ) != 0 )
|
|
|
|
{
|
|
|
|
flags |= BADCRL_NOT_TRUSTED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-01-16 22:12:10 +01:00
|
|
|
/*
|
|
|
|
* Check if CRL is correctly signed by the trusted CA
|
|
|
|
*/
|
2013-04-07 22:00:46 +02:00
|
|
|
md_info = md_info_from_type( crl_list->sig_md );
|
|
|
|
if( md_info == NULL )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Cannot check 'unknown' hash
|
|
|
|
*/
|
|
|
|
flags |= BADCRL_NOT_TRUSTED;
|
|
|
|
break;
|
|
|
|
}
|
2011-01-16 22:12:10 +01:00
|
|
|
|
2013-04-07 22:00:46 +02:00
|
|
|
md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
|
2011-01-16 22:12:10 +01:00
|
|
|
|
2014-06-05 17:53:52 +02:00
|
|
|
if( pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
|
|
|
|
crl_list->sig_md, hash, md_info->size,
|
|
|
|
crl_list->sig.p, crl_list->sig.len ) != 0 )
|
2011-01-16 22:12:10 +01:00
|
|
|
{
|
2013-08-12 18:41:18 +02:00
|
|
|
flags |= BADCRL_NOT_TRUSTED;
|
|
|
|
break;
|
2011-01-16 22:12:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for validity of CRL (Do not drop out)
|
|
|
|
*/
|
2013-09-18 11:11:02 +02:00
|
|
|
if( x509_time_expired( &crl_list->next_update ) )
|
2011-01-16 22:12:10 +01:00
|
|
|
flags |= BADCRL_EXPIRED;
|
|
|
|
|
2014-03-10 13:15:18 +01:00
|
|
|
if( x509_time_future( &crl_list->this_update ) )
|
|
|
|
flags |= BADCRL_FUTURE;
|
|
|
|
|
2011-01-16 22:12:10 +01:00
|
|
|
/*
|
|
|
|
* Check if certificate is revoked
|
|
|
|
*/
|
2014-06-17 16:39:18 +02:00
|
|
|
if( x509_crt_revoked( crt, crl_list ) )
|
2011-01-16 22:12:10 +01:00
|
|
|
{
|
|
|
|
flags |= BADCERT_REVOKED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
crl_list = crl_list->next;
|
|
|
|
}
|
2014-06-17 14:06:49 +02:00
|
|
|
return( flags );
|
2011-01-16 22:12:10 +01:00
|
|
|
}
|
2013-09-16 13:49:26 +02:00
|
|
|
#endif /* POLARSSL_X509_CRL_PARSE_C */
|
2011-01-16 22:12:10 +01:00
|
|
|
|
2014-10-17 11:36:18 +02:00
|
|
|
/*
|
|
|
|
* Like memcmp, but case-insensitive and always returns -1 if different
|
|
|
|
*/
|
|
|
|
static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
|
2013-09-09 17:21:45 +02:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
unsigned char diff;
|
|
|
|
const unsigned char *n1 = s1, *n2 = s2;
|
|
|
|
|
|
|
|
for( i = 0; i < len; i++ )
|
|
|
|
{
|
|
|
|
diff = n1[i] ^ n2[i];
|
|
|
|
|
2013-11-20 17:23:53 +01:00
|
|
|
if( diff == 0 )
|
2013-09-09 17:21:45 +02:00
|
|
|
continue;
|
|
|
|
|
2013-11-20 17:23:53 +01:00
|
|
|
if( diff == 32 &&
|
|
|
|
( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
|
|
|
|
( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
|
|
|
|
{
|
2013-09-09 17:21:45 +02:00
|
|
|
continue;
|
2013-11-20 17:23:53 +01:00
|
|
|
}
|
2013-09-09 17:21:45 +02:00
|
|
|
|
2014-10-17 11:36:18 +02:00
|
|
|
return( -1 );
|
2013-09-09 17:21:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2014-10-17 11:36:18 +02:00
|
|
|
/*
|
|
|
|
* Return 1 if match, 0 if not
|
|
|
|
* TODO: inverted return value!
|
|
|
|
*/
|
2013-06-25 16:25:17 +02:00
|
|
|
static int x509_wildcard_verify( const char *cn, x509_buf *name )
|
2012-02-11 17:09:32 +01:00
|
|
|
{
|
|
|
|
size_t i;
|
2014-05-28 11:33:54 +02:00
|
|
|
size_t cn_idx = 0, cn_len = strlen( cn );
|
2012-02-11 17:09:32 +01:00
|
|
|
|
2012-02-11 18:38:38 +01:00
|
|
|
if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
|
2012-02-11 17:09:32 +01:00
|
|
|
return( 0 );
|
|
|
|
|
2014-05-28 11:33:54 +02:00
|
|
|
for( i = 0; i < cn_len; ++i )
|
2012-02-11 17:09:32 +01:00
|
|
|
{
|
|
|
|
if( cn[i] == '.' )
|
|
|
|
{
|
|
|
|
cn_idx = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( cn_idx == 0 )
|
|
|
|
return( 0 );
|
|
|
|
|
2014-05-28 11:33:54 +02:00
|
|
|
if( cn_len - cn_idx == name->len - 1 &&
|
2014-10-17 11:36:18 +02:00
|
|
|
x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
|
2012-02-11 17:09:32 +01:00
|
|
|
{
|
|
|
|
return( 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2014-10-17 12:25:12 +02:00
|
|
|
/*
|
|
|
|
* Compare two X.509 strings, case-insensitive, and allowing for some encoding
|
|
|
|
* variations (but not all).
|
|
|
|
*
|
|
|
|
* Return 0 if equal, -1 otherwise.
|
|
|
|
*/
|
|
|
|
static int x509_string_cmp( const x509_buf *a, const x509_buf *b )
|
|
|
|
{
|
|
|
|
if( a->tag == b->tag &&
|
|
|
|
a->len == b->len &&
|
|
|
|
memcmp( a->p, b->p, b->len ) == 0 )
|
|
|
|
{
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ( a->tag == ASN1_UTF8_STRING || a->tag == ASN1_PRINTABLE_STRING ) &&
|
|
|
|
( b->tag == ASN1_UTF8_STRING || b->tag == ASN1_PRINTABLE_STRING ) &&
|
|
|
|
a->len == b->len &&
|
|
|
|
x509_memcasecmp( a->p, b->p, b->len ) == 0 )
|
|
|
|
{
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compare two X.509 Names (aka rdnSequence).
|
|
|
|
*
|
|
|
|
* See RFC 5280 section 7.1, though we don't implement the whole algorithm:
|
|
|
|
* we sometimes return unequal when the full algorithm would return equal,
|
|
|
|
* but never the other way. (In particular, we don't do Unicode normalisation
|
|
|
|
* or space folding.)
|
|
|
|
*
|
|
|
|
* Return 0 if equal, -1 otherwise.
|
|
|
|
*/
|
|
|
|
static int x509_name_cmp( const x509_name *a, const x509_name *b )
|
|
|
|
{
|
|
|
|
if( a == NULL && b == NULL )
|
|
|
|
return( 0 );
|
|
|
|
|
|
|
|
if( a == NULL || b == NULL )
|
|
|
|
return( -1 );
|
|
|
|
|
|
|
|
/* type */
|
|
|
|
if( a->oid.tag != b->oid.tag ||
|
|
|
|
a->oid.len != b->oid.len ||
|
|
|
|
memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 )
|
|
|
|
{
|
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* value */
|
|
|
|
if( x509_string_cmp( &a->val, &b->val ) != 0 )
|
|
|
|
return( -1 );
|
|
|
|
|
|
|
|
return( x509_name_cmp( a->next, b->next ) );
|
|
|
|
}
|
|
|
|
|
2014-04-08 13:18:01 +02:00
|
|
|
/*
|
2014-04-09 14:30:11 +02:00
|
|
|
* Check if 'parent' is a suitable parent (signing CA) for 'child'.
|
|
|
|
* Return 0 if yes, -1 if not.
|
2014-06-24 11:49:16 +02:00
|
|
|
*
|
|
|
|
* top means parent is a locally-trusted certificate
|
|
|
|
* bottom means child is the end entity cert
|
2014-04-08 13:18:01 +02:00
|
|
|
*/
|
2014-04-09 14:30:11 +02:00
|
|
|
static int x509_crt_check_parent( const x509_crt *child,
|
2014-06-19 12:18:08 +02:00
|
|
|
const x509_crt *parent,
|
2014-06-24 11:49:16 +02:00
|
|
|
int top, int bottom )
|
2014-04-08 13:18:01 +02:00
|
|
|
{
|
2014-06-24 11:49:16 +02:00
|
|
|
int need_ca_bit;
|
|
|
|
|
2014-06-19 12:18:08 +02:00
|
|
|
/* Parent must be the issuer */
|
2014-10-17 12:25:12 +02:00
|
|
|
if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
|
2014-04-09 14:30:11 +02:00
|
|
|
return( -1 );
|
2014-04-08 13:18:01 +02:00
|
|
|
|
2014-06-24 11:49:16 +02:00
|
|
|
/* Parent must have the basicConstraints CA bit set as a general rule */
|
|
|
|
need_ca_bit = 1;
|
|
|
|
|
|
|
|
/* Exception: v1/v2 certificates that are locally trusted. */
|
|
|
|
if( top && parent->version < 3 )
|
|
|
|
need_ca_bit = 0;
|
|
|
|
|
|
|
|
/* Exception: self-signed end-entity certs that are locally trusted. */
|
|
|
|
if( top && bottom &&
|
|
|
|
child->raw.len == parent->raw.len &&
|
|
|
|
memcmp( child->raw.p, parent->raw.p, child->raw.len ) == 0 )
|
2014-06-19 12:18:08 +02:00
|
|
|
{
|
2014-06-24 11:49:16 +02:00
|
|
|
need_ca_bit = 0;
|
2014-06-19 12:18:08 +02:00
|
|
|
}
|
|
|
|
|
2014-06-24 11:49:16 +02:00
|
|
|
if( need_ca_bit && ! parent->ca_istrue )
|
|
|
|
return( -1 );
|
|
|
|
|
2014-04-08 15:10:07 +02:00
|
|
|
#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
|
2014-06-24 11:49:16 +02:00
|
|
|
if( need_ca_bit &&
|
|
|
|
x509_crt_check_key_usage( parent, KU_KEY_CERT_SIGN ) != 0 )
|
|
|
|
{
|
2014-04-09 14:30:11 +02:00
|
|
|
return( -1 );
|
2014-06-24 11:49:16 +02:00
|
|
|
}
|
2014-04-08 15:10:07 +02:00
|
|
|
#endif
|
|
|
|
|
2014-04-09 14:30:11 +02:00
|
|
|
return( 0 );
|
2014-04-08 13:18:01 +02:00
|
|
|
}
|
|
|
|
|
2013-09-18 13:46:23 +02:00
|
|
|
static int x509_crt_verify_top(
|
2013-09-18 14:13:26 +02:00
|
|
|
x509_crt *child, x509_crt *trust_ca,
|
2012-11-14 13:39:52 +01:00
|
|
|
x509_crl *ca_crl, int path_cnt, int *flags,
|
2013-09-18 14:13:26 +02:00
|
|
|
int (*f_vrfy)(void *, x509_crt *, int, int *),
|
2012-09-28 09:10:55 +02:00
|
|
|
void *p_vrfy )
|
|
|
|
{
|
2013-04-07 22:00:46 +02:00
|
|
|
int ret;
|
2012-11-14 13:39:52 +01:00
|
|
|
int ca_flags = 0, check_path_cnt = path_cnt + 1;
|
2013-04-07 22:00:46 +02:00
|
|
|
unsigned char hash[POLARSSL_MD_MAX_SIZE];
|
|
|
|
const md_info_t *md_info;
|
2012-09-28 09:10:55 +02:00
|
|
|
|
2013-09-18 11:11:02 +02:00
|
|
|
if( x509_time_expired( &child->valid_to ) )
|
2012-09-28 09:10:55 +02:00
|
|
|
*flags |= BADCERT_EXPIRED;
|
|
|
|
|
2014-03-10 13:15:18 +01:00
|
|
|
if( x509_time_future( &child->valid_from ) )
|
|
|
|
*flags |= BADCERT_FUTURE;
|
|
|
|
|
2012-09-28 09:10:55 +02:00
|
|
|
/*
|
|
|
|
* Child is the top of the chain. Check against the trust_ca list.
|
|
|
|
*/
|
|
|
|
*flags |= BADCERT_NOT_TRUSTED;
|
|
|
|
|
2013-08-23 16:47:30 +02:00
|
|
|
md_info = md_info_from_type( child->sig_md );
|
|
|
|
if( md_info == NULL )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Cannot check 'unknown', no need to try any CA
|
|
|
|
*/
|
|
|
|
trust_ca = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
md( md_info, child->tbs.p, child->tbs.len, hash );
|
|
|
|
|
2014-04-09 14:30:45 +02:00
|
|
|
for( /* trust_ca */ ; trust_ca != NULL; trust_ca = trust_ca->next )
|
2012-09-28 09:10:55 +02:00
|
|
|
{
|
2014-06-24 11:49:16 +02:00
|
|
|
if( x509_crt_check_parent( child, trust_ca, 1, path_cnt == 0 ) != 0 )
|
2012-09-28 09:10:55 +02:00
|
|
|
continue;
|
|
|
|
|
2012-11-14 13:39:52 +01:00
|
|
|
/*
|
|
|
|
* Reduce path_len to check against if top of the chain is
|
|
|
|
* the same as the trusted CA
|
|
|
|
*/
|
|
|
|
if( child->subject_raw.len == trust_ca->subject_raw.len &&
|
|
|
|
memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
|
2013-08-12 18:41:18 +02:00
|
|
|
child->issuer_raw.len ) == 0 )
|
2012-11-14 13:39:52 +01:00
|
|
|
{
|
|
|
|
check_path_cnt--;
|
|
|
|
}
|
|
|
|
|
2012-09-28 09:10:55 +02:00
|
|
|
if( trust_ca->max_pathlen > 0 &&
|
2012-11-14 13:39:52 +01:00
|
|
|
trust_ca->max_pathlen < check_path_cnt )
|
2012-09-28 09:10:55 +02:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-06-05 16:34:18 +02:00
|
|
|
if( pk_verify_ext( child->sig_pk, child->sig_opts, &trust_ca->pk,
|
|
|
|
child->sig_md, hash, md_info->size,
|
|
|
|
child->sig.p, child->sig.len ) != 0 )
|
2012-09-28 09:10:55 +02:00
|
|
|
{
|
2013-08-12 18:41:18 +02:00
|
|
|
continue;
|
2013-08-09 12:30:45 +02:00
|
|
|
}
|
2012-09-28 09:10:55 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Top of chain is signed by a trusted CA
|
|
|
|
*/
|
|
|
|
*flags &= ~BADCERT_NOT_TRUSTED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-11-14 13:39:52 +01:00
|
|
|
/*
|
2012-11-24 11:53:17 +01:00
|
|
|
* If top of chain is not the same as the trusted CA send a verify request
|
|
|
|
* to the callback for any issues with validity and CRL presence for the
|
|
|
|
* trusted CA certificate.
|
2012-11-14 13:39:52 +01:00
|
|
|
*/
|
|
|
|
if( trust_ca != NULL &&
|
|
|
|
( child->subject_raw.len != trust_ca->subject_raw.len ||
|
|
|
|
memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
|
|
|
|
child->issuer_raw.len ) != 0 ) )
|
2012-09-28 09:10:55 +02:00
|
|
|
{
|
2013-09-16 13:49:26 +02:00
|
|
|
#if defined(POLARSSL_X509_CRL_PARSE_C)
|
2013-08-23 16:47:30 +02:00
|
|
|
/* Check trusted CA's CRL for the chain's top crt */
|
2013-09-18 13:46:23 +02:00
|
|
|
*flags |= x509_crt_verifycrl( child, trust_ca, ca_crl );
|
2013-09-23 12:20:02 +02:00
|
|
|
#else
|
|
|
|
((void) ca_crl);
|
2013-09-16 13:49:26 +02:00
|
|
|
#endif
|
2012-09-28 09:10:55 +02:00
|
|
|
|
2013-09-18 11:11:02 +02:00
|
|
|
if( x509_time_expired( &trust_ca->valid_to ) )
|
2012-09-28 09:10:55 +02:00
|
|
|
ca_flags |= BADCERT_EXPIRED;
|
|
|
|
|
2014-03-10 13:15:18 +01:00
|
|
|
if( x509_time_future( &trust_ca->valid_from ) )
|
|
|
|
ca_flags |= BADCERT_FUTURE;
|
|
|
|
|
2012-09-28 09:10:55 +02:00
|
|
|
if( NULL != f_vrfy )
|
|
|
|
{
|
2014-05-01 14:18:25 +02:00
|
|
|
if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1,
|
|
|
|
&ca_flags ) ) != 0 )
|
|
|
|
{
|
2012-09-28 09:10:55 +02:00
|
|
|
return( ret );
|
2014-05-01 14:18:25 +02:00
|
|
|
}
|
2012-09-28 09:10:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Call callback on top cert */
|
|
|
|
if( NULL != f_vrfy )
|
|
|
|
{
|
2014-06-17 16:39:18 +02:00
|
|
|
if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
|
2012-09-28 09:10:55 +02:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
*flags |= ca_flags;
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2013-09-18 13:46:23 +02:00
|
|
|
static int x509_crt_verify_child(
|
2013-09-18 14:13:26 +02:00
|
|
|
x509_crt *child, x509_crt *parent, x509_crt *trust_ca,
|
2012-11-14 13:39:52 +01:00
|
|
|
x509_crl *ca_crl, int path_cnt, int *flags,
|
2013-09-18 14:13:26 +02:00
|
|
|
int (*f_vrfy)(void *, x509_crt *, int, int *),
|
2012-09-28 09:10:55 +02:00
|
|
|
void *p_vrfy )
|
|
|
|
{
|
2013-04-07 22:00:46 +02:00
|
|
|
int ret;
|
2012-09-28 09:10:55 +02:00
|
|
|
int parent_flags = 0;
|
2013-04-07 22:00:46 +02:00
|
|
|
unsigned char hash[POLARSSL_MD_MAX_SIZE];
|
2013-09-18 14:13:26 +02:00
|
|
|
x509_crt *grandparent;
|
2013-04-07 22:00:46 +02:00
|
|
|
const md_info_t *md_info;
|
2012-09-28 09:10:55 +02:00
|
|
|
|
2014-04-08 11:55:03 +02:00
|
|
|
if( x509_time_expired( &child->valid_to ) )
|
|
|
|
*flags |= BADCERT_EXPIRED;
|
|
|
|
|
2014-03-10 13:15:18 +01:00
|
|
|
if( x509_time_future( &child->valid_from ) )
|
|
|
|
*flags |= BADCERT_FUTURE;
|
2012-09-28 09:10:55 +02:00
|
|
|
|
2013-04-07 22:00:46 +02:00
|
|
|
md_info = md_info_from_type( child->sig_md );
|
|
|
|
if( md_info == NULL )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Cannot check 'unknown' hash
|
|
|
|
*/
|
|
|
|
*flags |= BADCERT_NOT_TRUSTED;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
md( md_info, child->tbs.p, child->tbs.len, hash );
|
2012-09-28 09:10:55 +02:00
|
|
|
|
2014-06-05 16:34:18 +02:00
|
|
|
if( pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
|
|
|
|
child->sig_md, hash, md_info->size,
|
|
|
|
child->sig.p, child->sig.len ) != 0 )
|
2013-07-11 10:46:21 +02:00
|
|
|
{
|
2013-08-12 18:41:18 +02:00
|
|
|
*flags |= BADCERT_NOT_TRUSTED;
|
2013-07-11 10:46:21 +02:00
|
|
|
}
|
2013-04-07 22:00:46 +02:00
|
|
|
}
|
2012-09-28 09:10:55 +02:00
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
#if defined(POLARSSL_X509_CRL_PARSE_C)
|
2012-09-28 09:10:55 +02:00
|
|
|
/* Check trusted CA's CRL for the given crt */
|
2013-09-18 13:46:23 +02:00
|
|
|
*flags |= x509_crt_verifycrl(child, parent, ca_crl);
|
2013-09-16 13:49:26 +02:00
|
|
|
#endif
|
2012-09-28 09:10:55 +02:00
|
|
|
|
2014-04-09 14:30:11 +02:00
|
|
|
/* Look for a grandparent upwards the chain */
|
|
|
|
for( grandparent = parent->next;
|
|
|
|
grandparent != NULL;
|
|
|
|
grandparent = grandparent->next )
|
|
|
|
{
|
2014-06-24 11:49:16 +02:00
|
|
|
if( x509_crt_check_parent( parent, grandparent,
|
|
|
|
0, path_cnt == 0 ) == 0 )
|
2014-04-09 14:30:11 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Is our parent part of the chain or at the top? */
|
|
|
|
if( grandparent != NULL )
|
2012-09-28 09:10:55 +02:00
|
|
|
{
|
2014-04-09 14:30:45 +02:00
|
|
|
ret = x509_crt_verify_child( parent, grandparent, trust_ca, ca_crl,
|
|
|
|
path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
|
2012-09-28 09:10:55 +02:00
|
|
|
if( ret != 0 )
|
|
|
|
return( ret );
|
2013-09-09 17:21:45 +02:00
|
|
|
}
|
2012-09-28 09:10:55 +02:00
|
|
|
else
|
|
|
|
{
|
2014-04-09 14:30:45 +02:00
|
|
|
ret = x509_crt_verify_top( parent, trust_ca, ca_crl,
|
|
|
|
path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
|
2012-09-28 09:10:55 +02:00
|
|
|
if( ret != 0 )
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* child is verified to be a child of the parent, call verify callback */
|
|
|
|
if( NULL != f_vrfy )
|
2012-11-14 13:39:52 +01:00
|
|
|
if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
|
2012-09-28 09:10:55 +02:00
|
|
|
return( ret );
|
|
|
|
|
|
|
|
*flags |= parent_flags;
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
|
|
|
* Verify the certificate validity
|
|
|
|
*/
|
2013-09-18 14:13:26 +02:00
|
|
|
int x509_crt_verify( x509_crt *crt,
|
|
|
|
x509_crt *trust_ca,
|
2013-09-18 13:46:23 +02:00
|
|
|
x509_crl *ca_crl,
|
|
|
|
const char *cn, int *flags,
|
2013-09-18 14:13:26 +02:00
|
|
|
int (*f_vrfy)(void *, x509_crt *, int, int *),
|
2013-09-18 13:46:23 +02:00
|
|
|
void *p_vrfy )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2011-04-24 10:57:21 +02:00
|
|
|
size_t cn_len;
|
2012-09-28 09:10:55 +02:00
|
|
|
int ret;
|
2012-11-14 13:39:52 +01:00
|
|
|
int pathlen = 0;
|
2013-09-18 14:13:26 +02:00
|
|
|
x509_crt *parent;
|
2009-01-03 22:22:43 +01:00
|
|
|
x509_name *name;
|
2012-02-11 17:09:32 +01:00
|
|
|
x509_sequence *cur = NULL;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2009-05-03 12:18:48 +02:00
|
|
|
*flags = 0;
|
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
if( cn != NULL )
|
|
|
|
{
|
|
|
|
name = &crt->subject;
|
|
|
|
cn_len = strlen( cn );
|
|
|
|
|
2012-05-10 16:12:46 +02:00
|
|
|
if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2012-05-10 16:12:46 +02:00
|
|
|
cur = &crt->subject_alt_names;
|
|
|
|
|
|
|
|
while( cur != NULL )
|
2012-02-11 17:09:32 +01:00
|
|
|
{
|
2012-08-23 12:49:55 +02:00
|
|
|
if( cur->buf.len == cn_len &&
|
2014-10-17 11:36:18 +02:00
|
|
|
x509_memcasecmp( cn, cur->buf.p, cn_len ) == 0 )
|
2012-02-11 17:09:32 +01:00
|
|
|
break;
|
|
|
|
|
2012-08-23 12:49:55 +02:00
|
|
|
if( cur->buf.len > 2 &&
|
|
|
|
memcmp( cur->buf.p, "*.", 2 ) == 0 &&
|
2012-05-10 16:12:46 +02:00
|
|
|
x509_wildcard_verify( cn, &cur->buf ) )
|
2012-02-11 17:09:32 +01:00
|
|
|
break;
|
2012-05-10 16:12:46 +02:00
|
|
|
|
|
|
|
cur = cur->next;
|
2012-02-11 17:09:32 +01:00
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2012-05-10 16:12:46 +02:00
|
|
|
if( cur == NULL )
|
|
|
|
*flags |= BADCERT_CN_MISMATCH;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
2012-05-10 16:12:46 +02:00
|
|
|
else
|
2012-02-11 17:09:32 +01:00
|
|
|
{
|
2012-05-10 16:12:46 +02:00
|
|
|
while( name != NULL )
|
2012-02-11 17:09:32 +01:00
|
|
|
{
|
2013-04-07 22:00:46 +02:00
|
|
|
if( OID_CMP( OID_AT_CN, &name->oid ) )
|
2012-02-11 17:09:32 +01:00
|
|
|
{
|
2012-08-23 12:49:55 +02:00
|
|
|
if( name->val.len == cn_len &&
|
2014-10-17 11:36:18 +02:00
|
|
|
x509_memcasecmp( name->val.p, cn, cn_len ) == 0 )
|
2012-02-11 17:09:32 +01:00
|
|
|
break;
|
|
|
|
|
2012-08-23 12:49:55 +02:00
|
|
|
if( name->val.len > 2 &&
|
|
|
|
memcmp( name->val.p, "*.", 2 ) == 0 &&
|
2012-05-10 16:12:46 +02:00
|
|
|
x509_wildcard_verify( cn, &name->val ) )
|
2012-02-11 18:38:38 +01:00
|
|
|
break;
|
2012-02-11 17:09:32 +01:00
|
|
|
}
|
2012-05-10 16:12:46 +02:00
|
|
|
|
|
|
|
name = name->next;
|
2012-02-11 17:09:32 +01:00
|
|
|
}
|
|
|
|
|
2012-05-10 16:12:46 +02:00
|
|
|
if( name == NULL )
|
2012-02-11 17:09:32 +01:00
|
|
|
*flags |= BADCERT_CN_MISMATCH;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2014-04-09 14:30:11 +02:00
|
|
|
/* Look for a parent upwards the chain */
|
|
|
|
for( parent = crt->next; parent != NULL; parent = parent->next )
|
|
|
|
{
|
2014-06-24 11:49:16 +02:00
|
|
|
if( x509_crt_check_parent( crt, parent, 0, pathlen == 0 ) == 0 )
|
2014-04-09 14:30:11 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Are we part of the chain or at the top? */
|
|
|
|
if( parent != NULL )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2014-04-09 14:30:45 +02:00
|
|
|
ret = x509_crt_verify_child( crt, parent, trust_ca, ca_crl,
|
|
|
|
pathlen, flags, f_vrfy, p_vrfy );
|
2012-09-28 09:10:55 +02:00
|
|
|
if( ret != 0 )
|
|
|
|
return( ret );
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
2012-09-28 09:10:55 +02:00
|
|
|
else
|
2011-01-15 17:57:55 +01:00
|
|
|
{
|
2014-04-09 14:30:45 +02:00
|
|
|
ret = x509_crt_verify_top( crt, trust_ca, ca_crl,
|
|
|
|
pathlen, flags, f_vrfy, p_vrfy );
|
2012-09-28 09:10:55 +02:00
|
|
|
if( ret != 0 )
|
|
|
|
return( ret );
|
2011-01-13 18:54:59 +01:00
|
|
|
}
|
2012-09-28 09:10:55 +02:00
|
|
|
|
|
|
|
if( *flags != 0 )
|
2011-01-16 22:12:10 +01:00
|
|
|
return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
|
2011-01-13 18:54:59 +01:00
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2013-09-18 11:58:25 +02:00
|
|
|
/*
|
|
|
|
* Initialize a certificate chain
|
|
|
|
*/
|
2013-09-18 14:13:26 +02:00
|
|
|
void x509_crt_init( x509_crt *crt )
|
2013-09-18 11:58:25 +02:00
|
|
|
{
|
2013-09-18 14:13:26 +02:00
|
|
|
memset( crt, 0, sizeof(x509_crt) );
|
2013-09-18 11:58:25 +02:00
|
|
|
}
|
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
|
|
|
* Unallocate all certificate data
|
|
|
|
*/
|
2013-09-18 14:13:26 +02:00
|
|
|
void x509_crt_free( x509_crt *crt )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2013-09-18 14:13:26 +02:00
|
|
|
x509_crt *cert_cur = crt;
|
|
|
|
x509_crt *cert_prv;
|
2009-01-03 22:22:43 +01:00
|
|
|
x509_name *name_cur;
|
|
|
|
x509_name *name_prv;
|
2011-01-15 17:57:55 +01:00
|
|
|
x509_sequence *seq_cur;
|
|
|
|
x509_sequence *seq_prv;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
if( crt == NULL )
|
|
|
|
return;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2013-07-10 14:32:58 +02:00
|
|
|
pk_free( &cert_cur->pk );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
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( cert_cur->sig_opts );
|
|
|
|
#endif
|
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
name_cur = cert_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-07-03 13:37:05 +02:00
|
|
|
polarssl_free( name_prv );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
name_cur = cert_cur->subject.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-07-03 13:37:05 +02:00
|
|
|
polarssl_free( name_prv );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2011-01-15 17:57:55 +01:00
|
|
|
seq_cur = cert_cur->ext_key_usage.next;
|
|
|
|
while( seq_cur != NULL )
|
|
|
|
{
|
|
|
|
seq_prv = seq_cur;
|
|
|
|
seq_cur = seq_cur->next;
|
2014-06-13 17:20:13 +02:00
|
|
|
polarssl_zeroize( seq_prv, sizeof( x509_sequence ) );
|
2013-07-03 13:37:05 +02:00
|
|
|
polarssl_free( seq_prv );
|
2011-01-15 17:57:55 +01:00
|
|
|
}
|
|
|
|
|
2012-02-11 19:42:45 +01:00
|
|
|
seq_cur = cert_cur->subject_alt_names.next;
|
|
|
|
while( seq_cur != NULL )
|
|
|
|
{
|
|
|
|
seq_prv = seq_cur;
|
|
|
|
seq_cur = seq_cur->next;
|
2014-06-13 17:20:13 +02:00
|
|
|
polarssl_zeroize( seq_prv, sizeof( x509_sequence ) );
|
2013-07-03 13:37:05 +02:00
|
|
|
polarssl_free( seq_prv );
|
2012-02-11 19:42:45 +01:00
|
|
|
}
|
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
if( cert_cur->raw.p != NULL )
|
|
|
|
{
|
2014-06-13 17:20:13 +02:00
|
|
|
polarssl_zeroize( cert_cur->raw.p, cert_cur->raw.len );
|
2013-07-03 13:37:05 +02:00
|
|
|
polarssl_free( cert_cur->raw.p );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
cert_cur = cert_cur->next;
|
|
|
|
}
|
|
|
|
while( cert_cur != NULL );
|
|
|
|
|
|
|
|
cert_cur = crt;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
cert_prv = cert_cur;
|
|
|
|
cert_cur = cert_cur->next;
|
|
|
|
|
2014-06-13 17:20:13 +02:00
|
|
|
polarssl_zeroize( cert_prv, sizeof( x509_crt ) );
|
2009-01-03 22:22:43 +01:00
|
|
|
if( cert_prv != crt )
|
2013-07-03 13:37:05 +02:00
|
|
|
polarssl_free( cert_prv );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
while( cert_cur != NULL );
|
|
|
|
}
|
|
|
|
|
2014-05-01 13:03:14 +02:00
|
|
|
#endif /* POLARSSL_X509_CRT_PARSE_C */
|