comparison libtomcrypt/tests/der_test.c @ 1471:6dba84798cd5

Update to libtomcrypt 1.18.1, merged with Dropbear changes
author Matt Johnston <matt@ucc.asn.au>
date Fri, 09 Feb 2018 21:44:05 +0800
parents
children e9dba7abd939
comparison
equal deleted inserted replaced
1470:8bba51a55704 1471:6dba84798cd5
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
2 *
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
5 *
6 * The library is free for all purposes without any express
7 * guarantee it works.
8 */
9 #include <tomcrypt_test.h>
10 #if defined(GMP_LTC_DESC) || defined(USE_GMP)
11 #include <gmp.h>
12 #endif
13
14 #if !defined(LTC_DER) || !defined(LTC_TEST_MPI)
15
16 int der_test(void)
17 {
18 return CRYPT_NOP;
19 }
20
21 #else
22
23 static const unsigned char _der_tests_stinky_root_cert[] =
24 "MIIFETCCA/mgAwIBAgIQbv53JNmv518t5lkCHE272jANBgkqhkiG9w0BAQUFADCB"
25 "lTELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAlVUMRcwFQYDVQQHEw5TYWx0IExha2Ug"
26 "Q2l0eTEeMBwGA1UEChMVVGhlIFVTRVJUUlVTVCBOZXR3b3JrMSEwHwYDVQQLExho"
27 "dHRwOi8vd3d3LnVzZXJ0cnVzdC5jb20xHTAbBgNVBAMTFFVUTi1VU0VSRmlyc3Qt"
28 "T2JqZWN0MB4XDTA4MDQyOTAwMDAwMFoXDTEwMDQyOTIzNTk1OVowgbUxCzAJBgNV"
29 "BAYTAlVTMQ4wDAYDVQQRDAU0NDE0MzELMAkGA1UECAwCT0gxGTAXBgNVBAcMEE1h"
30 "eWZpZWxkIFZpbGxhZ2UxEDAOBgNVBAkMB1N1aXRlIEExFDASBgNVBAkMCzc2NyBC"
31 "ZXRhIERyMSIwIAYDVQQKDBlQcmVlbXB0aXZlIFNvbHV0aW9ucywgTExDMSIwIAYD"
32 "VQQDDBlQcmVlbXB0aXZlIFNvbHV0aW9ucywgTExDMIIBIjANBgkqhkiG9w0BAQEF"
33 "AAOCAQ8AMIIBCgKCAQEAzH7ZBkMcBuHx8d2f10RGTHAf7gzzVteGbOihJGH2BwlS"
34 "ZvNp6WEE4DfL+s1vp0wzk1XeLN5tRjg2qum9YqyCk7okh7pXGy46f5mWbLQiefGA"
35 "j5UXRcr6WJ3xeACdbXxKrYMV0REia+4Jb2UbFA8S81PjhRon6vcRz76ziUWwt8NC"
36 "igX+4ZC0skhhKzKszel6KGL7bJCtLG7ukw9DZCrvPCRcKFeM/GwQ6ACMgP88CSCL"
37 "t1fbIXDH1vd/x2XM3QlaSDN6hYDbef8m1T+9TCkXVKeqG1GYjSUrHzYnCZUmTRrR"
38 "38jgC3qXxiIpDKW105uM0nlXe2XF9c+ot2MdWvV4TwIDAQABo4IBOTCCATUwHwYD"
39 "VR0jBBgwFoAU2u1kdBScFDyr3ZmpvVsoTYs8ydgwHQYDVR0OBBYEFK+1HzZE4i28"
40 "oLIzuqlFR9SspiCIMA4GA1UdDwEB/wQEAwIHgDAMBgNVHRMBAf8EAjAAMBMGA1Ud"
41 "JQQMMAoGCCsGAQUFBwMDMBEGCWCGSAGG+EIBAQQEAwIEEDBGBgNVHSAEPzA9MDsG"
42 "DCsGAQQBsjEBAgEDAjArMCkGCCsGAQUFBwIBFh1odHRwczovL3NlY3VyZS5jb21v"
43 "ZG8ubmV0L0NQUzBCBgNVHR8EOzA5MDegNaAzhjFodHRwOi8vY3JsLnVzZXJ0cnVz"
44 "dC5jb20vVVROLVVTRVJGaXJzdC1PYmplY3QuY3JsMCEGA1UdEQQaMBiBFnN1cHBv"
45 "cnRAcHJlZW1wdGl2ZS5jb20wDQYJKoZIhvcNAQEFBQADggEBAC+JM26Dokvonudl"
46 "JXe/Yun7IBhimkagZUjbk9l/GQWN6i+v1o95UJ1wGJtBdm2+MxbSaPoNTDZR4B+2"
47 "lYL9MW57UVmePrnfUPXQKZZG+8gTRDz8+7ol/CEAKmS3MLKCRcH5oe+J5345sGxi"
48 "FC/KWNKedTNraW95xlg8NTlL2yRP7TMsjvBxgLmkbaFUoXzPTbQWmtovIagIT8GC"
49 "JeXwdFaRjbamiz3Irl+u7x/mhxdza6RvgBYylXRFMudANpeGsV7gDXlnfzpFDKHQ"
50 "niVwB7P5sbPFIlmIc+4/xRItkLIRjCVXaepgN9KYu3VOgiSDI6wXiTwP44/LUXQM"
51 "hetwa7s=";
52 const unsigned char _der_tests_cacert_root_cert[] =
53 "MIIHPTCCBSWgAwIBAgIBADANBgkqhkiG9w0BAQQFADB5MRAwDgYDVQQKEwdSb290"
54 "IENBMR4wHAYDVQQLExVodHRwOi8vd3d3LmNhY2VydC5vcmcxIjAgBgNVBAMTGUNB"
55 "IENlcnQgU2lnbmluZyBBdXRob3JpdHkxITAfBgkqhkiG9w0BCQEWEnN1cHBvcnRA"
56 "Y2FjZXJ0Lm9yZzAeFw0wMzAzMzAxMjI5NDlaFw0zMzAzMjkxMjI5NDlaMHkxEDAO"
57 "BgNVBAoTB1Jvb3QgQ0ExHjAcBgNVBAsTFWh0dHA6Ly93d3cuY2FjZXJ0Lm9yZzEi"
58 "MCAGA1UEAxMZQ0EgQ2VydCBTaWduaW5nIEF1dGhvcml0eTEhMB8GCSqGSIb3DQEJ"
59 "ARYSc3VwcG9ydEBjYWNlcnQub3JnMIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIIC"
60 "CgKCAgEAziLA4kZ97DYoB1CW8qAzQIxL8TtmPzHlawI229Z89vGIj053NgVBlfkJ"
61 "8BLPRoZzYLdufujAWGSuzbCtRRcMY/pnCujW0r8+55jE8Ez64AO7NV1sId6eINm6"
62 "zWYyN3L69wj1x81YyY7nDl7qPv4coRQKFWyGhFtkZip6qUtTefWIonvuLwphK42y"
63 "fk1WpRPs6tqSnqxEQR5YYGUFZvjARL3LlPdCfgv3ZWiYUQXw8wWRBB0bF4LsyFe7"
64 "w2t6iPGwcswlWyCR7BYCEo8y6RcYSNDHBS4CMEK4JZwFaz+qOqfrU0j36NK2B5jc"
65 "G8Y0f3/JHIJ6BVgrCFvzOKKrF11myZjXnhCLotLddJr3cQxyYN/Nb5gznZY0dj4k"
66 "epKwDpUeb+agRThHqtdB7Uq3EvbXG4OKDy7YCbZZ16oE/9KTfWgu3YtLq1i6L43q"
67 "laegw1SJpfvbi1EinbLDvhG+LJGGi5Z4rSDTii8aP8bQUWWHIbEZAWV/RRyH9XzQ"
68 "QUxPKZgh/TMfdQwEUfoZd9vUFBzugcMd9Zi3aQaRIt0AUMyBMawSB3s42mhb5ivU"
69 "fslfrejrckzzAeVLIL+aplfKkQABi6F1ITe1Yw1nPkZPcCBnzsXWWdsC4PDSy826"
70 "YreQQejdIOQpvGQpQsgi3Hia/0PsmBsJUUtaWsJx8cTLc6nloQsCAwEAAaOCAc4w"
71 "ggHKMB0GA1UdDgQWBBQWtTIb1Mfz4OaO873SsDrusjkY0TCBowYDVR0jBIGbMIGY"
72 "gBQWtTIb1Mfz4OaO873SsDrusjkY0aF9pHsweTEQMA4GA1UEChMHUm9vdCBDQTEe"
73 "MBwGA1UECxMVaHR0cDovL3d3dy5jYWNlcnQub3JnMSIwIAYDVQQDExlDQSBDZXJ0"
74 "IFNpZ25pbmcgQXV0aG9yaXR5MSEwHwYJKoZIhvcNAQkBFhJzdXBwb3J0QGNhY2Vy"
75 "dC5vcmeCAQAwDwYDVR0TAQH/BAUwAwEB/zAyBgNVHR8EKzApMCegJaAjhiFodHRw"
76 "czovL3d3dy5jYWNlcnQub3JnL3Jldm9rZS5jcmwwMAYJYIZIAYb4QgEEBCMWIWh0"
77 "dHBzOi8vd3d3LmNhY2VydC5vcmcvcmV2b2tlLmNybDA0BglghkgBhvhCAQgEJxYl"
78 "aHR0cDovL3d3dy5jYWNlcnQub3JnL2luZGV4LnBocD9pZD0xMDBWBglghkgBhvhC"
79 "AQ0ESRZHVG8gZ2V0IHlvdXIgb3duIGNlcnRpZmljYXRlIGZvciBGUkVFIGhlYWQg"
80 "b3ZlciB0byBodHRwOi8vd3d3LmNhY2VydC5vcmcwDQYJKoZIhvcNAQEEBQADggIB"
81 "ACjH7pyCArpcgBLKNQodgW+JapnM8mgPf6fhjViVPr3yBsOQWqy1YPaZQwGjiHCc"
82 "nWKdpIevZ1gNMDY75q1I08t0AoZxPuIrA2jxNGJARjtT6ij0rPtmlVOKTV39O9lg"
83 "18p5aTuxZZKmxoGCXJzN600BiqXfEVWqFcofN8CCmHBh22p8lqOOLlQ+TyGpkO/c"
84 "gr/c6EWtTZBzCDyUZbAEmXZ/4rzCahWqlwQ3JNgelE5tDlG+1sSPypZt90Pf6DBl"
85 "Jzt7u0NDY8RD97LsaMzhGY4i+5jhe1o+ATc7iwiwovOVThrLm82asduycPAtStvY"
86 "sONvRUgzEv/+PDIqVPfE94rwiCPCR/5kenHA0R6mY7AHfqQv0wGP3J8rtsYIqQ+T"
87 "SCX8Ev2fQtzzxD72V7DX3WnRBnc0CkvSyqD/HMaMyRa+xMwyN2hzXwj7UfdJUzYF"
88 "CpUCTPJ5GhD22Dp1nPMd8aINcGeGG7MW9S/lpOt5hvk9C8JzC6WZrG/8Z7jlLwum"
89 "GCSNe9FINSkYQKyTYOGWhlC0elnYjyELn8+CkcY7v2vcB5G5l1YjqrZslMZIBjzk"
90 "zk6q5PYvCdxTby78dOs6Y5nCpqyJvKeyRKANihDjbPIky/qbn3BHLt4Ui9SyIAmW"
91 "omTxJBzcoTWcFbLUvFUufQb1nA5V9FrWk9p2rSVzTMVD";
92 const unsigned long _der_tests_cacert_root_cert_size = sizeof(_der_tests_cacert_root_cert);
93
94 /*
95 SEQUENCE(3 elem)
96 SEQUENCE(8 elem)
97 [0](1)
98 INTEGER 2
99 INTEGER 0
100 SEQUENCE(2 elem)
101 OBJECT IDENTIFIER 1.2.840.113549.1.1.4
102 NULL
103 SEQUENCE(4 elem)
104 SET(1 elem)
105 SEQUENCE(2 elem)
106 OBJECT IDENTIFIER 2.5.4.10
107 PrintableString Root CA
108 SET(1 elem)
109 SEQUENCE(2 elem)
110 OBJECT IDENTIFIER 2.5.4.11
111 PrintableString http://www.cacert.org
112 SET(1 elem)
113 SEQUENCE(2 elem)
114 OBJECT IDENTIFIER 2.5.4.3
115 PrintableString CA Cert Signing Authority
116 SET(1 elem)
117 SEQUENCE(2 elem)
118 OBJECT IDENTIFIER 1.2.840.113549.1.9.1
119 IA5String [email protected]
120 SEQUENCE(2 elem)
121 UTCTime03-03-30 12:29:49 UTC
122 UTCTime33-03-29 12:29:49 UTC
123 SEQUENCE(4 elem)
124 SET(1 elem)
125 SEQUENCE(2 elem)
126 OBJECT IDENTIFIER 2.5.4.10
127 PrintableString Root CA
128 SET(1 elem)
129 SEQUENCE(2 elem)
130 OBJECT IDENTIFIER 2.5.4.11
131 PrintableString http://www.cacert.org
132 SET(1 elem)
133 SEQUENCE(2 elem)
134 OBJECT IDENTIFIER 2.5.4.3
135 PrintableString CA Cert Signing Authority
136 SET(1 elem)
137 SEQUENCE(2 elem)
138 OBJECT IDENTIFIER 1.2.840.113549.1.9.1
139 IA5String [email protected]
140 SEQUENCE(2 elem)
141 SEQUENCE(2 elem)
142 OBJECT IDENTIFIER 1.2.840.113549.1.1.1
143 NULL
144 BIT STRING(1 elem)
145 SEQUENCE(2 elem)
146 INTEGER (4096 bit)
147 INTEGER 65537
148 [3](1)
149 SEQUENCE(7 elem)
150 SEQUENCE(2 elem)
151 OBJECT IDENTIFIER 2.5.29.14
152 OCTET STRING(1 elem)
153 OCTET STRING(20 byte) 16B5321BD4C7F3E0E68EF3BDD2B03AEEB23918D1
154 SEQUENCE(2 elem)
155 OBJECT IDENTIFIER 2.5.29.35
156 OCTET STRING(1 elem)
157 SEQUENCE(3 elem)
158 [0]
159 [1](1)
160 [4](1)
161 SEQUENCE(4 elem)
162 SET(1 elem)
163 SEQUENCE(2 elem)
164 OBJECT IDENTIFIER 2.5.4.10
165 PrintableString Root CA
166 SET(1 elem)
167 SEQUENCE(2 elem)
168 OBJECT IDENTIFIER 2.5.4.11
169 PrintableString http://www.cacert.org
170 SET(1 elem)
171 SEQUENCE(2 elem)
172 OBJECT IDENTIFIER 2.5.4.3
173 PrintableString CA Cert Signing Authority
174 SET(1 elem)
175 SEQUENCE(2 elem)
176 OBJECT IDENTIFIER 1.2.840.113549.1.9.1
177 IA5String [email protected]
178 [2]
179 SEQUENCE(3 elem)
180 OBJECT IDENTIFIER 2.5.29.19
181 BOOLEAN true
182 OCTET STRING(1 elem)
183 SEQUENCE(1 elem)
184 BOOLEAN true
185 SEQUENCE(2 elem)
186 OBJECT IDENTIFIER 2.5.29.31
187 OCTET STRING(1 elem)
188 SEQUENCE(1 elem)
189 SEQUENCE(1 elem)
190 [0](1)
191 [0](1)
192 [6]
193 SEQUENCE(2 elem)
194 OBJECT IDENTIFIER 2.16.840.1.113730.1.4
195 OCTET STRING(1 elem)
196 IA5String https://www.cacert.org/revoke.crl
197 SEQUENCE(2 elem)
198 OBJECT IDENTIFIER 2.16.840.1.113730.1.8
199 OCTET STRING(1 elem)
200 IA5String http://www.cacert.org/index.php?id=10
201 SEQUENCE(2 elem)
202 OBJECT IDENTIFIER 2.16.840.1.113730.1.13
203 OCTET STRING(1 elem)
204 IA5String To get your own certificate for FREE head over to http://www.cacert.org
205 SEQUENCE(2 elem)
206 OBJECT IDENTIFIER 1.2.840.113549.1.1.4
207 NULL
208 BIT STRING(4096 bit)
209 */
210
211 #define __ASN1_FMTSTRING_FMT "line: %d, type=%d, size=%lu, data=%p, self=%p, next=%p, prev=%p, parent=%p, child=%p"
212 #define __ASN1_FMTSTRING_VAL(l) __LINE__, (l)->type, (l)->size, (l)->data, (l), (l)->next, (l)->prev, (l)->parent, (l)->child
213
214 #define __ASN1_ERR(l) fprintf(stderr, __ASN1_FMTSTRING_FMT "\n", __ASN1_FMTSTRING_VAL(l)); \
215 exit(EXIT_FAILURE)
216
217 #define __CHECK_ASN1_HAS(l, w) do { if ((l)->w == NULL) { \
218 __ASN1_ERR(l);\
219 } } while(0)
220
221 #define __CHECK_ASN1_HAS_NO(l, w) do { if ((l)->w != NULL) { \
222 __ASN1_ERR(l);\
223 } } while(0)
224
225
226
227 #define CHECK_ASN1_TYPE(l, t) do { if ((l)->type != (t)) { \
228 __ASN1_ERR(l);\
229 } } while(0)
230
231 #define CHECK_ASN1_HAS_CHILD(l) __CHECK_ASN1_HAS(l, child)
232 #define CHECK_ASN1_HAS_NO_CHILD(l) __CHECK_ASN1_HAS_NO(l, child)
233 #define CHECK_ASN1_HAS_NEXT(l) __CHECK_ASN1_HAS(l, next)
234 #define CHECK_ASN1_HAS_NO_NEXT(l) __CHECK_ASN1_HAS_NO(l, next)
235 #define CHECK_ASN1_HAS_DATA(l) __CHECK_ASN1_HAS(l, data)
236 #define CHECK_ASN1_HAS_NO_DATA(l) __CHECK_ASN1_HAS_NO(l, data)
237
238 #ifdef LTC_DER_TESTS_PRINT_FLEXI
239 static void _der_tests_print_flexi(ltc_asn1_list* l, unsigned int level)
240 {
241 char buf[1024];
242 const char* name = NULL;
243 const char* text = NULL;
244 ltc_asn1_list* ostring = NULL;
245 unsigned int n;
246
247 switch (l->type)
248 {
249 case LTC_ASN1_EOL:
250 name = "EOL";
251 snprintf(buf, sizeof(buf),__ASN1_FMTSTRING_FMT "\n", __ASN1_FMTSTRING_VAL(l));
252 text = buf;
253 break;
254 case LTC_ASN1_BOOLEAN:
255 name = "BOOLEAN";
256 {
257 if (*(int*)l->data)
258 text = "true";
259 else
260 text = "false";
261 }
262 break;
263 case LTC_ASN1_INTEGER:
264 name = "INTEGER";
265 break;
266 case LTC_ASN1_SHORT_INTEGER:
267 name = "SHORT INTEGER";
268 break;
269 case LTC_ASN1_BIT_STRING:
270 name = "BIT STRING";
271 break;
272 case LTC_ASN1_OCTET_STRING:
273 name = "OCTET STRING";
274 {
275 unsigned long ostring_l = l->size;
276 /* sometimes there's another sequence in an octet string...
277 * try to decode that... if it fails print out the octet string
278 */
279 if (der_decode_sequence_flexi(l->data, &ostring_l, &ostring) == CRYPT_OK) {
280 text = "";
281 }
282 else {
283 int r;
284 char* s = buf;
285 int sz = sizeof(buf);
286 for (n = 0; n < l->size; ++n) {
287 r = snprintf(s, sz, "%02X", ((unsigned char*)l->data)[n]);
288 if (r < 0 || r >= sz) {
289 fprintf(stderr, "%s boom\n", name);
290 exit(EXIT_FAILURE);
291 }
292 s += r;
293 sz -= r;
294 }
295 text = buf;
296 }
297 }
298 break;
299 case LTC_ASN1_NULL:
300 name = "NULL";
301 text = "";
302 break;
303 case LTC_ASN1_OBJECT_IDENTIFIER:
304 name = "OBJECT IDENTIFIER";
305 {
306 unsigned long i;
307 int r;
308 char* s = buf;
309 int sz = sizeof(buf);
310 for (i = 0; i < l->size; ++i) {
311 r = snprintf(s, sz, "%lu.", ((unsigned long*)l->data)[i]);
312 if (r < 0 || r >= sz) {
313 fprintf(stderr, "%s boom\n", name);
314 exit(EXIT_FAILURE);
315 }
316 s += r;
317 sz -= r;
318 }
319 /* replace the last . with a \0 */
320 *(s - 1) = '\0';
321 text = buf;
322 }
323 break;
324 case LTC_ASN1_IA5_STRING:
325 name = "IA5 STRING";
326 text = l->data;
327 break;
328 case LTC_ASN1_PRINTABLE_STRING:
329 name = "PRINTABLE STRING";
330 text = l->data;
331 break;
332 case LTC_ASN1_UTF8_STRING:
333 name = "UTF8 STRING";
334 break;
335 case LTC_ASN1_UTCTIME:
336 name = "UTCTIME";
337 {
338 ltc_utctime* ut = l->data;
339 snprintf(buf, sizeof(buf), "%02d-%02d-%02d %02d:%02d:%02d %c%02d:%02d",
340 ut->YY, ut->MM, ut->DD, ut->hh, ut->mm, ut->ss,
341 ut->off_dir ? '-' : '+', ut->off_hh, ut->off_mm);
342 text = buf;
343 }
344 break;
345 case LTC_ASN1_GENERALIZEDTIME:
346 name = "GENERALIZED TIME";
347 {
348 ltc_generalizedtime* gt = l->data;
349 if(gt->fs)
350 snprintf(buf, sizeof(buf), "%04d-%02d-%02d %02d:%02d:%02d.%02dZ",
351 gt->YYYY, gt->MM, gt->DD, gt->hh, gt->mm, gt->ss, gt->fs);
352 else
353 snprintf(buf, sizeof(buf), "%04d-%02d-%02d %02d:%02d:%02dZ",
354 gt->YYYY, gt->MM, gt->DD, gt->hh, gt->mm, gt->ss);
355 text = buf;
356 }
357 break;
358 case LTC_ASN1_CHOICE:
359 name = "CHOICE";
360 break;
361 case LTC_ASN1_SEQUENCE:
362 name = "SEQUENCE";
363 text = "";
364 break;
365 case LTC_ASN1_SET:
366 name = "SET";
367 text = "";
368 break;
369 case LTC_ASN1_SETOF:
370 name = "SETOF";
371 text = "";
372 break;
373 case LTC_ASN1_RAW_BIT_STRING:
374 name = "RAW BIT STRING";
375 break;
376 case LTC_ASN1_TELETEX_STRING:
377 name = "TELETEX STRING";
378 text = l->data;
379 break;
380 case LTC_ASN1_CONSTRUCTED:
381 if (l->used & 0x80)
382 name = "CONTEXT SPECIFIC";
383 else
384 name = "CONSTRUCTED";
385 snprintf(buf, sizeof(buf), "[%d]", l->used & 0x1f);
386 text = buf;
387 break;
388 case LTC_ASN1_CONTEXT_SPECIFIC:
389 name = "CONTEXT SPECIFIC";
390 {
391 int r;
392 char* s = buf;
393 int sz = sizeof(buf);
394 r = snprintf(s, sz, "[%d] ", l->used & 0x1f);
395 if (r < 0 || r >= sz) {
396 printf("Context Specific boom");
397 exit(EXIT_FAILURE);
398 }
399 s += r;
400 sz -= r;
401 for (n = 0; n < l->size; ++n) {
402 r = snprintf(s, sz, "%02X", ((unsigned char*)l->data)[n]);
403 if (r < 0 || r >= sz) {
404 printf("Context Specific boom");
405 exit(EXIT_FAILURE);
406 }
407 s += r;
408 sz -= r;
409 }
410 text = buf;
411 }
412 break;
413 }
414
415 for (n = 0; n < level; ++n) {
416 fprintf(stderr, " ");
417 }
418 if (name) {
419 if (text)
420 fprintf(stderr, "%s %s\n", name, text);
421 else
422 fprintf(stderr, "%s <missing decoding>\n", name);
423 }
424 else
425 fprintf(stderr, "WTF type=%i\n", l->type);
426
427 if (ostring) {
428 _der_tests_print_flexi(ostring, level + 1);
429 der_free_sequence_flexi(ostring);
430 }
431
432 if (l->child)
433 _der_tests_print_flexi(l->child, level + 1);
434
435 if (l->next)
436 _der_tests_print_flexi(l->next, level);
437 }
438 #endif
439
440 static void der_cacert_test(void)
441 {
442 unsigned char buf[sizeof(_der_tests_cacert_root_cert)];
443 unsigned long len1 = sizeof(buf), len2;
444
445 ltc_asn1_list *decoded_list, *l, *l1, *l2;
446
447 DO(base64_decode(_der_tests_stinky_root_cert, sizeof(_der_tests_stinky_root_cert), buf, &len1));
448 len2 = len1;
449 DO(der_decode_sequence_flexi(buf, &len2, &decoded_list));
450 der_free_sequence_flexi(decoded_list);
451
452 len1 = sizeof(buf);
453 DO(base64_decode(_der_tests_cacert_root_cert, sizeof(_der_tests_cacert_root_cert), buf, &len1));
454 len2 = len1;
455 DO(der_decode_sequence_flexi(buf, &len2, &decoded_list));
456 CHECK_ASN1_TYPE(decoded_list, LTC_ASN1_SEQUENCE);
457 CHECK_ASN1_HAS_DATA(decoded_list);
458
459 der_sequence_shrink(decoded_list);
460
461 CHECK_ASN1_TYPE(decoded_list, LTC_ASN1_SEQUENCE);
462 CHECK_ASN1_HAS_NO_DATA(decoded_list);
463
464 #ifdef LTC_DER_TESTS_PRINT_FLEXI
465 printf("\n\n--- test print start ---\n\n");
466 _der_tests_print_flexi(decoded_list, 0);
467 printf("\n\n--- test print end ---\n\n");
468 #endif
469
470 l = decoded_list;
471
472 /*
473 SEQUENCE(3 elem)
474 SEQUENCE(8 elem)
475 */
476
477 CHECK_ASN1_TYPE(l, LTC_ASN1_SEQUENCE);
478 CHECK_ASN1_HAS_CHILD(l);
479 CHECK_ASN1_HAS_NO_NEXT(l);
480
481 l = l->child;
482
483 CHECK_ASN1_TYPE(l, LTC_ASN1_SEQUENCE);
484 CHECK_ASN1_HAS_CHILD(l);
485 CHECK_ASN1_HAS_NEXT(l);
486
487 l1 = l->child;
488
489 /*
490 [0](1)
491 INTEGER 2
492 */
493
494 CHECK_ASN1_TYPE(l1, LTC_ASN1_CONSTRUCTED);
495 CHECK_ASN1_HAS_CHILD(l1);
496 CHECK_ASN1_HAS_NEXT(l1);
497
498 l2 = l1->child;
499
500 CHECK_ASN1_TYPE(l2, LTC_ASN1_INTEGER);
501 CHECK_ASN1_HAS_NO_CHILD(l2);
502 CHECK_ASN1_HAS_NO_NEXT(l2);
503
504 l1 = l1->next;
505
506 /*
507 INTEGER 0
508 */
509
510 CHECK_ASN1_TYPE(l1, LTC_ASN1_INTEGER);
511 CHECK_ASN1_HAS_NO_CHILD(l1);
512 CHECK_ASN1_HAS_NEXT(l1);
513
514 l1 = l1->next;
515
516 /*
517 SEQUENCE(2 elem)
518 OBJECT IDENTIFIER 1.2.840.113549.1.1.4
519 NULL
520 */
521
522 CHECK_ASN1_TYPE(l1, LTC_ASN1_SEQUENCE);
523 CHECK_ASN1_HAS_CHILD(l1);
524 CHECK_ASN1_HAS_NEXT(l1);
525
526 l2 = l1->child;
527
528 CHECK_ASN1_TYPE(l2, LTC_ASN1_OBJECT_IDENTIFIER);
529 CHECK_ASN1_HAS_NO_CHILD(l2);
530 CHECK_ASN1_HAS_NEXT(l2);
531
532 l2 = l2->next;
533
534 CHECK_ASN1_TYPE(l2, LTC_ASN1_NULL);
535 CHECK_ASN1_HAS_NO_CHILD(l2);
536 CHECK_ASN1_HAS_NO_NEXT(l2);
537
538 /*
539 SEQUENCE(4 elem)
540 SET(1 elem)
541 SEQUENCE(2 elem)
542 OBJECT IDENTIFIER 2.5.4.10
543 PrintableString Root CA
544 SET(1 elem)
545 SEQUENCE(2 elem)
546 OBJECT IDENTIFIER 2.5.4.11
547 PrintableString http://www.cacert.org
548 SET(1 elem)
549 SEQUENCE(2 elem)
550 OBJECT IDENTIFIER 2.5.4.3
551 PrintableString CA Cert Signing Authority
552 SET(1 elem)
553 SEQUENCE(2 elem)
554 OBJECT IDENTIFIER 1.2.840.113549.1.9.1
555 IA5String [email protected]
556 */
557
558 l = l->next;
559
560 /*
561 SEQUENCE(2 elem)
562 OBJECT IDENTIFIER 1.2.840.113549.1.1.4
563 NULL
564 */
565
566 CHECK_ASN1_TYPE(l, LTC_ASN1_SEQUENCE);
567 CHECK_ASN1_HAS_CHILD(l);
568 CHECK_ASN1_HAS_NEXT(l);
569
570 l1 = l->child;
571
572 CHECK_ASN1_TYPE(l1, LTC_ASN1_OBJECT_IDENTIFIER);
573 CHECK_ASN1_HAS_NO_CHILD(l1);
574 CHECK_ASN1_HAS_NEXT(l1);
575
576 l1 = l1->next;
577
578 CHECK_ASN1_TYPE(l1, LTC_ASN1_NULL);
579 CHECK_ASN1_HAS_NO_CHILD(l1);
580 CHECK_ASN1_HAS_NO_NEXT(l1);
581
582 l = l->next;
583
584 /*
585 BIT STRING(4096 bit)
586 */
587
588 CHECK_ASN1_TYPE(l, LTC_ASN1_BIT_STRING);
589 CHECK_ASN1_HAS_NO_CHILD(l);
590 CHECK_ASN1_HAS_NO_NEXT(l);
591
592 der_free_sequence_flexi(decoded_list);
593 }
594
595 static void der_set_test(void)
596 {
597 ltc_asn1_list list[10];
598 static const unsigned char oct_str[] = { 1, 2, 3, 4 };
599 static const unsigned char bin_str[] = { 1, 0, 0, 1 };
600 static const unsigned long int_val = 12345678UL;
601
602 unsigned char strs[10][10], outbuf[128];
603 unsigned long x, val, outlen;
604
605 /* make structure and encode it */
606 LTC_SET_ASN1(list, 0, LTC_ASN1_OCTET_STRING, oct_str, sizeof(oct_str));
607 LTC_SET_ASN1(list, 1, LTC_ASN1_BIT_STRING, bin_str, sizeof(bin_str));
608 LTC_SET_ASN1(list, 2, LTC_ASN1_SHORT_INTEGER, &int_val, 1);
609
610 /* encode it */
611 outlen = sizeof(outbuf);
612 DO(der_encode_set(list, 3, outbuf, &outlen));
613
614 /* 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 */
615 LTC_SET_ASN1(list, 0, LTC_ASN1_BIT_STRING, strs[1], sizeof(strs[1]));
616 LTC_SET_ASN1(list, 1, LTC_ASN1_SHORT_INTEGER, &val, 1);
617 LTC_SET_ASN1(list, 2, LTC_ASN1_OCTET_STRING, strs[0], sizeof(strs[0]));
618
619 DO(der_decode_set(outbuf, outlen, list, 3));
620
621 /* now compare the items */
622 if (memcmp(strs[0], oct_str, sizeof(oct_str))) {
623 fprintf(stderr, "error decoding set using der_decode_set (oct_str is wrong):\n");
624 exit(EXIT_FAILURE);
625 }
626
627 if (memcmp(strs[1], bin_str, sizeof(bin_str))) {
628 fprintf(stderr, "error decoding set using der_decode_set (bin_str is wrong):\n");
629 exit(EXIT_FAILURE);
630 }
631
632 if (val != int_val) {
633 fprintf(stderr, "error decoding set using der_decode_set (int_val is wrong):\n");
634 exit(EXIT_FAILURE);
635 }
636
637 strcpy((char*)strs[0], "one");
638 strcpy((char*)strs[1], "one2");
639 strcpy((char*)strs[2], "two");
640 strcpy((char*)strs[3], "aaa");
641 strcpy((char*)strs[4], "aaaa");
642 strcpy((char*)strs[5], "aab");
643 strcpy((char*)strs[6], "aaab");
644 strcpy((char*)strs[7], "bbb");
645 strcpy((char*)strs[8], "bbba");
646 strcpy((char*)strs[9], "bbbb");
647
648 for (x = 0; x < 10; x++) {
649 LTC_SET_ASN1(list, x, LTC_ASN1_PRINTABLE_STRING, strs[x], strlen((char*)strs[x]));
650 }
651
652 outlen = sizeof(outbuf);
653 DO(der_encode_setof(list, 10, outbuf, &outlen));
654
655 for (x = 0; x < 10; x++) {
656 LTC_SET_ASN1(list, x, LTC_ASN1_PRINTABLE_STRING, strs[x], sizeof(strs[x]) - 1);
657 }
658 XMEMSET(strs, 0, sizeof(strs));
659
660 DO(der_decode_set(outbuf, outlen, list, 10));
661
662 /* now compare */
663 for (x = 1; x < 10; x++) {
664 if (!(strlen((char*)strs[x-1]) <= strlen((char*)strs[x])) && strcmp((char*)strs[x-1], (char*)strs[x]) >= 0) {
665 fprintf(stderr, "error SET OF order at %lu is wrong\n", x);
666 exit(EXIT_FAILURE);
667 }
668 }
669
670 }
671
672
673 /* we are encoding
674
675 SEQUENCE {
676 PRINTABLE "printable"
677 IA5 "ia5"
678 SEQUENCE {
679 INTEGER 12345678
680 UTCTIME { 91, 5, 6, 16, 45, 40, 1, 7, 0 }
681 GENERALIZEDTIME { 2017, 03, 21, 10, 21, 12, 4, 1, 2, 0 }
682 SEQUENCE {
683 OCTET STRING { 1, 2, 3, 4 }
684 BIT STRING { 1, 0, 0, 1 }
685 SEQUENCE {
686 OID { 1, 2, 840, 113549 }
687 NULL
688 SET OF {
689 PRINTABLE "333" -- WILL GET SORTED
690 PRINTABLE "222"
691 }
692 }
693 }
694 }
695
696 */
697
698 static void der_flexi_test(void)
699 {
700 static const char printable_str[] = "printable";
701 static const char set1_str[] = "333";
702 static const char set2_str[] = "222";
703 static const char ia5_str[] = "ia5";
704 static const unsigned long int_val = 12345678UL;
705 static const ltc_utctime utctime = { 91, 5, 6, 16, 45, 40, 1, 7, 0 };
706 static const ltc_generalizedtime gtime = { 2017, 03, 21, 10, 21, 12, 421, 1, 2, 0 };
707 static const unsigned char oct_str[] = { 1, 2, 3, 4 };
708 static const unsigned char bit_str[] = { 1, 0, 0, 1 };
709 static const unsigned long oid_str[] = { 1, 2, 840, 113549 };
710
711 unsigned char encode_buf[192];
712 unsigned long encode_buf_len, decode_len;
713
714 ltc_asn1_list static_list[5][4], *decoded_list, *l;
715
716 /* build list */
717 LTC_SET_ASN1(static_list[0], 0, LTC_ASN1_PRINTABLE_STRING, (void *)printable_str, strlen(printable_str));
718 LTC_SET_ASN1(static_list[0], 1, LTC_ASN1_IA5_STRING, (void *)ia5_str, strlen(ia5_str));
719 LTC_SET_ASN1(static_list[0], 2, LTC_ASN1_SEQUENCE, static_list[1], 4);
720
721 LTC_SET_ASN1(static_list[1], 0, LTC_ASN1_SHORT_INTEGER, (void *)&int_val, 1);
722 LTC_SET_ASN1(static_list[1], 1, LTC_ASN1_UTCTIME, (void *)&utctime, 1);
723 LTC_SET_ASN1(static_list[1], 2, LTC_ASN1_GENERALIZEDTIME, (void *)&gtime, 1);
724 LTC_SET_ASN1(static_list[1], 3, LTC_ASN1_SEQUENCE, static_list[2], 3);
725
726 LTC_SET_ASN1(static_list[2], 0, LTC_ASN1_OCTET_STRING, (void *)oct_str, 4);
727 LTC_SET_ASN1(static_list[2], 1, LTC_ASN1_BIT_STRING, (void *)bit_str, 4);
728 LTC_SET_ASN1(static_list[2], 2, LTC_ASN1_SEQUENCE, static_list[3], 3);
729
730 LTC_SET_ASN1(static_list[3], 0, LTC_ASN1_OBJECT_IDENTIFIER,(void *)oid_str, 4);
731 LTC_SET_ASN1(static_list[3], 1, LTC_ASN1_NULL, NULL, 0);
732 LTC_SET_ASN1(static_list[3], 2, LTC_ASN1_SETOF, static_list[4], 2);
733
734 LTC_SET_ASN1(static_list[4], 0, LTC_ASN1_PRINTABLE_STRING, set1_str, strlen(set1_str));
735 LTC_SET_ASN1(static_list[4], 1, LTC_ASN1_PRINTABLE_STRING, set2_str, strlen(set2_str));
736
737 /* encode it */
738 encode_buf_len = sizeof(encode_buf);
739 DO(der_encode_sequence(&static_list[0][0], 3, encode_buf, &encode_buf_len));
740
741 #if 0
742 {
743 FILE *f;
744 f = fopen("t.bin", "wb");
745 fwrite(encode_buf, 1, encode_buf_len, f);
746 fclose(f);
747 }
748 #endif
749
750 /* decode with flexi */
751 decode_len = encode_buf_len;
752 DO(der_decode_sequence_flexi(encode_buf, &decode_len, &decoded_list));
753
754 if (decode_len != encode_buf_len) {
755 fprintf(stderr, "Decode len of %lu does not match encode len of %lu \n", decode_len, encode_buf_len);
756 exit(EXIT_FAILURE);
757 }
758
759 /* we expect l->next to be NULL and l->child to not be */
760 l = decoded_list;
761 if (l->next != NULL || l->child == NULL) {
762 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);
763 exit(EXIT_FAILURE);
764 }
765
766 /* we expect a SEQUENCE */
767 if (l->type != LTC_ASN1_SEQUENCE) {
768 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);
769 exit(EXIT_FAILURE);
770 }
771 l = l->child;
772
773 /* PRINTABLE STRING */
774 /* we expect printable_str */
775 if (l->next == NULL || l->child != NULL) {
776 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);
777 exit(EXIT_FAILURE);
778 }
779
780 if (l->type != LTC_ASN1_PRINTABLE_STRING) {
781 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);
782 exit(EXIT_FAILURE);
783 }
784
785 if (l->size != strlen(printable_str) || memcmp(printable_str, l->data, l->size)) {
786 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);
787 exit(EXIT_FAILURE);
788 }
789
790 /* move to next */
791 l = l->next;
792
793 /* IA5 STRING */
794 /* we expect ia5_str */
795 if (l->next == NULL || l->child != NULL) {
796 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);
797 exit(EXIT_FAILURE);
798 }
799
800 if (l->type != LTC_ASN1_IA5_STRING) {
801 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);
802 exit(EXIT_FAILURE);
803 }
804
805 if (l->size != strlen(ia5_str) || memcmp(ia5_str, l->data, l->size)) {
806 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);
807 exit(EXIT_FAILURE);
808 }
809
810 /* move to next */
811 l = l->next;
812
813 /* expect child anve move down */
814
815 if (l->next != NULL || l->child == NULL) {
816 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);
817 exit(EXIT_FAILURE);
818 }
819
820 if (l->type != LTC_ASN1_SEQUENCE) {
821 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);
822 exit(EXIT_FAILURE);
823 }
824 l = l->child;
825
826
827 /* INTEGER */
828
829 if (l->next == NULL || l->child != NULL) {
830 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);
831 exit(EXIT_FAILURE);
832 }
833
834 if (l->type != LTC_ASN1_INTEGER) {
835 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);
836 exit(EXIT_FAILURE);
837 }
838
839 if (mp_cmp_d(l->data, 12345678UL) != LTC_MP_EQ) {
840 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);
841 exit(EXIT_FAILURE);
842 }
843
844 /* move to next */
845 l = l->next;
846
847 /* UTCTIME */
848
849 if (l->next == NULL || l->child != NULL) {
850 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);
851 exit(EXIT_FAILURE);
852 }
853
854 if (l->type != LTC_ASN1_UTCTIME) {
855 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);
856 exit(EXIT_FAILURE);
857 }
858
859 if (memcmp(l->data, &utctime, sizeof(utctime))) {
860 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);
861 exit(EXIT_FAILURE);
862 }
863
864 /* move to next */
865 l = l->next;
866
867 /* GeneralizedTime */
868
869 if (l->next == NULL || l->child != NULL) {
870 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);
871 exit(EXIT_FAILURE);
872 }
873
874 if (l->type != LTC_ASN1_GENERALIZEDTIME) {
875 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);
876 exit(EXIT_FAILURE);
877 }
878
879 if (memcmp(l->data, &gtime, sizeof(gtime))) {
880 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);
881 exit(EXIT_FAILURE);
882 }
883
884 /* move to next */
885 l = l->next;
886
887 /* expect child anve move down */
888
889 if (l->next != NULL || l->child == NULL) {
890 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);
891 exit(EXIT_FAILURE);
892 }
893
894 if (l->type != LTC_ASN1_SEQUENCE) {
895 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);
896 exit(EXIT_FAILURE);
897 }
898 l = l->child;
899
900
901 /* OCTET STRING */
902 /* we expect oct_str */
903 if (l->next == NULL || l->child != NULL) {
904 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);
905 exit(EXIT_FAILURE);
906 }
907
908 if (l->type != LTC_ASN1_OCTET_STRING) {
909 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);
910 exit(EXIT_FAILURE);
911 }
912
913 if (l->size != sizeof(oct_str) || memcmp(oct_str, l->data, l->size)) {
914 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);
915 exit(EXIT_FAILURE);
916 }
917
918 /* move to next */
919 l = l->next;
920
921 /* BIT STRING */
922 /* we expect oct_str */
923 if (l->next == NULL || l->child != NULL) {
924 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);
925 exit(EXIT_FAILURE);
926 }
927
928 if (l->type != LTC_ASN1_BIT_STRING) {
929 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);
930 exit(EXIT_FAILURE);
931 }
932
933 if (l->size != sizeof(bit_str) || memcmp(bit_str, l->data, l->size)) {
934 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);
935 exit(EXIT_FAILURE);
936 }
937
938 /* move to next */
939 l = l->next;
940
941 /* expect child anve move down */
942
943 if (l->next != NULL || l->child == NULL) {
944 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);
945 exit(EXIT_FAILURE);
946 }
947
948 if (l->type != LTC_ASN1_SEQUENCE) {
949 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);
950 exit(EXIT_FAILURE);
951 }
952 l = l->child;
953
954
955 /* OID STRING */
956 /* we expect oid_str */
957 if (l->next == NULL || l->child != NULL) {
958 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);
959 exit(EXIT_FAILURE);
960 }
961
962 if (l->type != LTC_ASN1_OBJECT_IDENTIFIER) {
963 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);
964 exit(EXIT_FAILURE);
965 }
966
967 if (l->size != sizeof(oid_str)/sizeof(oid_str[0]) || memcmp(oid_str, l->data, l->size*sizeof(oid_str[0]))) {
968 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);
969 exit(EXIT_FAILURE);
970 }
971
972 /* move to next */
973 l = l->next;
974
975 /* NULL */
976 if (l->type != LTC_ASN1_NULL) {
977 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);
978 exit(EXIT_FAILURE);
979 }
980
981 /* move to next */
982 l = l->next;
983
984 /* expect child anve move down */
985 if (l->next != NULL || l->child == NULL) {
986 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);
987 exit(EXIT_FAILURE);
988 }
989
990 if (l->type != LTC_ASN1_SET) {
991 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);
992 exit(EXIT_FAILURE);
993 }
994 l = l->child;
995
996 /* PRINTABLE STRING */
997 /* we expect printable_str */
998 if (l->next == NULL || l->child != NULL) {
999 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);
1000 exit(EXIT_FAILURE);
1001 }
1002
1003 if (l->type != LTC_ASN1_PRINTABLE_STRING) {
1004 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);
1005 exit(EXIT_FAILURE);
1006 }
1007
1008 /* note we compare set2_str FIRST because the SET OF is sorted and "222" comes before "333" */
1009 if (l->size != strlen(set2_str) || memcmp(set2_str, l->data, l->size)) {
1010 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);
1011 exit(EXIT_FAILURE);
1012 }
1013
1014 /* move to next */
1015 l = l->next;
1016
1017 /* PRINTABLE STRING */
1018 /* we expect printable_str */
1019 if (l->type != LTC_ASN1_PRINTABLE_STRING) {
1020 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);
1021 exit(EXIT_FAILURE);
1022 }
1023
1024 if (l->size != strlen(set1_str) || memcmp(set1_str, l->data, l->size)) {
1025 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);
1026 exit(EXIT_FAILURE);
1027 }
1028
1029
1030 der_sequence_free(l);
1031
1032 }
1033
1034 static int der_choice_test(void)
1035 {
1036 ltc_asn1_list types[7], host[1];
1037 unsigned char bitbuf[10], octetbuf[10], ia5buf[10], printbuf[10], outbuf[256], x, y;
1038 unsigned long integer, oidbuf[10], outlen, inlen;
1039 void *mpinteger;
1040 ltc_utctime utctime = { 91, 5, 6, 16, 45, 40, 1, 7, 0 };
1041 ltc_generalizedtime gtime = { 2038, 01, 19, 3, 14, 8, 0, 0, 0, 0 };
1042
1043 /* setup variables */
1044 for (x = 0; x < sizeof(bitbuf); x++) { bitbuf[x] = x & 1; }
1045 for (x = 0; x < sizeof(octetbuf); x++) { octetbuf[x] = x; }
1046 for (x = 0; x < sizeof(ia5buf); x++) { ia5buf[x] = 'a'; }
1047 for (x = 0; x < sizeof(printbuf); x++) { printbuf[x] = 'a'; }
1048 integer = 1;
1049 for (x = 0; x < sizeof(oidbuf)/sizeof(oidbuf[0]); x++) { oidbuf[x] = x + 1; }
1050 DO(mp_init(&mpinteger));
1051
1052 for (x = 0; x < 14; x++) {
1053 /* setup list */
1054 LTC_SET_ASN1(types, 0, LTC_ASN1_PRINTABLE_STRING, printbuf, sizeof(printbuf));
1055 LTC_SET_ASN1(types, 1, LTC_ASN1_BIT_STRING, bitbuf, sizeof(bitbuf));
1056 LTC_SET_ASN1(types, 2, LTC_ASN1_OCTET_STRING, octetbuf, sizeof(octetbuf));
1057 LTC_SET_ASN1(types, 3, LTC_ASN1_IA5_STRING, ia5buf, sizeof(ia5buf));
1058 if (x > 7) {
1059 LTC_SET_ASN1(types, 4, LTC_ASN1_SHORT_INTEGER, &integer, 1);
1060 } else {
1061 LTC_SET_ASN1(types, 4, LTC_ASN1_INTEGER, mpinteger, 1);
1062 }
1063 LTC_SET_ASN1(types, 5, LTC_ASN1_OBJECT_IDENTIFIER, oidbuf, sizeof(oidbuf)/sizeof(oidbuf[0]));
1064 if (x > 7) {
1065 LTC_SET_ASN1(types, 6, LTC_ASN1_UTCTIME, &utctime, 1);
1066 } else {
1067 LTC_SET_ASN1(types, 6, LTC_ASN1_GENERALIZEDTIME, &gtime, 1);
1068 }
1069
1070 LTC_SET_ASN1(host, 0, LTC_ASN1_CHOICE, types, 7);
1071
1072
1073 /* encode */
1074 outlen = sizeof(outbuf);
1075 DO(der_encode_sequence(&types[x>6?x-7:x], 1, outbuf, &outlen));
1076
1077 /* decode it */
1078 inlen = outlen;
1079 DO(der_decode_sequence(outbuf, inlen, &host[0], 1));
1080
1081 for (y = 0; y < 7; y++) {
1082 if (types[y].used && y != (x>6?x-7:x)) {
1083 fprintf(stderr, "CHOICE, flag %u in trial %u was incorrectly set to one\n", y, x);
1084 return 1;
1085 }
1086 if (!types[y].used && y == (x>6?x-7:x)) {
1087 fprintf(stderr, "CHOICE, flag %u in trial %u was incorrectly set to zero\n", y, x);
1088 return 1;
1089 }
1090 }
1091 }
1092 mp_clear(mpinteger);
1093 return 0;
1094 }
1095
1096
1097 int der_test(void)
1098 {
1099 unsigned long x, y, z, zz, oid[2][32];
1100 unsigned char buf[3][2048];
1101 void *a, *b, *c, *d, *e, *f, *g;
1102
1103 static const unsigned char rsa_oid_der[] = { 0x06, 0x06, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d };
1104 static const unsigned long rsa_oid[] = { 1, 2, 840, 113549 };
1105
1106 static const unsigned char rsa_ia5[] = "[email protected]";
1107 static const unsigned char rsa_ia5_der[] = { 0x16, 0x0d, 0x74, 0x65, 0x73, 0x74, 0x31,
1108 0x40, 0x72, 0x73, 0x61, 0x2e, 0x63, 0x6f, 0x6d };
1109
1110 static const unsigned char rsa_printable[] = "Test User 1";
1111 static const unsigned char rsa_printable_der[] = { 0x13, 0x0b, 0x54, 0x65, 0x73, 0x74, 0x20, 0x55,
1112 0x73, 0x65, 0x72, 0x20, 0x31 };
1113
1114 static const ltc_utctime rsa_time1 = { 91, 5, 6, 16, 45, 40, 1, 7, 0 };
1115 static const ltc_utctime rsa_time2 = { 91, 5, 6, 23, 45, 40, 0, 0, 0 };
1116 ltc_utctime tmp_time;
1117
1118 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 };
1119 static const unsigned char rsa_time2_der[] = { 0x17, 0x0d, 0x39, 0x31, 0x30, 0x35, 0x30, 0x36, 0x32, 0x33, 0x34, 0x35, 0x34, 0x30, 0x5a };
1120
1121 static const wchar_t utf8_1[] = { 0x0041, 0x2262, 0x0391, 0x002E };
1122 static const unsigned char utf8_1_der[] = { 0x0C, 0x07, 0x41, 0xE2, 0x89, 0xA2, 0xCE, 0x91, 0x2E };
1123 static const wchar_t utf8_2[] = { 0xD55C, 0xAD6D, 0xC5B4 };
1124 static const unsigned char utf8_2_der[] = { 0x0C, 0x09, 0xED, 0x95, 0x9C, 0xEA, 0xB5, 0xAD, 0xEC, 0x96, 0xB4 };
1125
1126 unsigned char utf8_buf[32];
1127 wchar_t utf8_out[32];
1128
1129 der_cacert_test();
1130
1131 DO(mp_init_multi(&a, &b, &c, &d, &e, &f, &g, NULL));
1132 for (zz = 0; zz < 16; zz++) {
1133 #ifdef USE_TFM
1134 for (z = 0; z < 256; z++) {
1135 #else
1136 for (z = 0; z < 1024; z++) {
1137 #endif
1138 if (yarrow_read(buf[0], z, &yarrow_prng) != z) {
1139 fprintf(stderr, "Failed to read %lu bytes from yarrow\n", z);
1140 return 1;
1141 }
1142 DO(mp_read_unsigned_bin(a, buf[0], z));
1143 /* if (mp_iszero(a) == LTC_MP_NO) { a.sign = buf[0][0] & 1 ? LTC_MP_ZPOS : LTC_MP_NEG; } */
1144 x = sizeof(buf[0]);
1145 DO(der_encode_integer(a, buf[0], &x));
1146 DO(der_length_integer(a, &y));
1147 if (y != x) { fprintf(stderr, "DER INTEGER size mismatch\n"); return 1; }
1148 mp_set_int(b, 0);
1149 DO(der_decode_integer(buf[0], y, b));
1150 if (y != x || mp_cmp(a, b) != LTC_MP_EQ) {
1151 fprintf(stderr, "%lu: %lu vs %lu\n", z, x, y);
1152 mp_clear_multi(a, b, c, d, e, f, g, NULL);
1153 return 1;
1154 }
1155 }
1156 }
1157
1158 /* test short integer */
1159 for (zz = 0; zz < 256; zz++) {
1160 for (z = 1; z < 4; z++) {
1161 if (yarrow_read(buf[2], z, &yarrow_prng) != z) {
1162 fprintf(stderr, "Failed to read %lu bytes from yarrow\n", z);
1163 return 1;
1164 }
1165 /* encode with normal */
1166 DO(mp_read_unsigned_bin(a, buf[2], z));
1167
1168 x = sizeof(buf[0]);
1169 DO(der_encode_integer(a, buf[0], &x));
1170
1171 /* encode with short */
1172 y = sizeof(buf[1]);
1173 DO(der_encode_short_integer(mp_get_int(a), buf[1], &y));
1174 if (x != y || memcmp(buf[0], buf[1], x)) {
1175 fprintf(stderr, "DER INTEGER short encoding failed, %lu, %lu, 0x%lX\n", x, y, mp_get_int(a));
1176 for (zz = 0; zz < z; zz++) fprintf(stderr, "%02x ", buf[2][zz]);
1177 fprintf(stderr, "\n");
1178 for (z = 0; z < x; z++) fprintf(stderr, "%02x ", buf[0][z]);
1179 fprintf(stderr, "\n");
1180 for (z = 0; z < y; z++) fprintf(stderr, "%02x ", buf[1][z]);
1181 fprintf(stderr, "\n");
1182 mp_clear_multi(a, b, c, d, e, f, g, NULL);
1183 return 1;
1184 }
1185
1186 /* decode it */
1187 x = 0;
1188 DO(der_decode_short_integer(buf[1], y, &x));
1189 if (x != mp_get_int(a)) {
1190 fprintf(stderr, "DER INTEGER short decoding failed, %lu, %lu\n", x, mp_get_int(a));
1191 mp_clear_multi(a, b, c, d, e, f, g, NULL);
1192 return 1;
1193 }
1194 }
1195 }
1196 mp_clear_multi(a, b, c, d, e, f, g, NULL);
1197
1198
1199 /* Test bit string */
1200 for (zz = 1; zz < 1536; zz++) {
1201 yarrow_read(buf[0], zz, &yarrow_prng);
1202 for (z = 0; z < zz; z++) {
1203 buf[0][z] &= 0x01;
1204 }
1205 x = sizeof(buf[1]);
1206 DO(der_encode_bit_string(buf[0], zz, buf[1], &x));
1207 DO(der_length_bit_string(zz, &y));
1208 if (y != x) {
1209 fprintf(stderr, "\nDER BIT STRING length of encoded not match expected : %lu, %lu, %lu\n", z, x, y);
1210 return 1;
1211 }
1212
1213 y = sizeof(buf[2]);
1214 DO(der_decode_bit_string(buf[1], x, buf[2], &y));
1215 if (y != zz || memcmp(buf[0], buf[2], zz)) {
1216 fprintf(stderr, "%lu, %lu, %d\n", y, zz, memcmp(buf[0], buf[2], zz));
1217 return 1;
1218 }
1219 }
1220
1221 /* Test octet string */
1222 for (zz = 1; zz < 1536; zz++) {
1223 yarrow_read(buf[0], zz, &yarrow_prng);
1224 x = sizeof(buf[1]);
1225 DO(der_encode_octet_string(buf[0], zz, buf[1], &x));
1226 DO(der_length_octet_string(zz, &y));
1227 if (y != x) {
1228 fprintf(stderr, "\nDER OCTET STRING length of encoded not match expected : %lu, %lu, %lu\n", z, x, y);
1229 return 1;
1230 }
1231 y = sizeof(buf[2]);
1232 DO(der_decode_octet_string(buf[1], x, buf[2], &y));
1233 if (y != zz || memcmp(buf[0], buf[2], zz)) {
1234 fprintf(stderr, "%lu, %lu, %d\n", y, zz, memcmp(buf[0], buf[2], zz));
1235 return 1;
1236 }
1237 }
1238
1239 /* test OID */
1240 x = sizeof(buf[0]);
1241 DO(der_encode_object_identifier((unsigned long*)rsa_oid, sizeof(rsa_oid)/sizeof(rsa_oid[0]), buf[0], &x));
1242 if (x != sizeof(rsa_oid_der) || memcmp(rsa_oid_der, buf[0], x)) {
1243 fprintf(stderr, "rsa_oid_der encode failed to match, %lu, ", x);
1244 for (y = 0; y < x; y++) fprintf(stderr, "%02x ", buf[0][y]);
1245 fprintf(stderr, "\n");
1246 return 1;
1247 }
1248
1249 y = sizeof(oid[0])/sizeof(oid[0][0]);
1250 DO(der_decode_object_identifier(buf[0], x, oid[0], &y));
1251 if (y != sizeof(rsa_oid)/sizeof(rsa_oid[0]) || memcmp(rsa_oid, oid[0], sizeof(rsa_oid))) {
1252 fprintf(stderr, "rsa_oid_der decode failed to match, %lu, ", y);
1253 for (z = 0; z < y; z++) fprintf(stderr, "%lu ", oid[0][z]);
1254 fprintf(stderr, "\n");
1255 return 1;
1256 }
1257
1258 /* do random strings */
1259 for (zz = 0; zz < 5000; zz++) {
1260 /* pick a random number of words */
1261 yarrow_read(buf[0], 4, &yarrow_prng);
1262 LOAD32L(z, buf[0]);
1263 z = 2 + (z % ((sizeof(oid[0])/sizeof(oid[0][0])) - 2));
1264
1265 /* fill them in */
1266 oid[0][0] = buf[0][0] % 3;
1267 oid[0][1] = buf[0][1] % 40;
1268
1269 for (y = 2; y < z; y++) {
1270 yarrow_read(buf[0], 4, &yarrow_prng);
1271 LOAD32L(oid[0][y], buf[0]);
1272 }
1273
1274 /* encode it */
1275 x = sizeof(buf[0]);
1276 DO(der_encode_object_identifier(oid[0], z, buf[0], &x));
1277 DO(der_length_object_identifier(oid[0], z, &y));
1278 if (x != y) {
1279 fprintf(stderr, "Random OID %lu test failed, length mismatch: %lu, %lu\n", z, x, y);
1280 for (x = 0; x < z; x++) fprintf(stderr, "%lu\n", oid[0][x]);
1281 return 1;
1282 }
1283
1284 /* decode it */
1285 y = sizeof(oid[0])/sizeof(oid[0][0]);
1286 DO(der_decode_object_identifier(buf[0], x, oid[1], &y));
1287 if (y != z) {
1288 fprintf(stderr, "Random OID %lu test failed, decode length mismatch: %lu, %lu\n", z, x, y);
1289 return 1;
1290 }
1291 if (memcmp(oid[0], oid[1], sizeof(oid[0][0]) * z)) {
1292 fprintf(stderr, "Random OID %lu test failed, decoded values wrong\n", z);
1293 for (x = 0; x < z; x++) fprintf(stderr, "%lu\n", oid[0][x]);
1294 fprintf(stderr, "\n\n Got \n\n");
1295 for (x = 0; x < z; x++) fprintf(stderr, "%lu\n", oid[1][x]);
1296 return 1;
1297 }
1298 }
1299
1300 /* IA5 string */
1301 x = sizeof(buf[0]);
1302 DO(der_encode_ia5_string(rsa_ia5, strlen((char*)rsa_ia5), buf[0], &x));
1303 if (x != sizeof(rsa_ia5_der) || memcmp(buf[0], rsa_ia5_der, x)) {
1304 fprintf(stderr, "IA5 encode failed: %lu, %lu\n", x, (unsigned long)sizeof(rsa_ia5_der));
1305 return 1;
1306 }
1307 DO(der_length_ia5_string(rsa_ia5, strlen((char*)rsa_ia5), &y));
1308 if (y != x) {
1309 fprintf(stderr, "IA5 length failed to match: %lu, %lu\n", x, y);
1310 return 1;
1311 }
1312 y = sizeof(buf[1]);
1313 DO(der_decode_ia5_string(buf[0], x, buf[1], &y));
1314 if (y != strlen((char*)rsa_ia5) || memcmp(buf[1], rsa_ia5, strlen((char*)rsa_ia5))) {
1315 fprintf(stderr, "DER IA5 failed test vector\n");
1316 return 1;
1317 }
1318
1319 /* Printable string */
1320 x = sizeof(buf[0]);
1321 DO(der_encode_printable_string(rsa_printable, strlen((char*)rsa_printable), buf[0], &x));
1322 if (x != sizeof(rsa_printable_der) || memcmp(buf[0], rsa_printable_der, x)) {
1323 fprintf(stderr, "PRINTABLE encode failed: %lu, %lu\n", x, (unsigned long)sizeof(rsa_printable_der));
1324 return 1;
1325 }
1326 DO(der_length_printable_string(rsa_printable, strlen((char*)rsa_printable), &y));
1327 if (y != x) {
1328 fprintf(stderr, "printable length failed to match: %lu, %lu\n", x, y);
1329 return 1;
1330 }
1331 y = sizeof(buf[1]);
1332 DO(der_decode_printable_string(buf[0], x, buf[1], &y));
1333 if (y != strlen((char*)rsa_printable) || memcmp(buf[1], rsa_printable, strlen((char*)rsa_printable))) {
1334 fprintf(stderr, "DER printable failed test vector\n");
1335 return 1;
1336 }
1337
1338 /* Test UTC time */
1339 x = sizeof(buf[0]);
1340 DO(der_encode_utctime((ltc_utctime*)&rsa_time1, buf[0], &x));
1341 if (x != sizeof(rsa_time1_der) || memcmp(buf[0], rsa_time1_der, x)) {
1342 fprintf(stderr, "UTCTIME encode of rsa_time1 failed: %lu, %lu\n", x, (unsigned long)sizeof(rsa_time1_der));
1343 fprintf(stderr, "\n\n");
1344 for (y = 0; y < x; y++) fprintf(stderr, "%02x ", buf[0][y]);
1345 fprintf(stderr, "\n");
1346 return 1;
1347 }
1348 DO(der_length_utctime((ltc_utctime*)&rsa_time1, &y));
1349 if (y != x) {
1350 fprintf(stderr, "UTCTIME length failed to match for rsa_time1: %lu, %lu\n", x, y);
1351 return 1;
1352 }
1353 DO(der_decode_utctime(buf[0], &y, &tmp_time));
1354 if (y != x || memcmp(&rsa_time1, &tmp_time, sizeof(ltc_utctime))) {
1355 fprintf(stderr, "UTCTIME decode failed for rsa_time1: %lu %lu\n", x, y);
1356 fprintf(stderr, "\n\n%u %u %u %u %u %u %u %u %u\n\n",
1357 tmp_time.YY,
1358 tmp_time.MM,
1359 tmp_time.DD,
1360 tmp_time.hh,
1361 tmp_time.mm,
1362 tmp_time.ss,
1363 tmp_time.off_dir,
1364 tmp_time.off_mm,
1365 tmp_time.off_hh);
1366 return 1;
1367 }
1368
1369 x = sizeof(buf[0]);
1370 DO(der_encode_utctime((ltc_utctime*)&rsa_time2, buf[0], &x));
1371 if (x != sizeof(rsa_time2_der) || memcmp(buf[0], rsa_time2_der, x)) {
1372 fprintf(stderr, "UTCTIME encode of rsa_time2 failed: %lu, %lu\n", x, (unsigned long)sizeof(rsa_time1_der));
1373 fprintf(stderr, "\n\n");
1374 for (y = 0; y < x; y++) fprintf(stderr, "%02x ", buf[0][y]);
1375 fprintf(stderr, "\n");
1376 return 1;
1377 }
1378 DO(der_length_utctime((ltc_utctime*)&rsa_time2, &y));
1379 if (y != x) {
1380 fprintf(stderr, "UTCTIME length failed to match for rsa_time2: %lu, %lu\n", x, y);
1381 return 1;
1382 }
1383 DO(der_decode_utctime(buf[0], &y, &tmp_time));
1384 if (y != x || memcmp(&rsa_time2, &tmp_time, sizeof(ltc_utctime))) {
1385 fprintf(stderr, "UTCTIME decode failed for rsa_time2: %lu %lu\n", x, y);
1386 fprintf(stderr, "\n\n%u %u %u %u %u %u %u %u %u\n\n",
1387 tmp_time.YY,
1388 tmp_time.MM,
1389 tmp_time.DD,
1390 tmp_time.hh,
1391 tmp_time.mm,
1392 tmp_time.ss,
1393 tmp_time.off_dir,
1394 tmp_time.off_mm,
1395 tmp_time.off_hh);
1396
1397
1398 return 1;
1399 }
1400
1401 /* UTF 8 */
1402 /* encode it */
1403 x = sizeof(utf8_buf);
1404 DO(der_encode_utf8_string(utf8_1, sizeof(utf8_1) / sizeof(utf8_1[0]), utf8_buf, &x));
1405 DO(der_length_utf8_string(utf8_1, sizeof(utf8_1) / sizeof(utf8_1[0]), &y));
1406 if (x != sizeof(utf8_1_der) || memcmp(utf8_buf, utf8_1_der, x) || x != y) {
1407 fprintf(stderr, "DER UTF8_1 encoded to %lu bytes\n", x);
1408 for (y = 0; y < x; y++) fprintf(stderr, "%02x ", (unsigned)utf8_buf[y]);
1409 fprintf(stderr, "\n");
1410 return 1;
1411 }
1412 /* decode it */
1413 y = sizeof(utf8_out) / sizeof(utf8_out[0]);
1414 DO(der_decode_utf8_string(utf8_buf, x, utf8_out, &y));
1415 if (y != (sizeof(utf8_1) / sizeof(utf8_1[0])) || memcmp(utf8_1, utf8_out, y * sizeof(wchar_t))) {
1416 fprintf(stderr, "DER UTF8_1 decoded to %lu wchar_t\n", y);
1417 for (x = 0; x < y; x++) fprintf(stderr, "%04lx ", (unsigned long)utf8_out[x]);
1418 fprintf(stderr, "\n");
1419 return 1;
1420 }
1421
1422 /* encode it */
1423 x = sizeof(utf8_buf);
1424 DO(der_encode_utf8_string(utf8_2, sizeof(utf8_2) / sizeof(utf8_2[0]), utf8_buf, &x));
1425 if (x != sizeof(utf8_2_der) || memcmp(utf8_buf, utf8_2_der, x)) {
1426 fprintf(stderr, "DER UTF8_2 encoded to %lu bytes\n", x);
1427 for (y = 0; y < x; y++) fprintf(stderr, "%02x ", (unsigned)utf8_buf[y]);
1428 fprintf(stderr, "\n");
1429 return 1;
1430 }
1431 /* decode it */
1432 y = sizeof(utf8_out) / sizeof(utf8_out[0]);
1433 DO(der_decode_utf8_string(utf8_buf, x, utf8_out, &y));
1434 if (y != (sizeof(utf8_2) / sizeof(utf8_2[0])) || memcmp(utf8_2, utf8_out, y * sizeof(wchar_t))) {
1435 fprintf(stderr, "DER UTF8_2 decoded to %lu wchar_t\n", y);
1436 for (x = 0; x < y; x++) fprintf(stderr, "%04lx ", (unsigned long)utf8_out[x]);
1437 fprintf(stderr, "\n");
1438 return 1;
1439 }
1440
1441
1442 der_set_test();
1443 der_flexi_test();
1444 return der_choice_test();
1445 }
1446
1447 #endif
1448
1449 /* ref: $Format:%D$ */
1450 /* git commit: $Format:%H$ */
1451 /* commit time: $Format:%ai$ */