blob: 2d196f672008f9cbd5061966bfbd554f63cf3dfd [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
63 if( *p - start < 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000065
66 // We assume we never have lengths larger than 65535 bytes
67 //
68 *--(*p) = len % 256;
69 *--(*p) = ( len / 256 ) % 256;
70 *--(*p) = 0x82;
71
72 return( 3 );
73}
74
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075int mbedtls_asn1_write_tag( unsigned char **p, unsigned char *start, unsigned char tag )
Paul Bakkerbdb912d2012-02-13 23:11:30 +000076{
77 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000079
80 *--(*p) = tag;
81
82 return( 1 );
83}
84
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085int mbedtls_asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
Paul Bakker9852d002013-08-26 17:56:37 +020086 const unsigned char *buf, size_t size )
87{
88 size_t len = 0;
89
Manuel Pégourié-Gonnard0d66bb92015-10-21 12:07:47 +020090 if( *p < start || (size_t)( *p - start ) < size )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakker9852d002013-08-26 17:56:37 +020092
93 len = size;
94 (*p) -= len;
95 memcpy( *p, buf, len );
96
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020097 return( (int) len );
Paul Bakker9852d002013-08-26 17:56:37 +020098}
99
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100#if defined(MBEDTLS_BIGNUM_C)
101int mbedtls_asn1_write_mpi( unsigned char **p, unsigned char *start, const mbedtls_mpi *X )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000102{
103 int ret;
104 size_t len = 0;
105
106 // Write the MPI
107 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108 len = mbedtls_mpi_size( X );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000109
Manuel Pégourié-Gonnard0d66bb92015-10-21 12:07:47 +0200110 if( *p < start || (size_t)( *p - start ) < len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000112
113 (*p) -= len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( X, *p, len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000115
116 // DER format assumes 2s complement for numbers, so the leftmost bit
117 // should be 0 for positive numbers and 1 for negative numbers.
118 //
Paul Bakker66d5d072014-06-17 16:39:18 +0200119 if( X->s ==1 && **p & 0x80 )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000120 {
121 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000123
124 *--(*p) = 0x00;
125 len += 1;
126 }
127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
129 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_INTEGER ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000130
Paul Bakker3d8fb632014-04-17 12:42:41 +0200131 ret = (int) len;
132
133cleanup:
134 return( ret );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000135}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136#endif /* MBEDTLS_BIGNUM_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138int mbedtls_asn1_write_null( unsigned char **p, unsigned char *start )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000139{
140 int ret;
141 size_t len = 0;
142
143 // Write NULL
144 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, 0) );
146 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_NULL ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000147
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200148 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000149}
150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151int mbedtls_asn1_write_oid( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200152 const char *oid, size_t oid_len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000153{
154 int ret;
155 size_t len = 0;
156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200158 (const unsigned char *) oid, oid_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 MBEDTLS_ASN1_CHK_ADD( len , mbedtls_asn1_write_len( p, start, len ) );
160 MBEDTLS_ASN1_CHK_ADD( len , mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OID ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000161
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200162 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000163}
164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165int mbedtls_asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start,
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200166 const char *oid, size_t oid_len,
167 size_t par_len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000168{
169 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000170 size_t len = 0;
171
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200172 if( par_len == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_null( p, start ) );
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200174 else
175 len += par_len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid, oid_len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
180 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
181 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000182
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200183 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000184}
185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186int mbedtls_asn1_write_bool( unsigned char **p, unsigned char *start, int boolean )
Paul Bakker329def32013-09-06 16:34:38 +0200187{
188 int ret;
189 size_t len = 0;
190
191 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakker329def32013-09-06 16:34:38 +0200193
Jonathan Leroy00c6b3c2015-10-14 09:41:56 +0200194 *--(*p) = (boolean) ? 255 : 0;
Paul Bakker329def32013-09-06 16:34:38 +0200195 len++;
196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
198 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_BOOLEAN ) );
Paul Bakker329def32013-09-06 16:34:38 +0200199
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200200 return( (int) len );
Paul Bakker329def32013-09-06 16:34:38 +0200201}
202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203int mbedtls_asn1_write_int( unsigned char **p, unsigned char *start, int val )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000204{
205 int ret;
206 size_t len = 0;
207
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000208 // DER format assumes 2s complement for numbers, so the leftmost bit
209 // should be 0 for positive numbers and 1 for negative numbers.
210 //
211 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000213
214 len += 1;
215 *--(*p) = val;
216
Paul Bakker66d5d072014-06-17 16:39:18 +0200217 if( val > 0 && **p & 0x80 )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000218 {
219 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000221
222 *--(*p) = 0x00;
223 len += 1;
224 }
225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
227 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_INTEGER ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000228
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200229 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000230}
231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232int mbedtls_asn1_write_printable_string( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200233 const char *text, size_t text_len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000234{
235 int ret;
236 size_t len = 0;
237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200239 (const unsigned char *) text, text_len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
242 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_PRINTABLE_STRING ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000243
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200244 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000245}
Paul Bakker598e4502013-08-25 14:46:39 +0200246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247int mbedtls_asn1_write_ia5_string( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200248 const char *text, size_t text_len )
Paul Bakker05888152012-02-16 10:26:57 +0000249{
250 int ret;
251 size_t len = 0;
252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200254 (const unsigned char *) text, text_len ) );
Paul Bakker05888152012-02-16 10:26:57 +0000255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
257 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_IA5_STRING ) );
Paul Bakker05888152012-02-16 10:26:57 +0000258
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200259 return( (int) len );
Paul Bakker05888152012-02-16 10:26:57 +0000260}
Paul Bakker598e4502013-08-25 14:46:39 +0200261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262int mbedtls_asn1_write_bitstring( unsigned char **p, unsigned char *start,
Paul Bakker598e4502013-08-25 14:46:39 +0200263 const unsigned char *buf, size_t bits )
264{
265 int ret;
266 size_t len = 0, size;
267
268 size = ( bits / 8 ) + ( ( bits % 8 ) ? 1 : 0 );
269
270 // Calculate byte length
271 //
Manuel Pégourié-Gonnard0d66bb92015-10-21 12:07:47 +0200272 if( *p < start || (size_t)( *p - start ) < size + 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakker598e4502013-08-25 14:46:39 +0200274
275 len = size + 1;
276 (*p) -= size;
277 memcpy( *p, buf, size );
278
279 // Write unused bits
280 //
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200281 *--(*p) = (unsigned char) (size * 8 - bits);
Paul Bakker598e4502013-08-25 14:46:39 +0200282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
284 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_BIT_STRING ) );
Paul Bakker598e4502013-08-25 14:46:39 +0200285
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200286 return( (int) len );
Paul Bakker598e4502013-08-25 14:46:39 +0200287}
288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289int mbedtls_asn1_write_octet_string( unsigned char **p, unsigned char *start,
Paul Bakker598e4502013-08-25 14:46:39 +0200290 const unsigned char *buf, size_t size )
291{
292 int ret;
293 size_t len = 0;
294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, buf, size ) );
Paul Bakker598e4502013-08-25 14:46:39 +0200296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
298 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OCTET_STRING ) );
Paul Bakker598e4502013-08-25 14:46:39 +0200299
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200300 return( (int) len );
Paul Bakker598e4502013-08-25 14:46:39 +0200301}
Paul Bakker59ba59f2013-09-09 11:26:00 +0200302
Hanno Becker99288072018-10-12 10:42:13 +0100303
304/* This is a copy of the ASN.1 parsing function mbedtls_asn1_find_named_data(),
305 * which is replicated to avoid a dependency ASN1_WRITE_C on ASN1_PARSE_C. */
306static mbedtls_asn1_named_data *asn1_find_named_data(
307 mbedtls_asn1_named_data *list,
308 const char *oid, size_t len )
309{
310 while( list != NULL )
311 {
312 if( list->oid.len == len &&
313 memcmp( list->oid.p, oid, len ) == 0 )
314 {
315 break;
316 }
317
318 list = list->next;
319 }
320
321 return( list );
322}
323
324mbedtls_asn1_named_data *mbedtls_asn1_store_named_data(
325 mbedtls_asn1_named_data **head,
Paul Bakker59ba59f2013-09-09 11:26:00 +0200326 const char *oid, size_t oid_len,
327 const unsigned char *val,
328 size_t val_len )
329{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330 mbedtls_asn1_named_data *cur;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200331
Hanno Becker99288072018-10-12 10:42:13 +0100332 if( ( cur = asn1_find_named_data( *head, oid, oid_len ) ) == NULL )
Paul Bakker59ba59f2013-09-09 11:26:00 +0200333 {
334 // Add new entry if not present yet based on OID
335 //
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200336 if( ( cur = mbedtls_calloc( 1, sizeof(mbedtls_asn1_named_data) ) ) == NULL )
Paul Bakker59ba59f2013-09-09 11:26:00 +0200337 return( NULL );
338
Paul Bakker59ba59f2013-09-09 11:26:00 +0200339 cur->oid.len = oid_len;
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200340 cur->oid.p = mbedtls_calloc( 1, oid_len );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200341 if( cur->oid.p == NULL )
342 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343 mbedtls_free( cur );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200344 return( NULL );
345 }
346
Manuel Pégourié-Gonnarde5b0fc12014-11-12 22:27:42 +0100347 memcpy( cur->oid.p, oid, oid_len );
348
Paul Bakker59ba59f2013-09-09 11:26:00 +0200349 cur->val.len = val_len;
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200350 cur->val.p = mbedtls_calloc( 1, val_len );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200351 if( cur->val.p == NULL )
352 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353 mbedtls_free( cur->oid.p );
354 mbedtls_free( cur );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200355 return( NULL );
356 }
357
Paul Bakker59ba59f2013-09-09 11:26:00 +0200358 cur->next = *head;
359 *head = cur;
360 }
361 else if( cur->val.len < val_len )
362 {
Manuel Pégourié-Gonnard3e60d2a2015-12-10 10:50:51 +0100363 /*
364 * Enlarge existing value buffer if needed
365 * Preserve old data until the allocation succeeded, to leave list in
366 * a consistent state in case allocation fails.
367 */
368 void *p = mbedtls_calloc( 1, val_len );
369 if( p == NULL )
Paul Bakker59ba59f2013-09-09 11:26:00 +0200370 return( NULL );
Manuel Pégourié-Gonnard3e60d2a2015-12-10 10:50:51 +0100371
372 mbedtls_free( cur->val.p );
373 cur->val.p = p;
374 cur->val.len = val_len;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200375 }
376
377 if( val != NULL )
378 memcpy( cur->val.p, val, val_len );
379
380 return( cur );
381}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382#endif /* MBEDTLS_ASN1_WRITE_C */