blob: 32d1c736fcd18fe3f99b1e3a836e078ddca0666e [file] [log] [blame]
Paul Bakkerbdb912d2012-02-13 23:11:30 +00001/*
2 * ASN.1 buffer writing functionality
3 *
4 * Copyright (C) 2006-2012, Brainspark B.V.
5 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 * All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
26#include "polarssl/config.h"
27
28#if defined(POLARSSL_ASN1_WRITE_C)
29
30#include "polarssl/asn1write.h"
31
Paul Bakker59ba59f2013-09-09 11:26:00 +020032#if defined(POLARSSL_MEMORY_C)
33#include "polarssl/memory.h"
34#else
35#include <stdlib.h>
36#define polarssl_malloc malloc
37#define polarssl_free free
38#endif
39
Paul Bakkerbdb912d2012-02-13 23:11:30 +000040int asn1_write_len( unsigned char **p, unsigned char *start, size_t len )
41{
42 if( len < 0x80 )
43 {
44 if( *p - start < 1 )
45 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
46
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020047 *--(*p) = (unsigned char) len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000048 return( 1 );
49 }
50
51 if( len <= 0xFF )
52 {
53 if( *p - start < 2 )
54 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
55
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020056 *--(*p) = (unsigned char) len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000057 *--(*p) = 0x81;
58 return( 2 );
59 }
60
61 if( *p - start < 3 )
62 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
63
64 // We assume we never have lengths larger than 65535 bytes
65 //
66 *--(*p) = len % 256;
67 *--(*p) = ( len / 256 ) % 256;
68 *--(*p) = 0x82;
69
70 return( 3 );
71}
72
73int asn1_write_tag( unsigned char **p, unsigned char *start, unsigned char tag )
74{
75 if( *p - start < 1 )
76 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
77
78 *--(*p) = tag;
79
80 return( 1 );
81}
82
Paul Bakker9852d002013-08-26 17:56:37 +020083int asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
84 const unsigned char *buf, size_t size )
85{
86 size_t len = 0;
87
88 if( *p - start < (int) size )
89 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
90
91 len = size;
92 (*p) -= len;
93 memcpy( *p, buf, len );
94
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020095 return( (int) len );
Paul Bakker9852d002013-08-26 17:56:37 +020096}
97
Paul Bakkered27a042013-04-18 22:46:23 +020098#if defined(POLARSSL_BIGNUM_C)
Paul Bakkerbdb912d2012-02-13 23:11:30 +000099int asn1_write_mpi( unsigned char **p, unsigned char *start, mpi *X )
100{
101 int ret;
102 size_t len = 0;
103
104 // Write the MPI
105 //
106 len = mpi_size( X );
107
108 if( *p - start < (int) len )
109 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
110
111 (*p) -= len;
112 mpi_write_binary( X, *p, len );
113
114 // DER format assumes 2s complement for numbers, so the leftmost bit
115 // should be 0 for positive numbers and 1 for negative numbers.
116 //
117 if ( X->s ==1 && **p & 0x80 )
118 {
119 if( *p - start < 1 )
120 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
121
122 *--(*p) = 0x00;
123 len += 1;
124 }
125
126 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
127 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_INTEGER ) );
128
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200129 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000130}
Paul Bakkered27a042013-04-18 22:46:23 +0200131#endif /* POLARSSL_BIGNUM_C */
132
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000133int asn1_write_null( unsigned char **p, unsigned char *start )
134{
135 int ret;
136 size_t len = 0;
137
138 // Write NULL
139 //
140 ASN1_CHK_ADD( len, asn1_write_len( p, start, 0) );
141 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_NULL ) );
142
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200143 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000144}
145
Paul Bakker5f45e622013-09-09 12:02:36 +0200146int asn1_write_oid( unsigned char **p, unsigned char *start,
147 const char *oid, size_t oid_len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000148{
149 int ret;
150 size_t len = 0;
151
Paul Bakker9852d002013-08-26 17:56:37 +0200152 ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200153 (const unsigned char *) oid, oid_len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000154 ASN1_CHK_ADD( len , asn1_write_len( p, start, len ) );
155 ASN1_CHK_ADD( len , asn1_write_tag( p, start, ASN1_OID ) );
156
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200157 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000158}
159
160int asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start,
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200161 const char *oid, size_t oid_len,
162 size_t par_len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000163{
164 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000165 size_t len = 0;
166
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200167 if( par_len == 0 )
168 ASN1_CHK_ADD( len, asn1_write_null( p, start ) );
169 else
170 len += par_len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000171
Paul Bakker5f45e622013-09-09 12:02:36 +0200172 ASN1_CHK_ADD( len, asn1_write_oid( p, start, oid, oid_len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000173
Paul Bakker5f45e622013-09-09 12:02:36 +0200174 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000175 ASN1_CHK_ADD( len, asn1_write_tag( p, start,
176 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
177
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200178 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000179}
180
Paul Bakker329def32013-09-06 16:34:38 +0200181int asn1_write_bool( unsigned char **p, unsigned char *start, int boolean )
182{
183 int ret;
184 size_t len = 0;
185
186 if( *p - start < 1 )
187 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
188
189 *--(*p) = (boolean) ? 1 : 0;
190 len++;
191
192 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
193 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_BOOLEAN ) );
194
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200195 return( (int) len );
Paul Bakker329def32013-09-06 16:34:38 +0200196}
197
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000198int asn1_write_int( unsigned char **p, unsigned char *start, int val )
199{
200 int ret;
201 size_t len = 0;
202
203 // TODO negative values and values larger than 128
204 // DER format assumes 2s complement for numbers, so the leftmost bit
205 // should be 0 for positive numbers and 1 for negative numbers.
206 //
207 if( *p - start < 1 )
208 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
209
210 len += 1;
211 *--(*p) = val;
212
213 if ( val > 0 && **p & 0x80 )
214 {
215 if( *p - start < 1 )
216 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
217
218 *--(*p) = 0x00;
219 len += 1;
220 }
221
222 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
223 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_INTEGER ) );
224
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200225 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000226}
227
228int asn1_write_printable_string( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200229 const char *text, size_t text_len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000230{
231 int ret;
232 size_t len = 0;
233
Paul Bakker9852d002013-08-26 17:56:37 +0200234 ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200235 (const unsigned char *) text, text_len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000236
237 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
238 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_PRINTABLE_STRING ) );
239
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200240 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000241}
Paul Bakker598e4502013-08-25 14:46:39 +0200242
Paul Bakker05888152012-02-16 10:26:57 +0000243int asn1_write_ia5_string( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200244 const char *text, size_t text_len )
Paul Bakker05888152012-02-16 10:26:57 +0000245{
246 int ret;
247 size_t len = 0;
248
Paul Bakker9852d002013-08-26 17:56:37 +0200249 ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200250 (const unsigned char *) text, text_len ) );
Paul Bakker05888152012-02-16 10:26:57 +0000251
252 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
253 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_IA5_STRING ) );
254
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200255 return( (int) len );
Paul Bakker05888152012-02-16 10:26:57 +0000256}
Paul Bakker598e4502013-08-25 14:46:39 +0200257
258int asn1_write_bitstring( unsigned char **p, unsigned char *start,
259 const unsigned char *buf, size_t bits )
260{
261 int ret;
262 size_t len = 0, size;
263
264 size = ( bits / 8 ) + ( ( bits % 8 ) ? 1 : 0 );
265
266 // Calculate byte length
267 //
268 if( *p - start < (int) size + 1 )
269 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
270
271 len = size + 1;
272 (*p) -= size;
273 memcpy( *p, buf, size );
274
275 // Write unused bits
276 //
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200277 *--(*p) = (unsigned char) (size * 8 - bits);
Paul Bakker598e4502013-08-25 14:46:39 +0200278
279 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
280 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_BIT_STRING ) );
281
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200282 return( (int) len );
Paul Bakker598e4502013-08-25 14:46:39 +0200283}
284
285int asn1_write_octet_string( unsigned char **p, unsigned char *start,
286 const unsigned char *buf, size_t size )
287{
288 int ret;
289 size_t len = 0;
290
Paul Bakker9852d002013-08-26 17:56:37 +0200291 ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start, buf, size ) );
Paul Bakker598e4502013-08-25 14:46:39 +0200292
293 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
294 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_OCTET_STRING ) );
295
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200296 return( (int) len );
Paul Bakker598e4502013-08-25 14:46:39 +0200297}
Paul Bakker59ba59f2013-09-09 11:26:00 +0200298
299asn1_named_data *asn1_store_named_data( asn1_named_data **head,
300 const char *oid, size_t oid_len,
301 const unsigned char *val,
302 size_t val_len )
303{
304 asn1_named_data *cur;
305
306 if( ( cur = asn1_find_named_data( *head, oid, oid_len ) ) == NULL )
307 {
308 // Add new entry if not present yet based on OID
309 //
310 if( ( cur = polarssl_malloc( sizeof(asn1_named_data) ) ) == NULL )
311 return( NULL );
312
313 memset( cur, 0, sizeof(asn1_named_data) );
314
315 cur->oid.len = oid_len;
316 cur->oid.p = polarssl_malloc( oid_len );
317 if( cur->oid.p == NULL )
318 {
319 polarssl_free( cur );
320 return( NULL );
321 }
322
323 cur->val.len = val_len;
324 cur->val.p = polarssl_malloc( val_len );
325 if( cur->val.p == NULL )
326 {
327 polarssl_free( cur->oid.p );
328 polarssl_free( cur );
329 return( NULL );
330 }
331
332 memcpy( cur->oid.p, oid, oid_len );
333
334 cur->next = *head;
335 *head = cur;
336 }
337 else if( cur->val.len < val_len )
338 {
339 // Enlarge existing value buffer if needed
340 //
341 polarssl_free( cur->val.p );
342 cur->val.p = NULL;
343
344 cur->val.len = val_len;
345 cur->val.p = polarssl_malloc( val_len );
346 if( cur->val.p == NULL )
347 {
348 polarssl_free( cur->oid.p );
349 polarssl_free( cur );
350 return( NULL );
351 }
352 }
353
354 if( val != NULL )
355 memcpy( cur->val.p, val, val_len );
356
357 return( cur );
358}
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000359#endif