2012-02-13 23:11:30 +00:00
|
|
|
/*
|
|
|
|
* ASN.1 buffer writing functionality
|
|
|
|
*
|
2020-08-19 14:37:36 +00:00
|
|
|
* Copyright The Mbed TLS Contributors
|
Update license headers to Apache-2.0 OR GPL-2.0-or-later
This will allow us to ship the LTS branches in a single archive
This commit was generated using the following script:
# ========================
#!/bin/sh
header1='\ * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later\
*\
* This file is provided under the Apache License 2.0, or the\
* GNU General Public License v2.0 or later.\
*\
* **********\
* Apache License 2.0:\
*\
* Licensed under the Apache License, Version 2.0 (the "License"); you may\
* not use this file except in compliance with the License.\
* You may obtain a copy of the License at\
*\
* http://www.apache.org/licenses/LICENSE-2.0\
*\
* Unless required by applicable law or agreed to in writing, software\
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT\
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\
* See the License for the specific language governing permissions and\
* limitations under the License.\
*\
* **********\
*\
* **********\
* GNU General Public License v2.0 or later:\
*\
* This program is free software; you can redistribute it and/or modify\
* it under the terms of the GNU General Public License as published by\
* the Free Software Foundation; either version 2 of the License, or\
* (at your option) any later version.\
*\
* This program is distributed in the hope that it will be useful,\
* but WITHOUT ANY WARRANTY; without even the implied warranty of\
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\
* GNU General Public License for more details.\
*\
* You should have received a copy of the GNU General Public License along\
* with this program; if not, write to the Free Software Foundation, Inc.,\
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.\
*\
* **********'
find -path './.git' -prune -o '(' -name '*.c' -o -name '*.cpp' -o -name '*.fmt' -o -name '*.h' ')' -print | xargs sed -i "
# Normalize the first line of the copyright headers (no text on the first line of a block comment)
/^\/\*.*Copyright.*Arm/I s/\/\*/&\n */
# Insert new copyright header
/SPDX-License-Identifier/ i\
$header1
# Delete old copyright header
/SPDX-License-Identifier/,$ {
# Delete lines until the one preceding the mbedtls declaration
N
1,/This file is part of/ {
/This file is part of/! D
}
}
"
# Format copyright header for inclusion into scripts
header2=$(echo "$header1" | sed 's/^\\\? \* \?/#/')
find -path './.git' -prune -o '(' -name '*.gdb' -o -name '*.pl' -o -name '*.py' -o -name '*.sh' ')' -print | xargs sed -i "
# Insert new copyright header
/SPDX-License-Identifier/ i\
$header2
# Delete old copyright header
/SPDX-License-Identifier/,$ {
# Delete lines until the one preceding the mbedtls declaration
N
1,/This file is part of/ {
/This file is part of/! D
}
}
"
# ========================
Signed-off-by: Bence Szépkúti <bence.szepkuti@arm.com>
2020-06-05 11:02:18 +00:00
|
|
|
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
|
|
|
*
|
|
|
|
* This file is provided under the Apache License 2.0, or the
|
|
|
|
* GNU General Public License v2.0 or later.
|
|
|
|
*
|
|
|
|
* **********
|
|
|
|
* Apache License 2.0:
|
2012-02-13 23:11:30 +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
|
2012-02-13 23:11:30 +00:00
|
|
|
*
|
2015-09-04 12:21:07 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-02-13 23:11:30 +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.
|
2012-02-13 23:11:30 +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.
|
|
|
|
*
|
|
|
|
* **********
|
2012-02-13 23:11:30 +00:00
|
|
|
*/
|
|
|
|
|
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
|
2012-02-13 23:11:30 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_ASN1_WRITE_C)
|
2012-02-13 23:11:30 +00:00
|
|
|
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/asn1write.h"
|
2012-02-13 23:11:30 +00:00
|
|
|
|
2015-02-06 13:43:58 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_PLATFORM_C)
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/platform.h"
|
2013-09-09 09:26:00 +00:00
|
|
|
#else
|
|
|
|
#include <stdlib.h>
|
2015-05-26 14:04:06 +00:00
|
|
|
#define mbedtls_calloc calloc
|
2015-04-08 10:49:31 +00:00
|
|
|
#define mbedtls_free free
|
2013-09-09 09:26:00 +00:00
|
|
|
#endif
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_asn1_write_len( unsigned char **p, unsigned char *start, size_t len )
|
2012-02-13 23:11:30 +00:00
|
|
|
{
|
|
|
|
if( len < 0x80 )
|
|
|
|
{
|
|
|
|
if( *p - start < 1 )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
|
2012-02-13 23:11:30 +00:00
|
|
|
|
2013-10-11 16:58:55 +00:00
|
|
|
*--(*p) = (unsigned char) len;
|
2012-02-13 23:11:30 +00:00
|
|
|
return( 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( len <= 0xFF )
|
|
|
|
{
|
|
|
|
if( *p - start < 2 )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
|
2012-02-13 23:11:30 +00:00
|
|
|
|
2013-10-11 16:58:55 +00:00
|
|
|
*--(*p) = (unsigned char) len;
|
2012-02-13 23:11:30 +00:00
|
|
|
*--(*p) = 0x81;
|
|
|
|
return( 2 );
|
|
|
|
}
|
|
|
|
|
2016-07-14 10:39:56 +00:00
|
|
|
if( len <= 0xFFFF )
|
|
|
|
{
|
|
|
|
if( *p - start < 3 )
|
|
|
|
return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
|
2012-02-13 23:11:30 +00:00
|
|
|
|
2016-07-14 10:39:56 +00:00
|
|
|
*--(*p) = ( len ) & 0xFF;
|
|
|
|
*--(*p) = ( len >> 8 ) & 0xFF;
|
|
|
|
*--(*p) = 0x82;
|
|
|
|
return( 3 );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( len <= 0xFFFFFF )
|
|
|
|
{
|
|
|
|
if( *p - start < 4 )
|
|
|
|
return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
|
|
|
|
|
|
|
|
*--(*p) = ( len ) & 0xFF;
|
|
|
|
*--(*p) = ( len >> 8 ) & 0xFF;
|
|
|
|
*--(*p) = ( len >> 16 ) & 0xFF;
|
|
|
|
*--(*p) = 0x83;
|
|
|
|
return( 4 );
|
|
|
|
}
|
|
|
|
|
2018-05-23 15:55:16 +00:00
|
|
|
#if SIZE_MAX > 0xFFFFFFFF
|
2016-07-14 10:39:56 +00:00
|
|
|
if( len <= 0xFFFFFFFF )
|
2018-05-23 15:55:16 +00:00
|
|
|
#endif
|
2016-07-14 10:39:56 +00:00
|
|
|
{
|
|
|
|
if( *p - start < 5 )
|
|
|
|
return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
|
|
|
|
|
|
|
|
*--(*p) = ( len ) & 0xFF;
|
|
|
|
*--(*p) = ( len >> 8 ) & 0xFF;
|
|
|
|
*--(*p) = ( len >> 16 ) & 0xFF;
|
|
|
|
*--(*p) = ( len >> 24 ) & 0xFF;
|
|
|
|
*--(*p) = 0x84;
|
|
|
|
return( 5 );
|
|
|
|
}
|
2012-02-13 23:11:30 +00:00
|
|
|
|
2018-05-23 15:55:16 +00:00
|
|
|
#if SIZE_MAX > 0xFFFFFFFF
|
2016-07-14 10:39:56 +00:00
|
|
|
return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
|
2018-05-23 15:55:16 +00:00
|
|
|
#endif
|
2012-02-13 23:11:30 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_asn1_write_tag( unsigned char **p, unsigned char *start, unsigned char tag )
|
2012-02-13 23:11:30 +00:00
|
|
|
{
|
|
|
|
if( *p - start < 1 )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
|
2012-02-13 23:11:30 +00:00
|
|
|
|
|
|
|
*--(*p) = tag;
|
|
|
|
|
|
|
|
return( 1 );
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
|
2013-08-26 15:56:37 +00:00
|
|
|
const unsigned char *buf, size_t size )
|
|
|
|
{
|
|
|
|
size_t len = 0;
|
|
|
|
|
2015-10-21 10:07:47 +00:00
|
|
|
if( *p < start || (size_t)( *p - start ) < size )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
|
2013-08-26 15:56:37 +00:00
|
|
|
|
|
|
|
len = size;
|
|
|
|
(*p) -= len;
|
|
|
|
memcpy( *p, buf, len );
|
|
|
|
|
2013-10-11 16:58:55 +00:00
|
|
|
return( (int) len );
|
2013-08-26 15:56:37 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_BIGNUM_C)
|
|
|
|
int mbedtls_asn1_write_mpi( unsigned char **p, unsigned char *start, const mbedtls_mpi *X )
|
2012-02-13 23:11:30 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t len = 0;
|
|
|
|
|
|
|
|
// Write the MPI
|
|
|
|
//
|
2015-04-08 10:49:31 +00:00
|
|
|
len = mbedtls_mpi_size( X );
|
2012-02-13 23:11:30 +00:00
|
|
|
|
2015-10-21 10:07:47 +00:00
|
|
|
if( *p < start || (size_t)( *p - start ) < len )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
|
2012-02-13 23:11:30 +00:00
|
|
|
|
|
|
|
(*p) -= len;
|
2015-04-08 10:49:31 +00:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( X, *p, len ) );
|
2012-02-13 23:11:30 +00:00
|
|
|
|
|
|
|
// DER format assumes 2s complement for numbers, so the leftmost bit
|
|
|
|
// should be 0 for positive numbers and 1 for negative numbers.
|
|
|
|
//
|
2014-06-17 14:39:18 +00:00
|
|
|
if( X->s ==1 && **p & 0x80 )
|
2012-02-13 23:11:30 +00:00
|
|
|
{
|
|
|
|
if( *p - start < 1 )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
|
2012-02-13 23:11:30 +00:00
|
|
|
|
|
|
|
*--(*p) = 0x00;
|
|
|
|
len += 1;
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00: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_INTEGER ) );
|
2012-02-13 23:11:30 +00:00
|
|
|
|
2014-04-17 10:42:41 +00:00
|
|
|
ret = (int) len;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
return( ret );
|
2012-02-13 23:11:30 +00:00
|
|
|
}
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_BIGNUM_C */
|
2013-04-18 20:46:23 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_asn1_write_null( unsigned char **p, unsigned char *start )
|
2012-02-13 23:11:30 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t len = 0;
|
|
|
|
|
|
|
|
// Write NULL
|
|
|
|
//
|
2015-04-08 10:49:31 +00:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, 0) );
|
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_NULL ) );
|
2012-02-13 23:11:30 +00:00
|
|
|
|
2013-10-11 16:58:55 +00:00
|
|
|
return( (int) len );
|
2012-02-13 23:11:30 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_asn1_write_oid( unsigned char **p, unsigned char *start,
|
2013-09-09 10:02:36 +00:00
|
|
|
const char *oid, size_t oid_len )
|
2012-02-13 23:11:30 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t len = 0;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
|
2013-09-09 10:02:36 +00:00
|
|
|
(const unsigned char *) oid, oid_len ) );
|
2015-04-08 10:49:31 +00: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_OID ) );
|
2012-02-13 23:11:30 +00:00
|
|
|
|
2013-10-11 16:58:55 +00:00
|
|
|
return( (int) len );
|
2012-02-13 23:11:30 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start,
|
2013-09-12 00:17:54 +00:00
|
|
|
const char *oid, size_t oid_len,
|
|
|
|
size_t par_len )
|
2012-02-13 23:11:30 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t len = 0;
|
|
|
|
|
2013-09-12 00:17:54 +00:00
|
|
|
if( par_len == 0 )
|
2015-04-08 10:49:31 +00:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_null( p, start ) );
|
2013-09-12 00:17:54 +00:00
|
|
|
else
|
|
|
|
len += par_len;
|
2012-02-13 23:11:30 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid, oid_len ) );
|
2012-02-13 23:11:30 +00:00
|
|
|
|
2015-04-08 10:49:31 +00: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 ) );
|
2012-02-13 23:11:30 +00:00
|
|
|
|
2013-10-11 16:58:55 +00:00
|
|
|
return( (int) len );
|
2012-02-13 23:11:30 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_asn1_write_bool( unsigned char **p, unsigned char *start, int boolean )
|
2013-09-06 14:34:38 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t len = 0;
|
|
|
|
|
|
|
|
if( *p - start < 1 )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
|
2013-09-06 14:34:38 +00:00
|
|
|
|
2015-10-14 07:41:56 +00:00
|
|
|
*--(*p) = (boolean) ? 255 : 0;
|
2013-09-06 14:34:38 +00:00
|
|
|
len++;
|
|
|
|
|
2015-04-08 10:49:31 +00: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_BOOLEAN ) );
|
2013-09-06 14:34:38 +00:00
|
|
|
|
2013-10-11 16:58:55 +00:00
|
|
|
return( (int) len );
|
2013-09-06 14:34:38 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_asn1_write_int( unsigned char **p, unsigned char *start, int val )
|
2012-02-13 23:11:30 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t len = 0;
|
|
|
|
|
|
|
|
if( *p - start < 1 )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
|
2012-02-13 23:11:30 +00:00
|
|
|
|
|
|
|
len += 1;
|
|
|
|
*--(*p) = val;
|
|
|
|
|
2014-06-17 14:39:18 +00:00
|
|
|
if( val > 0 && **p & 0x80 )
|
2012-02-13 23:11:30 +00:00
|
|
|
{
|
|
|
|
if( *p - start < 1 )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
|
2012-02-13 23:11:30 +00:00
|
|
|
|
|
|
|
*--(*p) = 0x00;
|
|
|
|
len += 1;
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00: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_INTEGER ) );
|
2012-02-13 23:11:30 +00:00
|
|
|
|
2013-10-11 16:58:55 +00:00
|
|
|
return( (int) len );
|
2012-02-13 23:11:30 +00:00
|
|
|
}
|
|
|
|
|
2018-09-19 07:10:37 +00:00
|
|
|
int mbedtls_asn1_write_tagged_string( unsigned char **p, unsigned char *start, int tag,
|
2018-05-17 10:46:13 +00:00
|
|
|
const char *text, size_t text_len )
|
2012-02-13 23:11:30 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t len = 0;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
|
2018-05-17 10:46:13 +00:00
|
|
|
(const unsigned char *) text, text_len ) );
|
2012-02-13 23:11:30 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
|
2018-05-17 10:46:13 +00:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, tag ) );
|
2012-02-13 23:11:30 +00:00
|
|
|
|
2013-10-11 16:58:55 +00:00
|
|
|
return( (int) len );
|
2012-02-13 23:11:30 +00:00
|
|
|
}
|
2013-08-25 12:46:39 +00:00
|
|
|
|
2018-05-17 10:46:13 +00:00
|
|
|
int mbedtls_asn1_write_utf8_string( unsigned char **p, unsigned char *start,
|
|
|
|
const char *text, size_t text_len )
|
2012-02-16 10:26:57 +00:00
|
|
|
{
|
2018-09-19 07:10:37 +00:00
|
|
|
return( mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_UTF8_STRING, text, text_len) );
|
2018-05-17 10:46:13 +00:00
|
|
|
}
|
2012-02-16 10:26:57 +00:00
|
|
|
|
2018-05-17 10:46:13 +00:00
|
|
|
int mbedtls_asn1_write_printable_string( unsigned char **p, unsigned char *start,
|
|
|
|
const char *text, size_t text_len )
|
|
|
|
{
|
2018-09-19 07:10:37 +00:00
|
|
|
return( mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_PRINTABLE_STRING, text, text_len) );
|
2018-05-17 10:46:13 +00:00
|
|
|
}
|
2012-02-16 10:26:57 +00:00
|
|
|
|
2018-05-17 10:46:13 +00:00
|
|
|
int mbedtls_asn1_write_ia5_string( unsigned char **p, unsigned char *start,
|
|
|
|
const char *text, size_t text_len )
|
|
|
|
{
|
2018-09-19 07:10:37 +00:00
|
|
|
return( mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_IA5_STRING, text, text_len) );
|
2012-02-16 10:26:57 +00:00
|
|
|
}
|
2013-08-25 12:46:39 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_asn1_write_bitstring( unsigned char **p, unsigned char *start,
|
2013-08-25 12:46:39 +00:00
|
|
|
const unsigned char *buf, size_t bits )
|
|
|
|
{
|
|
|
|
int ret;
|
2018-09-26 09:48:24 +00:00
|
|
|
size_t len = 0;
|
|
|
|
size_t unused_bits, byte_len;
|
2013-08-25 12:46:39 +00:00
|
|
|
|
2018-09-26 09:48:24 +00:00
|
|
|
byte_len = ( bits + 7 ) / 8;
|
|
|
|
unused_bits = ( byte_len * 8 ) - bits;
|
2013-08-25 12:46:39 +00:00
|
|
|
|
2018-09-26 09:48:24 +00:00
|
|
|
if( *p < start || (size_t)( *p - start ) < byte_len + 1 )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
|
2013-08-25 12:46:39 +00:00
|
|
|
|
2018-09-26 09:48:24 +00:00
|
|
|
len = byte_len + 1;
|
2013-08-25 12:46:39 +00:00
|
|
|
|
2018-09-26 09:48:24 +00:00
|
|
|
/* Write the bitstring. Ensure the unused bits are zeroed */
|
|
|
|
if( byte_len > 0 )
|
|
|
|
{
|
|
|
|
byte_len--;
|
|
|
|
*--( *p ) = buf[byte_len] & ~( ( 0x1 << unused_bits ) - 1 );
|
|
|
|
( *p ) -= byte_len;
|
|
|
|
memcpy( *p, buf, byte_len );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write unused bits */
|
|
|
|
*--( *p ) = (unsigned char)unused_bits;
|
2013-08-25 12:46:39 +00:00
|
|
|
|
2015-04-08 10:49:31 +00: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-08-25 12:46:39 +00:00
|
|
|
|
2013-10-11 16:58:55 +00:00
|
|
|
return( (int) len );
|
2013-08-25 12:46:39 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_asn1_write_octet_string( unsigned char **p, unsigned char *start,
|
2013-08-25 12:46:39 +00:00
|
|
|
const unsigned char *buf, size_t size )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t len = 0;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, buf, size ) );
|
2013-08-25 12:46:39 +00:00
|
|
|
|
2015-04-08 10:49:31 +00: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_OCTET_STRING ) );
|
2013-08-25 12:46:39 +00:00
|
|
|
|
2013-10-11 16:58:55 +00:00
|
|
|
return( (int) len );
|
2013-08-25 12:46:39 +00:00
|
|
|
}
|
2013-09-09 09:26:00 +00:00
|
|
|
|
2018-10-12 09:42:13 +00:00
|
|
|
|
|
|
|
/* This is a copy of the ASN.1 parsing function mbedtls_asn1_find_named_data(),
|
|
|
|
* which is replicated to avoid a dependency ASN1_WRITE_C on ASN1_PARSE_C. */
|
|
|
|
static mbedtls_asn1_named_data *asn1_find_named_data(
|
|
|
|
mbedtls_asn1_named_data *list,
|
|
|
|
const char *oid, size_t len )
|
|
|
|
{
|
|
|
|
while( list != NULL )
|
|
|
|
{
|
|
|
|
if( list->oid.len == len &&
|
|
|
|
memcmp( list->oid.p, oid, len ) == 0 )
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
list = list->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return( list );
|
|
|
|
}
|
|
|
|
|
|
|
|
mbedtls_asn1_named_data *mbedtls_asn1_store_named_data(
|
|
|
|
mbedtls_asn1_named_data **head,
|
2013-09-09 09:26:00 +00:00
|
|
|
const char *oid, size_t oid_len,
|
|
|
|
const unsigned char *val,
|
|
|
|
size_t val_len )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_asn1_named_data *cur;
|
2013-09-09 09:26:00 +00:00
|
|
|
|
2018-10-12 09:42:13 +00:00
|
|
|
if( ( cur = asn1_find_named_data( *head, oid, oid_len ) ) == NULL )
|
2013-09-09 09:26:00 +00:00
|
|
|
{
|
|
|
|
// Add new entry if not present yet based on OID
|
|
|
|
//
|
2016-05-19 23:19:09 +00:00
|
|
|
cur = (mbedtls_asn1_named_data*)mbedtls_calloc( 1,
|
|
|
|
sizeof(mbedtls_asn1_named_data) );
|
|
|
|
if( cur == NULL )
|
2013-09-09 09:26:00 +00:00
|
|
|
return( NULL );
|
|
|
|
|
|
|
|
cur->oid.len = oid_len;
|
2015-05-26 14:04:06 +00:00
|
|
|
cur->oid.p = mbedtls_calloc( 1, oid_len );
|
2013-09-09 09:26:00 +00:00
|
|
|
if( cur->oid.p == NULL )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_free( cur );
|
2013-09-09 09:26:00 +00:00
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
|
2014-11-12 21:27:42 +00:00
|
|
|
memcpy( cur->oid.p, oid, oid_len );
|
|
|
|
|
2013-09-09 09:26:00 +00:00
|
|
|
cur->val.len = val_len;
|
2015-05-26 14:04:06 +00:00
|
|
|
cur->val.p = mbedtls_calloc( 1, val_len );
|
2013-09-09 09:26:00 +00:00
|
|
|
if( cur->val.p == NULL )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_free( cur->oid.p );
|
|
|
|
mbedtls_free( cur );
|
2013-09-09 09:26:00 +00:00
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
cur->next = *head;
|
|
|
|
*head = cur;
|
|
|
|
}
|
|
|
|
else if( cur->val.len < val_len )
|
|
|
|
{
|
2015-12-10 09:50:51 +00:00
|
|
|
/*
|
|
|
|
* Enlarge existing value buffer if needed
|
|
|
|
* Preserve old data until the allocation succeeded, to leave list in
|
|
|
|
* a consistent state in case allocation fails.
|
|
|
|
*/
|
|
|
|
void *p = mbedtls_calloc( 1, val_len );
|
|
|
|
if( p == NULL )
|
|
|
|
return( NULL );
|
2013-09-09 09:26:00 +00:00
|
|
|
|
2015-12-10 09:50:51 +00:00
|
|
|
mbedtls_free( cur->val.p );
|
|
|
|
cur->val.p = p;
|
2013-09-09 09:26:00 +00:00
|
|
|
cur->val.len = val_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( val != NULL )
|
|
|
|
memcpy( cur->val.p, val, val_len );
|
|
|
|
|
|
|
|
return( cur );
|
|
|
|
}
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_ASN1_WRITE_C */
|