blob: 98c676672555985a6987f8638474e8a071f4fcf3 [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
Gilles Peskine1dbab672019-03-01 18:15:18 +0100239 do
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000240 {
241 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Gilles Peskine1dbab672019-03-01 18:15:18 +0100243 len += 1;
244 *--(*p) = val & 0xff;
245 val >>= 8;
246 }
247 while( val > 0 );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000248
Gilles Peskine1dbab672019-03-01 18:15:18 +0100249 if( **p & 0x80 )
250 {
251 if( *p - start < 1 )
252 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000253 *--(*p) = 0x00;
254 len += 1;
255 }
256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
258 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_INTEGER ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000259
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200260 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000261}
262
thomas-deeeba6c9b2018-09-19 09:10:37 +0200263int mbedtls_asn1_write_tagged_string( unsigned char **p, unsigned char *start, int tag,
Jaeden Amero23f954d2018-05-17 11:46:13 +0100264 const char *text, size_t text_len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000265{
266 int ret;
267 size_t len = 0;
268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
Jaeden Amero23f954d2018-05-17 11:46:13 +0100270 (const unsigned char *) text, text_len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
Jaeden Amero23f954d2018-05-17 11:46:13 +0100273 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, tag ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000274
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200275 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000276}
Paul Bakker598e4502013-08-25 14:46:39 +0200277
Jaeden Amero23f954d2018-05-17 11:46:13 +0100278int mbedtls_asn1_write_utf8_string( unsigned char **p, unsigned char *start,
279 const char *text, size_t text_len )
280{
thomas-deeeba6c9b2018-09-19 09:10:37 +0200281 return( mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_UTF8_STRING, text, text_len) );
Jaeden Amero23f954d2018-05-17 11:46:13 +0100282}
283
284int mbedtls_asn1_write_printable_string( unsigned char **p, unsigned char *start,
285 const char *text, size_t text_len )
286{
thomas-deeeba6c9b2018-09-19 09:10:37 +0200287 return( mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_PRINTABLE_STRING, text, text_len) );
Jaeden Amero23f954d2018-05-17 11:46:13 +0100288}
289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290int mbedtls_asn1_write_ia5_string( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200291 const char *text, size_t text_len )
Paul Bakker05888152012-02-16 10:26:57 +0000292{
thomas-deeeba6c9b2018-09-19 09:10:37 +0200293 return( mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_IA5_STRING, text, text_len) );
Paul Bakker05888152012-02-16 10:26:57 +0000294}
Paul Bakker598e4502013-08-25 14:46:39 +0200295
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100296int mbedtls_asn1_write_named_bitstring( unsigned char **p,
297 unsigned char *start,
298 const unsigned char *buf,
299 size_t bits )
300{
301 size_t unused_bits, byte_len;
302 const unsigned char *cur_byte;
303 unsigned char cur_byte_shifted;
304 unsigned char bit;
305
306 byte_len = ( bits + 7 ) / 8;
307 unused_bits = ( byte_len * 8 ) - bits;
308
309 /*
310 * Named bitstrings require that trailing 0s are excluded in the encoding
311 * of the bitstring. Trailing 0s are considered part of the 'unused' bits
312 * when encoding this value in the first content octet
313 */
314 if( bits != 0 )
315 {
316 cur_byte = buf + byte_len - 1;
317 cur_byte_shifted = *cur_byte >> unused_bits;
318
319 for( ; ; )
320 {
321 bit = cur_byte_shifted & 0x1;
322 cur_byte_shifted >>= 1;
323
324 if( bit != 0 )
325 break;
326
327 bits--;
328 if( bits == 0 )
329 break;
330
331 if( bits % 8 == 0 )
332 cur_byte_shifted = *--cur_byte;
333 }
334 }
335
336 return( mbedtls_asn1_write_bitstring( p, start, buf, bits ) );
337}
338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339int mbedtls_asn1_write_bitstring( unsigned char **p, unsigned char *start,
Paul Bakker598e4502013-08-25 14:46:39 +0200340 const unsigned char *buf, size_t bits )
341{
342 int ret;
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100343 size_t len = 0;
344 size_t unused_bits, byte_len;
Paul Bakker598e4502013-08-25 14:46:39 +0200345
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100346 byte_len = ( bits + 7 ) / 8;
347 unused_bits = ( byte_len * 8 ) - bits;
Paul Bakker598e4502013-08-25 14:46:39 +0200348
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100349 if( *p < start || (size_t)( *p - start ) < byte_len + 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakker598e4502013-08-25 14:46:39 +0200351
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100352 len = byte_len + 1;
Paul Bakker598e4502013-08-25 14:46:39 +0200353
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100354 /* Write the bitstring. Ensure the unused bits are zeroed */
355 if( byte_len > 0 )
356 {
357 byte_len--;
358 *--( *p ) = buf[byte_len] & ~( ( 0x1 << unused_bits ) - 1 );
359 ( *p ) -= byte_len;
360 memcpy( *p, buf, byte_len );
361 }
362
363 /* Write unused bits */
364 *--( *p ) = (unsigned char)unused_bits;
Paul Bakker598e4502013-08-25 14:46:39 +0200365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
367 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_BIT_STRING ) );
Paul Bakker598e4502013-08-25 14:46:39 +0200368
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200369 return( (int) len );
Paul Bakker598e4502013-08-25 14:46:39 +0200370}
371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372int mbedtls_asn1_write_octet_string( unsigned char **p, unsigned char *start,
Paul Bakker598e4502013-08-25 14:46:39 +0200373 const unsigned char *buf, size_t size )
374{
375 int ret;
376 size_t len = 0;
377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, buf, size ) );
Paul Bakker598e4502013-08-25 14:46:39 +0200379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
381 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OCTET_STRING ) );
Paul Bakker598e4502013-08-25 14:46:39 +0200382
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200383 return( (int) len );
Paul Bakker598e4502013-08-25 14:46:39 +0200384}
Paul Bakker59ba59f2013-09-09 11:26:00 +0200385
Hanno Becker44da18a2018-10-12 10:42:13 +0100386
387/* This is a copy of the ASN.1 parsing function mbedtls_asn1_find_named_data(),
388 * which is replicated to avoid a dependency ASN1_WRITE_C on ASN1_PARSE_C. */
389static mbedtls_asn1_named_data *asn1_find_named_data(
390 mbedtls_asn1_named_data *list,
391 const char *oid, size_t len )
392{
393 while( list != NULL )
394 {
395 if( list->oid.len == len &&
396 memcmp( list->oid.p, oid, len ) == 0 )
397 {
398 break;
399 }
400
401 list = list->next;
402 }
403
404 return( list );
405}
406
407mbedtls_asn1_named_data *mbedtls_asn1_store_named_data(
408 mbedtls_asn1_named_data **head,
Paul Bakker59ba59f2013-09-09 11:26:00 +0200409 const char *oid, size_t oid_len,
410 const unsigned char *val,
411 size_t val_len )
412{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 mbedtls_asn1_named_data *cur;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200414
Hanno Becker44da18a2018-10-12 10:42:13 +0100415 if( ( cur = asn1_find_named_data( *head, oid, oid_len ) ) == NULL )
Paul Bakker59ba59f2013-09-09 11:26:00 +0200416 {
417 // Add new entry if not present yet based on OID
418 //
Simon Butcher29176892016-05-20 00:19:09 +0100419 cur = (mbedtls_asn1_named_data*)mbedtls_calloc( 1,
420 sizeof(mbedtls_asn1_named_data) );
421 if( cur == NULL )
Paul Bakker59ba59f2013-09-09 11:26:00 +0200422 return( NULL );
423
Paul Bakker59ba59f2013-09-09 11:26:00 +0200424 cur->oid.len = oid_len;
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200425 cur->oid.p = mbedtls_calloc( 1, oid_len );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200426 if( cur->oid.p == NULL )
427 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428 mbedtls_free( cur );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200429 return( NULL );
430 }
431
Manuel Pégourié-Gonnarde5b0fc12014-11-12 22:27:42 +0100432 memcpy( cur->oid.p, oid, oid_len );
433
Paul Bakker59ba59f2013-09-09 11:26:00 +0200434 cur->val.len = val_len;
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200435 cur->val.p = mbedtls_calloc( 1, val_len );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200436 if( cur->val.p == NULL )
437 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438 mbedtls_free( cur->oid.p );
439 mbedtls_free( cur );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200440 return( NULL );
441 }
442
Paul Bakker59ba59f2013-09-09 11:26:00 +0200443 cur->next = *head;
444 *head = cur;
445 }
446 else if( cur->val.len < val_len )
447 {
Manuel Pégourié-Gonnard97b52092015-12-10 10:50:51 +0100448 /*
449 * Enlarge existing value buffer if needed
450 * Preserve old data until the allocation succeeded, to leave list in
451 * a consistent state in case allocation fails.
452 */
453 void *p = mbedtls_calloc( 1, val_len );
454 if( p == NULL )
Paul Bakker59ba59f2013-09-09 11:26:00 +0200455 return( NULL );
Manuel Pégourié-Gonnard97b52092015-12-10 10:50:51 +0100456
457 mbedtls_free( cur->val.p );
458 cur->val.p = p;
459 cur->val.len = val_len;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200460 }
461
462 if( val != NULL )
463 memcpy( cur->val.p, val, val_len );
464
465 return( cur );
466}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467#endif /* MBEDTLS_ASN1_WRITE_C */