blob: d916fcbc6a71f132e1aa8deb8193837eed55df0a [file] [log] [blame]
Paul Bakkerbdb912d2012-02-13 23:11:30 +00001/*
2 * ASN.1 buffer writing functionality
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerbdb912d2012-02-13 23:11:30 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkerbdb912d2012-02-13 23:11:30 +000020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakkerbdb912d2012-02-13 23:11:30 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_ASN1_WRITE_C)
Paul Bakkerbdb912d2012-02-13 23:11:30 +000029
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/asn1write.h"
Paul Bakkerbdb912d2012-02-13 23:11:30 +000031
Rich Evans00ab4702015-02-06 13:43:58 +000032#include <string.h>
33
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000035#include "mbedtls/platform.h"
Paul Bakker59ba59f2013-09-09 11:26:00 +020036#else
37#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020038#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#define mbedtls_free free
Paul Bakker59ba59f2013-09-09 11:26:00 +020040#endif
41
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042int mbedtls_asn1_write_len( unsigned char **p, unsigned char *start, size_t len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +000043{
44 if( len < 0x80 )
45 {
46 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000048
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020049 *--(*p) = (unsigned char) len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000050 return( 1 );
51 }
52
53 if( len <= 0xFF )
54 {
55 if( *p - start < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000057
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020058 *--(*p) = (unsigned char) len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000059 *--(*p) = 0x81;
60 return( 2 );
61 }
62
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010063 if( len <= 0xFFFF )
64 {
65 if( *p - start < 3 )
66 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000067
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010068 *--(*p) = ( len ) & 0xFF;
69 *--(*p) = ( len >> 8 ) & 0xFF;
70 *--(*p) = 0x82;
71 return( 3 );
72 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +000073
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010074 if( len <= 0xFFFFFF )
75 {
76 if( *p - start < 4 )
77 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
78
79 *--(*p) = ( len ) & 0xFF;
80 *--(*p) = ( len >> 8 ) & 0xFF;
81 *--(*p) = ( len >> 16 ) & 0xFF;
82 *--(*p) = 0x83;
83 return( 4 );
84 }
85
86 if( len <= 0xFFFFFFFF )
87 {
88 if( *p - start < 5 )
89 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
90
91 *--(*p) = ( len ) & 0xFF;
92 *--(*p) = ( len >> 8 ) & 0xFF;
93 *--(*p) = ( len >> 16 ) & 0xFF;
94 *--(*p) = ( len >> 24 ) & 0xFF;
95 *--(*p) = 0x84;
96 return( 5 );
97 }
98
99 return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000100}
101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102int mbedtls_asn1_write_tag( unsigned char **p, unsigned char *start, unsigned char tag )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000103{
104 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000106
107 *--(*p) = tag;
108
109 return( 1 );
110}
111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112int mbedtls_asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
Paul Bakker9852d002013-08-26 17:56:37 +0200113 const unsigned char *buf, size_t size )
114{
115 size_t len = 0;
116
Manuel Pégourié-Gonnard22c3b7b2015-10-21 12:07:47 +0200117 if( *p < start || (size_t)( *p - start ) < size )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakker9852d002013-08-26 17:56:37 +0200119
120 len = size;
121 (*p) -= len;
122 memcpy( *p, buf, len );
123
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200124 return( (int) len );
Paul Bakker9852d002013-08-26 17:56:37 +0200125}
126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127#if defined(MBEDTLS_BIGNUM_C)
128int mbedtls_asn1_write_mpi( unsigned char **p, unsigned char *start, const mbedtls_mpi *X )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000129{
130 int ret;
131 size_t len = 0;
132
133 // Write the MPI
134 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 len = mbedtls_mpi_size( X );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000136
Manuel Pégourié-Gonnard22c3b7b2015-10-21 12:07:47 +0200137 if( *p < start || (size_t)( *p - start ) < len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000139
140 (*p) -= len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( X, *p, len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000142
143 // DER format assumes 2s complement for numbers, so the leftmost bit
144 // should be 0 for positive numbers and 1 for negative numbers.
145 //
Paul Bakker66d5d072014-06-17 16:39:18 +0200146 if( X->s ==1 && **p & 0x80 )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000147 {
148 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000150
151 *--(*p) = 0x00;
152 len += 1;
153 }
154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
156 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_INTEGER ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000157
Paul Bakker3d8fb632014-04-17 12:42:41 +0200158 ret = (int) len;
159
160cleanup:
161 return( ret );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000162}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163#endif /* MBEDTLS_BIGNUM_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165int mbedtls_asn1_write_null( unsigned char **p, unsigned char *start )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000166{
167 int ret;
168 size_t len = 0;
169
170 // Write NULL
171 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, 0) );
173 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_NULL ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000174
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200175 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000176}
177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178int mbedtls_asn1_write_oid( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200179 const char *oid, size_t oid_len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000180{
181 int ret;
182 size_t len = 0;
183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200185 (const unsigned char *) oid, oid_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 MBEDTLS_ASN1_CHK_ADD( len , mbedtls_asn1_write_len( p, start, len ) );
187 MBEDTLS_ASN1_CHK_ADD( len , mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OID ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000188
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200189 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000190}
191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192int mbedtls_asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start,
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200193 const char *oid, size_t oid_len,
194 size_t par_len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000195{
196 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000197 size_t len = 0;
198
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200199 if( par_len == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_null( p, start ) );
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200201 else
202 len += par_len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid, oid_len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
207 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
208 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000209
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200210 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000211}
212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213int mbedtls_asn1_write_bool( unsigned char **p, unsigned char *start, int boolean )
Paul Bakker329def32013-09-06 16:34:38 +0200214{
215 int ret;
216 size_t len = 0;
217
218 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakker329def32013-09-06 16:34:38 +0200220
Jonathan Leroy87c96c22015-10-14 09:41:56 +0200221 *--(*p) = (boolean) ? 255 : 0;
Paul Bakker329def32013-09-06 16:34:38 +0200222 len++;
223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
225 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_BOOLEAN ) );
Paul Bakker329def32013-09-06 16:34:38 +0200226
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200227 return( (int) len );
Paul Bakker329def32013-09-06 16:34:38 +0200228}
229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230int mbedtls_asn1_write_int( unsigned char **p, unsigned char *start, int val )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000231{
232 int ret;
233 size_t len = 0;
234
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000235 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000237
238 len += 1;
239 *--(*p) = val;
240
Paul Bakker66d5d072014-06-17 16:39:18 +0200241 if( val > 0 && **p & 0x80 )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000242 {
243 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000245
246 *--(*p) = 0x00;
247 len += 1;
248 }
249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
251 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_INTEGER ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000252
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200253 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000254}
255
Jaeden Amero23f954d2018-05-17 11:46:13 +0100256int mbedtls_asn1_write_any_string( unsigned char **p, unsigned char *start, int tag,
257 const char *text, size_t text_len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000258{
259 int ret;
260 size_t len = 0;
261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
Jaeden Amero23f954d2018-05-17 11:46:13 +0100263 (const unsigned char *) text, text_len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
Jaeden Amero23f954d2018-05-17 11:46:13 +0100266 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, tag ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000267
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200268 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000269}
Paul Bakker598e4502013-08-25 14:46:39 +0200270
Jaeden Amero23f954d2018-05-17 11:46:13 +0100271int mbedtls_asn1_write_utf8_string( unsigned char **p, unsigned char *start,
272 const char *text, size_t text_len )
273{
274 return( mbedtls_asn1_write_any_string(p, start, MBEDTLS_ASN1_UTF8_STRING, text, text_len) );
275}
276
277int mbedtls_asn1_write_printable_string( unsigned char **p, unsigned char *start,
278 const char *text, size_t text_len )
279{
280 return( mbedtls_asn1_write_any_string(p, start, MBEDTLS_ASN1_PRINTABLE_STRING, text, text_len) );
281}
282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283int mbedtls_asn1_write_ia5_string( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200284 const char *text, size_t text_len )
Paul Bakker05888152012-02-16 10:26:57 +0000285{
Jaeden Amero23f954d2018-05-17 11:46:13 +0100286 return( mbedtls_asn1_write_any_string(p, start, MBEDTLS_ASN1_IA5_STRING, text, text_len) );
Paul Bakker05888152012-02-16 10:26:57 +0000287}
Paul Bakker598e4502013-08-25 14:46:39 +0200288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289int mbedtls_asn1_write_bitstring( unsigned char **p, unsigned char *start,
Paul Bakker598e4502013-08-25 14:46:39 +0200290 const unsigned char *buf, size_t bits )
291{
292 int ret;
293 size_t len = 0, size;
294
295 size = ( bits / 8 ) + ( ( bits % 8 ) ? 1 : 0 );
296
297 // Calculate byte length
298 //
Manuel Pégourié-Gonnard22c3b7b2015-10-21 12:07:47 +0200299 if( *p < start || (size_t)( *p - start ) < size + 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakker598e4502013-08-25 14:46:39 +0200301
302 len = size + 1;
303 (*p) -= size;
304 memcpy( *p, buf, size );
305
306 // Write unused bits
307 //
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200308 *--(*p) = (unsigned char) (size * 8 - bits);
Paul Bakker598e4502013-08-25 14:46:39 +0200309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
311 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_BIT_STRING ) );
Paul Bakker598e4502013-08-25 14:46:39 +0200312
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200313 return( (int) len );
Paul Bakker598e4502013-08-25 14:46:39 +0200314}
315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316int mbedtls_asn1_write_octet_string( unsigned char **p, unsigned char *start,
Paul Bakker598e4502013-08-25 14:46:39 +0200317 const unsigned char *buf, size_t size )
318{
319 int ret;
320 size_t len = 0;
321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, buf, size ) );
Paul Bakker598e4502013-08-25 14:46:39 +0200323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
325 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OCTET_STRING ) );
Paul Bakker598e4502013-08-25 14:46:39 +0200326
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200327 return( (int) len );
Paul Bakker598e4502013-08-25 14:46:39 +0200328}
Paul Bakker59ba59f2013-09-09 11:26:00 +0200329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330mbedtls_asn1_named_data *mbedtls_asn1_store_named_data( mbedtls_asn1_named_data **head,
Paul Bakker59ba59f2013-09-09 11:26:00 +0200331 const char *oid, size_t oid_len,
332 const unsigned char *val,
333 size_t val_len )
334{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 mbedtls_asn1_named_data *cur;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 if( ( cur = mbedtls_asn1_find_named_data( *head, oid, oid_len ) ) == NULL )
Paul Bakker59ba59f2013-09-09 11:26:00 +0200338 {
339 // Add new entry if not present yet based on OID
340 //
Simon Butcher29176892016-05-20 00:19:09 +0100341 cur = (mbedtls_asn1_named_data*)mbedtls_calloc( 1,
342 sizeof(mbedtls_asn1_named_data) );
343 if( cur == NULL )
Paul Bakker59ba59f2013-09-09 11:26:00 +0200344 return( NULL );
345
Paul Bakker59ba59f2013-09-09 11:26:00 +0200346 cur->oid.len = oid_len;
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200347 cur->oid.p = mbedtls_calloc( 1, oid_len );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200348 if( cur->oid.p == NULL )
349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 mbedtls_free( cur );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200351 return( NULL );
352 }
353
Manuel Pégourié-Gonnarde5b0fc12014-11-12 22:27:42 +0100354 memcpy( cur->oid.p, oid, oid_len );
355
Paul Bakker59ba59f2013-09-09 11:26:00 +0200356 cur->val.len = val_len;
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200357 cur->val.p = mbedtls_calloc( 1, val_len );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200358 if( cur->val.p == NULL )
359 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360 mbedtls_free( cur->oid.p );
361 mbedtls_free( cur );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200362 return( NULL );
363 }
364
Paul Bakker59ba59f2013-09-09 11:26:00 +0200365 cur->next = *head;
366 *head = cur;
367 }
368 else if( cur->val.len < val_len )
369 {
Manuel Pégourié-Gonnard97b52092015-12-10 10:50:51 +0100370 /*
371 * Enlarge existing value buffer if needed
372 * Preserve old data until the allocation succeeded, to leave list in
373 * a consistent state in case allocation fails.
374 */
375 void *p = mbedtls_calloc( 1, val_len );
376 if( p == NULL )
Paul Bakker59ba59f2013-09-09 11:26:00 +0200377 return( NULL );
Manuel Pégourié-Gonnard97b52092015-12-10 10:50:51 +0100378
379 mbedtls_free( cur->val.p );
380 cur->val.p = p;
381 cur->val.len = val_len;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200382 }
383
384 if( val != NULL )
385 memcpy( cur->val.p, val, val_len );
386
387 return( cur );
388}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389#endif /* MBEDTLS_ASN1_WRITE_C */