blob: 78038675d9aff0ed2eee1fd5acbb610f99233a4f [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é-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.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)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/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
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/asn1write.h"
Paul Bakkerbdb912d2012-02-13 23:11:30 +000032
Rich Evans00ab4702015-02-06 13:43:58 +000033#include <string.h>
34
Paul Bakker7dc4c442014-02-01 22:50:26 +010035#if defined(POLARSSL_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/platform.h"
Paul Bakker59ba59f2013-09-09 11:26:00 +020037#else
38#include <stdlib.h>
39#define polarssl_malloc malloc
40#define polarssl_free free
41#endif
42
Paul Bakkerbdb912d2012-02-13 23:11:30 +000043int asn1_write_len( unsigned char **p, unsigned char *start, size_t len )
44{
45 if( len < 0x80 )
46 {
47 if( *p - start < 1 )
48 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
49
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020050 *--(*p) = (unsigned char) len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000051 return( 1 );
52 }
53
54 if( len <= 0xFF )
55 {
56 if( *p - start < 2 )
57 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
58
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020059 *--(*p) = (unsigned char) len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000060 *--(*p) = 0x81;
61 return( 2 );
62 }
63
64 if( *p - start < 3 )
65 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
66
67 // We assume we never have lengths larger than 65535 bytes
68 //
69 *--(*p) = len % 256;
70 *--(*p) = ( len / 256 ) % 256;
71 *--(*p) = 0x82;
72
73 return( 3 );
74}
75
76int asn1_write_tag( unsigned char **p, unsigned char *start, unsigned char tag )
77{
78 if( *p - start < 1 )
79 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
80
81 *--(*p) = tag;
82
83 return( 1 );
84}
85
Paul Bakker9852d002013-08-26 17:56:37 +020086int asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
87 const unsigned char *buf, size_t size )
88{
89 size_t len = 0;
90
91 if( *p - start < (int) size )
92 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
93
94 len = size;
95 (*p) -= len;
96 memcpy( *p, buf, len );
97
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020098 return( (int) len );
Paul Bakker9852d002013-08-26 17:56:37 +020099}
100
Paul Bakkered27a042013-04-18 22:46:23 +0200101#if defined(POLARSSL_BIGNUM_C)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000102int asn1_write_mpi( unsigned char **p, unsigned char *start, mpi *X )
103{
104 int ret;
105 size_t len = 0;
106
107 // Write the MPI
108 //
109 len = mpi_size( X );
110
111 if( *p - start < (int) len )
112 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
113
114 (*p) -= len;
Paul Bakker3d8fb632014-04-17 12:42:41 +0200115 MPI_CHK( mpi_write_binary( X, *p, len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000116
117 // DER format assumes 2s complement for numbers, so the leftmost bit
118 // should be 0 for positive numbers and 1 for negative numbers.
119 //
Paul Bakker66d5d072014-06-17 16:39:18 +0200120 if( X->s ==1 && **p & 0x80 )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000121 {
122 if( *p - start < 1 )
123 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
124
125 *--(*p) = 0x00;
126 len += 1;
127 }
128
129 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
130 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_INTEGER ) );
131
Paul Bakker3d8fb632014-04-17 12:42:41 +0200132 ret = (int) len;
133
134cleanup:
135 return( ret );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000136}
Paul Bakkered27a042013-04-18 22:46:23 +0200137#endif /* POLARSSL_BIGNUM_C */
138
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000139int asn1_write_null( unsigned char **p, unsigned char *start )
140{
141 int ret;
142 size_t len = 0;
143
144 // Write NULL
145 //
146 ASN1_CHK_ADD( len, asn1_write_len( p, start, 0) );
147 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_NULL ) );
148
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200149 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000150}
151
Paul Bakker5f45e622013-09-09 12:02:36 +0200152int asn1_write_oid( unsigned char **p, unsigned char *start,
153 const char *oid, size_t oid_len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000154{
155 int ret;
156 size_t len = 0;
157
Paul Bakker9852d002013-08-26 17:56:37 +0200158 ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200159 (const unsigned char *) oid, oid_len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000160 ASN1_CHK_ADD( len , asn1_write_len( p, start, len ) );
161 ASN1_CHK_ADD( len , asn1_write_tag( p, start, ASN1_OID ) );
162
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200163 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000164}
165
166int asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start,
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200167 const char *oid, size_t oid_len,
168 size_t par_len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000169{
170 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000171 size_t len = 0;
172
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200173 if( par_len == 0 )
174 ASN1_CHK_ADD( len, asn1_write_null( p, start ) );
175 else
176 len += par_len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000177
Paul Bakker5f45e622013-09-09 12:02:36 +0200178 ASN1_CHK_ADD( len, asn1_write_oid( p, start, oid, oid_len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000179
Paul Bakker5f45e622013-09-09 12:02:36 +0200180 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000181 ASN1_CHK_ADD( len, asn1_write_tag( p, start,
182 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
183
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200184 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000185}
186
Paul Bakker329def32013-09-06 16:34:38 +0200187int asn1_write_bool( unsigned char **p, unsigned char *start, int boolean )
188{
189 int ret;
190 size_t len = 0;
191
192 if( *p - start < 1 )
193 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
194
195 *--(*p) = (boolean) ? 1 : 0;
196 len++;
197
198 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
199 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_BOOLEAN ) );
200
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200201 return( (int) len );
Paul Bakker329def32013-09-06 16:34:38 +0200202}
203
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000204int asn1_write_int( unsigned char **p, unsigned char *start, int val )
205{
206 int ret;
207 size_t len = 0;
208
209 // TODO negative values and values larger than 128
210 // DER format assumes 2s complement for numbers, so the leftmost bit
211 // should be 0 for positive numbers and 1 for negative numbers.
212 //
213 if( *p - start < 1 )
214 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
215
216 len += 1;
217 *--(*p) = val;
218
Paul Bakker66d5d072014-06-17 16:39:18 +0200219 if( val > 0 && **p & 0x80 )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000220 {
221 if( *p - start < 1 )
222 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
223
224 *--(*p) = 0x00;
225 len += 1;
226 }
227
228 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
229 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_INTEGER ) );
230
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200231 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000232}
233
234int asn1_write_printable_string( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200235 const char *text, size_t text_len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000236{
237 int ret;
238 size_t len = 0;
239
Paul Bakker9852d002013-08-26 17:56:37 +0200240 ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200241 (const unsigned char *) text, text_len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000242
243 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
244 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_PRINTABLE_STRING ) );
245
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200246 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000247}
Paul Bakker598e4502013-08-25 14:46:39 +0200248
Paul Bakker05888152012-02-16 10:26:57 +0000249int asn1_write_ia5_string( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200250 const char *text, size_t text_len )
Paul Bakker05888152012-02-16 10:26:57 +0000251{
252 int ret;
253 size_t len = 0;
254
Paul Bakker9852d002013-08-26 17:56:37 +0200255 ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200256 (const unsigned char *) text, text_len ) );
Paul Bakker05888152012-02-16 10:26:57 +0000257
258 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
259 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_IA5_STRING ) );
260
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200261 return( (int) len );
Paul Bakker05888152012-02-16 10:26:57 +0000262}
Paul Bakker598e4502013-08-25 14:46:39 +0200263
264int asn1_write_bitstring( unsigned char **p, unsigned char *start,
265 const unsigned char *buf, size_t bits )
266{
267 int ret;
268 size_t len = 0, size;
269
270 size = ( bits / 8 ) + ( ( bits % 8 ) ? 1 : 0 );
271
272 // Calculate byte length
273 //
274 if( *p - start < (int) size + 1 )
275 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
276
277 len = size + 1;
278 (*p) -= size;
279 memcpy( *p, buf, size );
280
281 // Write unused bits
282 //
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200283 *--(*p) = (unsigned char) (size * 8 - bits);
Paul Bakker598e4502013-08-25 14:46:39 +0200284
285 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
286 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_BIT_STRING ) );
287
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200288 return( (int) len );
Paul Bakker598e4502013-08-25 14:46:39 +0200289}
290
291int asn1_write_octet_string( unsigned char **p, unsigned char *start,
292 const unsigned char *buf, size_t size )
293{
294 int ret;
295 size_t len = 0;
296
Paul Bakker9852d002013-08-26 17:56:37 +0200297 ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start, buf, size ) );
Paul Bakker598e4502013-08-25 14:46:39 +0200298
299 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
300 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_OCTET_STRING ) );
301
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200302 return( (int) len );
Paul Bakker598e4502013-08-25 14:46:39 +0200303}
Paul Bakker59ba59f2013-09-09 11:26:00 +0200304
305asn1_named_data *asn1_store_named_data( asn1_named_data **head,
306 const char *oid, size_t oid_len,
307 const unsigned char *val,
308 size_t val_len )
309{
310 asn1_named_data *cur;
311
312 if( ( cur = asn1_find_named_data( *head, oid, oid_len ) ) == NULL )
313 {
314 // Add new entry if not present yet based on OID
315 //
316 if( ( cur = polarssl_malloc( sizeof(asn1_named_data) ) ) == NULL )
317 return( NULL );
318
319 memset( cur, 0, sizeof(asn1_named_data) );
320
321 cur->oid.len = oid_len;
322 cur->oid.p = polarssl_malloc( oid_len );
323 if( cur->oid.p == NULL )
324 {
325 polarssl_free( cur );
326 return( NULL );
327 }
328
Manuel Pégourié-Gonnarde5b0fc12014-11-12 22:27:42 +0100329 memcpy( cur->oid.p, oid, oid_len );
330
Paul Bakker59ba59f2013-09-09 11:26:00 +0200331 cur->val.len = val_len;
332 cur->val.p = polarssl_malloc( val_len );
333 if( cur->val.p == NULL )
334 {
335 polarssl_free( cur->oid.p );
336 polarssl_free( cur );
337 return( NULL );
338 }
339
Paul Bakker59ba59f2013-09-09 11:26:00 +0200340 cur->next = *head;
341 *head = cur;
342 }
343 else if( cur->val.len < val_len )
344 {
345 // Enlarge existing value buffer if needed
346 //
347 polarssl_free( cur->val.p );
348 cur->val.p = NULL;
349
350 cur->val.len = val_len;
351 cur->val.p = polarssl_malloc( val_len );
352 if( cur->val.p == NULL )
353 {
354 polarssl_free( cur->oid.p );
355 polarssl_free( cur );
356 return( NULL );
357 }
358 }
359
360 if( val != NULL )
361 memcpy( cur->val.p, val, val_len );
362
363 return( cur );
364}
Paul Bakker9af723c2014-05-01 13:03:14 +0200365#endif /* POLARSSL_ASN1_WRITE_C */