PolarSSL v1.3.7
asn1write.c
Go to the documentation of this file.
1 /*
2  * ASN.1 buffer writing functionality
3  *
4  * Copyright (C) 2006-2014, 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 #if !defined(POLARSSL_CONFIG_FILE)
27 #include "polarssl/config.h"
28 #else
29 #include POLARSSL_CONFIG_FILE
30 #endif
31 
32 #if defined(POLARSSL_ASN1_WRITE_C)
33 
34 #include "polarssl/asn1write.h"
35 
36 #if defined(POLARSSL_PLATFORM_C)
37 #include "polarssl/platform.h"
38 #else
39 #include <stdlib.h>
40 #define polarssl_malloc malloc
41 #define polarssl_free free
42 #endif
43 
44 int asn1_write_len( unsigned char **p, unsigned char *start, size_t len )
45 {
46  if( len < 0x80 )
47  {
48  if( *p - start < 1 )
50 
51  *--(*p) = (unsigned char) len;
52  return( 1 );
53  }
54 
55  if( len <= 0xFF )
56  {
57  if( *p - start < 2 )
59 
60  *--(*p) = (unsigned char) len;
61  *--(*p) = 0x81;
62  return( 2 );
63  }
64 
65  if( *p - start < 3 )
67 
68  // We assume we never have lengths larger than 65535 bytes
69  //
70  *--(*p) = len % 256;
71  *--(*p) = ( len / 256 ) % 256;
72  *--(*p) = 0x82;
73 
74  return( 3 );
75 }
76 
77 int asn1_write_tag( unsigned char **p, unsigned char *start, unsigned char tag )
78 {
79  if( *p - start < 1 )
81 
82  *--(*p) = tag;
83 
84  return( 1 );
85 }
86 
87 int asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
88  const unsigned char *buf, size_t size )
89 {
90  size_t len = 0;
91 
92  if( *p - start < (int) size )
94 
95  len = size;
96  (*p) -= len;
97  memcpy( *p, buf, len );
98 
99  return( (int) len );
100 }
101 
102 #if defined(POLARSSL_BIGNUM_C)
103 int asn1_write_mpi( unsigned char **p, unsigned char *start, mpi *X )
104 {
105  int ret;
106  size_t len = 0;
107 
108  // Write the MPI
109  //
110  len = mpi_size( X );
111 
112  if( *p - start < (int) len )
114 
115  (*p) -= len;
116  MPI_CHK( mpi_write_binary( X, *p, len ) );
117 
118  // DER format assumes 2s complement for numbers, so the leftmost bit
119  // should be 0 for positive numbers and 1 for negative numbers.
120  //
121  if ( X->s ==1 && **p & 0x80 )
122  {
123  if( *p - start < 1 )
125 
126  *--(*p) = 0x00;
127  len += 1;
128  }
129 
130  ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
131  ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_INTEGER ) );
132 
133  ret = (int) len;
134 
135 cleanup:
136  return( ret );
137 }
138 #endif /* POLARSSL_BIGNUM_C */
139 
140 int asn1_write_null( unsigned char **p, unsigned char *start )
141 {
142  int ret;
143  size_t len = 0;
144 
145  // Write NULL
146  //
147  ASN1_CHK_ADD( len, asn1_write_len( p, start, 0) );
148  ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_NULL ) );
149 
150  return( (int) len );
151 }
152 
153 int asn1_write_oid( unsigned char **p, unsigned char *start,
154  const char *oid, size_t oid_len )
155 {
156  int ret;
157  size_t len = 0;
158 
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 ) );
163 
164  return( (int) len );
165 }
166 
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 )
170 {
171  int ret;
172  size_t len = 0;
173 
174  if( par_len == 0 )
175  ASN1_CHK_ADD( len, asn1_write_null( p, start ) );
176  else
177  len += par_len;
178 
179  ASN1_CHK_ADD( len, asn1_write_oid( p, start, oid, oid_len ) );
180 
181  ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
182  ASN1_CHK_ADD( len, asn1_write_tag( p, start,
184 
185  return( (int) len );
186 }
187 
188 int asn1_write_bool( unsigned char **p, unsigned char *start, int boolean )
189 {
190  int ret;
191  size_t len = 0;
192 
193  if( *p - start < 1 )
195 
196  *--(*p) = (boolean) ? 1 : 0;
197  len++;
198 
199  ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
200  ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_BOOLEAN ) );
201 
202  return( (int) len );
203 }
204 
205 int asn1_write_int( unsigned char **p, unsigned char *start, int val )
206 {
207  int ret;
208  size_t len = 0;
209 
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.
213  //
214  if( *p - start < 1 )
216 
217  len += 1;
218  *--(*p) = val;
219 
220  if ( val > 0 && **p & 0x80 )
221  {
222  if( *p - start < 1 )
224 
225  *--(*p) = 0x00;
226  len += 1;
227  }
228 
229  ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
230  ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_INTEGER ) );
231 
232  return( (int) len );
233 }
234 
235 int asn1_write_printable_string( unsigned char **p, unsigned char *start,
236  const char *text, size_t text_len )
237 {
238  int ret;
239  size_t len = 0;
240 
241  ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start,
242  (const unsigned char *) text, text_len ) );
243 
244  ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
246 
247  return( (int) len );
248 }
249 
250 int asn1_write_ia5_string( unsigned char **p, unsigned char *start,
251  const char *text, size_t text_len )
252 {
253  int ret;
254  size_t len = 0;
255 
256  ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start,
257  (const unsigned char *) text, text_len ) );
258 
259  ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
260  ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_IA5_STRING ) );
261 
262  return( (int) len );
263 }
264 
265 int asn1_write_bitstring( unsigned char **p, unsigned char *start,
266  const unsigned char *buf, size_t bits )
267 {
268  int ret;
269  size_t len = 0, size;
270 
271  size = ( bits / 8 ) + ( ( bits % 8 ) ? 1 : 0 );
272 
273  // Calculate byte length
274  //
275  if( *p - start < (int) size + 1 )
277 
278  len = size + 1;
279  (*p) -= size;
280  memcpy( *p, buf, size );
281 
282  // Write unused bits
283  //
284  *--(*p) = (unsigned char) (size * 8 - bits);
285 
286  ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
287  ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_BIT_STRING ) );
288 
289  return( (int) len );
290 }
291 
292 int asn1_write_octet_string( unsigned char **p, unsigned char *start,
293  const unsigned char *buf, size_t size )
294 {
295  int ret;
296  size_t len = 0;
297 
298  ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start, buf, size ) );
299 
300  ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
301  ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_OCTET_STRING ) );
302 
303  return( (int) len );
304 }
305 
307  const char *oid, size_t oid_len,
308  const unsigned char *val,
309  size_t val_len )
310 {
311  asn1_named_data *cur;
312 
313  if( ( cur = asn1_find_named_data( *head, oid, oid_len ) ) == NULL )
314  {
315  // Add new entry if not present yet based on OID
316  //
317  if( ( cur = polarssl_malloc( sizeof(asn1_named_data) ) ) == NULL )
318  return( NULL );
319 
320  memset( cur, 0, sizeof(asn1_named_data) );
321 
322  cur->oid.len = oid_len;
323  cur->oid.p = polarssl_malloc( oid_len );
324  if( cur->oid.p == NULL )
325  {
326  polarssl_free( cur );
327  return( NULL );
328  }
329 
330  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 
339  memcpy( cur->oid.p, oid, oid_len );
340 
341  cur->next = *head;
342  *head = cur;
343  }
344  else if( cur->val.len < val_len )
345  {
346  // Enlarge existing value buffer if needed
347  //
348  polarssl_free( cur->val.p );
349  cur->val.p = NULL;
350 
351  cur->val.len = val_len;
352  cur->val.p = polarssl_malloc( val_len );
353  if( cur->val.p == NULL )
354  {
355  polarssl_free( cur->oid.p );
356  polarssl_free( cur );
357  return( NULL );
358  }
359  }
360 
361  if( val != NULL )
362  memcpy( cur->val.p, val, val_len );
363 
364  return( cur );
365 }
366 #endif /* POLARSSL_ASN1_WRITE_C */
#define ASN1_NULL
Definition: asn1.h:79
#define ASN1_PRINTABLE_STRING
Definition: asn1.h:84
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...
#define ASN1_OID
Definition: asn1.h:80
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...
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:183
#define POLARSSL_ERR_ASN1_BUF_TOO_SMALL
Buffer too small when writing ASN.1 data structure.
Definition: asn1.h:60
#define polarssl_free
Definition: platform.h:91
#define ASN1_SEQUENCE
Definition: asn1.h:82
Configuration options (set of defines)
#define ASN1_CONSTRUCTED
Definition: asn1.h:92
MPI structure.
Definition: bignum.h:181
PolarSSL Platform abstraction layer.
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:155
asn1_buf oid
The object identifier.
Definition: asn1.h:154
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:76
unsigned char * p
ASN1 data, e.g.
Definition: asn1.h:124
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:77
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:152
size_t len
ASN1 length, e.g.
Definition: asn1.h:123
#define ASN1_CHK_ADD(g, f)
Definition: asn1write.h:32
#define ASN1_IA5_STRING
Definition: asn1.h:86
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:75
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:156
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:78
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.
#define polarssl_malloc
Definition: platform.h:90
#define MPI_CHK(f)
Definition: bignum.h:65