blob: bd0d6af4d8289fa3f6bc2025b2836f1f983572f9 [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
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 * **********
45 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000046 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkerbdb912d2012-02-13 23:11:30 +000047 */
48
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020053#endif
Paul Bakkerbdb912d2012-02-13 23:11:30 +000054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_ASN1_WRITE_C)
Paul Bakkerbdb912d2012-02-13 23:11:30 +000056
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000057#include "mbedtls/asn1write.h"
Paul Bakkerbdb912d2012-02-13 23:11:30 +000058
Rich Evans00ab4702015-02-06 13:43:58 +000059#include <string.h>
60
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000062#include "mbedtls/platform.h"
Paul Bakker59ba59f2013-09-09 11:26:00 +020063#else
64#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020065#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066#define mbedtls_free free
Paul Bakker59ba59f2013-09-09 11:26:00 +020067#endif
68
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069int mbedtls_asn1_write_len( unsigned char **p, unsigned char *start, size_t len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +000070{
71 if( len < 0x80 )
72 {
73 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000075
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020076 *--(*p) = (unsigned char) len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000077 return( 1 );
78 }
79
80 if( len <= 0xFF )
81 {
82 if( *p - start < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000084
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020085 *--(*p) = (unsigned char) len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000086 *--(*p) = 0x81;
87 return( 2 );
88 }
89
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010090 if( len <= 0xFFFF )
91 {
92 if( *p - start < 3 )
93 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000094
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010095 *--(*p) = ( len ) & 0xFF;
96 *--(*p) = ( len >> 8 ) & 0xFF;
97 *--(*p) = 0x82;
98 return( 3 );
99 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000100
Paul Bakkerc7d6bd42016-07-14 11:39:56 +0100101 if( len <= 0xFFFFFF )
102 {
103 if( *p - start < 4 )
104 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
105
106 *--(*p) = ( len ) & 0xFF;
107 *--(*p) = ( len >> 8 ) & 0xFF;
108 *--(*p) = ( len >> 16 ) & 0xFF;
109 *--(*p) = 0x83;
110 return( 4 );
111 }
112
Azim Khan45b79cf2018-05-23 16:55:16 +0100113#if SIZE_MAX > 0xFFFFFFFF
Paul Bakkerc7d6bd42016-07-14 11:39:56 +0100114 if( len <= 0xFFFFFFFF )
Azim Khan45b79cf2018-05-23 16:55:16 +0100115#endif
Paul Bakkerc7d6bd42016-07-14 11:39:56 +0100116 {
117 if( *p - start < 5 )
118 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
119
120 *--(*p) = ( len ) & 0xFF;
121 *--(*p) = ( len >> 8 ) & 0xFF;
122 *--(*p) = ( len >> 16 ) & 0xFF;
123 *--(*p) = ( len >> 24 ) & 0xFF;
124 *--(*p) = 0x84;
125 return( 5 );
126 }
127
Azim Khan45b79cf2018-05-23 16:55:16 +0100128#if SIZE_MAX > 0xFFFFFFFF
Paul Bakkerc7d6bd42016-07-14 11:39:56 +0100129 return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Azim Khan45b79cf2018-05-23 16:55:16 +0100130#endif
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000131}
132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133int mbedtls_asn1_write_tag( unsigned char **p, unsigned char *start, unsigned char tag )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000134{
135 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000137
138 *--(*p) = tag;
139
140 return( 1 );
141}
142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143int mbedtls_asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
Paul Bakker9852d002013-08-26 17:56:37 +0200144 const unsigned char *buf, size_t size )
145{
146 size_t len = 0;
147
Manuel Pégourié-Gonnard22c3b7b2015-10-21 12:07:47 +0200148 if( *p < start || (size_t)( *p - start ) < size )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakker9852d002013-08-26 17:56:37 +0200150
151 len = size;
152 (*p) -= len;
153 memcpy( *p, buf, len );
154
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200155 return( (int) len );
Paul Bakker9852d002013-08-26 17:56:37 +0200156}
157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158#if defined(MBEDTLS_BIGNUM_C)
159int mbedtls_asn1_write_mpi( unsigned char **p, unsigned char *start, const mbedtls_mpi *X )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000160{
161 int ret;
162 size_t len = 0;
163
164 // Write the MPI
165 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 len = mbedtls_mpi_size( X );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000167
Manuel Pégourié-Gonnard22c3b7b2015-10-21 12:07:47 +0200168 if( *p < start || (size_t)( *p - start ) < len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000170
171 (*p) -= len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( X, *p, len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000173
174 // DER format assumes 2s complement for numbers, so the leftmost bit
175 // should be 0 for positive numbers and 1 for negative numbers.
176 //
Paul Bakker66d5d072014-06-17 16:39:18 +0200177 if( X->s ==1 && **p & 0x80 )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000178 {
179 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000181
182 *--(*p) = 0x00;
183 len += 1;
184 }
185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
187 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_INTEGER ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000188
Paul Bakker3d8fb632014-04-17 12:42:41 +0200189 ret = (int) len;
190
191cleanup:
192 return( ret );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000193}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194#endif /* MBEDTLS_BIGNUM_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196int mbedtls_asn1_write_null( unsigned char **p, unsigned char *start )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000197{
198 int ret;
199 size_t len = 0;
200
201 // Write NULL
202 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, 0) );
204 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_NULL ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000205
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200206 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000207}
208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209int mbedtls_asn1_write_oid( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200210 const char *oid, size_t oid_len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000211{
212 int ret;
213 size_t len = 0;
214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200216 (const unsigned char *) oid, oid_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 MBEDTLS_ASN1_CHK_ADD( len , mbedtls_asn1_write_len( p, start, len ) );
218 MBEDTLS_ASN1_CHK_ADD( len , mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OID ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000219
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200220 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000221}
222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223int mbedtls_asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start,
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200224 const char *oid, size_t oid_len,
225 size_t par_len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000226{
227 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000228 size_t len = 0;
229
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200230 if( par_len == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_null( p, start ) );
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200232 else
233 len += par_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_oid( p, start, oid, oid_len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
238 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
239 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000240
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200241 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000242}
243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244int mbedtls_asn1_write_bool( unsigned char **p, unsigned char *start, int boolean )
Paul Bakker329def32013-09-06 16:34:38 +0200245{
246 int ret;
247 size_t len = 0;
248
249 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakker329def32013-09-06 16:34:38 +0200251
Jonathan Leroy87c96c22015-10-14 09:41:56 +0200252 *--(*p) = (boolean) ? 255 : 0;
Paul Bakker329def32013-09-06 16:34:38 +0200253 len++;
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_BOOLEAN ) );
Paul Bakker329def32013-09-06 16:34:38 +0200257
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200258 return( (int) len );
Paul Bakker329def32013-09-06 16:34:38 +0200259}
260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261int mbedtls_asn1_write_int( unsigned char **p, unsigned char *start, int val )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000262{
263 int ret;
264 size_t len = 0;
265
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000266 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000268
269 len += 1;
270 *--(*p) = val;
271
Paul Bakker66d5d072014-06-17 16:39:18 +0200272 if( val > 0 && **p & 0x80 )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000273 {
274 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000276
277 *--(*p) = 0x00;
278 len += 1;
279 }
280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
282 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_INTEGER ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000283
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200284 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000285}
286
thomas-deeeba6c9b2018-09-19 09:10:37 +0200287int mbedtls_asn1_write_tagged_string( unsigned char **p, unsigned char *start, int tag,
Jaeden Amero23f954d2018-05-17 11:46:13 +0100288 const char *text, size_t text_len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000289{
290 int ret;
291 size_t len = 0;
292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
Jaeden Amero23f954d2018-05-17 11:46:13 +0100294 (const unsigned char *) text, text_len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
Jaeden Amero23f954d2018-05-17 11:46:13 +0100297 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, tag ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000298
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200299 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000300}
Paul Bakker598e4502013-08-25 14:46:39 +0200301
Jaeden Amero23f954d2018-05-17 11:46:13 +0100302int mbedtls_asn1_write_utf8_string( unsigned char **p, unsigned char *start,
303 const char *text, size_t text_len )
304{
thomas-deeeba6c9b2018-09-19 09:10:37 +0200305 return( mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_UTF8_STRING, text, text_len) );
Jaeden Amero23f954d2018-05-17 11:46:13 +0100306}
307
308int mbedtls_asn1_write_printable_string( unsigned char **p, unsigned char *start,
309 const char *text, size_t text_len )
310{
thomas-deeeba6c9b2018-09-19 09:10:37 +0200311 return( mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_PRINTABLE_STRING, text, text_len) );
Jaeden Amero23f954d2018-05-17 11:46:13 +0100312}
313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314int mbedtls_asn1_write_ia5_string( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200315 const char *text, size_t text_len )
Paul Bakker05888152012-02-16 10:26:57 +0000316{
thomas-deeeba6c9b2018-09-19 09:10:37 +0200317 return( mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_IA5_STRING, text, text_len) );
Paul Bakker05888152012-02-16 10:26:57 +0000318}
Paul Bakker598e4502013-08-25 14:46:39 +0200319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320int mbedtls_asn1_write_bitstring( unsigned char **p, unsigned char *start,
Paul Bakker598e4502013-08-25 14:46:39 +0200321 const unsigned char *buf, size_t bits )
322{
323 int ret;
Andres Amaya Garciad60e3782018-09-26 10:48:24 +0100324 size_t len = 0;
325 size_t unused_bits, byte_len;
Paul Bakker598e4502013-08-25 14:46:39 +0200326
Andres Amaya Garciad60e3782018-09-26 10:48:24 +0100327 byte_len = ( bits + 7 ) / 8;
328 unused_bits = ( byte_len * 8 ) - bits;
Paul Bakker598e4502013-08-25 14:46:39 +0200329
Andres Amaya Garciad60e3782018-09-26 10:48:24 +0100330 if( *p < start || (size_t)( *p - start ) < byte_len + 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakker598e4502013-08-25 14:46:39 +0200332
Andres Amaya Garciad60e3782018-09-26 10:48:24 +0100333 len = byte_len + 1;
Paul Bakker598e4502013-08-25 14:46:39 +0200334
Andres Amaya Garciad60e3782018-09-26 10:48:24 +0100335 /* Write the bitstring. Ensure the unused bits are zeroed */
336 if( byte_len > 0 )
337 {
338 byte_len--;
339 *--( *p ) = buf[byte_len] & ~( ( 0x1 << unused_bits ) - 1 );
340 ( *p ) -= byte_len;
341 memcpy( *p, buf, byte_len );
342 }
343
344 /* Write unused bits */
345 *--( *p ) = (unsigned char)unused_bits;
Paul Bakker598e4502013-08-25 14:46:39 +0200346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
348 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_BIT_STRING ) );
Paul Bakker598e4502013-08-25 14:46:39 +0200349
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200350 return( (int) len );
Paul Bakker598e4502013-08-25 14:46:39 +0200351}
352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353int mbedtls_asn1_write_octet_string( unsigned char **p, unsigned char *start,
Paul Bakker598e4502013-08-25 14:46:39 +0200354 const unsigned char *buf, size_t size )
355{
356 int ret;
357 size_t len = 0;
358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, buf, size ) );
Paul Bakker598e4502013-08-25 14:46:39 +0200360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
362 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OCTET_STRING ) );
Paul Bakker598e4502013-08-25 14:46:39 +0200363
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200364 return( (int) len );
Paul Bakker598e4502013-08-25 14:46:39 +0200365}
Paul Bakker59ba59f2013-09-09 11:26:00 +0200366
Hanno Becker44da18a2018-10-12 10:42:13 +0100367
368/* This is a copy of the ASN.1 parsing function mbedtls_asn1_find_named_data(),
369 * which is replicated to avoid a dependency ASN1_WRITE_C on ASN1_PARSE_C. */
370static mbedtls_asn1_named_data *asn1_find_named_data(
371 mbedtls_asn1_named_data *list,
372 const char *oid, size_t len )
373{
374 while( list != NULL )
375 {
376 if( list->oid.len == len &&
377 memcmp( list->oid.p, oid, len ) == 0 )
378 {
379 break;
380 }
381
382 list = list->next;
383 }
384
385 return( list );
386}
387
388mbedtls_asn1_named_data *mbedtls_asn1_store_named_data(
389 mbedtls_asn1_named_data **head,
Paul Bakker59ba59f2013-09-09 11:26:00 +0200390 const char *oid, size_t oid_len,
391 const unsigned char *val,
392 size_t val_len )
393{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394 mbedtls_asn1_named_data *cur;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200395
Hanno Becker44da18a2018-10-12 10:42:13 +0100396 if( ( cur = asn1_find_named_data( *head, oid, oid_len ) ) == NULL )
Paul Bakker59ba59f2013-09-09 11:26:00 +0200397 {
398 // Add new entry if not present yet based on OID
399 //
Simon Butcher29176892016-05-20 00:19:09 +0100400 cur = (mbedtls_asn1_named_data*)mbedtls_calloc( 1,
401 sizeof(mbedtls_asn1_named_data) );
402 if( cur == NULL )
Paul Bakker59ba59f2013-09-09 11:26:00 +0200403 return( NULL );
404
Paul Bakker59ba59f2013-09-09 11:26:00 +0200405 cur->oid.len = oid_len;
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200406 cur->oid.p = mbedtls_calloc( 1, oid_len );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200407 if( cur->oid.p == NULL )
408 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 mbedtls_free( cur );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200410 return( NULL );
411 }
412
Manuel Pégourié-Gonnarde5b0fc12014-11-12 22:27:42 +0100413 memcpy( cur->oid.p, oid, oid_len );
414
Paul Bakker59ba59f2013-09-09 11:26:00 +0200415 cur->val.len = val_len;
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200416 cur->val.p = mbedtls_calloc( 1, val_len );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200417 if( cur->val.p == NULL )
418 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419 mbedtls_free( cur->oid.p );
420 mbedtls_free( cur );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200421 return( NULL );
422 }
423
Paul Bakker59ba59f2013-09-09 11:26:00 +0200424 cur->next = *head;
425 *head = cur;
426 }
427 else if( cur->val.len < val_len )
428 {
Manuel Pégourié-Gonnard97b52092015-12-10 10:50:51 +0100429 /*
430 * Enlarge existing value buffer if needed
431 * Preserve old data until the allocation succeeded, to leave list in
432 * a consistent state in case allocation fails.
433 */
434 void *p = mbedtls_calloc( 1, val_len );
435 if( p == NULL )
Paul Bakker59ba59f2013-09-09 11:26:00 +0200436 return( NULL );
Manuel Pégourié-Gonnard97b52092015-12-10 10:50:51 +0100437
438 mbedtls_free( cur->val.p );
439 cur->val.p = p;
440 cur->val.len = val_len;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200441 }
442
443 if( val != NULL )
444 memcpy( cur->val.p, val, val_len );
445
446 return( cur );
447}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448#endif /* MBEDTLS_ASN1_WRITE_C */