2012-02-13 23:11:30 +00:00
|
|
|
/*
|
|
|
|
* ASN.1 buffer writing functionality
|
|
|
|
*
|
2020-08-07 11:07:28 +00:00
|
|
|
* Copyright The Mbed TLS Contributors
|
2015-09-04 12:21:07 +00:00
|
|
|
* SPDX-License-Identifier: Apache-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
|
|
|
*/
|
|
|
|
|
2020-06-02 23:43:33 +00:00
|
|
|
#include "common.h"
|
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"
|
2019-11-22 13:21:35 +00:00
|
|
|
#include "mbedtls/error.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
|
|
|
{
|
2019-11-22 13:21:35 +00:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2012-02-13 23:11:30 +00:00
|
|
|
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
|
|
|
{
|
2019-11-22 13:21:35 +00:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2012-02-13 23:11:30 +00:00
|
|
|
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
|
|
|
{
|
2019-11-22 13:21:35 +00:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2012-02-13 23:11:30 +00:00
|
|
|
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
|
|
|
{
|
2019-11-22 13:21:35 +00:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2012-02-13 23:11:30 +00:00
|
|
|
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
|
|
|
{
|
2019-11-22 13:21:35 +00:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2013-09-06 14:34:38 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-10-29 13:58:10 +00:00
|
|
|
static int asn1_write_tagged_int( unsigned char **p, unsigned char *start, int val, int tag )
|
2012-02-13 23:11:30 +00:00
|
|
|
{
|
2019-11-22 13:21:35 +00:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2012-02-13 23:11:30 +00:00
|
|
|
size_t len = 0;
|
|
|
|
|
2019-03-01 17:15:18 +00:00
|
|
|
do
|
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 );
|
2019-03-01 17:15:18 +00:00
|
|
|
len += 1;
|
|
|
|
*--(*p) = val & 0xff;
|
|
|
|
val >>= 8;
|
|
|
|
}
|
|
|
|
while( val > 0 );
|
2012-02-13 23:11:30 +00:00
|
|
|
|
2019-03-01 17:15:18 +00:00
|
|
|
if( **p & 0x80 )
|
|
|
|
{
|
|
|
|
if( *p - start < 1 )
|
|
|
|
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 ) );
|
2019-10-29 13:58:10 +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
|
|
|
}
|
|
|
|
|
2019-10-29 13:58:10 +00:00
|
|
|
int mbedtls_asn1_write_int( unsigned char **p, unsigned char *start, int val )
|
|
|
|
{
|
|
|
|
return( asn1_write_tagged_int( p, start, val, MBEDTLS_ASN1_INTEGER ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
int mbedtls_asn1_write_enum( unsigned char **p, unsigned char *start, int val )
|
|
|
|
{
|
|
|
|
return( asn1_write_tagged_int( p, start, val, MBEDTLS_ASN1_ENUMERATED ) );
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2019-11-22 13:21:35 +00:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2012-02-13 23:11:30 +00:00
|
|
|
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
|
|
|
|
2018-09-26 09:48:24 +00:00
|
|
|
int mbedtls_asn1_write_named_bitstring( unsigned char **p,
|
|
|
|
unsigned char *start,
|
|
|
|
const unsigned char *buf,
|
|
|
|
size_t bits )
|
|
|
|
{
|
|
|
|
size_t unused_bits, byte_len;
|
|
|
|
const unsigned char *cur_byte;
|
|
|
|
unsigned char cur_byte_shifted;
|
|
|
|
unsigned char bit;
|
|
|
|
|
|
|
|
byte_len = ( bits + 7 ) / 8;
|
|
|
|
unused_bits = ( byte_len * 8 ) - bits;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Named bitstrings require that trailing 0s are excluded in the encoding
|
|
|
|
* of the bitstring. Trailing 0s are considered part of the 'unused' bits
|
|
|
|
* when encoding this value in the first content octet
|
|
|
|
*/
|
|
|
|
if( bits != 0 )
|
|
|
|
{
|
|
|
|
cur_byte = buf + byte_len - 1;
|
|
|
|
cur_byte_shifted = *cur_byte >> unused_bits;
|
|
|
|
|
|
|
|
for( ; ; )
|
|
|
|
{
|
|
|
|
bit = cur_byte_shifted & 0x1;
|
|
|
|
cur_byte_shifted >>= 1;
|
|
|
|
|
|
|
|
if( bit != 0 )
|
|
|
|
break;
|
|
|
|
|
|
|
|
bits--;
|
|
|
|
if( bits == 0 )
|
|
|
|
break;
|
|
|
|
|
|
|
|
if( bits % 8 == 0 )
|
|
|
|
cur_byte_shifted = *--cur_byte;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return( mbedtls_asn1_write_bitstring( p, start, buf, bits ) );
|
|
|
|
}
|
|
|
|
|
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 )
|
|
|
|
{
|
2019-11-22 13:21:35 +00:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
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 )
|
|
|
|
{
|
2019-11-22 13:21:35 +00:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2013-08-25 12:46:39 +00:00
|
|
|
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;
|
2019-03-04 14:00:06 +00:00
|
|
|
if( val_len != 0 )
|
2013-09-09 09:26:00 +00:00
|
|
|
{
|
2019-03-04 14:00:06 +00:00
|
|
|
cur->val.p = mbedtls_calloc( 1, val_len );
|
|
|
|
if( cur->val.p == NULL )
|
|
|
|
{
|
|
|
|
mbedtls_free( cur->oid.p );
|
|
|
|
mbedtls_free( cur );
|
|
|
|
return( NULL );
|
|
|
|
}
|
2013-09-09 09:26:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cur->next = *head;
|
|
|
|
*head = cur;
|
|
|
|
}
|
2019-03-04 14:00:06 +00:00
|
|
|
else if( val_len == 0 )
|
|
|
|
{
|
|
|
|
mbedtls_free( cur->val.p );
|
|
|
|
cur->val.p = NULL;
|
|
|
|
}
|
|
|
|
else if( cur->val.len != val_len )
|
2013-09-09 09:26:00 +00:00
|
|
|
{
|
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 */
|