2012-02-14 00:11:30 +01:00
|
|
|
/*
|
|
|
|
* ASN.1 buffer writing functionality
|
|
|
|
*
|
2015-07-27 11:11:48 +02:00
|
|
|
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
2015-09-04 14:21:07 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2012-02-14 00:11:30 +01: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
|
2012-02-14 00:11:30 +01:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-02-14 00:11:30 +01: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.
|
2012-02-14 00:11:30 +01:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* This file is part of mbed TLS (https://tls.mbed.org)
|
2012-02-14 00:11:30 +01: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
|
2012-02-14 00:11:30 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_ASN1_WRITE_C)
|
2012-02-14 00:11:30 +01:00
|
|
|
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/asn1write.h"
|
2012-02-14 00:11:30 +01:00
|
|
|
|
2015-02-06 14:43:58 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
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-09 11:26:00 +02:00
|
|
|
#else
|
|
|
|
#include <stdlib.h>
|
2015-05-26 16:04:06 +02:00
|
|
|
#define mbedtls_calloc calloc
|
2015-04-08 12:49:31 +02:00
|
|
|
#define mbedtls_free free
|
2013-09-09 11:26:00 +02:00
|
|
|
#endif
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_asn1_write_len( unsigned char **p, unsigned char *start, size_t len )
|
2012-02-14 00:11:30 +01:00
|
|
|
{
|
|
|
|
if( len < 0x80 )
|
|
|
|
{
|
|
|
|
if( *p - start < 1 )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
|
2012-02-14 00:11:30 +01:00
|
|
|
|
2013-10-11 18:58:55 +02:00
|
|
|
*--(*p) = (unsigned char) len;
|
2012-02-14 00:11:30 +01:00
|
|
|
return( 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( len <= 0xFF )
|
|
|
|
{
|
|
|
|
if( *p - start < 2 )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
|
2012-02-14 00:11:30 +01:00
|
|
|
|
2013-10-11 18:58:55 +02:00
|
|
|
*--(*p) = (unsigned char) len;
|
2012-02-14 00:11:30 +01:00
|
|
|
*--(*p) = 0x81;
|
|
|
|
return( 2 );
|
|
|
|
}
|
|
|
|
|
2016-07-14 12:39:56 +02:00
|
|
|
if( len <= 0xFFFF )
|
|
|
|
{
|
|
|
|
if( *p - start < 3 )
|
|
|
|
return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
|
2012-02-14 00:11:30 +01:00
|
|
|
|
2016-07-14 12:39:56 +02: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 17:55:16 +02:00
|
|
|
#if SIZE_MAX > 0xFFFFFFFF
|
2016-07-14 12:39:56 +02:00
|
|
|
if( len <= 0xFFFFFFFF )
|
2018-05-23 17:55:16 +02:00
|
|
|
#endif
|
2016-07-14 12:39:56 +02: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-14 00:11:30 +01:00
|
|
|
|
2018-05-23 17:55:16 +02:00
|
|
|
#if SIZE_MAX > 0xFFFFFFFF
|
2016-07-14 12:39:56 +02:00
|
|
|
return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
|
2018-05-23 17:55:16 +02:00
|
|
|
#endif
|
2012-02-14 00:11:30 +01:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_asn1_write_tag( unsigned char **p, unsigned char *start, unsigned char tag )
|
2012-02-14 00:11:30 +01:00
|
|
|
{
|
|
|
|
if( *p - start < 1 )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
|
2012-02-14 00:11:30 +01:00
|
|
|
|
|
|
|
*--(*p) = tag;
|
|
|
|
|
|
|
|
return( 1 );
|
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
|
2013-08-26 17:56:37 +02:00
|
|
|
const unsigned char *buf, size_t size )
|
|
|
|
{
|
|
|
|
size_t len = 0;
|
|
|
|
|
2015-10-21 12:07:47 +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-08-26 17:56:37 +02:00
|
|
|
|
|
|
|
len = size;
|
|
|
|
(*p) -= len;
|
|
|
|
memcpy( *p, buf, len );
|
|
|
|
|
2013-10-11 18:58:55 +02:00
|
|
|
return( (int) len );
|
2013-08-26 17:56:37 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_BIGNUM_C)
|
|
|
|
int mbedtls_asn1_write_mpi( unsigned char **p, unsigned char *start, const mbedtls_mpi *X )
|
2012-02-14 00:11:30 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t len = 0;
|
|
|
|
|
|
|
|
// Write the MPI
|
|
|
|
//
|
2015-04-08 12:49:31 +02:00
|
|
|
len = mbedtls_mpi_size( X );
|
2012-02-14 00:11:30 +01:00
|
|
|
|
2015-10-21 12:07:47 +02:00
|
|
|
if( *p < start || (size_t)( *p - start ) < len )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
|
2012-02-14 00:11:30 +01:00
|
|
|
|
|
|
|
(*p) -= len;
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( X, *p, len ) );
|
2012-02-14 00:11:30 +01: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 16:39:18 +02:00
|
|
|
if( X->s ==1 && **p & 0x80 )
|
2012-02-14 00:11:30 +01:00
|
|
|
{
|
|
|
|
if( *p - start < 1 )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
|
2012-02-14 00:11:30 +01:00
|
|
|
|
|
|
|
*--(*p) = 0x00;
|
|
|
|
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_INTEGER ) );
|
2012-02-14 00:11:30 +01:00
|
|
|
|
2014-04-17 12:42:41 +02:00
|
|
|
ret = (int) len;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
return( ret );
|
2012-02-14 00:11:30 +01:00
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_BIGNUM_C */
|
2013-04-18 22:46:23 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_asn1_write_null( unsigned char **p, unsigned char *start )
|
2012-02-14 00:11:30 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t len = 0;
|
|
|
|
|
|
|
|
// Write NULL
|
|
|
|
//
|
2015-04-08 12:49:31 +02: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-14 00:11:30 +01:00
|
|
|
|
2013-10-11 18:58:55 +02:00
|
|
|
return( (int) len );
|
2012-02-14 00:11:30 +01:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_asn1_write_oid( unsigned char **p, unsigned char *start,
|
2013-09-09 12:02:36 +02:00
|
|
|
const char *oid, size_t oid_len )
|
2012-02-14 00:11:30 +01: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,
|
2013-09-09 12:02:36 +02:00
|
|
|
(const unsigned char *) oid, oid_len ) );
|
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_OID ) );
|
2012-02-14 00:11:30 +01:00
|
|
|
|
2013-10-11 18:58:55 +02:00
|
|
|
return( (int) len );
|
2012-02-14 00:11:30 +01:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start,
|
2013-09-12 02:17:54 +02:00
|
|
|
const char *oid, size_t oid_len,
|
|
|
|
size_t par_len )
|
2012-02-14 00:11:30 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t len = 0;
|
|
|
|
|
2013-09-12 02:17:54 +02:00
|
|
|
if( par_len == 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_null( p, start ) );
|
2013-09-12 02:17:54 +02:00
|
|
|
else
|
|
|
|
len += par_len;
|
2012-02-14 00:11:30 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid, oid_len ) );
|
2012-02-14 00:11:30 +01: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 ) );
|
2012-02-14 00:11:30 +01:00
|
|
|
|
2013-10-11 18:58:55 +02:00
|
|
|
return( (int) len );
|
2012-02-14 00:11:30 +01:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_asn1_write_bool( unsigned char **p, unsigned char *start, int boolean )
|
2013-09-06 16:34:38 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t len = 0;
|
|
|
|
|
|
|
|
if( *p - start < 1 )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
|
2013-09-06 16:34:38 +02:00
|
|
|
|
2015-10-14 09:41:56 +02:00
|
|
|
*--(*p) = (boolean) ? 255 : 0;
|
2013-09-06 16:34:38 +02:00
|
|
|
len++;
|
|
|
|
|
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_BOOLEAN ) );
|
2013-09-06 16:34:38 +02:00
|
|
|
|
2013-10-11 18:58:55 +02:00
|
|
|
return( (int) len );
|
2013-09-06 16:34:38 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_asn1_write_int( unsigned char **p, unsigned char *start, int val )
|
2012-02-14 00:11:30 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t len = 0;
|
|
|
|
|
|
|
|
if( *p - start < 1 )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
|
2012-02-14 00:11:30 +01:00
|
|
|
|
|
|
|
len += 1;
|
|
|
|
*--(*p) = val;
|
|
|
|
|
2014-06-17 16:39:18 +02:00
|
|
|
if( val > 0 && **p & 0x80 )
|
2012-02-14 00:11:30 +01:00
|
|
|
{
|
|
|
|
if( *p - start < 1 )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
|
2012-02-14 00:11:30 +01:00
|
|
|
|
|
|
|
*--(*p) = 0x00;
|
|
|
|
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_INTEGER ) );
|
2012-02-14 00:11:30 +01:00
|
|
|
|
2013-10-11 18:58:55 +02:00
|
|
|
return( (int) len );
|
2012-02-14 00:11:30 +01:00
|
|
|
}
|
|
|
|
|
2018-09-19 09:10:37 +02:00
|
|
|
int mbedtls_asn1_write_tagged_string( unsigned char **p, unsigned char *start, int tag,
|
2018-05-17 12:46:13 +02:00
|
|
|
const char *text, size_t text_len )
|
2012-02-14 00:11:30 +01: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,
|
2018-05-17 12:46:13 +02:00
|
|
|
(const unsigned char *) text, text_len ) );
|
2012-02-14 00:11:30 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
|
2018-05-17 12:46:13 +02:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, tag ) );
|
2012-02-14 00:11:30 +01:00
|
|
|
|
2013-10-11 18:58:55 +02:00
|
|
|
return( (int) len );
|
2012-02-14 00:11:30 +01:00
|
|
|
}
|
2013-08-25 14:46:39 +02:00
|
|
|
|
2018-05-17 12:46:13 +02:00
|
|
|
int mbedtls_asn1_write_utf8_string( unsigned char **p, unsigned char *start,
|
|
|
|
const char *text, size_t text_len )
|
2012-02-16 11:26:57 +01:00
|
|
|
{
|
2018-09-19 09:10:37 +02:00
|
|
|
return( mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_UTF8_STRING, text, text_len) );
|
2018-05-17 12:46:13 +02:00
|
|
|
}
|
2012-02-16 11:26:57 +01:00
|
|
|
|
2018-05-17 12:46:13 +02:00
|
|
|
int mbedtls_asn1_write_printable_string( unsigned char **p, unsigned char *start,
|
|
|
|
const char *text, size_t text_len )
|
|
|
|
{
|
2018-09-19 09:10:37 +02:00
|
|
|
return( mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_PRINTABLE_STRING, text, text_len) );
|
2018-05-17 12:46:13 +02:00
|
|
|
}
|
2012-02-16 11:26:57 +01:00
|
|
|
|
2018-05-17 12:46:13 +02:00
|
|
|
int mbedtls_asn1_write_ia5_string( unsigned char **p, unsigned char *start,
|
|
|
|
const char *text, size_t text_len )
|
|
|
|
{
|
2018-09-19 09:10:37 +02:00
|
|
|
return( mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_IA5_STRING, text, text_len) );
|
2012-02-16 11:26:57 +01:00
|
|
|
}
|
2013-08-25 14:46:39 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_asn1_write_bitstring( unsigned char **p, unsigned char *start,
|
2013-08-25 14:46:39 +02: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 12:07:47 +02:00
|
|
|
if( *p < start || (size_t)( *p - start ) < size + 1 )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
|
2013-08-25 14:46:39 +02:00
|
|
|
|
|
|
|
len = size + 1;
|
|
|
|
(*p) -= size;
|
|
|
|
memcpy( *p, buf, size );
|
|
|
|
|
|
|
|
// Write unused bits
|
|
|
|
//
|
2013-10-11 18:58:55 +02:00
|
|
|
*--(*p) = (unsigned char) (size * 8 - bits);
|
2013-08-25 14:46:39 +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_BIT_STRING ) );
|
2013-08-25 14:46:39 +02:00
|
|
|
|
2013-10-11 18:58:55 +02:00
|
|
|
return( (int) len );
|
2013-08-25 14:46:39 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_asn1_write_octet_string( unsigned char **p, unsigned char *start,
|
2013-08-25 14:46:39 +02:00
|
|
|
const unsigned char *buf, size_t size )
|
|
|
|
{
|
|
|
|
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, buf, size ) );
|
2013-08-25 14:46:39 +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_OCTET_STRING ) );
|
2013-08-25 14:46:39 +02:00
|
|
|
|
2013-10-11 18:58:55 +02:00
|
|
|
return( (int) len );
|
2013-08-25 14:46:39 +02:00
|
|
|
}
|
2013-09-09 11:26:00 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_asn1_named_data *mbedtls_asn1_store_named_data( mbedtls_asn1_named_data **head,
|
2013-09-09 11:26:00 +02:00
|
|
|
const char *oid, size_t oid_len,
|
|
|
|
const unsigned char *val,
|
|
|
|
size_t val_len )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_asn1_named_data *cur;
|
2013-09-09 11:26:00 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( cur = mbedtls_asn1_find_named_data( *head, oid, oid_len ) ) == NULL )
|
2013-09-09 11:26:00 +02:00
|
|
|
{
|
|
|
|
// Add new entry if not present yet based on OID
|
|
|
|
//
|
2016-05-20 01:19:09 +02:00
|
|
|
cur = (mbedtls_asn1_named_data*)mbedtls_calloc( 1,
|
|
|
|
sizeof(mbedtls_asn1_named_data) );
|
|
|
|
if( cur == NULL )
|
2013-09-09 11:26:00 +02:00
|
|
|
return( NULL );
|
|
|
|
|
|
|
|
cur->oid.len = oid_len;
|
2015-05-26 16:04:06 +02:00
|
|
|
cur->oid.p = mbedtls_calloc( 1, oid_len );
|
2013-09-09 11:26:00 +02:00
|
|
|
if( cur->oid.p == NULL )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_free( cur );
|
2013-09-09 11:26:00 +02:00
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
|
2014-11-12 22:27:42 +01:00
|
|
|
memcpy( cur->oid.p, oid, oid_len );
|
|
|
|
|
2013-09-09 11:26:00 +02:00
|
|
|
cur->val.len = val_len;
|
2015-05-26 16:04:06 +02:00
|
|
|
cur->val.p = mbedtls_calloc( 1, val_len );
|
2013-09-09 11:26:00 +02:00
|
|
|
if( cur->val.p == NULL )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_free( cur->oid.p );
|
|
|
|
mbedtls_free( cur );
|
2013-09-09 11:26:00 +02:00
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
cur->next = *head;
|
|
|
|
*head = cur;
|
|
|
|
}
|
|
|
|
else if( cur->val.len < val_len )
|
|
|
|
{
|
2015-12-10 10:50:51 +01: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 11:26:00 +02:00
|
|
|
|
2015-12-10 10:50:51 +01:00
|
|
|
mbedtls_free( cur->val.p );
|
|
|
|
cur->val.p = p;
|
2013-09-09 11:26:00 +02:00
|
|
|
cur->val.len = val_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( val != NULL )
|
|
|
|
memcpy( cur->val.p, val, val_len );
|
|
|
|
|
|
|
|
return( cur );
|
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_ASN1_WRITE_C */
|