2013-09-16 13:49:26 +02:00
|
|
|
/*
|
|
|
|
* X.509 base functions for creating certificates / CSRs
|
|
|
|
*
|
2015-07-27 11:11:48 +02: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 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.
|
|
|
|
*
|
|
|
|
* **********
|
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* This file is part of mbed TLS (https://tls.mbed.org)
|
2013-09-16 13:49:26 +02:00
|
|
|
*/
|
|
|
|
|
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_CREATE_C)
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/x509.h"
|
|
|
|
#include "mbedtls/asn1write.h"
|
|
|
|
#include "mbedtls/oid.h"
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2015-02-06 14:43:58 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
2014-06-12 11:06:36 +02:00
|
|
|
typedef struct {
|
|
|
|
const char *name;
|
|
|
|
size_t name_len;
|
|
|
|
const char*oid;
|
|
|
|
} x509_attr_descriptor_t;
|
|
|
|
|
|
|
|
#define ADD_STRLEN( s ) s, sizeof( s ) - 1
|
|
|
|
|
|
|
|
static const x509_attr_descriptor_t x509_attrs[] =
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
{ ADD_STRLEN( "CN" ), MBEDTLS_OID_AT_CN },
|
|
|
|
{ ADD_STRLEN( "commonName" ), MBEDTLS_OID_AT_CN },
|
|
|
|
{ ADD_STRLEN( "C" ), MBEDTLS_OID_AT_COUNTRY },
|
|
|
|
{ ADD_STRLEN( "countryName" ), MBEDTLS_OID_AT_COUNTRY },
|
|
|
|
{ ADD_STRLEN( "O" ), MBEDTLS_OID_AT_ORGANIZATION },
|
|
|
|
{ ADD_STRLEN( "organizationName" ), MBEDTLS_OID_AT_ORGANIZATION },
|
|
|
|
{ ADD_STRLEN( "L" ), MBEDTLS_OID_AT_LOCALITY },
|
|
|
|
{ ADD_STRLEN( "locality" ), MBEDTLS_OID_AT_LOCALITY },
|
|
|
|
{ ADD_STRLEN( "R" ), MBEDTLS_OID_PKCS9_EMAIL },
|
|
|
|
{ ADD_STRLEN( "OU" ), MBEDTLS_OID_AT_ORG_UNIT },
|
|
|
|
{ ADD_STRLEN( "organizationalUnitName" ), MBEDTLS_OID_AT_ORG_UNIT },
|
|
|
|
{ ADD_STRLEN( "ST" ), MBEDTLS_OID_AT_STATE },
|
|
|
|
{ ADD_STRLEN( "stateOrProvinceName" ), MBEDTLS_OID_AT_STATE },
|
|
|
|
{ ADD_STRLEN( "emailAddress" ), MBEDTLS_OID_PKCS9_EMAIL },
|
|
|
|
{ ADD_STRLEN( "serialNumber" ), MBEDTLS_OID_AT_SERIAL_NUMBER },
|
|
|
|
{ ADD_STRLEN( "postalAddress" ), MBEDTLS_OID_AT_POSTAL_ADDRESS },
|
|
|
|
{ ADD_STRLEN( "postalCode" ), MBEDTLS_OID_AT_POSTAL_CODE },
|
|
|
|
{ ADD_STRLEN( "dnQualifier" ), MBEDTLS_OID_AT_DN_QUALIFIER },
|
|
|
|
{ ADD_STRLEN( "title" ), MBEDTLS_OID_AT_TITLE },
|
|
|
|
{ ADD_STRLEN( "surName" ), MBEDTLS_OID_AT_SUR_NAME },
|
|
|
|
{ ADD_STRLEN( "SN" ), MBEDTLS_OID_AT_SUR_NAME },
|
|
|
|
{ ADD_STRLEN( "givenName" ), MBEDTLS_OID_AT_GIVEN_NAME },
|
|
|
|
{ ADD_STRLEN( "GN" ), MBEDTLS_OID_AT_GIVEN_NAME },
|
|
|
|
{ ADD_STRLEN( "initials" ), MBEDTLS_OID_AT_INITIALS },
|
|
|
|
{ ADD_STRLEN( "pseudonym" ), MBEDTLS_OID_AT_PSEUDONYM },
|
|
|
|
{ ADD_STRLEN( "generationQualifier" ), MBEDTLS_OID_AT_GENERATION_QUALIFIER },
|
|
|
|
{ ADD_STRLEN( "domainComponent" ), MBEDTLS_OID_DOMAIN_COMPONENT },
|
|
|
|
{ ADD_STRLEN( "DC" ), MBEDTLS_OID_DOMAIN_COMPONENT },
|
2014-06-12 11:06:36 +02:00
|
|
|
{ NULL, 0, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char *x509_at_oid_from_name( const char *name, size_t name_len )
|
|
|
|
{
|
|
|
|
const x509_attr_descriptor_t *cur;
|
|
|
|
|
|
|
|
for( cur = x509_attrs; cur->name != NULL; cur++ )
|
|
|
|
if( cur->name_len == name_len &&
|
2015-05-28 17:06:07 +02:00
|
|
|
strncmp( cur->name, name, name_len ) == 0 )
|
2014-06-12 11:06:36 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
return( cur->oid );
|
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_x509_string_to_names( mbedtls_asn1_named_data **head, const char *name )
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
|
|
|
int ret = 0;
|
2013-10-28 21:19:10 +01:00
|
|
|
const char *s = name, *c = s;
|
|
|
|
const char *end = s + strlen( s );
|
2013-10-11 09:36:52 +02:00
|
|
|
const char *oid = NULL;
|
2013-09-16 13:49:26 +02:00
|
|
|
int in_tag = 1;
|
2015-04-08 12:49:31 +02:00
|
|
|
char data[MBEDTLS_X509_MAX_DN_NAME_SIZE];
|
2014-08-08 12:22:30 +02:00
|
|
|
char *d = data;
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
/* Clear existing chain if present */
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_asn1_free_named_data_list( head );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
while( c <= end )
|
|
|
|
{
|
|
|
|
if( in_tag && *c == '=' )
|
|
|
|
{
|
2014-06-12 11:06:36 +02:00
|
|
|
if( ( oid = x509_at_oid_from_name( s, c - s ) ) == NULL )
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = MBEDTLS_ERR_X509_UNKNOWN_OID;
|
2013-09-16 13:49:26 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
s = c + 1;
|
|
|
|
in_tag = 0;
|
2014-08-08 12:22:30 +02:00
|
|
|
d = data;
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
2014-08-08 12:22:30 +02:00
|
|
|
if( !in_tag && *c == '\\' && c != end )
|
|
|
|
{
|
|
|
|
c++;
|
|
|
|
|
|
|
|
/* Check for valid escaped characters */
|
|
|
|
if( c == end || *c != ',' )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = MBEDTLS_ERR_X509_INVALID_NAME;
|
2014-08-08 12:22:30 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( !in_tag && ( *c == ',' || c == end ) )
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
if( mbedtls_asn1_store_named_data( head, oid, strlen( oid ),
|
2014-08-08 12:22:30 +02:00
|
|
|
(unsigned char *) data,
|
|
|
|
d - data ) == NULL )
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
2015-05-28 09:33:39 +02:00
|
|
|
return( MBEDTLS_ERR_X509_ALLOC_FAILED );
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
while( c < end && *(c + 1) == ' ' )
|
|
|
|
c++;
|
|
|
|
|
|
|
|
s = c + 1;
|
|
|
|
in_tag = 1;
|
|
|
|
}
|
2014-08-08 12:22:30 +02:00
|
|
|
|
|
|
|
if( !in_tag && s != c + 1 )
|
|
|
|
{
|
|
|
|
*(d++) = *c;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( d - data == MBEDTLS_X509_MAX_DN_NAME_SIZE )
|
2014-08-08 12:22:30 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = MBEDTLS_ERR_X509_INVALID_NAME;
|
2014-08-08 12:22:30 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
c++;
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* The first byte of the value in the mbedtls_asn1_named_data structure is reserved
|
2013-09-16 13:49:26 +02:00
|
|
|
* to store the critical boolean for us
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_x509_set_extension( mbedtls_asn1_named_data **head, const char *oid, size_t oid_len,
|
2013-09-16 13:49:26 +02:00
|
|
|
int critical, const unsigned char *val, size_t val_len )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_asn1_named_data *cur;
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( cur = mbedtls_asn1_store_named_data( head, oid, oid_len,
|
2013-09-16 13:49:26 +02:00
|
|
|
NULL, val_len + 1 ) ) == NULL )
|
|
|
|
{
|
2015-05-28 09:33:39 +02:00
|
|
|
return( MBEDTLS_ERR_X509_ALLOC_FAILED );
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
cur->val.p[0] = critical;
|
|
|
|
memcpy( cur->val.p + 1, val, val_len );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* RelativeDistinguishedName ::=
|
|
|
|
* SET OF AttributeTypeAndValue
|
|
|
|
*
|
|
|
|
* AttributeTypeAndValue ::= SEQUENCE {
|
|
|
|
* type AttributeType,
|
|
|
|
* value AttributeValue }
|
|
|
|
*
|
|
|
|
* AttributeType ::= OBJECT IDENTIFIER
|
|
|
|
*
|
|
|
|
* AttributeValue ::= ANY DEFINED BY AttributeType
|
|
|
|
*/
|
|
|
|
static int x509_write_name( unsigned char **p, unsigned char *start,
|
|
|
|
const char *oid, size_t oid_len,
|
|
|
|
const unsigned char *name, size_t name_len )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t len = 0;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
// Write PrintableString for all except MBEDTLS_OID_PKCS9_EMAIL
|
2013-09-16 13:49:26 +02:00
|
|
|
//
|
2015-04-08 12:49:31 +02:00
|
|
|
if( MBEDTLS_OID_SIZE( MBEDTLS_OID_PKCS9_EMAIL ) == oid_len &&
|
|
|
|
memcmp( oid, MBEDTLS_OID_PKCS9_EMAIL, oid_len ) == 0 )
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_ia5_string( p, start,
|
2013-09-16 13:49:26 +02:00
|
|
|
(const char *) name,
|
|
|
|
name_len ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_printable_string( p, start,
|
2013-09-16 13:49:26 +02:00
|
|
|
(const char *) name,
|
|
|
|
name_len ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write OID
|
|
|
|
//
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid, oid_len ) );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
|
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_CONSTRUCTED |
|
|
|
|
MBEDTLS_ASN1_SEQUENCE ) );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
|
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_CONSTRUCTED |
|
|
|
|
MBEDTLS_ASN1_SET ) );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2013-10-11 18:58:55 +02:00
|
|
|
return( (int) len );
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_x509_write_names( unsigned char **p, unsigned char *start,
|
|
|
|
mbedtls_asn1_named_data *first )
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t len = 0;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_asn1_named_data *cur = first;
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
while( cur != NULL )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, x509_write_name( p, start, (char *) cur->oid.p,
|
2013-09-16 13:49:26 +02:00
|
|
|
cur->oid.len,
|
|
|
|
cur->val.p, cur->val.len ) );
|
|
|
|
cur = cur->next;
|
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
|
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_CONSTRUCTED |
|
|
|
|
MBEDTLS_ASN1_SEQUENCE ) );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2013-10-11 18:58:55 +02:00
|
|
|
return( (int) len );
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_x509_write_sig( unsigned char **p, unsigned char *start,
|
2013-09-16 13:49:26 +02:00
|
|
|
const char *oid, size_t oid_len,
|
|
|
|
unsigned char *sig, size_t size )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t len = 0;
|
|
|
|
|
2015-10-21 12:23:09 +02:00
|
|
|
if( *p < start || (size_t)( *p - start ) < size )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
len = size;
|
|
|
|
(*p) -= len;
|
|
|
|
memcpy( *p, sig, len );
|
|
|
|
|
2015-10-21 12:23:09 +02:00
|
|
|
if( *p - start < 1 )
|
|
|
|
return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
|
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
*--(*p) = 0;
|
|
|
|
len += 1;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
|
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_BIT_STRING ) );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
// Write OID
|
|
|
|
//
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_algorithm_identifier( p, start, oid,
|
2013-09-16 13:49:26 +02:00
|
|
|
oid_len, 0 ) );
|
|
|
|
|
2013-10-11 18:58:55 +02:00
|
|
|
return( (int) len );
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int x509_write_extension( unsigned char **p, unsigned char *start,
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_asn1_named_data *ext )
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t len = 0;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, ext->val.p + 1,
|
2013-09-16 13:49:26 +02:00
|
|
|
ext->val.len - 1 ) );
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, ext->val.len - 1 ) );
|
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OCTET_STRING ) );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
if( ext->val.p[0] != 0 )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_bool( p, start, 1 ) );
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, ext->oid.p,
|
2013-09-16 13:49:26 +02:00
|
|
|
ext->oid.len ) );
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, ext->oid.len ) );
|
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OID ) );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
|
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_CONSTRUCTED |
|
|
|
|
MBEDTLS_ASN1_SEQUENCE ) );
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2013-10-11 18:58:55 +02:00
|
|
|
return( (int) len );
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Extension ::= SEQUENCE {
|
|
|
|
* extnID OBJECT IDENTIFIER,
|
|
|
|
* critical BOOLEAN DEFAULT FALSE,
|
|
|
|
* extnValue OCTET STRING
|
|
|
|
* -- contains the DER encoding of an ASN.1 value
|
|
|
|
* -- corresponding to the extension type identified
|
|
|
|
* -- by extnID
|
|
|
|
* }
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_x509_write_extensions( unsigned char **p, unsigned char *start,
|
|
|
|
mbedtls_asn1_named_data *first )
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t len = 0;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_asn1_named_data *cur_ext = first;
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
while( cur_ext != NULL )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, x509_write_extension( p, start, cur_ext ) );
|
2013-09-16 13:49:26 +02:00
|
|
|
cur_ext = cur_ext->next;
|
|
|
|
}
|
|
|
|
|
2013-10-11 18:58:55 +02:00
|
|
|
return( (int) len );
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_X509_CREATE_C */
|