blob: 5219fcfec33e1cd6526556314d76690fe32d1f16 [file] [log] [blame]
Paul Bakkerbdb912d2012-02-13 23:11:30 +00001/*
2 * ASN.1 buffer writing functionality
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakkerbdb912d2012-02-13 23:11:30 +00005 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkerbdb912d2012-02-13 23:11:30 +00007 *
Paul Bakkerbdb912d2012-02-13 23:11:30 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020027#endif
Paul Bakkerbdb912d2012-02-13 23:11:30 +000028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if defined(MBEDTLS_ASN1_WRITE_C)
Paul Bakkerbdb912d2012-02-13 23:11:30 +000030
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/asn1write.h"
Paul Bakkerbdb912d2012-02-13 23:11:30 +000032
Rich Evans00ab4702015-02-06 13:43:58 +000033#include <string.h>
34
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/platform.h"
Paul Bakker59ba59f2013-09-09 11:26:00 +020037#else
38#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#define mbedtls_malloc malloc
40#define mbedtls_free free
Paul Bakker59ba59f2013-09-09 11:26:00 +020041#endif
42
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043int mbedtls_asn1_write_len( unsigned char **p, unsigned char *start, size_t len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +000044{
45 if( len < 0x80 )
46 {
47 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000049
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020050 *--(*p) = (unsigned char) len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000051 return( 1 );
52 }
53
54 if( len <= 0xFF )
55 {
56 if( *p - start < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000058
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020059 *--(*p) = (unsigned char) len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000060 *--(*p) = 0x81;
61 return( 2 );
62 }
63
64 if( *p - start < 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000066
67 // We assume we never have lengths larger than 65535 bytes
68 //
69 *--(*p) = len % 256;
70 *--(*p) = ( len / 256 ) % 256;
71 *--(*p) = 0x82;
72
73 return( 3 );
74}
75
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076int mbedtls_asn1_write_tag( unsigned char **p, unsigned char *start, unsigned char tag )
Paul Bakkerbdb912d2012-02-13 23:11:30 +000077{
78 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000080
81 *--(*p) = tag;
82
83 return( 1 );
84}
85
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086int mbedtls_asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
Paul Bakker9852d002013-08-26 17:56:37 +020087 const unsigned char *buf, size_t size )
88{
89 size_t len = 0;
90
91 if( *p - start < (int) size )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakker9852d002013-08-26 17:56:37 +020093
94 len = size;
95 (*p) -= len;
96 memcpy( *p, buf, len );
97
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020098 return( (int) len );
Paul Bakker9852d002013-08-26 17:56:37 +020099}
100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101#if defined(MBEDTLS_BIGNUM_C)
102int mbedtls_asn1_write_mpi( unsigned char **p, unsigned char *start, const mbedtls_mpi *X )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000103{
104 int ret;
105 size_t len = 0;
106
107 // Write the MPI
108 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 len = mbedtls_mpi_size( X );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000110
111 if( *p - start < (int) len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000113
114 (*p) -= len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( X, *p, len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000116
117 // DER format assumes 2s complement for numbers, so the leftmost bit
118 // should be 0 for positive numbers and 1 for negative numbers.
119 //
Paul Bakker66d5d072014-06-17 16:39:18 +0200120 if( X->s ==1 && **p & 0x80 )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000121 {
122 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000124
125 *--(*p) = 0x00;
126 len += 1;
127 }
128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
130 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_INTEGER ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000131
Paul Bakker3d8fb632014-04-17 12:42:41 +0200132 ret = (int) len;
133
134cleanup:
135 return( ret );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000136}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137#endif /* MBEDTLS_BIGNUM_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139int mbedtls_asn1_write_null( unsigned char **p, unsigned char *start )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000140{
141 int ret;
142 size_t len = 0;
143
144 // Write NULL
145 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, 0) );
147 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_NULL ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000148
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200149 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000150}
151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152int mbedtls_asn1_write_oid( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200153 const char *oid, size_t oid_len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000154{
155 int ret;
156 size_t len = 0;
157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200159 (const unsigned char *) oid, oid_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 MBEDTLS_ASN1_CHK_ADD( len , mbedtls_asn1_write_len( p, start, len ) );
161 MBEDTLS_ASN1_CHK_ADD( len , mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OID ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000162
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200163 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000164}
165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166int mbedtls_asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start,
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200167 const char *oid, size_t oid_len,
168 size_t par_len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000169{
170 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000171 size_t len = 0;
172
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200173 if( par_len == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_null( p, start ) );
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200175 else
176 len += par_len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid, oid_len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
181 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
182 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000183
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200184 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000185}
186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187int mbedtls_asn1_write_bool( unsigned char **p, unsigned char *start, int boolean )
Paul Bakker329def32013-09-06 16:34:38 +0200188{
189 int ret;
190 size_t len = 0;
191
192 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakker329def32013-09-06 16:34:38 +0200194
195 *--(*p) = (boolean) ? 1 : 0;
196 len++;
197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
199 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_BOOLEAN ) );
Paul Bakker329def32013-09-06 16:34:38 +0200200
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200201 return( (int) len );
Paul Bakker329def32013-09-06 16:34:38 +0200202}
203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204int mbedtls_asn1_write_int( unsigned char **p, unsigned char *start, int val )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000205{
206 int ret;
207 size_t len = 0;
208
209 // TODO negative values and values larger than 128
210 // DER format assumes 2s complement for numbers, so the leftmost bit
211 // should be 0 for positive numbers and 1 for negative numbers.
212 //
213 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000215
216 len += 1;
217 *--(*p) = val;
218
Paul Bakker66d5d072014-06-17 16:39:18 +0200219 if( val > 0 && **p & 0x80 )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000220 {
221 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000223
224 *--(*p) = 0x00;
225 len += 1;
226 }
227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
229 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_INTEGER ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000230
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200231 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000232}
233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234int mbedtls_asn1_write_printable_string( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200235 const char *text, size_t text_len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000236{
237 int ret;
238 size_t len = 0;
239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200241 (const unsigned char *) text, text_len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
244 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_PRINTABLE_STRING ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000245
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200246 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000247}
Paul Bakker598e4502013-08-25 14:46:39 +0200248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249int mbedtls_asn1_write_ia5_string( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200250 const char *text, size_t text_len )
Paul Bakker05888152012-02-16 10:26:57 +0000251{
252 int ret;
253 size_t len = 0;
254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200256 (const unsigned char *) text, text_len ) );
Paul Bakker05888152012-02-16 10:26:57 +0000257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
259 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_IA5_STRING ) );
Paul Bakker05888152012-02-16 10:26:57 +0000260
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200261 return( (int) len );
Paul Bakker05888152012-02-16 10:26:57 +0000262}
Paul Bakker598e4502013-08-25 14:46:39 +0200263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264int mbedtls_asn1_write_bitstring( unsigned char **p, unsigned char *start,
Paul Bakker598e4502013-08-25 14:46:39 +0200265 const unsigned char *buf, size_t bits )
266{
267 int ret;
268 size_t len = 0, size;
269
270 size = ( bits / 8 ) + ( ( bits % 8 ) ? 1 : 0 );
271
272 // Calculate byte length
273 //
274 if( *p - start < (int) size + 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakker598e4502013-08-25 14:46:39 +0200276
277 len = size + 1;
278 (*p) -= size;
279 memcpy( *p, buf, size );
280
281 // Write unused bits
282 //
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200283 *--(*p) = (unsigned char) (size * 8 - bits);
Paul Bakker598e4502013-08-25 14:46:39 +0200284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
286 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_BIT_STRING ) );
Paul Bakker598e4502013-08-25 14:46:39 +0200287
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200288 return( (int) len );
Paul Bakker598e4502013-08-25 14:46:39 +0200289}
290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291int mbedtls_asn1_write_octet_string( unsigned char **p, unsigned char *start,
Paul Bakker598e4502013-08-25 14:46:39 +0200292 const unsigned char *buf, size_t size )
293{
294 int ret;
295 size_t len = 0;
296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, buf, size ) );
Paul Bakker598e4502013-08-25 14:46:39 +0200298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
300 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OCTET_STRING ) );
Paul Bakker598e4502013-08-25 14:46:39 +0200301
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200302 return( (int) len );
Paul Bakker598e4502013-08-25 14:46:39 +0200303}
Paul Bakker59ba59f2013-09-09 11:26:00 +0200304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305mbedtls_asn1_named_data *mbedtls_asn1_store_named_data( mbedtls_asn1_named_data **head,
Paul Bakker59ba59f2013-09-09 11:26:00 +0200306 const char *oid, size_t oid_len,
307 const unsigned char *val,
308 size_t val_len )
309{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310 mbedtls_asn1_named_data *cur;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312 if( ( cur = mbedtls_asn1_find_named_data( *head, oid, oid_len ) ) == NULL )
Paul Bakker59ba59f2013-09-09 11:26:00 +0200313 {
314 // Add new entry if not present yet based on OID
315 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316 if( ( cur = mbedtls_malloc( sizeof(mbedtls_asn1_named_data) ) ) == NULL )
Paul Bakker59ba59f2013-09-09 11:26:00 +0200317 return( NULL );
318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 memset( cur, 0, sizeof(mbedtls_asn1_named_data) );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200320
321 cur->oid.len = oid_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322 cur->oid.p = mbedtls_malloc( oid_len );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200323 if( cur->oid.p == NULL )
324 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325 mbedtls_free( cur );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200326 return( NULL );
327 }
328
Manuel Pégourié-Gonnarde5b0fc12014-11-12 22:27:42 +0100329 memcpy( cur->oid.p, oid, oid_len );
330
Paul Bakker59ba59f2013-09-09 11:26:00 +0200331 cur->val.len = val_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 cur->val.p = mbedtls_malloc( val_len );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200333 if( cur->val.p == NULL )
334 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 mbedtls_free( cur->oid.p );
336 mbedtls_free( cur );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200337 return( NULL );
338 }
339
Paul Bakker59ba59f2013-09-09 11:26:00 +0200340 cur->next = *head;
341 *head = cur;
342 }
343 else if( cur->val.len < val_len )
344 {
345 // Enlarge existing value buffer if needed
346 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347 mbedtls_free( cur->val.p );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200348 cur->val.p = NULL;
349
350 cur->val.len = val_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 cur->val.p = mbedtls_malloc( val_len );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200352 if( cur->val.p == NULL )
353 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354 mbedtls_free( cur->oid.p );
355 mbedtls_free( cur );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200356 return( NULL );
357 }
358 }
359
360 if( val != NULL )
361 memcpy( cur->val.p, val, val_len );
362
363 return( cur );
364}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365#endif /* MBEDTLS_ASN1_WRITE_C */