blob: 626e0ff682fd5f192cfcfaae4bc4a1dd28f5aa38 [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 *
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 Bakker7dc4c442014-02-01 22:50:26 +010032#if defined(POLARSSL_PLATFORM_C)
33#include "polarssl/platform.h"
Paul Bakker59ba59f2013-09-09 11:26:00 +020034#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;
Paul Bakker3d8fb632014-04-17 12:42:41 +0200112 MPI_CHK( mpi_write_binary( X, *p, len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000113
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 Bakker3d8fb632014-04-17 12:42:41 +0200129 ret = (int) len;
130
131cleanup:
132 return( ret );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000133}
Paul Bakkered27a042013-04-18 22:46:23 +0200134#endif /* POLARSSL_BIGNUM_C */
135
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000136int asn1_write_null( unsigned char **p, unsigned char *start )
137{
138 int ret;
139 size_t len = 0;
140
141 // Write NULL
142 //
143 ASN1_CHK_ADD( len, asn1_write_len( p, start, 0) );
144 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_NULL ) );
145
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200146 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000147}
148
Paul Bakker5f45e622013-09-09 12:02:36 +0200149int asn1_write_oid( unsigned char **p, unsigned char *start,
150 const char *oid, size_t oid_len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000151{
152 int ret;
153 size_t len = 0;
154
Paul Bakker9852d002013-08-26 17:56:37 +0200155 ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200156 (const unsigned char *) oid, oid_len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000157 ASN1_CHK_ADD( len , asn1_write_len( p, start, len ) );
158 ASN1_CHK_ADD( len , asn1_write_tag( p, start, ASN1_OID ) );
159
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200160 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000161}
162
163int asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start,
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200164 const char *oid, size_t oid_len,
165 size_t par_len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000166{
167 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000168 size_t len = 0;
169
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200170 if( par_len == 0 )
171 ASN1_CHK_ADD( len, asn1_write_null( p, start ) );
172 else
173 len += par_len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000174
Paul Bakker5f45e622013-09-09 12:02:36 +0200175 ASN1_CHK_ADD( len, asn1_write_oid( p, start, oid, oid_len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000176
Paul Bakker5f45e622013-09-09 12:02:36 +0200177 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000178 ASN1_CHK_ADD( len, asn1_write_tag( p, start,
179 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
180
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200181 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000182}
183
Paul Bakker329def32013-09-06 16:34:38 +0200184int asn1_write_bool( unsigned char **p, unsigned char *start, int boolean )
185{
186 int ret;
187 size_t len = 0;
188
189 if( *p - start < 1 )
190 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
191
192 *--(*p) = (boolean) ? 1 : 0;
193 len++;
194
195 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
196 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_BOOLEAN ) );
197
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200198 return( (int) len );
Paul Bakker329def32013-09-06 16:34:38 +0200199}
200
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000201int asn1_write_int( unsigned char **p, unsigned char *start, int val )
202{
203 int ret;
204 size_t len = 0;
205
206 // TODO negative values and values larger than 128
207 // DER format assumes 2s complement for numbers, so the leftmost bit
208 // should be 0 for positive numbers and 1 for negative numbers.
209 //
210 if( *p - start < 1 )
211 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
212
213 len += 1;
214 *--(*p) = val;
215
216 if ( val > 0 && **p & 0x80 )
217 {
218 if( *p - start < 1 )
219 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
220
221 *--(*p) = 0x00;
222 len += 1;
223 }
224
225 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
226 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_INTEGER ) );
227
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200228 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000229}
230
231int asn1_write_printable_string( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200232 const char *text, size_t text_len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000233{
234 int ret;
235 size_t len = 0;
236
Paul Bakker9852d002013-08-26 17:56:37 +0200237 ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200238 (const unsigned char *) text, text_len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000239
240 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
241 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_PRINTABLE_STRING ) );
242
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200243 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000244}
Paul Bakker598e4502013-08-25 14:46:39 +0200245
Paul Bakker05888152012-02-16 10:26:57 +0000246int asn1_write_ia5_string( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200247 const char *text, size_t text_len )
Paul Bakker05888152012-02-16 10:26:57 +0000248{
249 int ret;
250 size_t len = 0;
251
Paul Bakker9852d002013-08-26 17:56:37 +0200252 ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200253 (const unsigned char *) text, text_len ) );
Paul Bakker05888152012-02-16 10:26:57 +0000254
255 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
256 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_IA5_STRING ) );
257
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200258 return( (int) len );
Paul Bakker05888152012-02-16 10:26:57 +0000259}
Paul Bakker598e4502013-08-25 14:46:39 +0200260
261int asn1_write_bitstring( unsigned char **p, unsigned char *start,
262 const unsigned char *buf, size_t bits )
263{
264 int ret;
265 size_t len = 0, size;
266
267 size = ( bits / 8 ) + ( ( bits % 8 ) ? 1 : 0 );
268
269 // Calculate byte length
270 //
271 if( *p - start < (int) size + 1 )
272 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
273
274 len = size + 1;
275 (*p) -= size;
276 memcpy( *p, buf, size );
277
278 // Write unused bits
279 //
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200280 *--(*p) = (unsigned char) (size * 8 - bits);
Paul Bakker598e4502013-08-25 14:46:39 +0200281
282 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
283 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_BIT_STRING ) );
284
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200285 return( (int) len );
Paul Bakker598e4502013-08-25 14:46:39 +0200286}
287
288int asn1_write_octet_string( unsigned char **p, unsigned char *start,
289 const unsigned char *buf, size_t size )
290{
291 int ret;
292 size_t len = 0;
293
Paul Bakker9852d002013-08-26 17:56:37 +0200294 ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start, buf, size ) );
Paul Bakker598e4502013-08-25 14:46:39 +0200295
296 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
297 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_OCTET_STRING ) );
298
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200299 return( (int) len );
Paul Bakker598e4502013-08-25 14:46:39 +0200300}
Paul Bakker59ba59f2013-09-09 11:26:00 +0200301
302asn1_named_data *asn1_store_named_data( asn1_named_data **head,
303 const char *oid, size_t oid_len,
304 const unsigned char *val,
305 size_t val_len )
306{
307 asn1_named_data *cur;
308
309 if( ( cur = asn1_find_named_data( *head, oid, oid_len ) ) == NULL )
310 {
311 // Add new entry if not present yet based on OID
312 //
313 if( ( cur = polarssl_malloc( sizeof(asn1_named_data) ) ) == NULL )
314 return( NULL );
315
316 memset( cur, 0, sizeof(asn1_named_data) );
317
318 cur->oid.len = oid_len;
319 cur->oid.p = polarssl_malloc( oid_len );
320 if( cur->oid.p == NULL )
321 {
322 polarssl_free( cur );
323 return( NULL );
324 }
325
326 cur->val.len = val_len;
327 cur->val.p = polarssl_malloc( val_len );
328 if( cur->val.p == NULL )
329 {
330 polarssl_free( cur->oid.p );
331 polarssl_free( cur );
332 return( NULL );
333 }
334
335 memcpy( cur->oid.p, oid, oid_len );
336
337 cur->next = *head;
338 *head = cur;
339 }
340 else if( cur->val.len < val_len )
341 {
342 // Enlarge existing value buffer if needed
343 //
344 polarssl_free( cur->val.p );
345 cur->val.p = NULL;
346
347 cur->val.len = val_len;
348 cur->val.p = polarssl_malloc( val_len );
349 if( cur->val.p == NULL )
350 {
351 polarssl_free( cur->oid.p );
352 polarssl_free( cur );
353 return( NULL );
354 }
355 }
356
357 if( val != NULL )
358 memcpy( cur->val.p, val, val_len );
359
360 return( cur );
361}
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000362#endif