2009-01-03 22:22:43 +01:00
|
|
|
/*
|
|
|
|
* X.509 certificate and private key decoding
|
|
|
|
*
|
2013-06-25 16:25:17 +02:00
|
|
|
* Copyright (C) 2006-2013, 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
|
|
|
*
|
|
|
|
* http://www.ietf.org/rfc/rfc3279.txt
|
2012-02-16 16:28:14 +01:00
|
|
|
* http://www.ietf.org/rfc/rfc3280.txt
|
2009-01-03 22:22:43 +01:00
|
|
|
*
|
|
|
|
* ftp://ftp.rsasecurity.com/pub/pkcs/ascii/pkcs-1v2.asc
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2009-01-03 22:51:57 +01:00
|
|
|
#include "polarssl/config.h"
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2009-01-03 22:51:57 +01:00
|
|
|
#if defined(POLARSSL_X509_PARSE_C)
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2009-01-03 22:51:57 +01:00
|
|
|
#include "polarssl/x509.h"
|
2011-11-10 15:43:23 +01:00
|
|
|
#include "polarssl/asn1.h"
|
2013-04-07 22:00:46 +02:00
|
|
|
#include "polarssl/oid.h"
|
2011-02-12 15:30:57 +01:00
|
|
|
#include "polarssl/pem.h"
|
2011-01-06 16:48:19 +01:00
|
|
|
#include "polarssl/dhm.h"
|
2013-06-24 19:28:55 +02:00
|
|
|
#if defined(POLARSSL_PKCS5_C)
|
|
|
|
#include "polarssl/pkcs5.h"
|
|
|
|
#endif
|
2013-06-24 19:33:27 +02:00
|
|
|
#if defined(POLARSSL_PKCS12_C)
|
|
|
|
#include "polarssl/pkcs12.h"
|
|
|
|
#endif
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2013-07-03 13:37:05 +02:00
|
|
|
#if defined(POLARSSL_MEMORY_C)
|
|
|
|
#include "polarssl/memory.h"
|
|
|
|
#else
|
|
|
|
#define polarssl_malloc malloc
|
|
|
|
#define polarssl_free free
|
|
|
|
#endif
|
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2011-12-04 23:11:35 +01:00
|
|
|
#if defined(_WIN32)
|
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
|
|
|
|
2011-04-25 17:28:35 +02:00
|
|
|
#if defined(POLARSSL_FS_IO)
|
|
|
|
#include <stdio.h>
|
2012-11-02 12:06:08 +01:00
|
|
|
#if !defined(_WIN32)
|
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
|
|
|
|
|
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 )
|
2011-05-09 18:17:09 +02:00
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
if( *p != end )
|
2011-05-09 18:17:09 +02:00
|
|
|
return( POLARSSL_ERR_X509_CERT_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 );
|
|
|
|
}
|
|
|
|
|
2011-10-12 11:55:01 +02:00
|
|
|
/*
|
2011-10-12 13:53:52 +02:00
|
|
|
* Version ::= INTEGER { v1(0), v2(1) }
|
2011-10-12 11:55:01 +02:00
|
|
|
*/
|
|
|
|
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 )
|
2011-10-19 16:15:17 +02:00
|
|
|
{
|
|
|
|
*ver = 0;
|
|
|
|
return( 0 );
|
|
|
|
}
|
2011-10-12 11:55:01 +02:00
|
|
|
|
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
|
|
|
* CertificateSerialNumber ::= INTEGER
|
|
|
|
*/
|
|
|
|
static int x509_get_serial( 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 *serial )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if( ( end - *p ) < 1 )
|
2011-05-09 18:17:09 +02:00
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
|
2009-01-03 22:51:57 +01:00
|
|
|
POLARSSL_ERR_ASN1_OUT_OF_DATA );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
if( **p != ( ASN1_CONTEXT_SPECIFIC | ASN1_PRIMITIVE | 2 ) &&
|
|
|
|
**p != ASN1_INTEGER )
|
2011-05-09 18:17:09 +02:00
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
|
2009-01-03 22:51:57 +01:00
|
|
|
POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
serial->tag = *(*p)++;
|
|
|
|
|
|
|
|
if( ( ret = asn1_get_len( p, end, &serial->len ) ) != 0 )
|
2011-05-09 18:17:09 +02:00
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL + ret );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
serial->p = *p;
|
|
|
|
*p += serial->len;
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2013-07-10 13:18:41 +02:00
|
|
|
/* Get a PK algorithm identifier
|
2013-07-08 18:26:18 +02:00
|
|
|
*
|
|
|
|
* AlgorithmIdentifier ::= SEQUENCE {
|
|
|
|
* algorithm OBJECT IDENTIFIER,
|
|
|
|
* parameters ANY DEFINED BY algorithm OPTIONAL }
|
|
|
|
*/
|
2013-07-10 12:55:08 +02:00
|
|
|
static int x509_get_pk_alg( unsigned char **p,
|
2013-07-10 13:18:41 +02:00
|
|
|
const unsigned char *end,
|
|
|
|
pk_type_t *pk_alg, x509_buf *params )
|
2013-07-08 18:26:18 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
x509_buf alg_oid;
|
|
|
|
|
|
|
|
memset( params, 0, sizeof(asn1_buf) );
|
|
|
|
|
|
|
|
if( ( ret = asn1_get_alg( p, end, &alg_oid, params ) ) != 0 )
|
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
|
|
|
|
|
|
|
|
if( oid_get_pk_alg( &alg_oid, pk_alg ) != 0 )
|
|
|
|
return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* No parameters with RSA (only for EC)
|
|
|
|
*/
|
|
|
|
if( *pk_alg == POLARSSL_PK_RSA &&
|
|
|
|
( ( params->tag != ASN1_NULL && params->tag != 0 ) ||
|
|
|
|
params->len != 0 ) )
|
|
|
|
{
|
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_ALG );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2013-07-10 13:18:41 +02:00
|
|
|
/* Get an algorithm identifier without parameters (eg for signatures)
|
|
|
|
*
|
2009-01-03 22:22:43 +01:00
|
|
|
* AlgorithmIdentifier ::= SEQUENCE {
|
|
|
|
* algorithm OBJECT IDENTIFIER,
|
|
|
|
* parameters ANY DEFINED BY algorithm OPTIONAL }
|
|
|
|
*/
|
2013-07-10 13:18:41 +02:00
|
|
|
static int x509_get_alg_null( unsigned char **p, const unsigned char *end,
|
|
|
|
x509_buf *alg )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2011-04-24 10:57:21 +02:00
|
|
|
int ret;
|
2013-07-01 15:27:48 +02:00
|
|
|
|
2013-07-10 13:18:41 +02:00
|
|
|
if( ( ret = asn1_get_alg_null( p, end, alg ) ) != 0 )
|
2013-07-01 15:27:48 +02:00
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
|
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2013-07-11 16:17:23 +02:00
|
|
|
|
|
|
|
#if defined(POLARSSL_ECP_C)
|
2013-07-02 14:56:43 +02:00
|
|
|
/* Get an EC group id from an ECParameters buffer
|
|
|
|
*
|
|
|
|
* ECParameters ::= CHOICE {
|
|
|
|
* namedCurve OBJECT IDENTIFIER
|
|
|
|
* -- implicitCurve NULL
|
|
|
|
* -- specifiedCurve SpecifiedECDomain
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
static int x509_get_ecparams( unsigned char **p, const unsigned char *end,
|
2013-07-11 13:17:43 +02:00
|
|
|
x509_buf *params )
|
2013-07-02 14:56:43 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2013-07-11 13:17:43 +02:00
|
|
|
params->tag = **p;
|
2013-07-02 14:56:43 +02:00
|
|
|
|
2013-07-11 13:17:43 +02:00
|
|
|
if( ( ret = asn1_get_tag( p, end, ¶ms->len, ASN1_OID ) ) != 0 )
|
2013-07-02 14:56:43 +02:00
|
|
|
return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
|
|
|
|
|
2013-07-11 13:17:43 +02:00
|
|
|
params->p = *p;
|
|
|
|
*p += params->len;
|
2013-07-02 14:56:43 +02:00
|
|
|
|
|
|
|
if( *p != end )
|
|
|
|
return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
|
|
|
|
POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
|
|
|
|
|
2013-07-11 13:17:43 +02:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Use EC parameters to initialise an EC group
|
|
|
|
*/
|
|
|
|
static int x509_use_ecparams( const x509_buf *params, ecp_group *grp )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
ecp_group_id grp_id;
|
|
|
|
|
|
|
|
if( oid_get_ec_grp( params, &grp_id ) != 0 )
|
|
|
|
return( POLARSSL_ERR_X509_UNKNOWN_NAMED_CURVE );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* grp may already be initilialized; if so, make sure IDs match
|
|
|
|
*/
|
|
|
|
if( grp->id != POLARSSL_ECP_DP_NONE && grp->id != grp_id )
|
|
|
|
return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
|
|
|
|
|
|
|
|
if( ( ret = ecp_use_known_dp( grp, grp_id ) ) != 0 )
|
|
|
|
return( ret );
|
|
|
|
|
|
|
|
return( 0 );
|
2013-07-02 14:56:43 +02:00
|
|
|
}
|
2013-07-11 16:17:23 +02:00
|
|
|
#endif /* POLARSSL_ECP_C */
|
2013-07-02 14:56:43 +02:00
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
|
|
|
* AttributeTypeAndValue ::= SEQUENCE {
|
|
|
|
* type AttributeType,
|
|
|
|
* value AttributeValue }
|
|
|
|
*
|
|
|
|
* AttributeType ::= OBJECT IDENTIFIER
|
|
|
|
*
|
|
|
|
* AttributeValue ::= ANY DEFINED BY AttributeType
|
|
|
|
*/
|
2011-02-20 11:40:16 +01:00
|
|
|
static int x509_get_attr_type_value( unsigned char **p,
|
|
|
|
const unsigned char *end,
|
|
|
|
x509_name *cur )
|
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
|
|
|
x509_buf *oid;
|
|
|
|
x509_buf *val;
|
|
|
|
|
|
|
|
if( ( ret = asn1_get_tag( p, end, &len,
|
|
|
|
ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
|
2011-05-09 18:17:09 +02:00
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
oid = &cur->oid;
|
|
|
|
oid->tag = **p;
|
|
|
|
|
|
|
|
if( ( ret = asn1_get_tag( p, end, &oid->len, ASN1_OID ) ) != 0 )
|
2011-05-09 18:17:09 +02:00
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
oid->p = *p;
|
|
|
|
*p += oid->len;
|
|
|
|
|
|
|
|
if( ( end - *p ) < 1 )
|
2011-05-09 18:17:09 +02:00
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
|
2009-01-03 22:51:57 +01:00
|
|
|
POLARSSL_ERR_ASN1_OUT_OF_DATA );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
if( **p != ASN1_BMP_STRING && **p != ASN1_UTF8_STRING &&
|
|
|
|
**p != ASN1_T61_STRING && **p != ASN1_PRINTABLE_STRING &&
|
|
|
|
**p != ASN1_IA5_STRING && **p != ASN1_UNIVERSAL_STRING )
|
2011-05-09 18:17:09 +02:00
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
|
2009-01-03 22:51:57 +01:00
|
|
|
POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
val = &cur->val;
|
|
|
|
val->tag = *(*p)++;
|
|
|
|
|
|
|
|
if( ( ret = asn1_get_len( p, end, &val->len ) ) != 0 )
|
2011-05-09 18:17:09 +02:00
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
val->p = *p;
|
|
|
|
*p += val->len;
|
|
|
|
|
|
|
|
cur->next = NULL;
|
|
|
|
|
2011-02-20 11:40:16 +01:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* RelativeDistinguishedName ::=
|
|
|
|
* SET OF AttributeTypeAndValue
|
|
|
|
*
|
|
|
|
* AttributeTypeAndValue ::= SEQUENCE {
|
|
|
|
* type AttributeType,
|
|
|
|
* value AttributeValue }
|
|
|
|
*
|
|
|
|
* AttributeType ::= OBJECT IDENTIFIER
|
|
|
|
*
|
|
|
|
* AttributeValue ::= ANY DEFINED BY AttributeType
|
|
|
|
*/
|
|
|
|
static int x509_get_name( unsigned char **p,
|
|
|
|
const unsigned char *end,
|
|
|
|
x509_name *cur )
|
|
|
|
{
|
2011-04-24 10:57:21 +02:00
|
|
|
int ret;
|
|
|
|
size_t len;
|
2011-02-20 11:40:16 +01:00
|
|
|
const unsigned char *end2;
|
|
|
|
x509_name *use;
|
|
|
|
|
|
|
|
if( ( ret = asn1_get_tag( p, end, &len,
|
|
|
|
ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 )
|
2011-05-09 18:17:09 +02:00
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
|
2011-02-20 11:40:16 +01:00
|
|
|
|
|
|
|
end2 = end;
|
|
|
|
end = *p + len;
|
|
|
|
use = cur;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if( ( ret = x509_get_attr_type_value( p, end, use ) ) != 0 )
|
|
|
|
return( ret );
|
|
|
|
|
|
|
|
if( *p != end )
|
|
|
|
{
|
2013-07-03 13:37:05 +02:00
|
|
|
use->next = (x509_name *) polarssl_malloc(
|
2011-02-20 11:40:16 +01:00
|
|
|
sizeof( x509_name ) );
|
|
|
|
|
|
|
|
if( use->next == NULL )
|
2011-12-10 22:55:01 +01:00
|
|
|
return( POLARSSL_ERR_X509_MALLOC_FAILED );
|
2011-02-20 11:40:16 +01:00
|
|
|
|
|
|
|
memset( use->next, 0, sizeof( x509_name ) );
|
|
|
|
|
|
|
|
use = use->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while( *p != end );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* recurse until end of SEQUENCE is reached
|
|
|
|
*/
|
|
|
|
if( *p == end2 )
|
|
|
|
return( 0 );
|
|
|
|
|
2013-07-03 13:37:05 +02:00
|
|
|
cur->next = (x509_name *) polarssl_malloc(
|
2009-01-03 22:22:43 +01:00
|
|
|
sizeof( x509_name ) );
|
|
|
|
|
|
|
|
if( cur->next == NULL )
|
2011-12-10 22:55:01 +01:00
|
|
|
return( POLARSSL_ERR_X509_MALLOC_FAILED );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2012-05-01 10:14:20 +02:00
|
|
|
memset( cur->next, 0, sizeof( x509_name ) );
|
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
return( x509_get_name( p, end2, cur->next ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Time ::= CHOICE {
|
|
|
|
* utcTime UTCTime,
|
|
|
|
* generalTime GeneralizedTime }
|
|
|
|
*/
|
2010-02-18 22:26:15 +01:00
|
|
|
static int x509_get_time( 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 *time )
|
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
|
|
|
char date[64];
|
2010-02-18 22:26:15 +01:00
|
|
|
unsigned char tag;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2010-02-18 22:26:15 +01:00
|
|
|
if( ( end - *p ) < 1 )
|
2011-05-09 18:17:09 +02:00
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
|
|
|
|
POLARSSL_ERR_ASN1_OUT_OF_DATA );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2010-02-18 22:26:15 +01:00
|
|
|
tag = **p;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2010-02-18 22:26:15 +01:00
|
|
|
if ( tag == ASN1_UTC_TIME )
|
|
|
|
{
|
|
|
|
(*p)++;
|
|
|
|
ret = asn1_get_len( p, end, &len );
|
|
|
|
|
|
|
|
if( ret != 0 )
|
2011-05-09 18:17:09 +02:00
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2010-02-18 22:26:15 +01:00
|
|
|
memset( date, 0, sizeof( date ) );
|
2011-06-09 15:55:13 +02:00
|
|
|
memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
|
|
|
|
len : sizeof( date ) - 1 );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2010-02-18 22:26:15 +01:00
|
|
|
if( sscanf( date, "%2d%2d%2d%2d%2d%2d",
|
|
|
|
&time->year, &time->mon, &time->day,
|
|
|
|
&time->hour, &time->min, &time->sec ) < 5 )
|
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2011-02-20 11:40:16 +01:00
|
|
|
time->year += 100 * ( time->year < 50 );
|
2010-02-18 22:26:15 +01:00
|
|
|
time->year += 1900;
|
|
|
|
|
|
|
|
*p += len;
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
else if ( tag == ASN1_GENERALIZED_TIME )
|
|
|
|
{
|
|
|
|
(*p)++;
|
|
|
|
ret = asn1_get_len( p, end, &len );
|
|
|
|
|
|
|
|
if( ret != 0 )
|
2011-05-09 18:17:09 +02:00
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
|
2010-02-18 22:26:15 +01:00
|
|
|
|
|
|
|
memset( date, 0, sizeof( date ) );
|
2011-06-09 15:55:13 +02:00
|
|
|
memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
|
|
|
|
len : sizeof( date ) - 1 );
|
2010-02-18 22:26:15 +01:00
|
|
|
|
|
|
|
if( sscanf( date, "%4d%2d%2d%2d%2d%2d",
|
|
|
|
&time->year, &time->mon, &time->day,
|
|
|
|
&time->hour, &time->min, &time->sec ) < 5 )
|
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
|
|
|
|
|
|
|
|
*p += len;
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
else
|
2011-05-09 18:17:09 +02:00
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_DATE + POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
|
2009-05-02 17:13:40 +02:00
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
|
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 )
|
2011-05-09 18:17:09 +02:00
|
|
|
return( POLARSSL_ERR_X509_CERT_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 )
|
2011-05-09 18:17:09 +02:00
|
|
|
return( POLARSSL_ERR_X509_CERT_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 );
|
|
|
|
}
|
|
|
|
|
2013-07-11 16:17:23 +02:00
|
|
|
#if defined(POLARSSL_RSA_C)
|
2013-07-09 12:32:51 +02:00
|
|
|
/*
|
|
|
|
* RSAPublicKey ::= SEQUENCE {
|
|
|
|
* modulus INTEGER, -- n
|
|
|
|
* publicExponent INTEGER -- e
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
static int x509_get_rsapubkey( unsigned char **p,
|
|
|
|
const unsigned char *end,
|
|
|
|
rsa_context *rsa )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
if( ( ret = asn1_get_tag( p, end, &len,
|
|
|
|
ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
|
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
|
|
|
|
|
|
|
|
if( *p + len != end )
|
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
|
|
|
|
POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
|
|
|
|
|
|
|
|
if( ( ret = asn1_get_mpi( p, end, &rsa->N ) ) != 0 ||
|
|
|
|
( ret = asn1_get_mpi( p, end, &rsa->E ) ) != 0 )
|
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
|
|
|
|
|
|
|
|
if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
|
|
|
|
return( ret );
|
|
|
|
|
|
|
|
rsa->len = mpi_size( &rsa->N );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
2013-07-11 16:17:23 +02:00
|
|
|
#endif /* POLARSSL_RSA_C */
|
2013-07-09 12:32:51 +02:00
|
|
|
|
2013-07-11 16:17:23 +02:00
|
|
|
#if defined(POLARSSL_ECP_C)
|
2013-07-11 15:32:03 +02:00
|
|
|
/*
|
|
|
|
* EC public key is an EC point
|
|
|
|
*/
|
2013-07-09 12:54:04 +02:00
|
|
|
static int x509_get_ecpubkey( unsigned char **p, const unsigned char *end,
|
2013-07-11 13:59:02 +02:00
|
|
|
ecp_keypair *key )
|
2013-07-09 12:54:04 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if( ( ret = ecp_point_read_binary( &key->grp, &key->Q,
|
2013-07-11 13:59:02 +02:00
|
|
|
(const unsigned char *) *p, end - *p ) ) != 0 ||
|
|
|
|
( ret = ecp_check_pubkey( &key->grp, &key->Q ) ) != 0 )
|
2013-07-09 12:54:04 +02:00
|
|
|
{
|
2013-07-11 13:59:02 +02:00
|
|
|
ecp_keypair_free( key );
|
2013-07-10 16:07:25 +02:00
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY );
|
|
|
|
}
|
|
|
|
|
2013-07-11 13:59:02 +02:00
|
|
|
/*
|
|
|
|
* We know ecp_point_read_binary consumed all bytes
|
|
|
|
*/
|
2013-07-10 16:07:25 +02:00
|
|
|
*p = (unsigned char *) end;
|
|
|
|
|
2013-07-09 12:54:04 +02:00
|
|
|
return( 0 );
|
|
|
|
}
|
2013-07-11 16:17:23 +02:00
|
|
|
#endif /* POLARSSL_ECP_C */
|
2013-07-09 12:54:04 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* SubjectPublicKeyInfo ::= SEQUENCE {
|
|
|
|
* algorithm AlgorithmIdentifier,
|
|
|
|
* subjectPublicKey BIT STRING }
|
|
|
|
*/
|
|
|
|
static int x509_get_pubkey( unsigned char **p,
|
|
|
|
const unsigned char *end,
|
|
|
|
pk_context *pk )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t len;
|
|
|
|
x509_buf alg_params;
|
|
|
|
pk_type_t pk_alg = POLARSSL_PK_NONE;
|
|
|
|
|
|
|
|
if( ( ret = asn1_get_tag( p, end, &len,
|
|
|
|
ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
|
|
|
|
{
|
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
end = *p + len;
|
|
|
|
|
2013-07-10 12:55:08 +02:00
|
|
|
if( ( ret = x509_get_pk_alg( p, end, &pk_alg, &alg_params ) ) != 0 )
|
2013-07-09 12:54:04 +02:00
|
|
|
return( ret );
|
|
|
|
|
2013-07-11 13:59:02 +02:00
|
|
|
if( ( ret = asn1_get_bitstring_null( p, end, &len ) ) != 0 )
|
2013-07-09 12:54:04 +02:00
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
|
|
|
|
|
|
|
|
if( *p + len != end )
|
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
|
|
|
|
POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
|
|
|
|
|
2013-07-09 13:10:49 +02:00
|
|
|
if( ( ret = pk_set_type( pk, pk_alg ) ) != 0 )
|
|
|
|
return( ret );
|
2013-07-09 12:54:04 +02:00
|
|
|
|
2013-07-11 16:17:23 +02:00
|
|
|
#if defined(POLARSSL_RSA_C)
|
|
|
|
if( pk_alg == POLARSSL_PK_RSA )
|
2013-07-09 12:54:04 +02:00
|
|
|
{
|
2013-07-11 16:17:23 +02:00
|
|
|
ret = x509_get_rsapubkey( p, end, pk_rsa( *pk ) );
|
|
|
|
} else
|
|
|
|
#endif /* POLARSSL_RSA_C */
|
|
|
|
#if defined(POLARSSL_ECP_C)
|
|
|
|
if( pk_alg == POLARSSL_PK_ECKEY_DH || pk_alg == POLARSSL_PK_ECKEY )
|
|
|
|
{
|
|
|
|
ret = x509_use_ecparams( &alg_params, &pk_ec( *pk )->grp ) ||
|
|
|
|
x509_get_ecpubkey( p, end, pk_ec( *pk ) );
|
|
|
|
} else
|
|
|
|
#endif /* POLARSSL_ECP_C */
|
|
|
|
ret = POLARSSL_ERR_X509_UNKNOWN_PK_ALG;
|
2013-07-09 13:10:49 +02:00
|
|
|
|
2013-07-10 16:07:25 +02:00
|
|
|
if( ret == 0 && *p != end )
|
|
|
|
ret = POLARSSL_ERR_X509_CERT_INVALID_PUBKEY
|
|
|
|
POLARSSL_ERR_ASN1_LENGTH_MISMATCH;
|
|
|
|
|
2013-07-09 13:10:49 +02:00
|
|
|
if( ret != 0 )
|
|
|
|
pk_free( pk );
|
|
|
|
|
|
|
|
return( ret );
|
2013-07-09 12:54:04 +02:00
|
|
|
}
|
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
static int x509_get_sig( 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 *sig )
|
|
|
|
{
|
2011-04-24 10:57:21 +02:00
|
|
|
int ret;
|
|
|
|
size_t len;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2012-02-11 19:42:45 +01:00
|
|
|
if( ( end - *p ) < 1 )
|
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE +
|
|
|
|
POLARSSL_ERR_ASN1_OUT_OF_DATA );
|
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
sig->tag = **p;
|
|
|
|
|
2013-07-11 13:59:02 +02:00
|
|
|
if( ( ret = asn1_get_bitstring_null( p, end, &len ) ) != 0 )
|
2011-05-09 18:17:09 +02:00
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE + ret );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
sig->len = len;
|
|
|
|
sig->p = *p;
|
|
|
|
|
|
|
|
*p += len;
|
|
|
|
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2009-05-02 17:13:40 +02:00
|
|
|
* X.509 Extensions (No parsing of extensions, pointer should
|
|
|
|
* be either manually updated or extensions should be parsed!
|
2009-01-03 22:22:43 +01:00
|
|
|
*/
|
|
|
|
static int x509_get_ext( unsigned char **p,
|
2010-03-16 22:09:09 +01:00
|
|
|
const unsigned char *end,
|
2011-10-12 11:56:41 +02:00
|
|
|
x509_buf *ext, int tag )
|
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
|
|
|
|
|
|
|
if( *p == end )
|
|
|
|
return( 0 );
|
|
|
|
|
|
|
|
ext->tag = **p;
|
2010-03-16 22:09:09 +01:00
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
if( ( ret = asn1_get_tag( p, end, &ext->len,
|
2011-10-12 11:56:41 +02:00
|
|
|
ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
|
2009-01-03 22:22:43 +01:00
|
|
|
return( ret );
|
|
|
|
|
|
|
|
ext->p = *p;
|
|
|
|
end = *p + ext->len;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
|
|
|
|
*
|
|
|
|
* Extension ::= SEQUENCE {
|
|
|
|
* extnID OBJECT IDENTIFIER,
|
|
|
|
* critical BOOLEAN DEFAULT FALSE,
|
|
|
|
* extnValue OCTET STRING }
|
|
|
|
*/
|
|
|
|
if( ( ret = asn1_get_tag( p, end, &len,
|
|
|
|
ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
|
2011-05-09 18:17:09 +02:00
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
if( end != *p + len )
|
2011-05-09 18:17:09 +02:00
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
|
2009-01-03 22:51:57 +01:00
|
|
|
POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2009-05-02 17:13:40 +02:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* X.509 CRL v2 extensions (no extensions parsed yet.)
|
|
|
|
*/
|
|
|
|
static int x509_get_crl_ext( unsigned char **p,
|
2010-03-16 22:09:09 +01:00
|
|
|
const unsigned char *end,
|
|
|
|
x509_buf *ext )
|
2009-05-02 17:13:40 +02:00
|
|
|
{
|
2011-04-24 10:57:21 +02:00
|
|
|
int ret;
|
2011-10-12 11:56:41 +02:00
|
|
|
size_t len = 0;
|
2009-05-02 17:13:40 +02:00
|
|
|
|
2011-10-12 11:56:41 +02:00
|
|
|
/* Get explicit tag */
|
|
|
|
if( ( ret = x509_get_ext( p, end, ext, 0) ) != 0 )
|
2009-05-02 17:13:40 +02:00
|
|
|
{
|
|
|
|
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 )
|
2011-05-09 18:17:09 +02:00
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
|
2009-05-02 17:13:40 +02:00
|
|
|
|
|
|
|
*p += len;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( *p != end )
|
2011-05-09 18:17:09 +02:00
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
|
2009-05-02 17:13:40 +02:00
|
|
|
POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2011-10-12 11:58:41 +02:00
|
|
|
/*
|
|
|
|
* 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 */
|
|
|
|
if (end <= *p)
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
end = *p + ext->len;
|
|
|
|
|
|
|
|
if( end != *p + ext->len )
|
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
|
|
|
|
POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
|
|
|
|
|
|
|
|
while( *p < end )
|
|
|
|
{
|
|
|
|
if( ( ret = asn1_get_tag( p, end, &len,
|
|
|
|
ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
|
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
|
|
|
|
|
|
|
|
*p += len;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( *p != end )
|
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
|
|
|
|
POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2011-01-15 17:57:55 +01:00
|
|
|
static int x509_get_basic_constraints( unsigned char **p,
|
|
|
|
const unsigned char *end,
|
|
|
|
int *ca_istrue,
|
|
|
|
int *max_pathlen )
|
|
|
|
{
|
2011-04-24 10:57:21 +02:00
|
|
|
int ret;
|
|
|
|
size_t len;
|
2011-01-15 17:57:55 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* BasicConstraints ::= SEQUENCE {
|
|
|
|
* cA BOOLEAN DEFAULT FALSE,
|
|
|
|
* pathLenConstraint INTEGER (0..MAX) OPTIONAL }
|
|
|
|
*/
|
2011-01-16 22:46:31 +01:00
|
|
|
*ca_istrue = 0; /* DEFAULT FALSE */
|
2011-01-15 17:57:55 +01:00
|
|
|
*max_pathlen = 0; /* endless */
|
|
|
|
|
|
|
|
if( ( ret = asn1_get_tag( p, end, &len,
|
|
|
|
ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
|
2011-05-09 18:17:09 +02:00
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
|
2011-01-15 17:57:55 +01:00
|
|
|
|
|
|
|
if( *p == end )
|
|
|
|
return 0;
|
|
|
|
|
2011-01-16 22:46:31 +01:00
|
|
|
if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
|
2011-01-15 17:57:55 +01:00
|
|
|
{
|
|
|
|
if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
|
2011-01-16 22:46:31 +01:00
|
|
|
ret = asn1_get_int( p, end, ca_istrue );
|
2011-01-15 17:57:55 +01:00
|
|
|
|
|
|
|
if( ret != 0 )
|
2011-05-09 18:17:09 +02:00
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
|
2011-01-15 17:57:55 +01:00
|
|
|
|
2011-01-16 22:46:31 +01:00
|
|
|
if( *ca_istrue != 0 )
|
|
|
|
*ca_istrue = 1;
|
2011-01-15 17:57:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if( *p == end )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
|
2011-05-09 18:17:09 +02:00
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
|
2011-01-15 17:57:55 +01:00
|
|
|
|
|
|
|
if( *p != end )
|
2011-05-09 18:17:09 +02:00
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
|
2011-01-15 17:57:55 +01:00
|
|
|
POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
|
|
|
|
|
|
|
|
(*max_pathlen)++;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int x509_get_ns_cert_type( unsigned char **p,
|
|
|
|
const unsigned char *end,
|
|
|
|
unsigned char *ns_cert_type)
|
|
|
|
{
|
|
|
|
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 )
|
2011-05-09 18:17:09 +02:00
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
|
2011-01-15 17:57:55 +01:00
|
|
|
|
|
|
|
if( bs.len != 1 )
|
2011-05-09 18:17:09 +02:00
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
|
2011-01-15 17:57:55 +01:00
|
|
|
POLARSSL_ERR_ASN1_INVALID_LENGTH );
|
|
|
|
|
|
|
|
/* Get actual bitstring */
|
|
|
|
*ns_cert_type = *bs.p;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 )
|
2011-05-09 18:17:09 +02:00
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
|
2011-01-15 17:57:55 +01:00
|
|
|
|
2012-08-23 15:03:52 +02:00
|
|
|
if( bs.len < 1 )
|
2011-05-09 18:17:09 +02:00
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
|
2011-01-15 17:57:55 +01:00
|
|
|
POLARSSL_ERR_ASN1_INVALID_LENGTH );
|
|
|
|
|
|
|
|
/* Get actual bitstring */
|
|
|
|
*key_usage = *bs.p;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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 )
|
2011-05-09 18:17:09 +02:00
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
|
2011-01-15 17:57:55 +01:00
|
|
|
|
|
|
|
/* Sequence length must be >= 1 */
|
|
|
|
if( ext_key_usage->buf.p == NULL )
|
2011-05-09 18:17:09 +02:00
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
|
2011-01-15 17:57:55 +01:00
|
|
|
POLARSSL_ERR_ASN1_INVALID_LENGTH );
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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 )
|
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
|
|
|
|
|
|
|
|
if( *p + len != end )
|
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
|
|
|
|
POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
|
|
|
|
|
|
|
|
while( *p < end )
|
|
|
|
{
|
|
|
|
if( ( end - *p ) < 1 )
|
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
|
|
|
|
POLARSSL_ERR_ASN1_OUT_OF_DATA );
|
|
|
|
|
|
|
|
tag = **p;
|
|
|
|
(*p)++;
|
|
|
|
if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
|
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
|
|
|
|
|
|
|
|
if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
|
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
|
|
|
|
POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
|
|
|
|
|
|
|
|
if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
|
|
|
|
{
|
|
|
|
*p += tag_len;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = &(cur->buf);
|
|
|
|
buf->tag = tag;
|
|
|
|
buf->p = *p;
|
|
|
|
buf->len = tag_len;
|
|
|
|
*p += buf->len;
|
|
|
|
|
|
|
|
/* Allocate and assign next pointer */
|
|
|
|
if (*p < end)
|
|
|
|
{
|
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 )
|
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set final sequence entry's next pointer to NULL */
|
|
|
|
cur->next = NULL;
|
|
|
|
|
|
|
|
if( *p != end )
|
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
|
|
|
|
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,
|
2011-01-15 17:57:55 +01:00
|
|
|
x509_cert *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 )
|
2011-05-09 18:17:09 +02:00
|
|
|
return( POLARSSL_ERR_X509_CERT_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 )
|
2011-05-09 18:17:09 +02:00
|
|
|
return( POLARSSL_ERR_X509_CERT_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 )
|
2011-05-09 18:17:09 +02:00
|
|
|
return( POLARSSL_ERR_X509_CERT_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 ) )
|
2011-05-09 18:17:09 +02:00
|
|
|
return( POLARSSL_ERR_X509_CERT_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 )
|
2011-05-09 18:17:09 +02:00
|
|
|
return( POLARSSL_ERR_X509_CERT_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 )
|
2011-05-09 18:17:09 +02:00
|
|
|
return( POLARSSL_ERR_X509_CERT_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 */
|
|
|
|
return ( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
|
|
|
|
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 )
|
2011-01-15 17:57:55 +01: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 )
|
|
|
|
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 )
|
|
|
|
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 )
|
|
|
|
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 )
|
|
|
|
return ( ret );
|
|
|
|
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 )
|
2011-05-09 18:17:09 +02:00
|
|
|
return( POLARSSL_ERR_X509_CERT_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 );
|
|
|
|
}
|
|
|
|
|
2009-05-02 17:13:40 +02:00
|
|
|
/*
|
|
|
|
* X.509 CRL Entries
|
|
|
|
*/
|
|
|
|
static int x509_get_entries( unsigned char **p,
|
2010-03-16 22:09:09 +01:00
|
|
|
const unsigned char *end,
|
2009-05-02 17:13:40 +02:00
|
|
|
x509_crl_entry *entry )
|
|
|
|
{
|
2011-04-24 10:57:21 +02:00
|
|
|
int ret;
|
|
|
|
size_t entry_len;
|
2009-05-02 17:13:40 +02:00
|
|
|
x509_crl_entry *cur_entry = entry;
|
|
|
|
|
|
|
|
if( *p == end )
|
|
|
|
return( 0 );
|
|
|
|
|
2009-07-27 22:21:53 +02:00
|
|
|
if( ( ret = asn1_get_tag( p, end, &entry_len,
|
2009-05-02 17:13:40 +02:00
|
|
|
ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
|
|
|
|
{
|
|
|
|
if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
|
|
|
|
return( 0 );
|
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2009-07-27 22:21:53 +02:00
|
|
|
end = *p + entry_len;
|
2009-05-02 17:13:40 +02:00
|
|
|
|
|
|
|
while( *p < end )
|
|
|
|
{
|
2011-04-24 10:57:21 +02:00
|
|
|
size_t len2;
|
2011-10-12 11:58:41 +02:00
|
|
|
const unsigned char *end2;
|
2009-05-02 17:13:40 +02:00
|
|
|
|
|
|
|
if( ( ret = asn1_get_tag( p, end, &len2,
|
|
|
|
ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
|
|
|
|
{
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2009-07-27 22:21:53 +02:00
|
|
|
cur_entry->raw.tag = **p;
|
|
|
|
cur_entry->raw.p = *p;
|
|
|
|
cur_entry->raw.len = len2;
|
2011-10-12 11:58:41 +02:00
|
|
|
end2 = *p + len2;
|
2009-07-27 22:21:53 +02:00
|
|
|
|
2011-10-12 11:58:41 +02:00
|
|
|
if( ( ret = x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
|
2009-05-02 17:13:40 +02:00
|
|
|
return( ret );
|
|
|
|
|
2011-10-12 11:58:41 +02:00
|
|
|
if( ( ret = x509_get_time( p, end2, &cur_entry->revocation_date ) ) != 0 )
|
2009-05-02 17:13:40 +02:00
|
|
|
return( ret );
|
|
|
|
|
2011-10-12 11:58:41 +02:00
|
|
|
if( ( ret = x509_get_crl_entry_ext( p, end2, &cur_entry->entry_ext ) ) != 0 )
|
2009-05-02 17:13:40 +02:00
|
|
|
return( ret );
|
|
|
|
|
2011-01-15 17:57:55 +01:00
|
|
|
if ( *p < end )
|
|
|
|
{
|
2013-07-03 13:37:05 +02:00
|
|
|
cur_entry->next = polarssl_malloc( sizeof( x509_crl_entry ) );
|
2012-01-13 14:44:06 +01:00
|
|
|
|
|
|
|
if( cur_entry->next == NULL )
|
|
|
|
return( POLARSSL_ERR_X509_MALLOC_FAILED );
|
|
|
|
|
2009-05-02 17:13:40 +02:00
|
|
|
cur_entry = cur_entry->next;
|
|
|
|
memset( cur_entry, 0, sizeof( x509_crl_entry ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2013-04-07 22:00:46 +02:00
|
|
|
static int x509_get_sig_alg( const x509_buf *sig_oid, md_type_t *md_alg,
|
|
|
|
pk_type_t *pk_alg )
|
2010-03-17 07:56:01 +01:00
|
|
|
{
|
2013-04-07 22:00:46 +02:00
|
|
|
int ret = oid_get_sig_alg( sig_oid, md_alg, pk_alg );
|
2010-03-17 07:56:01 +01:00
|
|
|
|
2013-04-07 22:00:46 +02:00
|
|
|
if( ret != 0 )
|
|
|
|
return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG + ret );
|
2010-03-17 07:56:01 +01:00
|
|
|
|
2013-04-07 22:00:46 +02:00
|
|
|
return( 0 );
|
2010-03-17 07:56:01 +01:00
|
|
|
}
|
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
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-06-25 16:25:17 +02:00
|
|
|
static int x509parse_crt_der_core( x509_cert *crt, const unsigned char *buf,
|
|
|
|
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;
|
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 )
|
2011-12-10 22:55:01 +01:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_INPUT );
|
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 );
|
|
|
|
|
|
|
|
buflen = 0;
|
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 )
|
|
|
|
{
|
2009-05-02 17:53:56 +02:00
|
|
|
x509_free( crt );
|
2009-01-03 22:51:57 +01:00
|
|
|
return( POLARSSL_ERR_X509_CERT_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
|
|
|
{
|
2009-05-02 17:53:56 +02:00
|
|
|
x509_free( crt );
|
2011-05-09 18:17:09 +02:00
|
|
|
return( POLARSSL_ERR_X509_CERT_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 )
|
|
|
|
{
|
2009-05-02 17:53:56 +02:00
|
|
|
x509_free( crt );
|
2011-05-09 18:17:09 +02:00
|
|
|
return( POLARSSL_ERR_X509_CERT_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 ||
|
|
|
|
( ret = x509_get_alg_null( &p, end, &crt->sig_oid1 ) ) != 0 )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2009-05-02 17:53:56 +02:00
|
|
|
x509_free( crt );
|
2009-01-03 22:22:43 +01:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
crt->version++;
|
|
|
|
|
|
|
|
if( crt->version > 3 )
|
|
|
|
{
|
2009-05-02 17:53:56 +02:00
|
|
|
x509_free( crt );
|
2009-01-03 22:51:57 +01:00
|
|
|
return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2013-04-07 22:00:46 +02:00
|
|
|
if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_md,
|
|
|
|
&crt->sig_pk ) ) != 0 )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2009-05-02 17:53:56 +02:00
|
|
|
x509_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 )
|
|
|
|
{
|
2009-05-02 17:53:56 +02:00
|
|
|
x509_free( crt );
|
2011-05-09 18:17:09 +02:00
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
|
|
|
|
{
|
2009-05-02 17:53:56 +02:00
|
|
|
x509_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 )
|
|
|
|
{
|
2009-05-02 17:53:56 +02:00
|
|
|
x509_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 )
|
|
|
|
{
|
2009-05-02 17:53:56 +02:00
|
|
|
x509_free( crt );
|
2011-05-09 18:17:09 +02:00
|
|
|
return( POLARSSL_ERR_X509_CERT_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
|
|
|
{
|
2009-05-02 17:53:56 +02:00
|
|
|
x509_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-07-10 14:32:58 +02:00
|
|
|
if( ( ret = x509_get_pubkey( &p, end, &crt->pk ) ) != 0 )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2009-05-02 17:53:56 +02:00
|
|
|
x509_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 )
|
|
|
|
{
|
2009-05-02 17:53:56 +02:00
|
|
|
x509_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 )
|
|
|
|
{
|
2009-05-02 17:53:56 +02:00
|
|
|
x509_free( crt );
|
2009-01-03 22:22:43 +01:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( crt->version == 3 )
|
|
|
|
{
|
2011-01-15 17:57:55 +01:00
|
|
|
ret = x509_get_crt_ext( &p, end, crt);
|
2009-01-03 22:22:43 +01:00
|
|
|
if( ret != 0 )
|
|
|
|
{
|
2009-05-02 17:53:56 +02:00
|
|
|
x509_free( crt );
|
2009-01-03 22:22:43 +01:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( p != end )
|
|
|
|
{
|
2009-05-02 17:53:56 +02:00
|
|
|
x509_free( crt );
|
2011-05-09 18:17:09 +02:00
|
|
|
return( POLARSSL_ERR_X509_CERT_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
|
|
|
end = crt_end;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
/*
|
2013-07-09 12:13:24 +02:00
|
|
|
* }
|
|
|
|
* -- end of TBSCertificate
|
|
|
|
*
|
2009-01-03 22:22:43 +01:00
|
|
|
* signatureAlgorithm AlgorithmIdentifier,
|
|
|
|
* signatureValue BIT STRING
|
|
|
|
*/
|
2013-07-10 13:18:41 +02:00
|
|
|
if( ( ret = x509_get_alg_null( &p, end, &crt->sig_oid2 ) ) != 0 )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2009-05-02 17:53:56 +02:00
|
|
|
x509_free( crt );
|
2009-01-03 22:22:43 +01:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2012-08-23 12:49:55 +02:00
|
|
|
if( crt->sig_oid1.len != crt->sig_oid2.len ||
|
|
|
|
memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2009-05-02 17:53:56 +02:00
|
|
|
x509_free( crt );
|
2009-01-03 22:51:57 +01:00
|
|
|
return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
|
|
|
|
{
|
2009-05-02 17:53:56 +02:00
|
|
|
x509_free( crt );
|
2009-01-03 22:22:43 +01:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( p != end )
|
|
|
|
{
|
2009-05-02 17:53:56 +02:00
|
|
|
x509_free( crt );
|
2011-05-09 18:17:09 +02:00
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
|
2009-01-03 22:51:57 +01:00
|
|
|
POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2011-12-04 13:24:18 +01:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-06-24 19:21:59 +02:00
|
|
|
* Parse one X.509 certificate in DER format from a buffer and add them to a
|
|
|
|
* chained list
|
2011-12-04 13:24:18 +01:00
|
|
|
*/
|
2013-06-24 19:21:59 +02:00
|
|
|
int x509parse_crt_der( x509_cert *chain, const unsigned char *buf, size_t buflen )
|
2011-12-04 13:24:18 +01:00
|
|
|
{
|
2013-06-24 19:21:59 +02:00
|
|
|
int ret;
|
|
|
|
x509_cert *crt = chain, *prev = NULL;
|
2011-12-04 13:24:18 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for valid input
|
|
|
|
*/
|
|
|
|
if( crt == NULL || buf == NULL )
|
2011-12-10 22:55:01 +01:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_INPUT );
|
2011-12-04 13:24:18 +01:00
|
|
|
|
|
|
|
while( crt->version != 0 && crt->next != NULL )
|
|
|
|
{
|
|
|
|
prev = crt;
|
|
|
|
crt = crt->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add new certificate on the end of the chain if needed.
|
|
|
|
*/
|
|
|
|
if ( crt->version != 0 && crt->next == NULL)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2013-07-03 13:37:05 +02:00
|
|
|
crt->next = (x509_cert *) polarssl_malloc( sizeof( x509_cert ) );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2009-05-02 17:53:56 +02:00
|
|
|
if( crt->next == NULL )
|
2011-12-10 22:55:01 +01:00
|
|
|
return( POLARSSL_ERR_X509_MALLOC_FAILED );
|
2009-03-28 19:52:39 +01:00
|
|
|
|
2011-12-04 13:24:18 +01:00
|
|
|
prev = crt;
|
2009-05-02 17:53:56 +02:00
|
|
|
crt = crt->next;
|
|
|
|
memset( crt, 0, sizeof( x509_cert ) );
|
2011-12-04 13:24:18 +01:00
|
|
|
}
|
|
|
|
|
2013-06-24 19:21:59 +02:00
|
|
|
if( ( ret = x509parse_crt_der_core( crt, buf, buflen ) ) != 0 )
|
|
|
|
{
|
|
|
|
if( prev )
|
|
|
|
prev->next = NULL;
|
|
|
|
|
|
|
|
if( crt != chain )
|
2013-07-03 13:37:05 +02:00
|
|
|
polarssl_free( crt );
|
2013-06-24 19:21:59 +02:00
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse one or more PEM certificates from a buffer and add them to the chained list
|
|
|
|
*/
|
|
|
|
int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
|
|
|
|
{
|
|
|
|
int ret, success = 0, first_error = 0, total_failed = 0;
|
|
|
|
int buf_format = X509_FORMAT_DER;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for valid input
|
|
|
|
*/
|
|
|
|
if( chain == NULL || buf == NULL )
|
|
|
|
return( POLARSSL_ERR_X509_INVALID_INPUT );
|
|
|
|
|
2011-12-04 13:24:18 +01:00
|
|
|
/*
|
|
|
|
* Determine buffer content. Buffer contains either one DER certificate or
|
|
|
|
* one or more PEM certificates.
|
|
|
|
*/
|
|
|
|
#if defined(POLARSSL_PEM_C)
|
2013-06-24 19:03:14 +02:00
|
|
|
if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
|
2011-12-04 13:24:18 +01:00
|
|
|
buf_format = X509_FORMAT_PEM;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if( buf_format == X509_FORMAT_DER )
|
2013-06-24 19:21:59 +02:00
|
|
|
return x509parse_crt_der( chain, buf, buflen );
|
|
|
|
|
2011-12-04 13:24:18 +01:00
|
|
|
#if defined(POLARSSL_PEM_C)
|
|
|
|
if( buf_format == X509_FORMAT_PEM )
|
|
|
|
{
|
|
|
|
pem_context pem;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2013-06-24 19:05:46 +02:00
|
|
|
else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
|
|
|
|
{
|
|
|
|
return( ret );
|
|
|
|
}
|
2013-06-24 13:02:41 +02:00
|
|
|
else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
|
2011-12-04 13:24:18 +01:00
|
|
|
{
|
|
|
|
pem_free( &pem );
|
|
|
|
|
2013-06-24 19:05:46 +02:00
|
|
|
/*
|
|
|
|
* PEM header and footer were found
|
|
|
|
*/
|
|
|
|
buflen -= use_len;
|
|
|
|
buf += use_len;
|
|
|
|
|
2011-12-04 13:24:18 +01:00
|
|
|
if( first_error == 0 )
|
|
|
|
first_error = ret;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
|
2013-06-24 19:21:59 +02:00
|
|
|
ret = x509parse_crt_der( chain, pem.buf, pem.buflen );
|
2011-12-04 13:24:18 +01:00
|
|
|
|
|
|
|
pem_free( &pem );
|
|
|
|
|
|
|
|
if( ret != 0 )
|
|
|
|
{
|
|
|
|
/*
|
2013-06-24 19:21:59 +02:00
|
|
|
* Quit parsing on a memory error
|
2011-12-04 13:24:18 +01:00
|
|
|
*/
|
2011-12-10 22:55:01 +01:00
|
|
|
if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
|
2011-12-04 13:24:18 +01:00
|
|
|
return( ret );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2011-12-04 13:24:18 +01:00
|
|
|
if( first_error == 0 )
|
|
|
|
first_error = ret;
|
|
|
|
|
2013-06-24 19:21:59 +02:00
|
|
|
total_failed++;
|
2011-12-04 13:24:18 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
success = 1;
|
|
|
|
}
|
2009-03-28 19:52:39 +01:00
|
|
|
}
|
2011-12-04 13:24:18 +01:00
|
|
|
#endif
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2011-12-04 13:24:18 +01:00
|
|
|
if( success )
|
2011-12-10 22:55:01 +01:00
|
|
|
return( total_failed );
|
2011-12-04 13:24:18 +01:00
|
|
|
else if( first_error )
|
|
|
|
return( first_error );
|
|
|
|
else
|
|
|
|
return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2009-05-02 17:13:40 +02:00
|
|
|
/*
|
|
|
|
* Parse one or more CRLs and add them to the chained list
|
|
|
|
*/
|
2011-04-24 10:57:21 +02:00
|
|
|
int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
|
2009-05-02 17:13:40 +02:00
|
|
|
{
|
2011-04-24 10:57:21 +02:00
|
|
|
int ret;
|
2011-05-26 15:16:06 +02:00
|
|
|
size_t len;
|
2009-05-02 17:13:40 +02:00
|
|
|
unsigned char *p, *end;
|
|
|
|
x509_crl *crl;
|
2011-02-12 15:30:57 +01:00
|
|
|
#if defined(POLARSSL_PEM_C)
|
2011-05-26 15:16:06 +02:00
|
|
|
size_t use_len;
|
2011-02-12 15:30:57 +01:00
|
|
|
pem_context pem;
|
|
|
|
#endif
|
2009-05-02 17:13:40 +02:00
|
|
|
|
|
|
|
crl = chain;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for valid input
|
|
|
|
*/
|
|
|
|
if( crl == NULL || buf == NULL )
|
2011-12-10 22:55:01 +01:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_INPUT );
|
2009-05-02 17:13:40 +02:00
|
|
|
|
|
|
|
while( crl->version != 0 && crl->next != NULL )
|
|
|
|
crl = crl->next;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add new CRL on the end of the chain if needed.
|
|
|
|
*/
|
|
|
|
if ( crl->version != 0 && crl->next == NULL)
|
|
|
|
{
|
2013-07-03 13:37:05 +02:00
|
|
|
crl->next = (x509_crl *) polarssl_malloc( sizeof( x509_crl ) );
|
2009-05-02 17:13:40 +02:00
|
|
|
|
2009-05-02 17:53:56 +02:00
|
|
|
if( crl->next == NULL )
|
|
|
|
{
|
2009-05-02 17:13:40 +02:00
|
|
|
x509_crl_free( crl );
|
2011-12-10 22:55:01 +01:00
|
|
|
return( POLARSSL_ERR_X509_MALLOC_FAILED );
|
2009-05-02 17:53:56 +02:00
|
|
|
}
|
2009-05-02 17:13:40 +02:00
|
|
|
|
2009-05-02 17:53:56 +02:00
|
|
|
crl = crl->next;
|
|
|
|
memset( crl, 0, sizeof( x509_crl ) );
|
2009-05-02 17:13:40 +02:00
|
|
|
}
|
|
|
|
|
2011-02-12 15:30:57 +01:00
|
|
|
#if defined(POLARSSL_PEM_C)
|
|
|
|
pem_init( &pem );
|
|
|
|
ret = pem_read_buffer( &pem,
|
|
|
|
"-----BEGIN X509 CRL-----",
|
|
|
|
"-----END X509 CRL-----",
|
|
|
|
buf, NULL, 0, &use_len );
|
2009-05-02 17:13:40 +02:00
|
|
|
|
2011-02-12 15:30:57 +01:00
|
|
|
if( ret == 0 )
|
2009-05-02 17:13:40 +02:00
|
|
|
{
|
|
|
|
/*
|
2011-02-12 15:30:57 +01:00
|
|
|
* Was PEM encoded
|
2009-05-02 17:13:40 +02:00
|
|
|
*/
|
2011-02-12 15:30:57 +01:00
|
|
|
buflen -= use_len;
|
|
|
|
buf += use_len;
|
2009-05-02 17:13:40 +02:00
|
|
|
|
|
|
|
/*
|
2011-02-12 15:30:57 +01:00
|
|
|
* Steal PEM buffer
|
2009-05-02 17:13:40 +02:00
|
|
|
*/
|
2011-02-12 15:30:57 +01:00
|
|
|
p = pem.buf;
|
|
|
|
pem.buf = NULL;
|
|
|
|
len = pem.buflen;
|
|
|
|
pem_free( &pem );
|
|
|
|
}
|
2013-06-24 13:02:41 +02:00
|
|
|
else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
|
2011-02-12 15:30:57 +01:00
|
|
|
{
|
|
|
|
pem_free( &pem );
|
|
|
|
return( ret );
|
2009-05-02 17:13:40 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* nope, copy the raw DER data
|
|
|
|
*/
|
2013-07-03 13:37:05 +02:00
|
|
|
p = (unsigned char *) polarssl_malloc( len = buflen );
|
2009-05-02 17:13:40 +02:00
|
|
|
|
|
|
|
if( p == NULL )
|
2011-12-10 22:55:01 +01:00
|
|
|
return( POLARSSL_ERR_X509_MALLOC_FAILED );
|
2009-05-02 17:13:40 +02:00
|
|
|
|
|
|
|
memcpy( p, buf, buflen );
|
|
|
|
|
|
|
|
buflen = 0;
|
|
|
|
}
|
2011-02-12 15:30:57 +01:00
|
|
|
#else
|
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 );
|
|
|
|
|
|
|
|
buflen = 0;
|
|
|
|
#endif
|
2009-05-02 17:13:40 +02:00
|
|
|
|
|
|
|
crl->raw.p = p;
|
|
|
|
crl->raw.len = len;
|
|
|
|
end = p + len;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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 );
|
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
|
|
|
|
}
|
|
|
|
|
2011-04-24 10:57:21 +02:00
|
|
|
if( len != (size_t) ( end - p ) )
|
2009-05-02 17:13:40 +02:00
|
|
|
{
|
|
|
|
x509_crl_free( crl );
|
2011-05-09 18:17:09 +02:00
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
|
2009-05-02 17:13:40 +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 );
|
2011-05-09 18:17:09 +02:00
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
|
2009-05-02 17:13:40 +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
|
|
|
|
*/
|
2011-10-12 11:55:01 +02:00
|
|
|
if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
|
2013-07-10 13:18:41 +02:00
|
|
|
( ret = x509_get_alg_null( &p, end, &crl->sig_oid1 ) ) != 0 )
|
2009-05-02 17:13:40 +02:00
|
|
|
{
|
|
|
|
x509_crl_free( crl );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
crl->version++;
|
|
|
|
|
|
|
|
if( crl->version > 2 )
|
|
|
|
{
|
|
|
|
x509_crl_free( crl );
|
|
|
|
return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
|
|
|
|
}
|
|
|
|
|
2013-04-07 22:00:46 +02:00
|
|
|
if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_md,
|
|
|
|
&crl->sig_pk ) ) != 0 )
|
2009-05-02 17:13:40 +02:00
|
|
|
{
|
|
|
|
x509_crl_free( crl );
|
|
|
|
return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* issuer Name
|
|
|
|
*/
|
|
|
|
crl->issuer_raw.p = p;
|
|
|
|
|
|
|
|
if( ( ret = asn1_get_tag( &p, end, &len,
|
|
|
|
ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
|
|
|
|
{
|
|
|
|
x509_crl_free( crl );
|
2011-05-09 18:17:09 +02:00
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
|
2009-05-02 17:13:40 +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
|
|
|
|
*/
|
2010-02-18 22:26:15 +01:00
|
|
|
if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
|
2009-05-02 17:13:40 +02:00
|
|
|
{
|
|
|
|
x509_crl_free( crl );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2010-02-18 22:26:15 +01:00
|
|
|
if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
|
2009-05-02 17:13:40 +02:00
|
|
|
{
|
2011-05-09 18:17:09 +02:00
|
|
|
if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
|
2009-07-27 22:21:53 +02:00
|
|
|
POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
|
2011-05-09 18:17:09 +02:00
|
|
|
ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
|
2009-07-27 22:21:53 +02:00
|
|
|
POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
|
2009-07-20 22:34:41 +02:00
|
|
|
{
|
2009-05-02 17:13:40 +02:00
|
|
|
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 );
|
2011-05-09 18:17:09 +02:00
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
|
2009-05-02 17:13:40 +02:00
|
|
|
POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
|
|
|
|
}
|
|
|
|
|
|
|
|
end = crl->raw.p + crl->raw.len;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* signatureAlgorithm AlgorithmIdentifier,
|
|
|
|
* signatureValue BIT STRING
|
|
|
|
*/
|
2013-07-10 13:18:41 +02:00
|
|
|
if( ( ret = x509_get_alg_null( &p, end, &crl->sig_oid2 ) ) != 0 )
|
2009-05-02 17:13:40 +02:00
|
|
|
{
|
|
|
|
x509_crl_free( crl );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2012-08-23 12:49:55 +02:00
|
|
|
if( crl->sig_oid1.len != crl->sig_oid2.len ||
|
|
|
|
memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
|
2009-05-02 17:13:40 +02:00
|
|
|
{
|
|
|
|
x509_crl_free( crl );
|
|
|
|
return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
|
|
|
|
{
|
|
|
|
x509_crl_free( crl );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( p != end )
|
|
|
|
{
|
|
|
|
x509_crl_free( crl );
|
2011-05-09 18:17:09 +02:00
|
|
|
return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
|
2009-05-02 17:13:40 +02:00
|
|
|
POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( buflen > 0 )
|
|
|
|
{
|
2013-07-03 13:37:05 +02:00
|
|
|
crl->next = (x509_crl *) polarssl_malloc( sizeof( x509_crl ) );
|
2009-05-02 17:13:40 +02:00
|
|
|
|
2009-05-02 17:53:56 +02:00
|
|
|
if( crl->next == NULL )
|
|
|
|
{
|
2009-05-02 17:13:40 +02:00
|
|
|
x509_crl_free( crl );
|
2011-12-10 22:55:01 +01:00
|
|
|
return( POLARSSL_ERR_X509_MALLOC_FAILED );
|
2009-05-02 17:53:56 +02:00
|
|
|
}
|
2009-05-02 17:13:40 +02:00
|
|
|
|
2009-05-02 17:53:56 +02:00
|
|
|
crl = crl->next;
|
|
|
|
memset( crl, 0, sizeof( x509_crl ) );
|
2009-05-02 17:13:40 +02:00
|
|
|
|
|
|
|
return( x509parse_crl( crl, buf, buflen ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2011-04-25 17:28:35 +02:00
|
|
|
#if defined(POLARSSL_FS_IO)
|
2009-04-19 20:44:26 +02:00
|
|
|
/*
|
|
|
|
* Load all data from a file into a given buffer.
|
|
|
|
*/
|
2013-06-25 16:25:17 +02:00
|
|
|
static int load_file( const char *path, unsigned char **buf, size_t *n )
|
2009-04-19 20:44:26 +02:00
|
|
|
{
|
2009-05-02 17:13:40 +02:00
|
|
|
FILE *f;
|
2009-04-19 20:44:26 +02:00
|
|
|
|
2009-05-02 17:13:40 +02:00
|
|
|
if( ( f = fopen( path, "rb" ) ) == NULL )
|
2011-12-10 22:55:01 +01:00
|
|
|
return( POLARSSL_ERR_X509_FILE_IO_ERROR );
|
2009-04-19 20:44:26 +02:00
|
|
|
|
2009-05-02 17:13:40 +02:00
|
|
|
fseek( f, 0, SEEK_END );
|
|
|
|
*n = (size_t) ftell( f );
|
|
|
|
fseek( f, 0, SEEK_SET );
|
2009-04-19 20:44:26 +02:00
|
|
|
|
2013-07-03 13:37:05 +02:00
|
|
|
if( ( *buf = (unsigned char *) polarssl_malloc( *n + 1 ) ) == NULL )
|
2013-05-14 13:26:51 +02:00
|
|
|
{
|
|
|
|
fclose( f );
|
2011-12-10 22:55:01 +01:00
|
|
|
return( POLARSSL_ERR_X509_MALLOC_FAILED );
|
2013-05-14 13:26:51 +02:00
|
|
|
}
|
2009-04-19 20:44:26 +02:00
|
|
|
|
2009-05-02 17:13:40 +02:00
|
|
|
if( fread( *buf, 1, *n, f ) != *n )
|
|
|
|
{
|
|
|
|
fclose( f );
|
2013-07-03 13:37:05 +02:00
|
|
|
polarssl_free( *buf );
|
2011-12-10 22:55:01 +01:00
|
|
|
return( POLARSSL_ERR_X509_FILE_IO_ERROR );
|
2009-05-02 17:13:40 +02:00
|
|
|
}
|
2009-04-19 20:44:26 +02:00
|
|
|
|
2009-05-02 17:13:40 +02:00
|
|
|
fclose( f );
|
2009-04-19 20:44:26 +02:00
|
|
|
|
2009-05-02 17:13:40 +02:00
|
|
|
(*buf)[*n] = '\0';
|
2009-04-19 20:44:26 +02:00
|
|
|
|
2009-05-02 17:13:40 +02:00
|
|
|
return( 0 );
|
2009-04-19 20:44:26 +02:00
|
|
|
}
|
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
|
|
|
* Load one or more certificates and add them to the chained list
|
|
|
|
*/
|
2011-12-10 22:55:01 +01:00
|
|
|
int x509parse_crtfile( x509_cert *chain, const char *path )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t n;
|
|
|
|
unsigned char *buf;
|
|
|
|
|
2013-06-27 13:00:00 +02:00
|
|
|
if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
|
2011-12-10 22:55:01 +01:00
|
|
|
return( ret );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2011-12-10 22:55:01 +01:00
|
|
|
ret = x509parse_crt( chain, buf, n );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
memset( buf, 0, n + 1 );
|
2013-07-03 13:37:05 +02:00
|
|
|
polarssl_free( buf );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2012-06-04 14:46:42 +02:00
|
|
|
int x509parse_crtpath( x509_cert *chain, const char *path )
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
#if defined(_WIN32)
|
2012-10-01 23:13:10 +02:00
|
|
|
int w_ret;
|
|
|
|
WCHAR szDir[MAX_PATH];
|
|
|
|
char filename[MAX_PATH];
|
|
|
|
char *p;
|
2012-11-02 12:06:08 +01:00
|
|
|
int len = strlen( path );
|
2012-10-01 23:13:10 +02:00
|
|
|
|
2012-11-02 13:53:26 +01: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 )
|
|
|
|
return( POLARSSL_ERR_X509_INVALID_INPUT );
|
2012-06-04 14:46:42 +02:00
|
|
|
|
2012-10-01 23:13:10 +02:00
|
|
|
memset( szDir, 0, sizeof(szDir) );
|
|
|
|
memset( filename, 0, MAX_PATH );
|
2012-11-02 12:06:08 +01:00
|
|
|
memcpy( filename, path, len );
|
|
|
|
filename[len++] = '\\';
|
|
|
|
p = filename + len;
|
|
|
|
filename[len++] = '*';
|
2012-10-01 23:13:10 +02:00
|
|
|
|
2012-11-02 12:06:08 +01:00
|
|
|
w_ret = MultiByteToWideChar( CP_ACP, 0, path, 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 );
|
2012-06-04 14:46:42 +02:00
|
|
|
if (hFind == INVALID_HANDLE_VALUE)
|
|
|
|
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
|
|
|
|
{
|
2012-11-02 12:06:08 +01: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;
|
|
|
|
|
2012-10-01 23:13:10 +02:00
|
|
|
w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
|
|
|
|
lstrlenW(file_data.cFileName),
|
2012-11-02 12:06:08 +01:00
|
|
|
p, len - 1,
|
|
|
|
NULL, NULL );
|
2012-06-04 14:46:42 +02:00
|
|
|
|
2012-10-01 23:13:10 +02:00
|
|
|
w_ret = x509parse_crtfile( chain, filename );
|
|
|
|
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
|
|
|
|
2012-11-02 12:06:08 +01:00
|
|
|
if (GetLastError() != ERROR_NO_MORE_FILES)
|
|
|
|
ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
|
2012-06-04 14:46:42 +02:00
|
|
|
|
2012-11-02 12:06:08 +01:00
|
|
|
cleanup:
|
2012-06-04 14:46:42 +02:00
|
|
|
FindClose( hFind );
|
|
|
|
#else
|
2013-06-24 19:22:42 +02:00
|
|
|
int t_ret, i;
|
|
|
|
struct stat sb;
|
|
|
|
struct dirent entry, *result = NULL;
|
2012-06-04 14:46:42 +02:00
|
|
|
char entry_name[255];
|
|
|
|
DIR *dir = opendir( path );
|
|
|
|
|
|
|
|
if( dir == NULL)
|
|
|
|
return( POLARSSL_ERR_X509_FILE_IO_ERROR );
|
|
|
|
|
2013-06-24 19:22:42 +02:00
|
|
|
while( ( t_ret = readdir_r( dir, &entry, &result ) ) == 0 )
|
2012-06-04 14:46:42 +02:00
|
|
|
{
|
2013-06-24 19:22:42 +02:00
|
|
|
if( result == NULL )
|
|
|
|
break;
|
|
|
|
|
|
|
|
snprintf( entry_name, sizeof(entry_name), "%s/%s", path, entry.d_name );
|
|
|
|
|
|
|
|
i = stat( entry_name, &sb );
|
|
|
|
|
|
|
|
if( i == -1 )
|
|
|
|
return( POLARSSL_ERR_X509_FILE_IO_ERROR );
|
|
|
|
|
|
|
|
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
|
|
|
|
//
|
2012-06-04 14:46:42 +02:00
|
|
|
t_ret = x509parse_crtfile( chain, entry_name );
|
|
|
|
if( t_ret < 0 )
|
2013-06-24 19:22:42 +02:00
|
|
|
ret++;
|
|
|
|
else
|
|
|
|
ret += t_ret;
|
2012-06-04 14:46:42 +02:00
|
|
|
}
|
|
|
|
closedir( dir );
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2009-05-02 17:13:40 +02:00
|
|
|
/*
|
|
|
|
* Load one or more CRLs and add them to the chained list
|
|
|
|
*/
|
2010-03-16 22:09:09 +01:00
|
|
|
int x509parse_crlfile( x509_crl *chain, const char *path )
|
2009-05-02 17:13:40 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t n;
|
|
|
|
unsigned char *buf;
|
|
|
|
|
2013-06-27 13:00:00 +02:00
|
|
|
if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
|
2011-12-10 22:55:01 +01:00
|
|
|
return( ret );
|
2009-05-02 17:13:40 +02:00
|
|
|
|
2011-06-09 15:55:13 +02:00
|
|
|
ret = x509parse_crl( chain, buf, n );
|
2009-05-02 17:13:40 +02:00
|
|
|
|
|
|
|
memset( buf, 0, n + 1 );
|
2013-07-03 13:37:05 +02:00
|
|
|
polarssl_free( buf );
|
2009-05-02 17:13:40 +02:00
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2013-06-27 11:27:58 +02:00
|
|
|
/*
|
2013-07-04 14:09:57 +02:00
|
|
|
* Load and parse a private key
|
2013-06-27 11:27:58 +02:00
|
|
|
*/
|
2013-07-04 14:09:57 +02:00
|
|
|
int x509parse_keyfile( pk_context *ctx,
|
|
|
|
const char *path, const char *pwd )
|
2013-06-27 11:27:58 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t n;
|
|
|
|
unsigned char *buf;
|
|
|
|
|
|
|
|
if ( (ret = load_file( path, &buf, &n ) ) != 0 )
|
|
|
|
return( ret );
|
|
|
|
|
|
|
|
if( pwd == NULL )
|
2013-07-04 14:09:57 +02:00
|
|
|
ret = x509parse_key( ctx, buf, n, NULL, 0 );
|
2013-06-27 11:27:58 +02:00
|
|
|
else
|
2013-07-04 14:09:57 +02:00
|
|
|
ret = x509parse_key( ctx, buf, n,
|
2013-06-27 11:27:58 +02:00
|
|
|
(const unsigned char *) pwd, strlen( pwd ) );
|
|
|
|
|
|
|
|
memset( buf, 0, n + 1 );
|
2013-07-25 11:25:09 +02:00
|
|
|
polarssl_free( buf );
|
2013-06-27 11:27:58 +02:00
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-07-04 14:09:57 +02:00
|
|
|
* Load and parse a public key
|
2013-06-27 11:27:58 +02:00
|
|
|
*/
|
2013-07-04 14:09:57 +02:00
|
|
|
int x509parse_public_keyfile( pk_context *ctx, const char *path )
|
2013-06-27 11:27:58 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t n;
|
|
|
|
unsigned char *buf;
|
|
|
|
|
|
|
|
if ( (ret = load_file( path, &buf, &n ) ) != 0 )
|
|
|
|
return( ret );
|
|
|
|
|
2013-07-04 14:09:57 +02:00
|
|
|
ret = x509parse_public_key( ctx, buf, n );
|
2013-06-27 11:27:58 +02:00
|
|
|
|
|
|
|
memset( buf, 0, n + 1 );
|
2013-07-25 11:25:09 +02:00
|
|
|
polarssl_free( buf );
|
2013-06-27 11:27:58 +02:00
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
2013-07-04 14:09:57 +02:00
|
|
|
|
2013-07-11 16:17:23 +02:00
|
|
|
#if defined(POLARSSL_RSA_C)
|
|
|
|
/*
|
|
|
|
* Load and parse a private RSA key
|
|
|
|
*/
|
|
|
|
int x509parse_keyfile_rsa( rsa_context *rsa, const char *path, const char *pwd )
|
|
|
|
{
|
|
|
|
pk_context pk;
|
|
|
|
|
|
|
|
pk_init( &pk );
|
|
|
|
pk_wrap_rsa( &pk, rsa );
|
|
|
|
|
|
|
|
return( x509parse_keyfile( &pk, path, pwd ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Load and parse a public RSA key
|
|
|
|
*/
|
|
|
|
int x509parse_public_keyfile_rsa( rsa_context *rsa, const char *path )
|
|
|
|
{
|
|
|
|
pk_context pk;
|
|
|
|
|
|
|
|
pk_init( &pk );
|
|
|
|
pk_wrap_rsa( &pk, rsa );
|
|
|
|
|
|
|
|
return( x509parse_public_keyfile( &pk, path ) );
|
|
|
|
}
|
|
|
|
#endif /* POLARSSL_RSA_C */
|
2011-04-25 17:28:35 +02:00
|
|
|
#endif /* POLARSSL_FS_IO */
|
|
|
|
|
2013-07-11 16:17:23 +02:00
|
|
|
#if defined(POLARSSL_RSA_C)
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
2013-06-24 19:00:59 +02:00
|
|
|
* Parse a PKCS#1 encoded private RSA key
|
2009-01-03 22:22:43 +01:00
|
|
|
*/
|
2013-06-24 19:00:59 +02:00
|
|
|
static int x509parse_key_pkcs1_der( rsa_context *rsa,
|
|
|
|
const unsigned char *key,
|
|
|
|
size_t keylen )
|
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
|
|
|
unsigned char *p, *end;
|
2011-07-13 13:26:43 +02:00
|
|
|
|
2013-06-24 19:00:59 +02:00
|
|
|
p = (unsigned char *) key;
|
|
|
|
end = p + keylen;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2013-06-24 19:00:59 +02:00
|
|
|
/*
|
|
|
|
* This function parses the RSAPrivateKey (PKCS#1)
|
|
|
|
*
|
|
|
|
* RSAPrivateKey ::= SEQUENCE {
|
|
|
|
* version Version,
|
|
|
|
* modulus INTEGER, -- n
|
|
|
|
* publicExponent INTEGER, -- e
|
|
|
|
* privateExponent INTEGER, -- d
|
|
|
|
* prime1 INTEGER, -- p
|
|
|
|
* prime2 INTEGER, -- q
|
|
|
|
* exponent1 INTEGER, -- d mod (p-1)
|
|
|
|
* exponent2 INTEGER, -- d mod (q-1)
|
|
|
|
* coefficient INTEGER, -- (inverse of q) mod p
|
|
|
|
* otherPrimeInfos OtherPrimeInfos OPTIONAL
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
if( ( ret = asn1_get_tag( &p, end, &len,
|
|
|
|
ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
|
|
|
|
{
|
|
|
|
return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
end = p + len;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2013-06-24 19:00:59 +02:00
|
|
|
if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
|
2011-07-13 13:26:43 +02:00
|
|
|
{
|
2013-06-24 19:00:59 +02:00
|
|
|
return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
|
2011-07-13 13:26:43 +02:00
|
|
|
}
|
|
|
|
|
2013-06-24 19:00:59 +02:00
|
|
|
if( rsa->ver != 0 )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2013-07-03 18:56:37 +02:00
|
|
|
return( POLARSSL_ERR_X509_KEY_INVALID_VERSION );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
2013-06-24 19:00:59 +02:00
|
|
|
|
|
|
|
if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
|
|
|
|
( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
|
|
|
|
( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
|
|
|
|
( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
|
|
|
|
( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
|
|
|
|
( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
|
|
|
|
( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
|
|
|
|
( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
|
2010-03-16 22:09:09 +01:00
|
|
|
{
|
2013-06-24 19:00:59 +02:00
|
|
|
rsa_free( rsa );
|
|
|
|
return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
rsa->len = mpi_size( &rsa->N );
|
|
|
|
|
|
|
|
if( p != end )
|
|
|
|
{
|
|
|
|
rsa_free( rsa );
|
|
|
|
return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
|
|
|
|
POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
|
|
|
|
{
|
|
|
|
rsa_free( rsa );
|
2011-02-12 15:30:57 +01:00
|
|
|
return( ret );
|
2010-03-16 22:09:09 +01:00
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2013-06-24 19:00:59 +02:00
|
|
|
return( 0 );
|
|
|
|
}
|
2013-07-11 16:17:23 +02:00
|
|
|
#endif /* POLARSSL_RSA_C */
|
2013-07-11 14:55:43 +02:00
|
|
|
|
2013-07-11 16:17:23 +02:00
|
|
|
#if defined(POLARSSL_ECP_C)
|
2013-06-24 19:00:59 +02:00
|
|
|
/*
|
2013-07-11 14:55:43 +02:00
|
|
|
* Parse a SEC1 encoded private EC key
|
2013-06-24 19:00:59 +02:00
|
|
|
*/
|
2013-07-11 14:55:43 +02:00
|
|
|
static int x509parse_key_sec1_der( ecp_keypair *eck,
|
|
|
|
const unsigned char *key,
|
|
|
|
size_t keylen )
|
2013-06-24 19:00:59 +02:00
|
|
|
{
|
|
|
|
int ret;
|
2013-07-11 14:55:43 +02:00
|
|
|
int version;
|
2013-06-24 19:00:59 +02:00
|
|
|
size_t len;
|
2013-07-11 14:55:43 +02:00
|
|
|
x509_buf params;
|
|
|
|
unsigned char *p = (unsigned char *) key;
|
|
|
|
unsigned char *end = p + keylen;
|
|
|
|
unsigned char *end2;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
/*
|
2013-07-11 14:55:43 +02:00
|
|
|
* RFC 5915, orf SEC1 Appendix C.4
|
2011-07-13 13:26:43 +02:00
|
|
|
*
|
2013-07-11 14:55:43 +02:00
|
|
|
* ECPrivateKey ::= SEQUENCE {
|
|
|
|
* version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
|
|
|
|
* privateKey OCTET STRING,
|
|
|
|
* parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
|
|
|
|
* publicKey [1] BIT STRING OPTIONAL
|
|
|
|
* }
|
2009-01-03 22:22:43 +01:00
|
|
|
*/
|
|
|
|
if( ( ret = asn1_get_tag( &p, end, &len,
|
|
|
|
ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
|
|
|
|
{
|
2011-05-09 18:17:09 +02:00
|
|
|
return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
end = p + len;
|
|
|
|
|
2013-07-11 14:55:43 +02:00
|
|
|
if( ( ret = asn1_get_int( &p, end, &version ) ) != 0 )
|
2011-05-09 18:17:09 +02:00
|
|
|
return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2013-07-11 14:55:43 +02:00
|
|
|
if( version != 1 )
|
|
|
|
return( POLARSSL_ERR_X509_KEY_INVALID_VERSION );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2013-07-11 14:55:43 +02:00
|
|
|
if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
|
|
|
|
return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
|
|
|
|
|
|
|
|
if( ( ret = mpi_read_binary( &eck->d, p, len ) ) != 0 )
|
|
|
|
{
|
|
|
|
ecp_keypair_free( eck );
|
2013-06-24 19:00:59 +02:00
|
|
|
return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
|
2013-07-11 14:55:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
p += len;
|
2013-06-24 19:00:59 +02:00
|
|
|
|
|
|
|
/*
|
2013-07-11 14:55:43 +02:00
|
|
|
* Is 'parameters' present?
|
2013-06-24 19:00:59 +02:00
|
|
|
*/
|
2013-07-11 14:55:43 +02:00
|
|
|
if( ( ret = asn1_get_tag( &p, end, &len,
|
|
|
|
ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) == 0 )
|
|
|
|
{
|
|
|
|
if( ( ret = x509_get_ecparams( &p, p + len, ¶ms) ) != 0 ||
|
|
|
|
( ret = x509_use_ecparams( ¶ms, &eck->grp ) ) != 0 )
|
|
|
|
{
|
|
|
|
ecp_keypair_free( eck );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
|
|
|
|
{
|
|
|
|
ecp_keypair_free( eck );
|
|
|
|
return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
|
|
|
|
}
|
2013-07-04 14:09:57 +02:00
|
|
|
|
2013-06-24 19:00:59 +02:00
|
|
|
/*
|
2013-07-11 14:55:43 +02:00
|
|
|
* Is 'publickey' present?
|
2013-06-24 19:00:59 +02:00
|
|
|
*/
|
2013-07-11 14:55:43 +02:00
|
|
|
if( ( ret = asn1_get_tag( &p, end, &len,
|
|
|
|
ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 1 ) ) == 0 )
|
|
|
|
{
|
|
|
|
end2 = p + len;
|
|
|
|
|
|
|
|
if( ( ret = asn1_get_bitstring_null( &p, end2, &len ) ) != 0 )
|
|
|
|
return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
|
|
|
|
|
|
|
|
if( p + len != end2 )
|
|
|
|
return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
|
|
|
|
POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
|
|
|
|
|
|
|
|
if( ( ret = x509_get_ecpubkey( &p, end2, eck ) ) != 0 )
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
else if ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
|
|
|
|
{
|
|
|
|
ecp_keypair_free( eck );
|
2013-06-24 19:00:59 +02:00
|
|
|
return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
|
2013-07-11 14:55:43 +02:00
|
|
|
}
|
2013-06-24 19:00:59 +02:00
|
|
|
|
2013-07-11 14:55:43 +02:00
|
|
|
if( ( ret = ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
|
2011-07-13 13:26:43 +02:00
|
|
|
{
|
2013-07-11 14:55:43 +02:00
|
|
|
ecp_keypair_free( eck );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2013-07-11 16:17:23 +02:00
|
|
|
#endif /* POLARSSL_ECP_C */
|
2013-07-11 14:55:43 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse an unencrypted PKCS#8 encoded private key
|
|
|
|
*/
|
|
|
|
static int x509parse_key_pkcs8_unencrypted_der(
|
|
|
|
pk_context *pk,
|
|
|
|
const unsigned char* key,
|
|
|
|
size_t keylen )
|
|
|
|
{
|
|
|
|
int ret, version;
|
|
|
|
size_t len;
|
|
|
|
x509_buf params;
|
|
|
|
unsigned char *p = (unsigned char *) key;
|
|
|
|
unsigned char *end = p + keylen;
|
|
|
|
pk_type_t pk_alg = POLARSSL_PK_NONE;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function parses the PrivatKeyInfo object (PKCS#8 v1.2 = RFC 5208)
|
|
|
|
*
|
|
|
|
* PrivateKeyInfo ::= SEQUENCE {
|
|
|
|
* version Version,
|
|
|
|
* privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
|
|
|
|
* privateKey PrivateKey,
|
|
|
|
* attributes [0] IMPLICIT Attributes OPTIONAL }
|
|
|
|
*
|
|
|
|
* Version ::= INTEGER
|
|
|
|
* PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
|
|
|
|
* PrivateKey ::= OCTET STRING
|
|
|
|
*
|
|
|
|
* The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
|
|
|
|
*/
|
|
|
|
|
|
|
|
if( ( ret = asn1_get_tag( &p, end, &len,
|
|
|
|
ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
|
|
|
|
{
|
|
|
|
return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
|
2011-07-13 13:26:43 +02:00
|
|
|
}
|
|
|
|
|
2013-06-24 19:00:59 +02:00
|
|
|
end = p + len;
|
2011-07-13 13:26:43 +02:00
|
|
|
|
2013-07-11 14:55:43 +02:00
|
|
|
if( ( ret = asn1_get_int( &p, end, &version ) ) != 0 )
|
|
|
|
return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
|
|
|
|
|
|
|
|
if( version != 0 )
|
|
|
|
return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
|
|
|
|
|
|
|
|
if( ( ret = x509_get_pk_alg( &p, end, &pk_alg, ¶ms ) ) != 0 )
|
|
|
|
return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
|
|
|
|
|
|
|
|
if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
|
|
|
|
return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
|
|
|
|
|
|
|
|
if( len < 1 )
|
|
|
|
return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
|
|
|
|
POLARSSL_ERR_ASN1_OUT_OF_DATA );
|
|
|
|
|
|
|
|
if( ( ret = pk_set_type( pk, pk_alg ) ) != 0 )
|
2013-06-24 19:00:59 +02:00
|
|
|
return( ret );
|
2011-07-13 13:26:43 +02:00
|
|
|
|
2013-07-11 16:17:23 +02:00
|
|
|
#if defined(POLARSSL_RSA_C)
|
|
|
|
if( pk_alg == POLARSSL_PK_RSA )
|
2013-07-11 14:55:43 +02:00
|
|
|
{
|
2013-07-11 16:17:23 +02:00
|
|
|
if( ( ret = x509parse_key_pkcs1_der( pk_rsa( *pk ), p, len ) ) != 0 )
|
2013-07-11 14:55:43 +02:00
|
|
|
{
|
|
|
|
pk_free( pk );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
} else
|
2013-07-11 16:17:23 +02:00
|
|
|
#endif /* POLARSSL_RSA_C */
|
|
|
|
#if defined(POLARSSL_ECP_C)
|
|
|
|
if( pk_alg == POLARSSL_PK_ECKEY || pk_alg == POLARSSL_PK_ECKEY_DH )
|
2013-07-11 14:55:43 +02:00
|
|
|
{
|
2013-07-11 16:17:23 +02:00
|
|
|
if( ( ret = x509_use_ecparams( ¶ms, &pk_ec( *pk )->grp ) ) != 0 ||
|
|
|
|
( ret = x509parse_key_sec1_der( pk_ec( *pk ), p, len ) ) != 0 )
|
2013-07-11 14:55:43 +02:00
|
|
|
{
|
|
|
|
pk_free( pk );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
} else
|
2013-07-11 16:17:23 +02:00
|
|
|
#endif /* POLARSSL_ECP_C */
|
2013-07-11 14:55:43 +02:00
|
|
|
return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-06-24 19:17:19 +02:00
|
|
|
/*
|
2013-07-11 16:17:23 +02:00
|
|
|
* Parse an encrypted PKCS#8 encoded private key
|
2013-06-24 19:17:19 +02:00
|
|
|
*/
|
2013-07-11 16:17:23 +02:00
|
|
|
static int x509parse_key_pkcs8_encrypted_der(
|
|
|
|
pk_context *pk,
|
2013-07-04 11:08:31 +02:00
|
|
|
const unsigned char *key, size_t keylen,
|
|
|
|
const unsigned char *pwd, size_t pwdlen )
|
2013-06-24 19:17:19 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t len;
|
2013-07-11 16:17:23 +02:00
|
|
|
unsigned char buf[2048];
|
2013-06-29 12:16:17 +02:00
|
|
|
unsigned char *p, *end;
|
2013-06-24 19:17:19 +02:00
|
|
|
x509_buf pbe_alg_oid, pbe_params;
|
2013-06-28 17:28:20 +02:00
|
|
|
#if defined(POLARSSL_PKCS12_C)
|
|
|
|
cipher_type_t cipher_alg;
|
|
|
|
md_type_t md_alg;
|
|
|
|
#endif
|
2013-06-24 19:17:19 +02:00
|
|
|
|
2013-07-11 16:17:23 +02:00
|
|
|
memset( buf, 0, sizeof( buf ) );
|
2013-06-24 19:17:19 +02:00
|
|
|
|
|
|
|
p = (unsigned char *) key;
|
|
|
|
end = p + keylen;
|
|
|
|
|
2013-06-24 19:28:55 +02:00
|
|
|
if( pwdlen == 0 )
|
|
|
|
return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
|
|
|
|
|
2013-06-24 19:17:19 +02:00
|
|
|
/*
|
|
|
|
* This function parses the EncryptedPrivatKeyInfo object (PKCS#8)
|
|
|
|
*
|
|
|
|
* EncryptedPrivateKeyInfo ::= SEQUENCE {
|
|
|
|
* encryptionAlgorithm EncryptionAlgorithmIdentifier,
|
|
|
|
* encryptedData EncryptedData
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
|
|
|
|
*
|
|
|
|
* EncryptedData ::= OCTET STRING
|
|
|
|
*
|
|
|
|
* The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
|
|
|
|
*/
|
|
|
|
if( ( ret = asn1_get_tag( &p, end, &len,
|
|
|
|
ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
|
|
|
|
{
|
|
|
|
return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
end = p + len;
|
|
|
|
|
2013-06-29 12:16:17 +02:00
|
|
|
if( ( ret = asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
|
2013-06-24 19:17:19 +02:00
|
|
|
return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
|
|
|
|
|
|
|
|
if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
|
|
|
|
return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
|
|
|
|
|
2013-07-11 16:17:23 +02:00
|
|
|
if( len > sizeof( buf ) )
|
2013-06-24 19:17:19 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_INPUT );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Decrypt EncryptedData with appropriate PDE
|
|
|
|
*/
|
2013-06-24 19:33:27 +02:00
|
|
|
#if defined(POLARSSL_PKCS12_C)
|
2013-06-28 17:28:20 +02:00
|
|
|
if( oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
|
2013-06-24 19:17:19 +02:00
|
|
|
{
|
2013-06-24 19:33:27 +02:00
|
|
|
if( ( ret = pkcs12_pbe( &pbe_params, PKCS12_PBE_DECRYPT,
|
2013-06-28 17:28:20 +02:00
|
|
|
cipher_alg, md_alg,
|
2013-06-24 19:33:27 +02:00
|
|
|
pwd, pwdlen, p, len, buf ) ) != 0 )
|
2013-06-24 19:17:19 +02:00
|
|
|
{
|
2013-06-24 19:33:27 +02:00
|
|
|
if( ret == POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH )
|
|
|
|
return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
|
|
|
|
|
2013-06-24 19:17:19 +02:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( OID_CMP( OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) )
|
|
|
|
{
|
|
|
|
if( ( ret = pkcs12_pbe_sha1_rc4_128( &pbe_params,
|
|
|
|
PKCS12_PBE_DECRYPT,
|
|
|
|
pwd, pwdlen,
|
|
|
|
p, len, buf ) ) != 0 )
|
|
|
|
{
|
|
|
|
return( ret );
|
|
|
|
}
|
2013-06-24 19:33:27 +02:00
|
|
|
|
|
|
|
// Best guess for password mismatch when using RC4. If first tag is
|
|
|
|
// not ASN1_CONSTRUCTED | ASN1_SEQUENCE
|
|
|
|
//
|
|
|
|
if( *buf != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
|
|
|
|
return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
|
2013-06-24 19:17:19 +02:00
|
|
|
}
|
2013-06-24 19:33:27 +02:00
|
|
|
else
|
|
|
|
#endif /* POLARSSL_PKCS12_C */
|
2013-06-24 19:28:55 +02:00
|
|
|
#if defined(POLARSSL_PKCS5_C)
|
2013-06-24 19:33:27 +02:00
|
|
|
if( OID_CMP( OID_PKCS5_PBES2, &pbe_alg_oid ) )
|
2013-06-24 19:28:55 +02:00
|
|
|
{
|
|
|
|
if( ( ret = pkcs5_pbes2( &pbe_params, PKCS5_DECRYPT, pwd, pwdlen,
|
|
|
|
p, len, buf ) ) != 0 )
|
|
|
|
{
|
|
|
|
if( ret == POLARSSL_ERR_PKCS5_PASSWORD_MISMATCH )
|
|
|
|
return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
|
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
}
|
2013-06-24 19:17:19 +02:00
|
|
|
else
|
2013-06-24 19:33:27 +02:00
|
|
|
#endif /* POLARSSL_PKCS5_C */
|
2013-06-24 19:17:19 +02:00
|
|
|
return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
|
|
|
|
|
2013-07-11 15:32:03 +02:00
|
|
|
return( x509parse_key_pkcs8_unencrypted_der( pk, buf, len ) );
|
2013-07-04 10:46:23 +02:00
|
|
|
}
|
|
|
|
|
2013-06-27 11:27:58 +02:00
|
|
|
/*
|
2013-07-11 15:32:03 +02:00
|
|
|
* Parse a private key
|
2013-06-27 11:27:58 +02:00
|
|
|
*/
|
2013-07-11 15:32:03 +02:00
|
|
|
int x509parse_key( pk_context *pk,
|
|
|
|
const unsigned char *key, size_t keylen,
|
|
|
|
const unsigned char *pwd, size_t pwdlen )
|
2013-06-27 11:27:58 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
#if defined(POLARSSL_PEM_C)
|
|
|
|
size_t len;
|
|
|
|
pem_context pem;
|
|
|
|
|
|
|
|
pem_init( &pem );
|
2013-07-11 15:32:03 +02:00
|
|
|
|
2013-07-11 16:17:23 +02:00
|
|
|
#if defined(POLARSSL_RSA_C)
|
2013-07-11 15:32:03 +02:00
|
|
|
ret = pem_read_buffer( &pem,
|
|
|
|
"-----BEGIN RSA PRIVATE KEY-----",
|
|
|
|
"-----END RSA PRIVATE KEY-----",
|
|
|
|
key, pwd, pwdlen, &len );
|
|
|
|
if( ret == 0 )
|
|
|
|
{
|
|
|
|
if( ( ret = pk_set_type( pk, POLARSSL_PK_RSA ) ) != 0 ||
|
|
|
|
( ret = x509parse_key_pkcs1_der( pk_rsa( *pk ),
|
|
|
|
pem.buf, pem.buflen ) ) != 0 )
|
|
|
|
{
|
|
|
|
pk_free( pk );
|
|
|
|
}
|
|
|
|
|
|
|
|
pem_free( &pem );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
|
|
|
|
return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
|
|
|
|
else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
|
|
|
|
return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
|
|
|
|
else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
|
|
|
|
return( ret );
|
2013-07-11 16:17:23 +02:00
|
|
|
#endif /* POLARSSL_RSA_C */
|
2013-07-11 15:32:03 +02:00
|
|
|
|
2013-07-11 16:17:23 +02:00
|
|
|
#if defined(POLARSSL_ECP_C)
|
2013-06-27 11:27:58 +02:00
|
|
|
ret = pem_read_buffer( &pem,
|
|
|
|
"-----BEGIN EC PRIVATE KEY-----",
|
|
|
|
"-----END EC PRIVATE KEY-----",
|
|
|
|
key, pwd, pwdlen, &len );
|
|
|
|
if( ret == 0 )
|
|
|
|
{
|
2013-07-11 15:32:03 +02:00
|
|
|
if( ( ret = pk_set_type( pk, POLARSSL_PK_ECKEY ) ) != 0 ||
|
|
|
|
( ret = x509parse_key_sec1_der( pk_ec( *pk ),
|
|
|
|
pem.buf, pem.buflen ) ) != 0 )
|
2013-06-27 11:27:58 +02:00
|
|
|
{
|
2013-07-11 15:32:03 +02:00
|
|
|
pk_free( pk );
|
2013-06-27 11:27:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pem_free( &pem );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
|
|
|
|
return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
|
|
|
|
else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
|
|
|
|
return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
|
|
|
|
else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
|
|
|
|
return( ret );
|
2013-07-11 16:17:23 +02:00
|
|
|
#endif /* POLARSSL_ECP_C */
|
2013-06-27 11:27:58 +02:00
|
|
|
|
2013-07-03 11:56:37 +02:00
|
|
|
ret = pem_read_buffer( &pem,
|
|
|
|
"-----BEGIN PRIVATE KEY-----",
|
|
|
|
"-----END PRIVATE KEY-----",
|
|
|
|
key, NULL, 0, &len );
|
|
|
|
if( ret == 0 )
|
|
|
|
{
|
2013-07-11 15:32:03 +02:00
|
|
|
if( ( ret = x509parse_key_pkcs8_unencrypted_der( pk,
|
2013-07-03 11:56:37 +02:00
|
|
|
pem.buf, pem.buflen ) ) != 0 )
|
|
|
|
{
|
2013-07-11 15:32:03 +02:00
|
|
|
pk_free( pk );
|
2013-07-03 11:56:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pem_free( &pem );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
|
|
|
|
return( ret );
|
|
|
|
|
|
|
|
ret = pem_read_buffer( &pem,
|
|
|
|
"-----BEGIN ENCRYPTED PRIVATE KEY-----",
|
|
|
|
"-----END ENCRYPTED PRIVATE KEY-----",
|
|
|
|
key, NULL, 0, &len );
|
|
|
|
if( ret == 0 )
|
|
|
|
{
|
2013-07-11 15:32:03 +02:00
|
|
|
if( ( ret = x509parse_key_pkcs8_encrypted_der( pk,
|
2013-07-03 11:56:37 +02:00
|
|
|
pem.buf, pem.buflen,
|
|
|
|
pwd, pwdlen ) ) != 0 )
|
|
|
|
{
|
2013-07-11 15:32:03 +02:00
|
|
|
pk_free( pk );
|
2013-07-03 11:56:37 +02:00
|
|
|
}
|
2013-06-27 11:27:58 +02:00
|
|
|
|
2013-07-03 11:56:37 +02:00
|
|
|
pem_free( &pem );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
|
|
|
|
return( ret );
|
2013-06-27 11:27:58 +02:00
|
|
|
#else
|
|
|
|
((void) pwd);
|
|
|
|
((void) pwdlen);
|
|
|
|
#endif /* POLARSSL_PEM_C */
|
|
|
|
|
2013-07-03 11:56:37 +02:00
|
|
|
/*
|
|
|
|
* At this point we only know it's not a PEM formatted key. Could be any
|
|
|
|
* of the known DER encoded private key formats
|
|
|
|
*
|
|
|
|
* We try the different DER format parsers to see if one passes without
|
|
|
|
* error
|
|
|
|
*/
|
2013-07-11 15:32:03 +02:00
|
|
|
if( ( ret = x509parse_key_pkcs8_encrypted_der( pk, key, keylen,
|
|
|
|
pwd, pwdlen ) ) == 0 )
|
2013-06-27 11:27:58 +02:00
|
|
|
{
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2013-07-11 15:32:03 +02:00
|
|
|
pk_free( pk );
|
2013-06-27 11:27:58 +02:00
|
|
|
|
|
|
|
if( ret == POLARSSL_ERR_X509_PASSWORD_MISMATCH )
|
|
|
|
{
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2013-07-11 15:32:03 +02:00
|
|
|
if( ( ret = x509parse_key_pkcs8_unencrypted_der( pk, key, keylen ) ) == 0 )
|
2013-07-03 11:56:37 +02:00
|
|
|
return( 0 );
|
|
|
|
|
2013-07-11 15:32:03 +02:00
|
|
|
pk_free( pk );
|
2013-07-01 19:45:45 +02:00
|
|
|
|
2013-07-11 16:17:23 +02:00
|
|
|
#if defined(POLARSSL_RSA_C)
|
2013-07-11 15:32:03 +02:00
|
|
|
if( ( ret = pk_set_type( pk, POLARSSL_PK_RSA ) ) == 0 &&
|
|
|
|
( ret = x509parse_key_pkcs1_der( pk_rsa( *pk ), key, keylen ) ) == 0 )
|
2013-07-09 13:10:49 +02:00
|
|
|
{
|
|
|
|
return( 0 );
|
|
|
|
}
|
2013-07-01 19:45:45 +02:00
|
|
|
|
2013-07-11 15:32:03 +02:00
|
|
|
pk_free( pk );
|
2013-07-11 16:17:23 +02:00
|
|
|
#endif /* POLARSSL_RSA_C */
|
2013-07-01 19:45:45 +02:00
|
|
|
|
2013-07-11 16:17:23 +02:00
|
|
|
#if defined(POLARSSL_ECP_C)
|
2013-07-11 15:32:03 +02:00
|
|
|
if( ( ret = pk_set_type( pk, POLARSSL_PK_ECKEY ) ) == 0 &&
|
|
|
|
( ret = x509parse_key_sec1_der( pk_ec( *pk ), key, keylen ) ) == 0 )
|
2013-07-01 19:45:45 +02:00
|
|
|
{
|
2013-07-09 13:10:49 +02:00
|
|
|
return( 0 );
|
2013-07-01 19:45:45 +02:00
|
|
|
}
|
|
|
|
|
2013-07-11 15:32:03 +02:00
|
|
|
pk_free( pk );
|
2013-07-11 16:17:23 +02:00
|
|
|
#endif /* POLARSSL_ECP_C */
|
2013-07-09 13:10:49 +02:00
|
|
|
|
2013-07-11 15:32:03 +02:00
|
|
|
return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
|
2013-07-01 19:45:45 +02:00
|
|
|
}
|
|
|
|
|
2013-06-27 11:27:58 +02:00
|
|
|
/*
|
2013-07-09 13:10:49 +02:00
|
|
|
* Parse a public key
|
2013-06-27 11:27:58 +02:00
|
|
|
*/
|
2013-07-09 13:10:49 +02:00
|
|
|
int x509parse_public_key( pk_context *ctx,
|
|
|
|
const unsigned char *key, size_t keylen )
|
2013-06-27 11:27:58 +02:00
|
|
|
{
|
|
|
|
int ret;
|
2013-07-09 13:10:49 +02:00
|
|
|
unsigned char *p;
|
2013-06-27 11:27:58 +02:00
|
|
|
#if defined(POLARSSL_PEM_C)
|
2013-07-01 19:45:45 +02:00
|
|
|
size_t len;
|
2013-06-27 11:27:58 +02:00
|
|
|
pem_context pem;
|
|
|
|
|
|
|
|
pem_init( &pem );
|
2013-07-01 19:45:45 +02:00
|
|
|
ret = pem_read_buffer( &pem,
|
2013-06-27 11:27:58 +02:00
|
|
|
"-----BEGIN PUBLIC KEY-----",
|
|
|
|
"-----END PUBLIC KEY-----",
|
|
|
|
key, NULL, 0, &len );
|
|
|
|
|
|
|
|
if( ret == 0 )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Was PEM encoded
|
|
|
|
*/
|
2013-07-01 19:45:45 +02:00
|
|
|
key = pem.buf;
|
2013-06-27 11:27:58 +02:00
|
|
|
keylen = pem.buflen;
|
|
|
|
}
|
|
|
|
else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
|
|
|
|
{
|
|
|
|
pem_free( &pem );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
#endif
|
2013-07-09 13:10:49 +02:00
|
|
|
p = (unsigned char *) key;
|
2013-06-27 11:27:58 +02:00
|
|
|
|
2013-07-09 13:10:49 +02:00
|
|
|
ret = x509_get_pubkey( &p, p + keylen, ctx );
|
2013-06-27 11:27:58 +02:00
|
|
|
|
|
|
|
#if defined(POLARSSL_PEM_C)
|
|
|
|
pem_free( &pem );
|
|
|
|
#endif
|
|
|
|
|
2013-07-01 19:45:45 +02:00
|
|
|
return( ret );
|
2013-06-27 11:27:58 +02:00
|
|
|
}
|
2013-07-04 14:09:57 +02:00
|
|
|
|
2013-07-11 16:17:23 +02:00
|
|
|
#if defined(POLARSSL_RSA_C)
|
|
|
|
/*
|
|
|
|
* Parse a private RSA key
|
|
|
|
*/
|
|
|
|
int x509parse_key_rsa( rsa_context *rsa,
|
|
|
|
const unsigned char *key, size_t keylen,
|
|
|
|
const unsigned char *pwd, size_t pwdlen )
|
|
|
|
{
|
|
|
|
pk_context pk;
|
|
|
|
|
|
|
|
pk_init( &pk );
|
|
|
|
pk_wrap_rsa( &pk, rsa );
|
|
|
|
|
|
|
|
return( x509parse_key( &pk, key, keylen, pwd, pwdlen ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse a public RSA key
|
|
|
|
*/
|
|
|
|
int x509parse_public_key_rsa( rsa_context *rsa,
|
|
|
|
const unsigned char *key, size_t keylen )
|
|
|
|
{
|
|
|
|
pk_context pk;
|
|
|
|
|
|
|
|
pk_init( &pk );
|
|
|
|
pk_wrap_rsa( &pk, rsa );
|
|
|
|
|
|
|
|
return( x509parse_public_key( &pk, key, keylen ) );
|
|
|
|
}
|
|
|
|
#endif /* POLARSSL_RSA_C */
|
|
|
|
|
2011-04-04 23:36:15 +02:00
|
|
|
#if defined(POLARSSL_DHM_C)
|
2011-01-06 16:48:19 +01:00
|
|
|
/*
|
|
|
|
* Parse DHM parameters
|
|
|
|
*/
|
2011-04-24 10:57:21 +02:00
|
|
|
int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
|
2011-01-06 16:48:19 +01:00
|
|
|
{
|
2011-04-24 10:57:21 +02:00
|
|
|
int ret;
|
|
|
|
size_t len;
|
2011-01-06 16:48:19 +01:00
|
|
|
unsigned char *p, *end;
|
2011-02-12 15:30:57 +01:00
|
|
|
#if defined(POLARSSL_PEM_C)
|
|
|
|
pem_context pem;
|
2011-01-06 16:48:19 +01:00
|
|
|
|
2011-02-12 15:30:57 +01:00
|
|
|
pem_init( &pem );
|
2011-01-06 16:48:19 +01:00
|
|
|
|
2011-02-12 15:30:57 +01:00
|
|
|
ret = pem_read_buffer( &pem,
|
|
|
|
"-----BEGIN DH PARAMETERS-----",
|
|
|
|
"-----END DH PARAMETERS-----",
|
|
|
|
dhmin, NULL, 0, &dhminlen );
|
2011-01-06 16:48:19 +01:00
|
|
|
|
2011-02-12 15:30:57 +01:00
|
|
|
if( ret == 0 )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Was PEM encoded
|
|
|
|
*/
|
|
|
|
dhminlen = pem.buflen;
|
2011-01-06 16:48:19 +01:00
|
|
|
}
|
2013-06-24 13:02:41 +02:00
|
|
|
else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
|
2011-01-06 16:48:19 +01:00
|
|
|
{
|
2011-02-12 15:30:57 +01:00
|
|
|
pem_free( &pem );
|
|
|
|
return( ret );
|
2011-01-06 16:48:19 +01:00
|
|
|
}
|
|
|
|
|
2011-02-12 15:30:57 +01:00
|
|
|
p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
|
|
|
|
#else
|
|
|
|
p = (unsigned char *) dhmin;
|
|
|
|
#endif
|
2011-01-06 16:48:19 +01:00
|
|
|
end = p + dhminlen;
|
|
|
|
|
2011-02-12 15:30:57 +01:00
|
|
|
memset( dhm, 0, sizeof( dhm_context ) );
|
|
|
|
|
2011-01-06 16:48:19 +01:00
|
|
|
/*
|
|
|
|
* DHParams ::= SEQUENCE {
|
|
|
|
* prime INTEGER, -- P
|
|
|
|
* generator INTEGER, -- g
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
if( ( ret = asn1_get_tag( &p, end, &len,
|
|
|
|
ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
|
|
|
|
{
|
2011-02-12 15:30:57 +01:00
|
|
|
#if defined(POLARSSL_PEM_C)
|
|
|
|
pem_free( &pem );
|
|
|
|
#endif
|
2011-05-09 18:17:09 +02:00
|
|
|
return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
|
2011-01-06 16:48:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
end = p + len;
|
|
|
|
|
|
|
|
if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
|
|
|
|
( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
|
|
|
|
{
|
2011-02-12 15:30:57 +01:00
|
|
|
#if defined(POLARSSL_PEM_C)
|
|
|
|
pem_free( &pem );
|
|
|
|
#endif
|
2011-01-06 16:48:19 +01:00
|
|
|
dhm_free( dhm );
|
2011-05-09 18:17:09 +02:00
|
|
|
return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
|
2011-01-06 16:48:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if( p != end )
|
|
|
|
{
|
2011-02-12 15:30:57 +01:00
|
|
|
#if defined(POLARSSL_PEM_C)
|
|
|
|
pem_free( &pem );
|
|
|
|
#endif
|
2011-01-06 16:48:19 +01:00
|
|
|
dhm_free( dhm );
|
2011-05-09 18:17:09 +02:00
|
|
|
return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
|
2011-01-06 16:48:19 +01:00
|
|
|
POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
|
|
|
|
}
|
|
|
|
|
2011-02-12 15:30:57 +01:00
|
|
|
#if defined(POLARSSL_PEM_C)
|
|
|
|
pem_free( &pem );
|
|
|
|
#endif
|
2011-01-06 16:48:19 +01:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2011-04-25 17:28:35 +02:00
|
|
|
#if defined(POLARSSL_FS_IO)
|
2011-01-06 16:48:19 +01:00
|
|
|
/*
|
2013-06-27 13:00:00 +02:00
|
|
|
* Load and parse DHM parameters
|
2011-01-06 16:48:19 +01:00
|
|
|
*/
|
|
|
|
int x509parse_dhmfile( dhm_context *dhm, const char *path )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t n;
|
|
|
|
unsigned char *buf;
|
|
|
|
|
2011-12-10 22:55:01 +01:00
|
|
|
if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
|
|
|
|
return( ret );
|
2011-01-06 16:48:19 +01:00
|
|
|
|
2011-06-09 15:55:13 +02:00
|
|
|
ret = x509parse_dhm( dhm, buf, n );
|
2011-01-06 16:48:19 +01:00
|
|
|
|
|
|
|
memset( buf, 0, n + 1 );
|
2013-07-03 13:37:05 +02:00
|
|
|
polarssl_free( buf );
|
2011-01-06 16:48:19 +01:00
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
2011-04-25 17:28:35 +02:00
|
|
|
#endif /* POLARSSL_FS_IO */
|
2011-04-04 23:36:15 +02:00
|
|
|
#endif /* POLARSSL_DHM_C */
|
2011-01-06 16:48:19 +01:00
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
#if defined _MSC_VER && !defined snprintf
|
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.
|
|
|
|
*/
|
2013-04-07 22:00:46 +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
|
|
|
|
if ( res < 0 )
|
2011-04-24 10:57:21 +02:00
|
|
|
return( (int) size + 20 );
|
2009-05-02 17:13:40 +02:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define snprintf compat_snprintf
|
2009-01-03 22:22:43 +01:00
|
|
|
#endif
|
|
|
|
|
2009-05-02 17:13:40 +02:00
|
|
|
#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
|
|
|
|
|
|
|
|
#define SAFE_SNPRINTF() \
|
|
|
|
{ \
|
|
|
|
if( ret == -1 ) \
|
|
|
|
return( -1 ); \
|
|
|
|
\
|
2011-04-24 10:57:21 +02:00
|
|
|
if ( (unsigned int) ret > n ) { \
|
2009-05-02 17:13:40 +02:00
|
|
|
p[n - 1] = '\0'; \
|
|
|
|
return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
|
|
|
|
} \
|
|
|
|
\
|
2011-04-24 10:57:21 +02:00
|
|
|
n -= (unsigned int) ret; \
|
|
|
|
p += (unsigned int) ret; \
|
2009-05-02 17:13:40 +02:00
|
|
|
}
|
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
|
|
|
* Store the name in printable form into buf; no more
|
2009-05-02 17:13:40 +02:00
|
|
|
* than size characters will be written
|
2009-01-03 22:22:43 +01:00
|
|
|
*/
|
2010-03-16 22:09:09 +01:00
|
|
|
int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2011-04-24 10:57:21 +02:00
|
|
|
int ret;
|
|
|
|
size_t i, n;
|
2009-01-03 22:22:43 +01:00
|
|
|
unsigned char c;
|
2010-03-16 22:09:09 +01:00
|
|
|
const x509_name *name;
|
2013-04-07 22:00:46 +02:00
|
|
|
const char *short_name = NULL;
|
2009-01-03 22:22:43 +01:00
|
|
|
char s[128], *p;
|
|
|
|
|
|
|
|
memset( s, 0, sizeof( s ) );
|
|
|
|
|
|
|
|
name = dn;
|
|
|
|
p = buf;
|
2009-05-02 17:13:40 +02:00
|
|
|
n = size;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
while( name != NULL )
|
|
|
|
{
|
2012-06-27 13:51:09 +02:00
|
|
|
if( !name->oid.p )
|
|
|
|
{
|
|
|
|
name = name->next;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-01-15 17:57:55 +01:00
|
|
|
if( name != dn )
|
|
|
|
{
|
2009-05-02 17:13:40 +02:00
|
|
|
ret = snprintf( p, n, ", " );
|
|
|
|
SAFE_SNPRINTF();
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2013-04-07 22:00:46 +02:00
|
|
|
ret = oid_get_attr_short_name( &name->oid, &short_name );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2013-04-07 22:00:46 +02:00
|
|
|
if( ret == 0 )
|
|
|
|
ret = snprintf( p, n, "%s=", short_name );
|
2009-01-03 22:22:43 +01:00
|
|
|
else
|
2009-05-02 17:13:40 +02:00
|
|
|
ret = snprintf( p, n, "\?\?=" );
|
2013-04-07 22:00:46 +02:00
|
|
|
SAFE_SNPRINTF();
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
for( i = 0; i < name->val.len; i++ )
|
|
|
|
{
|
2011-06-09 15:55:13 +02:00
|
|
|
if( i >= sizeof( s ) - 1 )
|
2009-01-03 22:22:43 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
c = name->val.p[i];
|
|
|
|
if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
|
|
|
|
s[i] = '?';
|
|
|
|
else s[i] = c;
|
|
|
|
}
|
|
|
|
s[i] = '\0';
|
2009-05-02 17:13:40 +02:00
|
|
|
ret = snprintf( p, n, "%s", s );
|
2013-04-07 22:00:46 +02:00
|
|
|
SAFE_SNPRINTF();
|
2009-01-03 22:22:43 +01:00
|
|
|
name = name->next;
|
|
|
|
}
|
|
|
|
|
2011-04-24 10:57:21 +02:00
|
|
|
return( (int) ( size - n ) );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2011-01-16 22:34:59 +01:00
|
|
|
/*
|
|
|
|
* Store the serial in printable form into buf; no more
|
|
|
|
* than size characters will be written
|
|
|
|
*/
|
|
|
|
int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
|
|
|
|
{
|
2011-04-24 10:57:21 +02:00
|
|
|
int ret;
|
|
|
|
size_t i, n, nr;
|
2011-01-16 22:34:59 +01:00
|
|
|
char *p;
|
|
|
|
|
|
|
|
p = buf;
|
|
|
|
n = size;
|
|
|
|
|
|
|
|
nr = ( serial->len <= 32 )
|
2011-11-25 13:37:37 +01:00
|
|
|
? serial->len : 28;
|
2011-01-16 22:34:59 +01:00
|
|
|
|
|
|
|
for( i = 0; i < nr; i++ )
|
|
|
|
{
|
2011-12-05 15:38:06 +01:00
|
|
|
if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
|
2011-12-05 15:22:49 +01:00
|
|
|
continue;
|
|
|
|
|
2011-01-16 22:34:59 +01:00
|
|
|
ret = snprintf( p, n, "%02X%s",
|
|
|
|
serial->p[i], ( i < nr - 1 ) ? ":" : "" );
|
|
|
|
SAFE_SNPRINTF();
|
|
|
|
}
|
|
|
|
|
2011-11-25 13:37:37 +01:00
|
|
|
if( nr != serial->len )
|
|
|
|
{
|
|
|
|
ret = snprintf( p, n, "...." );
|
|
|
|
SAFE_SNPRINTF();
|
|
|
|
}
|
|
|
|
|
2011-04-24 10:57:21 +02:00
|
|
|
return( (int) ( size - n ) );
|
2011-01-16 22:34:59 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
2010-03-16 22:09:09 +01:00
|
|
|
int x509parse_cert_info( char *buf, size_t size, const char *prefix,
|
|
|
|
const x509_cert *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-04-07 22:00:46 +02:00
|
|
|
const char *desc = NULL;
|
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
|
|
|
|
2009-05-02 17:13:40 +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();
|
|
|
|
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
|
|
|
|
2011-01-16 22:34:59 +01:00
|
|
|
ret = x509parse_serial_gets( p, n, &crt->serial);
|
|
|
|
SAFE_SNPRINTF();
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2009-05-02 17:13:40 +02:00
|
|
|
ret = snprintf( p, n, "\n%sissuer name : ", prefix );
|
|
|
|
SAFE_SNPRINTF();
|
|
|
|
ret = x509parse_dn_gets( p, n, &crt->issuer );
|
|
|
|
SAFE_SNPRINTF();
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2009-05-02 17:13:40 +02:00
|
|
|
ret = snprintf( p, n, "\n%ssubject name : ", prefix );
|
|
|
|
SAFE_SNPRINTF();
|
|
|
|
ret = x509parse_dn_gets( p, n, &crt->subject );
|
|
|
|
SAFE_SNPRINTF();
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2009-05-02 17:13:40 +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
|
|
|
|
2009-05-02 17:13:40 +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
|
|
|
|
2013-04-07 22:00:46 +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
|
|
|
|
2013-04-07 22:00:46 +02:00
|
|
|
ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
|
|
|
|
if( ret != 0 )
|
|
|
|
ret = snprintf( p, n, "???" );
|
|
|
|
else
|
|
|
|
ret = snprintf( p, n, desc );
|
2009-05-02 17:13:40 +02:00
|
|
|
SAFE_SNPRINTF();
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2013-07-11 16:17:23 +02:00
|
|
|
#if defined(POLARSSL_RSA_C)
|
|
|
|
if( crt->pk.type == POLARSSL_PK_RSA )
|
|
|
|
ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
|
|
|
|
(int) pk_rsa( crt->pk )->N.n * (int) sizeof( t_uint ) * 8 );
|
|
|
|
else
|
|
|
|
#endif /* POLARSSL_RSA_C */
|
|
|
|
#if defined(POLARSSL_ECP_C)
|
|
|
|
if( crt->pk.type == POLARSSL_PK_ECKEY ||
|
|
|
|
crt->pk.type == POLARSSL_PK_ECKEY_DH )
|
|
|
|
ret = snprintf( p, n, "\n%sEC key size : %d bits\n", prefix,
|
|
|
|
(int) pk_ec( crt->pk )->grp.pbits );
|
|
|
|
else
|
|
|
|
#endif /* POLARSSL_ECP_C */
|
|
|
|
ret = snprintf(p, n, "\n%sPK type looks wrong!", prefix);
|
2009-05-02 17:13:40 +02:00
|
|
|
SAFE_SNPRINTF();
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2011-04-24 10:57:21 +02:00
|
|
|
return( (int) ( size - n ) );
|
2009-05-02 17:13:40 +02:00
|
|
|
}
|
|
|
|
|
2011-01-15 17:57:55 +01:00
|
|
|
/*
|
|
|
|
* Return an informational string describing the given OID
|
|
|
|
*/
|
|
|
|
const char *x509_oid_get_description( x509_buf *oid )
|
|
|
|
{
|
2013-04-07 22:00:46 +02:00
|
|
|
const char *desc = NULL;
|
|
|
|
int ret;
|
2011-01-15 17:57:55 +01:00
|
|
|
|
2013-04-07 22:00:46 +02:00
|
|
|
ret = oid_get_extended_key_usage( oid, &desc );
|
2011-01-15 17:57:55 +01:00
|
|
|
|
2013-04-07 22:00:46 +02:00
|
|
|
if( ret != 0 )
|
|
|
|
return( NULL );
|
2011-01-15 17:57:55 +01:00
|
|
|
|
2013-04-07 22:00:46 +02:00
|
|
|
return( desc );
|
2011-01-15 17:57:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the x.y.z.... style numeric string for the given OID */
|
|
|
|
int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
|
|
|
|
{
|
2013-04-07 22:00:46 +02:00
|
|
|
return oid_get_numeric_string( buf, size, oid );
|
2011-01-15 17:57:55 +01:00
|
|
|
}
|
|
|
|
|
2009-05-02 17:13:40 +02:00
|
|
|
/*
|
|
|
|
* Return an informational string about the CRL.
|
|
|
|
*/
|
2010-03-16 22:09:09 +01:00
|
|
|
int x509parse_crl_info( char *buf, size_t size, const char *prefix,
|
|
|
|
const x509_crl *crl )
|
2009-05-02 17:13:40 +02:00
|
|
|
{
|
2011-04-24 10:57:21 +02:00
|
|
|
int ret;
|
2011-12-05 15:22:49 +01:00
|
|
|
size_t n;
|
2009-05-02 17:13:40 +02:00
|
|
|
char *p;
|
2013-04-07 22:00:46 +02:00
|
|
|
const char *desc;
|
2010-03-16 22:09:09 +01:00
|
|
|
const x509_crl_entry *entry;
|
2009-05-02 17:13:40 +02:00
|
|
|
|
|
|
|
p = buf;
|
|
|
|
n = size;
|
|
|
|
|
|
|
|
ret = snprintf( p, n, "%sCRL version : %d",
|
|
|
|
prefix, crl->version );
|
|
|
|
SAFE_SNPRINTF();
|
|
|
|
|
|
|
|
ret = snprintf( p, n, "\n%sissuer name : ", prefix );
|
|
|
|
SAFE_SNPRINTF();
|
|
|
|
ret = x509parse_dn_gets( p, n, &crl->issuer );
|
|
|
|
SAFE_SNPRINTF();
|
|
|
|
|
|
|
|
ret = snprintf( p, n, "\n%sthis update : " \
|
|
|
|
"%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();
|
|
|
|
|
|
|
|
ret = snprintf( p, n, "\n%snext update : " \
|
|
|
|
"%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;
|
|
|
|
|
|
|
|
ret = snprintf( p, n, "\n%sRevoked certificates:",
|
|
|
|
prefix );
|
|
|
|
SAFE_SNPRINTF();
|
|
|
|
|
2009-07-27 22:21:53 +02:00
|
|
|
while( entry != NULL && entry->raw.len != 0 )
|
2009-05-02 17:13:40 +02:00
|
|
|
{
|
|
|
|
ret = snprintf( p, n, "\n%sserial number: ",
|
|
|
|
prefix );
|
|
|
|
SAFE_SNPRINTF();
|
|
|
|
|
2011-12-05 15:22:49 +01:00
|
|
|
ret = x509parse_serial_gets( p, n, &entry->serial);
|
|
|
|
SAFE_SNPRINTF();
|
2009-05-02 17:13:40 +02:00
|
|
|
|
|
|
|
ret = snprintf( p, n, " revocation date: " \
|
|
|
|
"%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 );
|
2011-12-05 15:22:49 +01:00
|
|
|
SAFE_SNPRINTF();
|
2009-05-02 17:13:40 +02:00
|
|
|
|
|
|
|
entry = entry->next;
|
|
|
|
}
|
|
|
|
|
2013-04-07 22:00:46 +02:00
|
|
|
ret = snprintf( p, n, "\n%ssigned using : ", prefix );
|
2009-05-02 17:13:40 +02:00
|
|
|
SAFE_SNPRINTF();
|
|
|
|
|
2013-04-07 22:00:46 +02:00
|
|
|
ret = oid_get_sig_alg_desc( &crl->sig_oid1, &desc );
|
|
|
|
if( ret != 0 )
|
|
|
|
ret = snprintf( p, n, "???" );
|
|
|
|
else
|
|
|
|
ret = snprintf( p, n, desc );
|
2009-05-02 17:13:40 +02:00
|
|
|
SAFE_SNPRINTF();
|
|
|
|
|
2009-07-19 22:25:25 +02:00
|
|
|
ret = snprintf( p, n, "\n" );
|
|
|
|
SAFE_SNPRINTF();
|
|
|
|
|
2011-04-24 10:57:21 +02:00
|
|
|
return( (int) ( size - n ) );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2009-05-03 12:18:48 +02:00
|
|
|
* Return 0 if the x509_time is still valid, or 1 otherwise.
|
2009-01-03 22:22:43 +01:00
|
|
|
*/
|
2013-07-03 15:31:03 +02:00
|
|
|
#if defined(POLARSSL_HAVE_TIME)
|
2010-03-16 22:09:09 +01:00
|
|
|
int x509parse_time_expired( const x509_time *to )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2011-11-18 15:26:47 +01:00
|
|
|
int year, mon, day;
|
|
|
|
int hour, min, sec;
|
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
SYSTEMTIME st;
|
|
|
|
|
|
|
|
GetLocalTime(&st);
|
|
|
|
|
|
|
|
year = st.wYear;
|
|
|
|
mon = st.wMonth;
|
|
|
|
day = st.wDay;
|
|
|
|
hour = st.wHour;
|
|
|
|
min = st.wMinute;
|
|
|
|
sec = st.wSecond;
|
|
|
|
#else
|
2009-01-03 22:22:43 +01:00
|
|
|
struct tm *lt;
|
|
|
|
time_t tt;
|
|
|
|
|
|
|
|
tt = time( NULL );
|
|
|
|
lt = localtime( &tt );
|
|
|
|
|
2011-11-18 15:26:47 +01:00
|
|
|
year = lt->tm_year + 1900;
|
|
|
|
mon = lt->tm_mon + 1;
|
|
|
|
day = lt->tm_mday;
|
|
|
|
hour = lt->tm_hour;
|
|
|
|
min = lt->tm_min;
|
|
|
|
sec = lt->tm_sec;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if( year > to->year )
|
2009-05-03 12:18:48 +02:00
|
|
|
return( 1 );
|
|
|
|
|
2011-11-18 15:26:47 +01:00
|
|
|
if( year == to->year &&
|
|
|
|
mon > to->mon )
|
2009-05-03 12:18:48 +02:00
|
|
|
return( 1 );
|
|
|
|
|
2011-11-18 15:26:47 +01:00
|
|
|
if( year == to->year &&
|
|
|
|
mon == to->mon &&
|
|
|
|
day > to->day )
|
2009-05-03 12:18:48 +02:00
|
|
|
return( 1 );
|
|
|
|
|
2011-11-18 15:26:47 +01:00
|
|
|
if( year == to->year &&
|
|
|
|
mon == to->mon &&
|
|
|
|
day == to->day &&
|
|
|
|
hour > to->hour )
|
2011-01-16 22:40:22 +01:00
|
|
|
return( 1 );
|
|
|
|
|
2011-11-18 15:26:47 +01:00
|
|
|
if( year == to->year &&
|
|
|
|
mon == to->mon &&
|
|
|
|
day == to->day &&
|
|
|
|
hour == to->hour &&
|
|
|
|
min > to->min )
|
2011-01-16 22:40:22 +01:00
|
|
|
return( 1 );
|
|
|
|
|
2011-11-18 15:26:47 +01:00
|
|
|
if( year == to->year &&
|
|
|
|
mon == to->mon &&
|
|
|
|
day == to->day &&
|
|
|
|
hour == to->hour &&
|
|
|
|
min == to->min &&
|
|
|
|
sec > to->sec )
|
2011-01-16 22:40:22 +01:00
|
|
|
return( 1 );
|
|
|
|
|
2009-05-03 12:18:48 +02:00
|
|
|
return( 0 );
|
|
|
|
}
|
2013-07-03 15:31:03 +02:00
|
|
|
#else /* POLARSSL_HAVE_TIME */
|
|
|
|
int x509parse_time_expired( const x509_time *to )
|
|
|
|
{
|
|
|
|
((void) to);
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
#endif /* POLARSSL_HAVE_TIME */
|
2009-05-03 12:18:48 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Return 1 if the certificate is revoked, or 0 otherwise.
|
|
|
|
*/
|
2010-03-16 22:09:09 +01:00
|
|
|
int x509parse_revoked( const x509_cert *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
|
|
|
{
|
|
|
|
if( x509parse_time_expired( &cur->revocation_date ) )
|
|
|
|
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
|
|
|
/*
|
|
|
|
* Check that the given certificate is valid accoring to the CRL.
|
|
|
|
*/
|
|
|
|
static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
|
|
|
|
x509_crl *crl_list)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
2013-07-11 10:46:21 +02:00
|
|
|
/* EC NOT IMPLEMENTED YET */
|
|
|
|
if( ca->pk.type != POLARSSL_PK_RSA )
|
|
|
|
return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
|
|
|
|
|
|
|
|
if( !rsa_pkcs1_verify( pk_rsa( ca->pk ), RSA_PUBLIC, crl_list->sig_md,
|
2011-01-16 22:12:10 +01:00
|
|
|
0, hash, crl_list->sig.p ) == 0 )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* CRL is not trusted
|
|
|
|
*/
|
|
|
|
flags |= BADCRL_NOT_TRUSTED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for validity of CRL (Do not drop out)
|
|
|
|
*/
|
|
|
|
if( x509parse_time_expired( &crl_list->next_update ) )
|
|
|
|
flags |= BADCRL_EXPIRED;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if certificate is revoked
|
|
|
|
*/
|
|
|
|
if( x509parse_revoked(crt, crl_list) )
|
|
|
|
{
|
|
|
|
flags |= BADCERT_REVOKED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
crl_list = crl_list->next;
|
|
|
|
}
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
size_t cn_idx = 0;
|
|
|
|
|
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 );
|
|
|
|
|
|
|
|
for( i = 0; i < strlen( cn ); ++i )
|
|
|
|
{
|
|
|
|
if( cn[i] == '.' )
|
|
|
|
{
|
|
|
|
cn_idx = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( cn_idx == 0 )
|
|
|
|
return( 0 );
|
|
|
|
|
2012-08-23 12:49:55 +02:00
|
|
|
if( strlen( cn ) - cn_idx == name->len - 1 &&
|
|
|
|
memcmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
|
2012-02-11 17:09:32 +01:00
|
|
|
{
|
|
|
|
return( 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2012-09-28 09:10:55 +02:00
|
|
|
static int x509parse_verify_top(
|
|
|
|
x509_cert *child, x509_cert *trust_ca,
|
2012-11-14 13:39:52 +01:00
|
|
|
x509_crl *ca_crl, int path_cnt, int *flags,
|
2012-09-28 09:10:55 +02:00
|
|
|
int (*f_vrfy)(void *, x509_cert *, int, int *),
|
|
|
|
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
|
|
|
|
|
|
|
if( x509parse_time_expired( &child->valid_to ) )
|
|
|
|
*flags |= BADCERT_EXPIRED;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Child is the top of the chain. Check against the trust_ca list.
|
|
|
|
*/
|
|
|
|
*flags |= BADCERT_NOT_TRUSTED;
|
|
|
|
|
|
|
|
while( trust_ca != NULL )
|
|
|
|
{
|
|
|
|
if( trust_ca->version == 0 ||
|
|
|
|
child->issuer_raw.len != trust_ca->subject_raw.len ||
|
|
|
|
memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
|
|
|
|
child->issuer_raw.len ) != 0 )
|
|
|
|
{
|
|
|
|
trust_ca = trust_ca->next;
|
|
|
|
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,
|
|
|
|
child->issuer_raw.len ) == 0 )
|
|
|
|
{
|
|
|
|
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
|
|
|
{
|
|
|
|
trust_ca = trust_ca->next;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
continue;
|
|
|
|
}
|
2012-09-28 09:10:55 +02:00
|
|
|
|
2013-04-07 22:00:46 +02:00
|
|
|
md( md_info, child->tbs.p, child->tbs.len, hash );
|
2012-09-28 09:10:55 +02:00
|
|
|
|
2013-07-11 10:46:21 +02:00
|
|
|
/* EC NOT IMPLEMENTED YET */
|
|
|
|
if( trust_ca->pk.type != POLARSSL_PK_RSA )
|
|
|
|
return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
|
|
|
|
|
|
|
|
if( rsa_pkcs1_verify( pk_rsa( trust_ca->pk ), RSA_PUBLIC, child->sig_md,
|
2012-09-28 09:10:55 +02:00
|
|
|
0, hash, child->sig.p ) != 0 )
|
|
|
|
{
|
|
|
|
trust_ca = trust_ca->next;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
{
|
|
|
|
/* Check trusted CA's CRL for then chain's top crt */
|
|
|
|
*flags |= x509parse_verifycrl( child, trust_ca, ca_crl );
|
|
|
|
|
|
|
|
if( x509parse_time_expired( &trust_ca->valid_to ) )
|
|
|
|
ca_flags |= BADCERT_EXPIRED;
|
|
|
|
|
|
|
|
if( NULL != f_vrfy )
|
|
|
|
{
|
2012-11-14 13:39:52 +01: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 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Call callback on top cert */
|
|
|
|
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 |= ca_flags;
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
static int x509parse_verify_child(
|
|
|
|
x509_cert *child, x509_cert *parent, x509_cert *trust_ca,
|
2012-11-14 13:39:52 +01:00
|
|
|
x509_crl *ca_crl, int path_cnt, int *flags,
|
2012-09-28 09:10:55 +02:00
|
|
|
int (*f_vrfy)(void *, x509_cert *, int, int *),
|
|
|
|
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];
|
2012-09-28 09:10:55 +02:00
|
|
|
x509_cert *grandparent;
|
2013-04-07 22:00:46 +02:00
|
|
|
const md_info_t *md_info;
|
2012-09-28 09:10:55 +02:00
|
|
|
|
|
|
|
if( x509parse_time_expired( &child->valid_to ) )
|
|
|
|
*flags |= BADCERT_EXPIRED;
|
|
|
|
|
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
|
|
|
|
2013-07-11 10:46:21 +02:00
|
|
|
/* EC NOT IMPLEMENTED YET */
|
|
|
|
if( parent->pk.type != POLARSSL_PK_RSA )
|
|
|
|
return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
|
|
|
|
|
|
|
|
if( rsa_pkcs1_verify( pk_rsa( parent->pk ), RSA_PUBLIC, child->sig_md,
|
|
|
|
0, hash, child->sig.p ) != 0 )
|
|
|
|
{
|
2013-04-07 22:00:46 +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
|
|
|
|
|
|
|
/* Check trusted CA's CRL for the given crt */
|
|
|
|
*flags |= x509parse_verifycrl(child, parent, ca_crl);
|
|
|
|
|
|
|
|
grandparent = parent->next;
|
|
|
|
|
|
|
|
while( grandparent != NULL )
|
|
|
|
{
|
|
|
|
if( grandparent->version == 0 ||
|
|
|
|
grandparent->ca_istrue == 0 ||
|
|
|
|
parent->issuer_raw.len != grandparent->subject_raw.len ||
|
|
|
|
memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
|
|
|
|
parent->issuer_raw.len ) != 0 )
|
|
|
|
{
|
|
|
|
grandparent = grandparent->next;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( grandparent != NULL )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Part of the chain
|
|
|
|
*/
|
2012-11-14 13:39:52 +01:00
|
|
|
ret = x509parse_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 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-14 13:39:52 +01:00
|
|
|
ret = x509parse_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
|
|
|
|
*/
|
|
|
|
int x509parse_verify( x509_cert *crt,
|
|
|
|
x509_cert *trust_ca,
|
2009-05-03 12:18:48 +02:00
|
|
|
x509_crl *ca_crl,
|
2011-01-13 18:54:59 +01:00
|
|
|
const char *cn, int *flags,
|
2012-09-28 09:10:55 +02:00
|
|
|
int (*f_vrfy)(void *, x509_cert *, int, int *),
|
2011-01-13 18:54:59 +01: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;
|
2011-01-16 22:12:10 +01:00
|
|
|
x509_cert *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 &&
|
|
|
|
memcmp( 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 &&
|
|
|
|
memcmp( 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
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-09-28 09:10:55 +02:00
|
|
|
* Iterate upwards in the given cert chain, to find our crt parent.
|
|
|
|
* Ignore any upper cert with CA != TRUE.
|
2009-01-03 22:22:43 +01:00
|
|
|
*/
|
2011-01-16 22:12:10 +01:00
|
|
|
parent = crt->next;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2011-01-16 22:12:10 +01:00
|
|
|
while( parent != NULL && parent->version != 0 )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2011-01-16 22:12:10 +01:00
|
|
|
if( parent->ca_istrue == 0 ||
|
|
|
|
crt->issuer_raw.len != parent->subject_raw.len ||
|
|
|
|
memcmp( crt->issuer_raw.p, parent->subject_raw.p,
|
2009-01-03 22:22:43 +01:00
|
|
|
crt->issuer_raw.len ) != 0 )
|
|
|
|
{
|
2011-01-16 22:12:10 +01:00
|
|
|
parent = parent->next;
|
2009-01-03 22:22:43 +01:00
|
|
|
continue;
|
|
|
|
}
|
2012-09-28 09:10:55 +02:00
|
|
|
break;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2012-09-28 09:10:55 +02:00
|
|
|
if( parent != NULL )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2012-09-28 09:10:55 +02:00
|
|
|
/*
|
|
|
|
* Part of the chain
|
|
|
|
*/
|
2012-11-14 13:39:52 +01:00
|
|
|
ret = x509parse_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 );
|
|
|
|
}
|
|
|
|
else
|
2011-01-15 17:57:55 +01:00
|
|
|
{
|
2012-11-14 13:39:52 +01:00
|
|
|
ret = x509parse_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 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unallocate all certificate data
|
|
|
|
*/
|
|
|
|
void x509_free( x509_cert *crt )
|
|
|
|
{
|
|
|
|
x509_cert *cert_cur = crt;
|
|
|
|
x509_cert *cert_prv;
|
|
|
|
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
|
|
|
|
|
|
|
name_cur = cert_cur->issuer.next;
|
|
|
|
while( name_cur != NULL )
|
|
|
|
{
|
|
|
|
name_prv = name_cur;
|
|
|
|
name_cur = name_cur->next;
|
|
|
|
memset( name_prv, 0, 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;
|
|
|
|
memset( name_prv, 0, 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;
|
|
|
|
memset( seq_prv, 0, 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;
|
|
|
|
memset( seq_prv, 0, 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 )
|
|
|
|
{
|
|
|
|
memset( cert_cur->raw.p, 0, 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;
|
|
|
|
|
|
|
|
memset( cert_prv, 0, sizeof( x509_cert ) );
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
2009-05-02 17:13:40 +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
|
|
|
|
{
|
|
|
|
name_cur = crl_cur->issuer.next;
|
|
|
|
while( name_cur != NULL )
|
|
|
|
{
|
|
|
|
name_prv = name_cur;
|
|
|
|
name_cur = name_cur->next;
|
|
|
|
memset( name_prv, 0, sizeof( x509_name ) );
|
2013-07-03 13:37:05 +02:00
|
|
|
polarssl_free( name_prv );
|
2009-05-02 17:13:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
entry_cur = crl_cur->entry.next;
|
|
|
|
while( entry_cur != NULL )
|
|
|
|
{
|
|
|
|
entry_prv = entry_cur;
|
|
|
|
entry_cur = entry_cur->next;
|
|
|
|
memset( entry_prv, 0, sizeof( x509_crl_entry ) );
|
2013-07-03 13:37:05 +02:00
|
|
|
polarssl_free( entry_prv );
|
2009-05-02 17:13:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if( crl_cur->raw.p != NULL )
|
|
|
|
{
|
|
|
|
memset( crl_cur->raw.p, 0, crl_cur->raw.len );
|
2013-07-03 13:37:05 +02:00
|
|
|
polarssl_free( crl_cur->raw.p );
|
2009-05-02 17:13:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
crl_cur = crl_cur->next;
|
|
|
|
}
|
|
|
|
while( crl_cur != NULL );
|
|
|
|
|
|
|
|
crl_cur = crl;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
crl_prv = crl_cur;
|
|
|
|
crl_cur = crl_cur->next;
|
|
|
|
|
|
|
|
memset( crl_prv, 0, sizeof( x509_crl ) );
|
|
|
|
if( crl_prv != crl )
|
2013-07-03 13:37:05 +02:00
|
|
|
polarssl_free( crl_prv );
|
2009-05-02 17:13:40 +02:00
|
|
|
}
|
|
|
|
while( crl_cur != NULL );
|
|
|
|
}
|
|
|
|
|
2009-01-03 22:51:57 +01:00
|
|
|
#if defined(POLARSSL_SELF_TEST)
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2009-01-03 22:51:57 +01:00
|
|
|
#include "polarssl/certs.h"
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Checkup routine
|
|
|
|
*/
|
|
|
|
int x509_self_test( int verbose )
|
|
|
|
{
|
2011-05-26 15:16:06 +02:00
|
|
|
#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
|
2011-04-24 10:57:21 +02:00
|
|
|
int ret;
|
|
|
|
int flags;
|
|
|
|
size_t i, j;
|
2009-01-03 22:22:43 +01:00
|
|
|
x509_cert cacert;
|
|
|
|
x509_cert clicert;
|
|
|
|
rsa_context rsa;
|
2011-05-26 15:16:06 +02:00
|
|
|
#if defined(POLARSSL_DHM_C)
|
2011-01-06 16:48:19 +01:00
|
|
|
dhm_context dhm;
|
2011-05-26 15:16:06 +02:00
|
|
|
#endif
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
if( verbose != 0 )
|
|
|
|
printf( " X.509 certificate load: " );
|
|
|
|
|
|
|
|
memset( &clicert, 0, sizeof( x509_cert ) );
|
|
|
|
|
2013-06-24 19:03:14 +02:00
|
|
|
ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
|
2011-12-10 22:55:01 +01:00
|
|
|
strlen( test_cli_crt ) );
|
2009-01-03 22:22:43 +01:00
|
|
|
if( ret != 0 )
|
|
|
|
{
|
|
|
|
if( verbose != 0 )
|
|
|
|
printf( "failed\n" );
|
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
memset( &cacert, 0, sizeof( x509_cert ) );
|
|
|
|
|
2013-06-24 19:03:14 +02:00
|
|
|
ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
|
2011-12-10 22:55:01 +01:00
|
|
|
strlen( test_ca_crt ) );
|
2009-01-03 22:22:43 +01:00
|
|
|
if( ret != 0 )
|
|
|
|
{
|
|
|
|
if( verbose != 0 )
|
|
|
|
printf( "failed\n" );
|
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( verbose != 0 )
|
|
|
|
printf( "passed\n X.509 private key load: " );
|
|
|
|
|
|
|
|
i = strlen( test_ca_key );
|
|
|
|
j = strlen( test_ca_pwd );
|
|
|
|
|
2011-03-25 15:22:50 +01:00
|
|
|
rsa_init( &rsa, RSA_PKCS_V15, 0 );
|
|
|
|
|
2013-06-27 10:51:01 +02:00
|
|
|
if( ( ret = x509parse_key_rsa( &rsa,
|
2013-06-24 19:03:14 +02:00
|
|
|
(const unsigned char *) test_ca_key, i,
|
|
|
|
(const unsigned char *) test_ca_pwd, j ) ) != 0 )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
|
|
|
if( verbose != 0 )
|
|
|
|
printf( "failed\n" );
|
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( verbose != 0 )
|
|
|
|
printf( "passed\n X.509 signature verify: ");
|
|
|
|
|
2011-04-24 10:57:21 +02:00
|
|
|
ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
|
2009-01-03 22:22:43 +01:00
|
|
|
if( ret != 0 )
|
|
|
|
{
|
2011-04-24 10:57:21 +02:00
|
|
|
printf("%02x", flags);
|
2009-01-03 22:22:43 +01:00
|
|
|
if( verbose != 0 )
|
|
|
|
printf( "failed\n" );
|
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2011-05-26 15:16:06 +02:00
|
|
|
#if defined(POLARSSL_DHM_C)
|
2011-01-06 16:48:19 +01:00
|
|
|
if( verbose != 0 )
|
|
|
|
printf( "passed\n X.509 DHM parameter load: " );
|
|
|
|
|
|
|
|
i = strlen( test_dhm_params );
|
|
|
|
j = strlen( test_ca_pwd );
|
|
|
|
|
2013-06-24 19:03:14 +02:00
|
|
|
if( ( ret = x509parse_dhm( &dhm, (const unsigned char *) test_dhm_params, i ) ) != 0 )
|
2011-01-06 16:48:19 +01:00
|
|
|
{
|
|
|
|
if( verbose != 0 )
|
|
|
|
printf( "failed\n" );
|
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
if( verbose != 0 )
|
|
|
|
printf( "passed\n\n" );
|
2011-05-26 15:16:06 +02:00
|
|
|
#endif
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
x509_free( &cacert );
|
|
|
|
x509_free( &clicert );
|
|
|
|
rsa_free( &rsa );
|
2011-05-26 15:16:06 +02:00
|
|
|
#if defined(POLARSSL_DHM_C)
|
2011-01-06 16:48:19 +01:00
|
|
|
dhm_free( &dhm );
|
2011-05-26 15:16:06 +02:00
|
|
|
#endif
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
return( 0 );
|
2009-10-03 21:58:52 +02:00
|
|
|
#else
|
|
|
|
((void) verbose);
|
|
|
|
return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
|
|
|
|
#endif
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|