2013-09-16 11:49:26 +00:00
|
|
|
/*
|
2014-06-12 20:34:55 +00:00
|
|
|
* X.509 Certidicate Revocation List (CRL) parsing
|
2013-09-16 11:49:26 +00:00
|
|
|
*
|
2020-08-19 14:54:51 +00:00
|
|
|
* Copyright The Mbed TLS Contributors
|
Update license headers to Apache-2.0 OR GPL-2.0-or-later
This will allow us to ship the LTS branches in a single archive
This commit was generated using the following script:
# ========================
#!/bin/sh
header1='\ * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later\
*\
* This file is provided under the Apache License 2.0, or the\
* GNU General Public License v2.0 or later.\
*\
* **********\
* Apache License 2.0:\
*\
* 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\
*\
* http://www.apache.org/licenses/LICENSE-2.0\
*\
* 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.\
*\
* **********\
*\
* **********\
* GNU General Public License v2.0 or later:\
*\
* This program is free software; you can redistribute it and/or modify\
* it under the terms of the GNU General Public License as published by\
* the Free Software Foundation; either version 2 of the License, or\
* (at your option) any later version.\
*\
* This program is distributed in the hope that it will be useful,\
* but WITHOUT ANY WARRANTY; without even the implied warranty of\
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\
* GNU General Public License for more details.\
*\
* You should have received a copy of the GNU General Public License along\
* with this program; if not, write to the Free Software Foundation, Inc.,\
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.\
*\
* **********'
find -path './.git' -prune -o '(' -name '*.c' -o -name '*.cpp' -o -name '*.fmt' -o -name '*.h' ')' -print | xargs sed -i "
# Normalize the first line of the copyright headers (no text on the first line of a block comment)
/^\/\*.*Copyright.*Arm/I s/\/\*/&\n */
# Insert new copyright header
/SPDX-License-Identifier/ i\
$header1
# Delete old copyright header
/SPDX-License-Identifier/,$ {
# Delete lines until the one preceding the mbedtls declaration
N
1,/This file is part of/ {
/This file is part of/! D
}
}
"
# Format copyright header for inclusion into scripts
header2=$(echo "$header1" | sed 's/^\\\? \* \?/#/')
find -path './.git' -prune -o '(' -name '*.gdb' -o -name '*.pl' -o -name '*.py' -o -name '*.sh' ')' -print | xargs sed -i "
# Insert new copyright header
/SPDX-License-Identifier/ i\
$header2
# Delete old copyright header
/SPDX-License-Identifier/,$ {
# Delete lines until the one preceding the mbedtls declaration
N
1,/This file is part of/ {
/This file is part of/! D
}
}
"
# ========================
Signed-off-by: Bence Szépkúti <bence.szepkuti@arm.com>
2020-06-05 11:02:18 +00:00
|
|
|
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
|
|
|
*
|
|
|
|
* This file is provided under the Apache License 2.0, or the
|
|
|
|
* GNU General Public License v2.0 or later.
|
|
|
|
*
|
|
|
|
* **********
|
|
|
|
* Apache License 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
|
|
|
*
|
Update license headers to Apache-2.0 OR GPL-2.0-or-later
This will allow us to ship the LTS branches in a single archive
This commit was generated using the following script:
# ========================
#!/bin/sh
header1='\ * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later\
*\
* This file is provided under the Apache License 2.0, or the\
* GNU General Public License v2.0 or later.\
*\
* **********\
* Apache License 2.0:\
*\
* 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\
*\
* http://www.apache.org/licenses/LICENSE-2.0\
*\
* 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.\
*\
* **********\
*\
* **********\
* GNU General Public License v2.0 or later:\
*\
* This program is free software; you can redistribute it and/or modify\
* it under the terms of the GNU General Public License as published by\
* the Free Software Foundation; either version 2 of the License, or\
* (at your option) any later version.\
*\
* This program is distributed in the hope that it will be useful,\
* but WITHOUT ANY WARRANTY; without even the implied warranty of\
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\
* GNU General Public License for more details.\
*\
* You should have received a copy of the GNU General Public License along\
* with this program; if not, write to the Free Software Foundation, Inc.,\
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.\
*\
* **********'
find -path './.git' -prune -o '(' -name '*.c' -o -name '*.cpp' -o -name '*.fmt' -o -name '*.h' ')' -print | xargs sed -i "
# Normalize the first line of the copyright headers (no text on the first line of a block comment)
/^\/\*.*Copyright.*Arm/I s/\/\*/&\n */
# Insert new copyright header
/SPDX-License-Identifier/ i\
$header1
# Delete old copyright header
/SPDX-License-Identifier/,$ {
# Delete lines until the one preceding the mbedtls declaration
N
1,/This file is part of/ {
/This file is part of/! D
}
}
"
# Format copyright header for inclusion into scripts
header2=$(echo "$header1" | sed 's/^\\\? \* \?/#/')
find -path './.git' -prune -o '(' -name '*.gdb' -o -name '*.pl' -o -name '*.py' -o -name '*.sh' ')' -print | xargs sed -i "
# Insert new copyright header
/SPDX-License-Identifier/ i\
$header2
# Delete old copyright header
/SPDX-License-Identifier/,$ {
# Delete lines until the one preceding the mbedtls declaration
N
1,/This file is part of/ {
/This file is part of/! D
}
}
"
# ========================
Signed-off-by: Bence Szépkúti <bence.szepkuti@arm.com>
2020-06-05 11:02:18 +00:00
|
|
|
* **********
|
|
|
|
*
|
|
|
|
* **********
|
|
|
|
* GNU General Public License v2.0 or later:
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
* **********
|
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_CRL_PARSE_C)
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/x509_crl.h"
|
|
|
|
#include "mbedtls/oid.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 <stdlib.h>
|
2015-02-17 15:46:45 +00:00
|
|
|
#include <stdio.h>
|
2015-04-08 10:49:31 +00:00
|
|
|
#define mbedtls_free free
|
2015-05-26 14:04:06 +00:00
|
|
|
#define mbedtls_calloc calloc
|
2015-04-08 10:49:31 +00:00
|
|
|
#define mbedtls_snprintf snprintf
|
2013-09-16 11:49:26 +00:00
|
|
|
#endif
|
|
|
|
|
2013-10-28 17:48:30 +00:00
|
|
|
#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
|
2013-09-16 11:49:26 +00:00
|
|
|
#include <windows.h>
|
|
|
|
#else
|
|
|
|
#include <time.h>
|
|
|
|
#endif
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_FS_IO) || defined(EFIX64) || defined(EFI32)
|
2013-09-16 11:49:26 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#endif
|
|
|
|
|
2014-06-13 15:20:13 +00:00
|
|
|
/* Implementation that should never be optimized out by the compiler */
|
2015-04-08 10:49:31 +00:00
|
|
|
static void mbedtls_zeroize( void *v, size_t n ) {
|
2014-06-13 15:20:13 +00:00
|
|
|
volatile unsigned char *p = v; while( n-- ) *p++ = 0;
|
|
|
|
}
|
|
|
|
|
2013-09-16 11:49:26 +00:00
|
|
|
/*
|
|
|
|
* Version ::= INTEGER { v1(0), v2(1) }
|
|
|
|
*/
|
|
|
|
static int x509_crl_get_version( unsigned char **p,
|
|
|
|
const unsigned char *end,
|
|
|
|
int *ver )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
|
|
|
*ver = 0;
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_X509_INVALID_VERSION + ret );
|
2013-09-16 11:49:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2018-03-13 10:53:30 +00:00
|
|
|
* X.509 CRL v2 extensions
|
|
|
|
*
|
|
|
|
* We currently don't parse any extension's content, but we do check that the
|
|
|
|
* list of extensions is well-formed and abort on critical extensions (that
|
|
|
|
* are unsupported as we don't support any extension so far)
|
2013-09-16 11:49:26 +00:00
|
|
|
*/
|
|
|
|
static int x509_get_crl_ext( unsigned char **p,
|
|
|
|
const unsigned char *end,
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_buf *ext )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
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 )
|
|
|
|
return( 0 );
|
|
|
|
|
2018-03-13 10:53:30 +00:00
|
|
|
/*
|
|
|
|
* crlExtensions [0] EXPLICIT Extensions OPTIONAL
|
|
|
|
* -- if present, version MUST be v2
|
|
|
|
*/
|
|
|
|
if( ( ret = mbedtls_x509_get_ext( p, end, ext, 0 ) ) != 0 )
|
2013-09-16 11:49:26 +00:00
|
|
|
return( ret );
|
|
|
|
|
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 = ext->p + ext->len;
|
|
|
|
|
2013-09-16 11:49:26 +00:00
|
|
|
while( *p < end )
|
|
|
|
{
|
2018-03-13 10:53:30 +00:00
|
|
|
/*
|
|
|
|
* Extension ::= SEQUENCE {
|
|
|
|
* extnID OBJECT IDENTIFIER,
|
|
|
|
* critical BOOLEAN DEFAULT FALSE,
|
|
|
|
* extnValue OCTET STRING }
|
|
|
|
*/
|
|
|
|
int is_critical = 0;
|
|
|
|
const unsigned char *end_ext_data;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
/* Get enclosing sequence tag */
|
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
|
|
|
|
2018-03-13 10:53:30 +00:00
|
|
|
end_ext_data = *p + len;
|
|
|
|
|
|
|
|
/* Get OID (currently ignored) */
|
|
|
|
if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
|
|
|
|
MBEDTLS_ASN1_OID ) ) != 0 )
|
|
|
|
{
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
|
|
|
|
}
|
|
|
|
*p += len;
|
|
|
|
|
|
|
|
/* Get optional critical */
|
|
|
|
if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data,
|
|
|
|
&is_critical ) ) != 0 &&
|
|
|
|
( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
|
|
|
|
{
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Data should be octet string type */
|
|
|
|
if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
|
|
|
|
MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
|
|
|
|
|
|
|
|
/* Ignore data so far and just check its length */
|
2013-09-16 11:49:26 +00:00
|
|
|
*p += len;
|
2018-03-13 10:53:30 +00:00
|
|
|
if( *p != end_ext_data )
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
|
|
|
|
|
|
|
|
/* Abort on (unsupported) critical extensions */
|
|
|
|
if( is_critical )
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
|
|
|
|
MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
|
2013-09-16 11:49:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( *p != end )
|
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 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* X.509 CRL v2 entry extensions (no extensions parsed yet.)
|
|
|
|
*/
|
|
|
|
static int x509_get_crl_entry_ext( unsigned char **p,
|
|
|
|
const unsigned char *end,
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_buf *ext )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t len = 0;
|
|
|
|
|
|
|
|
/* OPTIONAL */
|
2014-06-17 14:39:18 +00:00
|
|
|
if( end <= *p )
|
2013-09-16 11:49:26 +00:00
|
|
|
return( 0 );
|
|
|
|
|
|
|
|
ext->tag = **p;
|
|
|
|
ext->p = *p;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get CRL-entry extension sequence header
|
|
|
|
* crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ret = mbedtls_asn1_get_tag( p, end, &ext->len,
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
|
|
|
ext->p = NULL;
|
|
|
|
return( 0 );
|
|
|
|
}
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
|
2013-09-16 11:49:26 +00:00
|
|
|
}
|
|
|
|
|
2014-05-01 11:03:14 +00:00
|
|
|
end = *p + ext->len;
|
2013-09-16 11:49:26 +00:00
|
|
|
|
|
|
|
if( end != *p + ext->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
|
|
|
|
|
|
|
while( *p < end )
|
|
|
|
{
|
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
|
|
|
|
|
|
|
*p += len;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( *p != end )
|
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 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* X.509 CRL Entries
|
|
|
|
*/
|
|
|
|
static int x509_get_entries( unsigned char **p,
|
|
|
|
const unsigned char *end,
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crl_entry *entry )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t entry_len;
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crl_entry *cur_entry = entry;
|
2013-09-16 11:49:26 +00:00
|
|
|
|
|
|
|
if( *p == end )
|
|
|
|
return( 0 );
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ret = mbedtls_asn1_get_tag( p, end, &entry_len,
|
|
|
|
MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED ) ) != 0 )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
|
2013-09-16 11:49:26 +00:00
|
|
|
return( 0 );
|
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
end = *p + entry_len;
|
|
|
|
|
|
|
|
while( *p < end )
|
|
|
|
{
|
|
|
|
size_t len2;
|
|
|
|
const unsigned char *end2;
|
|
|
|
|
2020-07-16 16:26:29 +00:00
|
|
|
cur_entry->raw.tag = **p;
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ret = mbedtls_asn1_get_tag( p, end, &len2,
|
|
|
|
MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED ) ) != 0 )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
cur_entry->raw.p = *p;
|
|
|
|
cur_entry->raw.len = len2;
|
|
|
|
end2 = *p + len2;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ret = mbedtls_x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
|
2013-09-16 11:49:26 +00:00
|
|
|
return( ret );
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ret = mbedtls_x509_get_time( p, end2,
|
2014-05-01 12:18:25 +00:00
|
|
|
&cur_entry->revocation_date ) ) != 0 )
|
2013-09-16 11:49:26 +00:00
|
|
|
return( ret );
|
|
|
|
|
2014-05-01 12:18:25 +00:00
|
|
|
if( ( ret = x509_get_crl_entry_ext( p, end2,
|
|
|
|
&cur_entry->entry_ext ) ) != 0 )
|
2013-09-16 11:49:26 +00:00
|
|
|
return( ret );
|
|
|
|
|
2014-06-17 14:39:18 +00:00
|
|
|
if( *p < end )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
2015-05-26 14:04:06 +00:00
|
|
|
cur_entry->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crl_entry ) );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
|
|
|
if( cur_entry->next == NULL )
|
2015-05-28 07:33:39 +00:00
|
|
|
return( MBEDTLS_ERR_X509_ALLOC_FAILED );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
|
|
|
cur_entry = cur_entry->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2014-11-19 15:58:28 +00:00
|
|
|
* Parse one CRLs in DER format and append it to the chained list
|
2013-09-16 11:49:26 +00:00
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_x509_crl_parse_der( mbedtls_x509_crl *chain,
|
2014-11-19 15:58:28 +00:00
|
|
|
const unsigned char *buf, size_t buflen )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t len;
|
2017-07-06 09:06:58 +00:00
|
|
|
unsigned char *p = NULL, *end = NULL;
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
|
|
|
|
mbedtls_x509_crl *crl = chain;
|
2013-09-16 11:49:26 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for valid input
|
|
|
|
*/
|
|
|
|
if( crl == NULL || buf == NULL )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) );
|
|
|
|
memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) );
|
|
|
|
memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Add new CRL on the end of the chain if needed.
|
|
|
|
*/
|
2014-11-19 15:58:28 +00:00
|
|
|
while( crl->version != 0 && crl->next != NULL )
|
|
|
|
crl = crl->next;
|
|
|
|
|
2014-06-17 14:39:18 +00:00
|
|
|
if( crl->version != 0 && crl->next == NULL )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
2015-05-26 14:04:06 +00:00
|
|
|
crl->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crl ) );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
|
|
|
if( crl->next == NULL )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crl_free( crl );
|
2015-05-28 07:33:39 +00:00
|
|
|
return( MBEDTLS_ERR_X509_ALLOC_FAILED );
|
2013-09-16 11:49:26 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crl_init( crl->next );
|
2013-09-16 11:49:26 +00:00
|
|
|
crl = crl->next;
|
|
|
|
}
|
|
|
|
|
2014-11-19 15:58:28 +00:00
|
|
|
/*
|
|
|
|
* Copy raw DER-encoded CRL
|
|
|
|
*/
|
2017-12-06 09:39:23 +00:00
|
|
|
if( buflen == 0 )
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_FORMAT );
|
2017-12-12 20:15:03 +00:00
|
|
|
|
|
|
|
p = mbedtls_calloc( 1, buflen );
|
|
|
|
if( p == NULL )
|
2015-05-28 07:33:39 +00:00
|
|
|
return( MBEDTLS_ERR_X509_ALLOC_FAILED );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2014-11-19 15:58:28 +00:00
|
|
|
memcpy( p, buf, buflen );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
|
|
|
crl->raw.p = p;
|
2014-11-19 15:58:28 +00:00
|
|
|
crl->raw.len = buflen;
|
|
|
|
|
|
|
|
end = p + buflen;
|
2013-09-16 11:49:26 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* CertificateList ::= SEQUENCE {
|
|
|
|
* tbsCertList TBSCertList,
|
|
|
|
* signatureAlgorithm AlgorithmIdentifier,
|
|
|
|
* signatureValue BIT STRING }
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crl_free( crl );
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_FORMAT );
|
2013-09-16 11:49:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( len != (size_t) ( end - p ) )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crl_free( crl );
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_FORMAT +
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
|
2013-09-16 11:49:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* TBSCertList ::= SEQUENCE {
|
|
|
|
*/
|
|
|
|
crl->tbs.p = p;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crl_free( crl );
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
|
2013-09-16 11:49:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
end = p + len;
|
|
|
|
crl->tbs.len = end - crl->tbs.p;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
|
|
|
|
* -- if present, MUST be v2
|
|
|
|
*
|
|
|
|
* signature AlgorithmIdentifier
|
|
|
|
*/
|
|
|
|
if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
|
2015-04-08 10:49:31 +00:00
|
|
|
( ret = mbedtls_x509_get_alg( &p, end, &crl->sig_oid, &sig_params1 ) ) != 0 )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crl_free( crl );
|
2013-09-16 11:49:26 +00:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2017-02-10 14:39:58 +00:00
|
|
|
if( crl->version < 0 || crl->version > 1 )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crl_free( crl );
|
|
|
|
return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
|
2013-09-16 11:49:26 +00:00
|
|
|
}
|
|
|
|
|
2017-02-10 14:39:58 +00:00
|
|
|
crl->version++;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ret = mbedtls_x509_get_sig_alg( &crl->sig_oid, &sig_params1,
|
2014-06-05 13:14:28 +00:00
|
|
|
&crl->sig_md, &crl->sig_pk,
|
|
|
|
&crl->sig_opts ) ) != 0 )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crl_free( crl );
|
|
|
|
return( MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG );
|
2013-09-16 11:49:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* issuer Name
|
|
|
|
*/
|
|
|
|
crl->issuer_raw.p = p;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crl_free( crl );
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
|
2013-09-16 11:49:26 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ret = mbedtls_x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crl_free( crl );
|
2013-09-16 11:49:26 +00:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
crl->issuer_raw.len = p - crl->issuer_raw.p;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* thisUpdate Time
|
|
|
|
* nextUpdate Time OPTIONAL
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ret = mbedtls_x509_get_time( &p, end, &crl->this_update ) ) != 0 )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crl_free( crl );
|
2013-09-16 11:49:26 +00:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ret = mbedtls_x509_get_time( &p, end, &crl->next_update ) ) != 0 )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ret != ( MBEDTLS_ERR_X509_INVALID_DATE +
|
|
|
|
MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) &&
|
|
|
|
ret != ( MBEDTLS_ERR_X509_INVALID_DATE +
|
|
|
|
MBEDTLS_ERR_ASN1_OUT_OF_DATA ) )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crl_free( crl );
|
2013-09-16 11:49:26 +00:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* revokedCertificates SEQUENCE OF SEQUENCE {
|
|
|
|
* userCertificate CertificateSerialNumber,
|
|
|
|
* revocationDate Time,
|
|
|
|
* crlEntryExtensions Extensions OPTIONAL
|
|
|
|
* -- if present, MUST be v2
|
|
|
|
* } OPTIONAL
|
|
|
|
*/
|
|
|
|
if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crl_free( crl );
|
2013-09-16 11:49:26 +00:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* crlExtensions EXPLICIT Extensions OPTIONAL
|
|
|
|
* -- if present, MUST be v2
|
|
|
|
*/
|
|
|
|
if( crl->version == 2 )
|
|
|
|
{
|
|
|
|
ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
|
|
|
|
|
|
|
|
if( ret != 0 )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crl_free( crl );
|
2013-09-16 11:49:26 +00:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( p != end )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crl_free( crl );
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_FORMAT +
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
|
2013-09-16 11:49:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
end = crl->raw.p + crl->raw.len;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* signatureAlgorithm AlgorithmIdentifier,
|
|
|
|
* signatureValue BIT STRING
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crl_free( crl );
|
2013-09-16 11:49:26 +00:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2015-03-27 15:30:47 +00:00
|
|
|
if( crl->sig_oid.len != sig_oid2.len ||
|
|
|
|
memcmp( crl->sig_oid.p, sig_oid2.p, crl->sig_oid.len ) != 0 ||
|
2014-06-05 15:02:24 +00:00
|
|
|
sig_params1.len != sig_params2.len ||
|
2015-04-30 09:15:22 +00:00
|
|
|
( sig_params1.len != 0 &&
|
|
|
|
memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crl_free( crl );
|
|
|
|
return( MBEDTLS_ERR_X509_SIG_MISMATCH );
|
2013-09-16 11:49:26 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ret = mbedtls_x509_get_sig( &p, end, &crl->sig ) ) != 0 )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crl_free( crl );
|
2013-09-16 11:49:26 +00:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( p != end )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crl_free( crl );
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_FORMAT +
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
|
2013-09-16 11:49:26 +00:00
|
|
|
}
|
|
|
|
|
2014-11-19 15:58:28 +00:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse one or more CRLs and add them to the chained list
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_x509_crl_parse( mbedtls_x509_crl *chain, const unsigned char *buf, size_t buflen )
|
2014-11-19 15:58:28 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_PEM_PARSE_C)
|
2014-11-19 15:58:28 +00:00
|
|
|
int ret;
|
2014-11-19 18:05:03 +00:00
|
|
|
size_t use_len;
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_pem_context pem;
|
2014-11-19 18:05:03 +00:00
|
|
|
int is_pem = 0;
|
2014-11-19 15:58:28 +00:00
|
|
|
|
|
|
|
if( chain == NULL || buf == NULL )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
|
2014-11-19 15:58:28 +00:00
|
|
|
|
2014-11-19 18:05:03 +00:00
|
|
|
do
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_pem_init( &pem );
|
2015-05-12 09:20:10 +00:00
|
|
|
|
2016-05-18 23:22:37 +00:00
|
|
|
// Avoid calling mbedtls_pem_read_buffer() on non-null-terminated
|
|
|
|
// string
|
|
|
|
if( buflen == 0 || buf[buflen - 1] != '\0' )
|
|
|
|
ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
|
|
|
|
else
|
|
|
|
ret = mbedtls_pem_read_buffer( &pem,
|
|
|
|
"-----BEGIN X509 CRL-----",
|
|
|
|
"-----END X509 CRL-----",
|
|
|
|
buf, NULL, 0, &use_len );
|
2014-11-19 15:58:28 +00:00
|
|
|
|
2014-11-19 18:05:03 +00:00
|
|
|
if( ret == 0 )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Was PEM encoded
|
|
|
|
*/
|
|
|
|
is_pem = 1;
|
2014-11-19 15:58:28 +00:00
|
|
|
|
2014-11-19 18:05:03 +00:00
|
|
|
buflen -= use_len;
|
|
|
|
buf += use_len;
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ret = mbedtls_x509_crl_parse_der( chain,
|
2014-11-19 18:05:03 +00:00
|
|
|
pem.buf, pem.buflen ) ) != 0 )
|
|
|
|
{
|
2016-12-08 17:19:21 +00:00
|
|
|
mbedtls_pem_free( &pem );
|
2014-11-19 18:05:03 +00:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
}
|
2016-12-08 17:08:44 +00:00
|
|
|
else if( is_pem )
|
2014-11-19 18:05:03 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_pem_free( &pem );
|
2014-11-19 15:58:28 +00:00
|
|
|
return( ret );
|
2014-11-19 18:05:03 +00:00
|
|
|
}
|
2016-12-08 17:19:21 +00:00
|
|
|
|
|
|
|
mbedtls_pem_free( &pem );
|
2013-09-16 11:49:26 +00:00
|
|
|
}
|
2015-05-12 09:20:10 +00:00
|
|
|
/* In the PEM case, buflen is 1 at the end, for the terminated NULL byte.
|
|
|
|
* And a valid CRL cannot be less than 1 byte anyway. */
|
|
|
|
while( is_pem && buflen > 1 );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2014-11-19 18:05:03 +00:00
|
|
|
if( is_pem )
|
|
|
|
return( 0 );
|
|
|
|
else
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_PEM_PARSE_C */
|
|
|
|
return( mbedtls_x509_crl_parse_der( chain, buf, buflen ) );
|
2013-09-16 11:49:26 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_FS_IO)
|
2013-09-16 11:49:26 +00:00
|
|
|
/*
|
|
|
|
* Load one or more CRLs and add them to the chained list
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_x509_crl_parse_file( mbedtls_x509_crl *chain, const char *path )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t n;
|
|
|
|
unsigned char *buf;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
|
2013-09-16 11:49:26 +00:00
|
|
|
return( ret );
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = mbedtls_x509_crl_parse( chain, buf, n );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2015-05-12 09:20:10 +00:00
|
|
|
mbedtls_zeroize( buf, n );
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_free( buf );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_FS_IO */
|
2013-09-16 11:49:26 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Return an informational string about the certificate.
|
|
|
|
*/
|
|
|
|
#define BEFORE_COLON 14
|
|
|
|
#define BC "14"
|
|
|
|
/*
|
|
|
|
* Return an informational string about the CRL.
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_x509_crl_info( char *buf, size_t size, const char *prefix,
|
|
|
|
const mbedtls_x509_crl *crl )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t n;
|
|
|
|
char *p;
|
2015-04-08 10:49:31 +00:00
|
|
|
const mbedtls_x509_crl_entry *entry;
|
2013-09-16 11:49:26 +00:00
|
|
|
|
|
|
|
p = buf;
|
|
|
|
n = size;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = mbedtls_snprintf( p, n, "%sCRL version : %d",
|
2013-09-16 11:49:26 +00:00
|
|
|
prefix, crl->version );
|
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_snprintf( p, n, "\n%sissuer name : ", prefix );
|
2015-06-22 09:12:02 +00:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = mbedtls_x509_dn_gets( p, n, &crl->issuer );
|
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_snprintf( p, n, "\n%sthis update : " \
|
2013-09-16 11:49:26 +00:00
|
|
|
"%04d-%02d-%02d %02d:%02d:%02d", prefix,
|
|
|
|
crl->this_update.year, crl->this_update.mon,
|
|
|
|
crl->this_update.day, crl->this_update.hour,
|
|
|
|
crl->this_update.min, crl->this_update.sec );
|
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_snprintf( p, n, "\n%snext update : " \
|
2013-09-16 11:49:26 +00:00
|
|
|
"%04d-%02d-%02d %02d:%02d:%02d", prefix,
|
|
|
|
crl->next_update.year, crl->next_update.mon,
|
|
|
|
crl->next_update.day, crl->next_update.hour,
|
|
|
|
crl->next_update.min, crl->next_update.sec );
|
2015-06-22 09:12:02 +00:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
2013-09-16 11:49:26 +00:00
|
|
|
|
|
|
|
entry = &crl->entry;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = mbedtls_snprintf( p, n, "\n%sRevoked certificates:",
|
2013-09-16 11:49:26 +00:00
|
|
|
prefix );
|
2015-06-22 09:12:02 +00:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
2013-09-16 11:49:26 +00:00
|
|
|
|
|
|
|
while( entry != NULL && entry->raw.len != 0 )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = mbedtls_snprintf( p, n, "\n%sserial number: ",
|
2013-09-16 11:49:26 +00:00
|
|
|
prefix );
|
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_x509_serial_gets( p, n, &entry->serial );
|
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_snprintf( p, n, " revocation date: " \
|
2013-09-16 11:49:26 +00:00
|
|
|
"%04d-%02d-%02d %02d:%02d:%02d",
|
|
|
|
entry->revocation_date.year, entry->revocation_date.mon,
|
|
|
|
entry->revocation_date.day, entry->revocation_date.hour,
|
|
|
|
entry->revocation_date.min, entry->revocation_date.sec );
|
2015-06-22 09:12:02 +00:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
2013-09-16 11:49:26 +00:00
|
|
|
|
|
|
|
entry = entry->next;
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
|
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_x509_sig_alg_gets( p, n, &crl->sig_oid, crl->sig_pk, crl->sig_md,
|
2014-06-05 15:07:30 +00:00
|
|
|
crl->sig_opts );
|
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_snprintf( p, n, "\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 ) );
|
|
|
|
}
|
|
|
|
|
2013-09-18 09:58:25 +00:00
|
|
|
/*
|
|
|
|
* Initialize a CRL chain
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
void mbedtls_x509_crl_init( mbedtls_x509_crl *crl )
|
2013-09-18 09:58:25 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
memset( crl, 0, sizeof(mbedtls_x509_crl) );
|
2013-09-18 09:58:25 +00:00
|
|
|
}
|
|
|
|
|
2013-09-16 11:49:26 +00:00
|
|
|
/*
|
|
|
|
* Unallocate all CRL data
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
void mbedtls_x509_crl_free( mbedtls_x509_crl *crl )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crl *crl_cur = crl;
|
|
|
|
mbedtls_x509_crl *crl_prv;
|
|
|
|
mbedtls_x509_name *name_cur;
|
|
|
|
mbedtls_x509_name *name_prv;
|
|
|
|
mbedtls_x509_crl_entry *entry_cur;
|
|
|
|
mbedtls_x509_crl_entry *entry_prv;
|
2013-09-16 11:49:26 +00:00
|
|
|
|
|
|
|
if( crl == NULL )
|
|
|
|
return;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
|
|
|
|
mbedtls_free( crl_cur->sig_opts );
|
2014-06-05 13:14:28 +00:00
|
|
|
#endif
|
|
|
|
|
2013-09-16 11:49:26 +00:00
|
|
|
name_cur = crl_cur->issuer.next;
|
|
|
|
while( name_cur != NULL )
|
|
|
|
{
|
|
|
|
name_prv = name_cur;
|
|
|
|
name_cur = name_cur->next;
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
|
|
|
|
mbedtls_free( name_prv );
|
2013-09-16 11:49:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
entry_cur = crl_cur->entry.next;
|
|
|
|
while( entry_cur != NULL )
|
|
|
|
{
|
|
|
|
entry_prv = entry_cur;
|
|
|
|
entry_cur = entry_cur->next;
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_zeroize( entry_prv, sizeof( mbedtls_x509_crl_entry ) );
|
|
|
|
mbedtls_free( entry_prv );
|
2013-09-16 11:49:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( crl_cur->raw.p != NULL )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_zeroize( crl_cur->raw.p, crl_cur->raw.len );
|
|
|
|
mbedtls_free( crl_cur->raw.p );
|
2013-09-16 11:49:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
crl_cur = crl_cur->next;
|
|
|
|
}
|
|
|
|
while( crl_cur != NULL );
|
|
|
|
|
|
|
|
crl_cur = crl;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
crl_prv = crl_cur;
|
|
|
|
crl_cur = crl_cur->next;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_zeroize( crl_prv, sizeof( mbedtls_x509_crl ) );
|
2013-09-16 11:49:26 +00:00
|
|
|
if( crl_prv != crl )
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_free( crl_prv );
|
2013-09-16 11:49:26 +00:00
|
|
|
}
|
|
|
|
while( crl_cur != NULL );
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_X509_CRL_PARSE_C */
|