2013-09-16 13:49:26 +02:00
|
|
|
/*
|
|
|
|
* X.509 Certificate Signing Request (CSR) parsing
|
|
|
|
*
|
2020-08-19 16:54:51 +02:00
|
|
|
* Copyright The Mbed TLS Contributors
|
Update license headers to Apache-2.0 OR GPL-2.0-or-later
This will allow us to ship the LTS branches in a single archive
This commit was generated using the following script:
# ========================
#!/bin/sh
header1='\ * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later\
*\
* This file is provided under the Apache License 2.0, or the\
* GNU General Public License v2.0 or later.\
*\
* **********\
* Apache License 2.0:\
*\
* Licensed under the Apache License, Version 2.0 (the "License"); you may\
* not use this file except in compliance with the License.\
* You may obtain a copy of the License at\
*\
* http://www.apache.org/licenses/LICENSE-2.0\
*\
* Unless required by applicable law or agreed to in writing, software\
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT\
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\
* See the License for the specific language governing permissions and\
* limitations under the License.\
*\
* **********\
*\
* **********\
* GNU General Public License v2.0 or later:\
*\
* This program is free software; you can redistribute it and/or modify\
* it under the terms of the GNU General Public License as published by\
* the Free Software Foundation; either version 2 of the License, or\
* (at your option) any later version.\
*\
* This program is distributed in the hope that it will be useful,\
* but WITHOUT ANY WARRANTY; without even the implied warranty of\
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\
* GNU General Public License for more details.\
*\
* You should have received a copy of the GNU General Public License along\
* with this program; if not, write to the Free Software Foundation, Inc.,\
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.\
*\
* **********'
find -path './.git' -prune -o '(' -name '*.c' -o -name '*.cpp' -o -name '*.fmt' -o -name '*.h' ')' -print | xargs sed -i "
# Normalize the first line of the copyright headers (no text on the first line of a block comment)
/^\/\*.*Copyright.*Arm/I s/\/\*/&\n */
# Insert new copyright header
/SPDX-License-Identifier/ i\
$header1
# Delete old copyright header
/SPDX-License-Identifier/,$ {
# Delete lines until the one preceding the mbedtls declaration
N
1,/This file is part of/ {
/This file is part of/! D
}
}
"
# Format copyright header for inclusion into scripts
header2=$(echo "$header1" | sed 's/^\\\? \* \?/#/')
find -path './.git' -prune -o '(' -name '*.gdb' -o -name '*.pl' -o -name '*.py' -o -name '*.sh' ')' -print | xargs sed -i "
# Insert new copyright header
/SPDX-License-Identifier/ i\
$header2
# Delete old copyright header
/SPDX-License-Identifier/,$ {
# Delete lines until the one preceding the mbedtls declaration
N
1,/This file is part of/ {
/This file is part of/! D
}
}
"
# ========================
Signed-off-by: Bence Szépkúti <bence.szepkuti@arm.com>
2020-06-05 13:02:18 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
|
|
|
*
|
|
|
|
* This file is provided under the Apache License 2.0, or the
|
|
|
|
* GNU General Public License v2.0 or later.
|
|
|
|
*
|
|
|
|
* **********
|
|
|
|
* Apache License 2.0:
|
2013-09-16 13:49:26 +02:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2013-09-16 13:49:26 +02:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2013-09-16 13:49:26 +02:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2013-09-16 13:49:26 +02:00
|
|
|
*
|
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 13:02:18 +02:00
|
|
|
* **********
|
|
|
|
*
|
|
|
|
* **********
|
|
|
|
* GNU General Public License v2.0 or later:
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
* **********
|
2013-09-16 13:49:26 +02:00
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* The ITU-T X.509 standard defines a certificate format for PKI.
|
|
|
|
*
|
2014-06-12 22:34:55 +02:00
|
|
|
* http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
|
|
|
|
* http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
|
|
|
|
* http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
|
2013-09-16 13:49:26 +02:00
|
|
|
*
|
|
|
|
* http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
|
|
|
|
* http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
|
|
|
|
*/
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if !defined(MBEDTLS_CONFIG_FILE)
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/config.h"
|
2014-04-29 12:39:06 +02:00
|
|
|
#else
|
2015-04-08 12:49:31 +02:00
|
|
|
#include MBEDTLS_CONFIG_FILE
|
2014-04-29 12:39:06 +02:00
|
|
|
#endif
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_X509_CSR_PARSE_C)
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/x509_csr.h"
|
|
|
|
#include "mbedtls/oid.h"
|
2015-02-06 14:43:58 +01:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_PEM_PARSE_C)
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/pem.h"
|
2013-09-16 13:49:26 +02:00
|
|
|
#endif
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_PLATFORM_C)
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/platform.h"
|
2013-09-16 13:49:26 +02:00
|
|
|
#else
|
2015-02-06 14:43:58 +01:00
|
|
|
#include <stdlib.h>
|
2015-02-17 16:46:45 +01:00
|
|
|
#include <stdio.h>
|
2015-04-08 12:49:31 +02:00
|
|
|
#define mbedtls_free free
|
2015-05-26 16:04:06 +02:00
|
|
|
#define mbedtls_calloc calloc
|
2015-04-08 12:49:31 +02:00
|
|
|
#define mbedtls_snprintf snprintf
|
2013-09-16 13:49:26 +02:00
|
|
|
#endif
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_FS_IO) || defined(EFIX64) || defined(EFI32)
|
2013-09-16 13:49:26 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#endif
|
|
|
|
|
2014-06-13 17:20:13 +02:00
|
|
|
/* Implementation that should never be optimized out by the compiler */
|
2015-04-08 12:49:31 +02:00
|
|
|
static void mbedtls_zeroize( void *v, size_t n ) {
|
2014-06-13 17:20:13 +02:00
|
|
|
volatile unsigned char *p = v; while( n-- ) *p++ = 0;
|
|
|
|
}
|
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
/*
|
|
|
|
* Version ::= INTEGER { v1(0) }
|
|
|
|
*/
|
|
|
|
static int x509_csr_get_version( unsigned char **p,
|
|
|
|
const unsigned char *end,
|
|
|
|
int *ver )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
|
|
|
*ver = 0;
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_X509_INVALID_VERSION + ret );
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2014-06-16 18:06:48 +02:00
|
|
|
* Parse a CSR in DER format
|
2013-09-16 13:49:26 +02:00
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_x509_csr_parse_der( mbedtls_x509_csr *csr,
|
2014-06-16 18:06:48 +02:00
|
|
|
const unsigned char *buf, size_t buflen )
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t len;
|
|
|
|
unsigned char *p, *end;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_buf sig_params;
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
memset( &sig_params, 0, sizeof( mbedtls_x509_buf ) );
|
2014-06-05 17:02:24 +02:00
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
/*
|
|
|
|
* Check for valid input
|
|
|
|
*/
|
2016-04-13 12:53:27 +02:00
|
|
|
if( csr == NULL || buf == NULL || buflen == 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_csr_init( csr );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2014-06-16 18:06:48 +02:00
|
|
|
/*
|
|
|
|
* first copy the raw DER data
|
|
|
|
*/
|
2015-05-26 16:04:06 +02:00
|
|
|
p = mbedtls_calloc( 1, len = buflen );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2014-06-16 18:06:48 +02:00
|
|
|
if( p == NULL )
|
2015-05-28 09:33:39 +02:00
|
|
|
return( MBEDTLS_ERR_X509_ALLOC_FAILED );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2014-06-16 18:06:48 +02:00
|
|
|
memcpy( p, buf, buflen );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
csr->raw.p = p;
|
|
|
|
csr->raw.len = len;
|
|
|
|
end = p + len;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* CertificationRequest ::= SEQUENCE {
|
|
|
|
* certificationRequestInfo CertificationRequestInfo,
|
|
|
|
* signatureAlgorithm AlgorithmIdentifier,
|
|
|
|
* signature BIT STRING
|
|
|
|
* }
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_csr_free( csr );
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_FORMAT );
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if( len != (size_t) ( end - p ) )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_csr_free( csr );
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_FORMAT +
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* CertificationRequestInfo ::= SEQUENCE {
|
|
|
|
*/
|
|
|
|
csr->cri.p = p;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_csr_free( csr );
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
end = p + len;
|
|
|
|
csr->cri.len = end - csr->cri.p;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Version ::= INTEGER { v1(0) }
|
|
|
|
*/
|
|
|
|
if( ( ret = x509_csr_get_version( &p, end, &csr->version ) ) != 0 )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_csr_free( csr );
|
2013-09-16 13:49:26 +02:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2017-02-17 14:54:43 +01:00
|
|
|
if( csr->version != 0 )
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_csr_free( csr );
|
|
|
|
return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
2017-02-17 14:54:43 +01:00
|
|
|
csr->version++;
|
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
/*
|
|
|
|
* subject Name
|
|
|
|
*/
|
|
|
|
csr->subject_raw.p = p;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_csr_free( csr );
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_x509_get_name( &p, p + len, &csr->subject ) ) != 0 )
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_csr_free( csr );
|
2013-09-16 13:49:26 +02:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
csr->subject_raw.len = p - csr->subject_raw.p;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* subjectPKInfo SubjectPublicKeyInfo
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &csr->pk ) ) != 0 )
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_csr_free( csr );
|
2013-09-16 13:49:26 +02:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* attributes [0] Attributes
|
2016-02-24 15:36:05 +01:00
|
|
|
*
|
|
|
|
* The list of possible attributes is open-ended, though RFC 2985
|
|
|
|
* (PKCS#9) defines a few in section 5.4. We currently don't support any,
|
|
|
|
* so we just ignore them. This is a safe thing to do as the worst thing
|
|
|
|
* that could happen is that we issue a certificate that does not match
|
|
|
|
* the requester's expectations - this cannot cause a violation of our
|
|
|
|
* signature policies.
|
2013-09-16 13:49:26 +02:00
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC ) ) != 0 )
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_csr_free( csr );
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
p += len;
|
|
|
|
|
|
|
|
end = csr->raw.p + csr->raw.len;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* signatureAlgorithm AlgorithmIdentifier,
|
|
|
|
* signature BIT STRING
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_x509_get_alg( &p, end, &csr->sig_oid, &sig_params ) ) != 0 )
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_csr_free( csr );
|
2013-09-16 13:49:26 +02:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_x509_get_sig_alg( &csr->sig_oid, &sig_params,
|
2014-06-05 15:14:28 +02:00
|
|
|
&csr->sig_md, &csr->sig_pk,
|
|
|
|
&csr->sig_opts ) ) != 0 )
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_csr_free( csr );
|
|
|
|
return( MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG );
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_x509_get_sig( &p, end, &csr->sig ) ) != 0 )
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_csr_free( csr );
|
2013-09-16 13:49:26 +02:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( p != end )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_csr_free( csr );
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_FORMAT +
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2014-06-16 18:06:48 +02:00
|
|
|
/*
|
|
|
|
* Parse a CSR, allowing for PEM or raw DER encoding
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_x509_csr_parse( mbedtls_x509_csr *csr, const unsigned char *buf, size_t buflen )
|
2014-06-16 18:06:48 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_PEM_PARSE_C)
|
2016-12-07 16:05:53 +01:00
|
|
|
int ret;
|
2014-06-16 18:06:48 +02:00
|
|
|
size_t use_len;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pem_context pem;
|
2014-06-16 18:06:48 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for valid input
|
|
|
|
*/
|
2016-04-13 12:53:27 +02:00
|
|
|
if( csr == NULL || buf == NULL || buflen == 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
|
2014-06-16 18:06:48 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_PEM_PARSE_C)
|
2015-05-12 11:20:10 +02:00
|
|
|
/* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
|
2018-06-20 08:13:24 +02:00
|
|
|
if( buf[buflen - 1] == '\0' )
|
|
|
|
{
|
2018-06-14 07:35:11 +02:00
|
|
|
mbedtls_pem_init( &pem );
|
2015-05-12 11:20:10 +02:00
|
|
|
ret = mbedtls_pem_read_buffer( &pem,
|
2018-09-30 16:36:50 +02:00
|
|
|
"-----BEGIN CERTIFICATE REQUEST-----",
|
|
|
|
"-----END CERTIFICATE REQUEST-----",
|
|
|
|
buf, NULL, 0, &use_len );
|
2018-10-07 18:48:37 +02:00
|
|
|
if( ret == MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
|
2018-09-30 16:36:50 +02:00
|
|
|
{
|
|
|
|
ret = mbedtls_pem_read_buffer( &pem,
|
|
|
|
"-----BEGIN NEW CERTIFICATE REQUEST-----",
|
|
|
|
"-----END NEW CERTIFICATE REQUEST-----",
|
|
|
|
buf, NULL, 0, &use_len );
|
|
|
|
}
|
2018-10-07 18:48:37 +02:00
|
|
|
|
2018-06-14 07:35:11 +02:00
|
|
|
if( ret == 0 )
|
2018-09-30 16:36:50 +02:00
|
|
|
{
|
2018-06-14 07:35:11 +02:00
|
|
|
/*
|
|
|
|
* Was PEM encoded, parse the result
|
|
|
|
*/
|
|
|
|
ret = mbedtls_x509_csr_parse_der( csr, pem.buf, pem.buflen );
|
2018-09-30 16:36:50 +02:00
|
|
|
}
|
2018-06-14 07:35:11 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pem_free( &pem );
|
2018-06-14 07:35:11 +02:00
|
|
|
if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
|
|
|
|
return( ret );
|
2014-06-16 18:06:48 +02:00
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_PEM_PARSE_C */
|
|
|
|
return( mbedtls_x509_csr_parse_der( csr, buf, buflen ) );
|
2014-06-16 18:06:48 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_FS_IO)
|
2013-09-16 13:49:26 +02:00
|
|
|
/*
|
|
|
|
* Load a CSR into the structure
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_x509_csr_parse_file( mbedtls_x509_csr *csr, const char *path )
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t n;
|
|
|
|
unsigned char *buf;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
|
2013-09-16 13:49:26 +02:00
|
|
|
return( ret );
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = mbedtls_x509_csr_parse( csr, buf, n );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2015-05-12 11:20:10 +02:00
|
|
|
mbedtls_zeroize( buf, n );
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_free( buf );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_FS_IO */
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
#define BEFORE_COLON 14
|
|
|
|
#define BC "14"
|
|
|
|
/*
|
|
|
|
* Return an informational string about the CSR.
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_x509_csr_info( char *buf, size_t size, const char *prefix,
|
|
|
|
const mbedtls_x509_csr *csr )
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t n;
|
|
|
|
char *p;
|
|
|
|
char key_size_str[BEFORE_COLON];
|
|
|
|
|
|
|
|
p = buf;
|
|
|
|
n = size;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = mbedtls_snprintf( p, n, "%sCSR version : %d",
|
2013-09-16 13:49:26 +02:00
|
|
|
prefix, csr->version );
|
2015-06-22 11:12:02 +02:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
|
2015-06-22 11:12:02 +02:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = mbedtls_x509_dn_gets( p, n, &csr->subject );
|
2015-06-22 11:12:02 +02:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
|
2015-06-22 11:12:02 +02:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = mbedtls_x509_sig_alg_gets( p, n, &csr->sig_oid, csr->sig_pk, csr->sig_md,
|
2014-06-05 17:07:30 +02:00
|
|
|
csr->sig_opts );
|
2015-06-22 11:12:02 +02:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
|
|
|
|
mbedtls_pk_get_name( &csr->pk ) ) ) != 0 )
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits\n", prefix, key_size_str,
|
2015-06-18 16:43:38 +02:00
|
|
|
(int) mbedtls_pk_get_bitlen( &csr->pk ) );
|
2015-06-22 11:12:02 +02:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
return( (int) ( size - n ) );
|
|
|
|
}
|
|
|
|
|
2013-09-18 11:58:25 +02:00
|
|
|
/*
|
|
|
|
* Initialize a CSR
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
void mbedtls_x509_csr_init( mbedtls_x509_csr *csr )
|
2013-09-18 11:58:25 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
memset( csr, 0, sizeof(mbedtls_x509_csr) );
|
2013-09-18 11:58:25 +02:00
|
|
|
}
|
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
/*
|
|
|
|
* Unallocate all CSR data
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
void mbedtls_x509_csr_free( mbedtls_x509_csr *csr )
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_name *name_cur;
|
|
|
|
mbedtls_x509_name *name_prv;
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
if( csr == NULL )
|
|
|
|
return;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_free( &csr->pk );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
|
|
|
|
mbedtls_free( csr->sig_opts );
|
2014-06-05 15:14:28 +02:00
|
|
|
#endif
|
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
name_cur = csr->subject.next;
|
|
|
|
while( name_cur != NULL )
|
|
|
|
{
|
|
|
|
name_prv = name_cur;
|
|
|
|
name_cur = name_cur->next;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
|
|
|
|
mbedtls_free( name_prv );
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if( csr->raw.p != NULL )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_zeroize( csr->raw.p, csr->raw.len );
|
|
|
|
mbedtls_free( csr->raw.p );
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_zeroize( csr, sizeof( mbedtls_x509_csr ) );
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_X509_CSR_PARSE_C */
|