blob: e393ef8d034a68b2d7295abd48b19fdecf3b2ed5 [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"
Teppo Järvelin707ceb82019-10-04 07:49:39 +030031#include "mbedtls/platform_util.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é-Gonnard7551cb92015-05-26 16:04:06 +020039#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#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
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010064 if( len <= 0xFFFF )
65 {
66 if( *p - start < 3 )
67 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000068
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010069 *--(*p) = ( len ) & 0xFF;
70 *--(*p) = ( len >> 8 ) & 0xFF;
71 *--(*p) = 0x82;
72 return( 3 );
73 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +000074
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010075 if( len <= 0xFFFFFF )
76 {
77 if( *p - start < 4 )
78 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
79
80 *--(*p) = ( len ) & 0xFF;
81 *--(*p) = ( len >> 8 ) & 0xFF;
82 *--(*p) = ( len >> 16 ) & 0xFF;
83 *--(*p) = 0x83;
84 return( 4 );
85 }
86
Azim Khan45b79cf2018-05-23 16:55:16 +010087#if SIZE_MAX > 0xFFFFFFFF
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010088 if( len <= 0xFFFFFFFF )
Azim Khan45b79cf2018-05-23 16:55:16 +010089#endif
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010090 {
91 if( *p - start < 5 )
92 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
93
94 *--(*p) = ( len ) & 0xFF;
95 *--(*p) = ( len >> 8 ) & 0xFF;
96 *--(*p) = ( len >> 16 ) & 0xFF;
97 *--(*p) = ( len >> 24 ) & 0xFF;
98 *--(*p) = 0x84;
99 return( 5 );
100 }
101
Azim Khan45b79cf2018-05-23 16:55:16 +0100102#if SIZE_MAX > 0xFFFFFFFF
Paul Bakkerc7d6bd42016-07-14 11:39:56 +0100103 return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Azim Khan45b79cf2018-05-23 16:55:16 +0100104#endif
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000105}
106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107int mbedtls_asn1_write_tag( unsigned char **p, unsigned char *start, unsigned char tag )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000108{
109 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000111
112 *--(*p) = tag;
113
114 return( 1 );
115}
116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117int mbedtls_asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
Paul Bakker9852d002013-08-26 17:56:37 +0200118 const unsigned char *buf, size_t size )
119{
120 size_t len = 0;
121
Manuel Pégourié-Gonnard22c3b7b2015-10-21 12:07:47 +0200122 if( *p < start || (size_t)( *p - start ) < size )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakker9852d002013-08-26 17:56:37 +0200124
125 len = size;
126 (*p) -= len;
Teppo Järvelin91d79382019-10-02 09:09:31 +0300127 mbedtls_platform_memcpy( *p, buf, len );
Paul Bakker9852d002013-08-26 17:56:37 +0200128
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200129 return( (int) len );
Paul Bakker9852d002013-08-26 17:56:37 +0200130}
131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132#if defined(MBEDTLS_BIGNUM_C)
133int mbedtls_asn1_write_mpi( unsigned char **p, unsigned char *start, const mbedtls_mpi *X )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000134{
135 int ret;
136 size_t len = 0;
137
138 // Write the MPI
139 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 len = mbedtls_mpi_size( X );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000141
Manuel Pégourié-Gonnard22c3b7b2015-10-21 12:07:47 +0200142 if( *p < start || (size_t)( *p - start ) < len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000144
145 (*p) -= len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( X, *p, len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000147
148 // DER format assumes 2s complement for numbers, so the leftmost bit
149 // should be 0 for positive numbers and 1 for negative numbers.
150 //
Paul Bakker66d5d072014-06-17 16:39:18 +0200151 if( X->s ==1 && **p & 0x80 )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000152 {
153 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000155
156 *--(*p) = 0x00;
157 len += 1;
158 }
159
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_INTEGER ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000162
Paul Bakker3d8fb632014-04-17 12:42:41 +0200163 ret = (int) len;
164
165cleanup:
166 return( ret );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000167}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168#endif /* MBEDTLS_BIGNUM_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170int mbedtls_asn1_write_null( unsigned char **p, unsigned char *start )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000171{
172 int ret;
173 size_t len = 0;
174
175 // Write NULL
176 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, 0) );
178 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_NULL ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000179
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200180 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000181}
182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183int mbedtls_asn1_write_oid( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200184 const char *oid, size_t oid_len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000185{
186 int ret;
187 size_t len = 0;
188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200190 (const unsigned char *) oid, oid_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 MBEDTLS_ASN1_CHK_ADD( len , mbedtls_asn1_write_len( p, start, len ) );
192 MBEDTLS_ASN1_CHK_ADD( len , mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OID ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000193
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200194 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000195}
196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197int mbedtls_asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start,
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200198 const char *oid, size_t oid_len,
199 size_t par_len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000200{
201 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000202 size_t len = 0;
203
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200204 if( par_len == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_null( p, start ) );
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200206 else
207 len += par_len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid, oid_len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
212 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
213 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000214
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200215 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000216}
217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218int mbedtls_asn1_write_bool( unsigned char **p, unsigned char *start, int boolean )
Paul Bakker329def32013-09-06 16:34:38 +0200219{
220 int ret;
221 size_t len = 0;
222
223 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakker329def32013-09-06 16:34:38 +0200225
Jonathan Leroy87c96c22015-10-14 09:41:56 +0200226 *--(*p) = (boolean) ? 255 : 0;
Paul Bakker329def32013-09-06 16:34:38 +0200227 len++;
228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
230 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_BOOLEAN ) );
Paul Bakker329def32013-09-06 16:34:38 +0200231
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200232 return( (int) len );
Paul Bakker329def32013-09-06 16:34:38 +0200233}
234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235int mbedtls_asn1_write_int( unsigned char **p, unsigned char *start, int val )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000236{
237 int ret;
238 size_t len = 0;
239
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000240 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000242
243 len += 1;
244 *--(*p) = val;
245
Paul Bakker66d5d072014-06-17 16:39:18 +0200246 if( val > 0 && **p & 0x80 )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000247 {
248 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000250
251 *--(*p) = 0x00;
252 len += 1;
253 }
254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
256 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_INTEGER ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000257
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200258 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000259}
260
thomas-deeeba6c9b2018-09-19 09:10:37 +0200261int mbedtls_asn1_write_tagged_string( unsigned char **p, unsigned char *start, int tag,
Jaeden Amero23f954d2018-05-17 11:46:13 +0100262 const char *text, size_t text_len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000263{
264 int ret;
265 size_t len = 0;
266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
Jaeden Amero23f954d2018-05-17 11:46:13 +0100268 (const unsigned char *) text, text_len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
Jaeden Amero23f954d2018-05-17 11:46:13 +0100271 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, tag ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000272
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200273 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000274}
Paul Bakker598e4502013-08-25 14:46:39 +0200275
Jaeden Amero23f954d2018-05-17 11:46:13 +0100276int mbedtls_asn1_write_utf8_string( unsigned char **p, unsigned char *start,
277 const char *text, size_t text_len )
278{
thomas-deeeba6c9b2018-09-19 09:10:37 +0200279 return( mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_UTF8_STRING, text, text_len) );
Jaeden Amero23f954d2018-05-17 11:46:13 +0100280}
281
282int mbedtls_asn1_write_printable_string( unsigned char **p, unsigned char *start,
283 const char *text, size_t text_len )
284{
thomas-deeeba6c9b2018-09-19 09:10:37 +0200285 return( mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_PRINTABLE_STRING, text, text_len) );
Jaeden Amero23f954d2018-05-17 11:46:13 +0100286}
287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288int mbedtls_asn1_write_ia5_string( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200289 const char *text, size_t text_len )
Paul Bakker05888152012-02-16 10:26:57 +0000290{
thomas-deeeba6c9b2018-09-19 09:10:37 +0200291 return( mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_IA5_STRING, text, text_len) );
Paul Bakker05888152012-02-16 10:26:57 +0000292}
Paul Bakker598e4502013-08-25 14:46:39 +0200293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294int mbedtls_asn1_write_bitstring( unsigned char **p, unsigned char *start,
Paul Bakker598e4502013-08-25 14:46:39 +0200295 const unsigned char *buf, size_t bits )
296{
297 int ret;
Andres Amaya Garciad60e3782018-09-26 10:48:24 +0100298 size_t len = 0;
299 size_t unused_bits, byte_len;
Paul Bakker598e4502013-08-25 14:46:39 +0200300
Andres Amaya Garciad60e3782018-09-26 10:48:24 +0100301 byte_len = ( bits + 7 ) / 8;
302 unused_bits = ( byte_len * 8 ) - bits;
Paul Bakker598e4502013-08-25 14:46:39 +0200303
Andres Amaya Garciad60e3782018-09-26 10:48:24 +0100304 if( *p < start || (size_t)( *p - start ) < byte_len + 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakker598e4502013-08-25 14:46:39 +0200306
Andres Amaya Garciad60e3782018-09-26 10:48:24 +0100307 len = byte_len + 1;
Paul Bakker598e4502013-08-25 14:46:39 +0200308
Andres Amaya Garciad60e3782018-09-26 10:48:24 +0100309 /* Write the bitstring. Ensure the unused bits are zeroed */
310 if( byte_len > 0 )
311 {
312 byte_len--;
313 *--( *p ) = buf[byte_len] & ~( ( 0x1 << unused_bits ) - 1 );
314 ( *p ) -= byte_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +0300315 mbedtls_platform_memcpy( *p, buf, byte_len );
Andres Amaya Garciad60e3782018-09-26 10:48:24 +0100316 }
317
318 /* Write unused bits */
319 *--( *p ) = (unsigned char)unused_bits;
Paul Bakker598e4502013-08-25 14:46:39 +0200320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
322 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_BIT_STRING ) );
Paul Bakker598e4502013-08-25 14:46:39 +0200323
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200324 return( (int) len );
Paul Bakker598e4502013-08-25 14:46:39 +0200325}
326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327int mbedtls_asn1_write_octet_string( unsigned char **p, unsigned char *start,
Paul Bakker598e4502013-08-25 14:46:39 +0200328 const unsigned char *buf, size_t size )
329{
330 int ret;
331 size_t len = 0;
332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, buf, size ) );
Paul Bakker598e4502013-08-25 14:46:39 +0200334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
336 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OCTET_STRING ) );
Paul Bakker598e4502013-08-25 14:46:39 +0200337
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200338 return( (int) len );
Paul Bakker598e4502013-08-25 14:46:39 +0200339}
Paul Bakker59ba59f2013-09-09 11:26:00 +0200340
Hanno Becker44da18a2018-10-12 10:42:13 +0100341
342/* This is a copy of the ASN.1 parsing function mbedtls_asn1_find_named_data(),
343 * which is replicated to avoid a dependency ASN1_WRITE_C on ASN1_PARSE_C. */
344static mbedtls_asn1_named_data *asn1_find_named_data(
345 mbedtls_asn1_named_data *list,
346 const char *oid, size_t len )
347{
348 while( list != NULL )
349 {
350 if( list->oid.len == len &&
Teppo Järvelin61f412e2019-10-03 12:25:22 +0300351 mbedtls_platform_memcmp( list->oid.p, oid, len ) == 0 )
Hanno Becker44da18a2018-10-12 10:42:13 +0100352 {
353 break;
354 }
355
356 list = list->next;
357 }
358
359 return( list );
360}
361
362mbedtls_asn1_named_data *mbedtls_asn1_store_named_data(
363 mbedtls_asn1_named_data **head,
Paul Bakker59ba59f2013-09-09 11:26:00 +0200364 const char *oid, size_t oid_len,
365 const unsigned char *val,
366 size_t val_len )
367{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 mbedtls_asn1_named_data *cur;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200369
Hanno Becker44da18a2018-10-12 10:42:13 +0100370 if( ( cur = asn1_find_named_data( *head, oid, oid_len ) ) == NULL )
Paul Bakker59ba59f2013-09-09 11:26:00 +0200371 {
372 // Add new entry if not present yet based on OID
373 //
Simon Butcher29176892016-05-20 00:19:09 +0100374 cur = (mbedtls_asn1_named_data*)mbedtls_calloc( 1,
375 sizeof(mbedtls_asn1_named_data) );
376 if( cur == NULL )
Paul Bakker59ba59f2013-09-09 11:26:00 +0200377 return( NULL );
378
Paul Bakker59ba59f2013-09-09 11:26:00 +0200379 cur->oid.len = oid_len;
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200380 cur->oid.p = mbedtls_calloc( 1, oid_len );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200381 if( cur->oid.p == NULL )
382 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383 mbedtls_free( cur );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200384 return( NULL );
385 }
386
Teppo Järvelin91d79382019-10-02 09:09:31 +0300387 mbedtls_platform_memcpy( cur->oid.p, oid, oid_len );
Manuel Pégourié-Gonnarde5b0fc12014-11-12 22:27:42 +0100388
Paul Bakker59ba59f2013-09-09 11:26:00 +0200389 cur->val.len = val_len;
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200390 cur->val.p = mbedtls_calloc( 1, val_len );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200391 if( cur->val.p == NULL )
392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 mbedtls_free( cur->oid.p );
394 mbedtls_free( cur );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200395 return( NULL );
396 }
397
Paul Bakker59ba59f2013-09-09 11:26:00 +0200398 cur->next = *head;
399 *head = cur;
400 }
401 else if( cur->val.len < val_len )
402 {
Manuel Pégourié-Gonnard97b52092015-12-10 10:50:51 +0100403 /*
404 * Enlarge existing value buffer if needed
405 * Preserve old data until the allocation succeeded, to leave list in
406 * a consistent state in case allocation fails.
407 */
408 void *p = mbedtls_calloc( 1, val_len );
409 if( p == NULL )
Paul Bakker59ba59f2013-09-09 11:26:00 +0200410 return( NULL );
Manuel Pégourié-Gonnard97b52092015-12-10 10:50:51 +0100411
412 mbedtls_free( cur->val.p );
413 cur->val.p = p;
414 cur->val.len = val_len;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200415 }
416
417 if( val != NULL )
Teppo Järvelin91d79382019-10-02 09:09:31 +0300418 mbedtls_platform_memcpy( cur->val.p, val, val_len );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200419
420 return( cur );
421}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422#endif /* MBEDTLS_ASN1_WRITE_C */