PolarSSL v1.3.3
asn1write.c
Go to the documentation of this file.
1 /*
2  * ASN.1 buffer writing functionality
3  *
4  * Copyright (C) 2006-2012, Brainspark B.V.
5  *
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 
32 #if defined(POLARSSL_MEMORY_C)
33 #include "polarssl/memory.h"
34 #else
35 #include <stdlib.h>
36 #define polarssl_malloc malloc
37 #define polarssl_free free
38 #endif
39 
40 int asn1_write_len( unsigned char **p, unsigned char *start, size_t len )
41 {
42  if( len < 0x80 )
43  {
44  if( *p - start < 1 )
46 
47  *--(*p) = (unsigned char) len;
48  return( 1 );
49  }
50 
51  if( len <= 0xFF )
52  {
53  if( *p - start < 2 )
55 
56  *--(*p) = (unsigned char) len;
57  *--(*p) = 0x81;
58  return( 2 );
59  }
60 
61  if( *p - start < 3 )
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 
73 int asn1_write_tag( unsigned char **p, unsigned char *start, unsigned char tag )
74 {
75  if( *p - start < 1 )
77 
78  *--(*p) = tag;
79 
80  return( 1 );
81 }
82 
83 int 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 )
90 
91  len = size;
92  (*p) -= len;
93  memcpy( *p, buf, len );
94 
95  return( (int) len );
96 }
97 
98 #if defined(POLARSSL_BIGNUM_C)
99 int 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 )
110 
111  (*p) -= len;
112  mpi_write_binary( X, *p, len );
113 
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 )
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 
129  return( (int) len );
130 }
131 #endif /* POLARSSL_BIGNUM_C */
132 
133 int asn1_write_null( unsigned char **p, unsigned char *start )
134 {
135  int ret;
136  size_t len = 0;
137 
138  // Write NULL
139  //
140  ASN1_CHK_ADD( len, asn1_write_len( p, start, 0) );
141  ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_NULL ) );
142 
143  return( (int) len );
144 }
145 
146 int asn1_write_oid( unsigned char **p, unsigned char *start,
147  const char *oid, size_t oid_len )
148 {
149  int ret;
150  size_t len = 0;
151 
152  ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start,
153  (const unsigned char *) oid, oid_len ) );
154  ASN1_CHK_ADD( len , asn1_write_len( p, start, len ) );
155  ASN1_CHK_ADD( len , asn1_write_tag( p, start, ASN1_OID ) );
156 
157  return( (int) len );
158 }
159 
160 int asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start,
161  const char *oid, size_t oid_len,
162  size_t par_len )
163 {
164  int ret;
165  size_t len = 0;
166 
167  if( par_len == 0 )
168  ASN1_CHK_ADD( len, asn1_write_null( p, start ) );
169  else
170  len += par_len;
171 
172  ASN1_CHK_ADD( len, asn1_write_oid( p, start, oid, oid_len ) );
173 
174  ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
175  ASN1_CHK_ADD( len, asn1_write_tag( p, start,
177 
178  return( (int) len );
179 }
180 
181 int asn1_write_bool( unsigned char **p, unsigned char *start, int boolean )
182 {
183  int ret;
184  size_t len = 0;
185 
186  if( *p - start < 1 )
188 
189  *--(*p) = (boolean) ? 1 : 0;
190  len++;
191 
192  ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
193  ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_BOOLEAN ) );
194 
195  return( (int) len );
196 }
197 
198 int asn1_write_int( unsigned char **p, unsigned char *start, int val )
199 {
200  int ret;
201  size_t len = 0;
202 
203  // TODO negative values and values larger than 128
204  // DER format assumes 2s complement for numbers, so the leftmost bit
205  // should be 0 for positive numbers and 1 for negative numbers.
206  //
207  if( *p - start < 1 )
209 
210  len += 1;
211  *--(*p) = val;
212 
213  if ( val > 0 && **p & 0x80 )
214  {
215  if( *p - start < 1 )
217 
218  *--(*p) = 0x00;
219  len += 1;
220  }
221 
222  ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
223  ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_INTEGER ) );
224 
225  return( (int) len );
226 }
227 
228 int asn1_write_printable_string( unsigned char **p, unsigned char *start,
229  const char *text, size_t text_len )
230 {
231  int ret;
232  size_t len = 0;
233 
234  ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start,
235  (const unsigned char *) text, text_len ) );
236 
237  ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
239 
240  return( (int) len );
241 }
242 
243 int asn1_write_ia5_string( unsigned char **p, unsigned char *start,
244  const char *text, size_t text_len )
245 {
246  int ret;
247  size_t len = 0;
248 
249  ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start,
250  (const unsigned char *) text, text_len ) );
251 
252  ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
253  ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_IA5_STRING ) );
254 
255  return( (int) len );
256 }
257 
258 int asn1_write_bitstring( unsigned char **p, unsigned char *start,
259  const unsigned char *buf, size_t bits )
260 {
261  int ret;
262  size_t len = 0, size;
263 
264  size = ( bits / 8 ) + ( ( bits % 8 ) ? 1 : 0 );
265 
266  // Calculate byte length
267  //
268  if( *p - start < (int) size + 1 )
270 
271  len = size + 1;
272  (*p) -= size;
273  memcpy( *p, buf, size );
274 
275  // Write unused bits
276  //
277  *--(*p) = (unsigned char) (size * 8 - bits);
278 
279  ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
280  ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_BIT_STRING ) );
281 
282  return( (int) len );
283 }
284 
285 int asn1_write_octet_string( unsigned char **p, unsigned char *start,
286  const unsigned char *buf, size_t size )
287 {
288  int ret;
289  size_t len = 0;
290 
291  ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start, buf, size ) );
292 
293  ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
294  ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_OCTET_STRING ) );
295 
296  return( (int) len );
297 }
298 
300  const char *oid, size_t oid_len,
301  const unsigned char *val,
302  size_t val_len )
303 {
304  asn1_named_data *cur;
305 
306  if( ( cur = asn1_find_named_data( *head, oid, oid_len ) ) == NULL )
307  {
308  // Add new entry if not present yet based on OID
309  //
310  if( ( cur = polarssl_malloc( sizeof(asn1_named_data) ) ) == NULL )
311  return( NULL );
312 
313  memset( cur, 0, sizeof(asn1_named_data) );
314 
315  cur->oid.len = oid_len;
316  cur->oid.p = polarssl_malloc( oid_len );
317  if( cur->oid.p == NULL )
318  {
319  polarssl_free( cur );
320  return( NULL );
321  }
322 
323  cur->val.len = val_len;
324  cur->val.p = polarssl_malloc( val_len );
325  if( cur->val.p == NULL )
326  {
327  polarssl_free( cur->oid.p );
328  polarssl_free( cur );
329  return( NULL );
330  }
331 
332  memcpy( cur->oid.p, oid, oid_len );
333 
334  cur->next = *head;
335  *head = cur;
336  }
337  else if( cur->val.len < val_len )
338  {
339  // Enlarge existing value buffer if needed
340  //
341  polarssl_free( cur->val.p );
342  cur->val.p = NULL;
343 
344  cur->val.len = val_len;
345  cur->val.p = polarssl_malloc( val_len );
346  if( cur->val.p == NULL )
347  {
348  polarssl_free( cur->oid.p );
349  polarssl_free( cur );
350  return( NULL );
351  }
352  }
353 
354  if( val != NULL )
355  memcpy( cur->val.p, val, val_len );
356 
357  return( cur );
358 }
359 #endif
#define ASN1_NULL
Definition: asn1.h:75
#define ASN1_PRINTABLE_STRING
Definition: asn1.h:80
int asn1_write_octet_string(unsigned char **p, unsigned char *start, const unsigned char *buf, size_t size)
Write an octet string tag (ASN1_OCTET_STRING) and value in ASN.1 format Note: function works backward...
Memory allocation layer.
#define ASN1_OID
Definition: asn1.h:76
int asn1_write_ia5_string(unsigned char **p, unsigned char *start, const char *text, size_t text_len)
Write an IA5 string tag (ASN1_IA5_STRING) and value in ASN.1 format Note: function works backwards in...
void *(* polarssl_malloc)(size_t len)
asn1_named_data * asn1_store_named_data(asn1_named_data **list, const char *oid, size_t oid_len, const unsigned char *val, size_t val_len)
Create or find a specific named_data entry for writing in a sequence or list based on the OID...
int s
Definition: bignum.h:179
#define POLARSSL_ERR_ASN1_BUF_TOO_SMALL
Buffer too small when writing ASN.1 data structure.
Definition: asn1.h:56
#define ASN1_SEQUENCE
Definition: asn1.h:78
Configuration options (set of defines)
#define ASN1_CONSTRUCTED
Definition: asn1.h:88
MPI structure.
Definition: bignum.h:177
int asn1_write_len(unsigned char **p, unsigned char *start, size_t len)
Write a length field in ASN.1 format Note: function works backwards in data buffer.
int asn1_write_printable_string(unsigned char **p, unsigned char *start, const char *text, size_t text_len)
Write a printable string tag (ASN1_PRINTABLE_STRING) and value in ASN.1 format Note: function works b...
asn1_buf val
The named value.
Definition: asn1.h:151
asn1_buf oid
The object identifier.
Definition: asn1.h:150
int asn1_write_raw_buffer(unsigned char **p, unsigned char *start, const unsigned char *buf, size_t size)
Write raw buffer data Note: function works backwards in data buffer.
#define ASN1_INTEGER
Definition: asn1.h:72
void(* polarssl_free)(void *ptr)
unsigned char * p
ASN1 data, e.g.
Definition: asn1.h:120
int asn1_write_null(unsigned char **p, unsigned char *start)
Write a NULL tag (ASN1_NULL) with zero data in ASN.1 format Note: function works backwards in data bu...
#define ASN1_BIT_STRING
Definition: asn1.h:73
int asn1_write_mpi(unsigned char **p, unsigned char *start, mpi *X)
Write a big number (ASN1_INTEGER) in ASN.1 format Note: function works backwards in data buffer...
int asn1_write_bitstring(unsigned char **p, unsigned char *start, const unsigned char *buf, size_t bits)
Write a bitstring tag (ASN1_BIT_STRING) and value in ASN.1 format Note: function works backwards in d...
int asn1_write_int(unsigned char **p, unsigned char *start, int val)
Write an int tag (ASN1_INTEGER) and value in ASN.1 format Note: function works backwards in data buff...
Container for a sequence or list of &#39;named&#39; ASN.1 data items.
Definition: asn1.h:148
size_t len
ASN1 length, e.g.
Definition: asn1.h:119
#define ASN1_CHK_ADD(g, f)
Definition: asn1write.h:32
#define ASN1_IA5_STRING
Definition: asn1.h:82
int asn1_write_algorithm_identifier(unsigned char **p, unsigned char *start, const char *oid, size_t oid_len, size_t par_len)
Write an AlgorithmIdentifier sequence in ASN.1 format Note: function works backwards in data buffer...
#define ASN1_BOOLEAN
Definition: asn1.h:71
size_t mpi_size(const mpi *X)
Return the total size in bytes.
int asn1_write_bool(unsigned char **p, unsigned char *start, int boolean)
Write a boolean tag (ASN1_BOOLEAN) and value in ASN.1 format Note: function works backwards in data b...
int mpi_write_binary(const mpi *X, unsigned char *buf, size_t buflen)
Export X into unsigned binary data, big endian.
asn1_named_data * asn1_find_named_data(asn1_named_data *list, const char *oid, size_t len)
Find a specific named_data entry in a sequence or list based on the OID.
struct _asn1_named_data * next
The next entry in the sequence.
Definition: asn1.h:152
ASN.1 buffer writing functionality.
int asn1_write_oid(unsigned char **p, unsigned char *start, const char *oid, size_t oid_len)
Write an OID tag (ASN1_OID) and data in ASN.1 format Note: function works backwards in data buffer...
#define ASN1_OCTET_STRING
Definition: asn1.h:74
int asn1_write_tag(unsigned char **p, unsigned char *start, unsigned char tag)
Write a ASN.1 tag in ASN.1 format Note: function works backwards in data buffer.