16
static void der_set_test(void)
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;
23
unsigned char strs[10][10], outbuf[128];
24
unsigned long x, val, outlen;
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);
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));
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]));
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));
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");
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");
62
fprintf(stderr, "error decoding set using der_decode_set (int_val is wrong):\n");
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");
77
for (x = 0; x < 10; x++) {
78
LTC_SET_ASN1(list, x, LTC_ASN1_PRINTABLE_STRING, strs[x], strlen((char*)strs[x]));
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));
87
for (x = 0; x < 10; x++) {
88
LTC_SET_ASN1(list, x, LTC_ASN1_PRINTABLE_STRING, strs[x], sizeof(strs[x]) - 1);
90
XMEMSET(strs, 0, sizeof(strs));
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));
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);
111
PRINTABLE "printable"
115
UTCTIME { 91, 5, 6, 16, 45, 40, 1, 7, 0 }
117
OCTET STRING { 1, 2, 3, 4 }
118
BIT STRING { 1, 0, 0, 1 }
120
OID { 1, 2, 840, 113549 }
123
PRINTABLE "333" // WILL GET SORTED
132
static void der_flexi_test(void)
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 };
144
unsigned char encode_buf[192];
145
unsigned long encode_buf_len, decode_len;
148
ltc_asn1_list static_list[5][3], *decoded_list, *l;
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);
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);
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);
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);
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));
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));
180
f = fopen("t.bin", "wb");
181
fwrite(encode_buf, 1, encode_buf_len, f);
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));
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);
198
/* we expect l->next to be NULL and l->child to not be */
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);
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);
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);
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);
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);
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);
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);
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);
252
/* expect child anve move down */
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);
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);
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);
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);
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);
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);
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);
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);
306
/* expect child anve move down */
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);
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);
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);
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);
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);
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);
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);
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);
360
/* expect child anve move down */
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
449
der_sequence_free(l);
13
453
static int der_choice_test(void)
15
455
ltc_asn1_list types[7], host[1];
16
456
unsigned char bitbuf[10], octetbuf[10], ia5buf[10], printbuf[10], outbuf[256];
17
457
unsigned long integer, oidbuf[10], outlen, inlen, x, y;
19
459
ltc_utctime utctime = { 91, 5, 6, 16, 45, 40, 1, 7, 0 };
21
461
/* setup variables */