blob: b54e26bd8ab475e5bf8481a7c7f46ece913e3906 [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
Azim Khan45b79cf2018-05-23 16:55:16 +010086#if SIZE_MAX > 0xFFFFFFFF
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010087 if( len <= 0xFFFFFFFF )
Azim Khan45b79cf2018-05-23 16:55:16 +010088#endif
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010089 {
90 if( *p - start < 5 )
91 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
92
93 *--(*p) = ( len ) & 0xFF;
94 *--(*p) = ( len >> 8 ) & 0xFF;
95 *--(*p) = ( len >> 16 ) & 0xFF;
96 *--(*p) = ( len >> 24 ) & 0xFF;
97 *--(*p) = 0x84;
98 return( 5 );
99 }
100
Azim Khan45b79cf2018-05-23 16:55:16 +0100101#if SIZE_MAX > 0xFFFFFFFF
Paul Bakkerc7d6bd42016-07-14 11:39:56 +0100102 return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Azim Khan45b79cf2018-05-23 16:55:16 +0100103#endif
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000104}
105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106int mbedtls_asn1_write_tag( unsigned char **p, unsigned char *start, unsigned char tag )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000107{
108 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000110
111 *--(*p) = tag;
112
113 return( 1 );
114}
115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116int mbedtls_asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
Paul Bakker9852d002013-08-26 17:56:37 +0200117 const unsigned char *buf, size_t size )
118{
119 size_t len = 0;
120
Manuel Pégourié-Gonnard22c3b7b2015-10-21 12:07:47 +0200121 if( *p < start || (size_t)( *p - start ) < size )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakker9852d002013-08-26 17:56:37 +0200123
124 len = size;
125 (*p) -= len;
126 memcpy( *p, buf, len );
127
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200128 return( (int) len );
Paul Bakker9852d002013-08-26 17:56:37 +0200129}
130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131#if defined(MBEDTLS_BIGNUM_C)
132int mbedtls_asn1_write_mpi( unsigned char **p, unsigned char *start, const mbedtls_mpi *X )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000133{
134 int ret;
135 size_t len = 0;
136
137 // Write the MPI
138 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 len = mbedtls_mpi_size( X );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000140
Manuel Pégourié-Gonnard22c3b7b2015-10-21 12:07:47 +0200141 if( *p < start || (size_t)( *p - start ) < len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000143
144 (*p) -= len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( X, *p, len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000146
147 // DER format assumes 2s complement for numbers, so the leftmost bit
148 // should be 0 for positive numbers and 1 for negative numbers.
149 //
Paul Bakker66d5d072014-06-17 16:39:18 +0200150 if( X->s ==1 && **p & 0x80 )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000151 {
152 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000154
155 *--(*p) = 0x00;
156 len += 1;
157 }
158
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_INTEGER ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000161
Paul Bakker3d8fb632014-04-17 12:42:41 +0200162 ret = (int) len;
163
164cleanup:
165 return( ret );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000166}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167#endif /* MBEDTLS_BIGNUM_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169int mbedtls_asn1_write_null( unsigned char **p, unsigned char *start )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000170{
171 int ret;
172 size_t len = 0;
173
174 // Write NULL
175 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, 0) );
177 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_NULL ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000178
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200179 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000180}
181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182int mbedtls_asn1_write_oid( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200183 const char *oid, size_t oid_len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000184{
185 int ret;
186 size_t len = 0;
187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200189 (const unsigned char *) oid, oid_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190 MBEDTLS_ASN1_CHK_ADD( len , mbedtls_asn1_write_len( p, start, len ) );
191 MBEDTLS_ASN1_CHK_ADD( len , mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OID ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000192
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200193 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000194}
195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196int mbedtls_asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start,
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200197 const char *oid, size_t oid_len,
198 size_t par_len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000199{
200 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000201 size_t len = 0;
202
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200203 if( par_len == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_null( p, start ) );
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200205 else
206 len += par_len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid, oid_len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
211 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
212 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000213
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200214 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000215}
216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217int mbedtls_asn1_write_bool( unsigned char **p, unsigned char *start, int boolean )
Paul Bakker329def32013-09-06 16:34:38 +0200218{
219 int ret;
220 size_t len = 0;
221
222 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakker329def32013-09-06 16:34:38 +0200224
Jonathan Leroy87c96c22015-10-14 09:41:56 +0200225 *--(*p) = (boolean) ? 255 : 0;
Paul Bakker329def32013-09-06 16:34:38 +0200226 len++;
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_BOOLEAN ) );
Paul Bakker329def32013-09-06 16:34:38 +0200230
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200231 return( (int) len );
Paul Bakker329def32013-09-06 16:34:38 +0200232}
233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234int mbedtls_asn1_write_int( unsigned char **p, unsigned char *start, int val )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000235{
236 int ret;
237 size_t len = 0;
238
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000239 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000241
242 len += 1;
243 *--(*p) = val;
244
Paul Bakker66d5d072014-06-17 16:39:18 +0200245 if( val > 0 && **p & 0x80 )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000246 {
247 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000249
250 *--(*p) = 0x00;
251 len += 1;
252 }
253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
255 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_INTEGER ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000256
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200257 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000258}
259
thomas-deeeba6c9b2018-09-19 09:10:37 +0200260int mbedtls_asn1_write_tagged_string( unsigned char **p, unsigned char *start, int tag,
Jaeden Amero23f954d2018-05-17 11:46:13 +0100261 const char *text, size_t text_len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000262{
263 int ret;
264 size_t len = 0;
265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
Jaeden Amero23f954d2018-05-17 11:46:13 +0100267 (const unsigned char *) text, text_len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
Jaeden Amero23f954d2018-05-17 11:46:13 +0100270 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, tag ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000271
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200272 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000273}
Paul Bakker598e4502013-08-25 14:46:39 +0200274
Jaeden Amero23f954d2018-05-17 11:46:13 +0100275int mbedtls_asn1_write_utf8_string( unsigned char **p, unsigned char *start,
276 const char *text, size_t text_len )
277{
thomas-deeeba6c9b2018-09-19 09:10:37 +0200278 return( mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_UTF8_STRING, text, text_len) );
Jaeden Amero23f954d2018-05-17 11:46:13 +0100279}
280
281int mbedtls_asn1_write_printable_string( unsigned char **p, unsigned char *start,
282 const char *text, size_t text_len )
283{
thomas-deeeba6c9b2018-09-19 09:10:37 +0200284 return( mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_PRINTABLE_STRING, text, text_len) );
Jaeden Amero23f954d2018-05-17 11:46:13 +0100285}
286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287int mbedtls_asn1_write_ia5_string( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200288 const char *text, size_t text_len )
Paul Bakker05888152012-02-16 10:26:57 +0000289{
thomas-deeeba6c9b2018-09-19 09:10:37 +0200290 return( mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_IA5_STRING, text, text_len) );
Paul Bakker05888152012-02-16 10:26:57 +0000291}
Paul Bakker598e4502013-08-25 14:46:39 +0200292
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100293int mbedtls_asn1_write_named_bitstring( unsigned char **p,
294 unsigned char *start,
295 const unsigned char *buf,
296 size_t bits )
297{
298 size_t unused_bits, byte_len;
299 const unsigned char *cur_byte;
300 unsigned char cur_byte_shifted;
301 unsigned char bit;
302
303 byte_len = ( bits + 7 ) / 8;
304 unused_bits = ( byte_len * 8 ) - bits;
305
306 /*
307 * Named bitstrings require that trailing 0s are excluded in the encoding
308 * of the bitstring. Trailing 0s are considered part of the 'unused' bits
309 * when encoding this value in the first content octet
310 */
311 if( bits != 0 )
312 {
313 cur_byte = buf + byte_len - 1;
314 cur_byte_shifted = *cur_byte >> unused_bits;
315
316 for( ; ; )
317 {
318 bit = cur_byte_shifted & 0x1;
319 cur_byte_shifted >>= 1;
320
321 if( bit != 0 )
322 break;
323
324 bits--;
325 if( bits == 0 )
326 break;
327
328 if( bits % 8 == 0 )
329 cur_byte_shifted = *--cur_byte;
330 }
331 }
332
333 return( mbedtls_asn1_write_bitstring( p, start, buf, bits ) );
334}
335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336int mbedtls_asn1_write_bitstring( unsigned char **p, unsigned char *start,
Paul Bakker598e4502013-08-25 14:46:39 +0200337 const unsigned char *buf, size_t bits )
338{
339 int ret;
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100340 size_t len = 0;
341 size_t unused_bits, byte_len;
Paul Bakker598e4502013-08-25 14:46:39 +0200342
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100343 byte_len = ( bits + 7 ) / 8;
344 unused_bits = ( byte_len * 8 ) - bits;
Paul Bakker598e4502013-08-25 14:46:39 +0200345
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100346 if( *p < start || (size_t)( *p - start ) < byte_len + 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakker598e4502013-08-25 14:46:39 +0200348
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100349 len = byte_len + 1;
Paul Bakker598e4502013-08-25 14:46:39 +0200350
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100351 /* Write the bitstring. Ensure the unused bits are zeroed */
352 if( byte_len > 0 )
353 {
354 byte_len--;
355 *--( *p ) = buf[byte_len] & ~( ( 0x1 << unused_bits ) - 1 );
356 ( *p ) -= byte_len;
357 memcpy( *p, buf, byte_len );
358 }
359
360 /* Write unused bits */
361 *--( *p ) = (unsigned char)unused_bits;
Paul Bakker598e4502013-08-25 14:46:39 +0200362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
364 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_BIT_STRING ) );
Paul Bakker598e4502013-08-25 14:46:39 +0200365
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200366 return( (int) len );
Paul Bakker598e4502013-08-25 14:46:39 +0200367}
368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369int mbedtls_asn1_write_octet_string( unsigned char **p, unsigned char *start,
Paul Bakker598e4502013-08-25 14:46:39 +0200370 const unsigned char *buf, size_t size )
371{
372 int ret;
373 size_t len = 0;
374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, buf, size ) );
Paul Bakker598e4502013-08-25 14:46:39 +0200376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
378 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OCTET_STRING ) );
Paul Bakker598e4502013-08-25 14:46:39 +0200379
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200380 return( (int) len );
Paul Bakker598e4502013-08-25 14:46:39 +0200381}
Paul Bakker59ba59f2013-09-09 11:26:00 +0200382
Hanno Becker44da18a2018-10-12 10:42:13 +0100383
384/* This is a copy of the ASN.1 parsing function mbedtls_asn1_find_named_data(),
385 * which is replicated to avoid a dependency ASN1_WRITE_C on ASN1_PARSE_C. */
386static mbedtls_asn1_named_data *asn1_find_named_data(
387 mbedtls_asn1_named_data *list,
388 const char *oid, size_t len )
389{
390 while( list != NULL )
391 {
392 if( list->oid.len == len &&
393 memcmp( list->oid.p, oid, len ) == 0 )
394 {
395 break;
396 }
397
398 list = list->next;
399 }
400
401 return( list );
402}
403
404mbedtls_asn1_named_data *mbedtls_asn1_store_named_data(
405 mbedtls_asn1_named_data **head,
Paul Bakker59ba59f2013-09-09 11:26:00 +0200406 const char *oid, size_t oid_len,
407 const unsigned char *val,
408 size_t val_len )
409{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410 mbedtls_asn1_named_data *cur;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200411
Hanno Becker44da18a2018-10-12 10:42:13 +0100412 if( ( cur = asn1_find_named_data( *head, oid, oid_len ) ) == NULL )
Paul Bakker59ba59f2013-09-09 11:26:00 +0200413 {
414 // Add new entry if not present yet based on OID
415 //
Simon Butcher29176892016-05-20 00:19:09 +0100416 cur = (mbedtls_asn1_named_data*)mbedtls_calloc( 1,
417 sizeof(mbedtls_asn1_named_data) );
418 if( cur == NULL )
Paul Bakker59ba59f2013-09-09 11:26:00 +0200419 return( NULL );
420
Paul Bakker59ba59f2013-09-09 11:26:00 +0200421 cur->oid.len = oid_len;
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200422 cur->oid.p = mbedtls_calloc( 1, oid_len );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200423 if( cur->oid.p == NULL )
424 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425 mbedtls_free( cur );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200426 return( NULL );
427 }
428
Manuel Pégourié-Gonnarde5b0fc12014-11-12 22:27:42 +0100429 memcpy( cur->oid.p, oid, oid_len );
430
Paul Bakker59ba59f2013-09-09 11:26:00 +0200431 cur->val.len = val_len;
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200432 cur->val.p = mbedtls_calloc( 1, val_len );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200433 if( cur->val.p == NULL )
434 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435 mbedtls_free( cur->oid.p );
436 mbedtls_free( cur );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200437 return( NULL );
438 }
439
Paul Bakker59ba59f2013-09-09 11:26:00 +0200440 cur->next = *head;
441 *head = cur;
442 }
443 else if( cur->val.len < val_len )
444 {
Manuel Pégourié-Gonnard97b52092015-12-10 10:50:51 +0100445 /*
446 * Enlarge existing value buffer if needed
447 * Preserve old data until the allocation succeeded, to leave list in
448 * a consistent state in case allocation fails.
449 */
450 void *p = mbedtls_calloc( 1, val_len );
451 if( p == NULL )
Paul Bakker59ba59f2013-09-09 11:26:00 +0200452 return( NULL );
Manuel Pégourié-Gonnard97b52092015-12-10 10:50:51 +0100453
454 mbedtls_free( cur->val.p );
455 cur->val.p = p;
456 cur->val.len = val_len;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200457 }
458
459 if( val != NULL )
460 memcpy( cur->val.p, val, val_len );
461
462 return( cur );
463}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464#endif /* MBEDTLS_ASN1_WRITE_C */