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-07-27 09:11:48 +00:00
|
|
|
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
2015-09-04 12:21:07 +00:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2013-09-16 11:49:26 +00:00
|
|
|
*
|
2015-09-04 12:21:07 +00: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 11:49:26 +00:00
|
|
|
*
|
2015-09-04 12:21:07 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2013-09-16 11:49:26 +00:00
|
|
|
*
|
2015-09-04 12:21:07 +00: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 11:49:26 +00:00
|
|
|
*
|
2015-09-04 12:21:07 +00:00
|
|
|
* This file is part of mbed TLS (https://tls.mbed.org)
|
2013-09-16 11:49:26 +00:00
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if !defined(MBEDTLS_CONFIG_FILE)
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/config.h"
|
2014-04-29 10:39:06 +00:00
|
|
|
#else
|
2015-04-08 10:49:31 +00:00
|
|
|
#include MBEDTLS_CONFIG_FILE
|
2014-04-29 10:39:06 +00:00
|
|
|
#endif
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_X509_USE_C)
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/x509.h"
|
2019-05-02 12:05:58 +00:00
|
|
|
#include "mbedtls/x509_internal.h"
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/asn1.h"
|
|
|
|
#include "mbedtls/oid.h"
|
2015-02-06 13:43:58 +00:00
|
|
|
|
2019-09-03 13:50:17 +00:00
|
|
|
/* We include x509xxx.c files here so that x509.c is one compilation unit including
|
|
|
|
* all the x509 files. This is done because some of the internal functions are shared.
|
|
|
|
* For code size savings internal functions should be static so that compiler can do better job
|
|
|
|
* when optimizing. We don't wan't x509.c file to get too big so including .c files.
|
|
|
|
*/
|
2019-09-03 09:33:16 +00:00
|
|
|
#include "x509_crl.c"
|
|
|
|
#include "x509_crt.c"
|
|
|
|
#include "x509_csr.c"
|
|
|
|
#include "x509_create.c"
|
|
|
|
#include "x509write_crt.c"
|
|
|
|
#include "x509write_csr.c"
|
|
|
|
|
2015-02-12 18:27:14 +00:00
|
|
|
#include <stdio.h>
|
2015-02-06 13:43:58 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_PEM_PARSE_C)
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/pem.h"
|
2013-09-16 11:49:26 +00:00
|
|
|
#endif
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_PLATFORM_C)
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/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>
|
2016-04-26 06:43:27 +00:00
|
|
|
#define mbedtls_free free
|
2015-05-26 14:04:06 +00:00
|
|
|
#define mbedtls_calloc calloc
|
2016-04-26 06:43:27 +00:00
|
|
|
#define mbedtls_printf printf
|
|
|
|
#define mbedtls_snprintf snprintf
|
2013-09-16 11:49:26 +00:00
|
|
|
#endif
|
|
|
|
|
2016-07-13 13:46:18 +00:00
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
|
|
|
#include "mbedtls/platform_time.h"
|
|
|
|
#endif
|
2017-12-05 12:07:33 +00:00
|
|
|
#if defined(MBEDTLS_HAVE_TIME_DATE)
|
2018-08-16 20:42:09 +00:00
|
|
|
#include "mbedtls/platform_util.h"
|
2013-09-16 11:49:26 +00:00
|
|
|
#include <time.h>
|
|
|
|
#endif
|
|
|
|
|
2018-10-15 11:01:35 +00:00
|
|
|
#define CHECK(code) if( ( ret = ( code ) ) != 0 ){ return( ret ); }
|
|
|
|
#define CHECK_RANGE(min, max, val) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
if( ( val ) < ( min ) || ( val ) > ( max ) ) \
|
|
|
|
{ \
|
|
|
|
return( ret ); \
|
|
|
|
} \
|
|
|
|
} while( 0 )
|
2015-02-13 11:48:02 +00:00
|
|
|
|
2013-09-16 11:49:26 +00:00
|
|
|
/*
|
|
|
|
* CertificateSerialNumber ::= INTEGER
|
|
|
|
*/
|
2019-09-03 13:50:17 +00:00
|
|
|
static int mbedtls_x509_get_serial( unsigned char **p, const unsigned char *end,
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_buf *serial )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if( ( end - *p ) < 1 )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_X509_INVALID_SERIAL +
|
|
|
|
MBEDTLS_ERR_ASN1_OUT_OF_DATA );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2015-04-08 10:49:31 +00: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 11:49:26 +00:00
|
|
|
|
|
|
|
serial->tag = *(*p)++;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ret = mbedtls_asn1_get_len( p, end, &serial->len ) ) != 0 )
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_SERIAL + ret );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
|
|
|
serial->p = *p;
|
|
|
|
*p += serial->len;
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2019-09-03 13:50:17 +00:00
|
|
|
#if defined(MBEDTLS_X509_CRL_PARSE_C) || defined(MBEDTLS_X509_CSR_PARSE_C) || \
|
|
|
|
( !defined(MBEDTLS_X509_ON_DEMAND_PARSING) && defined(MBEDTLS_X509_CRT_PARSE_C) )
|
|
|
|
/*
|
|
|
|
* Parse an algorithm identifier with (optional) parameters
|
2013-09-16 11:49:26 +00:00
|
|
|
*/
|
2019-09-03 13:50:17 +00:00
|
|
|
static int mbedtls_x509_get_alg( unsigned char **p, const unsigned char *end,
|
|
|
|
mbedtls_x509_buf *alg, mbedtls_x509_buf *params )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2019-09-03 13:50:17 +00:00
|
|
|
if( ( ret = mbedtls_asn1_get_alg( p, end, alg, params ) ) != 0 )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
2019-09-03 13:50:17 +00:00
|
|
|
#endif /* defined(MBEDTLS_X509_CRL_PARSE_C) || defined(MBEDTLS_X509_CSR_PARSE_C) ||
|
|
|
|
( !defined(MBEDTLS_X509_ON_DEMAND_PARSING) && defined(MBEDTLS_X509_CRT_PARSE_C) ) */
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2019-09-03 13:50:17 +00:00
|
|
|
#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
|
|
|
|
/* Get an algorithm identifier without parameters (eg for signatures)
|
|
|
|
*
|
|
|
|
* AlgorithmIdentifier ::= SEQUENCE {
|
|
|
|
* algorithm OBJECT IDENTIFIER,
|
|
|
|
* parameters ANY DEFINED BY algorithm OPTIONAL }
|
2014-01-22 09:12:57 +00:00
|
|
|
*/
|
2019-09-03 13:50:17 +00:00
|
|
|
static int mbedtls_x509_get_alg_null( unsigned char **p, const unsigned char *end,
|
|
|
|
mbedtls_x509_buf *alg )
|
2014-01-22 09:12:57 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2019-09-03 13:50:17 +00:00
|
|
|
if( ( ret = mbedtls_asn1_get_alg_null( p, end, alg ) ) != 0 )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
|
2014-01-22 09:12:57 +00:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
static int x509_get_hash_alg( const mbedtls_x509_buf *alg, mbedtls_md_type_t *md_alg )
|
2014-01-23 18:15:29 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
unsigned char *p;
|
|
|
|
const unsigned char *end;
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_buf md_oid;
|
2014-01-23 18:15:29 +00:00
|
|
|
size_t len;
|
|
|
|
|
|
|
|
/* Make sure we got a SEQUENCE and setup bounds */
|
2015-04-08 10:49:31 +00:00
|
|
|
if( alg->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_ALG +
|
|
|
|
MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
|
2014-01-23 18:15:29 +00:00
|
|
|
|
|
|
|
p = (unsigned char *) alg->p;
|
|
|
|
end = p + alg->len;
|
|
|
|
|
|
|
|
if( p >= end )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_X509_INVALID_ALG +
|
|
|
|
MBEDTLS_ERR_ASN1_OUT_OF_DATA );
|
2014-01-23 18:15:29 +00:00
|
|
|
|
|
|
|
/* Parse md_oid */
|
|
|
|
md_oid.tag = *p;
|
|
|
|
|
2015-04-08 10:49:31 +00: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 18:15:29 +00:00
|
|
|
|
|
|
|
md_oid.p = p;
|
|
|
|
p += md_oid.len;
|
|
|
|
|
|
|
|
/* Get md_alg from md_oid */
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ret = mbedtls_oid_get_md_alg( &md_oid, md_alg ) ) != 0 )
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
|
2014-01-23 18:15:29 +00:00
|
|
|
|
|
|
|
/* Make sure params is absent of NULL */
|
|
|
|
if( p == end )
|
|
|
|
return( 0 );
|
|
|
|
|
2015-04-08 10:49:31 +00: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 18:15:29 +00:00
|
|
|
|
|
|
|
if( p != end )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_X509_INVALID_ALG +
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
|
2014-01-23 18:15:29 +00:00
|
|
|
|
|
|
|
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
|
|
|
*/
|
2019-09-03 13:50:17 +00:00
|
|
|
static int mbedtls_x509_get_rsassa_pss_params( const mbedtls_x509_buf *params,
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_md_type_t *md_alg, mbedtls_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;
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_buf alg_id, alg_params;
|
2014-01-23 15:24:44 +00:00
|
|
|
|
|
|
|
/* First set everything to defaults */
|
2015-04-08 10:49:31 +00:00
|
|
|
*md_alg = MBEDTLS_MD_SHA1;
|
|
|
|
*mgf_md = MBEDTLS_MD_SHA1;
|
2014-01-23 15:24:44 +00:00
|
|
|
*salt_len = 20;
|
|
|
|
|
|
|
|
/* Make sure params is a SEQUENCE and setup bounds */
|
2015-04-08 10:49:31 +00:00
|
|
|
if( params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_ALG +
|
|
|
|
MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
|
2014-01-23 15:24:44 +00:00
|
|
|
|
|
|
|
p = (unsigned char *) params->p;
|
|
|
|
end = p + params->len;
|
|
|
|
|
|
|
|
if( p == end )
|
|
|
|
return( 0 );
|
|
|
|
|
2014-01-24 13:15:20 +00:00
|
|
|
/*
|
|
|
|
* HashAlgorithm
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
|
|
|
|
MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
|
2014-01-23 15:24:44 +00:00
|
|
|
{
|
2014-01-24 13:15:20 +00:00
|
|
|
end2 = p + len;
|
|
|
|
|
2014-01-23 18:15:29 +00:00
|
|
|
/* HashAlgorithm ::= AlgorithmIdentifier (without parameters) */
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ret = mbedtls_x509_get_alg_null( &p, end2, &alg_id ) ) != 0 )
|
2014-01-23 18:15:29 +00:00
|
|
|
return( ret );
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ret = mbedtls_oid_get_md_alg( &alg_id, md_alg ) ) != 0 )
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
|
2014-01-24 13:15:20 +00:00
|
|
|
|
|
|
|
if( p != end2 )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_X509_INVALID_ALG +
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
|
2014-01-23 15:24:44 +00:00
|
|
|
}
|
2015-04-08 10:49:31 +00:00
|
|
|
else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
|
2014-01-23 15:24:44 +00:00
|
|
|
|
2014-01-24 13:15:20 +00:00
|
|
|
if( p == end )
|
|
|
|
return( 0 );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* MaskGenAlgorithm
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
|
|
|
|
MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
|
2014-01-23 15:24:44 +00:00
|
|
|
{
|
2014-01-24 13:15:20 +00:00
|
|
|
end2 = p + len;
|
|
|
|
|
2014-01-23 18:15:29 +00:00
|
|
|
/* MaskGenAlgorithm ::= AlgorithmIdentifier (params = HashAlgorithm) */
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ret = mbedtls_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 */
|
2015-04-08 10:49:31 +00: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 18:15:29 +00:00
|
|
|
|
|
|
|
/* 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 )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_X509_INVALID_ALG +
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
|
2014-01-23 15:24:44 +00:00
|
|
|
}
|
2015-04-08 10:49:31 +00:00
|
|
|
else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
|
2014-01-23 15:24:44 +00:00
|
|
|
|
|
|
|
if( p == end )
|
|
|
|
return( 0 );
|
|
|
|
|
2014-01-24 13:15:20 +00:00
|
|
|
/*
|
|
|
|
* salt_len
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
|
|
|
|
MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 2 ) ) == 0 )
|
2014-01-23 15:24:44 +00:00
|
|
|
{
|
2014-01-24 13:15:20 +00:00
|
|
|
end2 = p + len;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ret = mbedtls_asn1_get_int( &p, end2, salt_len ) ) != 0 )
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
|
2014-01-24 13:15:20 +00:00
|
|
|
|
|
|
|
if( p != end2 )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_X509_INVALID_ALG +
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
|
2014-01-23 15:24:44 +00:00
|
|
|
}
|
2015-04-08 10:49:31 +00:00
|
|
|
else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
|
2014-01-23 15:24:44 +00:00
|
|
|
|
|
|
|
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
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
|
|
|
|
MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 3 ) ) == 0 )
|
2014-01-23 15:24:44 +00:00
|
|
|
{
|
2014-05-31 15:08:16 +00:00
|
|
|
int trailer_field;
|
|
|
|
|
2014-01-24 13:15:20 +00:00
|
|
|
end2 = p + len;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ret = mbedtls_asn1_get_int( &p, end2, &trailer_field ) ) != 0 )
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
|
2014-01-24 13:15:20 +00:00
|
|
|
|
|
|
|
if( p != end2 )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_X509_INVALID_ALG +
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
|
2014-05-31 15:08:16 +00:00
|
|
|
|
|
|
|
if( trailer_field != 1 )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_X509_INVALID_ALG );
|
2014-01-23 15:24:44 +00:00
|
|
|
}
|
2015-04-08 10:49:31 +00:00
|
|
|
else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
|
2014-01-23 15:24:44 +00:00
|
|
|
|
|
|
|
if( p != end )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_X509_INVALID_ALG +
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
|
2014-01-23 15:24:44 +00:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_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
|
2019-05-02 12:19:34 +00:00
|
|
|
*
|
|
|
|
* NOTE: This function returns an ASN.1 low-level error code.
|
2013-09-16 11:49:26 +00:00
|
|
|
*/
|
|
|
|
static int x509_get_attr_type_value( unsigned char **p,
|
|
|
|
const unsigned char *end,
|
2019-02-20 09:45:17 +00:00
|
|
|
mbedtls_x509_buf *oid,
|
|
|
|
mbedtls_x509_buf *val )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t len;
|
|
|
|
|
2019-02-20 11:30:29 +00:00
|
|
|
ret = mbedtls_asn1_get_tag( p, end, &len,
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED |
|
|
|
|
MBEDTLS_ASN1_SEQUENCE );
|
|
|
|
if( ret != 0 )
|
|
|
|
goto exit;
|
2013-09-16 11:49:26 +00:00
|
|
|
|
Obey bounds of ASN.1 substructures
When parsing a substructure of an ASN.1 structure, no field within
the substructure must exceed the bounds of the substructure.
Concretely, the `end` pointer passed to the ASN.1 parsing routines
must be updated to point to the end of the substructure while parsing
the latter.
This was previously not the case for the routines
- x509_get_attr_type_and_value(),
- mbedtls_x509_get_crt_ext(),
- mbedtls_x509_get_crl_ext().
These functions kept using the end of the parent structure as the
`end` pointer and would hence allow substructure fields to cross
the substructure boundary. This could lead to successful parsing
of ill-formed X.509 CRTs.
This commit fixes this.
Care has to be taken when adapting `mbedtls_x509_get_crt_ext()`
and `mbedtls_x509_get_crl_ext()`, as the underlying function
`mbedtls_x509_get_ext()` returns `0` if no extensions are present
but doesn't set the variable which holds the bounds of the Extensions
structure in case the latter is present. This commit addresses
this by returning early from `mbedtls_x509_get_crt_ext()` and
`mbedtls_x509_get_crl_ext()` if parsing has reached the end of
the input buffer.
The following X.509 parsing tests need to be adapted:
- "TBSCertificate, issuer two inner set datas"
This test exercises the X.509 CRT parser with a Subject name
which has two empty `AttributeTypeAndValue` structures.
This is supposed to fail with `MBEDTLS_ERR_ASN1_OUT_OF_DATA`
because the parser should attempt to parse the first structure
and fail because of a lack of data. Previously, it failed to
obey the (0-length) bounds of the first AttributeTypeAndValue
structure and would try to interpret the beginning of the second
AttributeTypeAndValue structure as the first field of the first
AttributeTypeAndValue structure, returning an UNEXPECTED_TAG error.
- "TBSCertificate, issuer, no full following string"
This test exercises the parser's behaviour on an AttributeTypeAndValue
structure which contains more data than expected; it should therefore
fail with MBEDTLS_ERR_ASN1_LENGTH_MISMATCH. Because of the missing bounds
check, it previously failed with UNEXPECTED_TAG because it interpreted
the remaining byte in the first AttributeTypeAndValue structure as the
first byte in the second AttributeTypeAndValue structure.
- "SubjectAltName repeated"
This test should exercise two SubjectAltNames extensions in succession,
but a wrong length values makes the second SubjectAltNames extension appear
outside of the Extensions structure. With the new bounds in place, this
therefore fails with a LENGTH_MISMATCH error. This commit adapts the test
data to put the 2nd SubjectAltNames extension inside the Extensions
structure, too.
2019-02-12 17:22:36 +00:00
|
|
|
end = *p + len;
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2019-02-20 09:45:17 +00:00
|
|
|
ret = mbedtls_asn1_get_tag( p, end, &oid->len, MBEDTLS_ASN1_OID );
|
|
|
|
if( ret != 0 )
|
2019-02-20 11:30:29 +00:00
|
|
|
goto exit;
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2019-02-20 09:35:34 +00:00
|
|
|
oid->tag = MBEDTLS_ASN1_OID;
|
2013-09-16 11:49:26 +00:00
|
|
|
oid->p = *p;
|
|
|
|
*p += oid->len;
|
|
|
|
|
2019-02-20 09:45:17 +00:00
|
|
|
if( *p == end )
|
|
|
|
{
|
2019-02-20 11:30:29 +00:00
|
|
|
ret = MBEDTLS_ERR_ASN1_OUT_OF_DATA;
|
|
|
|
goto exit;
|
2019-02-20 09:45:17 +00:00
|
|
|
}
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2019-02-20 09:38:45 +00:00
|
|
|
if( !MBEDTLS_ASN1_IS_STRING_TAG( **p ) )
|
|
|
|
{
|
2019-02-20 11:30:29 +00:00
|
|
|
ret = MBEDTLS_ERR_ASN1_UNEXPECTED_TAG;
|
|
|
|
goto exit;
|
2019-02-20 09:38:45 +00:00
|
|
|
}
|
2013-09-16 11:49:26 +00:00
|
|
|
|
|
|
|
val->tag = *(*p)++;
|
|
|
|
|
2019-02-20 11:30:29 +00:00
|
|
|
ret = mbedtls_asn1_get_len( p, end, &val->len );
|
|
|
|
if( ret != 0 )
|
|
|
|
goto exit;
|
2013-09-16 11:49:26 +00:00
|
|
|
|
|
|
|
val->p = *p;
|
|
|
|
*p += val->len;
|
|
|
|
|
Obey bounds of ASN.1 substructures
When parsing a substructure of an ASN.1 structure, no field within
the substructure must exceed the bounds of the substructure.
Concretely, the `end` pointer passed to the ASN.1 parsing routines
must be updated to point to the end of the substructure while parsing
the latter.
This was previously not the case for the routines
- x509_get_attr_type_and_value(),
- mbedtls_x509_get_crt_ext(),
- mbedtls_x509_get_crl_ext().
These functions kept using the end of the parent structure as the
`end` pointer and would hence allow substructure fields to cross
the substructure boundary. This could lead to successful parsing
of ill-formed X.509 CRTs.
This commit fixes this.
Care has to be taken when adapting `mbedtls_x509_get_crt_ext()`
and `mbedtls_x509_get_crl_ext()`, as the underlying function
`mbedtls_x509_get_ext()` returns `0` if no extensions are present
but doesn't set the variable which holds the bounds of the Extensions
structure in case the latter is present. This commit addresses
this by returning early from `mbedtls_x509_get_crt_ext()` and
`mbedtls_x509_get_crl_ext()` if parsing has reached the end of
the input buffer.
The following X.509 parsing tests need to be adapted:
- "TBSCertificate, issuer two inner set datas"
This test exercises the X.509 CRT parser with a Subject name
which has two empty `AttributeTypeAndValue` structures.
This is supposed to fail with `MBEDTLS_ERR_ASN1_OUT_OF_DATA`
because the parser should attempt to parse the first structure
and fail because of a lack of data. Previously, it failed to
obey the (0-length) bounds of the first AttributeTypeAndValue
structure and would try to interpret the beginning of the second
AttributeTypeAndValue structure as the first field of the first
AttributeTypeAndValue structure, returning an UNEXPECTED_TAG error.
- "TBSCertificate, issuer, no full following string"
This test exercises the parser's behaviour on an AttributeTypeAndValue
structure which contains more data than expected; it should therefore
fail with MBEDTLS_ERR_ASN1_LENGTH_MISMATCH. Because of the missing bounds
check, it previously failed with UNEXPECTED_TAG because it interpreted
the remaining byte in the first AttributeTypeAndValue structure as the
first byte in the second AttributeTypeAndValue structure.
- "SubjectAltName repeated"
This test should exercise two SubjectAltNames extensions in succession,
but a wrong length values makes the second SubjectAltNames extension appear
outside of the Extensions structure. With the new bounds in place, this
therefore fails with a LENGTH_MISMATCH error. This commit adapts the test
data to put the 2nd SubjectAltNames extension inside the Extensions
structure, too.
2019-02-12 17:22:36 +00:00
|
|
|
if( *p != end )
|
2019-02-20 11:30:29 +00:00
|
|
|
ret = MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
|
|
|
|
|
|
|
|
exit:
|
|
|
|
return( ret );
|
2013-09-16 11:49:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
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
|
2015-04-08 10:49:31 +00:00
|
|
|
* this list, eg mbedtls_x509_dn_gets().
|
2019-05-02 12:19:34 +00:00
|
|
|
*
|
|
|
|
* NOTE: This function returns an ASN.1 low-level error code.
|
2013-09-16 11:49:26 +00:00
|
|
|
*/
|
2019-02-20 09:45:17 +00:00
|
|
|
static int x509_set_sequence_iterate( unsigned char **p,
|
|
|
|
unsigned char const **end_set,
|
|
|
|
unsigned char const *end,
|
|
|
|
mbedtls_x509_buf *oid,
|
|
|
|
mbedtls_x509_buf *val )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
|
|
|
int ret;
|
2014-10-17 10:41:41 +00:00
|
|
|
size_t set_len;
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2019-02-20 09:45:17 +00:00
|
|
|
if( *p == *end_set )
|
2014-11-12 00:25:31 +00:00
|
|
|
{
|
2019-02-20 09:45:17 +00:00
|
|
|
/* Parse next TLV of ASN.1 SET structure. */
|
|
|
|
ret = mbedtls_asn1_get_tag( p, end, &set_len,
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED |
|
|
|
|
MBEDTLS_ASN1_SET );
|
|
|
|
if( ret != 0 )
|
2019-02-20 11:30:29 +00:00
|
|
|
goto exit;
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2019-02-20 09:45:17 +00:00
|
|
|
*end_set = *p + set_len;
|
|
|
|
}
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2019-05-02 12:19:34 +00:00
|
|
|
/* x509_get_attr_type_value() returns ASN.1 low-level error codes. */
|
2019-02-20 11:30:29 +00:00
|
|
|
ret = x509_get_attr_type_value( p, *end_set, oid, val );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
return( ret );
|
2019-02-20 09:45:17 +00:00
|
|
|
}
|
2015-02-04 17:11:55 +00:00
|
|
|
|
2019-02-20 12:41:55 +00:00
|
|
|
/*
|
|
|
|
* Like memcmp, but case-insensitive and always returns -1 if different
|
|
|
|
*/
|
2019-09-03 13:50:17 +00:00
|
|
|
static int mbedtls_x509_memcasecmp( const void *s1, const void *s2,
|
2019-02-22 11:46:06 +00:00
|
|
|
size_t len1, size_t len2 )
|
2019-02-20 12:41:55 +00:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
unsigned char diff;
|
|
|
|
const unsigned char *n1 = s1, *n2 = s2;
|
|
|
|
|
2019-02-22 11:46:06 +00:00
|
|
|
if( len1 != len2 )
|
|
|
|
return( -1 );
|
|
|
|
|
|
|
|
for( i = 0; i < len1; i++ )
|
2019-02-20 12:41:55 +00:00
|
|
|
{
|
|
|
|
diff = n1[i] ^ n2[i];
|
|
|
|
|
|
|
|
if( diff == 0 )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if( diff == 32 &&
|
|
|
|
( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
|
|
|
|
( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compare two X.509 strings, case-insensitive, and allowing for some encoding
|
|
|
|
* variations (but not all).
|
|
|
|
*
|
|
|
|
* Return 0 if equal, -1 otherwise.
|
|
|
|
*/
|
|
|
|
static int x509_string_cmp( const mbedtls_x509_buf *a,
|
|
|
|
const mbedtls_x509_buf *b )
|
|
|
|
{
|
|
|
|
if( a->tag == b->tag &&
|
|
|
|
a->len == b->len &&
|
|
|
|
memcmp( a->p, b->p, b->len ) == 0 )
|
|
|
|
{
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ( a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
|
|
|
|
( b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
|
2019-02-22 11:46:06 +00:00
|
|
|
mbedtls_x509_memcasecmp( a->p, b->p,
|
|
|
|
a->len, b->len ) == 0 )
|
2019-02-20 12:41:55 +00:00
|
|
|
{
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2019-02-21 13:58:38 +00:00
|
|
|
* Compare two X.509 Names (aka rdnSequence) given as raw ASN.1 data.
|
2019-02-20 12:41:55 +00:00
|
|
|
*
|
|
|
|
* See RFC 5280 section 7.1, though we don't implement the whole algorithm:
|
2019-02-21 13:58:38 +00:00
|
|
|
* We sometimes return unequal when the full algorithm would return equal,
|
2019-02-20 12:41:55 +00:00
|
|
|
* but never the other way. (In particular, we don't do Unicode normalisation
|
|
|
|
* or space folding.)
|
|
|
|
*
|
2019-02-21 14:31:51 +00:00
|
|
|
* Further, this function allows to pass a callback to be triggered for every
|
|
|
|
* pair of well-formed and equal entries in the two input name lists.
|
|
|
|
*
|
2019-02-21 13:58:38 +00:00
|
|
|
* Returns:
|
2019-02-21 14:31:51 +00:00
|
|
|
* - 0 if both sequences are well-formed, present the same X.509 name,
|
|
|
|
* and the callback (if provided) hasn't returned a non-zero value
|
|
|
|
* on any of the name components.
|
|
|
|
* - 1 if a difference was detected in the name components.
|
|
|
|
* - A non-zero error code if the abort callback returns a non-zero value.
|
|
|
|
* In this case, the returned error code is the error code from the callback.
|
2019-02-21 13:58:38 +00:00
|
|
|
* - A negative error code if a parsing error occurred in either
|
|
|
|
* of the two buffers.
|
|
|
|
*
|
|
|
|
* This function can be used to verify that a buffer contains a well-formed
|
|
|
|
* ASN.1 encoded X.509 name by calling it with equal parameters.
|
2019-02-20 12:41:55 +00:00
|
|
|
*/
|
2019-09-03 13:50:17 +00:00
|
|
|
static int mbedtls_x509_name_cmp_raw( mbedtls_x509_buf_raw const *a,
|
2019-02-21 14:31:51 +00:00
|
|
|
mbedtls_x509_buf_raw const *b,
|
|
|
|
int (*abort_check)( void *ctx,
|
2019-05-02 12:17:29 +00:00
|
|
|
mbedtls_x509_buf *oid,
|
|
|
|
mbedtls_x509_buf *val,
|
|
|
|
int next_merged ),
|
2019-02-21 14:31:51 +00:00
|
|
|
void *abort_check_ctx )
|
2019-02-20 12:42:07 +00:00
|
|
|
{
|
|
|
|
int ret;
|
2019-03-04 14:43:43 +00:00
|
|
|
size_t idx;
|
|
|
|
unsigned char *p[2], *end[2], *set[2];
|
2019-02-20 12:42:07 +00:00
|
|
|
|
2019-03-04 14:43:43 +00:00
|
|
|
p[0] = a->p;
|
|
|
|
p[1] = b->p;
|
|
|
|
end[0] = p[0] + a->len;
|
|
|
|
end[1] = p[1] + b->len;
|
2019-02-20 12:42:07 +00:00
|
|
|
|
2019-03-04 14:43:43 +00:00
|
|
|
for( idx = 0; idx < 2; idx++ )
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
ret = mbedtls_asn1_get_tag( &p[idx], end[idx], &len,
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED |
|
|
|
|
MBEDTLS_ASN1_SEQUENCE );
|
2019-02-20 12:42:07 +00:00
|
|
|
|
2019-03-04 14:43:43 +00:00
|
|
|
if( end[idx] != p[idx] + len )
|
|
|
|
{
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_NAME +
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
|
|
|
|
}
|
|
|
|
|
|
|
|
set[idx] = p[idx];
|
|
|
|
}
|
2019-02-20 12:42:07 +00:00
|
|
|
|
|
|
|
while( 1 )
|
|
|
|
{
|
2019-02-23 10:20:14 +00:00
|
|
|
int next_merged;
|
2019-03-04 14:43:43 +00:00
|
|
|
mbedtls_x509_buf oid[2], val[2];
|
2019-02-20 12:42:07 +00:00
|
|
|
|
2019-03-04 14:43:43 +00:00
|
|
|
ret = x509_set_sequence_iterate( &p[0], (const unsigned char **) &set[0],
|
|
|
|
end[0], &oid[0], &val[0] );
|
2019-02-20 12:42:07 +00:00
|
|
|
if( ret != 0 )
|
|
|
|
goto exit;
|
|
|
|
|
2019-03-04 14:43:43 +00:00
|
|
|
ret = x509_set_sequence_iterate( &p[1], (const unsigned char **) &set[1],
|
|
|
|
end[1], &oid[1], &val[1] );
|
2019-02-20 12:42:07 +00:00
|
|
|
if( ret != 0 )
|
|
|
|
goto exit;
|
|
|
|
|
2019-03-04 14:43:43 +00:00
|
|
|
if( oid[0].len != oid[1].len ||
|
|
|
|
memcmp( oid[0].p, oid[1].p, oid[1].len ) != 0 )
|
2019-02-20 12:42:07 +00:00
|
|
|
{
|
|
|
|
return( 1 );
|
|
|
|
}
|
|
|
|
|
2019-03-04 14:43:43 +00:00
|
|
|
if( x509_string_cmp( &val[0], &val[1] ) != 0 )
|
2019-02-20 12:42:07 +00:00
|
|
|
return( 1 );
|
|
|
|
|
2019-03-04 14:43:43 +00:00
|
|
|
next_merged = ( set[0] != p[0] );
|
|
|
|
if( next_merged != ( set[1] != p[1] ) )
|
2019-02-20 12:42:07 +00:00
|
|
|
return( 1 );
|
|
|
|
|
2019-02-21 14:31:51 +00:00
|
|
|
if( abort_check != NULL )
|
|
|
|
{
|
2019-03-04 14:43:43 +00:00
|
|
|
ret = abort_check( abort_check_ctx, &oid[0], &val[0],
|
2019-02-23 10:20:14 +00:00
|
|
|
next_merged );
|
2019-02-21 14:31:51 +00:00
|
|
|
if( ret != 0 )
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2019-03-04 14:43:43 +00:00
|
|
|
if( p[0] == end[0] && p[1] == end[1] )
|
2019-02-20 12:42:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
if( ret < 0 )
|
|
|
|
ret += MBEDTLS_ERR_X509_INVALID_NAME;
|
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2019-02-23 09:13:17 +00:00
|
|
|
static int x509_get_name_cb( void *ctx,
|
|
|
|
mbedtls_x509_buf *oid,
|
|
|
|
mbedtls_x509_buf *val,
|
|
|
|
int next_merged )
|
2019-02-23 10:20:14 +00:00
|
|
|
{
|
2019-02-23 09:13:17 +00:00
|
|
|
mbedtls_x509_name **cur_ptr = (mbedtls_x509_name**) ctx;
|
|
|
|
mbedtls_x509_name *cur = *cur_ptr;
|
2019-02-23 10:20:14 +00:00
|
|
|
|
2019-02-23 09:13:17 +00:00
|
|
|
if( cur->oid.p != NULL )
|
2019-02-23 10:20:14 +00:00
|
|
|
{
|
|
|
|
cur->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) );
|
|
|
|
if( cur->next == NULL )
|
2019-02-23 09:13:17 +00:00
|
|
|
return( MBEDTLS_ERR_ASN1_ALLOC_FAILED );
|
2019-02-23 10:20:14 +00:00
|
|
|
|
|
|
|
cur = cur->next;
|
|
|
|
}
|
|
|
|
|
2019-02-23 09:13:17 +00:00
|
|
|
cur->oid = *oid;
|
|
|
|
cur->val = *val;
|
|
|
|
cur->next_merged = next_merged;
|
|
|
|
|
|
|
|
*cur_ptr = cur;
|
2019-02-23 10:20:14 +00:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2019-09-03 13:50:17 +00:00
|
|
|
static int mbedtls_x509_get_name( unsigned char *p,
|
2019-03-04 14:43:43 +00:00
|
|
|
size_t len,
|
2019-02-23 09:13:17 +00:00
|
|
|
mbedtls_x509_name *cur )
|
|
|
|
{
|
2019-03-04 14:43:43 +00:00
|
|
|
mbedtls_x509_buf_raw name_buf = { p, len };
|
2019-02-23 09:13:17 +00:00
|
|
|
memset( cur, 0, sizeof( mbedtls_x509_name ) );
|
2019-03-04 14:43:43 +00:00
|
|
|
return( mbedtls_x509_name_cmp_raw( &name_buf, &name_buf,
|
|
|
|
x509_get_name_cb,
|
|
|
|
&cur ) );
|
2019-02-23 09:13:17 +00:00
|
|
|
}
|
2019-02-23 10:20:14 +00:00
|
|
|
|
2019-09-03 13:50:17 +00:00
|
|
|
#if ( !defined(MBEDTLS_X509_CRT_REMOVE_TIME) && defined(MBEDTLS_X509_CRT_PARSE_C) ) || \
|
|
|
|
defined(MBEDTLS_X509_CRL_PARSE_C)
|
2017-02-03 12:36:59 +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-02-03 12:36:59 +00:00
|
|
|
|
|
|
|
for( ; n > 0; --n )
|
|
|
|
{
|
|
|
|
if( ( **p < '0') || ( **p > '9' ) )
|
|
|
|
return ( MBEDTLS_ERR_X509_INVALID_DATE );
|
|
|
|
|
2015-02-13 11:48:02 +00:00
|
|
|
*res *= 10;
|
2017-02-03 12:36:59 +00:00
|
|
|
*res += ( *(*p)++ - '0' );
|
2015-02-13 11:48:02 +00:00
|
|
|
}
|
2017-02-03 12:36:59 +00:00
|
|
|
|
|
|
|
return( 0 );
|
2015-02-13 11:48:02 +00:00
|
|
|
}
|
|
|
|
|
2016-11-21 15:38:02 +00:00
|
|
|
static int x509_date_is_valid(const mbedtls_x509_time *t )
|
2016-09-23 12:16:02 +00:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_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 );
|
|
|
|
}
|
|
|
|
|
2017-02-03 12:36:59 +00:00
|
|
|
/*
|
|
|
|
* Parse an ASN1_UTC_TIME (yearlen=2) or ASN1_GENERALIZED_TIME (yearlen=4)
|
|
|
|
* field.
|
|
|
|
*/
|
|
|
|
static int x509_parse_time( unsigned char **p, size_t len, size_t yearlen,
|
2017-04-26 14:01:23 +00:00
|
|
|
mbedtls_x509_time *tm )
|
2017-02-03 12:36:59 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Minimum length is 10 or 12 depending on yearlen
|
|
|
|
*/
|
|
|
|
if ( len < yearlen + 8 )
|
|
|
|
return ( MBEDTLS_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 ) );
|
2017-02-03 12:36:59 +00:00
|
|
|
if ( 2 == yearlen )
|
|
|
|
{
|
2017-04-26 14:01:23 +00:00
|
|
|
if ( tm->year < 50 )
|
|
|
|
tm->year += 100;
|
2017-02-03 12:36:59 +00:00
|
|
|
|
2017-04-26 14:01:23 +00:00
|
|
|
tm->year += 1900;
|
2017-02-03 12:36:59 +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 ) );
|
2017-02-03 12:36:59 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse seconds if present
|
|
|
|
*/
|
|
|
|
if ( len >= 2 )
|
|
|
|
{
|
2017-04-26 14:01:23 +00:00
|
|
|
CHECK( x509_parse_int( p, 2, &tm->sec ) );
|
2017-02-03 12:36:59 +00:00
|
|
|
len -= 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return ( MBEDTLS_ERR_X509_INVALID_DATE );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse trailing 'Z' if present
|
|
|
|
*/
|
|
|
|
if ( 1 == len && 'Z' == **p )
|
|
|
|
{
|
|
|
|
(*p)++;
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We should have parsed all characters at this point
|
|
|
|
*/
|
|
|
|
if ( 0 != len )
|
|
|
|
return ( MBEDTLS_ERR_X509_INVALID_DATE );
|
|
|
|
|
2017-04-26 14:01:23 +00:00
|
|
|
CHECK( x509_date_is_valid( tm ) );
|
2017-02-03 12:36:59 +00:00
|
|
|
|
|
|
|
return ( 0 );
|
|
|
|
}
|
|
|
|
|
2013-09-16 11:49:26 +00:00
|
|
|
/*
|
|
|
|
* Time ::= CHOICE {
|
|
|
|
* utcTime UTCTime,
|
|
|
|
* generalTime GeneralizedTime }
|
|
|
|
*/
|
2019-09-03 13:50:17 +00:00
|
|
|
static int mbedtls_x509_get_time( unsigned char **p, const unsigned char *end,
|
2017-04-26 14:01:23 +00:00
|
|
|
mbedtls_x509_time *tm )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
|
|
|
int ret;
|
2017-02-03 12:36:59 +00:00
|
|
|
size_t len, year_len;
|
2013-09-16 11:49:26 +00:00
|
|
|
unsigned char tag;
|
|
|
|
|
|
|
|
if( ( end - *p ) < 1 )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_X509_INVALID_DATE +
|
|
|
|
MBEDTLS_ERR_ASN1_OUT_OF_DATA );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
|
|
|
tag = **p;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( tag == MBEDTLS_ASN1_UTC_TIME )
|
2017-02-03 12:36:59 +00:00
|
|
|
year_len = 2;
|
2015-04-08 10:49:31 +00:00
|
|
|
else if( tag == MBEDTLS_ASN1_GENERALIZED_TIME )
|
2017-02-03 12:36:59 +00:00
|
|
|
year_len = 4;
|
2013-09-16 11:49:26 +00:00
|
|
|
else
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_X509_INVALID_DATE +
|
|
|
|
MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
|
2017-02-03 12:36:59 +00:00
|
|
|
|
|
|
|
(*p)++;
|
|
|
|
ret = mbedtls_asn1_get_len( p, end, &len );
|
|
|
|
|
|
|
|
if( ret != 0 )
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_DATE + ret );
|
|
|
|
|
2017-04-26 14:01:23 +00:00
|
|
|
return x509_parse_time( p, len, year_len, tm );
|
2013-09-16 11:49:26 +00:00
|
|
|
}
|
2019-09-03 13:50:17 +00:00
|
|
|
#endif /* ( !defined(MBEDTLS_X509_CRT_REMOVE_TIME) && defined(MBEDTLS_X509_CRT_PARSE_C) ) ||
|
|
|
|
defined(MBEDTLS_X509_CRL_PARSE_C) */
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2019-09-03 13:50:17 +00:00
|
|
|
static int mbedtls_x509_get_sig( unsigned char **p, const unsigned char *end, mbedtls_x509_buf *sig )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
|
|
|
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 )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_X509_INVALID_SIGNATURE +
|
|
|
|
MBEDTLS_ERR_ASN1_OUT_OF_DATA );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2016-09-19 15:58:45 +00:00
|
|
|
tag_type = **p;
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ret = mbedtls_asn1_get_bitstring_null( p, end, &len ) ) != 0 )
|
|
|
|
return( MBEDTLS_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 );
|
|
|
|
}
|
|
|
|
|
2019-09-03 13:50:17 +00:00
|
|
|
static int mbedtls_x509_get_sig_alg_raw( unsigned char **p, unsigned char const *end,
|
2019-02-22 17:49:34 +00:00
|
|
|
mbedtls_md_type_t *md_alg,
|
|
|
|
mbedtls_pk_type_t *pk_alg,
|
|
|
|
void **sig_opts )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
mbedtls_asn1_buf alg, params;
|
|
|
|
ret = mbedtls_asn1_get_alg( p, end, &alg, ¶ms );
|
|
|
|
if( ret != 0 )
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
|
|
|
|
|
|
|
|
return( mbedtls_x509_get_sig_alg( &alg, ¶ms, md_alg,
|
|
|
|
pk_alg, sig_opts ) );
|
|
|
|
}
|
|
|
|
|
2014-01-24 18:28:43 +00:00
|
|
|
/*
|
|
|
|
* Get signature algorithm from alg OID and optional parameters
|
|
|
|
*/
|
2019-09-03 13:50:17 +00:00
|
|
|
static int mbedtls_x509_get_sig_alg( const mbedtls_x509_buf *sig_oid, const mbedtls_x509_buf *sig_params,
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_md_type_t *md_alg, mbedtls_pk_type_t *pk_alg,
|
2014-06-05 13:14:28 +00:00
|
|
|
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
|
|
|
|
2015-04-08 10:49:31 +00: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 11:49:26 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
|
|
|
|
if( *pk_alg == MBEDTLS_PK_RSASSA_PSS )
|
2014-01-24 18:28:43 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_pk_rsassa_pss_options *pss_opts;
|
2014-06-05 13:14:28 +00:00
|
|
|
|
2015-05-26 14:04:06 +00:00
|
|
|
pss_opts = mbedtls_calloc( 1, sizeof( mbedtls_pk_rsassa_pss_options ) );
|
2014-06-05 13:14:28 +00:00
|
|
|
if( pss_opts == NULL )
|
2015-05-28 07:33:39 +00:00
|
|
|
return( MBEDTLS_ERR_X509_ALLOC_FAILED );
|
2014-01-24 18:28:43 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = mbedtls_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
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_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
|
|
|
|
2019-02-22 16:03:29 +00:00
|
|
|
if( sig_opts != NULL )
|
|
|
|
*sig_opts = (void *) pss_opts;
|
|
|
|
else
|
|
|
|
mbedtls_free( pss_opts );
|
2014-01-24 18:28:43 +00:00
|
|
|
}
|
|
|
|
else
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
|
2014-01-24 18:28:43 +00:00
|
|
|
{
|
|
|
|
/* Make sure parameters are absent or NULL */
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( sig_params->tag != MBEDTLS_ASN1_NULL && sig_params->tag != 0 ) ||
|
2014-01-24 18:28:43 +00:00
|
|
|
sig_params->len != 0 )
|
2019-02-22 16:03:29 +00:00
|
|
|
return( MBEDTLS_ERR_X509_INVALID_ALG );
|
|
|
|
|
|
|
|
if( sig_opts != NULL )
|
|
|
|
*sig_opts = NULL;
|
2014-01-24 18:28:43 +00:00
|
|
|
}
|
|
|
|
|
2013-09-16 11:49:26 +00:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2019-09-03 13:50:17 +00:00
|
|
|
#if defined(MBEDTLS_X509_CRL_PARSE_C)
|
2013-09-16 11:49:26 +00:00
|
|
|
/*
|
|
|
|
* X.509 Extensions (No parsing of extensions, pointer should
|
2016-11-06 12:45:15 +00:00
|
|
|
* be either manually updated or extensions should be parsed!)
|
2013-09-16 11:49:26 +00:00
|
|
|
*/
|
2019-09-03 13:50:17 +00:00
|
|
|
static int mbedtls_x509_get_ext( unsigned char **p, const unsigned char *end,
|
Always return a high-level error code from X.509 module
Some functions within the X.509 module return an ASN.1 low level
error code where instead this error code should be wrapped by a
high-level X.509 error code as in the bulk of the module.
Specifically, the following functions are affected:
- mbedtls_x509_get_ext()
- x509_get_version()
- x509_get_uid()
This commit modifies these functions to always return an
X.509 high level error code.
Care has to be taken when adapting `mbetls_x509_get_ext()`:
Currently, the callers `mbedtls_x509_crt_ext()` treat the
return code `MBEDTLS_ERR_ASN1_UNEXPECTED_TAG` specially to
gracefully detect and continue if the extension structure is not
present. Wrapping the ASN.1 error with
`MBEDTLS_ERR_X509_INVALID_EXTENSIONS` and adapting the check
accordingly would mean that an unexpected tag somewhere
down the extension parsing would be ignored by the caller.
The way out of this is the following: Luckily, the extension
structure is always the last field in the surrounding structure,
so if there is some data remaining, it must be an Extension
structure, so we don't need to deal with a tag mismatch gracefully
in the first place.
We may therefore wrap the return code from the initial call to
`mbedtls_asn1_get_tag()` in `mbedtls_x509_get_ext()` by
`MBEDTLS_ERR_X509_INVALID_EXTENSIONS` and simply remove
the special treatment of `MBEDTLS_ERR_ASN1_UNEXPECTED_TAG`
in the callers `x509_crl_get_ext()` and `x509_crt_get_ext()`.
This renders `mbedtls_x509_get_ext()` unsuitable if it ever
happened that an Extension structure is optional and does not
occur at the end of its surrounding structure, but for CRTs
and CRLs, it's fine.
The following tests need to be adapted:
- "TBSCertificate v3, issuerID wrong tag"
The issuerID is optional, so if we look for its presence
but find a different tag, we silently continue and try
parsing the subjectID, and then the extensions. The tag '00'
used in this test doesn't match either of these, and the
previous code would hence return LENGTH_MISMATCH after
unsucessfully trying issuerID, subjectID and Extensions.
With the new code, any data remaining after issuerID and
subjectID _must_ be Extension data, so we fail with
UNEXPECTED_TAG when trying to parse the Extension data.
- "TBSCertificate v3, UIDs, invalid length"
The test hardcodes the expectation of
MBEDTLS_ERR_ASN1_INVALID_LENGTH, which needs to be
wrapped in MBEDTLS_ERR_X509_INVALID_FORMAT now.
Fixes #2431.
2019-02-12 11:52:10 +00:00
|
|
|
mbedtls_x509_buf *ext, int tag )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t len;
|
|
|
|
|
2019-02-11 14:33:36 +00:00
|
|
|
/* Extension structure use EXPLICIT tagging. That is, the actual
|
|
|
|
* `Extensions` structure is wrapped by a tag-length pair using
|
|
|
|
* the respective context-specific tag. */
|
Always return a high-level error code from X.509 module
Some functions within the X.509 module return an ASN.1 low level
error code where instead this error code should be wrapped by a
high-level X.509 error code as in the bulk of the module.
Specifically, the following functions are affected:
- mbedtls_x509_get_ext()
- x509_get_version()
- x509_get_uid()
This commit modifies these functions to always return an
X.509 high level error code.
Care has to be taken when adapting `mbetls_x509_get_ext()`:
Currently, the callers `mbedtls_x509_crt_ext()` treat the
return code `MBEDTLS_ERR_ASN1_UNEXPECTED_TAG` specially to
gracefully detect and continue if the extension structure is not
present. Wrapping the ASN.1 error with
`MBEDTLS_ERR_X509_INVALID_EXTENSIONS` and adapting the check
accordingly would mean that an unexpected tag somewhere
down the extension parsing would be ignored by the caller.
The way out of this is the following: Luckily, the extension
structure is always the last field in the surrounding structure,
so if there is some data remaining, it must be an Extension
structure, so we don't need to deal with a tag mismatch gracefully
in the first place.
We may therefore wrap the return code from the initial call to
`mbedtls_asn1_get_tag()` in `mbedtls_x509_get_ext()` by
`MBEDTLS_ERR_X509_INVALID_EXTENSIONS` and simply remove
the special treatment of `MBEDTLS_ERR_ASN1_UNEXPECTED_TAG`
in the callers `x509_crl_get_ext()` and `x509_crt_get_ext()`.
This renders `mbedtls_x509_get_ext()` unsuitable if it ever
happened that an Extension structure is optional and does not
occur at the end of its surrounding structure, but for CRTs
and CRLs, it's fine.
The following tests need to be adapted:
- "TBSCertificate v3, issuerID wrong tag"
The issuerID is optional, so if we look for its presence
but find a different tag, we silently continue and try
parsing the subjectID, and then the extensions. The tag '00'
used in this test doesn't match either of these, and the
previous code would hence return LENGTH_MISMATCH after
unsucessfully trying issuerID, subjectID and Extensions.
With the new code, any data remaining after issuerID and
subjectID _must_ be Extension data, so we fail with
UNEXPECTED_TAG when trying to parse the Extension data.
- "TBSCertificate v3, UIDs, invalid length"
The test hardcodes the expectation of
MBEDTLS_ERR_ASN1_INVALID_LENGTH, which needs to be
wrapped in MBEDTLS_ERR_X509_INVALID_FORMAT now.
Fixes #2431.
2019-02-12 11:52:10 +00:00
|
|
|
ret = mbedtls_asn1_get_tag( p, end, &ext->len,
|
|
|
|
MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag );
|
|
|
|
if( ret != 0 )
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
Always return a high-level error code from X.509 module
Some functions within the X.509 module return an ASN.1 low level
error code where instead this error code should be wrapped by a
high-level X.509 error code as in the bulk of the module.
Specifically, the following functions are affected:
- mbedtls_x509_get_ext()
- x509_get_version()
- x509_get_uid()
This commit modifies these functions to always return an
X.509 high level error code.
Care has to be taken when adapting `mbetls_x509_get_ext()`:
Currently, the callers `mbedtls_x509_crt_ext()` treat the
return code `MBEDTLS_ERR_ASN1_UNEXPECTED_TAG` specially to
gracefully detect and continue if the extension structure is not
present. Wrapping the ASN.1 error with
`MBEDTLS_ERR_X509_INVALID_EXTENSIONS` and adapting the check
accordingly would mean that an unexpected tag somewhere
down the extension parsing would be ignored by the caller.
The way out of this is the following: Luckily, the extension
structure is always the last field in the surrounding structure,
so if there is some data remaining, it must be an Extension
structure, so we don't need to deal with a tag mismatch gracefully
in the first place.
We may therefore wrap the return code from the initial call to
`mbedtls_asn1_get_tag()` in `mbedtls_x509_get_ext()` by
`MBEDTLS_ERR_X509_INVALID_EXTENSIONS` and simply remove
the special treatment of `MBEDTLS_ERR_ASN1_UNEXPECTED_TAG`
in the callers `x509_crl_get_ext()` and `x509_crt_get_ext()`.
This renders `mbedtls_x509_get_ext()` unsuitable if it ever
happened that an Extension structure is optional and does not
occur at the end of its surrounding structure, but for CRTs
and CRLs, it's fine.
The following tests need to be adapted:
- "TBSCertificate v3, issuerID wrong tag"
The issuerID is optional, so if we look for its presence
but find a different tag, we silently continue and try
parsing the subjectID, and then the extensions. The tag '00'
used in this test doesn't match either of these, and the
previous code would hence return LENGTH_MISMATCH after
unsucessfully trying issuerID, subjectID and Extensions.
With the new code, any data remaining after issuerID and
subjectID _must_ be Extension data, so we fail with
UNEXPECTED_TAG when trying to parse the Extension data.
- "TBSCertificate v3, UIDs, invalid length"
The test hardcodes the expectation of
MBEDTLS_ERR_ASN1_INVALID_LENGTH, which needs to be
wrapped in MBEDTLS_ERR_X509_INVALID_FORMAT now.
Fixes #2431.
2019-02-12 11:52:10 +00:00
|
|
|
ext->tag = MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag;
|
|
|
|
ext->p = *p;
|
|
|
|
end = *p + ext->len;
|
2013-09-16 11:49:26 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
|
|
|
|
*/
|
2015-04-08 10:49:31 +00: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 11:49:26 +00:00
|
|
|
|
|
|
|
if( end != *p + len )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
2019-09-03 13:50:17 +00:00
|
|
|
#endif /* defined(MBEDTLS_X509_CRL_PARSE_C) */
|
2013-09-16 11:49:26 +00:00
|
|
|
/*
|
|
|
|
* Store the name in printable form into buf; no more
|
|
|
|
* than size characters will be written
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_x509_dn_gets( char *buf, size_t size, const mbedtls_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;
|
2015-04-08 10:49:31 +00:00
|
|
|
const mbedtls_x509_name *name;
|
2013-09-16 11:49:26 +00:00
|
|
|
const char *short_name = NULL;
|
2015-04-08 10:49:31 +00:00
|
|
|
char s[MBEDTLS_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-04-08 10:49:31 +00:00
|
|
|
ret = mbedtls_snprintf( p, n, merge ? " + " : ", " );
|
2015-06-22 09:12:02 +00:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
2013-09-16 11:49:26 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = mbedtls_oid_get_attr_short_name( &name->oid, &short_name );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
|
|
|
if( ret == 0 )
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = mbedtls_snprintf( p, n, "%s=", short_name );
|
2013-09-16 11:49:26 +00:00
|
|
|
else
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = mbedtls_snprintf( p, n, "\?\?=" );
|
2015-06-22 09:12:02 +00:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
2013-09-16 11:49:26 +00: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 10:49:31 +00:00
|
|
|
ret = mbedtls_snprintf( p, n, "%s", s );
|
2015-06-22 09:12:02 +00:00
|
|
|
MBEDTLS_X509_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
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_x509_serial_gets( char *buf, size_t size, const mbedtls_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-04-08 10:49:31 +00:00
|
|
|
ret = mbedtls_snprintf( p, n, "%02X%s",
|
2013-09-16 11:49:26 +00:00
|
|
|
serial->p[i], ( i < nr - 1 ) ? ":" : "" );
|
2015-06-22 09:12:02 +00:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
2013-09-16 11:49:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( nr != serial->len )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = mbedtls_snprintf( p, n, "...." );
|
2015-06-22 09:12:02 +00:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
2013-09-16 11:49:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return( (int) ( size - n ) );
|
|
|
|
}
|
|
|
|
|
2019-06-10 14:08:43 +00:00
|
|
|
#if !defined(MBEDTLS_X509_REMOVE_INFO)
|
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
|
|
|
*/
|
2019-09-03 13:50:17 +00:00
|
|
|
static int mbedtls_x509_sig_alg_gets( char *buf, size_t size, mbedtls_pk_type_t pk_alg,
|
2019-02-21 17:13:46 +00:00
|
|
|
mbedtls_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;
|
2019-02-21 17:13:46 +00:00
|
|
|
mbedtls_x509_buf sig_oid;
|
|
|
|
mbedtls_md_type_t tmp_md_alg = md_alg;
|
2014-01-25 10:50:59 +00:00
|
|
|
|
2019-02-21 17:13:46 +00:00
|
|
|
#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
|
|
|
|
/* The hash for RSASSA is determined by the algorithm parameters;
|
|
|
|
* in the OID list, the hash is set to MBEDTLS_MD_NONE. */
|
|
|
|
if( pk_alg == MBEDTLS_PK_RSASSA_PSS )
|
|
|
|
tmp_md_alg = MBEDTLS_MD_NONE;
|
|
|
|
#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
|
|
|
|
|
|
|
|
sig_oid.tag = MBEDTLS_ASN1_OID;
|
|
|
|
ret = mbedtls_oid_get_oid_by_sig_alg( pk_alg, tmp_md_alg,
|
|
|
|
(const char**) &sig_oid.p,
|
|
|
|
&sig_oid.len );
|
|
|
|
if( ret == 0 &&
|
|
|
|
mbedtls_oid_get_sig_alg_desc( &sig_oid, &desc ) == 0 )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = mbedtls_snprintf( p, n, "%s", desc );
|
2019-02-21 17:13:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = mbedtls_snprintf( p, n, "???" );
|
2015-06-22 09:12:02 +00:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
2014-01-25 10:50:59 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
|
|
|
|
if( pk_alg == MBEDTLS_PK_RSASSA_PSS )
|
2014-01-25 10:50:59 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
const mbedtls_pk_rsassa_pss_options *pss_opts;
|
|
|
|
const mbedtls_md_info_t *md_info, *mgf_md_info;
|
2014-01-25 10:50:59 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
pss_opts = (const mbedtls_pk_rsassa_pss_options *) sig_opts;
|
2014-01-25 10:50:59 +00:00
|
|
|
|
2015-04-08 10:49:31 +00: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 10:50:59 +00:00
|
|
|
|
2015-04-08 10:49:31 +00: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 13:41:39 +00:00
|
|
|
pss_opts->expected_salt_len );
|
2015-06-22 09:12:02 +00:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
2014-01-25 10:50:59 +00:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
((void) pk_alg);
|
2014-06-05 13:41:39 +00:00
|
|
|
((void) md_alg);
|
|
|
|
((void) sig_opts);
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_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
|
|
|
|
*/
|
2019-09-03 13:50:17 +00:00
|
|
|
static int mbedtls_x509_key_size_helper( char *buf, size_t buf_size, const char *name )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
|
|
|
char *p = buf;
|
2015-06-18 14:25:56 +00:00
|
|
|
size_t n = buf_size;
|
2013-09-16 11:49:26 +00:00
|
|
|
int ret;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = mbedtls_snprintf( p, n, "%s key size", name );
|
2015-06-22 09:12:02 +00:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
2013-09-16 11:49:26 +00:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
2019-09-03 13:50:17 +00:00
|
|
|
#endif /* !MBEDTLS_X509_REMOVE_INFO */
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2015-06-18 18:52:58 +00:00
|
|
|
#if defined(MBEDTLS_HAVE_TIME_DATE)
|
2015-06-22 16:59:21 +00:00
|
|
|
/*
|
|
|
|
* Set the time structure to the current time.
|
|
|
|
* Return 0 on success, non-zero on failure.
|
|
|
|
*/
|
|
|
|
static int x509_get_current_time( mbedtls_x509_time *now )
|
|
|
|
{
|
2017-12-05 12:07:33 +00:00
|
|
|
struct tm *lt, tm_buf;
|
2016-04-26 06:43:27 +00:00
|
|
|
mbedtls_time_t tt;
|
2015-06-22 16:59:21 +00:00
|
|
|
int ret = 0;
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2016-04-26 06:43:27 +00:00
|
|
|
tt = mbedtls_time( NULL );
|
2018-09-05 14:06:19 +00:00
|
|
|
lt = mbedtls_platform_gmtime_r( &tt, &tm_buf );
|
2015-05-29 08:11:03 +00:00
|
|
|
|
2015-06-22 16:59:21 +00: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 08:11:03 +00:00
|
|
|
|
2015-06-22 16:59:21 +00:00
|
|
|
return( ret );
|
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
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
static int x509_check_time( const mbedtls_x509_time *before, const mbedtls_x509_time *after )
|
2014-03-10 11:26:11 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2015-06-02 09:38:50 +00:00
|
|
|
int mbedtls_x509_time_is_past( const mbedtls_x509_time *to )
|
2014-03-10 11:26:11 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_time now;
|
2014-03-10 11:26:11 +00:00
|
|
|
|
2015-05-29 08:11:03 +00:00
|
|
|
if( x509_get_current_time( &now ) != 0 )
|
2015-06-22 17:15:32 +00:00
|
|
|
return( 1 );
|
2014-03-10 11:26:11 +00:00
|
|
|
|
|
|
|
return( x509_check_time( &now, to ) );
|
|
|
|
}
|
|
|
|
|
2015-06-02 09:38:50 +00:00
|
|
|
int mbedtls_x509_time_is_future( const mbedtls_x509_time *from )
|
2014-03-10 11:26:11 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_time now;
|
2014-03-10 11:26:11 +00:00
|
|
|
|
2015-05-29 08:11:03 +00:00
|
|
|
if( x509_get_current_time( &now ) != 0 )
|
2015-06-22 17:15:32 +00:00
|
|
|
return( 1 );
|
2014-03-10 11:26:11 +00:00
|
|
|
|
|
|
|
return( x509_check_time( from, &now ) );
|
|
|
|
}
|
2019-06-07 16:03:40 +00:00
|
|
|
#endif /* MBEDTLS_HAVE_TIME_DATE */
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2019-02-26 19:01:00 +00:00
|
|
|
void mbedtls_x509_name_free( mbedtls_x509_name *name )
|
|
|
|
{
|
|
|
|
while( name != NULL )
|
|
|
|
{
|
|
|
|
mbedtls_x509_name *next = name->next;
|
|
|
|
mbedtls_platform_zeroize( name, sizeof( *name ) );
|
|
|
|
mbedtls_free( name );
|
|
|
|
name = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_SELF_TEST)
|
2013-09-16 20:53:25 +00:00
|
|
|
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/x509_crt.h"
|
|
|
|
#include "mbedtls/certs.h"
|
2013-09-16 20:53:25 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Checkup routine
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_x509_self_test( int verbose )
|
2013-09-16 20:53:25 +00:00
|
|
|
{
|
2018-10-17 12:01:08 +00:00
|
|
|
int ret = 0;
|
2017-05-05 16:56:30 +00:00
|
|
|
#if defined(MBEDTLS_CERTS_C) && defined(MBEDTLS_SHA256_C)
|
2015-05-11 17:54:43 +00:00
|
|
|
uint32_t flags;
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crt cacert;
|
|
|
|
mbedtls_x509_crt clicert;
|
2013-09-16 20:53:25 +00:00
|
|
|
|
|
|
|
if( verbose != 0 )
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf( " X.509 certificate load: " );
|
2013-09-16 20:53:25 +00:00
|
|
|
|
2018-10-17 12:01:08 +00:00
|
|
|
mbedtls_x509_crt_init( &cacert );
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crt_init( &clicert );
|
2013-09-16 20:53:25 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = mbedtls_x509_crt_parse( &clicert, (const unsigned char *) mbedtls_test_cli_crt,
|
2015-05-12 09:20:10 +00:00
|
|
|
mbedtls_test_cli_crt_len );
|
2013-09-16 20:53:25 +00:00
|
|
|
if( ret != 0 )
|
|
|
|
{
|
|
|
|
if( verbose != 0 )
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf( "failed\n" );
|
2013-09-16 20:53:25 +00:00
|
|
|
|
2018-10-17 12:01:08 +00:00
|
|
|
goto cleanup;
|
2013-09-16 20:53:25 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = mbedtls_x509_crt_parse( &cacert, (const unsigned char *) mbedtls_test_ca_crt,
|
2015-05-12 09:20:10 +00:00
|
|
|
mbedtls_test_ca_crt_len );
|
2013-09-16 20:53:25 +00:00
|
|
|
if( ret != 0 )
|
|
|
|
{
|
|
|
|
if( verbose != 0 )
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf( "failed\n" );
|
2013-09-16 20:53:25 +00:00
|
|
|
|
2018-10-17 12:01:08 +00:00
|
|
|
goto cleanup;
|
2013-09-16 20:53:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( verbose != 0 )
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf( "passed\n X.509 signature verify: ");
|
2013-09-16 20:53:25 +00:00
|
|
|
|
2019-08-19 11:48:09 +00:00
|
|
|
ret = mbedtls_x509_crt_verify( &clicert, &cacert, NULL,
|
|
|
|
#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
|
|
|
|
NULL,
|
|
|
|
#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
|
|
|
|
&flags, NULL, NULL );
|
2013-09-16 20:53:25 +00:00
|
|
|
if( ret != 0 )
|
|
|
|
{
|
|
|
|
if( verbose != 0 )
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf( "failed\n" );
|
2013-09-16 20:53:25 +00:00
|
|
|
|
2018-10-17 12:01:08 +00:00
|
|
|
goto cleanup;
|
2013-09-16 20:53:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( verbose != 0 )
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf( "passed\n\n");
|
2013-09-16 20:53:25 +00:00
|
|
|
|
2018-10-17 12:01:08 +00:00
|
|
|
cleanup:
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crt_free( &cacert );
|
|
|
|
mbedtls_x509_crt_free( &clicert );
|
2013-09-16 20:53:25 +00:00
|
|
|
#else
|
|
|
|
((void) verbose);
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_CERTS_C && MBEDTLS_SHA1_C */
|
2018-10-17 12:01:08 +00:00
|
|
|
return( ret );
|
2013-09-16 20:53:25 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_SELF_TEST */
|
2013-09-16 20:53:25 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_X509_USE_C */
|