2013-09-16 13:49:26 +02:00
|
|
|
/*
|
|
|
|
* X.509 certificate and private key decoding
|
|
|
|
*
|
2014-02-01 22:50:26 +01:00
|
|
|
* Copyright (C) 2006-2014, Brainspark B.V.
|
2013-09-16 13:49:26 +02:00
|
|
|
*
|
|
|
|
* This file is part of PolarSSL (http://www.polarssl.org)
|
|
|
|
* Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
|
|
|
|
*
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* The ITU-T X.509 standard defines a certificate format for PKI.
|
|
|
|
*
|
|
|
|
* http://www.ietf.org/rfc/rfc3279.txt
|
|
|
|
* http://www.ietf.org/rfc/rfc3280.txt
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2014-04-29 12:39:06 +02:00
|
|
|
#if !defined(POLARSSL_CONFIG_FILE)
|
2013-09-16 13:49:26 +02:00
|
|
|
#include "polarssl/config.h"
|
2014-04-29 12:39:06 +02:00
|
|
|
#else
|
|
|
|
#include POLARSSL_CONFIG_FILE
|
|
|
|
#endif
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
#if defined(POLARSSL_X509_USE_C)
|
|
|
|
|
|
|
|
#include "polarssl/x509.h"
|
|
|
|
#include "polarssl/asn1.h"
|
|
|
|
#include "polarssl/oid.h"
|
|
|
|
#if defined(POLARSSL_PEM_PARSE_C)
|
|
|
|
#include "polarssl/pem.h"
|
|
|
|
#endif
|
|
|
|
|
2014-02-01 22:50:26 +01:00
|
|
|
#if defined(POLARSSL_PLATFORM_C)
|
|
|
|
#include "polarssl/platform.h"
|
2013-09-16 13:49:26 +02:00
|
|
|
#else
|
2014-02-01 22:50:26 +01:00
|
|
|
#define polarssl_printf printf
|
2013-09-16 13:49:26 +02:00
|
|
|
#define polarssl_malloc malloc
|
|
|
|
#define polarssl_free free
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2013-10-28 18:48:30 +01:00
|
|
|
#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
|
2013-09-16 13:49:26 +02:00
|
|
|
#include <windows.h>
|
|
|
|
#else
|
|
|
|
#include <time.h>
|
|
|
|
#endif
|
|
|
|
|
2013-10-28 18:48:30 +01:00
|
|
|
#if defined(EFIX64) || defined(EFI32)
|
|
|
|
#include <stdio.h>
|
|
|
|
#endif
|
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
#if defined(POLARSSL_FS_IO)
|
|
|
|
#include <stdio.h>
|
|
|
|
#if !defined(_WIN32)
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* CertificateSerialNumber ::= INTEGER
|
|
|
|
*/
|
|
|
|
int x509_get_serial( unsigned char **p, const unsigned char *end,
|
|
|
|
x509_buf *serial )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if( ( end - *p ) < 1 )
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_SERIAL +
|
2013-09-16 13:49:26 +02:00
|
|
|
POLARSSL_ERR_ASN1_OUT_OF_DATA );
|
|
|
|
|
|
|
|
if( **p != ( ASN1_CONTEXT_SPECIFIC | ASN1_PRIMITIVE | 2 ) &&
|
|
|
|
**p != ASN1_INTEGER )
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_SERIAL +
|
2013-09-16 13:49:26 +02:00
|
|
|
POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
|
|
|
|
|
|
|
|
serial->tag = *(*p)++;
|
|
|
|
|
|
|
|
if( ( ret = asn1_get_len( p, end, &serial->len ) ) != 0 )
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_SERIAL + ret );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
serial->p = *p;
|
|
|
|
*p += serial->len;
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get an algorithm identifier without parameters (eg for signatures)
|
|
|
|
*
|
|
|
|
* AlgorithmIdentifier ::= SEQUENCE {
|
|
|
|
* algorithm OBJECT IDENTIFIER,
|
|
|
|
* parameters ANY DEFINED BY algorithm OPTIONAL }
|
|
|
|
*/
|
|
|
|
int x509_get_alg_null( unsigned char **p, const unsigned char *end,
|
|
|
|
x509_buf *alg )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if( ( ret = asn1_get_alg_null( p, end, alg ) ) != 0 )
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_ALG + ret );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2014-01-22 10:12:57 +01:00
|
|
|
/*
|
|
|
|
* Parse an algorithm identifier with (optional) paramaters
|
|
|
|
*/
|
|
|
|
int x509_get_alg( unsigned char **p, const unsigned char *end,
|
|
|
|
x509_buf *alg, x509_buf *params )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if( ( ret = asn1_get_alg( p, end, alg, params ) ) != 0 )
|
|
|
|
return( POLARSSL_ERR_X509_INVALID_ALG + ret );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2014-01-24 14:37:29 +01:00
|
|
|
#if defined(POLARSSL_RSASSA_PSS_CERTIFICATES)
|
2014-01-23 19:15:29 +01:00
|
|
|
/*
|
|
|
|
* HashAlgorithm ::= AlgorithmIdentifier
|
|
|
|
*
|
|
|
|
* AlgorithmIdentifier ::= SEQUENCE {
|
|
|
|
* algorithm OBJECT IDENTIFIER,
|
|
|
|
* parameters ANY DEFINED BY algorithm OPTIONAL }
|
|
|
|
*
|
|
|
|
* For HashAlgorithm, parameters MUST be NULL or absent.
|
|
|
|
*/
|
|
|
|
static int x509_get_hash_alg( const x509_buf *alg, md_type_t *md_alg )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
unsigned char *p;
|
|
|
|
const unsigned char *end;
|
|
|
|
x509_buf md_oid;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
/* Make sure we got a SEQUENCE and setup bounds */
|
|
|
|
if( alg->tag != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
|
|
|
|
return( POLARSSL_ERR_X509_INVALID_ALG +
|
|
|
|
POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
|
|
|
|
|
|
|
|
p = (unsigned char *) alg->p;
|
|
|
|
end = p + alg->len;
|
|
|
|
|
|
|
|
if( p >= end )
|
|
|
|
return( POLARSSL_ERR_X509_INVALID_ALG +
|
|
|
|
POLARSSL_ERR_ASN1_OUT_OF_DATA );
|
|
|
|
|
|
|
|
/* Parse md_oid */
|
|
|
|
md_oid.tag = *p;
|
|
|
|
|
|
|
|
if( ( ret = asn1_get_tag( &p, end, &md_oid.len, ASN1_OID ) ) != 0 )
|
|
|
|
return( POLARSSL_ERR_X509_INVALID_ALG + ret );
|
|
|
|
|
|
|
|
md_oid.p = p;
|
|
|
|
p += md_oid.len;
|
|
|
|
|
|
|
|
/* Get md_alg from md_oid */
|
|
|
|
if( ( ret = oid_get_md_alg( &md_oid, md_alg ) ) != 0 )
|
|
|
|
return( POLARSSL_ERR_X509_INVALID_ALG + ret );
|
|
|
|
|
|
|
|
/* Make sure params is absent of NULL */
|
|
|
|
if( p == end )
|
|
|
|
return( 0 );
|
|
|
|
|
2014-01-24 14:15:20 +01:00
|
|
|
if( ( ret = asn1_get_tag( &p, end, &len, ASN1_NULL ) ) != 0 || len != 0 )
|
2014-01-23 19:15:29 +01:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_ALG + ret );
|
|
|
|
|
|
|
|
if( p != end )
|
|
|
|
return( POLARSSL_ERR_X509_INVALID_ALG +
|
|
|
|
POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2014-01-23 16:24:44 +01:00
|
|
|
/*
|
|
|
|
* RSASSA-PSS-params ::= SEQUENCE {
|
|
|
|
* hashAlgorithm [0] HashAlgorithm DEFAULT sha1Identifier,
|
|
|
|
* maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT mgf1SHA1Identifier,
|
|
|
|
* saltLength [2] INTEGER DEFAULT 20,
|
|
|
|
* trailerField [3] INTEGER DEFAULT 1 }
|
|
|
|
* -- Note that the tags in this Sequence are explicit.
|
|
|
|
*/
|
|
|
|
int x509_get_rsassa_pss_params( const x509_buf *params,
|
2014-01-23 19:15:29 +01:00
|
|
|
md_type_t *md_alg, md_type_t *mgf_md,
|
|
|
|
int *salt_len, int *trailer_field )
|
2014-01-23 16:24:44 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
unsigned char *p;
|
2014-01-24 14:15:20 +01:00
|
|
|
const unsigned char *end, *end2;
|
2014-01-23 16:24:44 +01:00
|
|
|
size_t len;
|
2014-01-23 19:15:29 +01:00
|
|
|
x509_buf alg_id, alg_params;
|
2014-01-23 16:24:44 +01:00
|
|
|
|
|
|
|
/* First set everything to defaults */
|
|
|
|
*md_alg = POLARSSL_MD_SHA1;
|
2014-01-23 19:15:29 +01:00
|
|
|
*mgf_md = POLARSSL_MD_SHA1;
|
2014-01-23 16:24:44 +01:00
|
|
|
*salt_len = 20;
|
|
|
|
*trailer_field = 1;
|
|
|
|
|
|
|
|
/* Make sure params is a SEQUENCE and setup bounds */
|
|
|
|
if( params->tag != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
|
|
|
|
return( POLARSSL_ERR_X509_INVALID_ALG +
|
|
|
|
POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
|
|
|
|
|
|
|
|
p = (unsigned char *) params->p;
|
|
|
|
end = p + params->len;
|
|
|
|
|
|
|
|
if( p == end )
|
|
|
|
return( 0 );
|
|
|
|
|
2014-01-24 14:15:20 +01:00
|
|
|
/*
|
|
|
|
* HashAlgorithm
|
|
|
|
*/
|
2014-01-23 16:24:44 +01:00
|
|
|
if( ( ret = asn1_get_tag( &p, end, &len,
|
|
|
|
ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) == 0 )
|
|
|
|
{
|
2014-01-24 14:15:20 +01:00
|
|
|
end2 = p + len;
|
|
|
|
|
2014-01-23 19:15:29 +01:00
|
|
|
/* HashAlgorithm ::= AlgorithmIdentifier (without parameters) */
|
2014-01-24 14:15:20 +01:00
|
|
|
if( ( ret = x509_get_alg_null( &p, end2, &alg_id ) ) != 0 )
|
2014-01-23 19:15:29 +01:00
|
|
|
return( ret );
|
|
|
|
|
|
|
|
if( ( ret = oid_get_md_alg( &alg_id, md_alg ) ) != 0 )
|
|
|
|
return( POLARSSL_ERR_X509_INVALID_ALG + ret );
|
2014-01-24 14:15:20 +01:00
|
|
|
|
|
|
|
if( p != end2 )
|
|
|
|
return( POLARSSL_ERR_X509_INVALID_ALG +
|
|
|
|
POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
|
2014-01-23 16:24:44 +01:00
|
|
|
}
|
|
|
|
else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
|
|
|
|
return( POLARSSL_ERR_X509_INVALID_ALG + ret );
|
|
|
|
|
2014-01-24 14:15:20 +01:00
|
|
|
if( p == end )
|
|
|
|
return( 0 );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* MaskGenAlgorithm
|
|
|
|
*/
|
2014-01-23 16:24:44 +01:00
|
|
|
if( ( ret = asn1_get_tag( &p, end, &len,
|
|
|
|
ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 1 ) ) == 0 )
|
|
|
|
{
|
2014-01-24 14:15:20 +01:00
|
|
|
end2 = p + len;
|
|
|
|
|
2014-01-23 19:15:29 +01:00
|
|
|
/* MaskGenAlgorithm ::= AlgorithmIdentifier (params = HashAlgorithm) */
|
2014-01-24 14:15:20 +01:00
|
|
|
if( ( ret = x509_get_alg( &p, end2, &alg_id, &alg_params ) ) != 0 )
|
2014-01-23 19:15:29 +01:00
|
|
|
return( ret );
|
|
|
|
|
|
|
|
/* Only MFG1 is recognised for now */
|
|
|
|
if( ! OID_CMP( OID_MGF1, &alg_id ) )
|
|
|
|
return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE +
|
|
|
|
POLARSSL_ERR_OID_NOT_FOUND );
|
|
|
|
|
|
|
|
/* Parse HashAlgorithm */
|
|
|
|
if( ( ret = x509_get_hash_alg( &alg_params, mgf_md ) ) != 0 )
|
|
|
|
return( ret );
|
2014-01-24 14:15:20 +01:00
|
|
|
|
|
|
|
if( p != end2 )
|
|
|
|
return( POLARSSL_ERR_X509_INVALID_ALG +
|
|
|
|
POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
|
2014-01-23 16:24:44 +01:00
|
|
|
}
|
|
|
|
else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
|
|
|
|
return( POLARSSL_ERR_X509_INVALID_ALG + ret );
|
|
|
|
|
|
|
|
if( p == end )
|
|
|
|
return( 0 );
|
|
|
|
|
2014-01-24 14:15:20 +01:00
|
|
|
/*
|
|
|
|
* salt_len
|
|
|
|
*/
|
2014-01-23 16:24:44 +01:00
|
|
|
if( ( ret = asn1_get_tag( &p, end, &len,
|
|
|
|
ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 2 ) ) == 0 )
|
|
|
|
{
|
2014-01-24 14:15:20 +01:00
|
|
|
end2 = p + len;
|
|
|
|
|
|
|
|
if( ( ret = asn1_get_int( &p, end2, salt_len ) ) != 0 )
|
2014-01-23 16:24:44 +01:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_ALG + ret );
|
2014-01-24 14:15:20 +01:00
|
|
|
|
|
|
|
if( p != end2 )
|
|
|
|
return( POLARSSL_ERR_X509_INVALID_ALG +
|
|
|
|
POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
|
2014-01-23 16:24:44 +01:00
|
|
|
}
|
|
|
|
else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
|
|
|
|
return( POLARSSL_ERR_X509_INVALID_ALG + ret );
|
|
|
|
|
|
|
|
if( p == end )
|
|
|
|
return( 0 );
|
|
|
|
|
2014-01-24 14:15:20 +01:00
|
|
|
/*
|
|
|
|
* trailer_field
|
|
|
|
*/
|
2014-01-23 16:24:44 +01:00
|
|
|
if( ( ret = asn1_get_tag( &p, end, &len,
|
|
|
|
ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 3 ) ) == 0 )
|
|
|
|
{
|
2014-01-24 14:15:20 +01:00
|
|
|
end2 = p + len;
|
|
|
|
|
|
|
|
if( ( ret = asn1_get_int( &p, end2, trailer_field ) ) != 0 )
|
2014-01-23 16:24:44 +01:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_ALG + ret );
|
2014-01-24 14:15:20 +01:00
|
|
|
|
|
|
|
if( p != end2 )
|
|
|
|
return( POLARSSL_ERR_X509_INVALID_ALG +
|
|
|
|
POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
|
2014-01-23 16:24:44 +01:00
|
|
|
}
|
|
|
|
else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
|
|
|
|
return( POLARSSL_ERR_X509_INVALID_ALG + ret );
|
|
|
|
|
|
|
|
if( p != end )
|
|
|
|
return( POLARSSL_ERR_X509_INVALID_ALG +
|
|
|
|
POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
2014-01-24 14:37:29 +01:00
|
|
|
#endif /* POLARSSL_RSASSA_PSS_CERTIFICATES */
|
2014-01-23 16:24:44 +01:00
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
/*
|
|
|
|
* AttributeTypeAndValue ::= SEQUENCE {
|
|
|
|
* type AttributeType,
|
|
|
|
* value AttributeValue }
|
|
|
|
*
|
|
|
|
* AttributeType ::= OBJECT IDENTIFIER
|
|
|
|
*
|
|
|
|
* AttributeValue ::= ANY DEFINED BY AttributeType
|
|
|
|
*/
|
|
|
|
static int x509_get_attr_type_value( unsigned char **p,
|
|
|
|
const unsigned char *end,
|
|
|
|
x509_name *cur )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t len;
|
|
|
|
x509_buf *oid;
|
|
|
|
x509_buf *val;
|
|
|
|
|
|
|
|
if( ( ret = asn1_get_tag( p, end, &len,
|
|
|
|
ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_NAME + ret );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
if( ( end - *p ) < 1 )
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_NAME +
|
2013-09-16 13:49:26 +02:00
|
|
|
POLARSSL_ERR_ASN1_OUT_OF_DATA );
|
|
|
|
|
|
|
|
oid = &cur->oid;
|
|
|
|
oid->tag = **p;
|
|
|
|
|
|
|
|
if( ( ret = asn1_get_tag( p, end, &oid->len, ASN1_OID ) ) != 0 )
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_NAME + ret );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
oid->p = *p;
|
|
|
|
*p += oid->len;
|
|
|
|
|
|
|
|
if( ( end - *p ) < 1 )
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_NAME +
|
2013-09-16 13:49:26 +02:00
|
|
|
POLARSSL_ERR_ASN1_OUT_OF_DATA );
|
|
|
|
|
|
|
|
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 )
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_NAME +
|
2013-09-16 13:49:26 +02:00
|
|
|
POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
|
|
|
|
|
|
|
|
val = &cur->val;
|
|
|
|
val->tag = *(*p)++;
|
|
|
|
|
|
|
|
if( ( ret = asn1_get_len( p, end, &val->len ) ) != 0 )
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_NAME + ret );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
val->p = *p;
|
|
|
|
*p += val->len;
|
|
|
|
|
|
|
|
cur->next = NULL;
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* RelativeDistinguishedName ::=
|
|
|
|
* SET OF AttributeTypeAndValue
|
|
|
|
*
|
|
|
|
* AttributeTypeAndValue ::= SEQUENCE {
|
|
|
|
* type AttributeType,
|
|
|
|
* value AttributeValue }
|
|
|
|
*
|
|
|
|
* AttributeType ::= OBJECT IDENTIFIER
|
|
|
|
*
|
|
|
|
* AttributeValue ::= ANY DEFINED BY AttributeType
|
|
|
|
*/
|
|
|
|
int x509_get_name( unsigned char **p, const unsigned char *end,
|
|
|
|
x509_name *cur )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t len;
|
|
|
|
const unsigned char *end2;
|
|
|
|
x509_name *use;
|
|
|
|
|
|
|
|
if( ( ret = asn1_get_tag( p, end, &len,
|
|
|
|
ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 )
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_NAME + ret );
|
2013-09-16 13:49:26 +02: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 )
|
|
|
|
{
|
|
|
|
use->next = (x509_name *) polarssl_malloc(
|
|
|
|
sizeof( x509_name ) );
|
|
|
|
|
|
|
|
if( use->next == NULL )
|
|
|
|
return( POLARSSL_ERR_X509_MALLOC_FAILED );
|
|
|
|
|
|
|
|
memset( use->next, 0, sizeof( x509_name ) );
|
|
|
|
|
|
|
|
use = use->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while( *p != end );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* recurse until end of SEQUENCE is reached
|
|
|
|
*/
|
|
|
|
if( *p == end2 )
|
|
|
|
return( 0 );
|
|
|
|
|
|
|
|
cur->next = (x509_name *) polarssl_malloc(
|
|
|
|
sizeof( x509_name ) );
|
|
|
|
|
|
|
|
if( cur->next == NULL )
|
|
|
|
return( POLARSSL_ERR_X509_MALLOC_FAILED );
|
|
|
|
|
|
|
|
memset( cur->next, 0, sizeof( x509_name ) );
|
|
|
|
|
|
|
|
return( x509_get_name( p, end2, cur->next ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Time ::= CHOICE {
|
|
|
|
* utcTime UTCTime,
|
|
|
|
* generalTime GeneralizedTime }
|
|
|
|
*/
|
|
|
|
int x509_get_time( unsigned char **p, const unsigned char *end,
|
|
|
|
x509_time *time )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t len;
|
|
|
|
char date[64];
|
|
|
|
unsigned char tag;
|
|
|
|
|
|
|
|
if( ( end - *p ) < 1 )
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_DATE +
|
2013-09-16 13:49:26 +02:00
|
|
|
POLARSSL_ERR_ASN1_OUT_OF_DATA );
|
|
|
|
|
|
|
|
tag = **p;
|
|
|
|
|
|
|
|
if ( tag == ASN1_UTC_TIME )
|
|
|
|
{
|
|
|
|
(*p)++;
|
|
|
|
ret = asn1_get_len( p, end, &len );
|
2013-09-17 14:36:05 +02:00
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
if( ret != 0 )
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_DATE + ret );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
memset( date, 0, sizeof( date ) );
|
|
|
|
memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
|
|
|
|
len : sizeof( date ) - 1 );
|
|
|
|
|
2014-04-11 12:29:49 +02:00
|
|
|
if( sscanf( date, "%2d%2d%2d%2d%2d%2dZ",
|
2013-09-16 13:49:26 +02:00
|
|
|
&time->year, &time->mon, &time->day,
|
|
|
|
&time->hour, &time->min, &time->sec ) < 5 )
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_DATE );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
time->year += 100 * ( time->year < 50 );
|
|
|
|
time->year += 1900;
|
|
|
|
|
|
|
|
*p += len;
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
else if ( tag == ASN1_GENERALIZED_TIME )
|
|
|
|
{
|
|
|
|
(*p)++;
|
|
|
|
ret = asn1_get_len( p, end, &len );
|
2013-09-17 14:36:05 +02:00
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
if( ret != 0 )
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_DATE + ret );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
memset( date, 0, sizeof( date ) );
|
|
|
|
memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
|
|
|
|
len : sizeof( date ) - 1 );
|
|
|
|
|
2014-04-11 12:29:49 +02:00
|
|
|
if( sscanf( date, "%4d%2d%2d%2d%2d%2dZ",
|
2013-09-16 13:49:26 +02:00
|
|
|
&time->year, &time->mon, &time->day,
|
|
|
|
&time->hour, &time->min, &time->sec ) < 5 )
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_DATE );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
*p += len;
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
else
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_DATE +
|
|
|
|
POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int x509_get_sig( unsigned char **p, const unsigned char *end, x509_buf *sig )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
if( ( end - *p ) < 1 )
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_SIGNATURE +
|
2013-09-16 13:49:26 +02:00
|
|
|
POLARSSL_ERR_ASN1_OUT_OF_DATA );
|
|
|
|
|
|
|
|
sig->tag = **p;
|
|
|
|
|
|
|
|
if( ( ret = asn1_get_bitstring_null( p, end, &len ) ) != 0 )
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_SIGNATURE + ret );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
sig->len = len;
|
|
|
|
sig->p = *p;
|
|
|
|
|
|
|
|
*p += len;
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2014-01-24 19:28:43 +01:00
|
|
|
/*
|
|
|
|
* Get signature algorithm from alg OID and optional parameters
|
|
|
|
*/
|
|
|
|
int x509_get_sig_alg( const x509_buf *sig_oid, const x509_buf *sig_params,
|
|
|
|
md_type_t *md_alg, pk_type_t *pk_alg )
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
2014-01-24 19:28:43 +01:00
|
|
|
int ret;
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2014-01-24 19:28:43 +01:00
|
|
|
if( ( ret = oid_get_sig_alg( sig_oid, md_alg, pk_alg ) ) != 0 )
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_UNKNOWN_SIG_ALG + ret );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2014-01-24 19:28:43 +01:00
|
|
|
#if defined(POLARSSL_RSASSA_PSS_CERTIFICATES)
|
|
|
|
if( *pk_alg == POLARSSL_PK_RSASSA_PSS )
|
|
|
|
{
|
|
|
|
int salt_len, trailer_field;
|
|
|
|
md_type_t mgf_md;
|
|
|
|
|
|
|
|
/* Make sure params are valid */
|
|
|
|
ret = x509_get_rsassa_pss_params( sig_params,
|
|
|
|
md_alg, &mgf_md, &salt_len, &trailer_field );
|
|
|
|
if( ret != 0 )
|
|
|
|
return( ret );
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* Make sure parameters are absent or NULL */
|
|
|
|
if( ( sig_params->tag != ASN1_NULL && sig_params->tag != 0 ) ||
|
|
|
|
sig_params->len != 0 )
|
|
|
|
return( POLARSSL_ERR_X509_INVALID_ALG );
|
|
|
|
}
|
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* X.509 Extensions (No parsing of extensions, pointer should
|
|
|
|
* be either manually updated or extensions should be parsed!
|
|
|
|
*/
|
|
|
|
int x509_get_ext( unsigned char **p, const unsigned char *end,
|
|
|
|
x509_buf *ext, int tag )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
if( *p == end )
|
|
|
|
return( 0 );
|
|
|
|
|
|
|
|
ext->tag = **p;
|
|
|
|
|
|
|
|
if( ( ret = asn1_get_tag( p, end, &ext->len,
|
|
|
|
ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
|
|
|
|
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 )
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
if( end != *p + len )
|
2013-09-17 14:36:05 +02:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
|
2013-09-16 13:49:26 +02:00
|
|
|
POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(POLARSSL_FS_IO)
|
|
|
|
/*
|
|
|
|
* Load all data from a file into a given buffer.
|
|
|
|
*/
|
|
|
|
int x509_load_file( const char *path, unsigned char **buf, size_t *n )
|
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
long size;
|
|
|
|
|
|
|
|
if( ( f = fopen( path, "rb" ) ) == NULL )
|
|
|
|
return( POLARSSL_ERR_X509_FILE_IO_ERROR );
|
|
|
|
|
|
|
|
fseek( f, 0, SEEK_END );
|
|
|
|
if( ( size = ftell( f ) ) == -1 )
|
|
|
|
{
|
|
|
|
fclose( f );
|
|
|
|
return( POLARSSL_ERR_X509_FILE_IO_ERROR );
|
|
|
|
}
|
|
|
|
fseek( f, 0, SEEK_SET );
|
|
|
|
|
|
|
|
*n = (size_t) size;
|
|
|
|
|
|
|
|
if( *n + 1 == 0 ||
|
|
|
|
( *buf = (unsigned char *) polarssl_malloc( *n + 1 ) ) == NULL )
|
|
|
|
{
|
|
|
|
fclose( f );
|
|
|
|
return( POLARSSL_ERR_X509_MALLOC_FAILED );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( fread( *buf, 1, *n, f ) != *n )
|
|
|
|
{
|
|
|
|
fclose( f );
|
|
|
|
polarssl_free( *buf );
|
|
|
|
return( POLARSSL_ERR_X509_FILE_IO_ERROR );
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose( f );
|
|
|
|
|
|
|
|
(*buf)[*n] = '\0';
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
#endif /* POLARSSL_FS_IO */
|
|
|
|
|
2013-10-29 15:22:54 +01:00
|
|
|
#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
|
|
|
|
!defined(EFI32)
|
2013-09-16 13:49:26 +02:00
|
|
|
#include <stdarg.h>
|
|
|
|
|
|
|
|
#if !defined vsnprintf
|
|
|
|
#define vsnprintf _vsnprintf
|
|
|
|
#endif // vsnprintf
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Windows _snprintf and _vsnprintf are not compatible to linux versions.
|
|
|
|
* Result value is not size of buffer needed, but -1 if no fit is possible.
|
|
|
|
*
|
|
|
|
* This fuction tries to 'fix' this by at least suggesting enlarging the
|
|
|
|
* size by 20.
|
|
|
|
*/
|
|
|
|
static int compat_snprintf(char *str, size_t size, const char *format, ...)
|
|
|
|
{
|
|
|
|
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 )
|
|
|
|
return( (int) size + 20 );
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define snprintf compat_snprintf
|
2014-05-01 13:03:14 +02:00
|
|
|
#endif /* _MSC_VER && !snprintf && !EFIX64 && !EFI32 */
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
|
|
|
|
|
|
|
|
#define SAFE_SNPRINTF() \
|
|
|
|
{ \
|
|
|
|
if( ret == -1 ) \
|
|
|
|
return( -1 ); \
|
|
|
|
\
|
|
|
|
if ( (unsigned int) ret > n ) { \
|
|
|
|
p[n - 1] = '\0'; \
|
|
|
|
return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
n -= (unsigned int) ret; \
|
|
|
|
p += (unsigned int) ret; \
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Store the name in printable form into buf; no more
|
|
|
|
* than size characters will be written
|
|
|
|
*/
|
2013-09-18 11:11:02 +02:00
|
|
|
int x509_dn_gets( char *buf, size_t size, const x509_name *dn )
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t i, n;
|
|
|
|
unsigned char c;
|
|
|
|
const x509_name *name;
|
|
|
|
const char *short_name = NULL;
|
|
|
|
char s[128], *p;
|
|
|
|
|
|
|
|
memset( s, 0, sizeof( s ) );
|
|
|
|
|
|
|
|
name = dn;
|
|
|
|
p = buf;
|
|
|
|
n = size;
|
|
|
|
|
|
|
|
while( name != NULL )
|
|
|
|
{
|
|
|
|
if( !name->oid.p )
|
|
|
|
{
|
|
|
|
name = name->next;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( name != dn )
|
|
|
|
{
|
|
|
|
ret = snprintf( p, n, ", " );
|
|
|
|
SAFE_SNPRINTF();
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = oid_get_attr_short_name( &name->oid, &short_name );
|
|
|
|
|
|
|
|
if( ret == 0 )
|
|
|
|
ret = snprintf( p, n, "%s=", short_name );
|
|
|
|
else
|
|
|
|
ret = snprintf( p, n, "\?\?=" );
|
|
|
|
SAFE_SNPRINTF();
|
|
|
|
|
|
|
|
for( i = 0; i < name->val.len; i++ )
|
|
|
|
{
|
|
|
|
if( i >= sizeof( s ) - 1 )
|
|
|
|
break;
|
|
|
|
|
|
|
|
c = name->val.p[i];
|
|
|
|
if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
|
|
|
|
s[i] = '?';
|
|
|
|
else s[i] = c;
|
|
|
|
}
|
|
|
|
s[i] = '\0';
|
|
|
|
ret = snprintf( p, n, "%s", s );
|
|
|
|
SAFE_SNPRINTF();
|
|
|
|
name = name->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return( (int) ( size - n ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Store the serial in printable form into buf; no more
|
|
|
|
* than size characters will be written
|
|
|
|
*/
|
2013-09-18 11:11:02 +02:00
|
|
|
int x509_serial_gets( char *buf, size_t size, const x509_buf *serial )
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t i, n, nr;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
p = buf;
|
|
|
|
n = size;
|
|
|
|
|
|
|
|
nr = ( serial->len <= 32 )
|
|
|
|
? serial->len : 28;
|
|
|
|
|
|
|
|
for( i = 0; i < nr; i++ )
|
|
|
|
{
|
|
|
|
if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ret = snprintf( p, n, "%02X%s",
|
|
|
|
serial->p[i], ( i < nr - 1 ) ? ":" : "" );
|
|
|
|
SAFE_SNPRINTF();
|
|
|
|
}
|
|
|
|
|
|
|
|
if( nr != serial->len )
|
|
|
|
{
|
|
|
|
ret = snprintf( p, n, "...." );
|
|
|
|
SAFE_SNPRINTF();
|
|
|
|
}
|
|
|
|
|
|
|
|
return( (int) ( size - n ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Helper for writing "RSA key size", "EC key size", etc
|
|
|
|
*/
|
|
|
|
int x509_key_size_helper( char *buf, size_t size, const char *name )
|
|
|
|
{
|
|
|
|
char *p = buf;
|
|
|
|
size_t n = size;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if( strlen( name ) + sizeof( " key size" ) > size )
|
|
|
|
return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;
|
|
|
|
|
|
|
|
ret = snprintf( p, n, "%s key size", name );
|
|
|
|
SAFE_SNPRINTF();
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return an informational string describing the given OID
|
|
|
|
*/
|
|
|
|
const char *x509_oid_get_description( x509_buf *oid )
|
|
|
|
{
|
|
|
|
const char *desc = NULL;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = oid_get_extended_key_usage( oid, &desc );
|
|
|
|
|
|
|
|
if( ret != 0 )
|
|
|
|
return( NULL );
|
|
|
|
|
|
|
|
return( desc );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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 )
|
|
|
|
{
|
|
|
|
return oid_get_numeric_string( buf, size, oid );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return 0 if the x509_time is still valid, or 1 otherwise.
|
|
|
|
*/
|
|
|
|
#if defined(POLARSSL_HAVE_TIME)
|
|
|
|
|
2014-03-10 12:26:11 +01:00
|
|
|
static void x509_get_current_time( x509_time *now )
|
|
|
|
{
|
2013-10-28 18:48:30 +01:00
|
|
|
#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
|
2013-09-16 13:49:26 +02:00
|
|
|
SYSTEMTIME st;
|
|
|
|
|
2014-04-11 12:25:45 +02:00
|
|
|
GetSystemTime(&st);
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2014-03-10 12:26:11 +01:00
|
|
|
now->year = st.wYear;
|
|
|
|
now->mon = st.wMonth;
|
|
|
|
now->day = st.wDay;
|
|
|
|
now->hour = st.wHour;
|
|
|
|
now->min = st.wMinute;
|
|
|
|
now->sec = st.wSecond;
|
2013-09-16 13:49:26 +02:00
|
|
|
#else
|
2014-03-26 15:34:54 +01:00
|
|
|
struct tm lt;
|
2013-09-16 13:49:26 +02:00
|
|
|
time_t tt;
|
|
|
|
|
|
|
|
tt = time( NULL );
|
2014-04-11 12:25:45 +02:00
|
|
|
gmtime_r( &tt, < );
|
2014-03-26 15:34:54 +01:00
|
|
|
|
|
|
|
now->year = lt.tm_year + 1900;
|
|
|
|
now->mon = lt.tm_mon + 1;
|
|
|
|
now->day = lt.tm_mday;
|
|
|
|
now->hour = lt.tm_hour;
|
|
|
|
now->min = lt.tm_min;
|
|
|
|
now->sec = lt.tm_sec;
|
2014-05-01 13:03:14 +02:00
|
|
|
#endif /* _WIN32 && !EFIX64 && !EFI32 */
|
2014-03-10 12:26:11 +01:00
|
|
|
}
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2014-03-10 12:26:11 +01:00
|
|
|
/*
|
|
|
|
* Return 0 if before <= after, 1 otherwise
|
|
|
|
*/
|
|
|
|
static int x509_check_time( const x509_time *before, const x509_time *after )
|
|
|
|
{
|
|
|
|
if( before->year > after->year )
|
2013-09-16 13:49:26 +02:00
|
|
|
return( 1 );
|
|
|
|
|
2014-03-10 12:26:11 +01:00
|
|
|
if( before->year == after->year &&
|
|
|
|
before->mon > after->mon )
|
2013-09-16 13:49:26 +02:00
|
|
|
return( 1 );
|
|
|
|
|
2014-03-10 12:26:11 +01:00
|
|
|
if( before->year == after->year &&
|
|
|
|
before->mon == after->mon &&
|
|
|
|
before->day > after->day )
|
2013-09-16 13:49:26 +02:00
|
|
|
return( 1 );
|
|
|
|
|
2014-03-10 12:26:11 +01:00
|
|
|
if( before->year == after->year &&
|
|
|
|
before->mon == after->mon &&
|
|
|
|
before->day == after->day &&
|
|
|
|
before->hour > after->hour )
|
2013-09-16 13:49:26 +02:00
|
|
|
return( 1 );
|
|
|
|
|
2014-03-10 12:26:11 +01:00
|
|
|
if( before->year == after->year &&
|
|
|
|
before->mon == after->mon &&
|
|
|
|
before->day == after->day &&
|
|
|
|
before->hour == after->hour &&
|
|
|
|
before->min > after->min )
|
2013-09-16 13:49:26 +02:00
|
|
|
return( 1 );
|
|
|
|
|
2014-03-10 12:26:11 +01:00
|
|
|
if( before->year == after->year &&
|
|
|
|
before->mon == after->mon &&
|
|
|
|
before->day == after->day &&
|
|
|
|
before->hour == after->hour &&
|
|
|
|
before->min == after->min &&
|
|
|
|
before->sec > after->sec )
|
2013-09-16 13:49:26 +02:00
|
|
|
return( 1 );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
2014-03-10 12:26:11 +01:00
|
|
|
|
|
|
|
int x509_time_expired( const x509_time *to )
|
|
|
|
{
|
|
|
|
x509_time now;
|
|
|
|
|
|
|
|
x509_get_current_time( &now );
|
|
|
|
|
|
|
|
return( x509_check_time( &now, to ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
int x509_time_future( const x509_time *from )
|
|
|
|
{
|
|
|
|
x509_time now;
|
|
|
|
|
|
|
|
x509_get_current_time( &now );
|
|
|
|
|
|
|
|
return( x509_check_time( from, &now ) );
|
|
|
|
}
|
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
#else /* POLARSSL_HAVE_TIME */
|
2014-03-10 12:26:11 +01:00
|
|
|
|
2013-09-18 11:11:02 +02:00
|
|
|
int x509_time_expired( const x509_time *to )
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
|
|
|
((void) to);
|
|
|
|
return( 0 );
|
|
|
|
}
|
2014-03-10 12:26:11 +01:00
|
|
|
|
|
|
|
int x509_time_future( const x509_time *from )
|
|
|
|
{
|
|
|
|
((void) from);
|
|
|
|
return( 0 );
|
|
|
|
}
|
2013-09-16 13:49:26 +02:00
|
|
|
#endif /* POLARSSL_HAVE_TIME */
|
|
|
|
|
2013-09-16 22:53:25 +02:00
|
|
|
#if defined(POLARSSL_SELF_TEST)
|
|
|
|
|
|
|
|
#include "polarssl/x509_crt.h"
|
|
|
|
#include "polarssl/certs.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Checkup routine
|
|
|
|
*/
|
|
|
|
int x509_self_test( int verbose )
|
|
|
|
{
|
2014-04-29 15:29:41 +02:00
|
|
|
#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_SHA1_C)
|
2013-09-16 22:53:25 +02:00
|
|
|
int ret;
|
|
|
|
int flags;
|
2013-09-18 14:13:26 +02:00
|
|
|
x509_crt cacert;
|
|
|
|
x509_crt clicert;
|
2013-09-16 22:53:25 +02:00
|
|
|
|
|
|
|
if( verbose != 0 )
|
2014-02-01 22:50:26 +01:00
|
|
|
polarssl_printf( " X.509 certificate load: " );
|
2013-09-16 22:53:25 +02:00
|
|
|
|
2013-09-18 14:17:41 +02:00
|
|
|
x509_crt_init( &clicert );
|
2013-09-16 22:53:25 +02:00
|
|
|
|
2013-09-18 13:46:23 +02:00
|
|
|
ret = x509_crt_parse( &clicert, (const unsigned char *) test_cli_crt,
|
|
|
|
strlen( test_cli_crt ) );
|
2013-09-16 22:53:25 +02:00
|
|
|
if( ret != 0 )
|
|
|
|
{
|
|
|
|
if( verbose != 0 )
|
2014-02-01 22:50:26 +01:00
|
|
|
polarssl_printf( "failed\n" );
|
2013-09-16 22:53:25 +02:00
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2013-09-18 14:17:41 +02:00
|
|
|
x509_crt_init( &cacert );
|
2013-09-16 22:53:25 +02:00
|
|
|
|
2013-09-18 13:46:23 +02:00
|
|
|
ret = x509_crt_parse( &cacert, (const unsigned char *) test_ca_crt,
|
|
|
|
strlen( test_ca_crt ) );
|
2013-09-16 22:53:25 +02:00
|
|
|
if( ret != 0 )
|
|
|
|
{
|
|
|
|
if( verbose != 0 )
|
2014-02-01 22:50:26 +01:00
|
|
|
polarssl_printf( "failed\n" );
|
2013-09-16 22:53:25 +02:00
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( verbose != 0 )
|
2014-02-01 22:50:26 +01:00
|
|
|
polarssl_printf( "passed\n X.509 signature verify: ");
|
2013-09-16 22:53:25 +02:00
|
|
|
|
2013-09-18 13:46:23 +02:00
|
|
|
ret = x509_crt_verify( &clicert, &cacert, NULL, NULL, &flags, NULL, NULL );
|
2013-09-16 22:53:25 +02:00
|
|
|
if( ret != 0 )
|
|
|
|
{
|
|
|
|
if( verbose != 0 )
|
2014-02-01 22:50:26 +01:00
|
|
|
polarssl_printf( "failed\n" );
|
2013-09-16 22:53:25 +02:00
|
|
|
|
2014-02-01 22:50:26 +01:00
|
|
|
polarssl_printf("ret = %d, &flags = %04x\n", ret, flags);
|
2013-09-16 22:53:25 +02:00
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( verbose != 0 )
|
2014-02-01 22:50:26 +01:00
|
|
|
polarssl_printf( "passed\n\n");
|
2013-09-16 22:53:25 +02:00
|
|
|
|
|
|
|
x509_crt_free( &cacert );
|
|
|
|
x509_crt_free( &clicert );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
#else
|
|
|
|
((void) verbose);
|
|
|
|
return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
|
2014-05-01 13:03:14 +02:00
|
|
|
#endif /* POLARSSL_CERTS_C && POLARSSL_SHA1_C */
|
2013-09-16 22:53:25 +02:00
|
|
|
}
|
|
|
|
|
2014-05-01 13:03:14 +02:00
|
|
|
#endif /* POLARSSL_SELF_TEST */
|
2013-09-16 22:53:25 +02:00
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
#endif /* POLARSSL_X509_USE_C */
|