blob: deb1a2ff659871ed73c41bd364e7e63191d1c897 [file] [log] [blame]
Paul Bakkerbdb912d2012-02-13 23:11:30 +00001/*
2 * ASN.1 buffer writing functionality
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
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 */
19
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Paul Bakkerbdb912d2012-02-13 23:11:30 +000021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_ASN1_WRITE_C)
Paul Bakkerbdb912d2012-02-13 23:11:30 +000023
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/asn1write.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000025#include "mbedtls/error.h"
Paul Bakkerbdb912d2012-02-13 23:11:30 +000026
Rich Evans00ab4702015-02-06 13:43:58 +000027#include <string.h>
28
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/platform.h"
Paul Bakker59ba59f2013-09-09 11:26:00 +020031#else
32#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020033#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#define mbedtls_free free
Paul Bakker59ba59f2013-09-09 11:26:00 +020035#endif
36
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037int mbedtls_asn1_write_len( unsigned char **p, unsigned char *start, size_t len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +000038{
39 if( len < 0x80 )
40 {
41 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000043
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020044 *--(*p) = (unsigned char) len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000045 return( 1 );
46 }
47
48 if( len <= 0xFF )
49 {
50 if( *p - start < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000052
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020053 *--(*p) = (unsigned char) len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000054 *--(*p) = 0x81;
55 return( 2 );
56 }
57
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010058 if( len <= 0xFFFF )
59 {
60 if( *p - start < 3 )
61 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000062
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010063 *--(*p) = ( len ) & 0xFF;
64 *--(*p) = ( len >> 8 ) & 0xFF;
65 *--(*p) = 0x82;
66 return( 3 );
67 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +000068
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010069 if( len <= 0xFFFFFF )
70 {
71 if( *p - start < 4 )
72 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
73
74 *--(*p) = ( len ) & 0xFF;
75 *--(*p) = ( len >> 8 ) & 0xFF;
76 *--(*p) = ( len >> 16 ) & 0xFF;
77 *--(*p) = 0x83;
78 return( 4 );
79 }
80
Azim Khan45b79cf2018-05-23 16:55:16 +010081#if SIZE_MAX > 0xFFFFFFFF
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010082 if( len <= 0xFFFFFFFF )
Azim Khan45b79cf2018-05-23 16:55:16 +010083#endif
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010084 {
85 if( *p - start < 5 )
86 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
87
88 *--(*p) = ( len ) & 0xFF;
89 *--(*p) = ( len >> 8 ) & 0xFF;
90 *--(*p) = ( len >> 16 ) & 0xFF;
91 *--(*p) = ( len >> 24 ) & 0xFF;
92 *--(*p) = 0x84;
93 return( 5 );
94 }
95
Azim Khan45b79cf2018-05-23 16:55:16 +010096#if SIZE_MAX > 0xFFFFFFFF
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010097 return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Azim Khan45b79cf2018-05-23 16:55:16 +010098#endif
Paul Bakkerbdb912d2012-02-13 23:11:30 +000099}
100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101int mbedtls_asn1_write_tag( unsigned char **p, unsigned char *start, unsigned char tag )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000102{
103 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000105
106 *--(*p) = tag;
107
108 return( 1 );
109}
110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111int mbedtls_asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
Paul Bakker9852d002013-08-26 17:56:37 +0200112 const unsigned char *buf, size_t size )
113{
114 size_t len = 0;
115
Manuel Pégourié-Gonnard22c3b7b2015-10-21 12:07:47 +0200116 if( *p < start || (size_t)( *p - start ) < size )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakker9852d002013-08-26 17:56:37 +0200118
119 len = size;
120 (*p) -= len;
121 memcpy( *p, buf, len );
122
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200123 return( (int) len );
Paul Bakker9852d002013-08-26 17:56:37 +0200124}
125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126#if defined(MBEDTLS_BIGNUM_C)
127int mbedtls_asn1_write_mpi( unsigned char **p, unsigned char *start, const mbedtls_mpi *X )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000128{
Janos Follath24eed8d2019-11-22 13:21:35 +0000129 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000130 size_t len = 0;
131
132 // Write the MPI
133 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 len = mbedtls_mpi_size( X );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000135
Manuel Pégourié-Gonnard22c3b7b2015-10-21 12:07:47 +0200136 if( *p < start || (size_t)( *p - start ) < len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000138
139 (*p) -= len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( X, *p, len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000141
142 // DER format assumes 2s complement for numbers, so the leftmost bit
143 // should be 0 for positive numbers and 1 for negative numbers.
144 //
Paul Bakker66d5d072014-06-17 16:39:18 +0200145 if( X->s ==1 && **p & 0x80 )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000146 {
147 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000149
150 *--(*p) = 0x00;
151 len += 1;
152 }
153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
155 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_INTEGER ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000156
Paul Bakker3d8fb632014-04-17 12:42:41 +0200157 ret = (int) len;
158
159cleanup:
160 return( ret );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000161}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162#endif /* MBEDTLS_BIGNUM_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164int mbedtls_asn1_write_null( unsigned char **p, unsigned char *start )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000165{
Janos Follath24eed8d2019-11-22 13:21:35 +0000166 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000167 size_t len = 0;
168
169 // Write NULL
170 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, 0) );
172 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_NULL ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000173
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200174 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000175}
176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177int mbedtls_asn1_write_oid( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200178 const char *oid, size_t oid_len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000179{
Janos Follath24eed8d2019-11-22 13:21:35 +0000180 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000181 size_t len = 0;
182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200184 (const unsigned char *) oid, oid_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 MBEDTLS_ASN1_CHK_ADD( len , mbedtls_asn1_write_len( p, start, len ) );
186 MBEDTLS_ASN1_CHK_ADD( len , mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OID ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000187
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200188 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000189}
190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191int mbedtls_asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start,
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200192 const char *oid, size_t oid_len,
193 size_t par_len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000194{
Janos Follath24eed8d2019-11-22 13:21:35 +0000195 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000196 size_t len = 0;
197
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200198 if( par_len == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_null( p, start ) );
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200200 else
201 len += par_len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid, oid_len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
206 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
207 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000208
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200209 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000210}
211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212int mbedtls_asn1_write_bool( unsigned char **p, unsigned char *start, int boolean )
Paul Bakker329def32013-09-06 16:34:38 +0200213{
Janos Follath24eed8d2019-11-22 13:21:35 +0000214 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker329def32013-09-06 16:34:38 +0200215 size_t len = 0;
216
217 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakker329def32013-09-06 16:34:38 +0200219
Jonathan Leroy87c96c22015-10-14 09:41:56 +0200220 *--(*p) = (boolean) ? 255 : 0;
Paul Bakker329def32013-09-06 16:34:38 +0200221 len++;
222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
224 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_BOOLEAN ) );
Paul Bakker329def32013-09-06 16:34:38 +0200225
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200226 return( (int) len );
Paul Bakker329def32013-09-06 16:34:38 +0200227}
228
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200229static int asn1_write_tagged_int( unsigned char **p, unsigned char *start, int val, int tag )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000230{
Janos Follath24eed8d2019-11-22 13:21:35 +0000231 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000232 size_t len = 0;
233
Gilles Peskine1dbab672019-03-01 18:15:18 +0100234 do
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000235 {
236 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Gilles Peskine1dbab672019-03-01 18:15:18 +0100238 len += 1;
239 *--(*p) = val & 0xff;
240 val >>= 8;
241 }
242 while( val > 0 );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000243
Gilles Peskine1dbab672019-03-01 18:15:18 +0100244 if( **p & 0x80 )
245 {
246 if( *p - start < 1 )
247 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000248 *--(*p) = 0x00;
249 len += 1;
250 }
251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200253 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, tag ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000254
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200255 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000256}
257
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200258int mbedtls_asn1_write_int( unsigned char **p, unsigned char *start, int val )
259{
260 return( asn1_write_tagged_int( p, start, val, MBEDTLS_ASN1_INTEGER ) );
261}
262
263int mbedtls_asn1_write_enum( unsigned char **p, unsigned char *start, int val )
264{
265 return( asn1_write_tagged_int( p, start, val, MBEDTLS_ASN1_ENUMERATED ) );
266}
267
thomas-deeeba6c9b2018-09-19 09:10:37 +0200268int mbedtls_asn1_write_tagged_string( unsigned char **p, unsigned char *start, int tag,
Jaeden Amero23f954d2018-05-17 11:46:13 +0100269 const char *text, size_t text_len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000270{
Janos Follath24eed8d2019-11-22 13:21:35 +0000271 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000272 size_t len = 0;
273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
Jaeden Amero23f954d2018-05-17 11:46:13 +0100275 (const unsigned char *) text, text_len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
Jaeden Amero23f954d2018-05-17 11:46:13 +0100278 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, tag ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000279
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200280 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000281}
Paul Bakker598e4502013-08-25 14:46:39 +0200282
Jaeden Amero23f954d2018-05-17 11:46:13 +0100283int mbedtls_asn1_write_utf8_string( unsigned char **p, unsigned char *start,
284 const char *text, size_t text_len )
285{
thomas-deeeba6c9b2018-09-19 09:10:37 +0200286 return( mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_UTF8_STRING, text, text_len) );
Jaeden Amero23f954d2018-05-17 11:46:13 +0100287}
288
289int mbedtls_asn1_write_printable_string( unsigned char **p, unsigned char *start,
290 const char *text, size_t text_len )
291{
thomas-deeeba6c9b2018-09-19 09:10:37 +0200292 return( mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_PRINTABLE_STRING, text, text_len) );
Jaeden Amero23f954d2018-05-17 11:46:13 +0100293}
294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295int mbedtls_asn1_write_ia5_string( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200296 const char *text, size_t text_len )
Paul Bakker05888152012-02-16 10:26:57 +0000297{
thomas-deeeba6c9b2018-09-19 09:10:37 +0200298 return( mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_IA5_STRING, text, text_len) );
Paul Bakker05888152012-02-16 10:26:57 +0000299}
Paul Bakker598e4502013-08-25 14:46:39 +0200300
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100301int mbedtls_asn1_write_named_bitstring( unsigned char **p,
302 unsigned char *start,
303 const unsigned char *buf,
304 size_t bits )
305{
306 size_t unused_bits, byte_len;
307 const unsigned char *cur_byte;
308 unsigned char cur_byte_shifted;
309 unsigned char bit;
310
311 byte_len = ( bits + 7 ) / 8;
312 unused_bits = ( byte_len * 8 ) - bits;
313
314 /*
315 * Named bitstrings require that trailing 0s are excluded in the encoding
316 * of the bitstring. Trailing 0s are considered part of the 'unused' bits
317 * when encoding this value in the first content octet
318 */
319 if( bits != 0 )
320 {
321 cur_byte = buf + byte_len - 1;
322 cur_byte_shifted = *cur_byte >> unused_bits;
323
324 for( ; ; )
325 {
326 bit = cur_byte_shifted & 0x1;
327 cur_byte_shifted >>= 1;
328
329 if( bit != 0 )
330 break;
331
332 bits--;
333 if( bits == 0 )
334 break;
335
336 if( bits % 8 == 0 )
337 cur_byte_shifted = *--cur_byte;
338 }
339 }
340
341 return( mbedtls_asn1_write_bitstring( p, start, buf, bits ) );
342}
343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344int mbedtls_asn1_write_bitstring( unsigned char **p, unsigned char *start,
Paul Bakker598e4502013-08-25 14:46:39 +0200345 const unsigned char *buf, size_t bits )
346{
Janos Follath24eed8d2019-11-22 13:21:35 +0000347 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100348 size_t len = 0;
349 size_t unused_bits, byte_len;
Paul Bakker598e4502013-08-25 14:46:39 +0200350
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100351 byte_len = ( bits + 7 ) / 8;
352 unused_bits = ( byte_len * 8 ) - bits;
Paul Bakker598e4502013-08-25 14:46:39 +0200353
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100354 if( *p < start || (size_t)( *p - start ) < byte_len + 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakker598e4502013-08-25 14:46:39 +0200356
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100357 len = byte_len + 1;
Paul Bakker598e4502013-08-25 14:46:39 +0200358
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100359 /* Write the bitstring. Ensure the unused bits are zeroed */
360 if( byte_len > 0 )
361 {
362 byte_len--;
363 *--( *p ) = buf[byte_len] & ~( ( 0x1 << unused_bits ) - 1 );
364 ( *p ) -= byte_len;
365 memcpy( *p, buf, byte_len );
366 }
367
368 /* Write unused bits */
369 *--( *p ) = (unsigned char)unused_bits;
Paul Bakker598e4502013-08-25 14:46:39 +0200370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
372 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_BIT_STRING ) );
Paul Bakker598e4502013-08-25 14:46:39 +0200373
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200374 return( (int) len );
Paul Bakker598e4502013-08-25 14:46:39 +0200375}
376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377int mbedtls_asn1_write_octet_string( unsigned char **p, unsigned char *start,
Paul Bakker598e4502013-08-25 14:46:39 +0200378 const unsigned char *buf, size_t size )
379{
Janos Follath24eed8d2019-11-22 13:21:35 +0000380 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker598e4502013-08-25 14:46:39 +0200381 size_t len = 0;
382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, buf, size ) );
Paul Bakker598e4502013-08-25 14:46:39 +0200384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
386 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OCTET_STRING ) );
Paul Bakker598e4502013-08-25 14:46:39 +0200387
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200388 return( (int) len );
Paul Bakker598e4502013-08-25 14:46:39 +0200389}
Paul Bakker59ba59f2013-09-09 11:26:00 +0200390
Hanno Becker44da18a2018-10-12 10:42:13 +0100391
392/* This is a copy of the ASN.1 parsing function mbedtls_asn1_find_named_data(),
393 * which is replicated to avoid a dependency ASN1_WRITE_C on ASN1_PARSE_C. */
394static mbedtls_asn1_named_data *asn1_find_named_data(
395 mbedtls_asn1_named_data *list,
396 const char *oid, size_t len )
397{
398 while( list != NULL )
399 {
400 if( list->oid.len == len &&
401 memcmp( list->oid.p, oid, len ) == 0 )
402 {
403 break;
404 }
405
406 list = list->next;
407 }
408
409 return( list );
410}
411
412mbedtls_asn1_named_data *mbedtls_asn1_store_named_data(
413 mbedtls_asn1_named_data **head,
Paul Bakker59ba59f2013-09-09 11:26:00 +0200414 const char *oid, size_t oid_len,
415 const unsigned char *val,
416 size_t val_len )
417{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418 mbedtls_asn1_named_data *cur;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200419
Hanno Becker44da18a2018-10-12 10:42:13 +0100420 if( ( cur = asn1_find_named_data( *head, oid, oid_len ) ) == NULL )
Paul Bakker59ba59f2013-09-09 11:26:00 +0200421 {
422 // Add new entry if not present yet based on OID
423 //
Simon Butcher29176892016-05-20 00:19:09 +0100424 cur = (mbedtls_asn1_named_data*)mbedtls_calloc( 1,
425 sizeof(mbedtls_asn1_named_data) );
426 if( cur == NULL )
Paul Bakker59ba59f2013-09-09 11:26:00 +0200427 return( NULL );
428
Paul Bakker59ba59f2013-09-09 11:26:00 +0200429 cur->oid.len = oid_len;
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200430 cur->oid.p = mbedtls_calloc( 1, oid_len );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200431 if( cur->oid.p == NULL )
432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 mbedtls_free( cur );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200434 return( NULL );
435 }
436
Manuel Pégourié-Gonnarde5b0fc12014-11-12 22:27:42 +0100437 memcpy( cur->oid.p, oid, oid_len );
438
Paul Bakker59ba59f2013-09-09 11:26:00 +0200439 cur->val.len = val_len;
Gilles Peskine09c0a232019-03-04 15:00:06 +0100440 if( val_len != 0 )
Paul Bakker59ba59f2013-09-09 11:26:00 +0200441 {
Gilles Peskine09c0a232019-03-04 15:00:06 +0100442 cur->val.p = mbedtls_calloc( 1, val_len );
443 if( cur->val.p == NULL )
444 {
445 mbedtls_free( cur->oid.p );
446 mbedtls_free( cur );
447 return( NULL );
448 }
Paul Bakker59ba59f2013-09-09 11:26:00 +0200449 }
450
Paul Bakker59ba59f2013-09-09 11:26:00 +0200451 cur->next = *head;
452 *head = cur;
453 }
Gilles Peskine09c0a232019-03-04 15:00:06 +0100454 else if( val_len == 0 )
455 {
456 mbedtls_free( cur->val.p );
457 cur->val.p = NULL;
458 }
459 else if( cur->val.len != val_len )
Paul Bakker59ba59f2013-09-09 11:26:00 +0200460 {
Manuel Pégourié-Gonnard97b52092015-12-10 10:50:51 +0100461 /*
462 * Enlarge existing value buffer if needed
463 * Preserve old data until the allocation succeeded, to leave list in
464 * a consistent state in case allocation fails.
465 */
466 void *p = mbedtls_calloc( 1, val_len );
467 if( p == NULL )
Paul Bakker59ba59f2013-09-09 11:26:00 +0200468 return( NULL );
Manuel Pégourié-Gonnard97b52092015-12-10 10:50:51 +0100469
470 mbedtls_free( cur->val.p );
471 cur->val.p = p;
472 cur->val.len = val_len;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200473 }
474
475 if( val != NULL )
476 memcpy( cur->val.p, val, val_len );
477
478 return( cur );
479}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480#endif /* MBEDTLS_ASN1_WRITE_C */