blob: ef35ee43816327c05793312754e209212400dcd2 [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{
Paul Bakker7eb12432016-07-14 10:27:08 +010044 // We don't support lengths over 65535 for now
45 //
46 if( len > 0xFFFF )
47 return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
48
Paul Bakkerbdb912d2012-02-13 23:11:30 +000049 if( len < 0x80 )
50 {
51 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000053
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020054 *--(*p) = (unsigned char) len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000055 return( 1 );
56 }
57
58 if( len <= 0xFF )
59 {
60 if( *p - start < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000062
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020063 *--(*p) = (unsigned char) len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000064 *--(*p) = 0x81;
65 return( 2 );
66 }
67
68 if( *p - start < 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000070
Paul Bakkerbdb912d2012-02-13 23:11:30 +000071 *--(*p) = len % 256;
72 *--(*p) = ( len / 256 ) % 256;
73 *--(*p) = 0x82;
74
75 return( 3 );
76}
77
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078int mbedtls_asn1_write_tag( unsigned char **p, unsigned char *start, unsigned char tag )
Paul Bakkerbdb912d2012-02-13 23:11:30 +000079{
80 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000082
83 *--(*p) = tag;
84
85 return( 1 );
86}
87
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088int mbedtls_asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
Paul Bakker9852d002013-08-26 17:56:37 +020089 const unsigned char *buf, size_t size )
90{
91 size_t len = 0;
92
Manuel Pégourié-Gonnard22c3b7b2015-10-21 12:07:47 +020093 if( *p < start || (size_t)( *p - start ) < size )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakker9852d002013-08-26 17:56:37 +020095
96 len = size;
97 (*p) -= len;
98 memcpy( *p, buf, len );
99
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200100 return( (int) len );
Paul Bakker9852d002013-08-26 17:56:37 +0200101}
102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103#if defined(MBEDTLS_BIGNUM_C)
104int mbedtls_asn1_write_mpi( unsigned char **p, unsigned char *start, const mbedtls_mpi *X )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000105{
106 int ret;
107 size_t len = 0;
108
109 // Write the MPI
110 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111 len = mbedtls_mpi_size( X );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000112
Manuel Pégourié-Gonnard22c3b7b2015-10-21 12:07:47 +0200113 if( *p < start || (size_t)( *p - start ) < len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000115
116 (*p) -= len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( X, *p, len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000118
119 // DER format assumes 2s complement for numbers, so the leftmost bit
120 // should be 0 for positive numbers and 1 for negative numbers.
121 //
Paul Bakker66d5d072014-06-17 16:39:18 +0200122 if( X->s ==1 && **p & 0x80 )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000123 {
124 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000126
127 *--(*p) = 0x00;
128 len += 1;
129 }
130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
132 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_INTEGER ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000133
Paul Bakker3d8fb632014-04-17 12:42:41 +0200134 ret = (int) len;
135
136cleanup:
137 return( ret );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000138}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139#endif /* MBEDTLS_BIGNUM_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141int mbedtls_asn1_write_null( unsigned char **p, unsigned char *start )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000142{
143 int ret;
144 size_t len = 0;
145
146 // Write NULL
147 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, 0) );
149 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_NULL ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000150
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200151 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000152}
153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154int mbedtls_asn1_write_oid( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200155 const char *oid, size_t oid_len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000156{
157 int ret;
158 size_t len = 0;
159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200161 (const unsigned char *) oid, oid_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 MBEDTLS_ASN1_CHK_ADD( len , mbedtls_asn1_write_len( p, start, len ) );
163 MBEDTLS_ASN1_CHK_ADD( len , mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OID ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000164
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200165 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000166}
167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168int mbedtls_asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start,
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200169 const char *oid, size_t oid_len,
170 size_t par_len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000171{
172 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000173 size_t len = 0;
174
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200175 if( par_len == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_null( p, start ) );
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200177 else
178 len += par_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_oid( p, start, oid, oid_len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
183 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
184 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000185
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200186 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000187}
188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189int mbedtls_asn1_write_bool( unsigned char **p, unsigned char *start, int boolean )
Paul Bakker329def32013-09-06 16:34:38 +0200190{
191 int ret;
192 size_t len = 0;
193
194 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakker329def32013-09-06 16:34:38 +0200196
Jonathan Leroy87c96c22015-10-14 09:41:56 +0200197 *--(*p) = (boolean) ? 255 : 0;
Paul Bakker329def32013-09-06 16:34:38 +0200198 len++;
199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
201 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_BOOLEAN ) );
Paul Bakker329def32013-09-06 16:34:38 +0200202
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200203 return( (int) len );
Paul Bakker329def32013-09-06 16:34:38 +0200204}
205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206int mbedtls_asn1_write_int( unsigned char **p, unsigned char *start, int val )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000207{
208 int ret;
209 size_t len = 0;
210
211 // TODO negative values and values larger than 128
212 // DER format assumes 2s complement for numbers, so the leftmost bit
213 // should be 0 for positive numbers and 1 for negative numbers.
214 //
215 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000217
218 len += 1;
219 *--(*p) = val;
220
Paul Bakker66d5d072014-06-17 16:39:18 +0200221 if( val > 0 && **p & 0x80 )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000222 {
223 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000225
226 *--(*p) = 0x00;
227 len += 1;
228 }
229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
231 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_INTEGER ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000232
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200233 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000234}
235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236int mbedtls_asn1_write_printable_string( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200237 const char *text, size_t text_len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000238{
239 int ret;
240 size_t len = 0;
241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200243 (const unsigned char *) text, text_len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
246 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_PRINTABLE_STRING ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000247
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200248 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000249}
Paul Bakker598e4502013-08-25 14:46:39 +0200250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251int mbedtls_asn1_write_ia5_string( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200252 const char *text, size_t text_len )
Paul Bakker05888152012-02-16 10:26:57 +0000253{
254 int ret;
255 size_t len = 0;
256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200258 (const unsigned char *) text, text_len ) );
Paul Bakker05888152012-02-16 10:26:57 +0000259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
261 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_IA5_STRING ) );
Paul Bakker05888152012-02-16 10:26:57 +0000262
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200263 return( (int) len );
Paul Bakker05888152012-02-16 10:26:57 +0000264}
Paul Bakker598e4502013-08-25 14:46:39 +0200265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266int mbedtls_asn1_write_bitstring( unsigned char **p, unsigned char *start,
Paul Bakker598e4502013-08-25 14:46:39 +0200267 const unsigned char *buf, size_t bits )
268{
269 int ret;
270 size_t len = 0, size;
271
272 size = ( bits / 8 ) + ( ( bits % 8 ) ? 1 : 0 );
273
274 // Calculate byte length
275 //
Manuel Pégourié-Gonnard22c3b7b2015-10-21 12:07:47 +0200276 if( *p < start || (size_t)( *p - start ) < size + 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakker598e4502013-08-25 14:46:39 +0200278
279 len = size + 1;
280 (*p) -= size;
281 memcpy( *p, buf, size );
282
283 // Write unused bits
284 //
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200285 *--(*p) = (unsigned char) (size * 8 - bits);
Paul Bakker598e4502013-08-25 14:46:39 +0200286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
288 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_BIT_STRING ) );
Paul Bakker598e4502013-08-25 14:46:39 +0200289
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200290 return( (int) len );
Paul Bakker598e4502013-08-25 14:46:39 +0200291}
292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293int mbedtls_asn1_write_octet_string( unsigned char **p, unsigned char *start,
Paul Bakker598e4502013-08-25 14:46:39 +0200294 const unsigned char *buf, size_t size )
295{
296 int ret;
297 size_t len = 0;
298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, buf, size ) );
Paul Bakker598e4502013-08-25 14:46:39 +0200300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
302 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OCTET_STRING ) );
Paul Bakker598e4502013-08-25 14:46:39 +0200303
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200304 return( (int) len );
Paul Bakker598e4502013-08-25 14:46:39 +0200305}
Paul Bakker59ba59f2013-09-09 11:26:00 +0200306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307mbedtls_asn1_named_data *mbedtls_asn1_store_named_data( mbedtls_asn1_named_data **head,
Paul Bakker59ba59f2013-09-09 11:26:00 +0200308 const char *oid, size_t oid_len,
309 const unsigned char *val,
310 size_t val_len )
311{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312 mbedtls_asn1_named_data *cur;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 if( ( cur = mbedtls_asn1_find_named_data( *head, oid, oid_len ) ) == NULL )
Paul Bakker59ba59f2013-09-09 11:26:00 +0200315 {
316 // Add new entry if not present yet based on OID
317 //
Simon Butcher29176892016-05-20 00:19:09 +0100318 cur = (mbedtls_asn1_named_data*)mbedtls_calloc( 1,
319 sizeof(mbedtls_asn1_named_data) );
320 if( cur == NULL )
Paul Bakker59ba59f2013-09-09 11:26:00 +0200321 return( NULL );
322
Paul Bakker59ba59f2013-09-09 11:26:00 +0200323 cur->oid.len = oid_len;
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200324 cur->oid.p = mbedtls_calloc( 1, oid_len );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200325 if( cur->oid.p == NULL )
326 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327 mbedtls_free( cur );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200328 return( NULL );
329 }
330
Manuel Pégourié-Gonnarde5b0fc12014-11-12 22:27:42 +0100331 memcpy( cur->oid.p, oid, oid_len );
332
Paul Bakker59ba59f2013-09-09 11:26:00 +0200333 cur->val.len = val_len;
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200334 cur->val.p = mbedtls_calloc( 1, val_len );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200335 if( cur->val.p == NULL )
336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 mbedtls_free( cur->oid.p );
338 mbedtls_free( cur );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200339 return( NULL );
340 }
341
Paul Bakker59ba59f2013-09-09 11:26:00 +0200342 cur->next = *head;
343 *head = cur;
344 }
345 else if( cur->val.len < val_len )
346 {
Manuel Pégourié-Gonnard97b52092015-12-10 10:50:51 +0100347 /*
348 * Enlarge existing value buffer if needed
349 * Preserve old data until the allocation succeeded, to leave list in
350 * a consistent state in case allocation fails.
351 */
352 void *p = mbedtls_calloc( 1, val_len );
353 if( p == NULL )
Paul Bakker59ba59f2013-09-09 11:26:00 +0200354 return( NULL );
Manuel Pégourié-Gonnard97b52092015-12-10 10:50:51 +0100355
356 mbedtls_free( cur->val.p );
357 cur->val.p = p;
358 cur->val.len = val_len;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200359 }
360
361 if( val != NULL )
362 memcpy( cur->val.p, val, val_len );
363
364 return( cur );
365}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366#endif /* MBEDTLS_ASN1_WRITE_C */