2009-01-03 21:22:43 +00:00
|
|
|
/*
|
2014-06-12 20:34:55 +00:00
|
|
|
* X.509 certificate parsing and verification
|
2009-01-03 21:22:43 +00:00
|
|
|
*
|
2015-07-27 09:11:48 +00:00
|
|
|
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
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:
|
2010-07-18 20:36:00 +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
|
2010-07-18 20:36:00 +00:00
|
|
|
*
|
2015-09-04 12:21:07 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2009-01-03 21:22:43 +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.
|
2009-01-03 21:22:43 +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.
|
|
|
|
*
|
|
|
|
* **********
|
|
|
|
*
|
2015-09-04 12:21:07 +00:00
|
|
|
* This file is part of mbed TLS (https://tls.mbed.org)
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
|
|
|
/*
|
2012-02-16 15:28:14 +00:00
|
|
|
* The ITU-T X.509 standard defines a certificate format for PKI.
|
2009-01-03 21:22:43 +00:00
|
|
|
*
|
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)
|
2009-01-03 21:22:43 +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
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/x509_crt.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-15 18:43:33 +00:00
|
|
|
#endif
|
2009-01-03 21:22:43 +00:00
|
|
|
|
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-07-03 11:37:05 +00:00
|
|
|
#else
|
2018-10-03 14:11:19 +00:00
|
|
|
#include <stdio.h>
|
2015-02-06 13:43:58 +00:00
|
|
|
#include <stdlib.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-07-03 11:37:05 +00:00
|
|
|
#endif
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_THREADING_C)
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/threading.h"
|
2013-11-28 16:11:54 +00:00
|
|
|
#endif
|
|
|
|
|
2013-10-28 17:48:30 +00:00
|
|
|
#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
|
2011-11-18 14:26:47 +00:00
|
|
|
#include <windows.h>
|
|
|
|
#else
|
2009-01-03 21:22:43 +00:00
|
|
|
#include <time.h>
|
2011-11-18 14:26:47 +00:00
|
|
|
#endif
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_FS_IO)
|
2015-02-06 13:43:58 +00:00
|
|
|
#include <stdio.h>
|
2014-04-04 13:08:20 +00:00
|
|
|
#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
|
2012-06-04 12:46:42 +00:00
|
|
|
#include <sys/types.h>
|
2013-06-24 17:22:42 +00:00
|
|
|
#include <sys/stat.h>
|
2012-06-04 12:46:42 +00:00
|
|
|
#include <dirent.h>
|
2015-02-06 13:43:58 +00:00
|
|
|
#endif /* !_WIN32 || EFIX64 || EFI32 */
|
2011-04-25 15:28:35 +00:00
|
|
|
#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;
|
|
|
|
}
|
|
|
|
|
2015-06-15 12:34:59 +00:00
|
|
|
/*
|
|
|
|
* Default profile
|
|
|
|
*/
|
|
|
|
const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
|
|
|
|
{
|
2017-05-12 11:16:40 +00:00
|
|
|
#if defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_CERTIFICATES)
|
2017-05-04 14:17:21 +00:00
|
|
|
/* Allow SHA-1 (weak, but still safe in controlled environments) */
|
2015-06-15 13:33:19 +00:00
|
|
|
MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 ) |
|
2017-05-04 14:17:21 +00:00
|
|
|
#endif
|
|
|
|
/* Only SHA-2 hashes */
|
2015-06-15 13:33:19 +00:00
|
|
|
MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) |
|
|
|
|
MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
|
|
|
|
MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
|
|
|
|
MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
|
|
|
|
0xFFFFFFF, /* Any PK alg */
|
|
|
|
0xFFFFFFF, /* Any curve */
|
2015-06-15 12:34:59 +00:00
|
|
|
2048,
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Next-default profile
|
|
|
|
*/
|
2015-06-15 13:33:19 +00:00
|
|
|
const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
|
2015-06-15 12:34:59 +00:00
|
|
|
{
|
2015-06-15 13:33:19 +00:00
|
|
|
/* Hashes from SHA-256 and above */
|
|
|
|
MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
|
|
|
|
MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
|
|
|
|
MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
|
|
|
|
0xFFFFFFF, /* Any PK alg */
|
2015-06-15 12:34:59 +00:00
|
|
|
#if defined(MBEDTLS_ECP_C)
|
2015-06-15 13:33:19 +00:00
|
|
|
/* Curves at or above 128-bit security level */
|
|
|
|
MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
|
|
|
|
MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
|
|
|
|
MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
|
|
|
|
MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
|
|
|
|
MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
|
|
|
|
MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
|
|
|
|
MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256K1 ),
|
2015-06-15 12:34:59 +00:00
|
|
|
#else
|
2015-06-15 13:33:19 +00:00
|
|
|
0,
|
2015-06-15 12:34:59 +00:00
|
|
|
#endif
|
|
|
|
2048,
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* NSA Suite B Profile
|
|
|
|
*/
|
2015-06-15 13:33:19 +00:00
|
|
|
const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
|
2015-06-15 12:34:59 +00:00
|
|
|
{
|
2015-06-15 13:33:19 +00:00
|
|
|
/* Only SHA-256 and 384 */
|
|
|
|
MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
|
|
|
|
MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ),
|
|
|
|
/* Only ECDSA */
|
2018-02-06 13:59:38 +00:00
|
|
|
MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECDSA ) |
|
|
|
|
MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECKEY ),
|
2015-06-15 12:34:59 +00:00
|
|
|
#if defined(MBEDTLS_ECP_C)
|
2015-06-15 13:33:19 +00:00
|
|
|
/* Only NIST P-256 and P-384 */
|
|
|
|
MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
|
|
|
|
MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ),
|
2015-06-15 12:34:59 +00:00
|
|
|
#else
|
2015-06-15 13:33:19 +00:00
|
|
|
0,
|
2015-06-15 12:34:59 +00:00
|
|
|
#endif
|
2015-06-15 13:33:19 +00:00
|
|
|
0,
|
2015-06-15 12:34:59 +00:00
|
|
|
};
|
|
|
|
|
2015-06-15 14:17:55 +00:00
|
|
|
/*
|
|
|
|
* Check md_alg against profile
|
|
|
|
* Return 0 if md_alg acceptable for this profile, -1 otherwise
|
|
|
|
*/
|
|
|
|
static int x509_profile_check_md_alg( const mbedtls_x509_crt_profile *profile,
|
|
|
|
mbedtls_md_type_t md_alg )
|
|
|
|
{
|
2018-05-11 09:06:29 +00:00
|
|
|
if( md_alg == MBEDTLS_MD_NONE )
|
|
|
|
return( -1 );
|
|
|
|
|
2015-06-15 14:17:55 +00:00
|
|
|
if( ( profile->allowed_mds & MBEDTLS_X509_ID_FLAG( md_alg ) ) != 0 )
|
|
|
|
return( 0 );
|
|
|
|
|
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check pk_alg against profile
|
|
|
|
* Return 0 if pk_alg acceptable for this profile, -1 otherwise
|
|
|
|
*/
|
|
|
|
static int x509_profile_check_pk_alg( const mbedtls_x509_crt_profile *profile,
|
|
|
|
mbedtls_pk_type_t pk_alg )
|
|
|
|
{
|
2018-05-11 09:06:29 +00:00
|
|
|
if( pk_alg == MBEDTLS_PK_NONE )
|
|
|
|
return( -1 );
|
|
|
|
|
2015-06-15 14:17:55 +00:00
|
|
|
if( ( profile->allowed_pks & MBEDTLS_X509_ID_FLAG( pk_alg ) ) != 0 )
|
|
|
|
return( 0 );
|
|
|
|
|
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check key against profile
|
|
|
|
* Return 0 if pk_alg acceptable for this profile, -1 otherwise
|
|
|
|
*/
|
|
|
|
static int x509_profile_check_key( const mbedtls_x509_crt_profile *profile,
|
|
|
|
mbedtls_pk_type_t pk_alg,
|
|
|
|
const mbedtls_pk_context *pk )
|
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_RSA_C)
|
|
|
|
if( pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS )
|
|
|
|
{
|
2015-06-18 14:43:38 +00:00
|
|
|
if( mbedtls_pk_get_bitlen( pk ) >= profile->rsa_min_bitlen )
|
2015-06-15 14:17:55 +00:00
|
|
|
return( 0 );
|
|
|
|
|
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-10-23 12:08:48 +00:00
|
|
|
#if defined(MBEDTLS_ECP_C)
|
|
|
|
if( pk_alg == MBEDTLS_PK_ECDSA ||
|
|
|
|
pk_alg == MBEDTLS_PK_ECKEY ||
|
|
|
|
pk_alg == MBEDTLS_PK_ECKEY_DH )
|
2015-06-15 14:17:55 +00:00
|
|
|
{
|
|
|
|
mbedtls_ecp_group_id gid = mbedtls_pk_ec( *pk )->grp.id;
|
|
|
|
|
2018-05-11 09:06:29 +00:00
|
|
|
if( gid == MBEDTLS_ECP_DP_NONE )
|
|
|
|
return( -1 );
|
|
|
|
|
2015-06-15 14:17:55 +00:00
|
|
|
if( ( profile->allowed_curves & MBEDTLS_X509_ID_FLAG( gid ) ) != 0 )
|
|
|
|
return( 0 );
|
|
|
|
|
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
/*
|
|
|
|
* Version ::= INTEGER { v1(0), v2(1), v3(2) }
|
|
|
|
*/
|
|
|
|
static int x509_get_version( unsigned char **p,
|
2010-03-16 21:09:09 +00:00
|
|
|
const unsigned char *end,
|
2009-01-03 21:22:43 +00:00
|
|
|
int *ver )
|
|
|
|
{
|
2011-04-24 08:57:21 +00:00
|
|
|
int ret;
|
|
|
|
size_t len;
|
2009-01-03 21:22:43 +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 | 0 ) ) != 0 )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
|
2011-10-19 14:15:17 +00:00
|
|
|
{
|
|
|
|
*ver = 0;
|
|
|
|
return( 0 );
|
|
|
|
}
|
2009-01-03 21:22:43 +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
|
|
|
return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
end = *p + len;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_VERSION + ret );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
if( *p != end )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_X509_INVALID_VERSION +
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2009-05-02 15:13:40 +00:00
|
|
|
/*
|
|
|
|
* Validity ::= SEQUENCE {
|
|
|
|
* notBefore Time,
|
|
|
|
* notAfter Time }
|
|
|
|
*/
|
|
|
|
static int x509_get_dates( unsigned char **p,
|
2010-03-16 21:09:09 +00:00
|
|
|
const unsigned char *end,
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_time *from,
|
|
|
|
mbedtls_x509_time *to )
|
2009-05-02 15:13:40 +00:00
|
|
|
{
|
2011-04-24 08:57:21 +00:00
|
|
|
int ret;
|
|
|
|
size_t len;
|
2009-01-03 21:22:43 +00:00
|
|
|
|
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_DATE + ret );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2009-05-02 15:13:40 +00:00
|
|
|
end = *p + len;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 )
|
2009-05-02 15:13:40 +00:00
|
|
|
return( ret );
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 )
|
2009-05-02 15:13:40 +00:00
|
|
|
return( ret );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
if( *p != end )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_X509_INVALID_DATE +
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* X.509 v2/v3 unique identifier (not parsed)
|
|
|
|
*/
|
|
|
|
static int x509_get_uid( unsigned char **p,
|
2010-03-16 21:09:09 +00:00
|
|
|
const unsigned char *end,
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_buf *uid, int n )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if( *p == end )
|
|
|
|
return( 0 );
|
|
|
|
|
|
|
|
uid->tag = **p;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ret = mbedtls_asn1_get_tag( p, end, &uid->len,
|
|
|
|
MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
|
2009-01-03 21:22:43 +00:00
|
|
|
return( 0 );
|
|
|
|
|
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
|
|
|
return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uid->p = *p;
|
|
|
|
*p += uid->len;
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2013-09-16 11:49:26 +00:00
|
|
|
static int x509_get_basic_constraints( unsigned char **p,
|
|
|
|
const unsigned char *end,
|
|
|
|
int *ca_istrue,
|
|
|
|
int *max_pathlen )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2011-04-24 08:57:21 +00:00
|
|
|
int ret;
|
|
|
|
size_t len;
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
/*
|
2013-09-16 11:49:26 +00:00
|
|
|
* BasicConstraints ::= SEQUENCE {
|
|
|
|
* cA BOOLEAN DEFAULT FALSE,
|
|
|
|
* pathLenConstraint INTEGER (0..MAX) OPTIONAL }
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
2013-09-16 11:49:26 +00:00
|
|
|
*ca_istrue = 0; /* DEFAULT FALSE */
|
|
|
|
*max_pathlen = 0; /* endless */
|
|
|
|
|
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 );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2013-09-16 11:49:26 +00:00
|
|
|
if( *p == end )
|
2014-06-17 12:06:49 +00:00
|
|
|
return( 0 );
|
2009-05-02 15:13:40 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ret = mbedtls_asn1_get_bool( p, end, ca_istrue ) ) != 0 )
|
2009-05-02 15:13:40 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
|
|
|
|
ret = mbedtls_asn1_get_int( p, end, ca_istrue );
|
2009-05-02 15:13:40 +00:00
|
|
|
|
2013-09-16 11:49:26 +00:00
|
|
|
if( ret != 0 )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
|
2009-05-02 15:13:40 +00:00
|
|
|
|
2013-09-16 11:49:26 +00:00
|
|
|
if( *ca_istrue != 0 )
|
|
|
|
*ca_istrue = 1;
|
2009-05-02 15:13:40 +00:00
|
|
|
}
|
|
|
|
|
2013-09-16 11:49:26 +00:00
|
|
|
if( *p == end )
|
2014-06-17 12:06:49 +00:00
|
|
|
return( 0 );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 )
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2009-05-02 15:13:40 +00:00
|
|
|
if( *p != end )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
|
2009-05-02 15:13:40 +00:00
|
|
|
|
2020-04-14 13:49:52 +00:00
|
|
|
/* Do not accept max_pathlen equal to INT_MAX to avoid a signed integer
|
|
|
|
* overflow, which is an undefined behavior. */
|
|
|
|
if( *max_pathlen == INT_MAX )
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
|
|
|
|
MBEDTLS_ERR_ASN1_INVALID_LENGTH );
|
|
|
|
|
2013-09-16 11:49:26 +00:00
|
|
|
(*max_pathlen)++;
|
|
|
|
|
2014-06-17 12:06:49 +00:00
|
|
|
return( 0 );
|
2009-05-02 15:13:40 +00:00
|
|
|
}
|
|
|
|
|
2013-09-16 11:49:26 +00:00
|
|
|
static int x509_get_ns_cert_type( unsigned char **p,
|
|
|
|
const unsigned char *end,
|
|
|
|
unsigned char *ns_cert_type)
|
2011-10-12 09:58:41 +00:00
|
|
|
{
|
|
|
|
int ret;
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_bitstring bs = { 0, 0, NULL };
|
2011-10-12 09:58:41 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
|
2011-10-12 09:58:41 +00:00
|
|
|
|
2013-09-16 11:49:26 +00:00
|
|
|
if( bs.len != 1 )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
|
|
|
|
MBEDTLS_ERR_ASN1_INVALID_LENGTH );
|
2011-01-15 16:57:55 +00:00
|
|
|
|
|
|
|
/* Get actual bitstring */
|
|
|
|
*ns_cert_type = *bs.p;
|
2014-06-17 12:06:49 +00:00
|
|
|
return( 0 );
|
2011-01-15 16:57:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int x509_get_key_usage( unsigned char **p,
|
|
|
|
const unsigned char *end,
|
2015-03-27 15:50:00 +00:00
|
|
|
unsigned int *key_usage)
|
2011-01-15 16:57:55 +00:00
|
|
|
{
|
|
|
|
int ret;
|
2015-06-23 08:14:36 +00:00
|
|
|
size_t i;
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_bitstring bs = { 0, 0, NULL };
|
2011-01-15 16:57:55 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
|
2011-01-15 16:57:55 +00:00
|
|
|
|
2012-08-23 13:03:52 +00:00
|
|
|
if( bs.len < 1 )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
|
|
|
|
MBEDTLS_ERR_ASN1_INVALID_LENGTH );
|
2011-01-15 16:57:55 +00:00
|
|
|
|
|
|
|
/* Get actual bitstring */
|
2015-06-23 08:14:36 +00:00
|
|
|
*key_usage = 0;
|
|
|
|
for( i = 0; i < bs.len && i < sizeof( unsigned int ); i++ )
|
|
|
|
{
|
|
|
|
*key_usage |= (unsigned int) bs.p[i] << (8*i);
|
|
|
|
}
|
|
|
|
|
2014-06-17 12:06:49 +00:00
|
|
|
return( 0 );
|
2011-01-15 16:57:55 +00:00
|
|
|
}
|
|
|
|
|
2009-05-02 15:13:40 +00:00
|
|
|
/*
|
2011-01-15 16:57:55 +00:00
|
|
|
* ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
|
|
|
|
*
|
|
|
|
* KeyPurposeId ::= OBJECT IDENTIFIER
|
|
|
|
*/
|
|
|
|
static int x509_get_ext_key_usage( unsigned char **p,
|
|
|
|
const unsigned char *end,
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_sequence *ext_key_usage)
|
2011-01-15 16:57:55 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ret = mbedtls_asn1_get_sequence_of( p, end, ext_key_usage, MBEDTLS_ASN1_OID ) ) != 0 )
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
|
2011-01-15 16:57:55 +00:00
|
|
|
|
|
|
|
/* Sequence length must be >= 1 */
|
|
|
|
if( ext_key_usage->buf.p == NULL )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
|
|
|
|
MBEDTLS_ERR_ASN1_INVALID_LENGTH );
|
2011-01-15 16:57:55 +00:00
|
|
|
|
2014-06-17 12:06:49 +00:00
|
|
|
return( 0 );
|
2011-01-15 16:57:55 +00:00
|
|
|
}
|
|
|
|
|
2012-02-11 16:09:32 +00:00
|
|
|
/*
|
|
|
|
* SubjectAltName ::= GeneralNames
|
|
|
|
*
|
|
|
|
* GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
|
|
|
|
*
|
|
|
|
* GeneralName ::= CHOICE {
|
|
|
|
* otherName [0] OtherName,
|
|
|
|
* rfc822Name [1] IA5String,
|
|
|
|
* dNSName [2] IA5String,
|
|
|
|
* x400Address [3] ORAddress,
|
|
|
|
* directoryName [4] Name,
|
|
|
|
* ediPartyName [5] EDIPartyName,
|
|
|
|
* uniformResourceIdentifier [6] IA5String,
|
|
|
|
* iPAddress [7] OCTET STRING,
|
|
|
|
* registeredID [8] OBJECT IDENTIFIER }
|
|
|
|
*
|
|
|
|
* OtherName ::= SEQUENCE {
|
|
|
|
* type-id OBJECT IDENTIFIER,
|
|
|
|
* value [0] EXPLICIT ANY DEFINED BY type-id }
|
|
|
|
*
|
|
|
|
* EDIPartyName ::= SEQUENCE {
|
|
|
|
* nameAssigner [0] DirectoryString OPTIONAL,
|
|
|
|
* partyName [1] DirectoryString }
|
|
|
|
*
|
2015-01-22 16:11:05 +00:00
|
|
|
* NOTE: we only parse and use dNSName at this point.
|
2012-02-11 16:09:32 +00:00
|
|
|
*/
|
|
|
|
static int x509_get_subject_alt_name( unsigned char **p,
|
|
|
|
const unsigned char *end,
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_sequence *subject_alt_name )
|
2012-02-11 16:09:32 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t len, tag_len;
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_asn1_buf *buf;
|
2012-02-11 16:09:32 +00:00
|
|
|
unsigned char tag;
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_asn1_sequence *cur = subject_alt_name;
|
2012-02-11 16:09:32 +00:00
|
|
|
|
|
|
|
/* Get main 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 );
|
2012-02-11 16:09:32 +00:00
|
|
|
|
|
|
|
if( *p + len != end )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
|
2012-02-11 16:09:32 +00:00
|
|
|
|
|
|
|
while( *p < end )
|
|
|
|
{
|
|
|
|
if( ( end - *p ) < 1 )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
|
|
|
|
MBEDTLS_ERR_ASN1_OUT_OF_DATA );
|
2012-02-11 16:09:32 +00:00
|
|
|
|
|
|
|
tag = **p;
|
|
|
|
(*p)++;
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ret = mbedtls_asn1_get_len( p, end, &tag_len ) ) != 0 )
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
|
2012-02-11 16:09:32 +00:00
|
|
|
|
2017-08-25 16:13:12 +00:00
|
|
|
if( ( tag & MBEDTLS_ASN1_TAG_CLASS_MASK ) !=
|
|
|
|
MBEDTLS_ASN1_CONTEXT_SPECIFIC )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
|
|
|
|
MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
|
2017-08-25 16:13:12 +00:00
|
|
|
}
|
2012-02-11 16:09:32 +00:00
|
|
|
|
2014-04-01 11:43:28 +00:00
|
|
|
/* Skip everything but DNS name */
|
2015-04-08 10:49:31 +00:00
|
|
|
if( tag != ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | 2 ) )
|
2012-02-11 16:09:32 +00:00
|
|
|
{
|
|
|
|
*p += tag_len;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate and assign next pointer */
|
2014-04-01 11:43:28 +00:00
|
|
|
if( cur->buf.p != NULL )
|
2012-02-11 16:09:32 +00:00
|
|
|
{
|
2014-11-11 22:11:16 +00:00
|
|
|
if( cur->next != NULL )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
|
2014-11-11 22:11:16 +00:00
|
|
|
|
2015-05-26 14:04:06 +00:00
|
|
|
cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
|
2012-02-11 16:09:32 +00:00
|
|
|
|
|
|
|
if( cur->next == NULL )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
|
2015-05-28 07:33:39 +00:00
|
|
|
MBEDTLS_ERR_ASN1_ALLOC_FAILED );
|
2012-02-11 16:09:32 +00:00
|
|
|
|
|
|
|
cur = cur->next;
|
|
|
|
}
|
2014-04-01 11:43:28 +00:00
|
|
|
|
|
|
|
buf = &(cur->buf);
|
|
|
|
buf->tag = tag;
|
|
|
|
buf->p = *p;
|
|
|
|
buf->len = tag_len;
|
|
|
|
*p += buf->len;
|
2012-02-11 16:09:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set final sequence entry's next pointer to NULL */
|
|
|
|
cur->next = NULL;
|
|
|
|
|
|
|
|
if( *p != end )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
|
2012-02-11 16:09:32 +00:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2011-01-15 16:57:55 +00:00
|
|
|
/*
|
|
|
|
* X.509 v3 extensions
|
|
|
|
*
|
2009-05-02 15:13:40 +00:00
|
|
|
*/
|
|
|
|
static int x509_get_crt_ext( unsigned char **p,
|
2010-03-16 21:09:09 +00:00
|
|
|
const unsigned char *end,
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crt *crt )
|
2009-05-02 15:13:40 +00:00
|
|
|
{
|
2011-04-24 08:57:21 +00:00
|
|
|
int ret;
|
|
|
|
size_t len;
|
2009-07-27 21:34:45 +00:00
|
|
|
unsigned char *end_ext_data, *end_ext_octet;
|
2009-05-02 15:13:40 +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
|
|
|
if( *p == end )
|
|
|
|
return( 0 );
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ret = mbedtls_x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
|
2009-05-02 15:13:40 +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 = crt->v3_ext.p + crt->v3_ext.len;
|
2009-01-03 21:22:43 +00:00
|
|
|
while( *p < end )
|
|
|
|
{
|
2011-01-15 16:57:55 +00:00
|
|
|
/*
|
|
|
|
* Extension ::= SEQUENCE {
|
|
|
|
* extnID OBJECT IDENTIFIER,
|
|
|
|
* critical BOOLEAN DEFAULT FALSE,
|
|
|
|
* extnValue OCTET STRING }
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_buf extn_oid = {0, 0, NULL};
|
2011-01-15 16:57:55 +00:00
|
|
|
int is_critical = 0; /* DEFAULT FALSE */
|
2013-04-07 20:00:46 +00:00
|
|
|
int ext_type = 0;
|
2011-01-15 16:57:55 +00:00
|
|
|
|
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 );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2009-07-27 21:34:45 +00:00
|
|
|
end_ext_data = *p + len;
|
|
|
|
|
2011-01-15 16:57:55 +00:00
|
|
|
/* Get extension ID */
|
2018-07-16 08:49:12 +00:00
|
|
|
if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &extn_oid.len,
|
2018-07-24 10:54:39 +00:00
|
|
|
MBEDTLS_ASN1_OID ) ) != 0 )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2018-07-16 08:49:12 +00:00
|
|
|
extn_oid.tag = MBEDTLS_ASN1_OID;
|
2011-01-15 16:57:55 +00:00
|
|
|
extn_oid.p = *p;
|
|
|
|
*p += extn_oid.len;
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2011-01-15 16:57:55 +00:00
|
|
|
/* Get optional critical */
|
2015-04-08 10:49:31 +00:00
|
|
|
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 );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2011-01-15 16:57:55 +00:00
|
|
|
/* Data should be octet string type */
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
|
|
|
|
MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2009-07-27 21:34:45 +00:00
|
|
|
end_ext_octet = *p + len;
|
2010-03-16 21:09:09 +00:00
|
|
|
|
2009-07-27 21:34:45 +00:00
|
|
|
if( end_ext_octet != end_ext_data )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2011-01-15 16:57:55 +00:00
|
|
|
/*
|
|
|
|
* Detect supported extensions
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
|
2013-04-07 20:00:46 +00:00
|
|
|
|
|
|
|
if( ret != 0 )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2013-04-07 20:00:46 +00:00
|
|
|
/* No parser found, skip extension */
|
|
|
|
*p = end_ext_octet;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
|
2013-04-07 20:00:46 +00:00
|
|
|
if( is_critical )
|
|
|
|
{
|
|
|
|
/* Data is marked as critical: fail */
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
|
|
|
|
MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
|
2013-04-07 20:00:46 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-11-12 16:47:28 +00:00
|
|
|
/* Forbid repeated extensions */
|
|
|
|
if( ( crt->ext_types & ext_type ) != 0 )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
|
2014-11-12 16:47:28 +00:00
|
|
|
|
2013-04-07 20:00:46 +00:00
|
|
|
crt->ext_types |= ext_type;
|
|
|
|
|
|
|
|
switch( ext_type )
|
|
|
|
{
|
2015-04-20 11:19:02 +00:00
|
|
|
case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
|
2011-01-15 16:57:55 +00:00
|
|
|
/* Parse basic constraints */
|
|
|
|
if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
|
2011-01-16 21:46:31 +00:00
|
|
|
&crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
|
2014-06-17 12:06:49 +00:00
|
|
|
return( ret );
|
2013-04-07 20:00:46 +00:00
|
|
|
break;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
case MBEDTLS_X509_EXT_KEY_USAGE:
|
2011-01-15 16:57:55 +00:00
|
|
|
/* Parse key usage */
|
|
|
|
if( ( ret = x509_get_key_usage( p, end_ext_octet,
|
|
|
|
&crt->key_usage ) ) != 0 )
|
2014-06-17 12:06:49 +00:00
|
|
|
return( ret );
|
2013-04-07 20:00:46 +00:00
|
|
|
break;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
|
2011-01-15 16:57:55 +00:00
|
|
|
/* Parse extended key usage */
|
|
|
|
if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
|
|
|
|
&crt->ext_key_usage ) ) != 0 )
|
2014-06-17 12:06:49 +00:00
|
|
|
return( ret );
|
2013-04-07 20:00:46 +00:00
|
|
|
break;
|
|
|
|
|
2015-04-20 11:19:02 +00:00
|
|
|
case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
|
2013-04-07 20:00:46 +00:00
|
|
|
/* Parse subject alt name */
|
2012-02-11 16:09:32 +00:00
|
|
|
if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
|
|
|
|
&crt->subject_alt_names ) ) != 0 )
|
2014-06-17 12:06:49 +00:00
|
|
|
return( ret );
|
2013-04-07 20:00:46 +00:00
|
|
|
break;
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
case MBEDTLS_X509_EXT_NS_CERT_TYPE:
|
2013-04-07 20:00:46 +00:00
|
|
|
/* Parse netscape certificate type */
|
|
|
|
if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
|
|
|
|
&crt->ns_cert_type ) ) != 0 )
|
2014-06-17 12:06:49 +00:00
|
|
|
return( ret );
|
2013-04-07 20:00:46 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
|
2011-01-15 16:57:55 +00:00
|
|
|
}
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( *p != end )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2011-12-04 12:24:18 +00:00
|
|
|
* Parse and fill a single X.509 certificate in DER format
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
static int x509_crt_parse_der_core( mbedtls_x509_crt *crt, const unsigned char *buf,
|
2013-09-18 11:46:23 +00:00
|
|
|
size_t buflen )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2011-04-24 08:57:21 +00:00
|
|
|
int ret;
|
2011-05-26 13:16:06 +00:00
|
|
|
size_t len;
|
2012-09-25 12:10:00 +00:00
|
|
|
unsigned char *p, *end, *crt_end;
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
|
2014-01-22 09:12:57 +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 ) );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2009-03-28 18:52:39 +00:00
|
|
|
/*
|
|
|
|
* Check for valid input
|
|
|
|
*/
|
|
|
|
if( crt == NULL || buf == NULL )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
|
2009-03-28 18:52:39 +00:00
|
|
|
|
2016-02-17 14:34:12 +00:00
|
|
|
// Use the original buffer until we figure out actual length
|
|
|
|
p = (unsigned char*) buf;
|
|
|
|
len = buflen;
|
2009-01-03 21:22:43 +00:00
|
|
|
end = p + len;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Certificate ::= SEQUENCE {
|
|
|
|
* tbsCertificate TBSCertificate,
|
|
|
|
* 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 )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crt_free( crt );
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_FORMAT );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
2012-09-25 12:10:00 +00:00
|
|
|
if( len > (size_t) ( end - p ) )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crt_free( crt );
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_FORMAT +
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
2012-09-25 12:10:00 +00:00
|
|
|
crt_end = p + len;
|
2013-06-24 17:21:59 +00:00
|
|
|
|
2016-02-17 14:34:12 +00:00
|
|
|
// Create and populate a new buffer for the raw field
|
|
|
|
crt->raw.len = crt_end - buf;
|
|
|
|
crt->raw.p = p = mbedtls_calloc( 1, crt->raw.len );
|
|
|
|
if( p == NULL )
|
|
|
|
return( MBEDTLS_ERR_X509_ALLOC_FAILED );
|
|
|
|
|
|
|
|
memcpy( p, buf, crt->raw.len );
|
|
|
|
|
2017-09-25 09:47:58 +00:00
|
|
|
// Direct pointers to the new buffer
|
2016-02-17 14:34:12 +00:00
|
|
|
p += crt->raw.len - len;
|
|
|
|
end = crt_end = p + len;
|
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
/*
|
|
|
|
* TBSCertificate ::= SEQUENCE {
|
|
|
|
*/
|
|
|
|
crt->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 )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crt_free( crt );
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
end = p + len;
|
|
|
|
crt->tbs.len = end - crt->tbs.p;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Version ::= INTEGER { v1(0), v2(1), v3(2) }
|
|
|
|
*
|
|
|
|
* CertificateSerialNumber ::= INTEGER
|
|
|
|
*
|
|
|
|
* signature AlgorithmIdentifier
|
|
|
|
*/
|
2013-07-10 11:18:41 +00:00
|
|
|
if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
|
2015-04-08 10:49:31 +00:00
|
|
|
( ret = mbedtls_x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
|
|
|
|
( ret = mbedtls_x509_get_alg( &p, end, &crt->sig_oid,
|
2014-06-05 15:02:24 +00:00
|
|
|
&sig_params1 ) ) != 0 )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crt_free( crt );
|
2009-01-03 21:22:43 +00:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2017-03-09 16:16:11 +00:00
|
|
|
if( crt->version < 0 || crt->version > 2 )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crt_free( crt );
|
|
|
|
return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
2017-03-09 16:16:11 +00:00
|
|
|
crt->version++;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ret = mbedtls_x509_get_sig_alg( &crt->sig_oid, &sig_params1,
|
2014-06-05 13:14:28 +00:00
|
|
|
&crt->sig_md, &crt->sig_pk,
|
|
|
|
&crt->sig_opts ) ) != 0 )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crt_free( crt );
|
2010-03-17 06:56:01 +00:00
|
|
|
return( ret );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* issuer Name
|
|
|
|
*/
|
|
|
|
crt->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 )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crt_free( crt );
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ret = mbedtls_x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crt_free( crt );
|
2009-01-03 21:22:43 +00:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
crt->issuer_raw.len = p - crt->issuer_raw.p;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Validity ::= SEQUENCE {
|
|
|
|
* notBefore Time,
|
|
|
|
* notAfter Time }
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
|
|
|
|
&crt->valid_to ) ) != 0 )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crt_free( crt );
|
2009-01-03 21:22:43 +00:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* subject Name
|
|
|
|
*/
|
|
|
|
crt->subject_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 )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crt_free( crt );
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( len && ( ret = mbedtls_x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crt_free( crt );
|
2009-01-03 21:22:43 +00:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
crt->subject_raw.len = p - crt->subject_raw.p;
|
|
|
|
|
|
|
|
/*
|
2013-07-09 10:13:24 +00:00
|
|
|
* SubjectPublicKeyInfo
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crt_free( crt );
|
2009-01-03 21:22:43 +00:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
|
|
|
|
* -- If present, version shall be v2 or v3
|
|
|
|
* subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
|
|
|
|
* -- If present, version shall be v2 or v3
|
|
|
|
* extensions [3] EXPLICIT Extensions OPTIONAL
|
|
|
|
* -- If present, version shall be v3
|
|
|
|
*/
|
|
|
|
if( crt->version == 2 || crt->version == 3 )
|
|
|
|
{
|
|
|
|
ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
|
|
|
|
if( ret != 0 )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crt_free( crt );
|
2009-01-03 21:22:43 +00:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( crt->version == 2 || crt->version == 3 )
|
|
|
|
{
|
|
|
|
ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
|
|
|
|
if( ret != 0 )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crt_free( crt );
|
2013-09-16 11:49:26 +00:00
|
|
|
return( ret );
|
|
|
|
}
|
2009-05-02 15:13:40 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if !defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3)
|
2013-09-16 11:49:26 +00:00
|
|
|
if( crt->version == 3 )
|
2013-09-23 13:01:36 +00:00
|
|
|
#endif
|
2015-03-27 15:15:55 +00:00
|
|
|
{
|
2014-06-17 14:39:18 +00:00
|
|
|
ret = x509_get_crt_ext( &p, end, crt );
|
2009-05-02 15:13:40 +00:00
|
|
|
if( ret != 0 )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crt_free( crt );
|
2009-05-02 15:13:40 +00:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( p != end )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crt_free( crt );
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_FORMAT +
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
|
2009-05-02 15:13:40 +00:00
|
|
|
}
|
|
|
|
|
2013-09-16 11:49:26 +00:00
|
|
|
end = crt_end;
|
2009-05-02 15:13:40 +00:00
|
|
|
|
|
|
|
/*
|
2013-09-16 11:49:26 +00:00
|
|
|
* }
|
|
|
|
* -- end of TBSCertificate
|
|
|
|
*
|
2009-05-02 15:13:40 +00:00
|
|
|
* 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 )
|
2009-05-02 15:13:40 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crt_free( crt );
|
2009-05-02 15:13:40 +00:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2015-03-27 15:30:47 +00:00
|
|
|
if( crt->sig_oid.len != sig_oid2.len ||
|
|
|
|
memcmp( crt->sig_oid.p, sig_oid2.p, crt->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 ) )
|
2009-05-02 15:13:40 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crt_free( crt );
|
|
|
|
return( MBEDTLS_ERR_X509_SIG_MISMATCH );
|
2009-05-02 15:13:40 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ret = mbedtls_x509_get_sig( &p, end, &crt->sig ) ) != 0 )
|
2009-05-02 15:13:40 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crt_free( crt );
|
2009-05-02 15:13:40 +00:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( p != end )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crt_free( crt );
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_FORMAT +
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
|
2009-05-02 15:13:40 +00:00
|
|
|
}
|
|
|
|
|
2013-09-16 11:49:26 +00:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse one X.509 certificate in DER format from a buffer and add them to a
|
|
|
|
* chained list
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain, const unsigned char *buf,
|
2013-09-18 11:46:23 +00:00
|
|
|
size_t buflen )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
|
|
|
int ret;
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crt *crt = chain, *prev = NULL;
|
2013-09-16 11:49:26 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for valid input
|
|
|
|
*/
|
|
|
|
if( crt == 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
|
|
|
|
|
|
|
while( crt->version != 0 && crt->next != NULL )
|
|
|
|
{
|
|
|
|
prev = crt;
|
|
|
|
crt = crt->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add new certificate on the end of the chain if needed.
|
|
|
|
*/
|
2014-06-17 14:39:18 +00:00
|
|
|
if( crt->version != 0 && crt->next == NULL )
|
2009-05-02 15:13:40 +00:00
|
|
|
{
|
2015-05-26 14:04:06 +00:00
|
|
|
crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
|
2009-05-02 15:13:40 +00:00
|
|
|
|
2013-09-16 11:49:26 +00:00
|
|
|
if( crt->next == NULL )
|
2015-05-28 07:33:39 +00:00
|
|
|
return( MBEDTLS_ERR_X509_ALLOC_FAILED );
|
2009-05-02 15:13:40 +00:00
|
|
|
|
2013-09-16 11:49:26 +00:00
|
|
|
prev = crt;
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crt_init( crt->next );
|
2013-09-16 11:49:26 +00:00
|
|
|
crt = crt->next;
|
|
|
|
}
|
|
|
|
|
2013-09-18 11:46:23 +00:00
|
|
|
if( ( ret = x509_crt_parse_der_core( crt, buf, buflen ) ) != 0 )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
|
|
|
if( prev )
|
|
|
|
prev->next = NULL;
|
|
|
|
|
|
|
|
if( crt != chain )
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_free( crt );
|
2009-05-02 15:13:40 +00:00
|
|
|
|
2013-09-16 11:49:26 +00:00
|
|
|
return( ret );
|
2009-05-02 15:13:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2009-04-19 18:44:26 +00:00
|
|
|
/*
|
2014-05-01 12:18:25 +00:00
|
|
|
* Parse one or more PEM certificates from a buffer and add them to the chained
|
|
|
|
* list
|
2009-04-19 18:44:26 +00:00
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen )
|
2009-04-19 18:44:26 +00:00
|
|
|
{
|
2016-05-31 13:03:54 +00:00
|
|
|
#if defined(MBEDTLS_PEM_PARSE_C)
|
2016-12-07 15:05:53 +00:00
|
|
|
int success = 0, first_error = 0, total_failed = 0;
|
2015-04-08 10:49:31 +00:00
|
|
|
int buf_format = MBEDTLS_X509_FORMAT_DER;
|
2016-05-31 13:03:54 +00:00
|
|
|
#endif
|
2009-04-19 18:44:26 +00:00
|
|
|
|
2013-09-16 11:49:26 +00:00
|
|
|
/*
|
|
|
|
* Check for valid input
|
|
|
|
*/
|
|
|
|
if( chain == NULL || buf == NULL )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
|
2009-04-19 18:44:26 +00:00
|
|
|
|
2013-09-16 11:49:26 +00:00
|
|
|
/*
|
|
|
|
* Determine buffer content. Buffer contains either one DER certificate or
|
|
|
|
* one or more PEM certificates.
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_PEM_PARSE_C)
|
2015-05-12 10:43:54 +00:00
|
|
|
if( buflen != 0 && buf[buflen - 1] == '\0' &&
|
2015-05-12 09:20:10 +00:00
|
|
|
strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
buf_format = MBEDTLS_X509_FORMAT_PEM;
|
2015-05-12 09:20:10 +00:00
|
|
|
}
|
2009-04-19 18:44:26 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( buf_format == MBEDTLS_X509_FORMAT_DER )
|
|
|
|
return mbedtls_x509_crt_parse_der( chain, buf, buflen );
|
2016-05-31 13:03:54 +00:00
|
|
|
#else
|
|
|
|
return mbedtls_x509_crt_parse_der( chain, buf, buflen );
|
|
|
|
#endif
|
2013-08-19 12:29:31 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_PEM_PARSE_C)
|
|
|
|
if( buf_format == MBEDTLS_X509_FORMAT_PEM )
|
2013-05-14 11:26:51 +00:00
|
|
|
{
|
2013-09-16 11:49:26 +00:00
|
|
|
int ret;
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_pem_context pem;
|
2009-04-19 18:44:26 +00:00
|
|
|
|
2015-05-12 09:20:10 +00:00
|
|
|
/* 1 rather than 0 since the terminating NULL byte is counted in */
|
|
|
|
while( buflen > 1 )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
|
|
|
size_t use_len;
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_pem_init( &pem );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2015-05-12 09:20:10 +00:00
|
|
|
/* If we get there, we know the string is null-terminated */
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = mbedtls_pem_read_buffer( &pem,
|
2013-09-16 11:49:26 +00:00
|
|
|
"-----BEGIN CERTIFICATE-----",
|
|
|
|
"-----END CERTIFICATE-----",
|
|
|
|
buf, NULL, 0, &use_len );
|
|
|
|
|
|
|
|
if( ret == 0 )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Was PEM encoded
|
|
|
|
*/
|
|
|
|
buflen -= use_len;
|
|
|
|
buf += use_len;
|
|
|
|
}
|
2015-04-08 10:49:31 +00:00
|
|
|
else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
|
|
|
return( ret );
|
|
|
|
}
|
2015-04-08 10:49:31 +00:00
|
|
|
else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_pem_free( &pem );
|
2009-04-19 18:44:26 +00:00
|
|
|
|
2013-09-16 11:49:26 +00:00
|
|
|
/*
|
|
|
|
* PEM header and footer were found
|
|
|
|
*/
|
|
|
|
buflen -= use_len;
|
|
|
|
buf += use_len;
|
2009-04-19 18:44:26 +00:00
|
|
|
|
2013-09-16 11:49:26 +00:00
|
|
|
if( first_error == 0 )
|
|
|
|
first_error = ret;
|
2009-04-19 18:44:26 +00:00
|
|
|
|
2014-09-26 12:53:04 +00:00
|
|
|
total_failed++;
|
2013-09-16 11:49:26 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_pem_free( &pem );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
|
|
|
if( ret != 0 )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Quit parsing on a memory error
|
|
|
|
*/
|
2015-05-28 07:33:39 +00:00
|
|
|
if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
|
2013-09-16 11:49:26 +00:00
|
|
|
return( ret );
|
|
|
|
|
|
|
|
if( first_error == 0 )
|
|
|
|
first_error = ret;
|
|
|
|
|
|
|
|
total_failed++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
success = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( success )
|
|
|
|
return( total_failed );
|
|
|
|
else if( first_error )
|
|
|
|
return( first_error );
|
|
|
|
else
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
|
2016-05-31 13:03:54 +00:00
|
|
|
#endif /* MBEDTLS_PEM_PARSE_C */
|
2009-04-19 18:44:26 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_FS_IO)
|
2009-01-03 21:22:43 +00:00
|
|
|
/*
|
|
|
|
* Load one or more certificates and add them to the chained list
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
|
2009-01-03 21:22:43 +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 )
|
2011-12-10 21:55:01 +00:00
|
|
|
return( ret );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = mbedtls_x509_crt_parse( chain, buf, n );
|
2009-01-03 21:22:43 +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 );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
|
2012-06-04 12:46:42 +00:00
|
|
|
{
|
|
|
|
int ret = 0;
|
2013-10-28 17:48:30 +00:00
|
|
|
#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
|
2012-10-01 21:13:10 +00:00
|
|
|
int w_ret;
|
|
|
|
WCHAR szDir[MAX_PATH];
|
|
|
|
char filename[MAX_PATH];
|
2014-05-01 11:03:14 +00:00
|
|
|
char *p;
|
2015-10-21 08:16:29 +00:00
|
|
|
size_t len = strlen( path );
|
2012-10-01 21:13:10 +00:00
|
|
|
|
2014-05-01 11:03:14 +00:00
|
|
|
WIN32_FIND_DATAW file_data;
|
2012-06-04 12:46:42 +00:00
|
|
|
HANDLE hFind;
|
2012-11-02 11:06:08 +00:00
|
|
|
|
|
|
|
if( len > MAX_PATH - 3 )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
|
2012-06-04 12:46:42 +00:00
|
|
|
|
2014-05-01 11:03:14 +00:00
|
|
|
memset( szDir, 0, sizeof(szDir) );
|
|
|
|
memset( filename, 0, MAX_PATH );
|
|
|
|
memcpy( filename, path, len );
|
|
|
|
filename[len++] = '\\';
|
|
|
|
p = filename + len;
|
2012-11-02 11:06:08 +00:00
|
|
|
filename[len++] = '*';
|
2012-10-01 21:13:10 +00:00
|
|
|
|
2016-11-03 01:11:37 +00:00
|
|
|
w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
|
2014-05-01 12:18:25 +00:00
|
|
|
MAX_PATH - 3 );
|
2015-01-23 17:50:34 +00:00
|
|
|
if( w_ret == 0 )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
|
2012-06-04 12:46:42 +00:00
|
|
|
|
2012-11-02 12:53:26 +00:00
|
|
|
hFind = FindFirstFileW( szDir, &file_data );
|
2014-06-17 14:39:18 +00:00
|
|
|
if( hFind == INVALID_HANDLE_VALUE )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
|
2012-06-04 12:46:42 +00:00
|
|
|
|
2012-11-02 11:06:08 +00:00
|
|
|
len = MAX_PATH - len;
|
2012-06-04 12:46:42 +00:00
|
|
|
do
|
|
|
|
{
|
2014-05-01 11:03:14 +00:00
|
|
|
memset( p, 0, len );
|
2012-10-01 21:13:10 +00:00
|
|
|
|
2012-06-04 21:29:15 +00:00
|
|
|
if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
|
2012-06-04 12:46:42 +00:00
|
|
|
continue;
|
|
|
|
|
2014-05-01 11:03:14 +00:00
|
|
|
w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
|
2014-06-17 14:39:18 +00:00
|
|
|
lstrlenW( file_data.cFileName ),
|
2015-10-21 08:16:29 +00:00
|
|
|
p, (int) len - 1,
|
2014-05-01 11:03:14 +00:00
|
|
|
NULL, NULL );
|
2015-01-23 17:50:34 +00:00
|
|
|
if( w_ret == 0 )
|
2017-01-09 13:09:16 +00:00
|
|
|
{
|
|
|
|
ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2012-06-04 12:46:42 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
w_ret = mbedtls_x509_crt_parse_file( chain, filename );
|
2012-10-01 21:13:10 +00:00
|
|
|
if( w_ret < 0 )
|
2013-06-24 17:22:42 +00:00
|
|
|
ret++;
|
|
|
|
else
|
|
|
|
ret += w_ret;
|
2012-06-04 12:46:42 +00:00
|
|
|
}
|
2012-11-02 12:53:26 +00:00
|
|
|
while( FindNextFileW( hFind, &file_data ) != 0 );
|
2012-06-04 12:46:42 +00:00
|
|
|
|
2014-06-17 14:39:18 +00:00
|
|
|
if( GetLastError() != ERROR_NO_MORE_FILES )
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
|
2012-06-04 12:46:42 +00:00
|
|
|
|
2017-01-09 13:09:16 +00:00
|
|
|
cleanup:
|
2012-06-04 12:46:42 +00:00
|
|
|
FindClose( hFind );
|
2013-10-14 13:51:50 +00:00
|
|
|
#else /* _WIN32 */
|
2013-11-26 15:47:11 +00:00
|
|
|
int t_ret;
|
2016-09-02 13:06:04 +00:00
|
|
|
int snp_ret;
|
2013-06-24 17:22:42 +00:00
|
|
|
struct stat sb;
|
2013-11-26 15:47:11 +00:00
|
|
|
struct dirent *entry;
|
2016-09-02 13:06:04 +00:00
|
|
|
char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
|
2012-06-04 12:46:42 +00:00
|
|
|
DIR *dir = opendir( path );
|
|
|
|
|
2014-06-17 14:39:18 +00:00
|
|
|
if( dir == NULL )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
|
2012-06-04 12:46:42 +00:00
|
|
|
|
2017-01-09 17:27:59 +00:00
|
|
|
#if defined(MBEDTLS_THREADING_C)
|
2015-05-27 18:07:18 +00:00
|
|
|
if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
|
2015-06-22 16:39:57 +00:00
|
|
|
{
|
|
|
|
closedir( dir );
|
2013-11-28 16:11:54 +00:00
|
|
|
return( ret );
|
2015-06-22 16:39:57 +00:00
|
|
|
}
|
2017-01-09 17:27:59 +00:00
|
|
|
#endif /* MBEDTLS_THREADING_C */
|
2013-11-28 16:11:54 +00:00
|
|
|
|
2013-11-26 15:47:11 +00:00
|
|
|
while( ( entry = readdir( dir ) ) != NULL )
|
2012-06-04 12:46:42 +00:00
|
|
|
{
|
2016-09-02 13:06:04 +00:00
|
|
|
snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name,
|
|
|
|
"%s/%s", path, entry->d_name );
|
2013-06-24 17:22:42 +00:00
|
|
|
|
2016-09-02 13:06:04 +00:00
|
|
|
if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name )
|
|
|
|
{
|
|
|
|
ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
else if( stat( entry_name, &sb ) == -1 )
|
2013-09-09 15:26:14 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
|
2013-11-28 16:11:54 +00:00
|
|
|
goto cleanup;
|
2013-09-09 15:26:14 +00:00
|
|
|
}
|
2013-06-24 17:22:42 +00:00
|
|
|
|
2020-02-04 14:47:45 +00:00
|
|
|
if( !S_ISREG( sb.st_mode ) )
|
2012-06-04 12:46:42 +00:00
|
|
|
continue;
|
|
|
|
|
2013-06-24 17:22:42 +00:00
|
|
|
// Ignore parse errors
|
|
|
|
//
|
2015-04-08 10:49:31 +00:00
|
|
|
t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
|
2012-06-04 12:46:42 +00:00
|
|
|
if( t_ret < 0 )
|
2013-06-24 17:22:42 +00:00
|
|
|
ret++;
|
2013-09-16 11:49:26 +00:00
|
|
|
else
|
|
|
|
ret += t_ret;
|
|
|
|
}
|
2013-11-28 16:11:54 +00:00
|
|
|
|
|
|
|
cleanup:
|
2016-09-02 13:06:04 +00:00
|
|
|
closedir( dir );
|
|
|
|
|
2017-01-09 17:27:59 +00:00
|
|
|
#if defined(MBEDTLS_THREADING_C)
|
2015-05-27 18:07:18 +00:00
|
|
|
if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
|
2017-01-09 17:27:59 +00:00
|
|
|
#endif /* MBEDTLS_THREADING_C */
|
2013-11-28 16:11:54 +00:00
|
|
|
|
2013-10-14 13:51:50 +00:00
|
|
|
#endif /* _WIN32 */
|
2013-08-14 11:39:57 +00:00
|
|
|
|
|
|
|
return( ret );
|
2013-07-11 14:17:23 +00:00
|
|
|
}
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_FS_IO */
|
2013-07-11 14:17:23 +00:00
|
|
|
|
2014-04-01 11:43:28 +00:00
|
|
|
static int x509_info_subject_alt_name( char **buf, size_t *size,
|
2015-04-08 10:49:31 +00:00
|
|
|
const mbedtls_x509_sequence *subject_alt_name )
|
2014-04-01 11:43:28 +00:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
size_t n = *size;
|
|
|
|
char *p = *buf;
|
2015-04-08 10:49:31 +00:00
|
|
|
const mbedtls_x509_sequence *cur = subject_alt_name;
|
2014-04-01 16:00:07 +00:00
|
|
|
const char *sep = "";
|
|
|
|
size_t sep_len = 0;
|
2014-04-01 11:43:28 +00:00
|
|
|
|
|
|
|
while( cur != NULL )
|
|
|
|
{
|
2014-04-01 16:00:07 +00:00
|
|
|
if( cur->buf.len + sep_len >= n )
|
2014-04-01 11:43:28 +00:00
|
|
|
{
|
|
|
|
*p = '\0';
|
2015-06-22 09:12:02 +00:00
|
|
|
return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
|
2014-04-01 11:43:28 +00:00
|
|
|
}
|
|
|
|
|
2014-04-01 16:00:07 +00:00
|
|
|
n -= cur->buf.len + sep_len;
|
|
|
|
for( i = 0; i < sep_len; i++ )
|
|
|
|
*p++ = sep[i];
|
2014-04-01 11:43:28 +00:00
|
|
|
for( i = 0; i < cur->buf.len; i++ )
|
|
|
|
*p++ = cur->buf.p[i];
|
|
|
|
|
2014-04-01 16:00:07 +00:00
|
|
|
sep = ", ";
|
|
|
|
sep_len = 2;
|
|
|
|
|
2014-04-01 11:43:28 +00:00
|
|
|
cur = cur->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
*p = '\0';
|
|
|
|
|
|
|
|
*size = n;
|
|
|
|
*buf = p;
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2014-04-01 16:12:24 +00:00
|
|
|
#define PRINT_ITEM(i) \
|
|
|
|
{ \
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
|
2015-06-22 09:12:02 +00:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF; \
|
2014-04-01 16:12:24 +00:00
|
|
|
sep = ", "; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define CERT_TYPE(type,name) \
|
|
|
|
if( ns_cert_type & type ) \
|
|
|
|
PRINT_ITEM( name );
|
|
|
|
|
2014-04-01 11:01:11 +00:00
|
|
|
static int x509_info_cert_type( char **buf, size_t *size,
|
|
|
|
unsigned char ns_cert_type )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t n = *size;
|
|
|
|
char *p = *buf;
|
2014-04-01 16:00:07 +00:00
|
|
|
const char *sep = "";
|
2014-04-01 11:01:11 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
|
2015-04-20 11:19:02 +00:00
|
|
|
CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
|
2015-04-08 10:49:31 +00:00
|
|
|
CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
|
|
|
|
CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
|
|
|
|
CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
|
2015-04-20 11:19:02 +00:00
|
|
|
CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
|
|
|
|
CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
|
|
|
|
CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
|
2014-04-01 11:01:11 +00:00
|
|
|
|
|
|
|
*size = n;
|
|
|
|
*buf = p;
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2014-04-01 16:12:24 +00:00
|
|
|
#define KEY_USAGE(code,name) \
|
|
|
|
if( key_usage & code ) \
|
|
|
|
PRINT_ITEM( name );
|
|
|
|
|
2014-04-01 12:12:11 +00:00
|
|
|
static int x509_info_key_usage( char **buf, size_t *size,
|
2015-06-23 08:14:36 +00:00
|
|
|
unsigned int key_usage )
|
2014-04-01 12:12:11 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t n = *size;
|
|
|
|
char *p = *buf;
|
2014-04-01 16:00:07 +00:00
|
|
|
const char *sep = "";
|
2014-04-01 12:12:11 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
|
|
|
|
KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
|
2015-04-20 11:19:02 +00:00
|
|
|
KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
|
|
|
|
KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
|
|
|
|
KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
|
2015-04-08 10:49:31 +00:00
|
|
|
KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
|
|
|
|
KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
|
2015-06-23 08:14:36 +00:00
|
|
|
KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
|
|
|
|
KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
|
2014-04-01 12:12:11 +00:00
|
|
|
|
|
|
|
*size = n;
|
|
|
|
*buf = p;
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2014-04-01 15:32:44 +00:00
|
|
|
static int x509_info_ext_key_usage( char **buf, size_t *size,
|
2015-04-08 10:49:31 +00:00
|
|
|
const mbedtls_x509_sequence *extended_key_usage )
|
2014-04-01 15:32:44 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
const char *desc;
|
|
|
|
size_t n = *size;
|
|
|
|
char *p = *buf;
|
2015-04-08 10:49:31 +00:00
|
|
|
const mbedtls_x509_sequence *cur = extended_key_usage;
|
2014-04-01 16:00:07 +00:00
|
|
|
const char *sep = "";
|
2014-04-01 15:32:44 +00:00
|
|
|
|
|
|
|
while( cur != NULL )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
|
2014-04-01 15:32:44 +00:00
|
|
|
desc = "???";
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
|
2015-06-22 09:12:02 +00:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
2014-04-01 15:32:44 +00:00
|
|
|
|
2014-04-01 16:00:07 +00:00
|
|
|
sep = ", ";
|
|
|
|
|
2014-04-01 15:32:44 +00:00
|
|
|
cur = cur->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
*size = n;
|
|
|
|
*buf = p;
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2018-11-06 13:18:23 +00:00
|
|
|
/*
|
|
|
|
* Like memcmp, but case-insensitive and always returns -1 if different
|
|
|
|
*/
|
|
|
|
static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
unsigned char diff;
|
|
|
|
const unsigned char *n1 = s1, *n2 = s2;
|
|
|
|
|
|
|
|
for( i = 0; i < len; i++ )
|
|
|
|
{
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return 0 if name matches wildcard, -1 otherwise
|
|
|
|
*/
|
|
|
|
static int x509_check_wildcard( const char *cn, mbedtls_x509_buf *name )
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
size_t cn_idx = 0, cn_len = strlen( cn );
|
|
|
|
|
|
|
|
if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
|
|
|
|
return( 0 );
|
|
|
|
|
|
|
|
for( i = 0; i < cn_len; ++i )
|
|
|
|
{
|
|
|
|
if( cn[i] == '.' )
|
|
|
|
{
|
|
|
|
cn_idx = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( cn_idx == 0 )
|
|
|
|
return( -1 );
|
|
|
|
|
|
|
|
if( cn_len - cn_idx == name->len - 1 &&
|
|
|
|
x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
|
|
|
|
{
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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 ) &&
|
|
|
|
a->len == b->len &&
|
|
|
|
x509_memcasecmp( a->p, b->p, b->len ) == 0 )
|
|
|
|
{
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compare two X.509 Names (aka rdnSequence).
|
|
|
|
*
|
|
|
|
* See RFC 5280 section 7.1, though we don't implement the whole algorithm:
|
|
|
|
* we sometimes return unequal when the full algorithm would return equal,
|
|
|
|
* but never the other way. (In particular, we don't do Unicode normalisation
|
|
|
|
* or space folding.)
|
|
|
|
*
|
|
|
|
* Return 0 if equal, -1 otherwise.
|
|
|
|
*/
|
|
|
|
static int x509_name_cmp( const mbedtls_x509_name *a, const mbedtls_x509_name *b )
|
|
|
|
{
|
|
|
|
/* Avoid recursion, it might not be optimised by the compiler */
|
|
|
|
while( a != NULL || b != NULL )
|
|
|
|
{
|
|
|
|
if( a == NULL || b == NULL )
|
|
|
|
return( -1 );
|
|
|
|
|
|
|
|
/* type */
|
|
|
|
if( a->oid.tag != b->oid.tag ||
|
|
|
|
a->oid.len != b->oid.len ||
|
|
|
|
memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 )
|
|
|
|
{
|
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* value */
|
|
|
|
if( x509_string_cmp( &a->val, &b->val ) != 0 )
|
|
|
|
return( -1 );
|
|
|
|
|
|
|
|
/* structure of the list of sets */
|
|
|
|
if( a->next_merged != b->next_merged )
|
|
|
|
return( -1 );
|
|
|
|
|
|
|
|
a = a->next;
|
|
|
|
b = b->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* a == NULL == b */
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
/*
|
2009-05-02 15:13:40 +00:00
|
|
|
* Return an informational string about the certificate.
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
2014-04-01 10:19:09 +00:00
|
|
|
#define BEFORE_COLON 18
|
|
|
|
#define BC "18"
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
|
|
|
|
const mbedtls_x509_crt *crt )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2011-04-24 08:57:21 +00:00
|
|
|
int ret;
|
|
|
|
size_t n;
|
2009-05-02 15:13:40 +00:00
|
|
|
char *p;
|
2013-08-12 17:45:32 +00:00
|
|
|
char key_size_str[BEFORE_COLON];
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
p = buf;
|
2009-05-02 15:13:40 +00:00
|
|
|
n = size;
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2016-05-31 13:03:54 +00:00
|
|
|
if( NULL == crt )
|
|
|
|
{
|
|
|
|
ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" );
|
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
|
|
|
|
|
|
|
return( (int) ( size - n ) );
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
|
2009-01-03 21:22:43 +00:00
|
|
|
prefix, crt->version );
|
2015-06-22 09:12:02 +00:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = mbedtls_snprintf( p, n, "%sserial number : ",
|
2009-01-03 21:22:43 +00:00
|
|
|
prefix );
|
2015-06-22 09:12:02 +00:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
|
2015-06-22 09:12:02 +00:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
2009-01-03 21:22:43 +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, &crt->issuer );
|
2015-06-22 09:12:02 +00:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = mbedtls_snprintf( p, n, "\n%ssubject 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, &crt->subject );
|
2015-06-22 09:12:02 +00:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
|
2009-01-03 21:22:43 +00:00
|
|
|
"%04d-%02d-%02d %02d:%02d:%02d", prefix,
|
|
|
|
crt->valid_from.year, crt->valid_from.mon,
|
|
|
|
crt->valid_from.day, crt->valid_from.hour,
|
|
|
|
crt->valid_from.min, crt->valid_from.sec );
|
2015-06-22 09:12:02 +00:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
|
2009-01-03 21:22:43 +00:00
|
|
|
"%04d-%02d-%02d %02d:%02d:%02d", prefix,
|
|
|
|
crt->valid_to.year, crt->valid_to.mon,
|
|
|
|
crt->valid_to.day, crt->valid_to.hour,
|
|
|
|
crt->valid_to.min, crt->valid_to.sec );
|
2015-06-22 09:12:02 +00:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
2009-01-03 21:22:43 +00:00
|
|
|
|
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;
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = mbedtls_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk,
|
2014-06-05 15:07:30 +00:00
|
|
|
crt->sig_md, crt->sig_opts );
|
2015-06-22 09:12:02 +00:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2014-04-01 10:19:09 +00:00
|
|
|
/* Key size */
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
|
|
|
|
mbedtls_pk_get_name( &crt->pk ) ) ) != 0 )
|
2013-08-12 17:45:32 +00:00
|
|
|
{
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
|
2015-06-18 14:43:38 +00:00
|
|
|
(int) mbedtls_pk_get_bitlen( &crt->pk ) );
|
2015-06-22 09:12:02 +00:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2014-04-01 10:19:09 +00:00
|
|
|
/*
|
|
|
|
* Optional extensions
|
|
|
|
*/
|
|
|
|
|
2015-04-20 11:19:02 +00:00
|
|
|
if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
|
2014-04-01 10:19:09 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
|
2014-04-01 10:19:09 +00:00
|
|
|
crt->ca_istrue ? "true" : "false" );
|
2015-06-22 09:12:02 +00:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
2014-04-01 10:19:09 +00:00
|
|
|
|
|
|
|
if( crt->max_pathlen > 0 )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
|
2015-06-22 09:12:02 +00:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
2014-04-01 10:19:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-20 11:19:02 +00:00
|
|
|
if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
|
2014-04-01 10:19:09 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = mbedtls_snprintf( p, n, "\n%ssubject alt name : ", prefix );
|
2015-06-22 09:12:02 +00:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
2014-04-01 11:43:28 +00:00
|
|
|
|
|
|
|
if( ( ret = x509_info_subject_alt_name( &p, &n,
|
|
|
|
&crt->subject_alt_names ) ) != 0 )
|
|
|
|
return( ret );
|
2014-04-01 10:19:09 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
|
2014-04-01 10:19:09 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
|
2015-06-22 09:12:02 +00:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
2014-04-01 11:01:11 +00:00
|
|
|
|
|
|
|
if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
|
|
|
|
return( ret );
|
2014-04-01 10:19:09 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
|
2014-04-01 10:19:09 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
|
2015-06-22 09:12:02 +00:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
2014-04-01 12:12:11 +00:00
|
|
|
|
|
|
|
if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
|
|
|
|
return( ret );
|
2014-04-01 10:19:09 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
|
2014-04-01 10:19:09 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
|
2015-06-22 09:12:02 +00:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
2014-04-01 15:32:44 +00:00
|
|
|
|
|
|
|
if( ( ret = x509_info_ext_key_usage( &p, &n,
|
|
|
|
&crt->ext_key_usage ) ) != 0 )
|
|
|
|
return( ret );
|
2014-04-01 10:19:09 +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;
|
2014-04-01 10:19:09 +00:00
|
|
|
|
2011-04-24 08:57:21 +00:00
|
|
|
return( (int) ( size - n ) );
|
2009-05-02 15:13:40 +00:00
|
|
|
}
|
|
|
|
|
2015-04-20 09:38:13 +00:00
|
|
|
struct x509_crt_verify_string {
|
|
|
|
int code;
|
|
|
|
const char *string;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
|
2015-04-20 11:19:02 +00:00
|
|
|
{ MBEDTLS_X509_BADCERT_EXPIRED, "The certificate validity has expired" },
|
2015-04-20 09:38:13 +00:00
|
|
|
{ MBEDTLS_X509_BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" },
|
|
|
|
{ MBEDTLS_X509_BADCERT_CN_MISMATCH, "The certificate Common Name (CN) does not match with the expected CN" },
|
|
|
|
{ MBEDTLS_X509_BADCERT_NOT_TRUSTED, "The certificate is not correctly signed by the trusted CA" },
|
|
|
|
{ MBEDTLS_X509_BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" },
|
|
|
|
{ MBEDTLS_X509_BADCRL_EXPIRED, "The CRL is expired" },
|
2015-04-20 11:19:02 +00:00
|
|
|
{ MBEDTLS_X509_BADCERT_MISSING, "Certificate was missing" },
|
|
|
|
{ MBEDTLS_X509_BADCERT_SKIP_VERIFY, "Certificate verification was skipped" },
|
|
|
|
{ MBEDTLS_X509_BADCERT_OTHER, "Other reason (can be used by verify callback)" },
|
2015-04-20 09:38:13 +00:00
|
|
|
{ MBEDTLS_X509_BADCERT_FUTURE, "The certificate validity starts in the future" },
|
2015-04-20 11:19:02 +00:00
|
|
|
{ MBEDTLS_X509_BADCRL_FUTURE, "The CRL is from the future" },
|
|
|
|
{ MBEDTLS_X509_BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" },
|
|
|
|
{ MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" },
|
|
|
|
{ MBEDTLS_X509_BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" },
|
2015-06-15 08:39:46 +00:00
|
|
|
{ MBEDTLS_X509_BADCERT_BAD_MD, "The certificate is signed with an unacceptable hash." },
|
|
|
|
{ MBEDTLS_X509_BADCERT_BAD_PK, "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
|
|
|
|
{ MBEDTLS_X509_BADCERT_BAD_KEY, "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." },
|
|
|
|
{ MBEDTLS_X509_BADCRL_BAD_MD, "The CRL is signed with an unacceptable hash." },
|
|
|
|
{ MBEDTLS_X509_BADCRL_BAD_PK, "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
|
|
|
|
{ MBEDTLS_X509_BADCRL_BAD_KEY, "The CRL is signed with an unacceptable key (eg bad curve, RSA too short)." },
|
2015-04-20 09:38:13 +00:00
|
|
|
{ 0, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
|
2015-05-11 17:54:43 +00:00
|
|
|
uint32_t flags )
|
2015-04-20 09:38:13 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
const struct x509_crt_verify_string *cur;
|
|
|
|
char *p = buf;
|
|
|
|
size_t n = size;
|
|
|
|
|
|
|
|
for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
|
|
|
|
{
|
|
|
|
if( ( flags & cur->code ) == 0 )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
|
2015-06-22 09:12:02 +00:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
2015-04-20 09:38:13 +00:00
|
|
|
flags ^= cur->code;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( flags != 0 )
|
|
|
|
{
|
|
|
|
ret = mbedtls_snprintf( p, n, "%sUnknown reason "
|
|
|
|
"(this should not happen)\n", prefix );
|
2015-06-22 09:12:02 +00:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
2015-04-20 09:38:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return( (int) ( size - n ) );
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
|
2015-06-23 08:48:44 +00:00
|
|
|
int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
|
|
|
|
unsigned int usage )
|
2014-04-09 07:50:03 +00:00
|
|
|
{
|
2015-06-23 08:48:44 +00:00
|
|
|
unsigned int usage_must, usage_may;
|
|
|
|
unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
|
|
|
|
| MBEDTLS_X509_KU_DECIPHER_ONLY;
|
|
|
|
|
|
|
|
if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
|
|
|
|
return( 0 );
|
|
|
|
|
|
|
|
usage_must = usage & ~may_mask;
|
|
|
|
|
|
|
|
if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
|
|
|
|
return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
usage_may = usage & may_mask;
|
|
|
|
|
|
|
|
if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
|
2014-04-09 07:50:03 +00:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
|
|
|
|
int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
|
2014-04-10 15:53:56 +00:00
|
|
|
const char *usage_oid,
|
|
|
|
size_t usage_len )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
const mbedtls_x509_sequence *cur;
|
2014-04-10 15:53:56 +00:00
|
|
|
|
|
|
|
/* Extension is not mandatory, absent means no restriction */
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
|
2014-04-10 15:53:56 +00:00
|
|
|
return( 0 );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Look for the requested usage (or wildcard ANY) in our list
|
|
|
|
*/
|
|
|
|
for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
const mbedtls_x509_buf *cur_oid = &cur->buf;
|
2014-04-10 15:53:56 +00:00
|
|
|
|
|
|
|
if( cur_oid->len == usage_len &&
|
|
|
|
memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
|
|
|
|
{
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 )
|
2014-04-10 15:53:56 +00:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
|
2014-04-10 15:53:56 +00:00
|
|
|
}
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
|
2014-04-10 15:53:56 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_X509_CRL_PARSE_C)
|
2009-05-03 10:18:48 +00:00
|
|
|
/*
|
|
|
|
* Return 1 if the certificate is revoked, or 0 otherwise.
|
|
|
|
*/
|
2015-06-02 09:38:50 +00:00
|
|
|
int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl )
|
2009-05-03 10:18:48 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
const mbedtls_x509_crl_entry *cur = &crl->entry;
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2009-05-03 10:18:48 +00:00
|
|
|
while( cur != NULL && cur->serial.len != 0 )
|
|
|
|
{
|
2011-01-16 21:38:35 +00:00
|
|
|
if( crt->serial.len == cur->serial.len &&
|
|
|
|
memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
|
2009-05-03 10:18:48 +00:00
|
|
|
{
|
2015-06-02 09:38:50 +00:00
|
|
|
if( mbedtls_x509_time_is_past( &cur->revocation_date ) )
|
2009-05-03 10:18:48 +00:00
|
|
|
return( 1 );
|
|
|
|
}
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2009-05-03 10:18:48 +00:00
|
|
|
cur = cur->next;
|
|
|
|
}
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2011-01-16 21:12:10 +00:00
|
|
|
/*
|
2016-02-22 10:36:55 +00:00
|
|
|
* Check that the given certificate is not revoked according to the CRL.
|
2018-03-05 12:22:59 +00:00
|
|
|
* Skip validation if no CRL for the given CA is present.
|
2011-01-16 21:12:10 +00:00
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
|
2015-06-15 08:39:46 +00:00
|
|
|
mbedtls_x509_crl *crl_list,
|
|
|
|
const mbedtls_x509_crt_profile *profile )
|
2011-01-16 21:12:10 +00:00
|
|
|
{
|
|
|
|
int flags = 0;
|
2015-04-08 10:49:31 +00:00
|
|
|
unsigned char hash[MBEDTLS_MD_MAX_SIZE];
|
|
|
|
const mbedtls_md_info_t *md_info;
|
2011-01-16 21:12:10 +00:00
|
|
|
|
2012-09-28 07:10:55 +00:00
|
|
|
if( ca == NULL )
|
|
|
|
return( flags );
|
|
|
|
|
|
|
|
while( crl_list != NULL )
|
2011-01-16 21:12:10 +00:00
|
|
|
{
|
2012-09-28 07:10:55 +00:00
|
|
|
if( crl_list->version == 0 ||
|
2018-11-02 09:19:54 +00:00
|
|
|
x509_name_cmp( &crl_list->issuer, &ca->subject ) != 0 )
|
2011-01-16 21:12:10 +00:00
|
|
|
{
|
|
|
|
crl_list = crl_list->next;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-04-08 13:10:07 +00:00
|
|
|
/*
|
|
|
|
* Check if the CA is configured to sign CRLs
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
|
2018-11-02 09:19:54 +00:00
|
|
|
if( mbedtls_x509_crt_check_key_usage( ca,
|
|
|
|
MBEDTLS_X509_KU_CRL_SIGN ) != 0 )
|
2014-04-08 13:10:07 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
|
2014-04-08 13:10:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-01-16 21:12:10 +00:00
|
|
|
/*
|
|
|
|
* Check if CRL is correctly signed by the trusted CA
|
|
|
|
*/
|
2015-06-15 14:17:55 +00:00
|
|
|
if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
|
|
|
|
flags |= MBEDTLS_X509_BADCRL_BAD_MD;
|
|
|
|
|
|
|
|
if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
|
|
|
|
flags |= MBEDTLS_X509_BADCRL_BAD_PK;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
md_info = mbedtls_md_info_from_type( crl_list->sig_md );
|
2017-06-26 10:22:17 +00:00
|
|
|
if( mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash ) != 0 )
|
2013-04-07 20:00:46 +00:00
|
|
|
{
|
2017-06-26 10:22:17 +00:00
|
|
|
/* Note: this can't happen except after an internal error */
|
2015-04-08 10:49:31 +00:00
|
|
|
flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
|
2013-04-07 20:00:46 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-01-16 21:12:10 +00:00
|
|
|
|
2015-06-15 14:17:55 +00:00
|
|
|
if( x509_profile_check_key( profile, crl_list->sig_pk, &ca->pk ) != 0 )
|
|
|
|
flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
|
2015-06-15 08:39:46 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
|
|
|
|
crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
|
2014-06-05 15:53:52 +00:00
|
|
|
crl_list->sig.p, crl_list->sig.len ) != 0 )
|
2011-01-16 21:12:10 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
|
2013-08-12 16:41:18 +00:00
|
|
|
break;
|
2011-01-16 21:12:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for validity of CRL (Do not drop out)
|
|
|
|
*/
|
2015-06-02 09:38:50 +00:00
|
|
|
if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
|
2015-04-08 10:49:31 +00:00
|
|
|
flags |= MBEDTLS_X509_BADCRL_EXPIRED;
|
2011-01-16 21:12:10 +00:00
|
|
|
|
2015-06-02 09:38:50 +00:00
|
|
|
if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
|
2015-04-20 11:19:02 +00:00
|
|
|
flags |= MBEDTLS_X509_BADCRL_FUTURE;
|
2014-03-10 12:15:18 +00:00
|
|
|
|
2011-01-16 21:12:10 +00:00
|
|
|
/*
|
|
|
|
* Check if certificate is revoked
|
|
|
|
*/
|
2015-06-02 09:38:50 +00:00
|
|
|
if( mbedtls_x509_crt_is_revoked( crt, crl_list ) )
|
2011-01-16 21:12:10 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
flags |= MBEDTLS_X509_BADCERT_REVOKED;
|
2011-01-16 21:12:10 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
crl_list = crl_list->next;
|
|
|
|
}
|
2015-06-15 14:17:55 +00:00
|
|
|
|
2014-06-17 12:06:49 +00:00
|
|
|
return( flags );
|
2011-01-16 21:12:10 +00:00
|
|
|
}
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_X509_CRL_PARSE_C */
|
2011-01-16 21:12:10 +00:00
|
|
|
|
2014-04-08 11:18:01 +00:00
|
|
|
/*
|
2014-04-09 12:30:11 +00:00
|
|
|
* Check if 'parent' is a suitable parent (signing CA) for 'child'.
|
|
|
|
* Return 0 if yes, -1 if not.
|
2014-06-24 09:49:16 +00:00
|
|
|
*
|
|
|
|
* top means parent is a locally-trusted certificate
|
|
|
|
* bottom means child is the end entity cert
|
2014-04-08 11:18:01 +00:00
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
static int x509_crt_check_parent( const mbedtls_x509_crt *child,
|
|
|
|
const mbedtls_x509_crt *parent,
|
2014-06-24 09:49:16 +00:00
|
|
|
int top, int bottom )
|
2014-04-08 11:18:01 +00:00
|
|
|
{
|
2014-06-24 09:49:16 +00:00
|
|
|
int need_ca_bit;
|
|
|
|
|
2014-06-19 10:18:08 +00:00
|
|
|
/* Parent must be the issuer */
|
2014-10-17 10:25:12 +00:00
|
|
|
if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
|
2014-04-09 12:30:11 +00:00
|
|
|
return( -1 );
|
2014-04-08 11:18:01 +00:00
|
|
|
|
2014-06-24 09:49:16 +00:00
|
|
|
/* Parent must have the basicConstraints CA bit set as a general rule */
|
|
|
|
need_ca_bit = 1;
|
|
|
|
|
|
|
|
/* Exception: v1/v2 certificates that are locally trusted. */
|
|
|
|
if( top && parent->version < 3 )
|
|
|
|
need_ca_bit = 0;
|
|
|
|
|
|
|
|
/* Exception: self-signed end-entity certs that are locally trusted. */
|
|
|
|
if( top && bottom &&
|
|
|
|
child->raw.len == parent->raw.len &&
|
|
|
|
memcmp( child->raw.p, parent->raw.p, child->raw.len ) == 0 )
|
2014-06-19 10:18:08 +00:00
|
|
|
{
|
2014-06-24 09:49:16 +00:00
|
|
|
need_ca_bit = 0;
|
2014-06-19 10:18:08 +00:00
|
|
|
}
|
|
|
|
|
2014-06-24 09:49:16 +00:00
|
|
|
if( need_ca_bit && ! parent->ca_istrue )
|
|
|
|
return( -1 );
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
|
2014-06-24 09:49:16 +00:00
|
|
|
if( need_ca_bit &&
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
|
2014-06-24 09:49:16 +00:00
|
|
|
{
|
2014-04-09 12:30:11 +00:00
|
|
|
return( -1 );
|
2014-06-24 09:49:16 +00:00
|
|
|
}
|
2014-04-08 13:10:07 +00:00
|
|
|
#endif
|
|
|
|
|
2014-04-09 12:30:11 +00:00
|
|
|
return( 0 );
|
2014-04-08 11:18:01 +00:00
|
|
|
}
|
|
|
|
|
2017-06-29 08:45:25 +00:00
|
|
|
/*
|
2018-03-06 09:34:11 +00:00
|
|
|
* Verify a certificate with no parent inside the chain
|
2017-06-29 08:45:25 +00:00
|
|
|
* (either the parent is a trusted root, or there is no parent)
|
|
|
|
*
|
|
|
|
* See comments for mbedtls_x509_crt_verify_with_profile()
|
2018-03-06 09:34:11 +00:00
|
|
|
* (also for notation used below)
|
2017-06-29 08:45:25 +00:00
|
|
|
*
|
|
|
|
* This function is called in two cases:
|
|
|
|
* - child was found to have a parent in trusted roots, in which case we're
|
|
|
|
* called with trust_ca pointing directly to that parent (not the full list)
|
2018-03-07 08:36:30 +00:00
|
|
|
* - this is cases 1, 2 and 3 of the comment on verify_with_profile()
|
2017-06-29 08:45:25 +00:00
|
|
|
* - case 1 is special as child and trust_ca point to copies of the same
|
2018-03-06 09:34:11 +00:00
|
|
|
* certificate then
|
2017-06-29 08:45:25 +00:00
|
|
|
* - child was found to have no parent either in the chain or in trusted CAs
|
2018-03-07 08:36:30 +00:00
|
|
|
* - this is cases 4 and 5 of the comment on verify_with_profile()
|
2017-06-29 08:45:25 +00:00
|
|
|
*
|
|
|
|
* For historical reasons, the function currently does not assume that
|
|
|
|
* trust_ca points directly to the right root in the first case, and it
|
|
|
|
* doesn't know in which case it starts, so it always starts by searching for
|
|
|
|
* a parent in trust_ca.
|
|
|
|
*/
|
2013-09-18 11:46:23 +00:00
|
|
|
static int x509_crt_verify_top(
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crt *child, mbedtls_x509_crt *trust_ca,
|
2015-06-15 08:39:46 +00:00
|
|
|
mbedtls_x509_crl *ca_crl,
|
|
|
|
const mbedtls_x509_crt_profile *profile,
|
2015-10-12 07:02:20 +00:00
|
|
|
int path_cnt, int self_cnt, uint32_t *flags,
|
2015-05-11 17:54:43 +00:00
|
|
|
int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
|
2012-09-28 07:10:55 +00:00
|
|
|
void *p_vrfy )
|
|
|
|
{
|
2013-04-07 20:00:46 +00:00
|
|
|
int ret;
|
2015-05-11 17:54:43 +00:00
|
|
|
uint32_t ca_flags = 0;
|
2015-06-02 08:59:29 +00:00
|
|
|
int check_path_cnt;
|
2015-04-08 10:49:31 +00:00
|
|
|
unsigned char hash[MBEDTLS_MD_MAX_SIZE];
|
|
|
|
const mbedtls_md_info_t *md_info;
|
2016-12-09 17:26:23 +00:00
|
|
|
mbedtls_x509_crt *future_past_ca = NULL;
|
2012-09-28 07:10:55 +00:00
|
|
|
|
2015-06-02 09:38:50 +00:00
|
|
|
if( mbedtls_x509_time_is_past( &child->valid_to ) )
|
2015-04-20 11:19:02 +00:00
|
|
|
*flags |= MBEDTLS_X509_BADCERT_EXPIRED;
|
2012-09-28 07:10:55 +00:00
|
|
|
|
2015-06-02 09:38:50 +00:00
|
|
|
if( mbedtls_x509_time_is_future( &child->valid_from ) )
|
2015-04-08 10:49:31 +00:00
|
|
|
*flags |= MBEDTLS_X509_BADCERT_FUTURE;
|
2014-03-10 12:15:18 +00:00
|
|
|
|
2015-06-15 14:17:55 +00:00
|
|
|
if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
|
|
|
|
*flags |= MBEDTLS_X509_BADCERT_BAD_MD;
|
|
|
|
|
|
|
|
if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
|
|
|
|
*flags |= MBEDTLS_X509_BADCERT_BAD_PK;
|
|
|
|
|
2012-09-28 07:10:55 +00:00
|
|
|
/*
|
|
|
|
* Child is the top of the chain. Check against the trust_ca list.
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
*flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
|
2012-09-28 07:10:55 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
md_info = mbedtls_md_info_from_type( child->sig_md );
|
2017-06-26 10:22:17 +00:00
|
|
|
if( mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash ) != 0 )
|
2013-08-23 14:47:30 +00:00
|
|
|
{
|
2017-06-26 10:22:17 +00:00
|
|
|
/* Note: this can't happen except after an internal error */
|
|
|
|
/* Cannot check signature, no need to try any CA */
|
2013-08-23 14:47:30 +00:00
|
|
|
trust_ca = NULL;
|
|
|
|
}
|
|
|
|
|
2014-04-09 12:30:45 +00:00
|
|
|
for( /* trust_ca */ ; trust_ca != NULL; trust_ca = trust_ca->next )
|
2012-09-28 07:10:55 +00:00
|
|
|
{
|
2014-06-24 09:49:16 +00:00
|
|
|
if( x509_crt_check_parent( child, trust_ca, 1, path_cnt == 0 ) != 0 )
|
2012-09-28 07:10:55 +00:00
|
|
|
continue;
|
|
|
|
|
2015-05-13 09:40:30 +00:00
|
|
|
check_path_cnt = path_cnt + 1;
|
|
|
|
|
2012-11-14 12:39:52 +00:00
|
|
|
/*
|
2015-05-13 09:40:30 +00:00
|
|
|
* Reduce check_path_cnt to check against if top of the chain is
|
2012-11-14 12:39:52 +00:00
|
|
|
* the same as the trusted CA
|
|
|
|
*/
|
|
|
|
if( child->subject_raw.len == trust_ca->subject_raw.len &&
|
|
|
|
memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
|
2017-09-25 09:47:58 +00:00
|
|
|
child->subject_raw.len ) == 0 )
|
2012-11-14 12:39:52 +00:00
|
|
|
{
|
|
|
|
check_path_cnt--;
|
|
|
|
}
|
|
|
|
|
2015-10-12 07:02:20 +00:00
|
|
|
/* Self signed certificates do not count towards the limit */
|
2012-09-28 07:10:55 +00:00
|
|
|
if( trust_ca->max_pathlen > 0 &&
|
2015-10-12 07:02:20 +00:00
|
|
|
trust_ca->max_pathlen < check_path_cnt - self_cnt )
|
2012-09-28 07:10:55 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-12-09 17:26:23 +00:00
|
|
|
if( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &trust_ca->pk,
|
|
|
|
child->sig_md, hash, mbedtls_md_get_size( md_info ),
|
|
|
|
child->sig.p, child->sig.len ) != 0 )
|
2016-02-19 15:58:21 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-12-09 17:26:23 +00:00
|
|
|
if( mbedtls_x509_time_is_past( &trust_ca->valid_to ) ||
|
|
|
|
mbedtls_x509_time_is_future( &trust_ca->valid_from ) )
|
2016-02-19 15:58:21 +00:00
|
|
|
{
|
2016-12-09 17:26:23 +00:00
|
|
|
if ( future_past_ca == NULL )
|
|
|
|
future_past_ca = trust_ca;
|
2016-02-19 15:58:21 +00:00
|
|
|
|
2013-08-12 16:41:18 +00:00
|
|
|
continue;
|
2013-08-09 10:30:45 +00:00
|
|
|
}
|
2012-09-28 07:10:55 +00:00
|
|
|
|
2016-12-09 17:26:23 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( trust_ca != NULL || ( trust_ca = future_past_ca ) != NULL )
|
|
|
|
{
|
2012-09-28 07:10:55 +00:00
|
|
|
/*
|
|
|
|
* Top of chain is signed by a trusted CA
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
*flags &= ~MBEDTLS_X509_BADCERT_NOT_TRUSTED;
|
2015-06-27 12:41:38 +00:00
|
|
|
|
|
|
|
if( x509_profile_check_key( profile, child->sig_pk, &trust_ca->pk ) != 0 )
|
|
|
|
*flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
|
2012-09-28 07:10:55 +00:00
|
|
|
}
|
|
|
|
|
2012-11-14 12:39:52 +00:00
|
|
|
/*
|
2012-11-24 10:53:17 +00:00
|
|
|
* If top of chain is not the same as the trusted CA send a verify request
|
|
|
|
* to the callback for any issues with validity and CRL presence for the
|
|
|
|
* trusted CA certificate.
|
2012-11-14 12:39:52 +00:00
|
|
|
*/
|
|
|
|
if( trust_ca != NULL &&
|
|
|
|
( child->subject_raw.len != trust_ca->subject_raw.len ||
|
|
|
|
memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
|
2017-09-25 09:47:58 +00:00
|
|
|
child->subject_raw.len ) != 0 ) )
|
2012-09-28 07:10:55 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_X509_CRL_PARSE_C)
|
2013-08-23 14:47:30 +00:00
|
|
|
/* Check trusted CA's CRL for the chain's top crt */
|
2015-06-15 08:39:46 +00:00
|
|
|
*flags |= x509_crt_verifycrl( child, trust_ca, ca_crl, profile );
|
2013-09-23 10:20:02 +00:00
|
|
|
#else
|
|
|
|
((void) ca_crl);
|
2013-09-16 11:49:26 +00:00
|
|
|
#endif
|
2012-09-28 07:10:55 +00:00
|
|
|
|
2016-12-09 17:26:23 +00:00
|
|
|
if( mbedtls_x509_time_is_past( &trust_ca->valid_to ) )
|
|
|
|
ca_flags |= MBEDTLS_X509_BADCERT_EXPIRED;
|
|
|
|
|
|
|
|
if( mbedtls_x509_time_is_future( &trust_ca->valid_from ) )
|
|
|
|
ca_flags |= MBEDTLS_X509_BADCERT_FUTURE;
|
|
|
|
|
2012-09-28 07:10:55 +00:00
|
|
|
if( NULL != f_vrfy )
|
|
|
|
{
|
2014-05-01 12:18:25 +00:00
|
|
|
if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1,
|
|
|
|
&ca_flags ) ) != 0 )
|
|
|
|
{
|
2012-09-28 07:10:55 +00:00
|
|
|
return( ret );
|
2014-05-01 12:18:25 +00:00
|
|
|
}
|
2012-09-28 07:10:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Call callback on top cert */
|
|
|
|
if( NULL != f_vrfy )
|
|
|
|
{
|
2014-06-17 14:39:18 +00:00
|
|
|
if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
|
2012-09-28 07:10:55 +00:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
*flags |= ca_flags;
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2017-06-29 08:45:25 +00:00
|
|
|
/*
|
|
|
|
* Verify a certificate with a parent inside the chain
|
|
|
|
*
|
|
|
|
* See comments for mbedtls_x509_crt_verify_with_profile()
|
|
|
|
*/
|
2013-09-18 11:46:23 +00:00
|
|
|
static int x509_crt_verify_child(
|
2015-06-15 08:39:46 +00:00
|
|
|
mbedtls_x509_crt *child, mbedtls_x509_crt *parent,
|
|
|
|
mbedtls_x509_crt *trust_ca, mbedtls_x509_crl *ca_crl,
|
|
|
|
const mbedtls_x509_crt_profile *profile,
|
2015-10-12 07:02:20 +00:00
|
|
|
int path_cnt, int self_cnt, uint32_t *flags,
|
2015-05-11 17:54:43 +00:00
|
|
|
int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
|
2012-09-28 07:10:55 +00:00
|
|
|
void *p_vrfy )
|
|
|
|
{
|
2013-04-07 20:00:46 +00:00
|
|
|
int ret;
|
2015-05-11 17:54:43 +00:00
|
|
|
uint32_t parent_flags = 0;
|
2015-04-08 10:49:31 +00:00
|
|
|
unsigned char hash[MBEDTLS_MD_MAX_SIZE];
|
|
|
|
mbedtls_x509_crt *grandparent;
|
|
|
|
const mbedtls_md_info_t *md_info;
|
2012-09-28 07:10:55 +00:00
|
|
|
|
2015-10-12 07:02:20 +00:00
|
|
|
/* Counting intermediate self signed certificates */
|
2015-10-30 08:23:19 +00:00
|
|
|
if( ( path_cnt != 0 ) && x509_name_cmp( &child->issuer, &child->subject ) == 0 )
|
2015-10-12 07:02:20 +00:00
|
|
|
self_cnt++;
|
2012-09-28 07:10:55 +00:00
|
|
|
|
2014-11-20 15:34:20 +00:00
|
|
|
/* path_cnt is 0 for the first intermediate CA */
|
2015-04-08 10:49:31 +00:00
|
|
|
if( 1 + path_cnt > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
|
2014-11-20 15:34:20 +00:00
|
|
|
{
|
2017-06-26 08:11:49 +00:00
|
|
|
/* return immediately as the goal is to avoid unbounded recursion */
|
|
|
|
return( MBEDTLS_ERR_X509_FATAL_ERROR );
|
2014-11-20 15:34:20 +00:00
|
|
|
}
|
|
|
|
|
2015-06-02 09:38:50 +00:00
|
|
|
if( mbedtls_x509_time_is_past( &child->valid_to ) )
|
2015-04-20 11:19:02 +00:00
|
|
|
*flags |= MBEDTLS_X509_BADCERT_EXPIRED;
|
2014-04-08 09:55:03 +00:00
|
|
|
|
2015-06-02 09:38:50 +00:00
|
|
|
if( mbedtls_x509_time_is_future( &child->valid_from ) )
|
2015-04-08 10:49:31 +00:00
|
|
|
*flags |= MBEDTLS_X509_BADCERT_FUTURE;
|
2012-09-28 07:10:55 +00:00
|
|
|
|
2015-06-15 14:17:55 +00:00
|
|
|
if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
|
|
|
|
*flags |= MBEDTLS_X509_BADCERT_BAD_MD;
|
|
|
|
|
|
|
|
if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
|
|
|
|
*flags |= MBEDTLS_X509_BADCERT_BAD_PK;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
md_info = mbedtls_md_info_from_type( child->sig_md );
|
2018-03-07 08:41:20 +00:00
|
|
|
if( mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash ) != 0 )
|
2013-04-07 20:00:46 +00:00
|
|
|
{
|
2018-03-07 08:41:20 +00:00
|
|
|
/* Note: this can't happen except after an internal error */
|
2015-04-08 10:49:31 +00:00
|
|
|
*flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
|
2013-04-07 20:00:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-06-15 14:17:55 +00:00
|
|
|
if( x509_profile_check_key( profile, child->sig_pk, &parent->pk ) != 0 )
|
|
|
|
*flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
|
|
|
|
child->sig_md, hash, mbedtls_md_get_size( md_info ),
|
2014-06-05 14:34:18 +00:00
|
|
|
child->sig.p, child->sig.len ) != 0 )
|
2013-07-11 08:46:21 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
*flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
|
2013-07-11 08:46:21 +00:00
|
|
|
}
|
2013-04-07 20:00:46 +00:00
|
|
|
}
|
2012-09-28 07:10:55 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_X509_CRL_PARSE_C)
|
2012-09-28 07:10:55 +00:00
|
|
|
/* Check trusted CA's CRL for the given crt */
|
2015-06-15 08:39:46 +00:00
|
|
|
*flags |= x509_crt_verifycrl(child, parent, ca_crl, profile );
|
2013-09-16 11:49:26 +00:00
|
|
|
#endif
|
2012-09-28 07:10:55 +00:00
|
|
|
|
2015-09-01 14:35:00 +00:00
|
|
|
/* Look for a grandparent in trusted CAs */
|
|
|
|
for( grandparent = trust_ca;
|
2014-04-09 12:30:11 +00:00
|
|
|
grandparent != NULL;
|
|
|
|
grandparent = grandparent->next )
|
|
|
|
{
|
2014-06-24 09:49:16 +00:00
|
|
|
if( x509_crt_check_parent( parent, grandparent,
|
|
|
|
0, path_cnt == 0 ) == 0 )
|
2014-04-09 12:30:11 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( grandparent != NULL )
|
2012-09-28 07:10:55 +00:00
|
|
|
{
|
2015-09-01 14:35:00 +00:00
|
|
|
ret = x509_crt_verify_top( parent, grandparent, ca_crl, profile,
|
2015-10-12 07:02:20 +00:00
|
|
|
path_cnt + 1, self_cnt, &parent_flags, f_vrfy, p_vrfy );
|
2012-09-28 07:10:55 +00:00
|
|
|
if( ret != 0 )
|
|
|
|
return( ret );
|
2013-09-09 15:21:45 +00:00
|
|
|
}
|
2012-09-28 07:10:55 +00:00
|
|
|
else
|
|
|
|
{
|
2015-09-01 14:35:00 +00:00
|
|
|
/* Look for a grandparent upwards the chain */
|
|
|
|
for( grandparent = parent->next;
|
|
|
|
grandparent != NULL;
|
|
|
|
grandparent = grandparent->next )
|
|
|
|
{
|
2015-10-12 07:02:20 +00:00
|
|
|
/* +2 because the current step is not yet accounted for
|
|
|
|
* and because max_pathlen is one higher than it should be.
|
2015-10-30 08:23:19 +00:00
|
|
|
* Also self signed certificates do not count to the limit. */
|
2015-10-12 07:02:20 +00:00
|
|
|
if( grandparent->max_pathlen > 0 &&
|
|
|
|
grandparent->max_pathlen < 2 + path_cnt - self_cnt )
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-09-01 14:35:00 +00:00
|
|
|
if( x509_crt_check_parent( parent, grandparent,
|
|
|
|
0, path_cnt == 0 ) == 0 )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Is our parent part of the chain or at the top? */
|
|
|
|
if( grandparent != NULL )
|
|
|
|
{
|
|
|
|
ret = x509_crt_verify_child( parent, grandparent, trust_ca, ca_crl,
|
2015-10-12 07:02:20 +00:00
|
|
|
profile, path_cnt + 1, self_cnt, &parent_flags,
|
2015-09-01 14:35:00 +00:00
|
|
|
f_vrfy, p_vrfy );
|
|
|
|
if( ret != 0 )
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = x509_crt_verify_top( parent, trust_ca, ca_crl, profile,
|
2015-10-12 07:02:20 +00:00
|
|
|
path_cnt + 1, self_cnt, &parent_flags,
|
2015-09-01 14:35:00 +00:00
|
|
|
f_vrfy, p_vrfy );
|
|
|
|
if( ret != 0 )
|
|
|
|
return( ret );
|
|
|
|
}
|
2012-09-28 07:10:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* child is verified to be a child of the parent, call verify callback */
|
|
|
|
if( NULL != f_vrfy )
|
2012-11-14 12:39:52 +00:00
|
|
|
if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
|
2012-09-28 07:10:55 +00:00
|
|
|
return( ret );
|
|
|
|
|
|
|
|
*flags |= parent_flags;
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
/*
|
|
|
|
* Verify the certificate validity
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
|
|
|
|
mbedtls_x509_crt *trust_ca,
|
|
|
|
mbedtls_x509_crl *ca_crl,
|
2015-05-11 17:54:43 +00:00
|
|
|
const char *cn, uint32_t *flags,
|
|
|
|
int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
|
2013-09-18 11:46:23 +00:00
|
|
|
void *p_vrfy )
|
2015-06-15 08:39:46 +00:00
|
|
|
{
|
|
|
|
return( mbedtls_x509_crt_verify_with_profile( crt, trust_ca, ca_crl,
|
2015-06-15 12:34:59 +00:00
|
|
|
&mbedtls_x509_crt_profile_default, cn, flags, f_vrfy, p_vrfy ) );
|
2015-06-15 08:39:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Verify the certificate validity, with profile
|
2017-06-29 08:45:25 +00:00
|
|
|
*
|
|
|
|
* The chain building/verification is spread accross 4 functions:
|
|
|
|
* - this one
|
|
|
|
* - x509_crt_verify_child()
|
|
|
|
* - x509_crt_verify_top()
|
|
|
|
* - x509_crt_check_parent()
|
|
|
|
*
|
|
|
|
* There are five main cases to consider. Let's introduce some notation:
|
|
|
|
* - E means the end-entity certificate
|
2018-03-06 09:34:11 +00:00
|
|
|
* - I an intermediate CA
|
2017-06-29 08:45:25 +00:00
|
|
|
* - R the trusted root CA this chain anchors to
|
|
|
|
* - T the list of trusted roots (R and possible some others)
|
|
|
|
*
|
|
|
|
* The main cases with the calling sequence of the crt_verify_xxx() are:
|
|
|
|
* 1. E = R (explicitly trusted EE cert)
|
|
|
|
* verify(E, T) -> verify_top(E, R)
|
|
|
|
* 2. E -> R (EE signed by trusted root)
|
|
|
|
* verify(E, T) -> verify_top(E, R)
|
|
|
|
* 3. E -> I -> R (EE signed by intermediate signed by trusted root)
|
|
|
|
* verify(E, T) -> verify_child(E, I, T) -> verify_top(I, R)
|
2018-03-06 09:34:11 +00:00
|
|
|
* (plus variant with multiple intermediates)
|
2017-06-29 08:45:25 +00:00
|
|
|
* 4. E -> I (EE signed by intermediate that's not trusted)
|
|
|
|
* verify(E, T) -> verify_child(E, I, T) -> verify_top(I, T)
|
2018-03-06 09:34:11 +00:00
|
|
|
* (plus variant with multiple intermediates)
|
2017-06-29 08:45:25 +00:00
|
|
|
* 5. E (EE not trusted)
|
|
|
|
* verify(E, T) -> verify_top(E, T)
|
2018-03-07 08:36:30 +00:00
|
|
|
*
|
|
|
|
* Note: this notation and case numbering is also used in x509_crt_verify_top()
|
2015-06-15 08:39:46 +00:00
|
|
|
*/
|
|
|
|
int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
|
|
|
|
mbedtls_x509_crt *trust_ca,
|
|
|
|
mbedtls_x509_crl *ca_crl,
|
|
|
|
const mbedtls_x509_crt_profile *profile,
|
|
|
|
const char *cn, uint32_t *flags,
|
|
|
|
int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
|
|
|
|
void *p_vrfy )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2011-04-24 08:57:21 +00:00
|
|
|
size_t cn_len;
|
2012-09-28 07:10:55 +00:00
|
|
|
int ret;
|
2015-10-12 07:02:20 +00:00
|
|
|
int pathlen = 0, selfsigned = 0;
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crt *parent;
|
|
|
|
mbedtls_x509_name *name;
|
|
|
|
mbedtls_x509_sequence *cur = NULL;
|
2015-10-23 12:08:48 +00:00
|
|
|
mbedtls_pk_type_t pk_type;
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2009-05-03 10:18:48 +00:00
|
|
|
*flags = 0;
|
|
|
|
|
2017-06-22 10:19:27 +00:00
|
|
|
if( profile == NULL )
|
|
|
|
{
|
|
|
|
ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
if( cn != NULL )
|
|
|
|
{
|
|
|
|
name = &crt->subject;
|
|
|
|
cn_len = strlen( cn );
|
|
|
|
|
2015-04-20 11:19:02 +00:00
|
|
|
if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2012-05-10 14:12:46 +00:00
|
|
|
cur = &crt->subject_alt_names;
|
|
|
|
|
|
|
|
while( cur != NULL )
|
2012-02-11 16:09:32 +00:00
|
|
|
{
|
2012-08-23 10:49:55 +00:00
|
|
|
if( cur->buf.len == cn_len &&
|
2014-10-17 09:36:18 +00:00
|
|
|
x509_memcasecmp( cn, cur->buf.p, cn_len ) == 0 )
|
2012-02-11 16:09:32 +00:00
|
|
|
break;
|
|
|
|
|
2012-08-23 10:49:55 +00:00
|
|
|
if( cur->buf.len > 2 &&
|
|
|
|
memcmp( cur->buf.p, "*.", 2 ) == 0 &&
|
2015-06-23 07:24:29 +00:00
|
|
|
x509_check_wildcard( cn, &cur->buf ) == 0 )
|
|
|
|
{
|
2012-02-11 16:09:32 +00:00
|
|
|
break;
|
2015-06-23 07:24:29 +00:00
|
|
|
}
|
2012-05-10 14:12:46 +00:00
|
|
|
|
|
|
|
cur = cur->next;
|
2012-02-11 16:09:32 +00:00
|
|
|
}
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2012-05-10 14:12:46 +00:00
|
|
|
if( cur == NULL )
|
2015-04-08 10:49:31 +00:00
|
|
|
*flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
2012-05-10 14:12:46 +00:00
|
|
|
else
|
2012-02-11 16:09:32 +00:00
|
|
|
{
|
2012-05-10 14:12:46 +00:00
|
|
|
while( name != NULL )
|
2012-02-11 16:09:32 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, &name->oid ) == 0 )
|
2012-02-11 16:09:32 +00:00
|
|
|
{
|
2012-08-23 10:49:55 +00:00
|
|
|
if( name->val.len == cn_len &&
|
2014-10-17 09:36:18 +00:00
|
|
|
x509_memcasecmp( name->val.p, cn, cn_len ) == 0 )
|
2012-02-11 16:09:32 +00:00
|
|
|
break;
|
|
|
|
|
2012-08-23 10:49:55 +00:00
|
|
|
if( name->val.len > 2 &&
|
|
|
|
memcmp( name->val.p, "*.", 2 ) == 0 &&
|
2015-06-23 07:24:29 +00:00
|
|
|
x509_check_wildcard( cn, &name->val ) == 0 )
|
2012-02-11 17:38:38 +00:00
|
|
|
break;
|
2012-02-11 16:09:32 +00:00
|
|
|
}
|
2012-05-10 14:12:46 +00:00
|
|
|
|
|
|
|
name = name->next;
|
2012-02-11 16:09:32 +00:00
|
|
|
}
|
|
|
|
|
2012-05-10 14:12:46 +00:00
|
|
|
if( name == NULL )
|
2015-04-08 10:49:31 +00:00
|
|
|
*flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
|
2012-02-11 16:09:32 +00:00
|
|
|
}
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
2015-10-23 12:08:48 +00:00
|
|
|
/* Check the type and size of the key */
|
|
|
|
pk_type = mbedtls_pk_get_type( &crt->pk );
|
|
|
|
|
|
|
|
if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
|
|
|
|
*flags |= MBEDTLS_X509_BADCERT_BAD_PK;
|
|
|
|
|
|
|
|
if( x509_profile_check_key( profile, pk_type, &crt->pk ) != 0 )
|
|
|
|
*flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
|
|
|
|
|
2015-09-01 14:35:00 +00:00
|
|
|
/* Look for a parent in trusted CAs */
|
|
|
|
for( parent = trust_ca; parent != NULL; parent = parent->next )
|
2014-04-09 12:30:11 +00:00
|
|
|
{
|
2014-06-24 09:49:16 +00:00
|
|
|
if( x509_crt_check_parent( crt, parent, 0, pathlen == 0 ) == 0 )
|
2014-04-09 12:30:11 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( parent != NULL )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2015-09-01 14:35:00 +00:00
|
|
|
ret = x509_crt_verify_top( crt, parent, ca_crl, profile,
|
2015-10-12 07:02:20 +00:00
|
|
|
pathlen, selfsigned, flags, f_vrfy, p_vrfy );
|
2012-09-28 07:10:55 +00:00
|
|
|
if( ret != 0 )
|
2017-06-22 10:19:27 +00:00
|
|
|
goto exit;
|
2013-09-16 11:49:26 +00:00
|
|
|
}
|
2012-09-28 07:10:55 +00:00
|
|
|
else
|
2011-01-15 16:57:55 +00:00
|
|
|
{
|
2015-09-01 14:35:00 +00:00
|
|
|
/* Look for a parent upwards the chain */
|
|
|
|
for( parent = crt->next; parent != NULL; parent = parent->next )
|
|
|
|
if( x509_crt_check_parent( crt, parent, 0, pathlen == 0 ) == 0 )
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Are we part of the chain or at the top? */
|
|
|
|
if( parent != NULL )
|
|
|
|
{
|
|
|
|
ret = x509_crt_verify_child( crt, parent, trust_ca, ca_crl, profile,
|
2015-10-12 07:02:20 +00:00
|
|
|
pathlen, selfsigned, flags, f_vrfy, p_vrfy );
|
2015-09-01 14:35:00 +00:00
|
|
|
if( ret != 0 )
|
2017-06-22 10:19:27 +00:00
|
|
|
goto exit;
|
2015-09-01 14:35:00 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = x509_crt_verify_top( crt, trust_ca, ca_crl, profile,
|
2015-10-12 07:02:20 +00:00
|
|
|
pathlen, selfsigned, flags, f_vrfy, p_vrfy );
|
2015-09-01 14:35:00 +00:00
|
|
|
if( ret != 0 )
|
2017-06-22 10:19:27 +00:00
|
|
|
goto exit;
|
2015-09-01 14:35:00 +00:00
|
|
|
}
|
2011-01-13 17:54:59 +00:00
|
|
|
}
|
2012-09-28 07:10:55 +00:00
|
|
|
|
2017-06-22 10:19:27 +00:00
|
|
|
exit:
|
2017-07-06 10:16:25 +00:00
|
|
|
/* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
|
|
|
|
* the SSL module for authmode optional, but non-zero return from the
|
|
|
|
* callback means a fatal error so it shouldn't be ignored */
|
2017-06-26 08:11:49 +00:00
|
|
|
if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
|
|
|
|
ret = MBEDTLS_ERR_X509_FATAL_ERROR;
|
|
|
|
|
2017-06-22 10:19:27 +00:00
|
|
|
if( ret != 0 )
|
|
|
|
{
|
|
|
|
*flags = (uint32_t) -1;
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2012-09-28 07:10:55 +00:00
|
|
|
if( *flags != 0 )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
|
2011-01-13 17:54:59 +00:00
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2013-09-18 09:58:25 +00:00
|
|
|
/*
|
|
|
|
* Initialize a certificate chain
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
|
2013-09-18 09:58:25 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
memset( crt, 0, sizeof(mbedtls_x509_crt) );
|
2013-09-18 09:58:25 +00:00
|
|
|
}
|
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
/*
|
|
|
|
* Unallocate all certificate data
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crt *cert_cur = crt;
|
|
|
|
mbedtls_x509_crt *cert_prv;
|
|
|
|
mbedtls_x509_name *name_cur;
|
|
|
|
mbedtls_x509_name *name_prv;
|
|
|
|
mbedtls_x509_sequence *seq_cur;
|
|
|
|
mbedtls_x509_sequence *seq_prv;
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
if( crt == NULL )
|
|
|
|
return;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_pk_free( &cert_cur->pk );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
|
|
|
|
mbedtls_free( cert_cur->sig_opts );
|
2014-06-05 13:14:28 +00:00
|
|
|
#endif
|
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
name_cur = cert_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 );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
name_cur = cert_cur->subject.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 );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
2011-01-15 16:57:55 +00:00
|
|
|
seq_cur = cert_cur->ext_key_usage.next;
|
|
|
|
while( seq_cur != NULL )
|
|
|
|
{
|
|
|
|
seq_prv = seq_cur;
|
|
|
|
seq_cur = seq_cur->next;
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_zeroize( seq_prv, sizeof( mbedtls_x509_sequence ) );
|
|
|
|
mbedtls_free( seq_prv );
|
2011-01-15 16:57:55 +00:00
|
|
|
}
|
|
|
|
|
2012-02-11 18:42:45 +00:00
|
|
|
seq_cur = cert_cur->subject_alt_names.next;
|
|
|
|
while( seq_cur != NULL )
|
|
|
|
{
|
|
|
|
seq_prv = seq_cur;
|
|
|
|
seq_cur = seq_cur->next;
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_zeroize( seq_prv, sizeof( mbedtls_x509_sequence ) );
|
|
|
|
mbedtls_free( seq_prv );
|
2012-02-11 18:42:45 +00:00
|
|
|
}
|
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
if( cert_cur->raw.p != NULL )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_zeroize( cert_cur->raw.p, cert_cur->raw.len );
|
|
|
|
mbedtls_free( cert_cur->raw.p );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cert_cur = cert_cur->next;
|
|
|
|
}
|
|
|
|
while( cert_cur != NULL );
|
|
|
|
|
|
|
|
cert_cur = crt;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
cert_prv = cert_cur;
|
|
|
|
cert_cur = cert_cur->next;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
|
2009-01-03 21:22:43 +00:00
|
|
|
if( cert_prv != crt )
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_free( cert_prv );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
while( cert_cur != NULL );
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_X509_CRT_PARSE_C */
|