mbed os with nrf51 internal bandgap enabled to read battery level

Dependents:   BLE_file_test BLE_Blink ExternalEncoder

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elessair 0:f269e3021894 1 /*
elessair 0:f269e3021894 2 * ASN.1 buffer writing functionality
elessair 0:f269e3021894 3 *
elessair 0:f269e3021894 4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
elessair 0:f269e3021894 5 * SPDX-License-Identifier: Apache-2.0
elessair 0:f269e3021894 6 *
elessair 0:f269e3021894 7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
elessair 0:f269e3021894 8 * not use this file except in compliance with the License.
elessair 0:f269e3021894 9 * You may obtain a copy of the License at
elessair 0:f269e3021894 10 *
elessair 0:f269e3021894 11 * http://www.apache.org/licenses/LICENSE-2.0
elessair 0:f269e3021894 12 *
elessair 0:f269e3021894 13 * Unless required by applicable law or agreed to in writing, software
elessair 0:f269e3021894 14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
elessair 0:f269e3021894 15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
elessair 0:f269e3021894 16 * See the License for the specific language governing permissions and
elessair 0:f269e3021894 17 * limitations under the License.
elessair 0:f269e3021894 18 *
elessair 0:f269e3021894 19 * This file is part of mbed TLS (https://tls.mbed.org)
elessair 0:f269e3021894 20 */
elessair 0:f269e3021894 21
elessair 0:f269e3021894 22 #if !defined(MBEDTLS_CONFIG_FILE)
elessair 0:f269e3021894 23 #include "mbedtls/config.h"
elessair 0:f269e3021894 24 #else
elessair 0:f269e3021894 25 #include MBEDTLS_CONFIG_FILE
elessair 0:f269e3021894 26 #endif
elessair 0:f269e3021894 27
elessair 0:f269e3021894 28 #if defined(MBEDTLS_ASN1_WRITE_C)
elessair 0:f269e3021894 29
elessair 0:f269e3021894 30 #include "mbedtls/asn1write.h"
elessair 0:f269e3021894 31
elessair 0:f269e3021894 32 #include <string.h>
elessair 0:f269e3021894 33
elessair 0:f269e3021894 34 #if defined(MBEDTLS_PLATFORM_C)
elessair 0:f269e3021894 35 #include "mbedtls/platform.h"
elessair 0:f269e3021894 36 #else
elessair 0:f269e3021894 37 #include <stdlib.h>
elessair 0:f269e3021894 38 #define mbedtls_calloc calloc
elessair 0:f269e3021894 39 #define mbedtls_free free
elessair 0:f269e3021894 40 #endif
elessair 0:f269e3021894 41
elessair 0:f269e3021894 42 int mbedtls_asn1_write_len( unsigned char **p, unsigned char *start, size_t len )
elessair 0:f269e3021894 43 {
elessair 0:f269e3021894 44 if( len < 0x80 )
elessair 0:f269e3021894 45 {
elessair 0:f269e3021894 46 if( *p - start < 1 )
elessair 0:f269e3021894 47 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
elessair 0:f269e3021894 48
elessair 0:f269e3021894 49 *--(*p) = (unsigned char) len;
elessair 0:f269e3021894 50 return( 1 );
elessair 0:f269e3021894 51 }
elessair 0:f269e3021894 52
elessair 0:f269e3021894 53 if( len <= 0xFF )
elessair 0:f269e3021894 54 {
elessair 0:f269e3021894 55 if( *p - start < 2 )
elessair 0:f269e3021894 56 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
elessair 0:f269e3021894 57
elessair 0:f269e3021894 58 *--(*p) = (unsigned char) len;
elessair 0:f269e3021894 59 *--(*p) = 0x81;
elessair 0:f269e3021894 60 return( 2 );
elessair 0:f269e3021894 61 }
elessair 0:f269e3021894 62
elessair 0:f269e3021894 63 if( len <= 0xFFFF )
elessair 0:f269e3021894 64 {
elessair 0:f269e3021894 65 if( *p - start < 3 )
elessair 0:f269e3021894 66 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
elessair 0:f269e3021894 67
elessair 0:f269e3021894 68 *--(*p) = ( len ) & 0xFF;
elessair 0:f269e3021894 69 *--(*p) = ( len >> 8 ) & 0xFF;
elessair 0:f269e3021894 70 *--(*p) = 0x82;
elessair 0:f269e3021894 71 return( 3 );
elessair 0:f269e3021894 72 }
elessair 0:f269e3021894 73
elessair 0:f269e3021894 74 if( len <= 0xFFFFFF )
elessair 0:f269e3021894 75 {
elessair 0:f269e3021894 76 if( *p - start < 4 )
elessair 0:f269e3021894 77 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
elessair 0:f269e3021894 78
elessair 0:f269e3021894 79 *--(*p) = ( len ) & 0xFF;
elessair 0:f269e3021894 80 *--(*p) = ( len >> 8 ) & 0xFF;
elessair 0:f269e3021894 81 *--(*p) = ( len >> 16 ) & 0xFF;
elessair 0:f269e3021894 82 *--(*p) = 0x83;
elessair 0:f269e3021894 83 return( 4 );
elessair 0:f269e3021894 84 }
elessair 0:f269e3021894 85
elessair 0:f269e3021894 86 if( len <= 0xFFFFFFFF )
elessair 0:f269e3021894 87 {
elessair 0:f269e3021894 88 if( *p - start < 5 )
elessair 0:f269e3021894 89 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
elessair 0:f269e3021894 90
elessair 0:f269e3021894 91 *--(*p) = ( len ) & 0xFF;
elessair 0:f269e3021894 92 *--(*p) = ( len >> 8 ) & 0xFF;
elessair 0:f269e3021894 93 *--(*p) = ( len >> 16 ) & 0xFF;
elessair 0:f269e3021894 94 *--(*p) = ( len >> 24 ) & 0xFF;
elessair 0:f269e3021894 95 *--(*p) = 0x84;
elessair 0:f269e3021894 96 return( 5 );
elessair 0:f269e3021894 97 }
elessair 0:f269e3021894 98
elessair 0:f269e3021894 99 return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
elessair 0:f269e3021894 100 }
elessair 0:f269e3021894 101
elessair 0:f269e3021894 102 int mbedtls_asn1_write_tag( unsigned char **p, unsigned char *start, unsigned char tag )
elessair 0:f269e3021894 103 {
elessair 0:f269e3021894 104 if( *p - start < 1 )
elessair 0:f269e3021894 105 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
elessair 0:f269e3021894 106
elessair 0:f269e3021894 107 *--(*p) = tag;
elessair 0:f269e3021894 108
elessair 0:f269e3021894 109 return( 1 );
elessair 0:f269e3021894 110 }
elessair 0:f269e3021894 111
elessair 0:f269e3021894 112 int mbedtls_asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
elessair 0:f269e3021894 113 const unsigned char *buf, size_t size )
elessair 0:f269e3021894 114 {
elessair 0:f269e3021894 115 size_t len = 0;
elessair 0:f269e3021894 116
elessair 0:f269e3021894 117 if( *p < start || (size_t)( *p - start ) < size )
elessair 0:f269e3021894 118 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
elessair 0:f269e3021894 119
elessair 0:f269e3021894 120 len = size;
elessair 0:f269e3021894 121 (*p) -= len;
elessair 0:f269e3021894 122 memcpy( *p, buf, len );
elessair 0:f269e3021894 123
elessair 0:f269e3021894 124 return( (int) len );
elessair 0:f269e3021894 125 }
elessair 0:f269e3021894 126
elessair 0:f269e3021894 127 #if defined(MBEDTLS_BIGNUM_C)
elessair 0:f269e3021894 128 int mbedtls_asn1_write_mpi( unsigned char **p, unsigned char *start, const mbedtls_mpi *X )
elessair 0:f269e3021894 129 {
elessair 0:f269e3021894 130 int ret;
elessair 0:f269e3021894 131 size_t len = 0;
elessair 0:f269e3021894 132
elessair 0:f269e3021894 133 // Write the MPI
elessair 0:f269e3021894 134 //
elessair 0:f269e3021894 135 len = mbedtls_mpi_size( X );
elessair 0:f269e3021894 136
elessair 0:f269e3021894 137 if( *p < start || (size_t)( *p - start ) < len )
elessair 0:f269e3021894 138 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
elessair 0:f269e3021894 139
elessair 0:f269e3021894 140 (*p) -= len;
elessair 0:f269e3021894 141 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( X, *p, len ) );
elessair 0:f269e3021894 142
elessair 0:f269e3021894 143 // DER format assumes 2s complement for numbers, so the leftmost bit
elessair 0:f269e3021894 144 // should be 0 for positive numbers and 1 for negative numbers.
elessair 0:f269e3021894 145 //
elessair 0:f269e3021894 146 if( X->s ==1 && **p & 0x80 )
elessair 0:f269e3021894 147 {
elessair 0:f269e3021894 148 if( *p - start < 1 )
elessair 0:f269e3021894 149 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
elessair 0:f269e3021894 150
elessair 0:f269e3021894 151 *--(*p) = 0x00;
elessair 0:f269e3021894 152 len += 1;
elessair 0:f269e3021894 153 }
elessair 0:f269e3021894 154
elessair 0:f269e3021894 155 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
elessair 0:f269e3021894 156 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_INTEGER ) );
elessair 0:f269e3021894 157
elessair 0:f269e3021894 158 ret = (int) len;
elessair 0:f269e3021894 159
elessair 0:f269e3021894 160 cleanup:
elessair 0:f269e3021894 161 return( ret );
elessair 0:f269e3021894 162 }
elessair 0:f269e3021894 163 #endif /* MBEDTLS_BIGNUM_C */
elessair 0:f269e3021894 164
elessair 0:f269e3021894 165 int mbedtls_asn1_write_null( unsigned char **p, unsigned char *start )
elessair 0:f269e3021894 166 {
elessair 0:f269e3021894 167 int ret;
elessair 0:f269e3021894 168 size_t len = 0;
elessair 0:f269e3021894 169
elessair 0:f269e3021894 170 // Write NULL
elessair 0:f269e3021894 171 //
elessair 0:f269e3021894 172 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, 0) );
elessair 0:f269e3021894 173 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_NULL ) );
elessair 0:f269e3021894 174
elessair 0:f269e3021894 175 return( (int) len );
elessair 0:f269e3021894 176 }
elessair 0:f269e3021894 177
elessair 0:f269e3021894 178 int mbedtls_asn1_write_oid( unsigned char **p, unsigned char *start,
elessair 0:f269e3021894 179 const char *oid, size_t oid_len )
elessair 0:f269e3021894 180 {
elessair 0:f269e3021894 181 int ret;
elessair 0:f269e3021894 182 size_t len = 0;
elessair 0:f269e3021894 183
elessair 0:f269e3021894 184 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
elessair 0:f269e3021894 185 (const unsigned char *) oid, oid_len ) );
elessair 0:f269e3021894 186 MBEDTLS_ASN1_CHK_ADD( len , mbedtls_asn1_write_len( p, start, len ) );
elessair 0:f269e3021894 187 MBEDTLS_ASN1_CHK_ADD( len , mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OID ) );
elessair 0:f269e3021894 188
elessair 0:f269e3021894 189 return( (int) len );
elessair 0:f269e3021894 190 }
elessair 0:f269e3021894 191
elessair 0:f269e3021894 192 int mbedtls_asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start,
elessair 0:f269e3021894 193 const char *oid, size_t oid_len,
elessair 0:f269e3021894 194 size_t par_len )
elessair 0:f269e3021894 195 {
elessair 0:f269e3021894 196 int ret;
elessair 0:f269e3021894 197 size_t len = 0;
elessair 0:f269e3021894 198
elessair 0:f269e3021894 199 if( par_len == 0 )
elessair 0:f269e3021894 200 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_null( p, start ) );
elessair 0:f269e3021894 201 else
elessair 0:f269e3021894 202 len += par_len;
elessair 0:f269e3021894 203
elessair 0:f269e3021894 204 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid, oid_len ) );
elessair 0:f269e3021894 205
elessair 0:f269e3021894 206 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
elessair 0:f269e3021894 207 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
elessair 0:f269e3021894 208 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
elessair 0:f269e3021894 209
elessair 0:f269e3021894 210 return( (int) len );
elessair 0:f269e3021894 211 }
elessair 0:f269e3021894 212
elessair 0:f269e3021894 213 int mbedtls_asn1_write_bool( unsigned char **p, unsigned char *start, int boolean )
elessair 0:f269e3021894 214 {
elessair 0:f269e3021894 215 int ret;
elessair 0:f269e3021894 216 size_t len = 0;
elessair 0:f269e3021894 217
elessair 0:f269e3021894 218 if( *p - start < 1 )
elessair 0:f269e3021894 219 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
elessair 0:f269e3021894 220
elessair 0:f269e3021894 221 *--(*p) = (boolean) ? 255 : 0;
elessair 0:f269e3021894 222 len++;
elessair 0:f269e3021894 223
elessair 0:f269e3021894 224 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
elessair 0:f269e3021894 225 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_BOOLEAN ) );
elessair 0:f269e3021894 226
elessair 0:f269e3021894 227 return( (int) len );
elessair 0:f269e3021894 228 }
elessair 0:f269e3021894 229
elessair 0:f269e3021894 230 int mbedtls_asn1_write_int( unsigned char **p, unsigned char *start, int val )
elessair 0:f269e3021894 231 {
elessair 0:f269e3021894 232 int ret;
elessair 0:f269e3021894 233 size_t len = 0;
elessair 0:f269e3021894 234
elessair 0:f269e3021894 235 // TODO negative values and values larger than 128
elessair 0:f269e3021894 236 // DER format assumes 2s complement for numbers, so the leftmost bit
elessair 0:f269e3021894 237 // should be 0 for positive numbers and 1 for negative numbers.
elessair 0:f269e3021894 238 //
elessair 0:f269e3021894 239 if( *p - start < 1 )
elessair 0:f269e3021894 240 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
elessair 0:f269e3021894 241
elessair 0:f269e3021894 242 len += 1;
elessair 0:f269e3021894 243 *--(*p) = val;
elessair 0:f269e3021894 244
elessair 0:f269e3021894 245 if( val > 0 && **p & 0x80 )
elessair 0:f269e3021894 246 {
elessair 0:f269e3021894 247 if( *p - start < 1 )
elessair 0:f269e3021894 248 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
elessair 0:f269e3021894 249
elessair 0:f269e3021894 250 *--(*p) = 0x00;
elessair 0:f269e3021894 251 len += 1;
elessair 0:f269e3021894 252 }
elessair 0:f269e3021894 253
elessair 0:f269e3021894 254 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
elessair 0:f269e3021894 255 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_INTEGER ) );
elessair 0:f269e3021894 256
elessair 0:f269e3021894 257 return( (int) len );
elessair 0:f269e3021894 258 }
elessair 0:f269e3021894 259
elessair 0:f269e3021894 260 int mbedtls_asn1_write_printable_string( unsigned char **p, unsigned char *start,
elessair 0:f269e3021894 261 const char *text, size_t text_len )
elessair 0:f269e3021894 262 {
elessair 0:f269e3021894 263 int ret;
elessair 0:f269e3021894 264 size_t len = 0;
elessair 0:f269e3021894 265
elessair 0:f269e3021894 266 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
elessair 0:f269e3021894 267 (const unsigned char *) text, text_len ) );
elessair 0:f269e3021894 268
elessair 0:f269e3021894 269 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
elessair 0:f269e3021894 270 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_PRINTABLE_STRING ) );
elessair 0:f269e3021894 271
elessair 0:f269e3021894 272 return( (int) len );
elessair 0:f269e3021894 273 }
elessair 0:f269e3021894 274
elessair 0:f269e3021894 275 int mbedtls_asn1_write_ia5_string( unsigned char **p, unsigned char *start,
elessair 0:f269e3021894 276 const char *text, size_t text_len )
elessair 0:f269e3021894 277 {
elessair 0:f269e3021894 278 int ret;
elessair 0:f269e3021894 279 size_t len = 0;
elessair 0:f269e3021894 280
elessair 0:f269e3021894 281 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
elessair 0:f269e3021894 282 (const unsigned char *) text, text_len ) );
elessair 0:f269e3021894 283
elessair 0:f269e3021894 284 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
elessair 0:f269e3021894 285 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_IA5_STRING ) );
elessair 0:f269e3021894 286
elessair 0:f269e3021894 287 return( (int) len );
elessair 0:f269e3021894 288 }
elessair 0:f269e3021894 289
elessair 0:f269e3021894 290 int mbedtls_asn1_write_bitstring( unsigned char **p, unsigned char *start,
elessair 0:f269e3021894 291 const unsigned char *buf, size_t bits )
elessair 0:f269e3021894 292 {
elessair 0:f269e3021894 293 int ret;
elessair 0:f269e3021894 294 size_t len = 0, size;
elessair 0:f269e3021894 295
elessair 0:f269e3021894 296 size = ( bits / 8 ) + ( ( bits % 8 ) ? 1 : 0 );
elessair 0:f269e3021894 297
elessair 0:f269e3021894 298 // Calculate byte length
elessair 0:f269e3021894 299 //
elessair 0:f269e3021894 300 if( *p < start || (size_t)( *p - start ) < size + 1 )
elessair 0:f269e3021894 301 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
elessair 0:f269e3021894 302
elessair 0:f269e3021894 303 len = size + 1;
elessair 0:f269e3021894 304 (*p) -= size;
elessair 0:f269e3021894 305 memcpy( *p, buf, size );
elessair 0:f269e3021894 306
elessair 0:f269e3021894 307 // Write unused bits
elessair 0:f269e3021894 308 //
elessair 0:f269e3021894 309 *--(*p) = (unsigned char) (size * 8 - bits);
elessair 0:f269e3021894 310
elessair 0:f269e3021894 311 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
elessair 0:f269e3021894 312 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_BIT_STRING ) );
elessair 0:f269e3021894 313
elessair 0:f269e3021894 314 return( (int) len );
elessair 0:f269e3021894 315 }
elessair 0:f269e3021894 316
elessair 0:f269e3021894 317 int mbedtls_asn1_write_octet_string( unsigned char **p, unsigned char *start,
elessair 0:f269e3021894 318 const unsigned char *buf, size_t size )
elessair 0:f269e3021894 319 {
elessair 0:f269e3021894 320 int ret;
elessair 0:f269e3021894 321 size_t len = 0;
elessair 0:f269e3021894 322
elessair 0:f269e3021894 323 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, buf, size ) );
elessair 0:f269e3021894 324
elessair 0:f269e3021894 325 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
elessair 0:f269e3021894 326 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OCTET_STRING ) );
elessair 0:f269e3021894 327
elessair 0:f269e3021894 328 return( (int) len );
elessair 0:f269e3021894 329 }
elessair 0:f269e3021894 330
elessair 0:f269e3021894 331 mbedtls_asn1_named_data *mbedtls_asn1_store_named_data( mbedtls_asn1_named_data **head,
elessair 0:f269e3021894 332 const char *oid, size_t oid_len,
elessair 0:f269e3021894 333 const unsigned char *val,
elessair 0:f269e3021894 334 size_t val_len )
elessair 0:f269e3021894 335 {
elessair 0:f269e3021894 336 mbedtls_asn1_named_data *cur;
elessair 0:f269e3021894 337
elessair 0:f269e3021894 338 if( ( cur = mbedtls_asn1_find_named_data( *head, oid, oid_len ) ) == NULL )
elessair 0:f269e3021894 339 {
elessair 0:f269e3021894 340 // Add new entry if not present yet based on OID
elessair 0:f269e3021894 341 //
elessair 0:f269e3021894 342 cur = (mbedtls_asn1_named_data*)mbedtls_calloc( 1,
elessair 0:f269e3021894 343 sizeof(mbedtls_asn1_named_data) );
elessair 0:f269e3021894 344 if( cur == NULL )
elessair 0:f269e3021894 345 return( NULL );
elessair 0:f269e3021894 346
elessair 0:f269e3021894 347 cur->oid.len = oid_len;
elessair 0:f269e3021894 348 cur->oid.p = mbedtls_calloc( 1, oid_len );
elessair 0:f269e3021894 349 if( cur->oid.p == NULL )
elessair 0:f269e3021894 350 {
elessair 0:f269e3021894 351 mbedtls_free( cur );
elessair 0:f269e3021894 352 return( NULL );
elessair 0:f269e3021894 353 }
elessair 0:f269e3021894 354
elessair 0:f269e3021894 355 memcpy( cur->oid.p, oid, oid_len );
elessair 0:f269e3021894 356
elessair 0:f269e3021894 357 cur->val.len = val_len;
elessair 0:f269e3021894 358 cur->val.p = mbedtls_calloc( 1, val_len );
elessair 0:f269e3021894 359 if( cur->val.p == NULL )
elessair 0:f269e3021894 360 {
elessair 0:f269e3021894 361 mbedtls_free( cur->oid.p );
elessair 0:f269e3021894 362 mbedtls_free( cur );
elessair 0:f269e3021894 363 return( NULL );
elessair 0:f269e3021894 364 }
elessair 0:f269e3021894 365
elessair 0:f269e3021894 366 cur->next = *head;
elessair 0:f269e3021894 367 *head = cur;
elessair 0:f269e3021894 368 }
elessair 0:f269e3021894 369 else if( cur->val.len < val_len )
elessair 0:f269e3021894 370 {
elessair 0:f269e3021894 371 /*
elessair 0:f269e3021894 372 * Enlarge existing value buffer if needed
elessair 0:f269e3021894 373 * Preserve old data until the allocation succeeded, to leave list in
elessair 0:f269e3021894 374 * a consistent state in case allocation fails.
elessair 0:f269e3021894 375 */
elessair 0:f269e3021894 376 void *p = mbedtls_calloc( 1, val_len );
elessair 0:f269e3021894 377 if( p == NULL )
elessair 0:f269e3021894 378 return( NULL );
elessair 0:f269e3021894 379
elessair 0:f269e3021894 380 mbedtls_free( cur->val.p );
elessair 0:f269e3021894 381 cur->val.p = p;
elessair 0:f269e3021894 382 cur->val.len = val_len;
elessair 0:f269e3021894 383 }
elessair 0:f269e3021894 384
elessair 0:f269e3021894 385 if( val != NULL )
elessair 0:f269e3021894 386 memcpy( cur->val.p, val, val_len );
elessair 0:f269e3021894 387
elessair 0:f269e3021894 388 return( cur );
elessair 0:f269e3021894 389 }
elessair 0:f269e3021894 390 #endif /* MBEDTLS_ASN1_WRITE_C */