1 /*
2 * ASN.1 buffer writing functionality
3 *
4 * Copyright The Mbed TLS Contributors
5 * 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:
12 *
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.
24 *
25 * **********
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 */
46
47 #if !defined(MBEDTLS_CONFIG_FILE)
48 #include "mbedtls/config.h"
49 #else
50 #include MBEDTLS_CONFIG_FILE
51 #endif
52
53 #if defined(MBEDTLS_ASN1_WRITE_C)
54
55 #include "mbedtls/asn1write.h"
56
57 #include <string.h>
58
59 #if defined(MBEDTLS_PLATFORM_C)
60 #include "mbedtls/platform.h"
61 #else
62 #include <stdlib.h>
63 #define mbedtls_calloc calloc
64 #define mbedtls_free free
65 #endif
66
mbedtls_asn1_write_len(unsigned char ** p,unsigned char * start,size_t len)67 int mbedtls_asn1_write_len( unsigned char **p, unsigned char *start, size_t len )
68 {
69 if( len < 0x80 )
70 {
71 if( *p - start < 1 )
72 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
73
74 *--(*p) = (unsigned char) len;
75 return( 1 );
76 }
77
78 if( len <= 0xFF )
79 {
80 if( *p - start < 2 )
81 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
82
83 *--(*p) = (unsigned char) len;
84 *--(*p) = 0x81;
85 return( 2 );
86 }
87
88 if( len <= 0xFFFF )
89 {
90 if( *p - start < 3 )
91 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
92
93 *--(*p) = ( len ) & 0xFF;
94 *--(*p) = ( len >> 8 ) & 0xFF;
95 *--(*p) = 0x82;
96 return( 3 );
97 }
98
99 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
111 #if SIZE_MAX > 0xFFFFFFFF
112 if( len <= 0xFFFFFFFF )
113 #endif
114 {
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
126 #if SIZE_MAX > 0xFFFFFFFF
127 return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
128 #endif
129 }
130
mbedtls_asn1_write_tag(unsigned char ** p,unsigned char * start,unsigned char tag)131 int mbedtls_asn1_write_tag( unsigned char **p, unsigned char *start, unsigned char tag )
132 {
133 if( *p - start < 1 )
134 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
135
136 *--(*p) = tag;
137
138 return( 1 );
139 }
140
mbedtls_asn1_write_raw_buffer(unsigned char ** p,unsigned char * start,const unsigned char * buf,size_t size)141 int mbedtls_asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
142 const unsigned char *buf, size_t size )
143 {
144 size_t len = 0;
145
146 if( *p < start || (size_t)( *p - start ) < size )
147 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
148
149 len = size;
150 (*p) -= len;
151 memcpy( *p, buf, len );
152
153 return( (int) len );
154 }
155
156 #if defined(MBEDTLS_BIGNUM_C)
mbedtls_asn1_write_mpi(unsigned char ** p,unsigned char * start,const mbedtls_mpi * X)157 int mbedtls_asn1_write_mpi( unsigned char **p, unsigned char *start, const mbedtls_mpi *X )
158 {
159 int ret;
160 size_t len = 0;
161
162 // Write the MPI
163 //
164 len = mbedtls_mpi_size( X );
165
166 if( *p < start || (size_t)( *p - start ) < len )
167 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
168
169 (*p) -= len;
170 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( X, *p, len ) );
171
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 //
175 if( X->s ==1 && **p & 0x80 )
176 {
177 if( *p - start < 1 )
178 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
179
180 *--(*p) = 0x00;
181 len += 1;
182 }
183
184 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 ) );
186
187 ret = (int) len;
188
189 cleanup:
190 return( ret );
191 }
192 #endif /* MBEDTLS_BIGNUM_C */
193
mbedtls_asn1_write_null(unsigned char ** p,unsigned char * start)194 int mbedtls_asn1_write_null( unsigned char **p, unsigned char *start )
195 {
196 int ret;
197 size_t len = 0;
198
199 // Write NULL
200 //
201 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 ) );
203
204 return( (int) len );
205 }
206
mbedtls_asn1_write_oid(unsigned char ** p,unsigned char * start,const char * oid,size_t oid_len)207 int mbedtls_asn1_write_oid( unsigned char **p, unsigned char *start,
208 const char *oid, size_t oid_len )
209 {
210 int ret;
211 size_t len = 0;
212
213 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
214 (const unsigned char *) oid, oid_len ) );
215 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 ) );
217
218 return( (int) len );
219 }
220
mbedtls_asn1_write_algorithm_identifier(unsigned char ** p,unsigned char * start,const char * oid,size_t oid_len,size_t par_len)221 int mbedtls_asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start,
222 const char *oid, size_t oid_len,
223 size_t par_len )
224 {
225 int ret;
226 size_t len = 0;
227
228 if( par_len == 0 )
229 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_null( p, start ) );
230 else
231 len += par_len;
232
233 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid, oid_len ) );
234
235 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 ) );
238
239 return( (int) len );
240 }
241
mbedtls_asn1_write_bool(unsigned char ** p,unsigned char * start,int boolean)242 int mbedtls_asn1_write_bool( unsigned char **p, unsigned char *start, int boolean )
243 {
244 int ret;
245 size_t len = 0;
246
247 if( *p - start < 1 )
248 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
249
250 *--(*p) = (boolean) ? 255 : 0;
251 len++;
252
253 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 ) );
255
256 return( (int) len );
257 }
258
mbedtls_asn1_write_int(unsigned char ** p,unsigned char * start,int val)259 int mbedtls_asn1_write_int( unsigned char **p, unsigned char *start, int val )
260 {
261 int ret;
262 size_t len = 0;
263
264 if( *p - start < 1 )
265 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
266
267 len += 1;
268 *--(*p) = val;
269
270 if( val > 0 && **p & 0x80 )
271 {
272 if( *p - start < 1 )
273 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
274
275 *--(*p) = 0x00;
276 len += 1;
277 }
278
279 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 ) );
281
282 return( (int) len );
283 }
284
mbedtls_asn1_write_tagged_string(unsigned char ** p,unsigned char * start,int tag,const char * text,size_t text_len)285 int mbedtls_asn1_write_tagged_string( unsigned char **p, unsigned char *start, int tag,
286 const char *text, size_t text_len )
287 {
288 int ret;
289 size_t len = 0;
290
291 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
292 (const unsigned char *) text, text_len ) );
293
294 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
295 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, tag ) );
296
297 return( (int) len );
298 }
299
mbedtls_asn1_write_utf8_string(unsigned char ** p,unsigned char * start,const char * text,size_t text_len)300 int mbedtls_asn1_write_utf8_string( unsigned char **p, unsigned char *start,
301 const char *text, size_t text_len )
302 {
303 return( mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_UTF8_STRING, text, text_len) );
304 }
305
mbedtls_asn1_write_printable_string(unsigned char ** p,unsigned char * start,const char * text,size_t text_len)306 int mbedtls_asn1_write_printable_string( unsigned char **p, unsigned char *start,
307 const char *text, size_t text_len )
308 {
309 return( mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_PRINTABLE_STRING, text, text_len) );
310 }
311
mbedtls_asn1_write_ia5_string(unsigned char ** p,unsigned char * start,const char * text,size_t text_len)312 int mbedtls_asn1_write_ia5_string( unsigned char **p, unsigned char *start,
313 const char *text, size_t text_len )
314 {
315 return( mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_IA5_STRING, text, text_len) );
316 }
317
mbedtls_asn1_write_bitstring(unsigned char ** p,unsigned char * start,const unsigned char * buf,size_t bits)318 int mbedtls_asn1_write_bitstring( unsigned char **p, unsigned char *start,
319 const unsigned char *buf, size_t bits )
320 {
321 int ret;
322 size_t len = 0;
323 size_t unused_bits, byte_len;
324
325 byte_len = ( bits + 7 ) / 8;
326 unused_bits = ( byte_len * 8 ) - bits;
327
328 if( *p < start || (size_t)( *p - start ) < byte_len + 1 )
329 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
330
331 len = byte_len + 1;
332
333 /* 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;
344
345 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 ) );
347
348 return( (int) len );
349 }
350
mbedtls_asn1_write_octet_string(unsigned char ** p,unsigned char * start,const unsigned char * buf,size_t size)351 int mbedtls_asn1_write_octet_string( unsigned char **p, unsigned char *start,
352 const unsigned char *buf, size_t size )
353 {
354 int ret;
355 size_t len = 0;
356
357 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, buf, size ) );
358
359 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 ) );
361
362 return( (int) len );
363 }
364
365
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. */
asn1_find_named_data(mbedtls_asn1_named_data * list,const char * oid,size_t len)368 static 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
mbedtls_asn1_store_named_data(mbedtls_asn1_named_data ** head,const char * oid,size_t oid_len,const unsigned char * val,size_t val_len)386 mbedtls_asn1_named_data *mbedtls_asn1_store_named_data(
387 mbedtls_asn1_named_data **head,
388 const char *oid, size_t oid_len,
389 const unsigned char *val,
390 size_t val_len )
391 {
392 mbedtls_asn1_named_data *cur;
393
394 if( ( cur = asn1_find_named_data( *head, oid, oid_len ) ) == NULL )
395 {
396 // Add new entry if not present yet based on OID
397 //
398 cur = (mbedtls_asn1_named_data*)mbedtls_calloc( 1,
399 sizeof(mbedtls_asn1_named_data) );
400 if( cur == NULL )
401 return( NULL );
402
403 cur->oid.len = oid_len;
404 cur->oid.p = mbedtls_calloc( 1, oid_len );
405 if( cur->oid.p == NULL )
406 {
407 mbedtls_free( cur );
408 return( NULL );
409 }
410
411 memcpy( cur->oid.p, oid, oid_len );
412
413 cur->val.len = val_len;
414 cur->val.p = mbedtls_calloc( 1, val_len );
415 if( cur->val.p == NULL )
416 {
417 mbedtls_free( cur->oid.p );
418 mbedtls_free( cur );
419 return( NULL );
420 }
421
422 cur->next = *head;
423 *head = cur;
424 }
425 else if( cur->val.len < val_len )
426 {
427 /*
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 )
434 return( NULL );
435
436 mbedtls_free( cur->val.p );
437 cur->val.p = p;
438 cur->val.len = val_len;
439 }
440
441 if( val != NULL )
442 memcpy( cur->val.p, val, val_len );
443
444 return( cur );
445 }
446 #endif /* MBEDTLS_ASN1_WRITE_C */
447