blob: 8032fb41aae99a0ddaba06eca671b013ec2c20e8 [file] [log] [blame]
Paul Bakkerbdb912d2012-02-13 23:11:30 +00001/*
2 * ASN.1 buffer writing functionality
3 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakkerbdb912d2012-02-13 23:11:30 +00005 *
Manuel Pégourié-Gonnard967a2a52015-01-22 14:28:16 +00006 * This file is part of mbed TLS (http://www.polarssl.org)
Paul Bakkerbdb912d2012-02-13 23:11:30 +00007 * 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
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakkerbdb912d2012-02-13 23:11:30 +000027#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
29#include POLARSSL_CONFIG_FILE
30#endif
Paul Bakkerbdb912d2012-02-13 23:11:30 +000031
32#if defined(POLARSSL_ASN1_WRITE_C)
33
34#include "polarssl/asn1write.h"
35
Paul Bakker7dc4c442014-02-01 22:50:26 +010036#if defined(POLARSSL_PLATFORM_C)
37#include "polarssl/platform.h"
Paul Bakker59ba59f2013-09-09 11:26:00 +020038#else
39#include <stdlib.h>
40#define polarssl_malloc malloc
41#define polarssl_free free
42#endif
43
Paul Bakkerbdb912d2012-02-13 23:11:30 +000044int asn1_write_len( unsigned char **p, unsigned char *start, size_t len )
45{
46 if( len < 0x80 )
47 {
48 if( *p - start < 1 )
49 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
50
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020051 *--(*p) = (unsigned char) len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000052 return( 1 );
53 }
54
55 if( len <= 0xFF )
56 {
57 if( *p - start < 2 )
58 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
59
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020060 *--(*p) = (unsigned char) len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000061 *--(*p) = 0x81;
62 return( 2 );
63 }
64
65 if( *p - start < 3 )
66 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
67
68 // We assume we never have lengths larger than 65535 bytes
69 //
70 *--(*p) = len % 256;
71 *--(*p) = ( len / 256 ) % 256;
72 *--(*p) = 0x82;
73
74 return( 3 );
75}
76
77int asn1_write_tag( unsigned char **p, unsigned char *start, unsigned char tag )
78{
79 if( *p - start < 1 )
80 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
81
82 *--(*p) = tag;
83
84 return( 1 );
85}
86
Paul Bakker9852d002013-08-26 17:56:37 +020087int asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
88 const unsigned char *buf, size_t size )
89{
90 size_t len = 0;
91
92 if( *p - start < (int) size )
93 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
94
95 len = size;
96 (*p) -= len;
97 memcpy( *p, buf, len );
98
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020099 return( (int) len );
Paul Bakker9852d002013-08-26 17:56:37 +0200100}
101
Paul Bakkered27a042013-04-18 22:46:23 +0200102#if defined(POLARSSL_BIGNUM_C)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000103int asn1_write_mpi( unsigned char **p, unsigned char *start, mpi *X )
104{
105 int ret;
106 size_t len = 0;
107
108 // Write the MPI
109 //
110 len = mpi_size( X );
111
112 if( *p - start < (int) len )
113 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
114
115 (*p) -= len;
Paul Bakker3d8fb632014-04-17 12:42:41 +0200116 MPI_CHK( mpi_write_binary( X, *p, len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000117
118 // DER format assumes 2s complement for numbers, so the leftmost bit
119 // should be 0 for positive numbers and 1 for negative numbers.
120 //
Paul Bakker66d5d072014-06-17 16:39:18 +0200121 if( X->s ==1 && **p & 0x80 )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000122 {
123 if( *p - start < 1 )
124 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
125
126 *--(*p) = 0x00;
127 len += 1;
128 }
129
130 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
131 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_INTEGER ) );
132
Paul Bakker3d8fb632014-04-17 12:42:41 +0200133 ret = (int) len;
134
135cleanup:
136 return( ret );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000137}
Paul Bakkered27a042013-04-18 22:46:23 +0200138#endif /* POLARSSL_BIGNUM_C */
139
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000140int asn1_write_null( unsigned char **p, unsigned char *start )
141{
142 int ret;
143 size_t len = 0;
144
145 // Write NULL
146 //
147 ASN1_CHK_ADD( len, asn1_write_len( p, start, 0) );
148 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_NULL ) );
149
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200150 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000151}
152
Paul Bakker5f45e622013-09-09 12:02:36 +0200153int asn1_write_oid( unsigned char **p, unsigned char *start,
154 const char *oid, size_t oid_len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000155{
156 int ret;
157 size_t len = 0;
158
Paul Bakker9852d002013-08-26 17:56:37 +0200159 ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200160 (const unsigned char *) oid, oid_len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000161 ASN1_CHK_ADD( len , asn1_write_len( p, start, len ) );
162 ASN1_CHK_ADD( len , asn1_write_tag( p, start, ASN1_OID ) );
163
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200164 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000165}
166
167int asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start,
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200168 const char *oid, size_t oid_len,
169 size_t par_len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000170{
171 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000172 size_t len = 0;
173
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200174 if( par_len == 0 )
175 ASN1_CHK_ADD( len, asn1_write_null( p, start ) );
176 else
177 len += par_len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000178
Paul Bakker5f45e622013-09-09 12:02:36 +0200179 ASN1_CHK_ADD( len, asn1_write_oid( p, start, oid, oid_len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000180
Paul Bakker5f45e622013-09-09 12:02:36 +0200181 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000182 ASN1_CHK_ADD( len, asn1_write_tag( p, start,
183 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
184
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200185 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000186}
187
Paul Bakker329def32013-09-06 16:34:38 +0200188int asn1_write_bool( unsigned char **p, unsigned char *start, int boolean )
189{
190 int ret;
191 size_t len = 0;
192
193 if( *p - start < 1 )
194 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
195
196 *--(*p) = (boolean) ? 1 : 0;
197 len++;
198
199 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
200 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_BOOLEAN ) );
201
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200202 return( (int) len );
Paul Bakker329def32013-09-06 16:34:38 +0200203}
204
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000205int asn1_write_int( unsigned char **p, unsigned char *start, int val )
206{
207 int ret;
208 size_t len = 0;
209
210 // TODO negative values and values larger than 128
211 // DER format assumes 2s complement for numbers, so the leftmost bit
212 // should be 0 for positive numbers and 1 for negative numbers.
213 //
214 if( *p - start < 1 )
215 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
216
217 len += 1;
218 *--(*p) = val;
219
Paul Bakker66d5d072014-06-17 16:39:18 +0200220 if( val > 0 && **p & 0x80 )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000221 {
222 if( *p - start < 1 )
223 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
224
225 *--(*p) = 0x00;
226 len += 1;
227 }
228
229 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
230 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_INTEGER ) );
231
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200232 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000233}
234
235int asn1_write_printable_string( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200236 const char *text, size_t text_len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000237{
238 int ret;
239 size_t len = 0;
240
Paul Bakker9852d002013-08-26 17:56:37 +0200241 ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200242 (const unsigned char *) text, text_len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000243
244 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
245 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_PRINTABLE_STRING ) );
246
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200247 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000248}
Paul Bakker598e4502013-08-25 14:46:39 +0200249
Paul Bakker05888152012-02-16 10:26:57 +0000250int asn1_write_ia5_string( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200251 const char *text, size_t text_len )
Paul Bakker05888152012-02-16 10:26:57 +0000252{
253 int ret;
254 size_t len = 0;
255
Paul Bakker9852d002013-08-26 17:56:37 +0200256 ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200257 (const unsigned char *) text, text_len ) );
Paul Bakker05888152012-02-16 10:26:57 +0000258
259 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
260 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_IA5_STRING ) );
261
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200262 return( (int) len );
Paul Bakker05888152012-02-16 10:26:57 +0000263}
Paul Bakker598e4502013-08-25 14:46:39 +0200264
265int asn1_write_bitstring( unsigned char **p, unsigned char *start,
266 const unsigned char *buf, size_t bits )
267{
268 int ret;
269 size_t len = 0, size;
270
271 size = ( bits / 8 ) + ( ( bits % 8 ) ? 1 : 0 );
272
273 // Calculate byte length
274 //
275 if( *p - start < (int) size + 1 )
276 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
277
278 len = size + 1;
279 (*p) -= size;
280 memcpy( *p, buf, size );
281
282 // Write unused bits
283 //
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200284 *--(*p) = (unsigned char) (size * 8 - bits);
Paul Bakker598e4502013-08-25 14:46:39 +0200285
286 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
287 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_BIT_STRING ) );
288
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200289 return( (int) len );
Paul Bakker598e4502013-08-25 14:46:39 +0200290}
291
292int asn1_write_octet_string( unsigned char **p, unsigned char *start,
293 const unsigned char *buf, size_t size )
294{
295 int ret;
296 size_t len = 0;
297
Paul Bakker9852d002013-08-26 17:56:37 +0200298 ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start, buf, size ) );
Paul Bakker598e4502013-08-25 14:46:39 +0200299
300 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
301 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_OCTET_STRING ) );
302
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200303 return( (int) len );
Paul Bakker598e4502013-08-25 14:46:39 +0200304}
Paul Bakker59ba59f2013-09-09 11:26:00 +0200305
306asn1_named_data *asn1_store_named_data( asn1_named_data **head,
307 const char *oid, size_t oid_len,
308 const unsigned char *val,
309 size_t val_len )
310{
311 asn1_named_data *cur;
312
313 if( ( cur = asn1_find_named_data( *head, oid, oid_len ) ) == NULL )
314 {
315 // Add new entry if not present yet based on OID
316 //
317 if( ( cur = polarssl_malloc( sizeof(asn1_named_data) ) ) == NULL )
318 return( NULL );
319
320 memset( cur, 0, sizeof(asn1_named_data) );
321
322 cur->oid.len = oid_len;
323 cur->oid.p = polarssl_malloc( oid_len );
324 if( cur->oid.p == NULL )
325 {
326 polarssl_free( cur );
327 return( NULL );
328 }
329
Manuel Pégourié-Gonnarde5b0fc12014-11-12 22:27:42 +0100330 memcpy( cur->oid.p, oid, oid_len );
331
Paul Bakker59ba59f2013-09-09 11:26:00 +0200332 cur->val.len = val_len;
333 cur->val.p = polarssl_malloc( val_len );
334 if( cur->val.p == NULL )
335 {
336 polarssl_free( cur->oid.p );
337 polarssl_free( cur );
338 return( NULL );
339 }
340
Paul Bakker59ba59f2013-09-09 11:26:00 +0200341 cur->next = *head;
342 *head = cur;
343 }
344 else if( cur->val.len < val_len )
345 {
346 // Enlarge existing value buffer if needed
347 //
348 polarssl_free( cur->val.p );
349 cur->val.p = NULL;
350
351 cur->val.len = val_len;
352 cur->val.p = polarssl_malloc( val_len );
353 if( cur->val.p == NULL )
354 {
355 polarssl_free( cur->oid.p );
356 polarssl_free( cur );
357 return( NULL );
358 }
359 }
360
361 if( val != NULL )
362 memcpy( cur->val.p, val, val_len );
363
364 return( cur );
365}
Paul Bakker9af723c2014-05-01 13:03:14 +0200366#endif /* POLARSSL_ASN1_WRITE_C */