Mercurial > dropbear
comparison libtomcrypt/testprof/der_tests.c @ 415:8b9aba1d5fa4 channel-fix
merge of '73fe066c5d9e2395354ba74756124d45c978a04d'
and 'f5014cc84558f1e8eba42dbecf9f72f94bfe6134'
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Tue, 06 Feb 2007 16:00:18 +0000 |
parents | 0cbe8f6dbf9e |
children | f849a5ca2efc |
comparison
equal
deleted
inserted
replaced
414:c53a26c430e5 | 415:8b9aba1d5fa4 |
---|---|
1 #include <tomcrypt_test.h> | 1 #include <tomcrypt_test.h> |
2 #if defined(GMP_DESC) || defined(USE_GMP) | |
3 #include <gmp.h> | |
4 #endif | |
2 | 5 |
3 #ifndef LTC_DER | 6 #ifndef LTC_DER |
4 | 7 |
5 int der_tests(void) | 8 int der_tests(void) |
6 { | 9 { |
8 return 0; | 11 return 0; |
9 } | 12 } |
10 | 13 |
11 #else | 14 #else |
12 | 15 |
16 static void der_set_test(void) | |
17 { | |
18 ltc_asn1_list list[10]; | |
19 static const unsigned char oct_str[] = { 1, 2, 3, 4 }; | |
20 static const unsigned char bin_str[] = { 1, 0, 0, 1 }; | |
21 static const unsigned long int_val = 12345678UL; | |
22 | |
23 unsigned char strs[10][10], outbuf[128]; | |
24 unsigned long x, val, outlen; | |
25 int err; | |
26 | |
27 /* make structure and encode it */ | |
28 LTC_SET_ASN1(list, 0, LTC_ASN1_OCTET_STRING, oct_str, sizeof(oct_str)); | |
29 LTC_SET_ASN1(list, 1, LTC_ASN1_BIT_STRING, bin_str, sizeof(bin_str)); | |
30 LTC_SET_ASN1(list, 2, LTC_ASN1_SHORT_INTEGER, &int_val, 1); | |
31 | |
32 /* encode it */ | |
33 outlen = sizeof(outbuf); | |
34 if ((err = der_encode_set(list, 3, outbuf, &outlen)) != CRYPT_OK) { | |
35 fprintf(stderr, "error encoding set: %s\n", error_to_string(err)); | |
36 exit(EXIT_FAILURE); | |
37 } | |
38 | |
39 | |
40 /* first let's test the set_decoder out of order to see what happens, we should get all the fields we expect even though they're in a diff order */ | |
41 LTC_SET_ASN1(list, 0, LTC_ASN1_BIT_STRING, strs[1], sizeof(strs[1])); | |
42 LTC_SET_ASN1(list, 1, LTC_ASN1_SHORT_INTEGER, &val, 1); | |
43 LTC_SET_ASN1(list, 2, LTC_ASN1_OCTET_STRING, strs[0], sizeof(strs[0])); | |
44 | |
45 if ((err = der_decode_set(outbuf, outlen, list, 3)) != CRYPT_OK) { | |
46 fprintf(stderr, "error decoding set using der_decode_set: %s\n", error_to_string(err)); | |
47 exit(EXIT_FAILURE); | |
48 } | |
49 | |
50 /* now compare the items */ | |
51 if (memcmp(strs[0], oct_str, sizeof(oct_str))) { | |
52 fprintf(stderr, "error decoding set using der_decode_set (oct_str is wrong):\n"); | |
53 exit(EXIT_FAILURE); | |
54 } | |
55 | |
56 if (memcmp(strs[1], bin_str, sizeof(bin_str))) { | |
57 fprintf(stderr, "error decoding set using der_decode_set (bin_str is wrong):\n"); | |
58 exit(EXIT_FAILURE); | |
59 } | |
60 | |
61 if (val != int_val) { | |
62 fprintf(stderr, "error decoding set using der_decode_set (int_val is wrong):\n"); | |
63 exit(EXIT_FAILURE); | |
64 } | |
65 | |
66 strcpy((char*)strs[0], "one"); | |
67 strcpy((char*)strs[1], "one2"); | |
68 strcpy((char*)strs[2], "two"); | |
69 strcpy((char*)strs[3], "aaa"); | |
70 strcpy((char*)strs[4], "aaaa"); | |
71 strcpy((char*)strs[5], "aab"); | |
72 strcpy((char*)strs[6], "aaab"); | |
73 strcpy((char*)strs[7], "bbb"); | |
74 strcpy((char*)strs[8], "bbba"); | |
75 strcpy((char*)strs[9], "bbbb"); | |
76 | |
77 for (x = 0; x < 10; x++) { | |
78 LTC_SET_ASN1(list, x, LTC_ASN1_PRINTABLE_STRING, strs[x], strlen((char*)strs[x])); | |
79 } | |
80 | |
81 outlen = sizeof(outbuf); | |
82 if ((err = der_encode_setof(list, 10, outbuf, &outlen)) != CRYPT_OK) { | |
83 fprintf(stderr, "error encoding SET OF: %s\n", error_to_string(err)); | |
84 exit(EXIT_FAILURE); | |
85 } | |
86 | |
87 for (x = 0; x < 10; x++) { | |
88 LTC_SET_ASN1(list, x, LTC_ASN1_PRINTABLE_STRING, strs[x], sizeof(strs[x]) - 1); | |
89 } | |
90 XMEMSET(strs, 0, sizeof(strs)); | |
91 | |
92 if ((err = der_decode_set(outbuf, outlen, list, 10)) != CRYPT_OK) { | |
93 fprintf(stderr, "error decoding SET OF: %s\n", error_to_string(err)); | |
94 exit(EXIT_FAILURE); | |
95 } | |
96 | |
97 /* now compare */ | |
98 for (x = 1; x < 10; x++) { | |
99 if (!(strlen((char*)strs[x-1]) <= strlen((char*)strs[x])) && strcmp((char*)strs[x-1], (char*)strs[x]) >= 0) { | |
100 fprintf(stderr, "error SET OF order at %lu is wrong\n", x); | |
101 exit(EXIT_FAILURE); | |
102 } | |
103 } | |
104 | |
105 } | |
106 | |
107 | |
108 /* we are encoding | |
109 | |
110 SEQUENCE { | |
111 PRINTABLE "printable" | |
112 IA5 "ia5" | |
113 SEQUENCE { | |
114 INTEGER 12345678 | |
115 UTCTIME { 91, 5, 6, 16, 45, 40, 1, 7, 0 } | |
116 SEQUENCE { | |
117 OCTET STRING { 1, 2, 3, 4 } | |
118 BIT STRING { 1, 0, 0, 1 } | |
119 SEQUENCE { | |
120 OID { 1, 2, 840, 113549 } | |
121 NULL | |
122 SET OF { | |
123 PRINTABLE "333" // WILL GET SORTED | |
124 PRINTABLE "222" | |
125 } | |
126 } | |
127 } | |
128 } | |
129 | |
130 */ | |
131 | |
132 static void der_flexi_test(void) | |
133 { | |
134 static const char printable_str[] = "printable"; | |
135 static const char set1_str[] = "333"; | |
136 static const char set2_str[] = "222"; | |
137 static const char ia5_str[] = "ia5"; | |
138 static const unsigned long int_val = 12345678UL; | |
139 static const ltc_utctime utctime = { 91, 5, 6, 16, 45, 40, 1, 7, 0 }; | |
140 static const unsigned char oct_str[] = { 1, 2, 3, 4 }; | |
141 static const unsigned char bit_str[] = { 1, 0, 0, 1 }; | |
142 static const unsigned long oid_str[] = { 1, 2, 840, 113549 }; | |
143 | |
144 unsigned char encode_buf[192]; | |
145 unsigned long encode_buf_len, decode_len; | |
146 int err; | |
147 | |
148 ltc_asn1_list static_list[5][3], *decoded_list, *l; | |
149 | |
150 /* build list */ | |
151 LTC_SET_ASN1(static_list[0], 0, LTC_ASN1_PRINTABLE_STRING, (void *)printable_str, strlen(printable_str)); | |
152 LTC_SET_ASN1(static_list[0], 1, LTC_ASN1_IA5_STRING, (void *)ia5_str, strlen(ia5_str)); | |
153 LTC_SET_ASN1(static_list[0], 2, LTC_ASN1_SEQUENCE, static_list[1], 3); | |
154 | |
155 LTC_SET_ASN1(static_list[1], 0, LTC_ASN1_SHORT_INTEGER, (void *)&int_val, 1); | |
156 LTC_SET_ASN1(static_list[1], 1, LTC_ASN1_UTCTIME, (void *)&utctime, 1); | |
157 LTC_SET_ASN1(static_list[1], 2, LTC_ASN1_SEQUENCE, static_list[2], 3); | |
158 | |
159 LTC_SET_ASN1(static_list[2], 0, LTC_ASN1_OCTET_STRING, (void *)oct_str, 4); | |
160 LTC_SET_ASN1(static_list[2], 1, LTC_ASN1_BIT_STRING, (void *)bit_str, 4); | |
161 LTC_SET_ASN1(static_list[2], 2, LTC_ASN1_SEQUENCE, static_list[3], 3); | |
162 | |
163 LTC_SET_ASN1(static_list[3], 0, LTC_ASN1_OBJECT_IDENTIFIER,(void *)oid_str, 4); | |
164 LTC_SET_ASN1(static_list[3], 1, LTC_ASN1_NULL, NULL, 0); | |
165 LTC_SET_ASN1(static_list[3], 2, LTC_ASN1_SETOF, static_list[4], 2); | |
166 | |
167 LTC_SET_ASN1(static_list[4], 0, LTC_ASN1_PRINTABLE_STRING, set1_str, strlen(set1_str)); | |
168 LTC_SET_ASN1(static_list[4], 1, LTC_ASN1_PRINTABLE_STRING, set2_str, strlen(set2_str)); | |
169 | |
170 /* encode it */ | |
171 encode_buf_len = sizeof(encode_buf); | |
172 if ((err = der_encode_sequence(&static_list[0][0], 3, encode_buf, &encode_buf_len)) != CRYPT_OK) { | |
173 fprintf(stderr, "Encoding static_list: %s\n", error_to_string(err)); | |
174 exit(EXIT_FAILURE); | |
175 } | |
176 | |
177 #if 0 | |
178 { | |
179 FILE *f; | |
180 f = fopen("t.bin", "wb"); | |
181 fwrite(encode_buf, 1, encode_buf_len, f); | |
182 fclose(f); | |
183 } | |
184 #endif | |
185 | |
186 /* decode with flexi */ | |
187 decode_len = encode_buf_len; | |
188 if ((err = der_decode_sequence_flexi(encode_buf, &decode_len, &decoded_list)) != CRYPT_OK) { | |
189 fprintf(stderr, "decoding static_list: %s\n", error_to_string(err)); | |
190 exit(EXIT_FAILURE); | |
191 } | |
192 | |
193 if (decode_len != encode_buf_len) { | |
194 fprintf(stderr, "Decode len of %lu does not match encode len of %lu \n", decode_len, encode_buf_len); | |
195 exit(EXIT_FAILURE); | |
196 } | |
197 | |
198 /* we expect l->next to be NULL and l->child to not be */ | |
199 l = decoded_list; | |
200 if (l->next != NULL || l->child == NULL) { | |
201 fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child); | |
202 exit(EXIT_FAILURE); | |
203 } | |
204 | |
205 /* we expect a SEQUENCE */ | |
206 if (l->type != LTC_ASN1_SEQUENCE) { | |
207 fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child); | |
208 exit(EXIT_FAILURE); | |
209 } | |
210 l = l->child; | |
211 | |
212 /* PRINTABLE STRING */ | |
213 /* we expect printable_str */ | |
214 if (l->next == NULL || l->child != NULL) { | |
215 fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child); | |
216 exit(EXIT_FAILURE); | |
217 } | |
218 | |
219 if (l->type != LTC_ASN1_PRINTABLE_STRING) { | |
220 fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child); | |
221 exit(EXIT_FAILURE); | |
222 } | |
223 | |
224 if (l->size != strlen(printable_str) || memcmp(printable_str, l->data, l->size)) { | |
225 fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child); | |
226 exit(EXIT_FAILURE); | |
227 } | |
228 | |
229 /* move to next */ | |
230 l = l->next; | |
231 | |
232 /* IA5 STRING */ | |
233 /* we expect ia5_str */ | |
234 if (l->next == NULL || l->child != NULL) { | |
235 fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child); | |
236 exit(EXIT_FAILURE); | |
237 } | |
238 | |
239 if (l->type != LTC_ASN1_IA5_STRING) { | |
240 fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child); | |
241 exit(EXIT_FAILURE); | |
242 } | |
243 | |
244 if (l->size != strlen(ia5_str) || memcmp(ia5_str, l->data, l->size)) { | |
245 fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child); | |
246 exit(EXIT_FAILURE); | |
247 } | |
248 | |
249 /* move to next */ | |
250 l = l->next; | |
251 | |
252 /* expect child anve move down */ | |
253 | |
254 if (l->next != NULL || l->child == NULL) { | |
255 fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child); | |
256 exit(EXIT_FAILURE); | |
257 } | |
258 | |
259 if (l->type != LTC_ASN1_SEQUENCE) { | |
260 fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child); | |
261 exit(EXIT_FAILURE); | |
262 } | |
263 l = l->child; | |
264 | |
265 | |
266 /* INTEGER */ | |
267 | |
268 if (l->next == NULL || l->child != NULL) { | |
269 fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child); | |
270 exit(EXIT_FAILURE); | |
271 } | |
272 | |
273 if (l->type != LTC_ASN1_INTEGER) { | |
274 fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child); | |
275 exit(EXIT_FAILURE); | |
276 } | |
277 | |
278 if (mp_cmp_d(l->data, 12345678UL) != LTC_MP_EQ) { | |
279 fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child); | |
280 exit(EXIT_FAILURE); | |
281 } | |
282 | |
283 /* move to next */ | |
284 l = l->next; | |
285 | |
286 /* UTCTIME */ | |
287 | |
288 if (l->next == NULL || l->child != NULL) { | |
289 fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child); | |
290 exit(EXIT_FAILURE); | |
291 } | |
292 | |
293 if (l->type != LTC_ASN1_UTCTIME) { | |
294 fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child); | |
295 exit(EXIT_FAILURE); | |
296 } | |
297 | |
298 if (memcmp(l->data, &utctime, sizeof(utctime))) { | |
299 fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child); | |
300 exit(EXIT_FAILURE); | |
301 } | |
302 | |
303 /* move to next */ | |
304 l = l->next; | |
305 | |
306 /* expect child anve move down */ | |
307 | |
308 if (l->next != NULL || l->child == NULL) { | |
309 fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child); | |
310 exit(EXIT_FAILURE); | |
311 } | |
312 | |
313 if (l->type != LTC_ASN1_SEQUENCE) { | |
314 fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child); | |
315 exit(EXIT_FAILURE); | |
316 } | |
317 l = l->child; | |
318 | |
319 | |
320 /* OCTET STRING */ | |
321 /* we expect oct_str */ | |
322 if (l->next == NULL || l->child != NULL) { | |
323 fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child); | |
324 exit(EXIT_FAILURE); | |
325 } | |
326 | |
327 if (l->type != LTC_ASN1_OCTET_STRING) { | |
328 fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child); | |
329 exit(EXIT_FAILURE); | |
330 } | |
331 | |
332 if (l->size != sizeof(oct_str) || memcmp(oct_str, l->data, l->size)) { | |
333 fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child); | |
334 exit(EXIT_FAILURE); | |
335 } | |
336 | |
337 /* move to next */ | |
338 l = l->next; | |
339 | |
340 /* BIT STRING */ | |
341 /* we expect oct_str */ | |
342 if (l->next == NULL || l->child != NULL) { | |
343 fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child); | |
344 exit(EXIT_FAILURE); | |
345 } | |
346 | |
347 if (l->type != LTC_ASN1_BIT_STRING) { | |
348 fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child); | |
349 exit(EXIT_FAILURE); | |
350 } | |
351 | |
352 if (l->size != sizeof(bit_str) || memcmp(bit_str, l->data, l->size)) { | |
353 fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child); | |
354 exit(EXIT_FAILURE); | |
355 } | |
356 | |
357 /* move to next */ | |
358 l = l->next; | |
359 | |
360 /* expect child anve move down */ | |
361 | |
362 if (l->next != NULL || l->child == NULL) { | |
363 fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child); | |
364 exit(EXIT_FAILURE); | |
365 } | |
366 | |
367 if (l->type != LTC_ASN1_SEQUENCE) { | |
368 fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child); | |
369 exit(EXIT_FAILURE); | |
370 } | |
371 l = l->child; | |
372 | |
373 | |
374 /* OID STRING */ | |
375 /* we expect oid_str */ | |
376 if (l->next == NULL || l->child != NULL) { | |
377 fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child); | |
378 exit(EXIT_FAILURE); | |
379 } | |
380 | |
381 if (l->type != LTC_ASN1_OBJECT_IDENTIFIER) { | |
382 fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child); | |
383 exit(EXIT_FAILURE); | |
384 } | |
385 | |
386 if (l->size != sizeof(oid_str)/sizeof(oid_str[0]) || memcmp(oid_str, l->data, l->size*sizeof(oid_str[0]))) { | |
387 fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child); | |
388 exit(EXIT_FAILURE); | |
389 } | |
390 | |
391 /* move to next */ | |
392 l = l->next; | |
393 | |
394 /* NULL */ | |
395 if (l->type != LTC_ASN1_NULL) { | |
396 fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child); | |
397 exit(EXIT_FAILURE); | |
398 } | |
399 | |
400 /* move to next */ | |
401 l = l->next; | |
402 | |
403 /* expect child anve move down */ | |
404 if (l->next != NULL || l->child == NULL) { | |
405 fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child); | |
406 exit(EXIT_FAILURE); | |
407 } | |
408 | |
409 if (l->type != LTC_ASN1_SET) { | |
410 fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child); | |
411 exit(EXIT_FAILURE); | |
412 } | |
413 l = l->child; | |
414 | |
415 /* PRINTABLE STRING */ | |
416 /* we expect printable_str */ | |
417 if (l->next == NULL || l->child != NULL) { | |
418 fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child); | |
419 exit(EXIT_FAILURE); | |
420 } | |
421 | |
422 if (l->type != LTC_ASN1_PRINTABLE_STRING) { | |
423 fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child); | |
424 exit(EXIT_FAILURE); | |
425 } | |
426 | |
427 /* note we compare set2_str FIRST because the SET OF is sorted and "222" comes before "333" */ | |
428 if (l->size != strlen(set2_str) || memcmp(set2_str, l->data, l->size)) { | |
429 fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child); | |
430 exit(EXIT_FAILURE); | |
431 } | |
432 | |
433 /* move to next */ | |
434 l = l->next; | |
435 | |
436 /* PRINTABLE STRING */ | |
437 /* we expect printable_str */ | |
438 if (l->type != LTC_ASN1_PRINTABLE_STRING) { | |
439 fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child); | |
440 exit(EXIT_FAILURE); | |
441 } | |
442 | |
443 if (l->size != strlen(set1_str) || memcmp(set1_str, l->data, l->size)) { | |
444 fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child); | |
445 exit(EXIT_FAILURE); | |
446 } | |
447 | |
448 | |
449 der_sequence_free(l); | |
450 | |
451 } | |
452 | |
13 static int der_choice_test(void) | 453 static int der_choice_test(void) |
14 { | 454 { |
15 ltc_asn1_list types[7], host[1]; | 455 ltc_asn1_list types[7], host[1]; |
16 unsigned char bitbuf[10], octetbuf[10], ia5buf[10], printbuf[10], outbuf[256]; | 456 unsigned char bitbuf[10], octetbuf[10], ia5buf[10], printbuf[10], outbuf[256]; |
17 unsigned long integer, oidbuf[10], outlen, inlen, x, y; | 457 unsigned long integer, oidbuf[10], outlen, inlen, x, y; |
18 mp_int mpinteger; | 458 void *mpinteger; |
19 ltc_utctime utctime = { 91, 5, 6, 16, 45, 40, 1, 7, 0 }; | 459 ltc_utctime utctime = { 91, 5, 6, 16, 45, 40, 1, 7, 0 }; |
20 | 460 |
21 /* setup variables */ | 461 /* setup variables */ |
22 for (x = 0; x < sizeof(bitbuf); x++) { bitbuf[x] = x & 1; } | 462 for (x = 0; x < sizeof(bitbuf); x++) { bitbuf[x] = x & 1; } |
23 for (x = 0; x < sizeof(octetbuf); x++) { octetbuf[x] = x; } | 463 for (x = 0; x < sizeof(octetbuf); x++) { octetbuf[x] = x; } |
24 for (x = 0; x < sizeof(ia5buf); x++) { ia5buf[x] = 'a'; } | 464 for (x = 0; x < sizeof(ia5buf); x++) { ia5buf[x] = 'a'; } |
25 for (x = 0; x < sizeof(printbuf); x++) { printbuf[x] = 'a'; } | 465 for (x = 0; x < sizeof(printbuf); x++) { printbuf[x] = 'a'; } |
26 integer = 1; | 466 integer = 1; |
27 for (x = 0; x < sizeof(oidbuf)/sizeof(oidbuf[0]); x++) { oidbuf[x] = x + 1; } | 467 for (x = 0; x < sizeof(oidbuf)/sizeof(oidbuf[0]); x++) { oidbuf[x] = x + 1; } |
28 DO(mpi_to_ltc_error(mp_init(&mpinteger))); | 468 DO(mp_init(&mpinteger)); |
29 | 469 |
30 for (x = 0; x < 14; x++) { | 470 for (x = 0; x < 14; x++) { |
31 /* setup list */ | 471 /* setup list */ |
32 LTC_SET_ASN1(types, 0, LTC_ASN1_PRINTABLE_STRING, printbuf, sizeof(printbuf)); | 472 LTC_SET_ASN1(types, 0, LTC_ASN1_PRINTABLE_STRING, printbuf, sizeof(printbuf)); |
33 LTC_SET_ASN1(types, 1, LTC_ASN1_BIT_STRING, bitbuf, sizeof(bitbuf)); | 473 LTC_SET_ASN1(types, 1, LTC_ASN1_BIT_STRING, bitbuf, sizeof(bitbuf)); |
34 LTC_SET_ASN1(types, 2, LTC_ASN1_OCTET_STRING, octetbuf, sizeof(octetbuf)); | 474 LTC_SET_ASN1(types, 2, LTC_ASN1_OCTET_STRING, octetbuf, sizeof(octetbuf)); |
35 LTC_SET_ASN1(types, 3, LTC_ASN1_IA5_STRING, ia5buf, sizeof(ia5buf)); | 475 LTC_SET_ASN1(types, 3, LTC_ASN1_IA5_STRING, ia5buf, sizeof(ia5buf)); |
36 if (x > 7) { | 476 if (x > 7) { |
37 LTC_SET_ASN1(types, 4, LTC_ASN1_SHORT_INTEGER, &integer, 1); | 477 LTC_SET_ASN1(types, 4, LTC_ASN1_SHORT_INTEGER, &integer, 1); |
38 } else { | 478 } else { |
39 LTC_SET_ASN1(types, 4, LTC_ASN1_INTEGER, &mpinteger, 1); | 479 LTC_SET_ASN1(types, 4, LTC_ASN1_INTEGER, mpinteger, 1); |
40 } | 480 } |
41 LTC_SET_ASN1(types, 5, LTC_ASN1_OBJECT_IDENTIFIER, oidbuf, sizeof(oidbuf)/sizeof(oidbuf[0])); | 481 LTC_SET_ASN1(types, 5, LTC_ASN1_OBJECT_IDENTIFIER, oidbuf, sizeof(oidbuf)/sizeof(oidbuf[0])); |
42 LTC_SET_ASN1(types, 6, LTC_ASN1_UTCTIME, &utctime, 1); | 482 LTC_SET_ASN1(types, 6, LTC_ASN1_UTCTIME, &utctime, 1); |
43 | 483 |
44 LTC_SET_ASN1(host, 0, LTC_ASN1_CHOICE, types, 7); | 484 LTC_SET_ASN1(host, 0, LTC_ASN1_CHOICE, types, 7); |
48 outlen = sizeof(outbuf); | 488 outlen = sizeof(outbuf); |
49 DO(der_encode_sequence(&types[x>6?x-7:x], 1, outbuf, &outlen)); | 489 DO(der_encode_sequence(&types[x>6?x-7:x], 1, outbuf, &outlen)); |
50 | 490 |
51 /* decode it */ | 491 /* decode it */ |
52 inlen = outlen; | 492 inlen = outlen; |
53 DO(der_decode_sequence(outbuf, inlen, &host, 1)); | 493 DO(der_decode_sequence(outbuf, inlen, &host[0], 1)); |
54 | 494 |
55 for (y = 0; y < 7; y++) { | 495 for (y = 0; y < 7; y++) { |
56 if (types[y].used && y != (x>6?x-7:x)) { | 496 if (types[y].used && y != (x>6?x-7:x)) { |
57 fprintf(stderr, "CHOICE, flag %lu in trial %lu was incorrectly set to one\n", y, x); | 497 fprintf(stderr, "CHOICE, flag %lu in trial %lu was incorrectly set to one\n", y, x); |
58 return 1; | 498 return 1; |
61 fprintf(stderr, "CHOICE, flag %lu in trial %lu was incorrectly set to zero\n", y, x); | 501 fprintf(stderr, "CHOICE, flag %lu in trial %lu was incorrectly set to zero\n", y, x); |
62 return 1; | 502 return 1; |
63 } | 503 } |
64 } | 504 } |
65 } | 505 } |
66 mp_clear(&mpinteger); | 506 mp_clear(mpinteger); |
67 return 0; | 507 return 0; |
68 } | 508 } |
69 | 509 |
70 | 510 |
71 int der_tests(void) | 511 int der_tests(void) |
72 { | 512 { |
73 unsigned long x, y, z, zz, oid[2][32]; | 513 unsigned long x, y, z, zz, oid[2][32]; |
74 unsigned char buf[3][2048]; | 514 unsigned char buf[3][2048]; |
75 mp_int a, b, c, d, e, f, g; | 515 void *a, *b, *c, *d, *e, *f, *g; |
76 | 516 |
77 static const unsigned char rsa_oid_der[] = { 0x06, 0x06, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d }; | 517 static const unsigned char rsa_oid_der[] = { 0x06, 0x06, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d }; |
78 static const unsigned long rsa_oid[] = { 1, 2, 840, 113549 }; | 518 static const unsigned long rsa_oid[] = { 1, 2, 840, 113549 }; |
79 | 519 |
80 static const unsigned char rsa_ia5[] = "[email protected]"; | 520 static const unsigned char rsa_ia5[] = "[email protected]"; |
90 ltc_utctime tmp_time; | 530 ltc_utctime tmp_time; |
91 | 531 |
92 static const unsigned char rsa_time1_der[] = { 0x17, 0x11, 0x39, 0x31, 0x30, 0x35, 0x30, 0x36, 0x31, 0x36, 0x34, 0x35, 0x34, 0x30, 0x2D, 0x30, 0x37, 0x30, 0x30 }; | 532 static const unsigned char rsa_time1_der[] = { 0x17, 0x11, 0x39, 0x31, 0x30, 0x35, 0x30, 0x36, 0x31, 0x36, 0x34, 0x35, 0x34, 0x30, 0x2D, 0x30, 0x37, 0x30, 0x30 }; |
93 static const unsigned char rsa_time2_der[] = { 0x17, 0x0d, 0x39, 0x31, 0x30, 0x35, 0x30, 0x36, 0x32, 0x33, 0x34, 0x35, 0x34, 0x30, 0x5a }; | 533 static const unsigned char rsa_time2_der[] = { 0x17, 0x0d, 0x39, 0x31, 0x30, 0x35, 0x30, 0x36, 0x32, 0x33, 0x34, 0x35, 0x34, 0x30, 0x5a }; |
94 | 534 |
95 DO(mpi_to_ltc_error(mp_init_multi(&a, &b, &c, &d, &e, &f, &g, NULL))); | 535 static const wchar_t utf8_1[] = { 0x0041, 0x2262, 0x0391, 0x002E }; |
536 static const unsigned char utf8_1_der[] = { 0x0C, 0x07, 0x41, 0xE2, 0x89, 0xA2, 0xCE, 0x91, 0x2E }; | |
537 static const wchar_t utf8_2[] = { 0xD55C, 0xAD6D, 0xC5B4 }; | |
538 static const unsigned char utf8_2_der[] = { 0x0C, 0x09, 0xED, 0x95, 0x9C, 0xEA, 0xB5, 0xAD, 0xEC, 0x96, 0xB4 }; | |
539 | |
540 unsigned char utf8_buf[32]; | |
541 wchar_t utf8_out[32]; | |
542 | |
543 DO(mp_init_multi(&a, &b, &c, &d, &e, &f, &g, NULL)); | |
96 for (zz = 0; zz < 16; zz++) { | 544 for (zz = 0; zz < 16; zz++) { |
545 #ifdef USE_TFM | |
546 for (z = 0; z < 256; z++) { | |
547 #else | |
97 for (z = 0; z < 1024; z++) { | 548 for (z = 0; z < 1024; z++) { |
549 #endif | |
98 if (yarrow_read(buf[0], z, &yarrow_prng) != z) { | 550 if (yarrow_read(buf[0], z, &yarrow_prng) != z) { |
99 fprintf(stderr, "Failed to read %lu bytes from yarrow\n", z); | 551 fprintf(stderr, "Failed to read %lu bytes from yarrow\n", z); |
100 return 1; | 552 return 1; |
101 } | 553 } |
102 DO(mpi_to_ltc_error(mp_read_unsigned_bin(&a, buf[0], z))); | 554 DO(mp_read_unsigned_bin(a, buf[0], z)); |
103 if (mp_iszero(&a) == MP_NO) { a.sign = buf[0][0] & 1 ? MP_ZPOS : MP_NEG; } | 555 /* if (mp_iszero(a) == LTC_MP_NO) { a.sign = buf[0][0] & 1 ? LTC_MP_ZPOS : LTC_MP_NEG; } */ |
104 x = sizeof(buf[0]); | 556 x = sizeof(buf[0]); |
105 DO(der_encode_integer(&a, buf[0], &x)); | 557 DO(der_encode_integer(a, buf[0], &x)); |
106 DO(der_length_integer(&a, &y)); | 558 DO(der_length_integer(a, &y)); |
107 if (y != x) { fprintf(stderr, "DER INTEGER size mismatch\n"); return 1; } | 559 if (y != x) { fprintf(stderr, "DER INTEGER size mismatch\n"); return 1; } |
108 mp_zero(&b); | 560 mp_set_int(b, 0); |
109 DO(der_decode_integer(buf[0], y, &b)); | 561 DO(der_decode_integer(buf[0], y, b)); |
110 if (y != x || mp_cmp(&a, &b) != MP_EQ) { | 562 if (y != x || mp_cmp(a, b) != LTC_MP_EQ) { |
111 fprintf(stderr, "%lu: %lu vs %lu\n", z, x, y); | 563 fprintf(stderr, "%lu: %lu vs %lu\n", z, x, y); |
112 #ifdef BN_MP_TORADIX_C | 564 mp_clear_multi(a, b, c, d, e, f, g, NULL); |
113 mp_todecimal(&a, buf[0]); | |
114 mp_todecimal(&b, buf[1]); | |
115 fprintf(stderr, "a == %s\nb == %s\n", buf[0], buf[1]); | |
116 #endif | |
117 mp_clear_multi(&a, &b, &c, &d, &e, &f, &g, NULL); | |
118 return 1; | 565 return 1; |
119 } | 566 } |
120 } | 567 } |
121 } | 568 } |
122 | 569 |
126 if (yarrow_read(buf[0], z, &yarrow_prng) != z) { | 573 if (yarrow_read(buf[0], z, &yarrow_prng) != z) { |
127 fprintf(stderr, "Failed to read %lu bytes from yarrow\n", z); | 574 fprintf(stderr, "Failed to read %lu bytes from yarrow\n", z); |
128 return 1; | 575 return 1; |
129 } | 576 } |
130 /* encode with normal */ | 577 /* encode with normal */ |
131 DO(mpi_to_ltc_error(mp_read_unsigned_bin(&a, buf[0], z))); | 578 DO(mp_read_unsigned_bin(a, buf[0], z)); |
132 | 579 |
133 x = sizeof(buf[0]); | 580 x = sizeof(buf[0]); |
134 DO(der_encode_integer(&a, buf[0], &x)); | 581 DO(der_encode_integer(a, buf[0], &x)); |
135 | 582 |
136 /* encode with short */ | 583 /* encode with short */ |
137 y = sizeof(buf[1]); | 584 y = sizeof(buf[1]); |
138 DO(der_encode_short_integer(mp_get_int(&a), buf[1], &y)); | 585 DO(der_encode_short_integer(mp_get_int(a), buf[1], &y)); |
139 if (x != y || memcmp(buf[0], buf[1], x)) { | 586 if (x != y || memcmp(buf[0], buf[1], x)) { |
140 fprintf(stderr, "DER INTEGER short encoding failed, %lu, %lu\n", x, y); | 587 fprintf(stderr, "DER INTEGER short encoding failed, %lu, %lu\n", x, y); |
141 for (z = 0; z < x; z++) fprintf(stderr, "%02x ", buf[0][z]); fprintf(stderr, "\n"); | 588 for (z = 0; z < x; z++) fprintf(stderr, "%02x ", buf[0][z]); fprintf(stderr, "\n"); |
142 for (z = 0; z < y; z++) fprintf(stderr, "%02x ", buf[1][z]); fprintf(stderr, "\n"); | 589 for (z = 0; z < y; z++) fprintf(stderr, "%02x ", buf[1][z]); fprintf(stderr, "\n"); |
143 mp_clear_multi(&a, &b, &c, &d, &e, &f, &g, NULL); | 590 mp_clear_multi(a, b, c, d, e, f, g, NULL); |
144 return 1; | 591 return 1; |
145 } | 592 } |
146 | 593 |
147 /* decode it */ | 594 /* decode it */ |
148 x = 0; | 595 x = 0; |
149 DO(der_decode_short_integer(buf[1], y, &x)); | 596 DO(der_decode_short_integer(buf[1], y, &x)); |
150 if (x != mp_get_int(&a)) { | 597 if (x != mp_get_int(a)) { |
151 fprintf(stderr, "DER INTEGER short decoding failed, %lu, %lu\n", x, mp_get_int(&a)); | 598 fprintf(stderr, "DER INTEGER short decoding failed, %lu, %lu\n", x, mp_get_int(a)); |
152 mp_clear_multi(&a, &b, &c, &d, &e, &f, &g, NULL); | 599 mp_clear_multi(a, b, c, d, e, f, g, NULL); |
153 return 1; | 600 return 1; |
154 } | 601 } |
155 } | 602 } |
156 } | 603 } |
157 mp_clear_multi(&a, &b, &c, &d, &e, &f, &g, NULL); | 604 mp_clear_multi(a, b, c, d, e, f, g, NULL); |
158 | 605 |
159 | 606 |
160 /* Test bit string */ | 607 /* Test bit string */ |
161 for (zz = 1; zz < 1536; zz++) { | 608 for (zz = 1; zz < 1536; zz++) { |
162 yarrow_read(buf[0], zz, &yarrow_prng); | 609 yarrow_read(buf[0], zz, &yarrow_prng); |
197 } | 644 } |
198 } | 645 } |
199 | 646 |
200 /* test OID */ | 647 /* test OID */ |
201 x = sizeof(buf[0]); | 648 x = sizeof(buf[0]); |
202 DO(der_encode_object_identifier(rsa_oid, sizeof(rsa_oid)/sizeof(rsa_oid[0]), buf[0], &x)); | 649 DO(der_encode_object_identifier((unsigned long*)rsa_oid, sizeof(rsa_oid)/sizeof(rsa_oid[0]), buf[0], &x)); |
203 if (x != sizeof(rsa_oid_der) || memcmp(rsa_oid_der, buf[0], x)) { | 650 if (x != sizeof(rsa_oid_der) || memcmp(rsa_oid_der, buf[0], x)) { |
204 fprintf(stderr, "rsa_oid_der encode failed to match, %lu, ", x); | 651 fprintf(stderr, "rsa_oid_der encode failed to match, %lu, ", x); |
205 for (y = 0; y < x; y++) fprintf(stderr, "%02x ", buf[0][y]); | 652 for (y = 0; y < x; y++) fprintf(stderr, "%02x ", buf[0][y]); |
206 fprintf(stderr, "\n"); | 653 fprintf(stderr, "\n"); |
207 return 1; | 654 return 1; |
257 } | 704 } |
258 } | 705 } |
259 | 706 |
260 /* IA5 string */ | 707 /* IA5 string */ |
261 x = sizeof(buf[0]); | 708 x = sizeof(buf[0]); |
262 DO(der_encode_ia5_string(rsa_ia5, strlen(rsa_ia5), buf[0], &x)); | 709 DO(der_encode_ia5_string(rsa_ia5, strlen((char*)rsa_ia5), buf[0], &x)); |
263 if (x != sizeof(rsa_ia5_der) || memcmp(buf[0], rsa_ia5_der, x)) { | 710 if (x != sizeof(rsa_ia5_der) || memcmp(buf[0], rsa_ia5_der, x)) { |
264 fprintf(stderr, "IA5 encode failed: %lu, %lu\n", x, (unsigned long)sizeof(rsa_ia5_der)); | 711 fprintf(stderr, "IA5 encode failed: %lu, %lu\n", x, (unsigned long)sizeof(rsa_ia5_der)); |
265 return 1; | 712 return 1; |
266 } | 713 } |
267 DO(der_length_ia5_string(rsa_ia5, strlen(rsa_ia5), &y)); | 714 DO(der_length_ia5_string(rsa_ia5, strlen((char*)rsa_ia5), &y)); |
268 if (y != x) { | 715 if (y != x) { |
269 fprintf(stderr, "IA5 length failed to match: %lu, %lu\n", x, y); | 716 fprintf(stderr, "IA5 length failed to match: %lu, %lu\n", x, y); |
270 return 1; | 717 return 1; |
271 } | 718 } |
272 y = sizeof(buf[1]); | 719 y = sizeof(buf[1]); |
273 DO(der_decode_ia5_string(buf[0], x, buf[1], &y)); | 720 DO(der_decode_ia5_string(buf[0], x, buf[1], &y)); |
274 if (y != strlen(rsa_ia5) || memcmp(buf[1], rsa_ia5, strlen(rsa_ia5))) { | 721 if (y != strlen((char*)rsa_ia5) || memcmp(buf[1], rsa_ia5, strlen((char*)rsa_ia5))) { |
275 fprintf(stderr, "DER IA5 failed test vector\n"); | 722 fprintf(stderr, "DER IA5 failed test vector\n"); |
276 return 1; | 723 return 1; |
277 } | 724 } |
278 | 725 |
279 /* Printable string */ | 726 /* Printable string */ |
280 x = sizeof(buf[0]); | 727 x = sizeof(buf[0]); |
281 DO(der_encode_printable_string(rsa_printable, strlen(rsa_printable), buf[0], &x)); | 728 DO(der_encode_printable_string(rsa_printable, strlen((char*)rsa_printable), buf[0], &x)); |
282 if (x != sizeof(rsa_printable_der) || memcmp(buf[0], rsa_printable_der, x)) { | 729 if (x != sizeof(rsa_printable_der) || memcmp(buf[0], rsa_printable_der, x)) { |
283 fprintf(stderr, "PRINTABLE encode failed: %lu, %lu\n", x, (unsigned long)sizeof(rsa_printable_der)); | 730 fprintf(stderr, "PRINTABLE encode failed: %lu, %lu\n", x, (unsigned long)sizeof(rsa_printable_der)); |
284 return 1; | 731 return 1; |
285 } | 732 } |
286 DO(der_length_printable_string(rsa_printable, strlen(rsa_printable), &y)); | 733 DO(der_length_printable_string(rsa_printable, strlen((char*)rsa_printable), &y)); |
287 if (y != x) { | 734 if (y != x) { |
288 fprintf(stderr, "printable length failed to match: %lu, %lu\n", x, y); | 735 fprintf(stderr, "printable length failed to match: %lu, %lu\n", x, y); |
289 return 1; | 736 return 1; |
290 } | 737 } |
291 y = sizeof(buf[1]); | 738 y = sizeof(buf[1]); |
292 DO(der_decode_printable_string(buf[0], x, buf[1], &y)); | 739 DO(der_decode_printable_string(buf[0], x, buf[1], &y)); |
293 if (y != strlen(rsa_printable) || memcmp(buf[1], rsa_printable, strlen(rsa_printable))) { | 740 if (y != strlen((char*)rsa_printable) || memcmp(buf[1], rsa_printable, strlen((char*)rsa_printable))) { |
294 fprintf(stderr, "DER printable failed test vector\n"); | 741 fprintf(stderr, "DER printable failed test vector\n"); |
295 return 1; | 742 return 1; |
296 } | 743 } |
297 | 744 |
298 /* Test UTC time */ | 745 /* Test UTC time */ |
299 x = sizeof(buf[0]); | 746 x = sizeof(buf[0]); |
300 DO(der_encode_utctime(&rsa_time1, buf[0], &x)); | 747 DO(der_encode_utctime((ltc_utctime*)&rsa_time1, buf[0], &x)); |
301 if (x != sizeof(rsa_time1_der) || memcmp(buf[0], rsa_time1_der, x)) { | 748 if (x != sizeof(rsa_time1_der) || memcmp(buf[0], rsa_time1_der, x)) { |
302 fprintf(stderr, "UTCTIME encode of rsa_time1 failed: %lu, %lu\n", x, (unsigned long)sizeof(rsa_time1_der)); | 749 fprintf(stderr, "UTCTIME encode of rsa_time1 failed: %lu, %lu\n", x, (unsigned long)sizeof(rsa_time1_der)); |
303 fprintf(stderr, "\n\n"); | 750 fprintf(stderr, "\n\n"); |
304 for (y = 0; y < x; y++) fprintf(stderr, "%02x ", buf[0][y]); printf("\n"); | 751 for (y = 0; y < x; y++) fprintf(stderr, "%02x ", buf[0][y]); printf("\n"); |
305 | 752 |
306 return 1; | 753 return 1; |
307 } | 754 } |
308 DO(der_length_utctime(&rsa_time1, &y)); | 755 DO(der_length_utctime((ltc_utctime*)&rsa_time1, &y)); |
309 if (y != x) { | 756 if (y != x) { |
310 fprintf(stderr, "UTCTIME length failed to match for rsa_time1: %lu, %lu\n", x, y); | 757 fprintf(stderr, "UTCTIME length failed to match for rsa_time1: %lu, %lu\n", x, y); |
311 return 1; | 758 return 1; |
312 } | 759 } |
313 DO(der_decode_utctime(buf[0], &y, &tmp_time)); | 760 DO(der_decode_utctime(buf[0], &y, &tmp_time)); |
325 tmp_time.off_hh); | 772 tmp_time.off_hh); |
326 return 1; | 773 return 1; |
327 } | 774 } |
328 | 775 |
329 x = sizeof(buf[0]); | 776 x = sizeof(buf[0]); |
330 DO(der_encode_utctime(&rsa_time2, buf[0], &x)); | 777 DO(der_encode_utctime((ltc_utctime*)&rsa_time2, buf[0], &x)); |
331 if (x != sizeof(rsa_time2_der) || memcmp(buf[0], rsa_time2_der, x)) { | 778 if (x != sizeof(rsa_time2_der) || memcmp(buf[0], rsa_time2_der, x)) { |
332 fprintf(stderr, "UTCTIME encode of rsa_time2 failed: %lu, %lu\n", x, (unsigned long)sizeof(rsa_time1_der)); | 779 fprintf(stderr, "UTCTIME encode of rsa_time2 failed: %lu, %lu\n", x, (unsigned long)sizeof(rsa_time1_der)); |
333 fprintf(stderr, "\n\n"); | 780 fprintf(stderr, "\n\n"); |
334 for (y = 0; y < x; y++) fprintf(stderr, "%02x ", buf[0][y]); printf("\n"); | 781 for (y = 0; y < x; y++) fprintf(stderr, "%02x ", buf[0][y]); printf("\n"); |
335 | 782 |
336 return 1; | 783 return 1; |
337 } | 784 } |
338 DO(der_length_utctime(&rsa_time2, &y)); | 785 DO(der_length_utctime((ltc_utctime*)&rsa_time2, &y)); |
339 if (y != x) { | 786 if (y != x) { |
340 fprintf(stderr, "UTCTIME length failed to match for rsa_time2: %lu, %lu\n", x, y); | 787 fprintf(stderr, "UTCTIME length failed to match for rsa_time2: %lu, %lu\n", x, y); |
341 return 1; | 788 return 1; |
342 } | 789 } |
343 DO(der_decode_utctime(buf[0], &y, &tmp_time)); | 790 DO(der_decode_utctime(buf[0], &y, &tmp_time)); |
356 | 803 |
357 | 804 |
358 return 1; | 805 return 1; |
359 } | 806 } |
360 | 807 |
361 | 808 /* UTF 8 */ |
362 | 809 /* encode it */ |
810 x = sizeof(utf8_buf); | |
811 DO(der_encode_utf8_string(utf8_1, sizeof(utf8_1) / sizeof(utf8_1[0]), utf8_buf, &x)); | |
812 if (x != sizeof(utf8_1_der) || memcmp(utf8_buf, utf8_1_der, x)) { | |
813 fprintf(stderr, "DER UTF8_1 encoded to %lu bytes\n", x); | |
814 for (y = 0; y < x; y++) fprintf(stderr, "%02x ", (unsigned)utf8_buf[y]); fprintf(stderr, "\n"); | |
815 return 1; | |
816 } | |
817 /* decode it */ | |
818 y = sizeof(utf8_out) / sizeof(utf8_out[0]); | |
819 DO(der_decode_utf8_string(utf8_buf, x, utf8_out, &y)); | |
820 if (y != (sizeof(utf8_1) / sizeof(utf8_1[0])) || memcmp(utf8_1, utf8_out, y * sizeof(wchar_t))) { | |
821 fprintf(stderr, "DER UTF8_1 decoded to %lu wchar_t\n", y); | |
822 for (x = 0; x < y; x++) fprintf(stderr, "%04lx ", (unsigned long)utf8_out[x]); fprintf(stderr, "\n"); | |
823 return 1; | |
824 } | |
825 | |
826 /* encode it */ | |
827 x = sizeof(utf8_buf); | |
828 DO(der_encode_utf8_string(utf8_2, sizeof(utf8_2) / sizeof(utf8_2[0]), utf8_buf, &x)); | |
829 if (x != sizeof(utf8_2_der) || memcmp(utf8_buf, utf8_2_der, x)) { | |
830 fprintf(stderr, "DER UTF8_2 encoded to %lu bytes\n", x); | |
831 for (y = 0; y < x; y++) fprintf(stderr, "%02x ", (unsigned)utf8_buf[y]); fprintf(stderr, "\n"); | |
832 return 1; | |
833 } | |
834 /* decode it */ | |
835 y = sizeof(utf8_out) / sizeof(utf8_out[0]); | |
836 DO(der_decode_utf8_string(utf8_buf, x, utf8_out, &y)); | |
837 if (y != (sizeof(utf8_2) / sizeof(utf8_2[0])) || memcmp(utf8_2, utf8_out, y * sizeof(wchar_t))) { | |
838 fprintf(stderr, "DER UTF8_2 decoded to %lu wchar_t\n", y); | |
839 for (x = 0; x < y; x++) fprintf(stderr, "%04lx ", (unsigned long)utf8_out[x]); fprintf(stderr, "\n"); | |
840 return 1; | |
841 } | |
842 | |
843 | |
844 der_set_test(); | |
845 der_flexi_test(); | |
363 return der_choice_test(); | 846 return der_choice_test(); |
364 } | 847 } |
365 | 848 |
366 #endif | 849 #endif |
367 | 850 |
368 /* $Source: /cvs/libtom/libtomcrypt/testprof/der_tests.c,v $ */ | 851 /* $Source: /cvs/libtom/libtomcrypt/testprof/der_tests.c,v $ */ |
369 /* $Revision: 1.25 $ */ | 852 /* $Revision: 1.49 $ */ |
370 /* $Date: 2005/06/20 20:37:45 $ */ | 853 /* $Date: 2006/11/26 02:10:21 $ */ |