2 * ASN.1 buffer writing functionality
4 * Copyright (C) 2006-2014, Brainspark B.V.
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
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.
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.
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.
26 #if !defined(POLARSSL_CONFIG_FILE)
29 #include POLARSSL_CONFIG_FILE
32 #if defined(POLARSSL_ASN1_WRITE_C)
34 #include "asn1write.h"
36 #if defined(POLARSSL_PLATFORM_C)
40 #define polarssl_malloc malloc
41 #define polarssl_free free
44 int asn1_write_len( unsigned char **p
, unsigned char *start
, size_t len
)
49 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL
);
51 *--(*p
) = (unsigned char) len
;
58 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL
);
60 *--(*p
) = (unsigned char) len
;
66 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL
);
68 // We assume we never have lengths larger than 65535 bytes
71 *--(*p
) = ( len
/ 256 ) % 256;
77 int asn1_write_tag( unsigned char **p
, unsigned char *start
, unsigned char tag
)
80 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL
);
87 int asn1_write_raw_buffer( unsigned char **p
, unsigned char *start
,
88 const unsigned char *buf
, size_t size
)
92 if( *p
- start
< (int) size
)
93 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL
);
97 memcpy( *p
, buf
, len
);
102 #if defined(POLARSSL_BIGNUM_C)
103 int asn1_write_mpi( unsigned char **p
, unsigned char *start
, mpi
*X
)
112 if( *p
- start
< (int) len
)
113 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL
);
116 MPI_CHK( mpi_write_binary( X
, *p
, len
) );
118 // DER format assumes 2s complement for numbers, so the leftmost bit
119 // should be 0 for positive numbers and 1 for negative numbers.
121 if ( X
->s
==1 && **p
& 0x80 )
124 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL
);
130 ASN1_CHK_ADD( len
, asn1_write_len( p
, start
, len
) );
131 ASN1_CHK_ADD( len
, asn1_write_tag( p
, start
, ASN1_INTEGER
) );
138 #endif /* POLARSSL_BIGNUM_C */
140 int asn1_write_null( unsigned char **p
, unsigned char *start
)
147 ASN1_CHK_ADD( len
, asn1_write_len( p
, start
, 0) );
148 ASN1_CHK_ADD( len
, asn1_write_tag( p
, start
, ASN1_NULL
) );
153 int asn1_write_oid( unsigned char **p
, unsigned char *start
,
154 const char *oid
, size_t oid_len
)
159 ASN1_CHK_ADD( len
, asn1_write_raw_buffer( p
, start
,
160 (const unsigned char *) oid
, oid_len
) );
161 ASN1_CHK_ADD( len
, asn1_write_len( p
, start
, len
) );
162 ASN1_CHK_ADD( len
, asn1_write_tag( p
, start
, ASN1_OID
) );
167 int asn1_write_algorithm_identifier( unsigned char **p
, unsigned char *start
,
168 const char *oid
, size_t oid_len
,
175 ASN1_CHK_ADD( len
, asn1_write_null( p
, start
) );
179 ASN1_CHK_ADD( len
, asn1_write_oid( p
, start
, oid
, oid_len
) );
181 ASN1_CHK_ADD( len
, asn1_write_len( p
, start
, len
) );
182 ASN1_CHK_ADD( len
, asn1_write_tag( p
, start
,
183 ASN1_CONSTRUCTED
| ASN1_SEQUENCE
) );
188 int asn1_write_bool( unsigned char **p
, unsigned char *start
, int boolean
)
194 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL
);
196 *--(*p
) = (boolean
) ? 1 : 0;
199 ASN1_CHK_ADD( len
, asn1_write_len( p
, start
, len
) );
200 ASN1_CHK_ADD( len
, asn1_write_tag( p
, start
, ASN1_BOOLEAN
) );
205 int asn1_write_int( unsigned char **p
, unsigned char *start
, int val
)
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.
215 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL
);
220 if ( val
> 0 && **p
& 0x80 )
223 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL
);
229 ASN1_CHK_ADD( len
, asn1_write_len( p
, start
, len
) );
230 ASN1_CHK_ADD( len
, asn1_write_tag( p
, start
, ASN1_INTEGER
) );
235 int asn1_write_printable_string( unsigned char **p
, unsigned char *start
,
236 const char *text
, size_t text_len
)
241 ASN1_CHK_ADD( len
, asn1_write_raw_buffer( p
, start
,
242 (const unsigned char *) text
, text_len
) );
244 ASN1_CHK_ADD( len
, asn1_write_len( p
, start
, len
) );
245 ASN1_CHK_ADD( len
, asn1_write_tag( p
, start
, ASN1_PRINTABLE_STRING
) );
250 int asn1_write_ia5_string( unsigned char **p
, unsigned char *start
,
251 const char *text
, size_t text_len
)
256 ASN1_CHK_ADD( len
, asn1_write_raw_buffer( p
, start
,
257 (const unsigned char *) text
, text_len
) );
259 ASN1_CHK_ADD( len
, asn1_write_len( p
, start
, len
) );
260 ASN1_CHK_ADD( len
, asn1_write_tag( p
, start
, ASN1_IA5_STRING
) );
265 int asn1_write_bitstring( unsigned char **p
, unsigned char *start
,
266 const unsigned char *buf
, size_t bits
)
269 size_t len
= 0, size
;
271 size
= ( bits
/ 8 ) + ( ( bits
% 8 ) ? 1 : 0 );
273 // Calculate byte length
275 if( *p
- start
< (int) size
+ 1 )
276 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL
);
280 memcpy( *p
, buf
, size
);
284 *--(*p
) = (unsigned char) (size
* 8 - bits
);
286 ASN1_CHK_ADD( len
, asn1_write_len( p
, start
, len
) );
287 ASN1_CHK_ADD( len
, asn1_write_tag( p
, start
, ASN1_BIT_STRING
) );
292 int asn1_write_octet_string( unsigned char **p
, unsigned char *start
,
293 const unsigned char *buf
, size_t size
)
298 ASN1_CHK_ADD( len
, asn1_write_raw_buffer( p
, start
, buf
, size
) );
300 ASN1_CHK_ADD( len
, asn1_write_len( p
, start
, len
) );
301 ASN1_CHK_ADD( len
, asn1_write_tag( p
, start
, ASN1_OCTET_STRING
) );
306 asn1_named_data
*asn1_store_named_data( asn1_named_data
**head
,
307 const char *oid
, size_t oid_len
,
308 const unsigned char *val
,
311 asn1_named_data
*cur
;
313 if( ( cur
= asn1_find_named_data( *head
, oid
, oid_len
) ) == NULL
)
315 // Add new entry if not present yet based on OID
317 if( ( cur
= polarssl_malloc( sizeof(asn1_named_data
) ) ) == NULL
)
320 memset( cur
, 0, sizeof(asn1_named_data
) );
322 cur
->oid
.len
= oid_len
;
323 cur
->oid
.p
= polarssl_malloc( oid_len
);
324 if( cur
->oid
.p
== NULL
)
326 polarssl_free( cur
);
330 cur
->val
.len
= val_len
;
331 cur
->val
.p
= polarssl_malloc( val_len
);
332 if( cur
->val
.p
== NULL
)
334 polarssl_free( cur
->oid
.p
);
335 polarssl_free( cur
);
339 memcpy( cur
->oid
.p
, oid
, oid_len
);
344 else if( cur
->val
.len
< val_len
)
346 // Enlarge existing value buffer if needed
348 polarssl_free( cur
->val
.p
);
351 cur
->val
.len
= val_len
;
352 cur
->val
.p
= polarssl_malloc( val_len
);
353 if( cur
->val
.p
== NULL
)
355 polarssl_free( cur
->oid
.p
);
356 polarssl_free( cur
);
362 memcpy( cur
->val
.p
, val
, val_len
);
366 #endif /* POLARSSL_ASN1_WRITE_C */