2013-09-16 11:49:26 +00:00
|
|
|
/*
|
2014-06-12 20:34:55 +00:00
|
|
|
* X.509 common functions for parsing and verification
|
2013-09-16 11:49:26 +00:00
|
|
|
*
|
2015-01-23 09:45:19 +00:00
|
|
|
* Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
|
2013-09-16 11:49:26 +00:00
|
|
|
*
|
2015-03-06 13:17:10 +00:00
|
|
|
* This file is part of mbed TLS (https://tls.mbed.org)
|
2013-09-16 11:49:26 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* The ITU-T X.509 standard defines a certificate format for PKI.
|
|
|
|
*
|
2014-06-12 20:34:55 +00:00
|
|
|
* http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
|
|
|
|
* http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
|
|
|
|
* http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
|
2013-09-16 11:49:26 +00:00
|
|
|
*
|
|
|
|
* http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
|
|
|
|
* http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
|
|
|
|
*/
|
|
|
|
|
2014-04-29 10:39:06 +00:00
|
|
|
#if !defined(POLARSSL_CONFIG_FILE)
|
2013-09-16 11:49:26 +00:00
|
|
|
#include "polarssl/config.h"
|
2014-04-29 10:39:06 +00:00
|
|
|
#else
|
|
|
|
#include POLARSSL_CONFIG_FILE
|
|
|
|
#endif
|
2013-09-16 11:49:26 +00:00
|
|
|
|
|
|
|
#if defined(POLARSSL_X509_USE_C)
|
|
|
|
|
|
|
|
#include "polarssl/x509.h"
|
|
|
|
#include "polarssl/asn1.h"
|
|
|
|
#include "polarssl/oid.h"
|
2015-02-06 13:43:58 +00:00
|
|
|
|
2015-02-12 18:27:14 +00:00
|
|
|
#include <stdio.h>
|
2015-02-06 13:43:58 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2013-09-16 11:49:26 +00:00
|
|
|
#if defined(POLARSSL_PEM_PARSE_C)
|
|
|
|
#include "polarssl/pem.h"
|
|
|
|
#endif
|
|
|
|
|
2014-02-01 21:50:26 +00:00
|
|
|
#if defined(POLARSSL_PLATFORM_C)
|
|
|
|
#include "polarssl/platform.h"
|
2013-09-16 11:49:26 +00:00
|
|
|
#else
|
2015-02-06 13:43:58 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2013-09-16 11:49:26 +00:00
|
|
|
#define polarssl_free free
|
2015-01-30 11:00:01 +00:00
|
|
|
#define polarssl_malloc malloc
|
|
|
|
#define polarssl_printf printf
|
|
|
|
#define polarssl_snprintf snprintf
|
2013-09-16 11:49:26 +00:00
|
|
|
#endif
|
|
|
|
|
2013-10-28 17:48:30 +00:00
|
|
|
#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
|
2013-09-16 11:49:26 +00:00
|
|
|
#include <windows.h>
|
|
|
|
#else
|
|
|
|
#include <time.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(POLARSSL_FS_IO)
|
2015-02-12 18:27:14 +00:00
|
|
|
#include <stdio.h>
|
2013-09-16 11:49:26 +00:00
|
|
|
#if !defined(_WIN32)
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2015-02-13 11:48:02 +00:00
|
|
|
#define CHECK(code) if( ( ret = code ) != 0 ){ return( ret ); }
|
2016-09-23 12:16:02 +00:00
|
|
|
#define CHECK_RANGE(min, max, val) if( val < min || val > max ){ return( ret ); }
|
2015-02-13 11:48:02 +00:00
|
|
|
|
2013-09-16 11:49:26 +00:00
|
|
|
/*
|
|
|
|
* 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 12:36:05 +00:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_SERIAL +
|
2013-09-16 11:49:26 +00:00
|
|
|
POLARSSL_ERR_ASN1_OUT_OF_DATA );
|
|
|
|
|
|
|
|
if( **p != ( ASN1_CONTEXT_SPECIFIC | ASN1_PRIMITIVE | 2 ) &&
|
|
|
|
**p != ASN1_INTEGER )
|
2013-09-17 12:36:05 +00:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_SERIAL +
|
2013-09-16 11:49:26 +00:00
|
|
|
POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
|
|
|
|
|
|
|
|
serial->tag = *(*p)++;
|
|
|
|
|
|
|
|
if( ( ret = asn1_get_len( p, end, &serial->len ) ) != 0 )
|
2013-09-17 12:36:05 +00:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_SERIAL + ret );
|
2013-09-16 11:49:26 +00: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 12:36:05 +00:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_ALG + ret );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2014-01-22 09:12:57 +00: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-06-06 14:42:37 +00:00
|
|
|
#if defined(POLARSSL_X509_RSASSA_PSS_SUPPORT)
|
2014-01-23 18:15:29 +00: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 13:15:20 +00:00
|
|
|
if( ( ret = asn1_get_tag( &p, end, &len, ASN1_NULL ) ) != 0 || len != 0 )
|
2014-01-23 18:15:29 +00: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 15:24:44 +00: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.
|
2014-05-31 15:08:16 +00:00
|
|
|
*
|
|
|
|
* RFC 4055 (which defines use of RSASSA-PSS in PKIX) states that the value
|
|
|
|
* of trailerField MUST be 1, and PKCS#1 v2.2 doesn't even define any other
|
|
|
|
* option. Enfore this at parsing time.
|
2014-01-23 15:24:44 +00:00
|
|
|
*/
|
|
|
|
int x509_get_rsassa_pss_params( const x509_buf *params,
|
2014-01-23 18:15:29 +00:00
|
|
|
md_type_t *md_alg, md_type_t *mgf_md,
|
2014-05-31 15:08:16 +00:00
|
|
|
int *salt_len )
|
2014-01-23 15:24:44 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
unsigned char *p;
|
2014-01-24 13:15:20 +00:00
|
|
|
const unsigned char *end, *end2;
|
2014-01-23 15:24:44 +00:00
|
|
|
size_t len;
|
2014-01-23 18:15:29 +00:00
|
|
|
x509_buf alg_id, alg_params;
|
2014-01-23 15:24:44 +00:00
|
|
|
|
|
|
|
/* First set everything to defaults */
|
|
|
|
*md_alg = POLARSSL_MD_SHA1;
|
2014-01-23 18:15:29 +00:00
|
|
|
*mgf_md = POLARSSL_MD_SHA1;
|
2014-01-23 15:24:44 +00:00
|
|
|
*salt_len = 20;
|
|
|
|
|
|
|
|
/* 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 13:15:20 +00:00
|
|
|
/*
|
|
|
|
* HashAlgorithm
|
|
|
|
*/
|
2014-01-23 15:24:44 +00:00
|
|
|
if( ( ret = asn1_get_tag( &p, end, &len,
|
|
|
|
ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) == 0 )
|
|
|
|
{
|
2014-01-24 13:15:20 +00:00
|
|
|
end2 = p + len;
|
|
|
|
|
2014-01-23 18:15:29 +00:00
|
|
|
/* HashAlgorithm ::= AlgorithmIdentifier (without parameters) */
|
2014-01-24 13:15:20 +00:00
|
|
|
if( ( ret = x509_get_alg_null( &p, end2, &alg_id ) ) != 0 )
|
2014-01-23 18:15:29 +00:00
|
|
|
return( ret );
|
|
|
|
|
|
|
|
if( ( ret = oid_get_md_alg( &alg_id, md_alg ) ) != 0 )
|
|
|
|
return( POLARSSL_ERR_X509_INVALID_ALG + ret );
|
2014-01-24 13:15:20 +00:00
|
|
|
|
|
|
|
if( p != end2 )
|
|
|
|
return( POLARSSL_ERR_X509_INVALID_ALG +
|
|
|
|
POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
|
2014-01-23 15:24:44 +00:00
|
|
|
}
|
|
|
|
else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
|
|
|
|
return( POLARSSL_ERR_X509_INVALID_ALG + ret );
|
|
|
|
|
2014-01-24 13:15:20 +00:00
|
|
|
if( p == end )
|
|
|
|
return( 0 );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* MaskGenAlgorithm
|
|
|
|
*/
|
2014-01-23 15:24:44 +00:00
|
|
|
if( ( ret = asn1_get_tag( &p, end, &len,
|
|
|
|
ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 1 ) ) == 0 )
|
|
|
|
{
|
2014-01-24 13:15:20 +00:00
|
|
|
end2 = p + len;
|
|
|
|
|
2014-01-23 18:15:29 +00:00
|
|
|
/* MaskGenAlgorithm ::= AlgorithmIdentifier (params = HashAlgorithm) */
|
2014-01-24 13:15:20 +00:00
|
|
|
if( ( ret = x509_get_alg( &p, end2, &alg_id, &alg_params ) ) != 0 )
|
2014-01-23 18:15:29 +00: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 13:15:20 +00:00
|
|
|
|
|
|
|
if( p != end2 )
|
|
|
|
return( POLARSSL_ERR_X509_INVALID_ALG +
|
|
|
|
POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
|
2014-01-23 15:24:44 +00:00
|
|
|
}
|
|
|
|
else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
|
|
|
|
return( POLARSSL_ERR_X509_INVALID_ALG + ret );
|
|
|
|
|
|
|
|
if( p == end )
|
|
|
|
return( 0 );
|
|
|
|
|
2014-01-24 13:15:20 +00:00
|
|
|
/*
|
|
|
|
* salt_len
|
|
|
|
*/
|
2014-01-23 15:24:44 +00:00
|
|
|
if( ( ret = asn1_get_tag( &p, end, &len,
|
|
|
|
ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 2 ) ) == 0 )
|
|
|
|
{
|
2014-01-24 13:15:20 +00:00
|
|
|
end2 = p + len;
|
|
|
|
|
|
|
|
if( ( ret = asn1_get_int( &p, end2, salt_len ) ) != 0 )
|
2014-01-23 15:24:44 +00:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_ALG + ret );
|
2014-01-24 13:15:20 +00:00
|
|
|
|
|
|
|
if( p != end2 )
|
|
|
|
return( POLARSSL_ERR_X509_INVALID_ALG +
|
|
|
|
POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
|
2014-01-23 15:24:44 +00:00
|
|
|
}
|
|
|
|
else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
|
|
|
|
return( POLARSSL_ERR_X509_INVALID_ALG + ret );
|
|
|
|
|
|
|
|
if( p == end )
|
|
|
|
return( 0 );
|
|
|
|
|
2014-01-24 13:15:20 +00:00
|
|
|
/*
|
2014-05-31 15:08:16 +00:00
|
|
|
* trailer_field (if present, must be 1)
|
2014-01-24 13:15:20 +00:00
|
|
|
*/
|
2014-01-23 15:24:44 +00:00
|
|
|
if( ( ret = asn1_get_tag( &p, end, &len,
|
|
|
|
ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 3 ) ) == 0 )
|
|
|
|
{
|
2014-05-31 15:08:16 +00:00
|
|
|
int trailer_field;
|
|
|
|
|
2014-01-24 13:15:20 +00:00
|
|
|
end2 = p + len;
|
|
|
|
|
2014-05-31 15:08:16 +00:00
|
|
|
if( ( ret = asn1_get_int( &p, end2, &trailer_field ) ) != 0 )
|
2014-01-23 15:24:44 +00:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_ALG + ret );
|
2014-01-24 13:15:20 +00:00
|
|
|
|
|
|
|
if( p != end2 )
|
|
|
|
return( POLARSSL_ERR_X509_INVALID_ALG +
|
|
|
|
POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
|
2014-05-31 15:08:16 +00:00
|
|
|
|
|
|
|
if( trailer_field != 1 )
|
|
|
|
return( POLARSSL_ERR_X509_INVALID_ALG );
|
2014-01-23 15:24:44 +00: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-06-06 14:42:37 +00:00
|
|
|
#endif /* POLARSSL_X509_RSASSA_PSS_SUPPORT */
|
2014-01-23 15:24:44 +00:00
|
|
|
|
2013-09-16 11:49:26 +00: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 12:36:05 +00:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_NAME + ret );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
|
|
|
if( ( end - *p ) < 1 )
|
2013-09-17 12:36:05 +00:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_NAME +
|
2013-09-16 11:49:26 +00: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 12:36:05 +00:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_NAME + ret );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
|
|
|
oid->p = *p;
|
|
|
|
*p += oid->len;
|
|
|
|
|
|
|
|
if( ( end - *p ) < 1 )
|
2013-09-17 12:36:05 +00:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_NAME +
|
2013-09-16 11:49:26 +00:00
|
|
|
POLARSSL_ERR_ASN1_OUT_OF_DATA );
|
|
|
|
|
|
|
|
if( **p != ASN1_BMP_STRING && **p != ASN1_UTF8_STRING &&
|
|
|
|
**p != ASN1_T61_STRING && **p != ASN1_PRINTABLE_STRING &&
|
2015-03-27 12:03:09 +00:00
|
|
|
**p != ASN1_IA5_STRING && **p != ASN1_UNIVERSAL_STRING &&
|
|
|
|
**p != ASN1_BIT_STRING )
|
2013-09-17 12:36:05 +00:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_NAME +
|
2013-09-16 11:49:26 +00: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 12:36:05 +00:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_NAME + ret );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
|
|
|
val->p = *p;
|
|
|
|
*p += val->len;
|
|
|
|
|
|
|
|
cur->next = NULL;
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2015-02-04 17:11:55 +00:00
|
|
|
* Name ::= CHOICE { -- only one possibility for now --
|
|
|
|
* rdnSequence RDNSequence }
|
|
|
|
*
|
|
|
|
* RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
|
|
|
|
*
|
2013-09-16 11:49:26 +00:00
|
|
|
* RelativeDistinguishedName ::=
|
|
|
|
* SET OF AttributeTypeAndValue
|
|
|
|
*
|
|
|
|
* AttributeTypeAndValue ::= SEQUENCE {
|
|
|
|
* type AttributeType,
|
|
|
|
* value AttributeValue }
|
|
|
|
*
|
|
|
|
* AttributeType ::= OBJECT IDENTIFIER
|
|
|
|
*
|
|
|
|
* AttributeValue ::= ANY DEFINED BY AttributeType
|
2014-10-17 10:41:41 +00:00
|
|
|
*
|
2015-02-04 17:11:55 +00:00
|
|
|
* The data structure is optimized for the common case where each RDN has only
|
|
|
|
* one element, which is represented as a list of AttributeTypeAndValue.
|
|
|
|
* For the general case we still use a flat list, but we mark elements of the
|
|
|
|
* same set so that they are "merged" together in the functions that consume
|
|
|
|
* this list, eg x509_dn_gets().
|
2013-09-16 11:49:26 +00:00
|
|
|
*/
|
|
|
|
int x509_get_name( unsigned char **p, const unsigned char *end,
|
|
|
|
x509_name *cur )
|
|
|
|
{
|
|
|
|
int ret;
|
2014-10-17 10:41:41 +00:00
|
|
|
size_t set_len;
|
|
|
|
const unsigned char *end_set;
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2014-11-12 00:25:31 +00:00
|
|
|
/* don't use recursion, we'd risk stack overflow if not optimized */
|
|
|
|
while( 1 )
|
|
|
|
{
|
|
|
|
/*
|
2015-02-04 17:11:55 +00:00
|
|
|
* parse SET
|
2014-11-12 00:25:31 +00:00
|
|
|
*/
|
|
|
|
if( ( ret = asn1_get_tag( p, end, &set_len,
|
|
|
|
ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 )
|
|
|
|
return( POLARSSL_ERR_X509_INVALID_NAME + ret );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2014-11-12 00:25:31 +00:00
|
|
|
end_set = *p + set_len;
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2015-02-04 17:11:55 +00:00
|
|
|
while( 1 )
|
|
|
|
{
|
|
|
|
if( ( ret = x509_get_attr_type_value( p, end_set, cur ) ) != 0 )
|
|
|
|
return( ret );
|
|
|
|
|
|
|
|
if( *p == end_set )
|
|
|
|
break;
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2015-05-11 09:39:49 +00:00
|
|
|
/* Mark this item as being not the only one in a set */
|
2015-02-04 17:11:55 +00:00
|
|
|
cur->next_merged = 1;
|
|
|
|
|
2015-02-15 22:35:38 +00:00
|
|
|
cur->next = polarssl_malloc( sizeof( x509_name ) );
|
2015-02-04 17:11:55 +00:00
|
|
|
|
|
|
|
if( cur->next == NULL )
|
|
|
|
return( POLARSSL_ERR_X509_MALLOC_FAILED );
|
|
|
|
|
|
|
|
memset( cur->next, 0, sizeof( x509_name ) );
|
|
|
|
|
|
|
|
cur = cur->next;
|
|
|
|
}
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2014-11-12 00:25:31 +00:00
|
|
|
/*
|
|
|
|
* continue until end of SEQUENCE is reached
|
|
|
|
*/
|
|
|
|
if( *p == end )
|
|
|
|
return( 0 );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2015-02-15 22:35:38 +00:00
|
|
|
cur->next = polarssl_malloc( sizeof( x509_name ) );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2014-11-12 00:25:31 +00:00
|
|
|
if( cur->next == NULL )
|
|
|
|
return( POLARSSL_ERR_X509_MALLOC_FAILED );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2014-11-12 00:25:31 +00:00
|
|
|
memset( cur->next, 0, sizeof( x509_name ) );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2014-11-12 00:25:31 +00:00
|
|
|
cur = cur->next;
|
|
|
|
}
|
2013-09-16 11:49:26 +00:00
|
|
|
}
|
|
|
|
|
2017-07-31 15:40:12 +00:00
|
|
|
static int x509_parse_int( unsigned char **p, size_t n, int *res )
|
|
|
|
{
|
2015-02-13 11:48:02 +00:00
|
|
|
*res = 0;
|
2017-07-31 15:40:12 +00:00
|
|
|
|
|
|
|
for( ; n > 0; --n )
|
|
|
|
{
|
|
|
|
if( ( **p < '0') || ( **p > '9' ) )
|
|
|
|
return( POLARSSL_ERR_X509_INVALID_DATE );
|
|
|
|
|
2015-02-13 11:48:02 +00:00
|
|
|
*res *= 10;
|
2017-07-31 15:40:12 +00:00
|
|
|
*res += ( *(*p)++ - '0' );
|
2015-02-13 11:48:02 +00:00
|
|
|
}
|
2017-07-31 15:40:12 +00:00
|
|
|
|
|
|
|
return( 0 );
|
2015-02-13 11:48:02 +00:00
|
|
|
}
|
|
|
|
|
2017-04-26 14:01:23 +00:00
|
|
|
static int x509_date_is_valid(const x509_time *t)
|
2016-09-23 12:16:02 +00:00
|
|
|
{
|
|
|
|
int ret = POLARSSL_ERR_X509_INVALID_DATE;
|
2016-11-21 15:38:02 +00:00
|
|
|
int month_len;
|
2016-09-23 12:16:02 +00:00
|
|
|
|
2017-04-26 14:01:23 +00:00
|
|
|
CHECK_RANGE( 0, 9999, t->year );
|
|
|
|
CHECK_RANGE( 0, 23, t->hour );
|
|
|
|
CHECK_RANGE( 0, 59, t->min );
|
|
|
|
CHECK_RANGE( 0, 59, t->sec );
|
2016-09-23 12:16:02 +00:00
|
|
|
|
2017-04-26 14:01:23 +00:00
|
|
|
switch( t->mon )
|
2016-09-23 12:16:02 +00:00
|
|
|
{
|
|
|
|
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
|
2016-11-21 15:38:02 +00:00
|
|
|
month_len = 31;
|
2016-09-23 12:16:02 +00:00
|
|
|
break;
|
|
|
|
case 4: case 6: case 9: case 11:
|
2016-11-21 15:38:02 +00:00
|
|
|
month_len = 30;
|
2016-09-23 12:16:02 +00:00
|
|
|
break;
|
|
|
|
case 2:
|
2016-11-21 15:38:02 +00:00
|
|
|
if( ( !( t->year % 4 ) && t->year % 100 ) ||
|
|
|
|
!( t->year % 400 ) )
|
|
|
|
month_len = 29;
|
|
|
|
else
|
|
|
|
month_len = 28;
|
2016-09-23 12:16:02 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return( ret );
|
|
|
|
}
|
2016-11-21 15:38:02 +00:00
|
|
|
CHECK_RANGE( 1, month_len, t->day );
|
2016-09-23 12:16:02 +00:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2016-09-19 08:35:18 +00:00
|
|
|
/*
|
|
|
|
* Parse an ASN1_UTC_TIME (yearlen=2) or ASN1_GENERALIZED_TIME (yearlen=4) field.
|
|
|
|
*/
|
2017-04-26 14:01:23 +00:00
|
|
|
static int x509_parse_time( unsigned char **p, size_t len, size_t yearlen,
|
|
|
|
x509_time *tm )
|
2016-09-19 08:35:18 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* minimum length is 10 or 12 depending on yearlen
|
|
|
|
*/
|
|
|
|
if ( len < yearlen + 8 )
|
|
|
|
return POLARSSL_ERR_X509_INVALID_DATE;
|
|
|
|
len -= yearlen + 8;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* parse year, month, day, hour, minute
|
|
|
|
*/
|
2017-04-26 14:01:23 +00:00
|
|
|
CHECK( x509_parse_int( p, yearlen, &tm->year ) );
|
2016-09-19 08:35:18 +00:00
|
|
|
if ( 2 == yearlen )
|
|
|
|
{
|
2017-04-26 14:01:23 +00:00
|
|
|
if ( tm->year < 50 )
|
|
|
|
tm->year += 100;
|
2016-09-19 08:35:18 +00:00
|
|
|
|
2017-04-26 14:01:23 +00:00
|
|
|
tm->year += 1900;
|
2016-09-19 08:35:18 +00:00
|
|
|
}
|
|
|
|
|
2017-04-26 14:01:23 +00:00
|
|
|
CHECK( x509_parse_int( p, 2, &tm->mon ) );
|
|
|
|
CHECK( x509_parse_int( p, 2, &tm->day ) );
|
|
|
|
CHECK( x509_parse_int( p, 2, &tm->hour ) );
|
|
|
|
CHECK( x509_parse_int( p, 2, &tm->min ) );
|
2016-09-19 08:35:18 +00:00
|
|
|
|
|
|
|
/*
|
2017-04-26 14:01:23 +00:00
|
|
|
* parse seconds if present
|
2016-09-19 08:35:18 +00:00
|
|
|
*/
|
|
|
|
if ( len >= 2 && **p >= '0' && **p <= '9' )
|
|
|
|
{
|
2017-04-26 14:01:23 +00:00
|
|
|
CHECK( x509_parse_int( p, 2, &tm->sec ) );
|
2016-09-19 08:35:18 +00:00
|
|
|
len -= 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#if defined(POLARSSL_X509_ALLOW_RELAXED_DATE)
|
|
|
|
/*
|
|
|
|
* if relaxed mode, allow seconds to be absent
|
|
|
|
*/
|
2017-04-26 14:01:23 +00:00
|
|
|
tm->sec = 0;
|
2016-09-19 08:35:18 +00:00
|
|
|
#else
|
|
|
|
return POLARSSL_ERR_X509_INVALID_DATE;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* parse trailing 'Z' if present
|
|
|
|
*/
|
|
|
|
if ( 1 == len && 'Z' == **p )
|
|
|
|
{
|
|
|
|
(*p)++;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#if defined(POLARSSL_X509_ALLOW_RELAXED_DATE)
|
|
|
|
/*
|
|
|
|
* if relaxed mode, allow timezone to be present
|
|
|
|
*/
|
|
|
|
else if ( 5 == len && ( '+' == **p || '-' == **p ) )
|
|
|
|
{
|
|
|
|
int tz; /* throwaway timezone */
|
|
|
|
|
|
|
|
(*p)++;
|
|
|
|
CHECK( x509_parse_int( p, 4, &tz ) );
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
/*
|
|
|
|
* okay if no trailing 'Z' or timezone specified
|
|
|
|
*/
|
|
|
|
else if ( 0 == len )
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return POLARSSL_ERR_X509_INVALID_DATE;
|
|
|
|
}
|
|
|
|
|
2013-09-16 11:49:26 +00:00
|
|
|
/*
|
|
|
|
* Time ::= CHOICE {
|
|
|
|
* utcTime UTCTime,
|
|
|
|
* generalTime GeneralizedTime }
|
|
|
|
*/
|
|
|
|
int x509_get_time( unsigned char **p, const unsigned char *end,
|
2017-04-26 14:01:23 +00:00
|
|
|
x509_time *tm )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t len;
|
|
|
|
unsigned char tag;
|
|
|
|
|
|
|
|
if( ( end - *p ) < 1 )
|
2013-09-17 12:36:05 +00:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_DATE +
|
2013-09-16 11:49:26 +00:00
|
|
|
POLARSSL_ERR_ASN1_OUT_OF_DATA );
|
|
|
|
|
|
|
|
tag = **p;
|
|
|
|
|
2014-06-17 14:39:18 +00:00
|
|
|
if( tag == ASN1_UTC_TIME )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
|
|
|
(*p)++;
|
|
|
|
ret = asn1_get_len( p, end, &len );
|
|
|
|
if( ret != 0 )
|
2013-09-17 12:36:05 +00:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_DATE + ret );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2017-04-26 14:01:23 +00:00
|
|
|
CHECK( x509_parse_time( p, len, 2, tm ) );
|
2016-10-17 21:32:47 +00:00
|
|
|
|
2017-04-26 14:01:23 +00:00
|
|
|
CHECK( x509_date_is_valid( tm ) );
|
2016-10-17 21:32:47 +00:00
|
|
|
|
|
|
|
return( 0 );
|
2013-09-16 11:49:26 +00:00
|
|
|
}
|
2014-06-17 14:39:18 +00:00
|
|
|
else if( tag == ASN1_GENERALIZED_TIME )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
|
|
|
(*p)++;
|
|
|
|
ret = asn1_get_len( p, end, &len );
|
|
|
|
if( ret != 0 )
|
2013-09-17 12:36:05 +00:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_DATE + ret );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2017-04-26 14:01:23 +00:00
|
|
|
CHECK( x509_parse_time( p, len, 4, tm ) );
|
2016-10-17 21:32:47 +00:00
|
|
|
|
2017-04-26 14:01:23 +00:00
|
|
|
CHECK( x509_date_is_valid( tm ) );
|
2016-10-17 21:32:47 +00:00
|
|
|
|
|
|
|
return( 0 );
|
2013-09-16 11:49:26 +00:00
|
|
|
}
|
|
|
|
else
|
2013-09-17 12:36:05 +00:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_DATE +
|
|
|
|
POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
|
2013-09-16 11:49:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int x509_get_sig( unsigned char **p, const unsigned char *end, x509_buf *sig )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t len;
|
2016-09-19 15:58:45 +00:00
|
|
|
int tag_type;
|
2013-09-16 11:49:26 +00:00
|
|
|
|
|
|
|
if( ( end - *p ) < 1 )
|
2013-09-17 12:36:05 +00:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_SIGNATURE +
|
2013-09-16 11:49:26 +00:00
|
|
|
POLARSSL_ERR_ASN1_OUT_OF_DATA );
|
|
|
|
|
2016-09-19 15:58:45 +00:00
|
|
|
tag_type = **p;
|
2013-09-16 11:49:26 +00:00
|
|
|
|
|
|
|
if( ( ret = asn1_get_bitstring_null( p, end, &len ) ) != 0 )
|
2013-09-17 12:36:05 +00:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_SIGNATURE + ret );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2016-09-19 15:58:45 +00:00
|
|
|
sig->tag = tag_type;
|
2013-09-16 11:49:26 +00:00
|
|
|
sig->len = len;
|
|
|
|
sig->p = *p;
|
|
|
|
|
|
|
|
*p += len;
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2014-01-24 18:28:43 +00: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,
|
2014-06-05 13:14:28 +00:00
|
|
|
md_type_t *md_alg, pk_type_t *pk_alg,
|
|
|
|
void **sig_opts )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
2014-01-24 18:28:43 +00:00
|
|
|
int ret;
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2014-06-05 13:14:28 +00:00
|
|
|
if( *sig_opts != NULL )
|
|
|
|
return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
|
|
|
|
|
2014-01-24 18:28:43 +00:00
|
|
|
if( ( ret = oid_get_sig_alg( sig_oid, md_alg, pk_alg ) ) != 0 )
|
2013-09-17 12:36:05 +00:00
|
|
|
return( POLARSSL_ERR_X509_UNKNOWN_SIG_ALG + ret );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2014-06-06 14:42:37 +00:00
|
|
|
#if defined(POLARSSL_X509_RSASSA_PSS_SUPPORT)
|
2014-01-24 18:28:43 +00:00
|
|
|
if( *pk_alg == POLARSSL_PK_RSASSA_PSS )
|
|
|
|
{
|
2014-06-05 13:14:28 +00:00
|
|
|
pk_rsassa_pss_options *pss_opts;
|
|
|
|
|
|
|
|
pss_opts = polarssl_malloc( sizeof( pk_rsassa_pss_options ) );
|
|
|
|
if( pss_opts == NULL )
|
|
|
|
return( POLARSSL_ERR_X509_MALLOC_FAILED );
|
2014-01-24 18:28:43 +00:00
|
|
|
|
|
|
|
ret = x509_get_rsassa_pss_params( sig_params,
|
2014-06-05 13:14:28 +00:00
|
|
|
md_alg,
|
|
|
|
&pss_opts->mgf1_hash_id,
|
|
|
|
&pss_opts->expected_salt_len );
|
2014-01-24 18:28:43 +00:00
|
|
|
if( ret != 0 )
|
2014-06-05 13:14:28 +00:00
|
|
|
{
|
|
|
|
polarssl_free( pss_opts );
|
2014-01-24 18:28:43 +00:00
|
|
|
return( ret );
|
2014-06-05 13:14:28 +00:00
|
|
|
}
|
2014-01-24 18:28:43 +00:00
|
|
|
|
2014-06-05 13:14:28 +00:00
|
|
|
*sig_opts = (void *) pss_opts;
|
2014-01-24 18:28:43 +00:00
|
|
|
}
|
|
|
|
else
|
2014-06-17 12:34:44 +00:00
|
|
|
#endif /* POLARSSL_X509_RSASSA_PSS_SUPPORT */
|
2014-01-24 18:28:43 +00:00
|
|
|
{
|
|
|
|
/* 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 11:49:26 +00: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 12:36:05 +00:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
|
|
|
if( end != *p + len )
|
2013-09-17 12:36:05 +00:00
|
|
|
return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
|
2013-09-16 11:49:26 +00:00
|
|
|
POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2013-10-29 14:22:54 +00:00
|
|
|
#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
|
|
|
|
!defined(EFI32)
|
2013-09-16 11:49:26 +00:00
|
|
|
#include <stdarg.h>
|
|
|
|
|
|
|
|
#if !defined vsnprintf
|
|
|
|
#define vsnprintf _vsnprintf
|
|
|
|
#endif // vsnprintf
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Windows _snprintf and _vsnprintf are not compatible to linux versions.
|
|
|
|
* Result value is not size of buffer needed, but -1 if no fit is possible.
|
|
|
|
*
|
|
|
|
* This fuction tries to 'fix' this by at least suggesting enlarging the
|
|
|
|
* size by 20.
|
|
|
|
*/
|
2014-06-17 14:39:18 +00:00
|
|
|
static int compat_snprintf( char *str, size_t size, const char *format, ... )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int res = -1;
|
|
|
|
|
|
|
|
va_start( ap, format );
|
|
|
|
|
|
|
|
res = vsnprintf( str, size, format, ap );
|
|
|
|
|
|
|
|
va_end( ap );
|
|
|
|
|
|
|
|
// No quick fix possible
|
2014-06-17 14:39:18 +00:00
|
|
|
if( res < 0 )
|
2013-09-16 11:49:26 +00:00
|
|
|
return( (int) size + 20 );
|
|
|
|
|
2014-06-17 12:06:49 +00:00
|
|
|
return( res );
|
2013-09-16 11:49:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define snprintf compat_snprintf
|
2014-05-01 11:03:14 +00:00
|
|
|
#endif /* _MSC_VER && !snprintf && !EFIX64 && !EFI32 */
|
2013-09-16 11:49:26 +00:00
|
|
|
|
|
|
|
#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
|
|
|
|
|
2014-06-17 12:06:49 +00:00
|
|
|
#define SAFE_SNPRINTF() \
|
|
|
|
{ \
|
|
|
|
if( ret == -1 ) \
|
|
|
|
return( -1 ); \
|
|
|
|
\
|
2014-06-17 14:39:18 +00:00
|
|
|
if( (unsigned int) ret > n ) { \
|
2014-06-17 12:06:49 +00:00
|
|
|
p[n - 1] = '\0'; \
|
|
|
|
return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL ); \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
n -= (unsigned int) ret; \
|
|
|
|
p += (unsigned int) ret; \
|
2013-09-16 11:49:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Store the name in printable form into buf; no more
|
|
|
|
* than size characters will be written
|
|
|
|
*/
|
2013-09-18 09:11:02 +00:00
|
|
|
int x509_dn_gets( char *buf, size_t size, const x509_name *dn )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t i, n;
|
2015-02-04 17:11:55 +00:00
|
|
|
unsigned char c, merge = 0;
|
2013-09-16 11:49:26 +00:00
|
|
|
const x509_name *name;
|
|
|
|
const char *short_name = NULL;
|
2014-08-08 10:22:30 +00:00
|
|
|
char s[X509_MAX_DN_NAME_SIZE], *p;
|
2013-09-16 11:49:26 +00:00
|
|
|
|
|
|
|
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 )
|
|
|
|
{
|
2015-01-30 11:00:01 +00:00
|
|
|
ret = polarssl_snprintf( p, n, merge ? " + " : ", " );
|
2013-09-16 11:49:26 +00:00
|
|
|
SAFE_SNPRINTF();
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = oid_get_attr_short_name( &name->oid, &short_name );
|
|
|
|
|
|
|
|
if( ret == 0 )
|
2015-01-30 11:00:01 +00:00
|
|
|
ret = polarssl_snprintf( p, n, "%s=", short_name );
|
2013-09-16 11:49:26 +00:00
|
|
|
else
|
2015-01-30 11:00:01 +00:00
|
|
|
ret = polarssl_snprintf( p, n, "\?\?=" );
|
2013-09-16 11:49:26 +00:00
|
|
|
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';
|
2015-01-30 11:00:01 +00:00
|
|
|
ret = polarssl_snprintf( p, n, "%s", s );
|
2013-09-16 11:49:26 +00:00
|
|
|
SAFE_SNPRINTF();
|
2015-02-04 17:11:55 +00:00
|
|
|
|
|
|
|
merge = name->next_merged;
|
2013-09-16 11:49:26 +00:00
|
|
|
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 09:11:02 +00:00
|
|
|
int x509_serial_gets( char *buf, size_t size, const x509_buf *serial )
|
2013-09-16 11:49:26 +00: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;
|
|
|
|
|
2015-01-30 11:00:01 +00:00
|
|
|
ret = polarssl_snprintf( p, n, "%02X%s",
|
2013-09-16 11:49:26 +00:00
|
|
|
serial->p[i], ( i < nr - 1 ) ? ":" : "" );
|
|
|
|
SAFE_SNPRINTF();
|
|
|
|
}
|
|
|
|
|
|
|
|
if( nr != serial->len )
|
|
|
|
{
|
2015-01-30 11:00:01 +00:00
|
|
|
ret = polarssl_snprintf( p, n, "...." );
|
2013-09-16 11:49:26 +00:00
|
|
|
SAFE_SNPRINTF();
|
|
|
|
}
|
|
|
|
|
|
|
|
return( (int) ( size - n ) );
|
|
|
|
}
|
|
|
|
|
2014-01-25 10:50:59 +00:00
|
|
|
/*
|
2014-06-05 13:41:39 +00:00
|
|
|
* Helper for writing signature algorithms
|
2014-01-25 10:50:59 +00:00
|
|
|
*/
|
|
|
|
int x509_sig_alg_gets( char *buf, size_t size, const x509_buf *sig_oid,
|
2014-06-05 13:41:39 +00:00
|
|
|
pk_type_t pk_alg, md_type_t md_alg,
|
|
|
|
const void *sig_opts )
|
2014-01-25 10:50:59 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
char *p = buf;
|
|
|
|
size_t n = size;
|
|
|
|
const char *desc = NULL;
|
|
|
|
|
|
|
|
ret = oid_get_sig_alg_desc( sig_oid, &desc );
|
|
|
|
if( ret != 0 )
|
2015-01-30 11:00:01 +00:00
|
|
|
ret = polarssl_snprintf( p, n, "???" );
|
2014-01-25 10:50:59 +00:00
|
|
|
else
|
2015-01-30 11:00:01 +00:00
|
|
|
ret = polarssl_snprintf( p, n, "%s", desc );
|
2014-01-25 10:50:59 +00:00
|
|
|
SAFE_SNPRINTF();
|
|
|
|
|
2014-06-06 14:42:37 +00:00
|
|
|
#if defined(POLARSSL_X509_RSASSA_PSS_SUPPORT)
|
2014-01-25 10:50:59 +00:00
|
|
|
if( pk_alg == POLARSSL_PK_RSASSA_PSS )
|
|
|
|
{
|
2014-06-05 13:41:39 +00:00
|
|
|
const pk_rsassa_pss_options *pss_opts;
|
2014-01-25 10:50:59 +00:00
|
|
|
const md_info_t *md_info, *mgf_md_info;
|
|
|
|
|
2014-06-05 13:41:39 +00:00
|
|
|
pss_opts = (const pk_rsassa_pss_options *) sig_opts;
|
2014-01-25 10:50:59 +00:00
|
|
|
|
|
|
|
md_info = md_info_from_type( md_alg );
|
2014-06-05 13:41:39 +00:00
|
|
|
mgf_md_info = md_info_from_type( pss_opts->mgf1_hash_id );
|
2014-01-25 10:50:59 +00:00
|
|
|
|
2015-01-30 11:00:01 +00:00
|
|
|
ret = polarssl_snprintf( p, n, " (%s, MGF1-%s, 0x%02X)",
|
2014-01-25 10:50:59 +00:00
|
|
|
md_info ? md_info->name : "???",
|
|
|
|
mgf_md_info ? mgf_md_info->name : "???",
|
2014-06-05 13:41:39 +00:00
|
|
|
pss_opts->expected_salt_len );
|
2014-01-25 10:50:59 +00:00
|
|
|
SAFE_SNPRINTF();
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
((void) pk_alg);
|
2014-06-05 13:41:39 +00:00
|
|
|
((void) md_alg);
|
|
|
|
((void) sig_opts);
|
2014-06-06 14:42:37 +00:00
|
|
|
#endif /* POLARSSL_X509_RSASSA_PSS_SUPPORT */
|
2014-01-25 10:50:59 +00:00
|
|
|
|
2014-08-16 10:45:52 +00:00
|
|
|
return( (int)( size - n ) );
|
2014-01-25 10:50:59 +00:00
|
|
|
}
|
|
|
|
|
2013-09-16 11:49:26 +00:00
|
|
|
/*
|
|
|
|
* 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 )
|
2014-06-17 12:06:49 +00:00
|
|
|
return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2015-01-30 11:00:01 +00:00
|
|
|
ret = polarssl_snprintf( p, n, "%s key size", name );
|
2013-09-16 11:49:26 +00:00
|
|
|
SAFE_SNPRINTF();
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return an informational string describing the given OID
|
|
|
|
*/
|
2015-03-23 12:58:27 +00:00
|
|
|
#if ! defined(POLARSSL_DEPRECATED_REMOVED)
|
2013-09-16 11:49:26 +00:00
|
|
|
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 );
|
|
|
|
}
|
2015-03-23 12:58:27 +00:00
|
|
|
#endif
|
2013-09-16 11:49:26 +00:00
|
|
|
|
|
|
|
/* Return the x.y.z.... style numeric string for the given OID */
|
2015-03-23 12:58:27 +00:00
|
|
|
#if ! defined(POLARSSL_DEPRECATED_REMOVED)
|
2013-09-16 11:49:26 +00:00
|
|
|
int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
|
|
|
|
{
|
|
|
|
return oid_get_numeric_string( buf, size, oid );
|
|
|
|
}
|
2015-03-23 12:58:27 +00:00
|
|
|
#endif
|
2013-09-16 11:49:26 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Return 0 if the x509_time is still valid, or 1 otherwise.
|
|
|
|
*/
|
|
|
|
#if defined(POLARSSL_HAVE_TIME)
|
|
|
|
|
2014-03-10 11:26:11 +00:00
|
|
|
static void x509_get_current_time( x509_time *now )
|
|
|
|
{
|
2013-10-28 17:48:30 +00:00
|
|
|
#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
|
2013-09-16 11:49:26 +00:00
|
|
|
SYSTEMTIME st;
|
|
|
|
|
2014-06-17 14:39:18 +00:00
|
|
|
GetSystemTime( &st );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2014-03-10 11:26:11 +00: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 11:49:26 +00:00
|
|
|
#else
|
2014-03-26 14:34:54 +00:00
|
|
|
struct tm lt;
|
2013-09-16 11:49:26 +00:00
|
|
|
time_t tt;
|
|
|
|
|
|
|
|
tt = time( NULL );
|
2014-04-11 10:25:45 +00:00
|
|
|
gmtime_r( &tt, < );
|
2014-03-26 14:34:54 +00: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 11:03:14 +00:00
|
|
|
#endif /* _WIN32 && !EFIX64 && !EFI32 */
|
2014-03-10 11:26:11 +00:00
|
|
|
}
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2014-03-10 11:26:11 +00: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 11:49:26 +00:00
|
|
|
return( 1 );
|
|
|
|
|
2014-03-10 11:26:11 +00:00
|
|
|
if( before->year == after->year &&
|
|
|
|
before->mon > after->mon )
|
2013-09-16 11:49:26 +00:00
|
|
|
return( 1 );
|
|
|
|
|
2014-03-10 11:26:11 +00:00
|
|
|
if( before->year == after->year &&
|
|
|
|
before->mon == after->mon &&
|
|
|
|
before->day > after->day )
|
2013-09-16 11:49:26 +00:00
|
|
|
return( 1 );
|
|
|
|
|
2014-03-10 11:26:11 +00:00
|
|
|
if( before->year == after->year &&
|
|
|
|
before->mon == after->mon &&
|
|
|
|
before->day == after->day &&
|
|
|
|
before->hour > after->hour )
|
2013-09-16 11:49:26 +00:00
|
|
|
return( 1 );
|
|
|
|
|
2014-03-10 11:26:11 +00: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 11:49:26 +00:00
|
|
|
return( 1 );
|
|
|
|
|
2014-03-10 11:26:11 +00: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 11:49:26 +00:00
|
|
|
return( 1 );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
2014-03-10 11:26:11 +00: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 11:49:26 +00:00
|
|
|
#else /* POLARSSL_HAVE_TIME */
|
2014-03-10 11:26:11 +00:00
|
|
|
|
2013-09-18 09:11:02 +00:00
|
|
|
int x509_time_expired( const x509_time *to )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
|
|
|
((void) to);
|
|
|
|
return( 0 );
|
|
|
|
}
|
2014-03-10 11:26:11 +00:00
|
|
|
|
|
|
|
int x509_time_future( const x509_time *from )
|
|
|
|
{
|
|
|
|
((void) from);
|
|
|
|
return( 0 );
|
|
|
|
}
|
2013-09-16 11:49:26 +00:00
|
|
|
#endif /* POLARSSL_HAVE_TIME */
|
|
|
|
|
2013-09-16 20:53:25 +00: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 13:29:41 +00:00
|
|
|
#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_SHA1_C)
|
2013-09-16 20:53:25 +00:00
|
|
|
int ret;
|
|
|
|
int flags;
|
2013-09-18 12:13:26 +00:00
|
|
|
x509_crt cacert;
|
|
|
|
x509_crt clicert;
|
2013-09-16 20:53:25 +00:00
|
|
|
|
|
|
|
if( verbose != 0 )
|
2014-02-01 21:50:26 +00:00
|
|
|
polarssl_printf( " X.509 certificate load: " );
|
2013-09-16 20:53:25 +00:00
|
|
|
|
2013-09-18 12:17:41 +00:00
|
|
|
x509_crt_init( &clicert );
|
2013-09-16 20:53:25 +00:00
|
|
|
|
2013-09-18 11:46:23 +00:00
|
|
|
ret = x509_crt_parse( &clicert, (const unsigned char *) test_cli_crt,
|
|
|
|
strlen( test_cli_crt ) );
|
2013-09-16 20:53:25 +00:00
|
|
|
if( ret != 0 )
|
|
|
|
{
|
|
|
|
if( verbose != 0 )
|
2014-02-01 21:50:26 +00:00
|
|
|
polarssl_printf( "failed\n" );
|
2013-09-16 20:53:25 +00:00
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2013-09-18 12:17:41 +00:00
|
|
|
x509_crt_init( &cacert );
|
2013-09-16 20:53:25 +00:00
|
|
|
|
2013-09-18 11:46:23 +00:00
|
|
|
ret = x509_crt_parse( &cacert, (const unsigned char *) test_ca_crt,
|
|
|
|
strlen( test_ca_crt ) );
|
2013-09-16 20:53:25 +00:00
|
|
|
if( ret != 0 )
|
|
|
|
{
|
|
|
|
if( verbose != 0 )
|
2014-02-01 21:50:26 +00:00
|
|
|
polarssl_printf( "failed\n" );
|
2013-09-16 20:53:25 +00:00
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( verbose != 0 )
|
2014-02-01 21:50:26 +00:00
|
|
|
polarssl_printf( "passed\n X.509 signature verify: ");
|
2013-09-16 20:53:25 +00:00
|
|
|
|
2013-09-18 11:46:23 +00:00
|
|
|
ret = x509_crt_verify( &clicert, &cacert, NULL, NULL, &flags, NULL, NULL );
|
2013-09-16 20:53:25 +00:00
|
|
|
if( ret != 0 )
|
|
|
|
{
|
|
|
|
if( verbose != 0 )
|
2014-02-01 21:50:26 +00:00
|
|
|
polarssl_printf( "failed\n" );
|
2013-09-16 20:53:25 +00:00
|
|
|
|
2014-06-17 14:39:18 +00:00
|
|
|
polarssl_printf( "ret = %d, &flags = %04x\n", ret, flags );
|
2013-09-16 20:53:25 +00:00
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( verbose != 0 )
|
2014-02-01 21:50:26 +00:00
|
|
|
polarssl_printf( "passed\n\n");
|
2013-09-16 20:53:25 +00:00
|
|
|
|
|
|
|
x509_crt_free( &cacert );
|
|
|
|
x509_crt_free( &clicert );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
#else
|
|
|
|
((void) verbose);
|
|
|
|
return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
|
2014-05-01 11:03:14 +00:00
|
|
|
#endif /* POLARSSL_CERTS_C && POLARSSL_SHA1_C */
|
2013-09-16 20:53:25 +00:00
|
|
|
}
|
|
|
|
|
2014-05-01 11:03:14 +00:00
|
|
|
#endif /* POLARSSL_SELF_TEST */
|
2013-09-16 20:53:25 +00:00
|
|
|
|
2013-09-16 11:49:26 +00:00
|
|
|
#endif /* POLARSSL_X509_USE_C */
|