blob: d94d0a760550552ccf5dbff8beaa36cc78adfd1b [file] [log] [blame]
Paul Bakkerbdb912d2012-02-13 23:11:30 +00001/*
2 * ASN.1 buffer writing functionality
3 *
Bence Szépkútia2947ac2020-08-19 16:37:36 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakkerbdb912d2012-02-13 23:11:30 +000024 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
Paul Bakkerbdb912d2012-02-13 23:11:30 +000045 */
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020049#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#endif
Paul Bakkerbdb912d2012-02-13 23:11:30 +000052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if defined(MBEDTLS_ASN1_WRITE_C)
Paul Bakkerbdb912d2012-02-13 23:11:30 +000054
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/asn1write.h"
Paul Bakkerbdb912d2012-02-13 23:11:30 +000056
Rich Evans00ab4702015-02-06 13:43:58 +000057#include <string.h>
58
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000060#include "mbedtls/platform.h"
Paul Bakker59ba59f2013-09-09 11:26:00 +020061#else
62#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020063#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#define mbedtls_free free
Paul Bakker59ba59f2013-09-09 11:26:00 +020065#endif
66
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067int mbedtls_asn1_write_len( unsigned char **p, unsigned char *start, size_t len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +000068{
69 if( len < 0x80 )
70 {
71 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000073
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020074 *--(*p) = (unsigned char) len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000075 return( 1 );
76 }
77
78 if( len <= 0xFF )
79 {
80 if( *p - start < 2 )
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
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020083 *--(*p) = (unsigned char) len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000084 *--(*p) = 0x81;
85 return( 2 );
86 }
87
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010088 if( len <= 0xFFFF )
89 {
90 if( *p - start < 3 )
91 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000092
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010093 *--(*p) = ( len ) & 0xFF;
94 *--(*p) = ( len >> 8 ) & 0xFF;
95 *--(*p) = 0x82;
96 return( 3 );
97 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +000098
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010099 if( len <= 0xFFFFFF )
100 {
101 if( *p - start < 4 )
102 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
103
104 *--(*p) = ( len ) & 0xFF;
105 *--(*p) = ( len >> 8 ) & 0xFF;
106 *--(*p) = ( len >> 16 ) & 0xFF;
107 *--(*p) = 0x83;
108 return( 4 );
109 }
110
Azim Khan45b79cf2018-05-23 16:55:16 +0100111#if SIZE_MAX > 0xFFFFFFFF
Paul Bakkerc7d6bd42016-07-14 11:39:56 +0100112 if( len <= 0xFFFFFFFF )
Azim Khan45b79cf2018-05-23 16:55:16 +0100113#endif
Paul Bakkerc7d6bd42016-07-14 11:39:56 +0100114 {
115 if( *p - start < 5 )
116 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
117
118 *--(*p) = ( len ) & 0xFF;
119 *--(*p) = ( len >> 8 ) & 0xFF;
120 *--(*p) = ( len >> 16 ) & 0xFF;
121 *--(*p) = ( len >> 24 ) & 0xFF;
122 *--(*p) = 0x84;
123 return( 5 );
124 }
125
Azim Khan45b79cf2018-05-23 16:55:16 +0100126#if SIZE_MAX > 0xFFFFFFFF
Paul Bakkerc7d6bd42016-07-14 11:39:56 +0100127 return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Azim Khan45b79cf2018-05-23 16:55:16 +0100128#endif
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000129}
130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131int mbedtls_asn1_write_tag( unsigned char **p, unsigned char *start, unsigned char tag )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000132{
133 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000135
136 *--(*p) = tag;
137
138 return( 1 );
139}
140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141int mbedtls_asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
Paul Bakker9852d002013-08-26 17:56:37 +0200142 const unsigned char *buf, size_t size )
143{
144 size_t len = 0;
145
Manuel Pégourié-Gonnard22c3b7b2015-10-21 12:07:47 +0200146 if( *p < start || (size_t)( *p - start ) < size )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakker9852d002013-08-26 17:56:37 +0200148
149 len = size;
150 (*p) -= len;
151 memcpy( *p, buf, len );
152
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200153 return( (int) len );
Paul Bakker9852d002013-08-26 17:56:37 +0200154}
155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156#if defined(MBEDTLS_BIGNUM_C)
157int mbedtls_asn1_write_mpi( unsigned char **p, unsigned char *start, const mbedtls_mpi *X )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000158{
159 int ret;
160 size_t len = 0;
161
162 // Write the MPI
163 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164 len = mbedtls_mpi_size( X );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000165
Manuel Pégourié-Gonnard22c3b7b2015-10-21 12:07:47 +0200166 if( *p < start || (size_t)( *p - start ) < len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000168
169 (*p) -= len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( X, *p, len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000171
172 // DER format assumes 2s complement for numbers, so the leftmost bit
173 // should be 0 for positive numbers and 1 for negative numbers.
174 //
Paul Bakker66d5d072014-06-17 16:39:18 +0200175 if( X->s ==1 && **p & 0x80 )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000176 {
177 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000179
180 *--(*p) = 0x00;
181 len += 1;
182 }
183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
185 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_INTEGER ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000186
Paul Bakker3d8fb632014-04-17 12:42:41 +0200187 ret = (int) len;
188
189cleanup:
190 return( ret );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000191}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192#endif /* MBEDTLS_BIGNUM_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194int mbedtls_asn1_write_null( unsigned char **p, unsigned char *start )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000195{
196 int ret;
197 size_t len = 0;
198
199 // Write NULL
200 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, 0) );
202 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_NULL ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000203
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200204 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000205}
206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207int mbedtls_asn1_write_oid( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200208 const char *oid, size_t oid_len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000209{
210 int ret;
211 size_t len = 0;
212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200214 (const unsigned char *) oid, oid_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 MBEDTLS_ASN1_CHK_ADD( len , mbedtls_asn1_write_len( p, start, len ) );
216 MBEDTLS_ASN1_CHK_ADD( len , mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OID ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000217
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200218 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000219}
220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221int mbedtls_asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start,
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200222 const char *oid, size_t oid_len,
223 size_t par_len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000224{
225 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000226 size_t len = 0;
227
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200228 if( par_len == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_null( p, start ) );
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200230 else
231 len += par_len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid, oid_len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
236 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
237 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000238
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200239 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000240}
241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242int mbedtls_asn1_write_bool( unsigned char **p, unsigned char *start, int boolean )
Paul Bakker329def32013-09-06 16:34:38 +0200243{
244 int ret;
245 size_t len = 0;
246
247 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakker329def32013-09-06 16:34:38 +0200249
Jonathan Leroy87c96c22015-10-14 09:41:56 +0200250 *--(*p) = (boolean) ? 255 : 0;
Paul Bakker329def32013-09-06 16:34:38 +0200251 len++;
252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
254 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_BOOLEAN ) );
Paul Bakker329def32013-09-06 16:34:38 +0200255
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200256 return( (int) len );
Paul Bakker329def32013-09-06 16:34:38 +0200257}
258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259int mbedtls_asn1_write_int( unsigned char **p, unsigned char *start, int val )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000260{
261 int ret;
262 size_t len = 0;
263
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000264 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000266
267 len += 1;
268 *--(*p) = val;
269
Paul Bakker66d5d072014-06-17 16:39:18 +0200270 if( val > 0 && **p & 0x80 )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000271 {
272 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000274
275 *--(*p) = 0x00;
276 len += 1;
277 }
278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
280 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_INTEGER ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000281
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200282 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000283}
284
thomas-deeeba6c9b2018-09-19 09:10:37 +0200285int mbedtls_asn1_write_tagged_string( unsigned char **p, unsigned char *start, int tag,
Jaeden Amero23f954d2018-05-17 11:46:13 +0100286 const char *text, size_t text_len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000287{
288 int ret;
289 size_t len = 0;
290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
Jaeden Amero23f954d2018-05-17 11:46:13 +0100292 (const unsigned char *) text, text_len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
Jaeden Amero23f954d2018-05-17 11:46:13 +0100295 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, tag ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000296
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200297 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000298}
Paul Bakker598e4502013-08-25 14:46:39 +0200299
Jaeden Amero23f954d2018-05-17 11:46:13 +0100300int mbedtls_asn1_write_utf8_string( unsigned char **p, unsigned char *start,
301 const char *text, size_t text_len )
302{
thomas-deeeba6c9b2018-09-19 09:10:37 +0200303 return( mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_UTF8_STRING, text, text_len) );
Jaeden Amero23f954d2018-05-17 11:46:13 +0100304}
305
306int mbedtls_asn1_write_printable_string( unsigned char **p, unsigned char *start,
307 const char *text, size_t text_len )
308{
thomas-deeeba6c9b2018-09-19 09:10:37 +0200309 return( mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_PRINTABLE_STRING, text, text_len) );
Jaeden Amero23f954d2018-05-17 11:46:13 +0100310}
311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312int mbedtls_asn1_write_ia5_string( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200313 const char *text, size_t text_len )
Paul Bakker05888152012-02-16 10:26:57 +0000314{
thomas-deeeba6c9b2018-09-19 09:10:37 +0200315 return( mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_IA5_STRING, text, text_len) );
Paul Bakker05888152012-02-16 10:26:57 +0000316}
Paul Bakker598e4502013-08-25 14:46:39 +0200317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318int mbedtls_asn1_write_bitstring( unsigned char **p, unsigned char *start,
Paul Bakker598e4502013-08-25 14:46:39 +0200319 const unsigned char *buf, size_t bits )
320{
321 int ret;
Andres Amaya Garciad60e3782018-09-26 10:48:24 +0100322 size_t len = 0;
323 size_t unused_bits, byte_len;
Paul Bakker598e4502013-08-25 14:46:39 +0200324
Andres Amaya Garciad60e3782018-09-26 10:48:24 +0100325 byte_len = ( bits + 7 ) / 8;
326 unused_bits = ( byte_len * 8 ) - bits;
Paul Bakker598e4502013-08-25 14:46:39 +0200327
Andres Amaya Garciad60e3782018-09-26 10:48:24 +0100328 if( *p < start || (size_t)( *p - start ) < byte_len + 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakker598e4502013-08-25 14:46:39 +0200330
Andres Amaya Garciad60e3782018-09-26 10:48:24 +0100331 len = byte_len + 1;
Paul Bakker598e4502013-08-25 14:46:39 +0200332
Andres Amaya Garciad60e3782018-09-26 10:48:24 +0100333 /* Write the bitstring. Ensure the unused bits are zeroed */
334 if( byte_len > 0 )
335 {
336 byte_len--;
337 *--( *p ) = buf[byte_len] & ~( ( 0x1 << unused_bits ) - 1 );
338 ( *p ) -= byte_len;
339 memcpy( *p, buf, byte_len );
340 }
341
342 /* Write unused bits */
343 *--( *p ) = (unsigned char)unused_bits;
Paul Bakker598e4502013-08-25 14:46:39 +0200344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
346 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_BIT_STRING ) );
Paul Bakker598e4502013-08-25 14:46:39 +0200347
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200348 return( (int) len );
Paul Bakker598e4502013-08-25 14:46:39 +0200349}
350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351int mbedtls_asn1_write_octet_string( unsigned char **p, unsigned char *start,
Paul Bakker598e4502013-08-25 14:46:39 +0200352 const unsigned char *buf, size_t size )
353{
354 int ret;
355 size_t len = 0;
356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, buf, size ) );
Paul Bakker598e4502013-08-25 14:46:39 +0200358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
360 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OCTET_STRING ) );
Paul Bakker598e4502013-08-25 14:46:39 +0200361
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200362 return( (int) len );
Paul Bakker598e4502013-08-25 14:46:39 +0200363}
Paul Bakker59ba59f2013-09-09 11:26:00 +0200364
Hanno Becker44da18a2018-10-12 10:42:13 +0100365
366/* This is a copy of the ASN.1 parsing function mbedtls_asn1_find_named_data(),
367 * which is replicated to avoid a dependency ASN1_WRITE_C on ASN1_PARSE_C. */
368static mbedtls_asn1_named_data *asn1_find_named_data(
369 mbedtls_asn1_named_data *list,
370 const char *oid, size_t len )
371{
372 while( list != NULL )
373 {
374 if( list->oid.len == len &&
375 memcmp( list->oid.p, oid, len ) == 0 )
376 {
377 break;
378 }
379
380 list = list->next;
381 }
382
383 return( list );
384}
385
386mbedtls_asn1_named_data *mbedtls_asn1_store_named_data(
387 mbedtls_asn1_named_data **head,
Paul Bakker59ba59f2013-09-09 11:26:00 +0200388 const char *oid, size_t oid_len,
389 const unsigned char *val,
390 size_t val_len )
391{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392 mbedtls_asn1_named_data *cur;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200393
Hanno Becker44da18a2018-10-12 10:42:13 +0100394 if( ( cur = asn1_find_named_data( *head, oid, oid_len ) ) == NULL )
Paul Bakker59ba59f2013-09-09 11:26:00 +0200395 {
396 // Add new entry if not present yet based on OID
397 //
Simon Butcher29176892016-05-20 00:19:09 +0100398 cur = (mbedtls_asn1_named_data*)mbedtls_calloc( 1,
399 sizeof(mbedtls_asn1_named_data) );
400 if( cur == NULL )
Paul Bakker59ba59f2013-09-09 11:26:00 +0200401 return( NULL );
402
Paul Bakker59ba59f2013-09-09 11:26:00 +0200403 cur->oid.len = oid_len;
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200404 cur->oid.p = mbedtls_calloc( 1, oid_len );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200405 if( cur->oid.p == NULL )
406 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407 mbedtls_free( cur );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200408 return( NULL );
409 }
410
Manuel Pégourié-Gonnarde5b0fc12014-11-12 22:27:42 +0100411 memcpy( cur->oid.p, oid, oid_len );
412
Paul Bakker59ba59f2013-09-09 11:26:00 +0200413 cur->val.len = val_len;
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200414 cur->val.p = mbedtls_calloc( 1, val_len );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200415 if( cur->val.p == NULL )
416 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417 mbedtls_free( cur->oid.p );
418 mbedtls_free( cur );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200419 return( NULL );
420 }
421
Paul Bakker59ba59f2013-09-09 11:26:00 +0200422 cur->next = *head;
423 *head = cur;
424 }
425 else if( cur->val.len < val_len )
426 {
Manuel Pégourié-Gonnard97b52092015-12-10 10:50:51 +0100427 /*
428 * Enlarge existing value buffer if needed
429 * Preserve old data until the allocation succeeded, to leave list in
430 * a consistent state in case allocation fails.
431 */
432 void *p = mbedtls_calloc( 1, val_len );
433 if( p == NULL )
Paul Bakker59ba59f2013-09-09 11:26:00 +0200434 return( NULL );
Manuel Pégourié-Gonnard97b52092015-12-10 10:50:51 +0100435
436 mbedtls_free( cur->val.p );
437 cur->val.p = p;
438 cur->val.len = val_len;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200439 }
440
441 if( val != NULL )
442 memcpy( cur->val.p, val, val_len );
443
444 return( cur );
445}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446#endif /* MBEDTLS_ASN1_WRITE_C */