Mercurial > dropbear
comparison libtomcrypt/src/pk/asn1/der/sequence/der_encode_sequence_ex.c @ 1511:5916af64acd4 fuzz
merge from main
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sat, 17 Feb 2018 19:29:51 +0800 |
parents | 6dba84798cd5 |
children |
comparison
equal
deleted
inserted
replaced
1457:32f990cc96b1 | 1511:5916af64acd4 |
---|---|
3 * LibTomCrypt is a library that provides various cryptographic | 3 * LibTomCrypt is a library that provides various cryptographic |
4 * algorithms in a highly modular and flexible manner. | 4 * algorithms in a highly modular and flexible manner. |
5 * | 5 * |
6 * The library is free for all purposes without any express | 6 * The library is free for all purposes without any express |
7 * guarantee it works. | 7 * guarantee it works. |
8 * | |
9 * Tom St Denis, [email protected], http://libtom.org | |
10 */ | 8 */ |
11 #include "tomcrypt.h" | 9 #include "tomcrypt.h" |
12 #include <stdarg.h> | |
13 | 10 |
14 | 11 |
15 /** | 12 /** |
16 @file der_encode_sequence_ex.c | 13 @file der_encode_sequence_ex.c |
17 ASN.1 DER, encode a SEQUENCE, Tom St Denis | 14 ASN.1 DER, encode a SEQUENCE, Tom St Denis |
21 | 18 |
22 /** | 19 /** |
23 Encode a SEQUENCE | 20 Encode a SEQUENCE |
24 @param list The list of items to encode | 21 @param list The list of items to encode |
25 @param inlen The number of items in the list | 22 @param inlen The number of items in the list |
26 @param out [out] The destination | 23 @param out [out] The destination |
27 @param outlen [in/out] The size of the output | 24 @param outlen [in/out] The size of the output |
28 @param type_of LTC_ASN1_SEQUENCE or LTC_ASN1_SET/LTC_ASN1_SETOF | 25 @param type_of LTC_ASN1_SEQUENCE or LTC_ASN1_SET/LTC_ASN1_SETOF |
29 @return CRYPT_OK on success | 26 @return CRYPT_OK on success |
30 */ | 27 */ |
31 int der_encode_sequence_ex(ltc_asn1_list *list, unsigned long inlen, | 28 int der_encode_sequence_ex(ltc_asn1_list *list, unsigned long inlen, |
32 unsigned char *out, unsigned long *outlen, int type_of) | 29 unsigned char *out, unsigned long *outlen, int type_of) |
33 { | 30 { |
34 int err, type; | 31 int err; |
32 ltc_asn1_type type; | |
35 unsigned long size, x, y, z, i; | 33 unsigned long size, x, y, z, i; |
36 void *data; | 34 void *data; |
37 | 35 |
38 LTC_ARGCHK(list != NULL); | 36 LTC_ARGCHK(list != NULL); |
39 LTC_ARGCHK(out != NULL); | 37 LTC_ARGCHK(out != NULL); |
40 LTC_ARGCHK(outlen != NULL); | 38 LTC_ARGCHK(outlen != NULL); |
41 | 39 |
42 /* get size of output that will be required */ | 40 /* get size of output that will be required */ |
43 y = 0; | 41 y = 0; z = 0; |
44 for (i = 0; i < inlen; i++) { | 42 if ((err = der_length_sequence_ex(list, inlen, &y, &z)) != CRYPT_OK) return CRYPT_INVALID_ARG; |
45 type = list[i].type; | |
46 size = list[i].size; | |
47 data = list[i].data; | |
48 | |
49 if (type == LTC_ASN1_EOL) { | |
50 break; | |
51 } | |
52 | |
53 switch (type) { | |
54 case LTC_ASN1_BOOLEAN: | |
55 if ((err = der_length_boolean(&x)) != CRYPT_OK) { | |
56 goto LBL_ERR; | |
57 } | |
58 y += x; | |
59 break; | |
60 | |
61 case LTC_ASN1_INTEGER: | |
62 if ((err = der_length_integer(data, &x)) != CRYPT_OK) { | |
63 goto LBL_ERR; | |
64 } | |
65 y += x; | |
66 break; | |
67 | |
68 case LTC_ASN1_SHORT_INTEGER: | |
69 if ((err = der_length_short_integer(*((unsigned long*)data), &x)) != CRYPT_OK) { | |
70 goto LBL_ERR; | |
71 } | |
72 y += x; | |
73 break; | |
74 | |
75 case LTC_ASN1_BIT_STRING: | |
76 if ((err = der_length_bit_string(size, &x)) != CRYPT_OK) { | |
77 goto LBL_ERR; | |
78 } | |
79 y += x; | |
80 break; | |
81 | |
82 case LTC_ASN1_OCTET_STRING: | |
83 if ((err = der_length_octet_string(size, &x)) != CRYPT_OK) { | |
84 goto LBL_ERR; | |
85 } | |
86 y += x; | |
87 break; | |
88 | |
89 case LTC_ASN1_NULL: | |
90 y += 2; | |
91 break; | |
92 | |
93 case LTC_ASN1_OBJECT_IDENTIFIER: | |
94 if ((err = der_length_object_identifier(data, size, &x)) != CRYPT_OK) { | |
95 goto LBL_ERR; | |
96 } | |
97 y += x; | |
98 break; | |
99 | |
100 case LTC_ASN1_IA5_STRING: | |
101 if ((err = der_length_ia5_string(data, size, &x)) != CRYPT_OK) { | |
102 goto LBL_ERR; | |
103 } | |
104 y += x; | |
105 break; | |
106 | |
107 case LTC_ASN1_PRINTABLE_STRING: | |
108 if ((err = der_length_printable_string(data, size, &x)) != CRYPT_OK) { | |
109 goto LBL_ERR; | |
110 } | |
111 y += x; | |
112 break; | |
113 | |
114 case LTC_ASN1_UTF8_STRING: | |
115 if ((err = der_length_utf8_string(data, size, &x)) != CRYPT_OK) { | |
116 goto LBL_ERR; | |
117 } | |
118 y += x; | |
119 break; | |
120 | |
121 case LTC_ASN1_UTCTIME: | |
122 if ((err = der_length_utctime(data, &x)) != CRYPT_OK) { | |
123 goto LBL_ERR; | |
124 } | |
125 y += x; | |
126 break; | |
127 | |
128 case LTC_ASN1_SET: | |
129 case LTC_ASN1_SETOF: | |
130 case LTC_ASN1_SEQUENCE: | |
131 if ((err = der_length_sequence(data, size, &x)) != CRYPT_OK) { | |
132 goto LBL_ERR; | |
133 } | |
134 y += x; | |
135 break; | |
136 | |
137 default: | |
138 err = CRYPT_INVALID_ARG; | |
139 goto LBL_ERR; | |
140 } | |
141 } | |
142 | |
143 /* calc header size */ | |
144 z = y; | |
145 if (y < 128) { | |
146 y += 2; | |
147 } else if (y < 256) { | |
148 /* 0x30 0x81 LL */ | |
149 y += 3; | |
150 } else if (y < 65536UL) { | |
151 /* 0x30 0x82 LL LL */ | |
152 y += 4; | |
153 } else if (y < 16777216UL) { | |
154 /* 0x30 0x83 LL LL LL */ | |
155 y += 5; | |
156 } else { | |
157 err = CRYPT_INVALID_ARG; | |
158 goto LBL_ERR; | |
159 } | |
160 | 43 |
161 /* too big ? */ | 44 /* too big ? */ |
162 if (*outlen < y) { | 45 if (*outlen < y) { |
163 *outlen = y; | 46 *outlen = y; |
164 err = CRYPT_BUFFER_OVERFLOW; | 47 err = CRYPT_BUFFER_OVERFLOW; |
166 } | 49 } |
167 | 50 |
168 /* store header */ | 51 /* store header */ |
169 x = 0; | 52 x = 0; |
170 out[x++] = (type_of == LTC_ASN1_SEQUENCE) ? 0x30 : 0x31; | 53 out[x++] = (type_of == LTC_ASN1_SEQUENCE) ? 0x30 : 0x31; |
171 | 54 |
172 if (z < 128) { | 55 if (z < 128) { |
173 out[x++] = (unsigned char)z; | 56 out[x++] = (unsigned char)z; |
174 } else if (z < 256) { | 57 } else if (z < 256) { |
175 out[x++] = 0x81; | 58 out[x++] = 0x81; |
176 out[x++] = (unsigned char)z; | 59 out[x++] = (unsigned char)z; |
190 for (i = 0; i < inlen; i++) { | 73 for (i = 0; i < inlen; i++) { |
191 type = list[i].type; | 74 type = list[i].type; |
192 size = list[i].size; | 75 size = list[i].size; |
193 data = list[i].data; | 76 data = list[i].data; |
194 | 77 |
195 if (type == LTC_ASN1_EOL) { | 78 if (type == LTC_ASN1_EOL) { |
196 break; | 79 break; |
197 } | 80 } |
198 | 81 |
199 switch (type) { | 82 switch (type) { |
200 case LTC_ASN1_BOOLEAN: | 83 case LTC_ASN1_BOOLEAN: |
201 z = *outlen; | 84 z = *outlen; |
202 if ((err = der_encode_boolean(*((int *)data), out + x, &z)) != CRYPT_OK) { | 85 if ((err = der_encode_boolean(*((int *)data), out + x, &z)) != CRYPT_OK) { |
203 goto LBL_ERR; | 86 goto LBL_ERR; |
204 } | 87 } |
205 x += z; | 88 break; |
206 *outlen -= z; | 89 |
207 break; | |
208 | |
209 case LTC_ASN1_INTEGER: | 90 case LTC_ASN1_INTEGER: |
210 z = *outlen; | 91 z = *outlen; |
211 if ((err = der_encode_integer(data, out + x, &z)) != CRYPT_OK) { | 92 if ((err = der_encode_integer(data, out + x, &z)) != CRYPT_OK) { |
212 goto LBL_ERR; | 93 goto LBL_ERR; |
213 } | 94 } |
214 x += z; | |
215 *outlen -= z; | |
216 break; | 95 break; |
217 | 96 |
218 case LTC_ASN1_SHORT_INTEGER: | 97 case LTC_ASN1_SHORT_INTEGER: |
219 z = *outlen; | 98 z = *outlen; |
220 if ((err = der_encode_short_integer(*((unsigned long*)data), out + x, &z)) != CRYPT_OK) { | 99 if ((err = der_encode_short_integer(*((unsigned long*)data), out + x, &z)) != CRYPT_OK) { |
221 goto LBL_ERR; | 100 goto LBL_ERR; |
222 } | 101 } |
223 x += z; | |
224 *outlen -= z; | |
225 break; | 102 break; |
226 | 103 |
227 case LTC_ASN1_BIT_STRING: | 104 case LTC_ASN1_BIT_STRING: |
228 z = *outlen; | 105 z = *outlen; |
229 if ((err = der_encode_bit_string(data, size, out + x, &z)) != CRYPT_OK) { | 106 if ((err = der_encode_bit_string(data, size, out + x, &z)) != CRYPT_OK) { |
230 goto LBL_ERR; | 107 goto LBL_ERR; |
231 } | 108 } |
232 x += z; | 109 break; |
233 *outlen -= z; | 110 |
111 case LTC_ASN1_RAW_BIT_STRING: | |
112 z = *outlen; | |
113 if ((err = der_encode_raw_bit_string(data, size, out + x, &z)) != CRYPT_OK) { | |
114 goto LBL_ERR; | |
115 } | |
234 break; | 116 break; |
235 | 117 |
236 case LTC_ASN1_OCTET_STRING: | 118 case LTC_ASN1_OCTET_STRING: |
237 z = *outlen; | 119 z = *outlen; |
238 if ((err = der_encode_octet_string(data, size, out + x, &z)) != CRYPT_OK) { | 120 if ((err = der_encode_octet_string(data, size, out + x, &z)) != CRYPT_OK) { |
239 goto LBL_ERR; | 121 goto LBL_ERR; |
240 } | 122 } |
241 x += z; | |
242 *outlen -= z; | |
243 break; | 123 break; |
244 | 124 |
245 case LTC_ASN1_NULL: | 125 case LTC_ASN1_NULL: |
246 out[x++] = 0x05; | 126 out[x] = 0x05; |
247 out[x++] = 0x00; | 127 out[x+1] = 0x00; |
248 *outlen -= 2; | 128 z = 2; |
249 break; | 129 break; |
250 | 130 |
251 case LTC_ASN1_OBJECT_IDENTIFIER: | 131 case LTC_ASN1_OBJECT_IDENTIFIER: |
252 z = *outlen; | 132 z = *outlen; |
253 if ((err = der_encode_object_identifier(data, size, out + x, &z)) != CRYPT_OK) { | 133 if ((err = der_encode_object_identifier(data, size, out + x, &z)) != CRYPT_OK) { |
254 goto LBL_ERR; | 134 goto LBL_ERR; |
255 } | 135 } |
256 x += z; | |
257 *outlen -= z; | |
258 break; | 136 break; |
259 | 137 |
260 case LTC_ASN1_IA5_STRING: | 138 case LTC_ASN1_IA5_STRING: |
261 z = *outlen; | 139 z = *outlen; |
262 if ((err = der_encode_ia5_string(data, size, out + x, &z)) != CRYPT_OK) { | 140 if ((err = der_encode_ia5_string(data, size, out + x, &z)) != CRYPT_OK) { |
263 goto LBL_ERR; | 141 goto LBL_ERR; |
264 } | 142 } |
265 x += z; | 143 break; |
266 *outlen -= z; | 144 |
267 break; | |
268 | |
269 case LTC_ASN1_PRINTABLE_STRING: | 145 case LTC_ASN1_PRINTABLE_STRING: |
270 z = *outlen; | 146 z = *outlen; |
271 if ((err = der_encode_printable_string(data, size, out + x, &z)) != CRYPT_OK) { | 147 if ((err = der_encode_printable_string(data, size, out + x, &z)) != CRYPT_OK) { |
272 goto LBL_ERR; | 148 goto LBL_ERR; |
273 } | 149 } |
274 x += z; | |
275 *outlen -= z; | |
276 break; | 150 break; |
277 | 151 |
278 case LTC_ASN1_UTF8_STRING: | 152 case LTC_ASN1_UTF8_STRING: |
279 z = *outlen; | 153 z = *outlen; |
280 if ((err = der_encode_utf8_string(data, size, out + x, &z)) != CRYPT_OK) { | 154 if ((err = der_encode_utf8_string(data, size, out + x, &z)) != CRYPT_OK) { |
281 goto LBL_ERR; | 155 goto LBL_ERR; |
282 } | 156 } |
283 x += z; | |
284 *outlen -= z; | |
285 break; | 157 break; |
286 | 158 |
287 case LTC_ASN1_UTCTIME: | 159 case LTC_ASN1_UTCTIME: |
288 z = *outlen; | 160 z = *outlen; |
289 if ((err = der_encode_utctime(data, out + x, &z)) != CRYPT_OK) { | 161 if ((err = der_encode_utctime(data, out + x, &z)) != CRYPT_OK) { |
290 goto LBL_ERR; | 162 goto LBL_ERR; |
291 } | 163 } |
292 x += z; | 164 break; |
293 *outlen -= z; | 165 |
166 case LTC_ASN1_GENERALIZEDTIME: | |
167 z = *outlen; | |
168 if ((err = der_encode_generalizedtime(data, out + x, &z)) != CRYPT_OK) { | |
169 goto LBL_ERR; | |
170 } | |
294 break; | 171 break; |
295 | 172 |
296 case LTC_ASN1_SET: | 173 case LTC_ASN1_SET: |
297 z = *outlen; | 174 z = *outlen; |
298 if ((err = der_encode_set(data, size, out + x, &z)) != CRYPT_OK) { | 175 if ((err = der_encode_set(data, size, out + x, &z)) != CRYPT_OK) { |
299 goto LBL_ERR; | 176 goto LBL_ERR; |
300 } | 177 } |
301 x += z; | |
302 *outlen -= z; | |
303 break; | 178 break; |
304 | 179 |
305 case LTC_ASN1_SETOF: | 180 case LTC_ASN1_SETOF: |
306 z = *outlen; | 181 z = *outlen; |
307 if ((err = der_encode_setof(data, size, out + x, &z)) != CRYPT_OK) { | 182 if ((err = der_encode_setof(data, size, out + x, &z)) != CRYPT_OK) { |
308 goto LBL_ERR; | 183 goto LBL_ERR; |
309 } | 184 } |
310 x += z; | |
311 *outlen -= z; | |
312 break; | 185 break; |
313 | 186 |
314 case LTC_ASN1_SEQUENCE: | 187 case LTC_ASN1_SEQUENCE: |
315 z = *outlen; | 188 z = *outlen; |
316 if ((err = der_encode_sequence_ex(data, size, out + x, &z, type)) != CRYPT_OK) { | 189 if ((err = der_encode_sequence_ex(data, size, out + x, &z, type)) != CRYPT_OK) { |
317 goto LBL_ERR; | 190 goto LBL_ERR; |
318 } | 191 } |
319 x += z; | 192 break; |
320 *outlen -= z; | 193 |
321 break; | 194 case LTC_ASN1_CHOICE: |
322 | 195 case LTC_ASN1_CONSTRUCTED: |
323 default: | 196 case LTC_ASN1_CONTEXT_SPECIFIC: |
197 case LTC_ASN1_EOL: | |
198 case LTC_ASN1_TELETEX_STRING: | |
324 err = CRYPT_INVALID_ARG; | 199 err = CRYPT_INVALID_ARG; |
325 goto LBL_ERR; | 200 goto LBL_ERR; |
326 } | 201 } |
202 | |
203 x += z; | |
204 *outlen -= z; | |
327 } | 205 } |
328 *outlen = x; | 206 *outlen = x; |
329 err = CRYPT_OK; | 207 err = CRYPT_OK; |
330 | 208 |
331 LBL_ERR: | 209 LBL_ERR: |
332 return err; | 210 return err; |
333 } | 211 } |
334 | 212 |
335 #endif | 213 #endif |
214 | |
215 /* ref: $Format:%D$ */ | |
216 /* git commit: $Format:%H$ */ | |
217 /* commit time: $Format:%ai$ */ |