blob: 893841f8045c5449866b5339d0a8a58304ea2d43 [file] [log] [blame]
Paul Bakkerbdb912d2012-02-13 23:11:30 +00001/*
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
32int asn1_write_len( unsigned char **p, unsigned char *start, size_t len )
33{
34 if( len < 0x80 )
35 {
36 if( *p - start < 1 )
37 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
38
39 *--(*p) = len;
40 return( 1 );
41 }
42
43 if( len <= 0xFF )
44 {
45 if( *p - start < 2 )
46 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
47
48 *--(*p) = len;
49 *--(*p) = 0x81;
50 return( 2 );
51 }
52
53 if( *p - start < 3 )
54 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
55
56 // We assume we never have lengths larger than 65535 bytes
57 //
58 *--(*p) = len % 256;
59 *--(*p) = ( len / 256 ) % 256;
60 *--(*p) = 0x82;
61
62 return( 3 );
63}
64
65int asn1_write_tag( unsigned char **p, unsigned char *start, unsigned char tag )
66{
67 if( *p - start < 1 )
68 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
69
70 *--(*p) = tag;
71
72 return( 1 );
73}
74
Paul Bakker9852d002013-08-26 17:56:37 +020075int asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
76 const unsigned char *buf, size_t size )
77{
78 size_t len = 0;
79
80 if( *p - start < (int) size )
81 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
82
83 len = size;
84 (*p) -= len;
85 memcpy( *p, buf, len );
86
87 return( len );
88}
89
Paul Bakkered27a042013-04-18 22:46:23 +020090#if defined(POLARSSL_BIGNUM_C)
Paul Bakkerbdb912d2012-02-13 23:11:30 +000091int asn1_write_mpi( unsigned char **p, unsigned char *start, mpi *X )
92{
93 int ret;
94 size_t len = 0;
95
96 // Write the MPI
97 //
98 len = mpi_size( X );
99
100 if( *p - start < (int) len )
101 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
102
103 (*p) -= len;
104 mpi_write_binary( X, *p, len );
105
106 // DER format assumes 2s complement for numbers, so the leftmost bit
107 // should be 0 for positive numbers and 1 for negative numbers.
108 //
109 if ( X->s ==1 && **p & 0x80 )
110 {
111 if( *p - start < 1 )
112 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
113
114 *--(*p) = 0x00;
115 len += 1;
116 }
117
118 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
119 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_INTEGER ) );
120
121 return( len );
122}
Paul Bakkered27a042013-04-18 22:46:23 +0200123#endif /* POLARSSL_BIGNUM_C */
124
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000125int asn1_write_null( unsigned char **p, unsigned char *start )
126{
127 int ret;
128 size_t len = 0;
129
130 // Write NULL
131 //
132 ASN1_CHK_ADD( len, asn1_write_len( p, start, 0) );
133 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_NULL ) );
134
135 return( len );
136}
137
Paul Bakker37de6be2013-04-07 13:11:31 +0200138int asn1_write_oid( unsigned char **p, unsigned char *start, const char *oid )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000139{
140 int ret;
141 size_t len = 0;
142
Paul Bakker9852d002013-08-26 17:56:37 +0200143 ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start,
144 (const unsigned char *) oid, strlen( oid ) ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000145
146 ASN1_CHK_ADD( len , asn1_write_len( p, start, len ) );
147 ASN1_CHK_ADD( len , asn1_write_tag( p, start, ASN1_OID ) );
148
149 return( len );
150}
151
152int asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start,
Paul Bakker7accbce2013-08-26 17:34:53 +0200153 const char *oid )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000154{
155 int ret;
156 size_t null_len = 0;
157 size_t oid_len = 0;
158 size_t len = 0;
159
160 // Write NULL
161 //
162 ASN1_CHK_ADD( null_len, asn1_write_null( p, start ) );
163
164 // Write OID
165 //
Paul Bakker7accbce2013-08-26 17:34:53 +0200166 ASN1_CHK_ADD( oid_len, asn1_write_oid( p, start, oid ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000167
168 len = oid_len + null_len;
169 ASN1_CHK_ADD( len, asn1_write_len( p, start, oid_len + null_len ) );
170 ASN1_CHK_ADD( len, asn1_write_tag( p, start,
171 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
172
173 return( len );
174}
175
Paul Bakker329def32013-09-06 16:34:38 +0200176int asn1_write_bool( unsigned char **p, unsigned char *start, int boolean )
177{
178 int ret;
179 size_t len = 0;
180
181 if( *p - start < 1 )
182 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
183
184 *--(*p) = (boolean) ? 1 : 0;
185 len++;
186
187 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
188 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_BOOLEAN ) );
189
190 return( len );
191}
192
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000193int asn1_write_int( unsigned char **p, unsigned char *start, int val )
194{
195 int ret;
196 size_t len = 0;
197
198 // TODO negative values and values larger than 128
199 // DER format assumes 2s complement for numbers, so the leftmost bit
200 // should be 0 for positive numbers and 1 for negative numbers.
201 //
202 if( *p - start < 1 )
203 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
204
205 len += 1;
206 *--(*p) = val;
207
208 if ( val > 0 && **p & 0x80 )
209 {
210 if( *p - start < 1 )
211 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
212
213 *--(*p) = 0x00;
214 len += 1;
215 }
216
217 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
218 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_INTEGER ) );
219
220 return( len );
221}
222
223int asn1_write_printable_string( unsigned char **p, unsigned char *start,
224 char *text )
225{
226 int ret;
227 size_t len = 0;
228
Paul Bakker9852d002013-08-26 17:56:37 +0200229 ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start,
230 (const unsigned char *) text, strlen( text ) ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000231
232 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
233 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_PRINTABLE_STRING ) );
234
235 return( len );
236}
Paul Bakker598e4502013-08-25 14:46:39 +0200237
Paul Bakker05888152012-02-16 10:26:57 +0000238int asn1_write_ia5_string( unsigned char **p, unsigned char *start,
Paul Bakker598e4502013-08-25 14:46:39 +0200239 char *text )
Paul Bakker05888152012-02-16 10:26:57 +0000240{
241 int ret;
242 size_t len = 0;
243
Paul Bakker9852d002013-08-26 17:56:37 +0200244 ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start,
245 (const unsigned char *) text, strlen( text ) ) );
Paul Bakker05888152012-02-16 10:26:57 +0000246
247 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
248 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_IA5_STRING ) );
249
250 return( len );
251}
Paul Bakker598e4502013-08-25 14:46:39 +0200252
253int asn1_write_bitstring( unsigned char **p, unsigned char *start,
254 const unsigned char *buf, size_t bits )
255{
256 int ret;
257 size_t len = 0, size;
258
259 size = ( bits / 8 ) + ( ( bits % 8 ) ? 1 : 0 );
260
261 // Calculate byte length
262 //
263 if( *p - start < (int) size + 1 )
264 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
265
266 len = size + 1;
267 (*p) -= size;
268 memcpy( *p, buf, size );
269
270 // Write unused bits
271 //
272 *--(*p) = size * 8 - bits;
273
274 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
275 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_BIT_STRING ) );
276
277 return( len );
278}
279
280int asn1_write_octet_string( unsigned char **p, unsigned char *start,
281 const unsigned char *buf, size_t size )
282{
283 int ret;
284 size_t len = 0;
285
Paul Bakker9852d002013-08-26 17:56:37 +0200286 ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start, buf, size ) );
Paul Bakker598e4502013-08-25 14:46:39 +0200287
288 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
289 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_OCTET_STRING ) );
290
291 return( len );
292}
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000293#endif