Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

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