blob: 463c730fbac9df2a3d1e352a12ae06b1bbe8792e [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
176int asn1_write_int( unsigned char **p, unsigned char *start, int val )
177{
178 int ret;
179 size_t len = 0;
180
181 // TODO negative values and values larger than 128
182 // DER format assumes 2s complement for numbers, so the leftmost bit
183 // should be 0 for positive numbers and 1 for negative numbers.
184 //
185 if( *p - start < 1 )
186 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
187
188 len += 1;
189 *--(*p) = val;
190
191 if ( val > 0 && **p & 0x80 )
192 {
193 if( *p - start < 1 )
194 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
195
196 *--(*p) = 0x00;
197 len += 1;
198 }
199
200 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
201 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_INTEGER ) );
202
203 return( len );
204}
205
206int asn1_write_printable_string( unsigned char **p, unsigned char *start,
207 char *text )
208{
209 int ret;
210 size_t len = 0;
211
Paul Bakker9852d002013-08-26 17:56:37 +0200212 ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start,
213 (const unsigned char *) text, strlen( text ) ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000214
215 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
216 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_PRINTABLE_STRING ) );
217
218 return( len );
219}
Paul Bakker598e4502013-08-25 14:46:39 +0200220
Paul Bakker05888152012-02-16 10:26:57 +0000221int asn1_write_ia5_string( unsigned char **p, unsigned char *start,
Paul Bakker598e4502013-08-25 14:46:39 +0200222 char *text )
Paul Bakker05888152012-02-16 10:26:57 +0000223{
224 int ret;
225 size_t len = 0;
226
Paul Bakker9852d002013-08-26 17:56:37 +0200227 ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start,
228 (const unsigned char *) text, strlen( text ) ) );
Paul Bakker05888152012-02-16 10:26:57 +0000229
230 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
231 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_IA5_STRING ) );
232
233 return( len );
234}
Paul Bakker598e4502013-08-25 14:46:39 +0200235
236int asn1_write_bitstring( unsigned char **p, unsigned char *start,
237 const unsigned char *buf, size_t bits )
238{
239 int ret;
240 size_t len = 0, size;
241
242 size = ( bits / 8 ) + ( ( bits % 8 ) ? 1 : 0 );
243
244 // Calculate byte length
245 //
246 if( *p - start < (int) size + 1 )
247 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
248
249 len = size + 1;
250 (*p) -= size;
251 memcpy( *p, buf, size );
252
253 // Write unused bits
254 //
255 *--(*p) = size * 8 - bits;
256
257 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
258 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_BIT_STRING ) );
259
260 return( len );
261}
262
263int asn1_write_octet_string( unsigned char **p, unsigned char *start,
264 const unsigned char *buf, size_t size )
265{
266 int ret;
267 size_t len = 0;
268
Paul Bakker9852d002013-08-26 17:56:37 +0200269 ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start, buf, size ) );
Paul Bakker598e4502013-08-25 14:46:39 +0200270
271 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
272 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_OCTET_STRING ) );
273
274 return( len );
275}
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000276#endif