blob: 7a965453cdae8c83fb4d5b4e3a713276664cd523 [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é-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 *
Paul Bakkerbdb912d2012-02-13 23:11:30 +00009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakkerbdb912d2012-02-13 23:11:30 +000025#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#else
27#include POLARSSL_CONFIG_FILE
28#endif
Paul Bakkerbdb912d2012-02-13 23:11:30 +000029
30#if defined(POLARSSL_ASN1_WRITE_C)
31
32#include "polarssl/asn1write.h"
33
Paul Bakker7dc4c442014-02-01 22:50:26 +010034#if defined(POLARSSL_PLATFORM_C)
35#include "polarssl/platform.h"
Paul Bakker59ba59f2013-09-09 11:26:00 +020036#else
37#include <stdlib.h>
38#define polarssl_malloc malloc
39#define polarssl_free free
40#endif
41
Paul Bakkerbdb912d2012-02-13 23:11:30 +000042int asn1_write_len( unsigned char **p, unsigned char *start, size_t len )
43{
44 if( len < 0x80 )
45 {
46 if( *p - start < 1 )
47 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
48
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020049 *--(*p) = (unsigned char) len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000050 return( 1 );
51 }
52
53 if( len <= 0xFF )
54 {
55 if( *p - start < 2 )
56 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
57
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020058 *--(*p) = (unsigned char) len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000059 *--(*p) = 0x81;
60 return( 2 );
61 }
62
63 if( *p - start < 3 )
64 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
65
66 // We assume we never have lengths larger than 65535 bytes
67 //
68 *--(*p) = len % 256;
69 *--(*p) = ( len / 256 ) % 256;
70 *--(*p) = 0x82;
71
72 return( 3 );
73}
74
75int asn1_write_tag( unsigned char **p, unsigned char *start, unsigned char tag )
76{
77 if( *p - start < 1 )
78 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
79
80 *--(*p) = tag;
81
82 return( 1 );
83}
84
Paul Bakker9852d002013-08-26 17:56:37 +020085int asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
86 const unsigned char *buf, size_t size )
87{
88 size_t len = 0;
89
90 if( *p - start < (int) size )
91 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
92
93 len = size;
94 (*p) -= len;
95 memcpy( *p, buf, len );
96
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020097 return( (int) len );
Paul Bakker9852d002013-08-26 17:56:37 +020098}
99
Paul Bakkered27a042013-04-18 22:46:23 +0200100#if defined(POLARSSL_BIGNUM_C)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000101int asn1_write_mpi( unsigned char **p, unsigned char *start, mpi *X )
102{
103 int ret;
104 size_t len = 0;
105
106 // Write the MPI
107 //
108 len = mpi_size( X );
109
110 if( *p - start < (int) len )
111 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
112
113 (*p) -= len;
Paul Bakker3d8fb632014-04-17 12:42:41 +0200114 MPI_CHK( mpi_write_binary( X, *p, len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000115
116 // DER format assumes 2s complement for numbers, so the leftmost bit
117 // should be 0 for positive numbers and 1 for negative numbers.
118 //
Paul Bakker66d5d072014-06-17 16:39:18 +0200119 if( X->s ==1 && **p & 0x80 )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000120 {
121 if( *p - start < 1 )
122 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
123
124 *--(*p) = 0x00;
125 len += 1;
126 }
127
128 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
129 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_INTEGER ) );
130
Paul Bakker3d8fb632014-04-17 12:42:41 +0200131 ret = (int) len;
132
133cleanup:
134 return( ret );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000135}
Paul Bakkered27a042013-04-18 22:46:23 +0200136#endif /* POLARSSL_BIGNUM_C */
137
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000138int asn1_write_null( unsigned char **p, unsigned char *start )
139{
140 int ret;
141 size_t len = 0;
142
143 // Write NULL
144 //
145 ASN1_CHK_ADD( len, asn1_write_len( p, start, 0) );
146 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_NULL ) );
147
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200148 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000149}
150
Paul Bakker5f45e622013-09-09 12:02:36 +0200151int asn1_write_oid( unsigned char **p, unsigned char *start,
152 const char *oid, size_t oid_len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000153{
154 int ret;
155 size_t len = 0;
156
Paul Bakker9852d002013-08-26 17:56:37 +0200157 ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200158 (const unsigned char *) oid, oid_len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000159 ASN1_CHK_ADD( len , asn1_write_len( p, start, len ) );
160 ASN1_CHK_ADD( len , asn1_write_tag( p, start, ASN1_OID ) );
161
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200162 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000163}
164
165int asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start,
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200166 const char *oid, size_t oid_len,
167 size_t par_len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000168{
169 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000170 size_t len = 0;
171
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200172 if( par_len == 0 )
173 ASN1_CHK_ADD( len, asn1_write_null( p, start ) );
174 else
175 len += par_len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000176
Paul Bakker5f45e622013-09-09 12:02:36 +0200177 ASN1_CHK_ADD( len, asn1_write_oid( p, start, oid, oid_len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000178
Paul Bakker5f45e622013-09-09 12:02:36 +0200179 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000180 ASN1_CHK_ADD( len, asn1_write_tag( p, start,
181 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
182
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200183 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000184}
185
Paul Bakker329def32013-09-06 16:34:38 +0200186int asn1_write_bool( unsigned char **p, unsigned char *start, int boolean )
187{
188 int ret;
189 size_t len = 0;
190
191 if( *p - start < 1 )
192 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
193
194 *--(*p) = (boolean) ? 1 : 0;
195 len++;
196
197 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
198 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_BOOLEAN ) );
199
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200200 return( (int) len );
Paul Bakker329def32013-09-06 16:34:38 +0200201}
202
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000203int asn1_write_int( unsigned char **p, unsigned char *start, int val )
204{
205 int ret;
206 size_t len = 0;
207
208 // TODO negative values and values larger than 128
209 // DER format assumes 2s complement for numbers, so the leftmost bit
210 // should be 0 for positive numbers and 1 for negative numbers.
211 //
212 if( *p - start < 1 )
213 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
214
215 len += 1;
216 *--(*p) = val;
217
Paul Bakker66d5d072014-06-17 16:39:18 +0200218 if( val > 0 && **p & 0x80 )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000219 {
220 if( *p - start < 1 )
221 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
222
223 *--(*p) = 0x00;
224 len += 1;
225 }
226
227 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
228 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_INTEGER ) );
229
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200230 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000231}
232
233int asn1_write_printable_string( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200234 const char *text, size_t text_len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000235{
236 int ret;
237 size_t len = 0;
238
Paul Bakker9852d002013-08-26 17:56:37 +0200239 ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200240 (const unsigned char *) text, text_len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000241
242 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
243 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_PRINTABLE_STRING ) );
244
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200245 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000246}
Paul Bakker598e4502013-08-25 14:46:39 +0200247
Paul Bakker05888152012-02-16 10:26:57 +0000248int asn1_write_ia5_string( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200249 const char *text, size_t text_len )
Paul Bakker05888152012-02-16 10:26:57 +0000250{
251 int ret;
252 size_t len = 0;
253
Paul Bakker9852d002013-08-26 17:56:37 +0200254 ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200255 (const unsigned char *) text, text_len ) );
Paul Bakker05888152012-02-16 10:26:57 +0000256
257 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
258 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_IA5_STRING ) );
259
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200260 return( (int) len );
Paul Bakker05888152012-02-16 10:26:57 +0000261}
Paul Bakker598e4502013-08-25 14:46:39 +0200262
263int asn1_write_bitstring( unsigned char **p, unsigned char *start,
264 const unsigned char *buf, size_t bits )
265{
266 int ret;
267 size_t len = 0, size;
268
269 size = ( bits / 8 ) + ( ( bits % 8 ) ? 1 : 0 );
270
271 // Calculate byte length
272 //
273 if( *p - start < (int) size + 1 )
274 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
275
276 len = size + 1;
277 (*p) -= size;
278 memcpy( *p, buf, size );
279
280 // Write unused bits
281 //
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200282 *--(*p) = (unsigned char) (size * 8 - bits);
Paul Bakker598e4502013-08-25 14:46:39 +0200283
284 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
285 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_BIT_STRING ) );
286
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200287 return( (int) len );
Paul Bakker598e4502013-08-25 14:46:39 +0200288}
289
290int asn1_write_octet_string( unsigned char **p, unsigned char *start,
291 const unsigned char *buf, size_t size )
292{
293 int ret;
294 size_t len = 0;
295
Paul Bakker9852d002013-08-26 17:56:37 +0200296 ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start, buf, size ) );
Paul Bakker598e4502013-08-25 14:46:39 +0200297
298 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
299 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_OCTET_STRING ) );
300
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200301 return( (int) len );
Paul Bakker598e4502013-08-25 14:46:39 +0200302}
Paul Bakker59ba59f2013-09-09 11:26:00 +0200303
304asn1_named_data *asn1_store_named_data( asn1_named_data **head,
305 const char *oid, size_t oid_len,
306 const unsigned char *val,
307 size_t val_len )
308{
309 asn1_named_data *cur;
310
311 if( ( cur = asn1_find_named_data( *head, oid, oid_len ) ) == NULL )
312 {
313 // Add new entry if not present yet based on OID
314 //
315 if( ( cur = polarssl_malloc( sizeof(asn1_named_data) ) ) == NULL )
316 return( NULL );
317
318 memset( cur, 0, sizeof(asn1_named_data) );
319
320 cur->oid.len = oid_len;
321 cur->oid.p = polarssl_malloc( oid_len );
322 if( cur->oid.p == NULL )
323 {
324 polarssl_free( cur );
325 return( NULL );
326 }
327
Manuel Pégourié-Gonnarde5b0fc12014-11-12 22:27:42 +0100328 memcpy( cur->oid.p, oid, oid_len );
329
Paul Bakker59ba59f2013-09-09 11:26:00 +0200330 cur->val.len = val_len;
331 cur->val.p = polarssl_malloc( val_len );
332 if( cur->val.p == NULL )
333 {
334 polarssl_free( cur->oid.p );
335 polarssl_free( cur );
336 return( NULL );
337 }
338
Paul Bakker59ba59f2013-09-09 11:26:00 +0200339 cur->next = *head;
340 *head = cur;
341 }
342 else if( cur->val.len < val_len )
343 {
344 // Enlarge existing value buffer if needed
345 //
346 polarssl_free( cur->val.p );
347 cur->val.p = NULL;
348
349 cur->val.len = val_len;
350 cur->val.p = polarssl_malloc( val_len );
351 if( cur->val.p == NULL )
352 {
353 polarssl_free( cur->oid.p );
354 polarssl_free( cur );
355 return( NULL );
356 }
357 }
358
359 if( val != NULL )
360 memcpy( cur->val.p, val, val_len );
361
362 return( cur );
363}
Paul Bakker9af723c2014-05-01 13:03:14 +0200364#endif /* POLARSSL_ASN1_WRITE_C */