2012-02-13 23:11:30 +00:00
|
|
|
/*
|
|
|
|
* ASN.1 buffer writing functionality
|
|
|
|
*
|
2015-07-27 09:11:48 +00:00
|
|
|
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
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
|
|
|
*
|
2015-09-04 12:21:07 +00:00
|
|
|
* This file is part of mbed TLS (https://tls.mbed.org)
|
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;
|
|
|
|
size_t len = 0, size;
|
|
|
|
|
|
|
|
size = ( bits / 8 ) + ( ( bits % 8 ) ? 1 : 0 );
|
|
|
|
|
|
|
|
// Calculate byte length
|
|
|
|
//
|
2015-10-21 10:07:47 +00:00
|
|
|
if( *p < start || (size_t)( *p - start ) < size + 1 )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
|
2013-08-25 12:46:39 +00:00
|
|
|
|
|
|
|
len = size + 1;
|
|
|
|
(*p) -= size;
|
|
|
|
memcpy( *p, buf, size );
|
|
|
|
|
|
|
|
// Write unused bits
|
|
|
|
//
|
2013-10-11 16:58:55 +00:00
|
|
|
*--(*p) = (unsigned char) (size * 8 - 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 */
|