blob: 8d92888b882b54faa04aa55eeae1ab4a93429e6e [file] [log] [blame]
Paul Bakkerbdb912d2012-02-13 23:11:30 +00001/*
2 * ASN.1 buffer writing functionality
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakkerbdb912d2012-02-13 23:11:30 +00005 *
Manuel Pégourié-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakkerbdb912d2012-02-13 23:11:30 +00007 *
Paul Bakkerbdb912d2012-02-13 23:11:30 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakkerbdb912d2012-02-13 23:11:30 +000024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakkerbdb912d2012-02-13 23:11:30 +000028
29#if defined(POLARSSL_ASN1_WRITE_C)
30
31#include "polarssl/asn1write.h"
32
Paul Bakker7dc4c442014-02-01 22:50:26 +010033#if defined(POLARSSL_PLATFORM_C)
34#include "polarssl/platform.h"
Paul Bakker59ba59f2013-09-09 11:26:00 +020035#else
36#include <stdlib.h>
37#define polarssl_malloc malloc
38#define polarssl_free free
39#endif
40
Paul Bakkerbdb912d2012-02-13 23:11:30 +000041int asn1_write_len( unsigned char **p, unsigned char *start, size_t len )
42{
43 if( len < 0x80 )
44 {
45 if( *p - start < 1 )
46 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
47
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020048 *--(*p) = (unsigned char) len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000049 return( 1 );
50 }
51
52 if( len <= 0xFF )
53 {
54 if( *p - start < 2 )
55 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
56
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020057 *--(*p) = (unsigned char) len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000058 *--(*p) = 0x81;
59 return( 2 );
60 }
61
62 if( *p - start < 3 )
63 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
64
65 // We assume we never have lengths larger than 65535 bytes
66 //
67 *--(*p) = len % 256;
68 *--(*p) = ( len / 256 ) % 256;
69 *--(*p) = 0x82;
70
71 return( 3 );
72}
73
74int asn1_write_tag( unsigned char **p, unsigned char *start, unsigned char tag )
75{
76 if( *p - start < 1 )
77 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
78
79 *--(*p) = tag;
80
81 return( 1 );
82}
83
Paul Bakker9852d002013-08-26 17:56:37 +020084int asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
85 const unsigned char *buf, size_t size )
86{
87 size_t len = 0;
88
89 if( *p - start < (int) size )
90 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
91
92 len = size;
93 (*p) -= len;
94 memcpy( *p, buf, len );
95
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020096 return( (int) len );
Paul Bakker9852d002013-08-26 17:56:37 +020097}
98
Paul Bakkered27a042013-04-18 22:46:23 +020099#if defined(POLARSSL_BIGNUM_C)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000100int asn1_write_mpi( unsigned char **p, unsigned char *start, mpi *X )
101{
102 int ret;
103 size_t len = 0;
104
105 // Write the MPI
106 //
107 len = mpi_size( X );
108
109 if( *p - start < (int) len )
110 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
111
112 (*p) -= len;
Paul Bakker3d8fb632014-04-17 12:42:41 +0200113 MPI_CHK( mpi_write_binary( X, *p, len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000114
115 // DER format assumes 2s complement for numbers, so the leftmost bit
116 // should be 0 for positive numbers and 1 for negative numbers.
117 //
Paul Bakker66d5d072014-06-17 16:39:18 +0200118 if( X->s ==1 && **p & 0x80 )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000119 {
120 if( *p - start < 1 )
121 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
122
123 *--(*p) = 0x00;
124 len += 1;
125 }
126
127 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
128 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_INTEGER ) );
129
Paul Bakker3d8fb632014-04-17 12:42:41 +0200130 ret = (int) len;
131
132cleanup:
133 return( ret );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000134}
Paul Bakkered27a042013-04-18 22:46:23 +0200135#endif /* POLARSSL_BIGNUM_C */
136
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000137int asn1_write_null( unsigned char **p, unsigned char *start )
138{
139 int ret;
140 size_t len = 0;
141
142 // Write NULL
143 //
144 ASN1_CHK_ADD( len, asn1_write_len( p, start, 0) );
145 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_NULL ) );
146
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200147 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000148}
149
Paul Bakker5f45e622013-09-09 12:02:36 +0200150int asn1_write_oid( unsigned char **p, unsigned char *start,
151 const char *oid, size_t oid_len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000152{
153 int ret;
154 size_t len = 0;
155
Paul Bakker9852d002013-08-26 17:56:37 +0200156 ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200157 (const unsigned char *) oid, oid_len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000158 ASN1_CHK_ADD( len , asn1_write_len( p, start, len ) );
159 ASN1_CHK_ADD( len , asn1_write_tag( p, start, ASN1_OID ) );
160
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200161 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000162}
163
164int asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start,
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200165 const char *oid, size_t oid_len,
166 size_t par_len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000167{
168 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000169 size_t len = 0;
170
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200171 if( par_len == 0 )
172 ASN1_CHK_ADD( len, asn1_write_null( p, start ) );
173 else
174 len += par_len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000175
Paul Bakker5f45e622013-09-09 12:02:36 +0200176 ASN1_CHK_ADD( len, asn1_write_oid( p, start, oid, oid_len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000177
Paul Bakker5f45e622013-09-09 12:02:36 +0200178 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000179 ASN1_CHK_ADD( len, asn1_write_tag( p, start,
180 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
181
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200182 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000183}
184
Paul Bakker329def32013-09-06 16:34:38 +0200185int asn1_write_bool( unsigned char **p, unsigned char *start, int boolean )
186{
187 int ret;
188 size_t len = 0;
189
190 if( *p - start < 1 )
191 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
192
193 *--(*p) = (boolean) ? 1 : 0;
194 len++;
195
196 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
197 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_BOOLEAN ) );
198
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200199 return( (int) len );
Paul Bakker329def32013-09-06 16:34:38 +0200200}
201
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000202int asn1_write_int( unsigned char **p, unsigned char *start, int val )
203{
204 int ret;
205 size_t len = 0;
206
207 // TODO negative values and values larger than 128
208 // DER format assumes 2s complement for numbers, so the leftmost bit
209 // should be 0 for positive numbers and 1 for negative numbers.
210 //
211 if( *p - start < 1 )
212 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
213
214 len += 1;
215 *--(*p) = val;
216
Paul Bakker66d5d072014-06-17 16:39:18 +0200217 if( val > 0 && **p & 0x80 )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000218 {
219 if( *p - start < 1 )
220 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
221
222 *--(*p) = 0x00;
223 len += 1;
224 }
225
226 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
227 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_INTEGER ) );
228
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200229 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000230}
231
232int asn1_write_printable_string( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200233 const char *text, size_t text_len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000234{
235 int ret;
236 size_t len = 0;
237
Paul Bakker9852d002013-08-26 17:56:37 +0200238 ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200239 (const unsigned char *) text, text_len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000240
241 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
242 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_PRINTABLE_STRING ) );
243
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200244 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000245}
Paul Bakker598e4502013-08-25 14:46:39 +0200246
Paul Bakker05888152012-02-16 10:26:57 +0000247int asn1_write_ia5_string( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200248 const char *text, size_t text_len )
Paul Bakker05888152012-02-16 10:26:57 +0000249{
250 int ret;
251 size_t len = 0;
252
Paul Bakker9852d002013-08-26 17:56:37 +0200253 ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200254 (const unsigned char *) text, text_len ) );
Paul Bakker05888152012-02-16 10:26:57 +0000255
256 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
257 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_IA5_STRING ) );
258
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200259 return( (int) len );
Paul Bakker05888152012-02-16 10:26:57 +0000260}
Paul Bakker598e4502013-08-25 14:46:39 +0200261
262int asn1_write_bitstring( unsigned char **p, unsigned char *start,
263 const unsigned char *buf, size_t bits )
264{
265 int ret;
266 size_t len = 0, size;
267
268 size = ( bits / 8 ) + ( ( bits % 8 ) ? 1 : 0 );
269
270 // Calculate byte length
271 //
272 if( *p - start < (int) size + 1 )
273 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
274
275 len = size + 1;
276 (*p) -= size;
277 memcpy( *p, buf, size );
278
279 // Write unused bits
280 //
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200281 *--(*p) = (unsigned char) (size * 8 - bits);
Paul Bakker598e4502013-08-25 14:46:39 +0200282
283 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
284 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_BIT_STRING ) );
285
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200286 return( (int) len );
Paul Bakker598e4502013-08-25 14:46:39 +0200287}
288
289int asn1_write_octet_string( unsigned char **p, unsigned char *start,
290 const unsigned char *buf, size_t size )
291{
292 int ret;
293 size_t len = 0;
294
Paul Bakker9852d002013-08-26 17:56:37 +0200295 ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start, buf, size ) );
Paul Bakker598e4502013-08-25 14:46:39 +0200296
297 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
298 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_OCTET_STRING ) );
299
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200300 return( (int) len );
Paul Bakker598e4502013-08-25 14:46:39 +0200301}
Paul Bakker59ba59f2013-09-09 11:26:00 +0200302
303asn1_named_data *asn1_store_named_data( asn1_named_data **head,
304 const char *oid, size_t oid_len,
305 const unsigned char *val,
306 size_t val_len )
307{
308 asn1_named_data *cur;
309
310 if( ( cur = asn1_find_named_data( *head, oid, oid_len ) ) == NULL )
311 {
312 // Add new entry if not present yet based on OID
313 //
314 if( ( cur = polarssl_malloc( sizeof(asn1_named_data) ) ) == NULL )
315 return( NULL );
316
317 memset( cur, 0, sizeof(asn1_named_data) );
318
319 cur->oid.len = oid_len;
320 cur->oid.p = polarssl_malloc( oid_len );
321 if( cur->oid.p == NULL )
322 {
323 polarssl_free( cur );
324 return( NULL );
325 }
326
Manuel Pégourié-Gonnarde5b0fc12014-11-12 22:27:42 +0100327 memcpy( cur->oid.p, oid, oid_len );
328
Paul Bakker59ba59f2013-09-09 11:26:00 +0200329 cur->val.len = val_len;
330 cur->val.p = polarssl_malloc( val_len );
331 if( cur->val.p == NULL )
332 {
333 polarssl_free( cur->oid.p );
334 polarssl_free( cur );
335 return( NULL );
336 }
337
Paul Bakker59ba59f2013-09-09 11:26:00 +0200338 cur->next = *head;
339 *head = cur;
340 }
341 else if( cur->val.len < val_len )
342 {
343 // Enlarge existing value buffer if needed
344 //
345 polarssl_free( cur->val.p );
346 cur->val.p = NULL;
347
348 cur->val.len = val_len;
349 cur->val.p = polarssl_malloc( val_len );
350 if( cur->val.p == NULL )
351 {
352 polarssl_free( cur->oid.p );
353 polarssl_free( cur );
354 return( NULL );
355 }
356 }
357
358 if( val != NULL )
359 memcpy( cur->val.p, val, val_len );
360
361 return( cur );
362}
Paul Bakker9af723c2014-05-01 13:03:14 +0200363#endif /* POLARSSL_ASN1_WRITE_C */