2013-09-16 13:49:26 +02:00
|
|
|
/*
|
2014-06-12 22:34:55 +02:00
|
|
|
* X.509 common functions for parsing and verification
|
2013-09-16 13:49:26 +02:00
|
|
|
*
|
2015-07-27 11:11:48 +02:00
|
|
|
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
2015-09-04 14:21:07 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2013-09-16 13:49:26 +02:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2013-09-16 13:49:26 +02:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2013-09-16 13:49:26 +02:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2013-09-16 13:49:26 +02:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* This file is part of mbed TLS (https://tls.mbed.org)
|
2013-09-16 13:49:26 +02:00
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* The ITU-T X.509 standard defines a certificate format for PKI.
|
|
|
|
*
|
2014-06-12 22:34:55 +02:00
|
|
|
* http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
|
|
|
|
* http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
|
|
|
|
* http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
|
2013-09-16 13:49:26 +02:00
|
|
|
*
|
|
|
|
* http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
|
|
|
|
* http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
|
|
|
|
*/
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if !defined(MBEDTLS_CONFIG_FILE)
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/config.h"
|
2014-04-29 12:39:06 +02:00
|
|
|
#else
|
2015-04-08 12:49:31 +02:00
|
|
|
#include MBEDTLS_CONFIG_FILE
|
2014-04-29 12:39:06 +02:00
|
|
|
#endif
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_X509_USE_C)
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/x509.h"
|
|
|
|
#include "mbedtls/asn1.h"
|
|
|
|
#include "mbedtls/oid.h"
|
2015-02-06 14:43:58 +01:00
|
|
|
|
2015-02-12 19:27:14 +01:00
|
|
|
#include <stdio.h>
|
2015-02-06 14:43:58 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_PEM_PARSE_C)
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/pem.h"
|
2013-09-16 13:49:26 +02:00
|
|
|
#endif
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_PLATFORM_C)
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/platform.h"
|
2013-09-16 13:49:26 +02:00
|
|
|
#else
|
2015-02-06 14:43:58 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2015-04-08 12:49:31 +02:00
|
|
|
#define mbedtls_free free
|
2015-05-26 16:04:06 +02:00
|
|
|
#define mbedtls_calloc calloc
|
2015-04-08 12:49:31 +02:00
|
|
|
#define mbedtls_printf printf
|
|
|
|
#define mbedtls_snprintf snprintf
|
2013-09-16 13:49:26 +02:00
|
|
|
#endif
|
|
|
|
|
2013-10-28 18:48:30 +01:00
|
|
|
#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
|
2013-09-16 13:49:26 +02:00
|
|
|
#include <windows.h>
|
|
|
|
#else
|
|
|
|
#include <time.h>
|
|
|
|
#endif
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_FS_IO)
|
2015-02-12 19:27:14 +01:00
|
|
|
#include <stdio.h>
|
2013-09-16 13:49:26 +02:00
|
|
|
#if !defined(_WIN32)
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2015-02-13 12:48:02 +01:00
|
|
|
#define CHECK(code) if( ( ret = code ) != 0 ){ return( ret ); }
|
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
/*
|
|
|
|
* CertificateSerialNumber ::= INTEGER
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_x509_get_serial( unsigned char **p, const unsigned char *end,
|
|
|
|
mbedtls_x509_buf *serial )
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if( ( end - *p ) < 1 )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_X509_INVALID_SERIAL +
|
|
|
|
MBEDTLS_ERR_ASN1_OUT_OF_DATA );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( **p != ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_PRIMITIVE | 2 ) &&
|
|
|
|
**p != MBEDTLS_ASN1_INTEGER )
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_SERIAL +
|
|
|
|
MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
serial->tag = *(*p)++;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_asn1_get_len( p, end, &serial->len ) ) != 0 )
|
|
|
|
return( MBEDTLS_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 }
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_x509_get_alg_null( unsigned char **p, const unsigned char *end,
|
|
|
|
mbedtls_x509_buf *alg )
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_asn1_get_alg_null( p, end, alg ) ) != 0 )
|
|
|
|
return( MBEDTLS_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
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_x509_get_alg( unsigned char **p, const unsigned char *end,
|
|
|
|
mbedtls_x509_buf *alg, mbedtls_x509_buf *params )
|
2014-01-22 10:12:57 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_asn1_get_alg( p, end, alg, params ) ) != 0 )
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
|
2014-01-22 10:12:57 +01:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
|
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.
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
static int x509_get_hash_alg( const mbedtls_x509_buf *alg, mbedtls_md_type_t *md_alg )
|
2014-01-23 19:15:29 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
unsigned char *p;
|
|
|
|
const unsigned char *end;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_buf md_oid;
|
2014-01-23 19:15:29 +01:00
|
|
|
size_t len;
|
|
|
|
|
|
|
|
/* Make sure we got a SEQUENCE and setup bounds */
|
2015-04-08 12:49:31 +02:00
|
|
|
if( alg->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_ALG +
|
|
|
|
MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
|
2014-01-23 19:15:29 +01:00
|
|
|
|
|
|
|
p = (unsigned char *) alg->p;
|
|
|
|
end = p + alg->len;
|
|
|
|
|
|
|
|
if( p >= end )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_X509_INVALID_ALG +
|
|
|
|
MBEDTLS_ERR_ASN1_OUT_OF_DATA );
|
2014-01-23 19:15:29 +01:00
|
|
|
|
|
|
|
/* Parse md_oid */
|
|
|
|
md_oid.tag = *p;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_asn1_get_tag( &p, end, &md_oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
|
2014-01-23 19:15:29 +01:00
|
|
|
|
|
|
|
md_oid.p = p;
|
|
|
|
p += md_oid.len;
|
|
|
|
|
|
|
|
/* Get md_alg from md_oid */
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_oid_get_md_alg( &md_oid, md_alg ) ) != 0 )
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
|
2014-01-23 19:15:29 +01:00
|
|
|
|
|
|
|
/* Make sure params is absent of NULL */
|
|
|
|
if( p == end )
|
|
|
|
return( 0 );
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_NULL ) ) != 0 || len != 0 )
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
|
2014-01-23 19:15:29 +01:00
|
|
|
|
|
|
|
if( p != end )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_X509_INVALID_ALG +
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
|
2014-01-23 19:15:29 +01:00
|
|
|
|
|
|
|
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.
|
2014-05-31 17:08:16 +02: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 16:24:44 +01:00
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_x509_get_rsassa_pss_params( const mbedtls_x509_buf *params,
|
|
|
|
mbedtls_md_type_t *md_alg, mbedtls_md_type_t *mgf_md,
|
2014-05-31 17:08:16 +02:00
|
|
|
int *salt_len )
|
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;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_buf alg_id, alg_params;
|
2014-01-23 16:24:44 +01:00
|
|
|
|
|
|
|
/* First set everything to defaults */
|
2015-04-08 12:49:31 +02:00
|
|
|
*md_alg = MBEDTLS_MD_SHA1;
|
|
|
|
*mgf_md = MBEDTLS_MD_SHA1;
|
2014-01-23 16:24:44 +01:00
|
|
|
*salt_len = 20;
|
|
|
|
|
|
|
|
/* Make sure params is a SEQUENCE and setup bounds */
|
2015-04-08 12:49:31 +02:00
|
|
|
if( params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_ALG +
|
|
|
|
MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
|
2014-01-23 16:24:44 +01:00
|
|
|
|
|
|
|
p = (unsigned char *) params->p;
|
|
|
|
end = p + params->len;
|
|
|
|
|
|
|
|
if( p == end )
|
|
|
|
return( 0 );
|
|
|
|
|
2014-01-24 14:15:20 +01:00
|
|
|
/*
|
|
|
|
* HashAlgorithm
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
|
|
|
|
MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
|
2014-01-23 16:24:44 +01:00
|
|
|
{
|
2014-01-24 14:15:20 +01:00
|
|
|
end2 = p + len;
|
|
|
|
|
2014-01-23 19:15:29 +01:00
|
|
|
/* HashAlgorithm ::= AlgorithmIdentifier (without parameters) */
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_x509_get_alg_null( &p, end2, &alg_id ) ) != 0 )
|
2014-01-23 19:15:29 +01:00
|
|
|
return( ret );
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_oid_get_md_alg( &alg_id, md_alg ) ) != 0 )
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
|
2014-01-24 14:15:20 +01:00
|
|
|
|
|
|
|
if( p != end2 )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_X509_INVALID_ALG +
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
|
2014-01-23 16:24:44 +01:00
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
|
2014-01-23 16:24:44 +01:00
|
|
|
|
2014-01-24 14:15:20 +01:00
|
|
|
if( p == end )
|
|
|
|
return( 0 );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* MaskGenAlgorithm
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
|
|
|
|
MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
|
2014-01-23 16:24:44 +01:00
|
|
|
{
|
2014-01-24 14:15:20 +01:00
|
|
|
end2 = p + len;
|
|
|
|
|
2014-01-23 19:15:29 +01:00
|
|
|
/* MaskGenAlgorithm ::= AlgorithmIdentifier (params = HashAlgorithm) */
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_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 */
|
2015-04-08 12:49:31 +02:00
|
|
|
if( MBEDTLS_OID_CMP( MBEDTLS_OID_MGF1, &alg_id ) != 0 )
|
|
|
|
return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE +
|
|
|
|
MBEDTLS_ERR_OID_NOT_FOUND );
|
2014-01-23 19:15:29 +01:00
|
|
|
|
|
|
|
/* 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 )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_X509_INVALID_ALG +
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
|
2014-01-23 16:24:44 +01:00
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
|
2014-01-23 16:24:44 +01:00
|
|
|
|
|
|
|
if( p == end )
|
|
|
|
return( 0 );
|
|
|
|
|
2014-01-24 14:15:20 +01:00
|
|
|
/*
|
|
|
|
* salt_len
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
|
|
|
|
MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 2 ) ) == 0 )
|
2014-01-23 16:24:44 +01:00
|
|
|
{
|
2014-01-24 14:15:20 +01:00
|
|
|
end2 = p + len;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_asn1_get_int( &p, end2, salt_len ) ) != 0 )
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
|
2014-01-24 14:15:20 +01:00
|
|
|
|
|
|
|
if( p != end2 )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_X509_INVALID_ALG +
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
|
2014-01-23 16:24:44 +01:00
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
|
2014-01-23 16:24:44 +01:00
|
|
|
|
|
|
|
if( p == end )
|
|
|
|
return( 0 );
|
|
|
|
|
2014-01-24 14:15:20 +01:00
|
|
|
/*
|
2014-05-31 17:08:16 +02:00
|
|
|
* trailer_field (if present, must be 1)
|
2014-01-24 14:15:20 +01:00
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
|
|
|
|
MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 3 ) ) == 0 )
|
2014-01-23 16:24:44 +01:00
|
|
|
{
|
2014-05-31 17:08:16 +02:00
|
|
|
int trailer_field;
|
|
|
|
|
2014-01-24 14:15:20 +01:00
|
|
|
end2 = p + len;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_asn1_get_int( &p, end2, &trailer_field ) ) != 0 )
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
|
2014-01-24 14:15:20 +01:00
|
|
|
|
|
|
|
if( p != end2 )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_X509_INVALID_ALG +
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
|
2014-05-31 17:08:16 +02:00
|
|
|
|
|
|
|
if( trailer_field != 1 )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_X509_INVALID_ALG );
|
2014-01-23 16:24:44 +01:00
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
|
2014-01-23 16:24:44 +01:00
|
|
|
|
|
|
|
if( p != end )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_X509_INVALID_ALG +
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
|
2014-01-23 16:24:44 +01:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
|
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,
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_name *cur )
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t len;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_buf *oid;
|
|
|
|
mbedtls_x509_buf *val;
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_NAME + ret );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
if( ( end - *p ) < 1 )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_X509_INVALID_NAME +
|
|
|
|
MBEDTLS_ERR_ASN1_OUT_OF_DATA );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
oid = &cur->oid;
|
|
|
|
oid->tag = **p;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_asn1_get_tag( p, end, &oid->len, MBEDTLS_ASN1_OID ) ) != 0 )
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_NAME + ret );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
oid->p = *p;
|
|
|
|
*p += oid->len;
|
|
|
|
|
|
|
|
if( ( end - *p ) < 1 )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_X509_INVALID_NAME +
|
|
|
|
MBEDTLS_ERR_ASN1_OUT_OF_DATA );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( **p != MBEDTLS_ASN1_BMP_STRING && **p != MBEDTLS_ASN1_UTF8_STRING &&
|
|
|
|
**p != MBEDTLS_ASN1_T61_STRING && **p != MBEDTLS_ASN1_PRINTABLE_STRING &&
|
|
|
|
**p != MBEDTLS_ASN1_IA5_STRING && **p != MBEDTLS_ASN1_UNIVERSAL_STRING &&
|
|
|
|
**p != MBEDTLS_ASN1_BIT_STRING )
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_NAME +
|
|
|
|
MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
val = &cur->val;
|
|
|
|
val->tag = *(*p)++;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_asn1_get_len( p, end, &val->len ) ) != 0 )
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_NAME + ret );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
val->p = *p;
|
|
|
|
*p += val->len;
|
|
|
|
|
|
|
|
cur->next = NULL;
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2015-02-04 18:11:55 +01:00
|
|
|
* Name ::= CHOICE { -- only one possibility for now --
|
|
|
|
* rdnSequence RDNSequence }
|
|
|
|
*
|
|
|
|
* RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
|
|
|
|
*
|
2013-09-16 13:49:26 +02:00
|
|
|
* RelativeDistinguishedName ::=
|
|
|
|
* SET OF AttributeTypeAndValue
|
|
|
|
*
|
|
|
|
* AttributeTypeAndValue ::= SEQUENCE {
|
|
|
|
* type AttributeType,
|
|
|
|
* value AttributeValue }
|
|
|
|
*
|
|
|
|
* AttributeType ::= OBJECT IDENTIFIER
|
|
|
|
*
|
|
|
|
* AttributeValue ::= ANY DEFINED BY AttributeType
|
2014-10-17 12:41:41 +02:00
|
|
|
*
|
2015-02-04 18:11:55 +01: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
|
2015-04-08 12:49:31 +02:00
|
|
|
* this list, eg mbedtls_x509_dn_gets().
|
2013-09-16 13:49:26 +02:00
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_x509_get_name( unsigned char **p, const unsigned char *end,
|
|
|
|
mbedtls_x509_name *cur )
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
|
|
|
int ret;
|
2014-10-17 12:41:41 +02:00
|
|
|
size_t set_len;
|
|
|
|
const unsigned char *end_set;
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2014-11-12 01:25:31 +01:00
|
|
|
/* don't use recursion, we'd risk stack overflow if not optimized */
|
|
|
|
while( 1 )
|
|
|
|
{
|
|
|
|
/*
|
2015-02-04 18:11:55 +01:00
|
|
|
* parse SET
|
2014-11-12 01:25:31 +01:00
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_asn1_get_tag( p, end, &set_len,
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET ) ) != 0 )
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_NAME + ret );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2014-11-12 01:25:31 +01:00
|
|
|
end_set = *p + set_len;
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2015-02-04 18:11:55 +01: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 13:49:26 +02:00
|
|
|
|
2015-05-12 12:56:41 +02:00
|
|
|
/* Mark this item as being no the only one in a set */
|
2015-02-04 18:11:55 +01:00
|
|
|
cur->next_merged = 1;
|
|
|
|
|
2015-05-26 16:04:06 +02:00
|
|
|
cur->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) );
|
2015-02-04 18:11:55 +01:00
|
|
|
|
|
|
|
if( cur->next == NULL )
|
2015-05-28 09:33:39 +02:00
|
|
|
return( MBEDTLS_ERR_X509_ALLOC_FAILED );
|
2015-02-04 18:11:55 +01:00
|
|
|
|
|
|
|
cur = cur->next;
|
|
|
|
}
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2014-11-12 01:25:31 +01:00
|
|
|
/*
|
|
|
|
* continue until end of SEQUENCE is reached
|
|
|
|
*/
|
|
|
|
if( *p == end )
|
|
|
|
return( 0 );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2015-05-26 16:04:06 +02:00
|
|
|
cur->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2014-11-12 01:25:31 +01:00
|
|
|
if( cur->next == NULL )
|
2015-05-28 09:33:39 +02:00
|
|
|
return( MBEDTLS_ERR_X509_ALLOC_FAILED );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2014-11-12 01:25:31 +01:00
|
|
|
cur = cur->next;
|
|
|
|
}
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
2015-02-13 12:48:02 +01:00
|
|
|
static int x509_parse_int(unsigned char **p, unsigned n, int *res){
|
|
|
|
*res = 0;
|
|
|
|
for( ; n > 0; --n ){
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( **p < '0') || ( **p > '9' ) ) return MBEDTLS_ERR_X509_INVALID_DATE;
|
2015-02-13 12:48:02 +01:00
|
|
|
*res *= 10;
|
|
|
|
*res += (*(*p)++ - '0');
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
/*
|
|
|
|
* Time ::= CHOICE {
|
|
|
|
* utcTime UTCTime,
|
|
|
|
* generalTime GeneralizedTime }
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_x509_get_time( unsigned char **p, const unsigned char *end,
|
|
|
|
mbedtls_x509_time *time )
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t len;
|
|
|
|
unsigned char tag;
|
|
|
|
|
|
|
|
if( ( end - *p ) < 1 )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_X509_INVALID_DATE +
|
|
|
|
MBEDTLS_ERR_ASN1_OUT_OF_DATA );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
tag = **p;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( tag == MBEDTLS_ASN1_UTC_TIME )
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
|
|
|
(*p)++;
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = mbedtls_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 )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_X509_INVALID_DATE + ret );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2015-02-13 12:48:02 +01:00
|
|
|
CHECK( x509_parse_int( p, 2, &time->year ) );
|
|
|
|
CHECK( x509_parse_int( p, 2, &time->mon ) );
|
|
|
|
CHECK( x509_parse_int( p, 2, &time->day ) );
|
|
|
|
CHECK( x509_parse_int( p, 2, &time->hour ) );
|
|
|
|
CHECK( x509_parse_int( p, 2, &time->min ) );
|
|
|
|
if( len > 10 )
|
|
|
|
CHECK( x509_parse_int( p, 2, &time->sec ) );
|
|
|
|
if( len > 12 && *(*p)++ != 'Z' )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_X509_INVALID_DATE );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
time->year += 100 * ( time->year < 50 );
|
|
|
|
time->year += 1900;
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
else if( tag == MBEDTLS_ASN1_GENERALIZED_TIME )
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
|
|
|
(*p)++;
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = mbedtls_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 )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_X509_INVALID_DATE + ret );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2015-02-13 12:48:02 +01:00
|
|
|
CHECK( x509_parse_int( p, 4, &time->year ) );
|
|
|
|
CHECK( x509_parse_int( p, 2, &time->mon ) );
|
|
|
|
CHECK( x509_parse_int( p, 2, &time->day ) );
|
|
|
|
CHECK( x509_parse_int( p, 2, &time->hour ) );
|
|
|
|
CHECK( x509_parse_int( p, 2, &time->min ) );
|
|
|
|
if( len > 12 )
|
|
|
|
CHECK( x509_parse_int( p, 2, &time->sec ) );
|
|
|
|
if( len > 14 && *(*p)++ != 'Z' )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_X509_INVALID_DATE );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
else
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_X509_INVALID_DATE +
|
|
|
|
MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_x509_get_sig( unsigned char **p, const unsigned char *end, mbedtls_x509_buf *sig )
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
if( ( end - *p ) < 1 )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_X509_INVALID_SIGNATURE +
|
|
|
|
MBEDTLS_ERR_ASN1_OUT_OF_DATA );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
sig->tag = **p;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_asn1_get_bitstring_null( p, end, &len ) ) != 0 )
|
|
|
|
return( MBEDTLS_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
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_x509_get_sig_alg( const mbedtls_x509_buf *sig_oid, const mbedtls_x509_buf *sig_params,
|
|
|
|
mbedtls_md_type_t *md_alg, mbedtls_pk_type_t *pk_alg,
|
2014-06-05 15:14:28 +02:00
|
|
|
void **sig_opts )
|
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-06-05 15:14:28 +02:00
|
|
|
if( *sig_opts != NULL )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
|
2014-06-05 15:14:28 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_oid_get_sig_alg( sig_oid, md_alg, pk_alg ) ) != 0 )
|
|
|
|
return( MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + ret );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
|
|
|
|
if( *pk_alg == MBEDTLS_PK_RSASSA_PSS )
|
2014-01-24 19:28:43 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_rsassa_pss_options *pss_opts;
|
2014-06-05 15:14:28 +02:00
|
|
|
|
2015-05-26 16:04:06 +02:00
|
|
|
pss_opts = mbedtls_calloc( 1, sizeof( mbedtls_pk_rsassa_pss_options ) );
|
2014-06-05 15:14:28 +02:00
|
|
|
if( pss_opts == NULL )
|
2015-05-28 09:33:39 +02:00
|
|
|
return( MBEDTLS_ERR_X509_ALLOC_FAILED );
|
2014-01-24 19:28:43 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = mbedtls_x509_get_rsassa_pss_params( sig_params,
|
2014-06-05 15:14:28 +02:00
|
|
|
md_alg,
|
|
|
|
&pss_opts->mgf1_hash_id,
|
|
|
|
&pss_opts->expected_salt_len );
|
2014-01-24 19:28:43 +01:00
|
|
|
if( ret != 0 )
|
2014-06-05 15:14:28 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_free( pss_opts );
|
2014-01-24 19:28:43 +01:00
|
|
|
return( ret );
|
2014-06-05 15:14:28 +02:00
|
|
|
}
|
2014-01-24 19:28:43 +01:00
|
|
|
|
2014-06-05 15:14:28 +02:00
|
|
|
*sig_opts = (void *) pss_opts;
|
2014-01-24 19:28:43 +01:00
|
|
|
}
|
|
|
|
else
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
|
2014-01-24 19:28:43 +01:00
|
|
|
{
|
|
|
|
/* Make sure parameters are absent or NULL */
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( sig_params->tag != MBEDTLS_ASN1_NULL && sig_params->tag != 0 ) ||
|
2014-01-24 19:28:43 +01:00
|
|
|
sig_params->len != 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_X509_INVALID_ALG );
|
2014-01-24 19:28:43 +01:00
|
|
|
}
|
|
|
|
|
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!
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_x509_get_ext( unsigned char **p, const unsigned char *end,
|
|
|
|
mbedtls_x509_buf *ext, int tag )
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
if( *p == end )
|
|
|
|
return( 0 );
|
|
|
|
|
|
|
|
ext->tag = **p;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_asn1_get_tag( p, end, &ext->len,
|
|
|
|
MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag ) ) != 0 )
|
2013-09-16 13:49:26 +02: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 }
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
if( end != *p + len )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Store the name in printable form into buf; no more
|
|
|
|
* than size characters will be written
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_x509_dn_gets( char *buf, size_t size, const mbedtls_x509_name *dn )
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t i, n;
|
2015-02-04 18:11:55 +01:00
|
|
|
unsigned char c, merge = 0;
|
2015-04-08 12:49:31 +02:00
|
|
|
const mbedtls_x509_name *name;
|
2013-09-16 13:49:26 +02:00
|
|
|
const char *short_name = NULL;
|
2015-04-08 12:49:31 +02:00
|
|
|
char s[MBEDTLS_X509_MAX_DN_NAME_SIZE], *p;
|
2013-09-16 13:49:26 +02: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-04-08 12:49:31 +02:00
|
|
|
ret = mbedtls_snprintf( p, n, merge ? " + " : ", " );
|
2015-06-22 11:12:02 +02:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = mbedtls_oid_get_attr_short_name( &name->oid, &short_name );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
if( ret == 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = mbedtls_snprintf( p, n, "%s=", short_name );
|
2013-09-16 13:49:26 +02:00
|
|
|
else
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = mbedtls_snprintf( p, n, "\?\?=" );
|
2015-06-22 11:12:02 +02:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
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-04-08 12:49:31 +02:00
|
|
|
ret = mbedtls_snprintf( p, n, "%s", s );
|
2015-06-22 11:12:02 +02:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
2015-02-04 18:11:55 +01:00
|
|
|
|
|
|
|
merge = name->next_merged;
|
2013-09-16 13:49:26 +02:00
|
|
|
name = name->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return( (int) ( size - n ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Store the serial in printable form into buf; no more
|
|
|
|
* than size characters will be written
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_x509_serial_gets( char *buf, size_t size, const mbedtls_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;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = mbedtls_snprintf( p, n, "%02X%s",
|
2013-09-16 13:49:26 +02:00
|
|
|
serial->p[i], ( i < nr - 1 ) ? ":" : "" );
|
2015-06-22 11:12:02 +02:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if( nr != serial->len )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = mbedtls_snprintf( p, n, "...." );
|
2015-06-22 11:12:02 +02:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return( (int) ( size - n ) );
|
|
|
|
}
|
|
|
|
|
2014-01-25 11:50:59 +01:00
|
|
|
/*
|
2014-06-05 15:41:39 +02:00
|
|
|
* Helper for writing signature algorithms
|
2014-01-25 11:50:59 +01:00
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_x509_sig_alg_gets( char *buf, size_t size, const mbedtls_x509_buf *sig_oid,
|
|
|
|
mbedtls_pk_type_t pk_alg, mbedtls_md_type_t md_alg,
|
2014-06-05 15:41:39 +02:00
|
|
|
const void *sig_opts )
|
2014-01-25 11:50:59 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
char *p = buf;
|
|
|
|
size_t n = size;
|
|
|
|
const char *desc = NULL;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = mbedtls_oid_get_sig_alg_desc( sig_oid, &desc );
|
2014-01-25 11:50:59 +01:00
|
|
|
if( ret != 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = mbedtls_snprintf( p, n, "???" );
|
2014-01-25 11:50:59 +01:00
|
|
|
else
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = mbedtls_snprintf( p, n, "%s", desc );
|
2015-06-22 11:12:02 +02:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
2014-01-25 11:50:59 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
|
|
|
|
if( pk_alg == MBEDTLS_PK_RSASSA_PSS )
|
2014-01-25 11:50:59 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
const mbedtls_pk_rsassa_pss_options *pss_opts;
|
|
|
|
const mbedtls_md_info_t *md_info, *mgf_md_info;
|
2014-01-25 11:50:59 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
pss_opts = (const mbedtls_pk_rsassa_pss_options *) sig_opts;
|
2014-01-25 11:50:59 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
md_info = mbedtls_md_info_from_type( md_alg );
|
|
|
|
mgf_md_info = mbedtls_md_info_from_type( pss_opts->mgf1_hash_id );
|
2014-01-25 11:50:59 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = mbedtls_snprintf( p, n, " (%s, MGF1-%s, 0x%02X)",
|
|
|
|
md_info ? mbedtls_md_get_name( md_info ) : "???",
|
|
|
|
mgf_md_info ? mbedtls_md_get_name( mgf_md_info ) : "???",
|
2014-06-05 15:41:39 +02:00
|
|
|
pss_opts->expected_salt_len );
|
2015-06-22 11:12:02 +02:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
2014-01-25 11:50:59 +01:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
((void) pk_alg);
|
2014-06-05 15:41:39 +02:00
|
|
|
((void) md_alg);
|
|
|
|
((void) sig_opts);
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
|
2014-01-25 11:50:59 +01:00
|
|
|
|
2014-08-16 12:45:52 +02:00
|
|
|
return( (int)( size - n ) );
|
2014-01-25 11:50:59 +01:00
|
|
|
}
|
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
/*
|
|
|
|
* Helper for writing "RSA key size", "EC key size", etc
|
|
|
|
*/
|
2015-06-18 16:25:56 +02:00
|
|
|
int mbedtls_x509_key_size_helper( char *buf, size_t buf_size, const char *name )
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
|
|
|
char *p = buf;
|
2015-06-18 16:25:56 +02:00
|
|
|
size_t n = buf_size;
|
2013-09-16 13:49:26 +02:00
|
|
|
int ret;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = mbedtls_snprintf( p, n, "%s key size", name );
|
2015-06-22 11:12:02 +02:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2015-06-18 20:52:58 +02:00
|
|
|
#if defined(MBEDTLS_HAVE_TIME_DATE)
|
2015-06-22 18:59:21 +02:00
|
|
|
/*
|
|
|
|
* Set the time structure to the current time.
|
|
|
|
* Return 0 on success, non-zero on failure.
|
|
|
|
*/
|
|
|
|
#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
|
2015-05-29 10:11:03 +02:00
|
|
|
static int x509_get_current_time( mbedtls_x509_time *now )
|
2014-03-10 12:26:11 +01:00
|
|
|
{
|
2013-09-16 13:49:26 +02:00
|
|
|
SYSTEMTIME st;
|
|
|
|
|
2014-06-17 16:39:18 +02:00
|
|
|
GetSystemTime( &st );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2014-03-10 12:26:11 +01:00
|
|
|
now->year = st.wYear;
|
2015-05-29 10:11:03 +02:00
|
|
|
now->mon = st.wMonth;
|
|
|
|
now->day = st.wDay;
|
2014-03-10 12:26:11 +01:00
|
|
|
now->hour = st.wHour;
|
2015-05-29 10:11:03 +02:00
|
|
|
now->min = st.wMinute;
|
|
|
|
now->sec = st.wSecond;
|
2015-06-22 18:59:21 +02:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
2013-09-16 13:49:26 +02:00
|
|
|
#else
|
2015-06-22 18:59:21 +02:00
|
|
|
static int x509_get_current_time( mbedtls_x509_time *now )
|
|
|
|
{
|
2015-05-29 10:11:03 +02:00
|
|
|
struct tm *lt;
|
2013-09-16 13:49:26 +02:00
|
|
|
time_t tt;
|
2015-06-22 18:59:21 +02:00
|
|
|
int ret = 0;
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2015-05-29 10:11:03 +02:00
|
|
|
#if defined(MBEDTLS_THREADING_C)
|
|
|
|
if( mbedtls_mutex_lock( &mbedtls_threading_gmtime_mutex ) != 0 )
|
|
|
|
return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
|
|
|
|
#endif
|
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
tt = time( NULL );
|
2015-05-29 10:11:03 +02:00
|
|
|
lt = gmtime( &tt );
|
|
|
|
|
2015-06-22 18:59:21 +02:00
|
|
|
if( lt == NULL )
|
|
|
|
ret = -1;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2015-05-29 10:11:03 +02:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_THREADING_C)
|
|
|
|
if( mbedtls_mutex_unlock( &mbedtls_threading_gmtime_mutex ) != 0 )
|
|
|
|
return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
|
|
|
|
#endif
|
|
|
|
|
2015-06-22 18:59:21 +02:00
|
|
|
return( ret );
|
2014-03-10 12:26:11 +01:00
|
|
|
}
|
2015-06-22 18:59:21 +02:00
|
|
|
#endif /* _WIN32 && !EFIX64 && !EFI32 */
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2014-03-10 12:26:11 +01:00
|
|
|
/*
|
|
|
|
* Return 0 if before <= after, 1 otherwise
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
static int x509_check_time( const mbedtls_x509_time *before, const mbedtls_x509_time *after )
|
2014-03-10 12:26:11 +01:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2015-06-02 11:38:50 +02:00
|
|
|
int mbedtls_x509_time_is_past( const mbedtls_x509_time *to )
|
2014-03-10 12:26:11 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_time now;
|
2014-03-10 12:26:11 +01:00
|
|
|
|
2015-05-29 10:11:03 +02:00
|
|
|
if( x509_get_current_time( &now ) != 0 )
|
2015-06-22 19:15:32 +02:00
|
|
|
return( 1 );
|
2014-03-10 12:26:11 +01:00
|
|
|
|
|
|
|
return( x509_check_time( &now, to ) );
|
|
|
|
}
|
|
|
|
|
2015-06-02 11:38:50 +02:00
|
|
|
int mbedtls_x509_time_is_future( const mbedtls_x509_time *from )
|
2014-03-10 12:26:11 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_time now;
|
2014-03-10 12:26:11 +01:00
|
|
|
|
2015-05-29 10:11:03 +02:00
|
|
|
if( x509_get_current_time( &now ) != 0 )
|
2015-06-22 19:15:32 +02:00
|
|
|
return( 1 );
|
2014-03-10 12:26:11 +01:00
|
|
|
|
|
|
|
return( x509_check_time( from, &now ) );
|
|
|
|
}
|
|
|
|
|
2015-06-18 20:52:58 +02:00
|
|
|
#else /* MBEDTLS_HAVE_TIME_DATE */
|
2014-03-10 12:26:11 +01:00
|
|
|
|
2015-06-02 11:38:50 +02:00
|
|
|
int mbedtls_x509_time_is_past( const mbedtls_x509_time *to )
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
|
|
|
((void) to);
|
|
|
|
return( 0 );
|
|
|
|
}
|
2014-03-10 12:26:11 +01:00
|
|
|
|
2015-06-02 11:38:50 +02:00
|
|
|
int mbedtls_x509_time_is_future( const mbedtls_x509_time *from )
|
2014-03-10 12:26:11 +01:00
|
|
|
{
|
|
|
|
((void) from);
|
|
|
|
return( 0 );
|
|
|
|
}
|
2015-06-18 20:52:58 +02:00
|
|
|
#endif /* MBEDTLS_HAVE_TIME_DATE */
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SELF_TEST)
|
2013-09-16 22:53:25 +02:00
|
|
|
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/x509_crt.h"
|
|
|
|
#include "mbedtls/certs.h"
|
2013-09-16 22:53:25 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Checkup routine
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_x509_self_test( int verbose )
|
2013-09-16 22:53:25 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_CERTS_C) && defined(MBEDTLS_SHA1_C)
|
2013-09-16 22:53:25 +02:00
|
|
|
int ret;
|
2015-05-11 19:54:43 +02:00
|
|
|
uint32_t flags;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_crt cacert;
|
|
|
|
mbedtls_x509_crt clicert;
|
2013-09-16 22:53:25 +02:00
|
|
|
|
|
|
|
if( verbose != 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_printf( " X.509 certificate load: " );
|
2013-09-16 22:53:25 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_crt_init( &clicert );
|
2013-09-16 22:53:25 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = mbedtls_x509_crt_parse( &clicert, (const unsigned char *) mbedtls_test_cli_crt,
|
2015-05-12 11:20:10 +02:00
|
|
|
mbedtls_test_cli_crt_len );
|
2013-09-16 22:53:25 +02:00
|
|
|
if( ret != 0 )
|
|
|
|
{
|
|
|
|
if( verbose != 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_printf( "failed\n" );
|
2013-09-16 22:53:25 +02:00
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_crt_init( &cacert );
|
2013-09-16 22:53:25 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = mbedtls_x509_crt_parse( &cacert, (const unsigned char *) mbedtls_test_ca_crt,
|
2015-05-12 11:20:10 +02:00
|
|
|
mbedtls_test_ca_crt_len );
|
2013-09-16 22:53:25 +02:00
|
|
|
if( ret != 0 )
|
|
|
|
{
|
|
|
|
if( verbose != 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_printf( "failed\n" );
|
2013-09-16 22:53:25 +02:00
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( verbose != 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_printf( "passed\n X.509 signature verify: ");
|
2013-09-16 22:53:25 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = mbedtls_x509_crt_verify( &clicert, &cacert, NULL, NULL, &flags, NULL, NULL );
|
2013-09-16 22:53:25 +02:00
|
|
|
if( ret != 0 )
|
|
|
|
{
|
|
|
|
if( verbose != 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_printf( "failed\n" );
|
2013-09-16 22:53:25 +02:00
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( verbose != 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_printf( "passed\n\n");
|
2013-09-16 22:53:25 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_crt_free( &cacert );
|
|
|
|
mbedtls_x509_crt_free( &clicert );
|
2013-09-16 22:53:25 +02:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
#else
|
|
|
|
((void) verbose);
|
2015-08-07 10:56:09 +02:00
|
|
|
return( 0 );
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_CERTS_C && MBEDTLS_SHA1_C */
|
2013-09-16 22:53:25 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_SELF_TEST */
|
2013-09-16 22:53:25 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_X509_USE_C */
|