new polarssl
[syren.git] / src / libpolarssl / asn1write.c
blob89c23368a96253a16f812475ccb2b30f53220fc3
1 /*
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>
9 * All rights reserved.
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)
27 #include "config.h"
28 #else
29 #include POLARSSL_CONFIG_FILE
30 #endif
32 #if defined(POLARSSL_ASN1_WRITE_C)
34 #include "asn1write.h"
36 #if defined(POLARSSL_PLATFORM_C)
37 #include "platform.h"
38 #else
39 #include <stdlib.h>
40 #define polarssl_malloc malloc
41 #define polarssl_free free
42 #endif
44 int asn1_write_len( unsigned char **p, unsigned char *start, size_t len )
46 if( len < 0x80 )
48 if( *p - start < 1 )
49 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
51 *--(*p) = (unsigned char) len;
52 return( 1 );
55 if( len <= 0xFF )
57 if( *p - start < 2 )
58 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
60 *--(*p) = (unsigned char) len;
61 *--(*p) = 0x81;
62 return( 2 );
65 if( *p - start < 3 )
66 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
68 // We assume we never have lengths larger than 65535 bytes
70 *--(*p) = len % 256;
71 *--(*p) = ( len / 256 ) % 256;
72 *--(*p) = 0x82;
74 return( 3 );
77 int asn1_write_tag( unsigned char **p, unsigned char *start, unsigned char tag )
79 if( *p - start < 1 )
80 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
82 *--(*p) = tag;
84 return( 1 );
87 int asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
88 const unsigned char *buf, size_t size )
90 size_t len = 0;
92 if( *p - start < (int) size )
93 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
95 len = size;
96 (*p) -= len;
97 memcpy( *p, buf, len );
99 return( (int) len );
102 #if defined(POLARSSL_BIGNUM_C)
103 int asn1_write_mpi( unsigned char **p, unsigned char *start, mpi *X )
105 int ret;
106 size_t len = 0;
108 // Write the MPI
110 len = mpi_size( X );
112 if( *p - start < (int) len )
113 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
115 (*p) -= len;
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 )
123 if( *p - start < 1 )
124 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
126 *--(*p) = 0x00;
127 len += 1;
130 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
131 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_INTEGER ) );
133 ret = (int) len;
135 cleanup:
136 return( ret );
138 #endif /* POLARSSL_BIGNUM_C */
140 int asn1_write_null( unsigned char **p, unsigned char *start )
142 int ret;
143 size_t len = 0;
145 // Write NULL
147 ASN1_CHK_ADD( len, asn1_write_len( p, start, 0) );
148 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_NULL ) );
150 return( (int) len );
153 int asn1_write_oid( unsigned char **p, unsigned char *start,
154 const char *oid, size_t oid_len )
156 int ret;
157 size_t len = 0;
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 ) );
164 return( (int) len );
167 int asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start,
168 const char *oid, size_t oid_len,
169 size_t par_len )
171 int ret;
172 size_t len = 0;
174 if( par_len == 0 )
175 ASN1_CHK_ADD( len, asn1_write_null( p, start ) );
176 else
177 len += par_len;
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 ) );
185 return( (int) len );
188 int asn1_write_bool( unsigned char **p, unsigned char *start, int boolean )
190 int ret;
191 size_t len = 0;
193 if( *p - start < 1 )
194 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
196 *--(*p) = (boolean) ? 1 : 0;
197 len++;
199 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
200 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_BOOLEAN ) );
202 return( (int) len );
205 int asn1_write_int( unsigned char **p, unsigned char *start, int val )
207 int ret;
208 size_t len = 0;
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.
214 if( *p - start < 1 )
215 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
217 len += 1;
218 *--(*p) = val;
220 if ( val > 0 && **p & 0x80 )
222 if( *p - start < 1 )
223 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
225 *--(*p) = 0x00;
226 len += 1;
229 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
230 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_INTEGER ) );
232 return( (int) len );
235 int asn1_write_printable_string( unsigned char **p, unsigned char *start,
236 const char *text, size_t text_len )
238 int ret;
239 size_t len = 0;
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 ) );
247 return( (int) len );
250 int asn1_write_ia5_string( unsigned char **p, unsigned char *start,
251 const char *text, size_t text_len )
253 int ret;
254 size_t len = 0;
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 ) );
262 return( (int) len );
265 int asn1_write_bitstring( unsigned char **p, unsigned char *start,
266 const unsigned char *buf, size_t bits )
268 int ret;
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 );
278 len = size + 1;
279 (*p) -= size;
280 memcpy( *p, buf, size );
282 // Write unused bits
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 ) );
289 return( (int) len );
292 int asn1_write_octet_string( unsigned char **p, unsigned char *start,
293 const unsigned char *buf, size_t size )
295 int ret;
296 size_t len = 0;
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 ) );
303 return( (int) len );
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,
309 size_t val_len )
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 )
318 return( 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 );
327 return( NULL );
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 );
336 return( NULL );
339 memcpy( cur->oid.p, oid, oid_len );
341 cur->next = *head;
342 *head = cur;
344 else if( cur->val.len < val_len )
346 // Enlarge existing value buffer if needed
348 polarssl_free( cur->val.p );
349 cur->val.p = NULL;
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 );
357 return( NULL );
361 if( val != NULL )
362 memcpy( cur->val.p, val, val_len );
364 return( cur );
366 #endif /* POLARSSL_ASN1_WRITE_C */