blob: 38858637332564bbf52ef5a794e13f3922caf1f7 [file] [log] [blame]
Paul Bakkerbdb912d2012-02-13 23:11:30 +00001/*
2 * ASN.1 buffer writing functionality
3 *
Manuel Pégourié-Gonnard0edee5e2015-01-26 15:29:40 +00004 * Copyright (C) 2006-2012, ARM Limited, All Rights Reserved
Paul Bakkerbdb912d2012-02-13 23:11:30 +00005 *
Manuel Pégourié-Gonnarde12abf92015-01-28 17:13:45 +00006 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakkerbdb912d2012-02-13 23:11:30 +00007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23#include "polarssl/config.h"
24
25#if defined(POLARSSL_ASN1_WRITE_C)
26
27#include "polarssl/asn1write.h"
28
29int asn1_write_len( unsigned char **p, unsigned char *start, size_t len )
30{
31 if( len < 0x80 )
32 {
33 if( *p - start < 1 )
34 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
35
36 *--(*p) = len;
37 return( 1 );
38 }
39
40 if( len <= 0xFF )
41 {
42 if( *p - start < 2 )
43 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
44
45 *--(*p) = len;
46 *--(*p) = 0x81;
47 return( 2 );
48 }
49
50 if( *p - start < 3 )
51 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
52
53 // We assume we never have lengths larger than 65535 bytes
54 //
55 *--(*p) = len % 256;
56 *--(*p) = ( len / 256 ) % 256;
57 *--(*p) = 0x82;
58
59 return( 3 );
60}
61
62int asn1_write_tag( unsigned char **p, unsigned char *start, unsigned char tag )
63{
64 if( *p - start < 1 )
65 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
66
67 *--(*p) = tag;
68
69 return( 1 );
70}
71
72int asn1_write_mpi( unsigned char **p, unsigned char *start, mpi *X )
73{
74 int ret;
75 size_t len = 0;
76
77 // Write the MPI
78 //
79 len = mpi_size( X );
80
81 if( *p - start < (int) len )
82 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
83
84 (*p) -= len;
Paul Bakker7890e622014-04-17 12:42:41 +020085 MPI_CHK( mpi_write_binary( X, *p, len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000086
87 // DER format assumes 2s complement for numbers, so the leftmost bit
88 // should be 0 for positive numbers and 1 for negative numbers.
89 //
90 if ( X->s ==1 && **p & 0x80 )
91 {
92 if( *p - start < 1 )
93 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
94
95 *--(*p) = 0x00;
96 len += 1;
97 }
98
99 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
100 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_INTEGER ) );
101
Paul Bakker7890e622014-04-17 12:42:41 +0200102 ret = (int) len;
103
104cleanup:
105 return( ret );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000106}
107
108int asn1_write_null( unsigned char **p, unsigned char *start )
109{
110 int ret;
111 size_t len = 0;
112
113 // Write NULL
114 //
115 ASN1_CHK_ADD( len, asn1_write_len( p, start, 0) );
116 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_NULL ) );
117
118 return( len );
119}
120
121int asn1_write_oid( unsigned char **p, unsigned char *start, char *oid )
122{
123 int ret;
124 size_t len = 0;
125
126 // Write OID
127 //
128 len = strlen( oid );
129
130 if( *p - start < (int) len )
131 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
132
133 (*p) -= len;
134 memcpy( *p, oid, len );
135
136 ASN1_CHK_ADD( len , asn1_write_len( p, start, len ) );
137 ASN1_CHK_ADD( len , asn1_write_tag( p, start, ASN1_OID ) );
138
139 return( len );
140}
141
142int asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start,
143 char *algorithm_oid )
144{
145 int ret;
146 size_t null_len = 0;
147 size_t oid_len = 0;
148 size_t len = 0;
149
150 // Write NULL
151 //
152 ASN1_CHK_ADD( null_len, asn1_write_null( p, start ) );
153
154 // Write OID
155 //
156 ASN1_CHK_ADD( oid_len, asn1_write_oid( p, start, algorithm_oid ) );
157
158 len = oid_len + null_len;
159 ASN1_CHK_ADD( len, asn1_write_len( p, start, oid_len + null_len ) );
160 ASN1_CHK_ADD( len, asn1_write_tag( p, start,
161 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
162
163 return( len );
164}
165
166int asn1_write_int( unsigned char **p, unsigned char *start, int val )
167{
168 int ret;
169 size_t len = 0;
170
171 // TODO negative values and values larger than 128
172 // DER format assumes 2s complement for numbers, so the leftmost bit
173 // should be 0 for positive numbers and 1 for negative numbers.
174 //
175 if( *p - start < 1 )
176 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
177
178 len += 1;
179 *--(*p) = val;
180
181 if ( val > 0 && **p & 0x80 )
182 {
183 if( *p - start < 1 )
184 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
185
186 *--(*p) = 0x00;
187 len += 1;
188 }
189
190 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
191 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_INTEGER ) );
192
193 return( len );
194}
195
196int asn1_write_printable_string( unsigned char **p, unsigned char *start,
197 char *text )
198{
199 int ret;
200 size_t len = 0;
201
202 // Write string
203 //
204 len = strlen( text );
205
206 if( *p - start < (int) len )
207 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
208
209 (*p) -= len;
210 memcpy( *p, text, len );
211
212 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
213 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_PRINTABLE_STRING ) );
214
215 return( len );
216}
217
Paul Bakker05888152012-02-16 10:26:57 +0000218int asn1_write_ia5_string( unsigned char **p, unsigned char *start,
219 char *text )
220{
221 int ret;
222 size_t len = 0;
223
224 // Write string
225 //
226 len = strlen( text );
227
228 if( *p - start < (int) len )
229 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
230
231 (*p) -= len;
232 memcpy( *p, text, len );
233
234 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
235 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_IA5_STRING ) );
236
237 return( len );
238}
239
240
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000241#endif