unit-tests: added some more ASN.1 length tests
[strongswan.git] / src / libstrongswan / tests / suites / test_asn1.c
1 /*
2 * Copyright (C) 2013 Andreas Steffen
3 * HSR Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16
17 #include "test_suite.h"
18
19 #include <asn1/asn1.h>
20 #include <asn1/oid.h>
21 #include <utils/chunk.h>
22
23 /*******************************************************************************
24 * algorithm_identifier
25 */
26
27 START_TEST(test_asn1_algorithmIdentifier)
28 {
29 typedef struct {
30 int n;
31 chunk_t algid;
32 } testdata_t;
33
34 testdata_t test[] = {
35 { OID_ECDSA_WITH_SHA1, chunk_from_chars(0x30, 0x09, 0x06, 0x07,
36 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x01) },
37 { OID_SHA1_WITH_RSA, chunk_from_chars(0x30, 0x0d, 0x06, 0x09,
38 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00) },
39 };
40
41 chunk_t algid;
42 int i;
43
44 for (i = 0; i < countof(test); i++)
45 {
46 algid = asn1_algorithmIdentifier(test[i].n);
47 ck_assert(chunk_equals(algid, test[i].algid));
48 free(algid.ptr);
49 }
50 }
51 END_TEST
52
53 /*******************************************************************************
54 * parse_algorithm_identifier
55 */
56
57 START_TEST(test_asn1_parse_algorithmIdentifier)
58 {
59 typedef struct {
60 int alg;
61 bool empty;
62 chunk_t parameters;
63 } testdata_t;
64
65 testdata_t test[] = {
66 { OID_ECDSA_WITH_SHA1, TRUE, chunk_empty },
67 { OID_SHA1_WITH_RSA, TRUE, chunk_from_chars(0x05, 0x00) },
68 { OID_3DES_EDE_CBC, FALSE, chunk_from_chars(0x04, 0x01, 0xaa) },
69 { OID_PBKDF2, FALSE, chunk_from_chars(0x30, 0x01, 0xaa) }
70 };
71
72 chunk_t algid, parameters;
73 int i, alg;
74
75 for (i = 0; i < countof(test); i++)
76 {
77 algid = asn1_wrap(ASN1_SEQUENCE, "mc",
78 asn1_build_known_oid(test[i].alg), test[i].parameters);
79 parameters = chunk_empty;
80 if (i == 2)
81 {
82 alg = asn1_parse_algorithmIdentifier(algid, 0, NULL);
83 }
84 else
85 {
86 alg = asn1_parse_algorithmIdentifier(algid, 0, &parameters);
87 if (test[i].empty)
88 {
89 ck_assert(parameters.len == 0 && parameters.ptr == NULL);
90 }
91 else
92 {
93 ck_assert(chunk_equals(parameters, test[i].parameters));
94 }
95 }
96 ck_assert(alg == test[i].alg);
97 chunk_free(&algid);
98 }
99 }
100 END_TEST
101
102 /*******************************************************************************
103 * known_oid
104 */
105
106 START_TEST(test_asn1_known_oid)
107 {
108 typedef struct {
109 int n;
110 chunk_t oid;
111 } testdata_t;
112
113 testdata_t test[] = {
114 { OID_UNKNOWN, chunk_empty },
115 { OID_UNKNOWN, chunk_from_chars(0x55, 0x04, 0x02) },
116 { OID_COUNTRY, chunk_from_chars(0x55, 0x04, 0x06) },
117 { OID_STRONGSWAN, chunk_from_chars(0x2b, 0x06, 0x01, 0x04, 0x01,
118 0x82, 0xa0, 0x2a, 0x01) }
119 };
120
121 int i;
122
123 for (i = 0; i < countof(test); i++)
124 {
125 ck_assert(asn1_known_oid(test[i].oid) == test[i].n);
126 }
127 }
128 END_TEST
129
130 /*******************************************************************************
131 * build_known_oid
132 */
133
134 START_TEST(test_asn1_build_known_oid)
135 {
136 typedef struct {
137 int n;
138 chunk_t oid;
139 } testdata_t;
140
141 testdata_t test[] = {
142 { OID_UNKNOWN, chunk_empty },
143 { OID_MAX, chunk_empty },
144 { OID_COUNTRY, chunk_from_chars(0x06, 0x03, 0x55, 0x04, 0x06) },
145 { OID_STRONGSWAN, chunk_from_chars(0x06, 0x09, 0x2b, 0x06, 0x01, 0x04,
146 0x01, 0x82, 0xa0, 0x2a, 0x01) }
147 };
148
149 int i;
150 chunk_t oid = chunk_empty;
151
152 for (i = 0; i < countof(test); i++)
153 {
154 oid = asn1_build_known_oid(test[i].n);
155 if (test[i].oid.len == 0)
156 {
157 ck_assert(oid.len == 0 && oid.ptr == NULL);
158 }
159 else
160 {
161 ck_assert(chunk_equals(oid, test[i].oid));
162 chunk_free(&oid);
163 }
164 }
165 }
166 END_TEST
167
168 /*******************************************************************************
169 * oid_from_string
170 */
171
172 START_TEST(test_asn1_oid_from_string)
173 {
174 typedef struct {
175 char *string;
176 chunk_t oid;
177 } testdata_t;
178
179 testdata_t test[] = {
180 { "", chunk_empty },
181 { " ", chunk_empty },
182 { "0.2.262.1", chunk_from_chars(
183 0x02, 0x82, 0x06, 0x01) },
184 { "1.2.840.10045.4.1", chunk_from_chars(
185 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x01) },
186 { "1.3.6.1.4.1.36906.1", chunk_from_chars(
187 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0xa0, 0x2a, 0x01) },
188 { "2.16.840.1.101.3.4.2.1", chunk_from_chars(
189 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01) },
190 { "0.10.100.1000.10000.100000.1000000.10000000.100000000.268435455",
191 chunk_from_chars(0x0a,0x64, 0x87, 0x68, 0xce, 0x10, 0x86, 0x8d,
192 0x20, 0xbd, 0x84, 0x40, 0x84, 0xe2, 0xad, 0x00,
193 0xaf, 0xd7, 0xc2, 0x00, 0xff, 0xff, 0xff, 0x7f) },
194 { "0.1.2.3.4.5.6.7.8.9.10.128.129.130.131.132.133.134.135.136.137."
195 "256.257.258.259.260.261.262.263.264.265.384.385.386.387.388."
196 "2097153", chunk_from_chars(
197 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
198 0x81, 0x00, 0x81, 0x01, 0x81, 0x02, 0x81, 0x03, 0x81, 0x04,
199 0x81, 0x05, 0x81, 0x06, 0x81, 0x07, 0x81, 0x08, 0x81, 0x09,
200 0x82, 0x00, 0x82, 0x01, 0x82, 0x02, 0x82, 0x03, 0x82, 0x04,
201 0x82, 0x05, 0x82, 0x06, 0x82, 0x07, 0x82, 0x08, 0x82, 0x09,
202 0x83, 0x00, 0x83, 0x01, 0x83, 0x02, 0x83, 0x03, 0x83, 0x04,
203 0x81, 0x80, 0x80, 0x01) },
204 { "0.1.2.3.4.5.6.7.8.9.10.128.129.130.131.132.133.134.135.136.137."
205 "256.257.258.259.260.261.262.263.264.265.384.385.386.387.388."
206 "1.2097153", chunk_empty },
207 { "1.a.2.b.3", chunk_empty }
208 };
209
210 int i;
211 chunk_t oid = chunk_empty;
212
213 for (i = 0; i < countof(test); i++)
214 {
215 oid = asn1_oid_from_string(test[i].string);
216 if (test[i].oid.len == 0)
217 {
218 ck_assert(oid.len == 0 && oid.ptr == NULL);
219 }
220 else
221 {
222 ck_assert(chunk_equals(oid, test[i].oid));
223 chunk_free(&oid);
224 }
225 }
226 }
227 END_TEST
228
229 /*******************************************************************************
230 * oid_to_string
231 */
232
233 START_TEST(test_asn1_oid_to_string)
234 {
235 typedef struct {
236 char *string;
237 chunk_t oid;
238 } testdata_t;
239
240 testdata_t test[] = {
241 { NULL, chunk_empty },
242 { "0.2.262.1", chunk_from_chars(
243 0x02, 0x82, 0x06, 0x01) },
244 { "1.2.840.10045.4.1", chunk_from_chars(
245 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x01) },
246 { "1.3.6.1.4.1.36906.1", chunk_from_chars(
247 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0xa0, 0x2a, 0x01) },
248 { "2.16.840.1.101.3.4.2.1", chunk_from_chars(
249 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01) },
250 { "0.10.100.1000.10000.100000.1000000.10000000.100000000.268435455",
251 chunk_from_chars( 0x0a, 0x64, 0x87, 0x68, 0xce, 0x10, 0x86, 0x8d,
252 0x20, 0xbd, 0x84, 0x40, 0x84, 0xe2, 0xad, 0x00,
253 0xaf, 0xd7, 0xc2, 0x00, 0xff, 0xff, 0xff, 0x7f) },
254 { NULL, chunk_from_chars(
255 0x0a, 0x02, 0x64, 0x87, 0x68, 0xce, 0x10, 0x86, 0x8d, 0x20,
256 0xbd, 0x84, 0x40, 0x84, 0xe2, 0xad, 0x00, 0xaf, 0xd7, 0xc2, 0x00,
257 0xff, 0xff, 0xff, 0x7f) },
258 { NULL, chunk_from_chars(0x0a, 0x87) }
259 };
260
261 int i;
262 char *string = NULL;
263
264 for (i = 0; i < countof(test); i++)
265 {
266 string = asn1_oid_to_string(test[i].oid);
267 if (test[i].string == NULL)
268 {
269 ck_assert(string == NULL);
270 }
271 else
272 {
273 ck_assert(streq(string, test[i].string));
274 free(string);
275 }
276 }
277 }
278 END_TEST
279
280 /*******************************************************************************
281 * length
282 */
283
284 START_TEST(test_asn1_length)
285 {
286 chunk_t a;
287
288 a = chunk_empty;
289 ck_assert(asn1_length(&a) == ASN1_INVALID_LENGTH);
290
291 a = chunk_from_chars(0x04);
292 ck_assert(asn1_length(&a) == ASN1_INVALID_LENGTH);
293
294 a = chunk_from_chars(0x04, 0x00);
295 ck_assert(asn1_length(&a) == 0);
296
297 a = chunk_from_chars(0x04, 0x01);
298 ck_assert(asn1_length(&a) == ASN1_INVALID_LENGTH);
299
300 a = chunk_from_chars(0x04, 0x01, 0xaa);
301 ck_assert(asn1_length(&a) == 1);
302
303 a = chunk_from_chars(0x04, 0x7f, 0xaa);
304 a.len = 2 + 127;
305 ck_assert(asn1_length(&a) == 127);
306
307 a = chunk_from_chars(0x04, 0x80, 0xaa);
308 a.len = 2 + 128;
309 ck_assert(asn1_length(&a) == ASN1_INVALID_LENGTH);
310
311 a = chunk_from_chars(0x04, 0x81);
312 ck_assert(asn1_length(&a) == ASN1_INVALID_LENGTH);
313
314 a = chunk_from_chars(0x04, 0x81, 0x00);
315 ck_assert(asn1_length(&a) == 0);
316
317 a = chunk_from_chars(0x04, 0x81, 0x80, 0xaa);
318 ck_assert(asn1_length(&a) == ASN1_INVALID_LENGTH);
319
320 a = chunk_from_chars(0x04, 0x81, 0x80, 0xaa);
321 a.len = 3 + 128;
322 ck_assert(asn1_length(&a) == 128);
323
324 a = chunk_from_chars(0x04, 0x82, 0x01, 0x02, 0xaa);
325 a.len = 4 + 258;
326 ck_assert(asn1_length(&a) == 258);
327
328 a = chunk_from_chars(0x04, 0x83, 0x01, 0x02, 0x03, 0xaa);
329 a.len = 5 + 66051;
330 ck_assert(asn1_length(&a) == 66051);
331
332 a = chunk_from_chars(0x04, 0x84, 0x01, 0x02, 0x03, 0x04, 0xaa);
333 a.len = 6 + 16909060;
334 ck_assert(asn1_length(&a) == 16909060);
335
336 /* largest chunk on 32 bit system */
337 a = chunk_from_chars(0x04, 0x84, 0xff, 0xff, 0xff, 0xf9, 0xaa);
338 a.len = 4294967295;
339 ck_assert(asn1_length(&a) == 4294967289);
340
341 }
342 END_TEST
343
344 /*******************************************************************************
345 * unwrap
346 */
347
348 START_TEST(test_asn1_unwrap)
349 {
350 chunk_t c0 = chunk_from_chars(0x30);
351 chunk_t c1 = chunk_from_chars(0x30, 0x01, 0xaa);
352 chunk_t c2 = chunk_from_chars(0x30, 0x80);
353 chunk_t c3 = chunk_from_chars(0x30, 0x81);
354 chunk_t c4 = chunk_from_chars(0x30, 0x81, 0x01, 0xaa);
355 chunk_t c5 = chunk_from_chars(0x30, 0x81, 0x02, 0xaa);
356
357 chunk_t inner;
358 chunk_t inner_ref = chunk_from_chars(0xaa);
359
360 ck_assert(asn1_unwrap(&c0, &inner) == ASN1_INVALID);
361
362 ck_assert(asn1_unwrap(&c1, &inner) == ASN1_SEQUENCE);
363
364 ck_assert(chunk_equals(inner, inner_ref));
365
366 ck_assert(asn1_unwrap(&c2, &inner) == ASN1_INVALID);
367
368 ck_assert(asn1_unwrap(&c3, &inner) == ASN1_INVALID);
369
370 ck_assert(asn1_unwrap(&c4, &inner) == ASN1_SEQUENCE);
371
372 ck_assert(chunk_equals(inner, inner_ref));
373
374 ck_assert(asn1_unwrap(&c5, &inner) == ASN1_INVALID);
375 }
376 END_TEST
377
378 /*******************************************************************************
379 * is_asn1
380 */
381
382 START_TEST(test_is_asn1)
383 {
384 typedef struct {
385 bool asn1;
386 chunk_t chunk;
387 } testdata_t;
388
389 u_char buf[8];
390 chunk_t chunk_zero = { buf, 0 };
391 chunk_t chunk_mean = { 0, 1 };
392
393 testdata_t test[] = {
394 { FALSE, chunk_zero },
395 { FALSE, chunk_empty },
396 { FALSE, chunk_mean },
397 { TRUE, chunk_from_chars(0x30, 0x00) },
398 { TRUE, chunk_from_chars(0x31, 0x00) },
399 { TRUE, chunk_from_chars(0x04, 0x00) },
400 { FALSE, chunk_from_chars(0x02, 0x00) },
401 { FALSE, chunk_from_chars(0x30, 0x01) },
402 { FALSE, chunk_from_chars(0x30, 0x80) },
403 { TRUE, chunk_from_chars(0x30, 0x01, 0xa1) },
404 { FALSE, chunk_from_chars(0x30, 0x01, 0xa1, 0xa2) },
405 { TRUE, chunk_from_chars(0x30, 0x01, 0xa1, 0x0a) },
406 { FALSE, chunk_from_chars(0x30, 0x01, 0xa1, 0xa2, 0x0a) },
407 };
408
409 int i;
410
411 for (i = 0; i < countof(test); i++)
412 {
413 ck_assert(is_asn1(test[i].chunk) == test[i].asn1);
414 }
415 }
416 END_TEST
417
418 /*******************************************************************************
419 * is_printablestring
420 */
421
422 START_TEST(test_asn1_is_printablestring)
423 {
424 typedef struct {
425 bool printable;
426 char *string;
427 } testdata_t;
428
429
430 testdata_t test[] = {
431 { TRUE, "" },
432 { TRUE, "Z" },
433 { FALSE, "Z#" },
434 { FALSE, "&Z" },
435 { FALSE, "Z@z" },
436 { FALSE, "!" }, { FALSE, "*" }, { FALSE, "$" }, { FALSE, "%" },
437 { FALSE, "[" }, { FALSE, "]" }, { FALSE, "{" }, { FALSE, "}" },
438 { FALSE, "|" }, { FALSE, "~" }, { FALSE, "^" }, { FALSE, "_" },
439 { FALSE, "\"" }, { FALSE, "\\" }, { FALSE, "ä" }, { FALSE, "à" },
440 { TRUE, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
441 "0123456789 '()+,-./:=?" },
442 };
443
444 chunk_t chunk;
445 int i;
446
447 ck_assert(asn1_is_printablestring(chunk_empty));
448
449 for (i = 0; i < countof(test); i++)
450 {
451 chunk = chunk_from_str(test[i].string);
452 ck_assert(asn1_is_printablestring(chunk) == test[i].printable);
453 }
454 }
455 END_TEST
456
457 /*******************************************************************************
458 * to_time
459 */
460
461 START_TEST(test_asn1_to_time)
462 {
463 typedef struct {
464 time_t time;
465 u_int8_t type;
466 char *string;
467 } testdata_t;
468
469 testdata_t test[] = {
470 { 352980, 0x18, "197001050203Z" },
471 { 352984, 0x18, "19700105020304Z" },
472 { 352980, 0x17, "7001050203Z" },
473 { 347580, 0x17, "7001050203+0130" },
474 { 358380, 0x17, "7001050203-0130" },
475 { 352984, 0x17, "700105020304Z" },
476 { 347584, 0x17, "700105020304+0130" },
477 { 358384, 0x17, "700105020304-0130" },
478 { 0, 0x17, "700105020304+01" },
479 { 0, 0x17, "700105020304-01" },
480 { 0, 0x17, "700105020304" },
481 { 0, 0x17, "70010502Z" },
482 { 0, 0x17, "7001050203xxZ" },
483 { 0, 0x17, "7000050203Z" },
484 { 0, 0x17, "7013050203Z" },
485 { 5097600, 0x17, "7003010000Z" },
486 { 68256000, 0x17, "7203010000Z" },
487 { 951868800, 0x17, "0003010000Z" },
488 { 4107542400UL, 0x18, "210003010000Z" }
489 };
490
491 int i;
492 chunk_t chunk;
493
494 for (i = 0; i < countof(test); i++)
495 {
496 if (sizeof(time_t) == 4 && test[i].time < 0)
497 {
498 continue;
499 }
500 chunk = chunk_from_str(test[i].string);
501 ck_assert(asn1_to_time(&chunk, test[i].type) == test[i].time);
502 }
503 }
504 END_TEST
505
506 /*******************************************************************************
507 * from_time
508 */
509
510 START_TEST(test_asn1_from_time)
511 {
512 typedef struct {
513 time_t time;
514 u_int8_t type;
515 chunk_t chunk;
516 } testdata_t;
517
518 testdata_t test[] = {
519 { 352984, 0x18, chunk_from_chars(
520 0x18, 0x0f, 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x35,
521 0x30, 0x32, 0x30, 0x33, 0x30, 0x34, 0x5a) },
522 { 352984, 0x17, chunk_from_chars(
523 0x17, 0x0d, 0x37, 0x30, 0x30, 0x31, 0x30, 0x35,
524 0x30, 0x32, 0x30, 0x33, 0x30, 0x34, 0x5a) },
525 { 1078099200, 0x17, chunk_from_chars(
526 0x17, 0x0d, 0x30, 0x34, 0x30, 0x33, 0x30, 0x31,
527 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x5a) },
528 { 4107542400UL, 0x18, chunk_from_chars(
529 0x18, 0x0f, 0x32, 0x31, 0x30, 0x30, 0x30, 0x33, 0x30, 0x31,
530 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x5a) }
531 };
532
533 int i;
534 chunk_t chunk;
535
536 for (i = 0; i < countof(test); i++)
537 {
538 if (sizeof(time_t) == 4 && test[i].time < 0)
539 {
540 continue;
541 }
542 chunk = asn1_from_time(&test[i].time, test[i].type);
543 ck_assert(chunk_equals(chunk, test[i].chunk));
544 free(chunk.ptr);
545 }
546 }
547 END_TEST
548
549 /*******************************************************************************
550 * parse_time
551 */
552
553 START_TEST(test_asn1_parse_time)
554 {
555 typedef struct {
556 time_t time;
557 chunk_t chunk;
558 } testdata_t;
559
560 testdata_t test[] = {
561 { 352984, chunk_from_chars(
562 0x18, 0x0f, 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x35,
563 0x30, 0x32, 0x30, 0x33, 0x30, 0x34, 0x5a) },
564 { 352984, chunk_from_chars(
565 0x17, 0x0d, 0x37, 0x30, 0x30, 0x31, 0x30, 0x35,
566 0x30, 0x32, 0x30, 0x33, 0x30, 0x34, 0x5a) },
567 { 0, chunk_from_chars(0x05, 0x00) }
568 };
569
570 int i;
571
572 for (i = 0; i < countof(test); i++)
573 {
574 ck_assert(asn1_parse_time(test[i].chunk, 0) == test[i].time);
575 }
576 }
577 END_TEST
578
579 /*******************************************************************************
580 * build_object
581 */
582
583 START_TEST(test_asn1_build_object)
584 {
585 typedef struct {
586 size_t len;
587 size_t size;
588 u_char *b;
589 } testdata_t;
590
591 u_char b0[] = { 0x05, 0x00 };
592 u_char b1[] = { 0x04, 0x7f };
593 u_char b2[] = { 0x04, 0x81, 0x80 };
594 u_char b3[] = { 0x04, 0x81, 0xff };
595 u_char b4[] = { 0x04, 0x82, 0x01, 0x00 };
596 u_char b5[] = { 0x04, 0x82, 0xff, 0xff };
597 u_char b6[] = { 0x04, 0x83, 0x01, 0x00, 0x00 };
598
599 testdata_t test[] = {
600 { 0, sizeof(b0), b0 },
601 { 127, sizeof(b1), b1 },
602 { 128, sizeof(b2), b2 },
603 { 255, sizeof(b3), b3 },
604 { 256, sizeof(b4), b4 },
605 { 65535, sizeof(b5), b5 },
606 { 65536, sizeof(b6), b6 }
607 };
608
609 chunk_t a = chunk_empty;
610 u_char *pos;
611 int i;
612
613 for (i = 0; i < countof(test); i++)
614 {
615 pos = asn1_build_object(&a, test[i].b[0], test[i].len);
616 ck_assert(pos == (a.ptr + test[i].size));
617 ck_assert(a.len == test[i].size + test[i].len);
618 ck_assert(memeq(a.ptr, test[i].b, test[i].size));
619 chunk_free(&a);
620 }
621 }
622 END_TEST
623
624 /*******************************************************************************
625 * simple_object
626 */
627
628 START_TEST(test_asn1_simple_object)
629 {
630 chunk_t a = chunk_empty;
631 chunk_t b = chunk_from_chars(0x04, 0x05, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5);
632 chunk_t c = chunk_from_chars(0xa1, 0xa2, 0xa3, 0xa4, 0xa5);
633
634 a = asn1_simple_object(0x04, c);
635 ck_assert(chunk_equals(a, b));
636 chunk_free(&a);
637 }
638 END_TEST
639
640 /*******************************************************************************
641 * parse_simple_object
642 */
643
644 START_TEST(test_asn1_parse_simple_object)
645 {
646 typedef struct {
647 bool res;
648 int type;
649 chunk_t chunk;
650 } testdata_t;
651
652 testdata_t test[] = {
653 { FALSE, 0x04, chunk_from_chars(0x04) },
654 { FALSE, 0x04, chunk_from_chars(0x02, 0x01, 0x55) },
655 { FALSE, 0x04, chunk_from_chars(0x04, 0x01) },
656 { TRUE, 0x04, chunk_from_chars(0x04, 0x01, 0x55) },
657 { TRUE, 0x06, chunk_from_chars(0x06, 0x02, 0x55, 0x03) },
658 { TRUE, 0x06, chunk_from_chars(0x06, 0x00) },
659 { TRUE, 0x13, chunk_from_chars(0x13, 0x01, 0x55), }
660 };
661
662 int i;
663 bool res;
664
665 for (i = 0; i < countof(test); i++)
666 {
667 res = asn1_parse_simple_object(&test[i].chunk, test[i].type, 0, "test");
668 ck_assert(res == test[i].res);
669 if (res && test[i].chunk.len)
670 {
671 ck_assert(*test[i].chunk.ptr == 0x55);
672 }
673 }
674 }
675 END_TEST
676
677 /*******************************************************************************
678 * bitstring
679 */
680
681 START_TEST(test_asn1_bitstring)
682 {
683 chunk_t a = chunk_empty;
684 chunk_t b = chunk_from_chars(0x03, 0x05, 0x00, 0xa1, 0xa2, 0xa3, 0xa4);
685 chunk_t c = chunk_from_chars(0xa1, 0xa2, 0xa3, 0xa4);
686 chunk_t d = chunk_clone(c);
687
688 a = asn1_bitstring("c", c);
689 ck_assert(chunk_equals(a, b));
690 chunk_free(&a);
691
692 a = asn1_bitstring("m", d);
693 ck_assert(chunk_equals(a, b));
694 chunk_free(&a);
695 }
696 END_TEST
697
698 /*******************************************************************************
699 * integer
700 */
701
702 START_TEST(test_asn1_integer)
703 {
704 typedef struct {
705 chunk_t b;
706 chunk_t c;
707 } testdata_t;
708
709 chunk_t b0 = chunk_from_chars(0x02, 0x01, 0x00);
710 chunk_t b1 = chunk_from_chars(0x02, 0x01, 0x7f);
711 chunk_t b2 = chunk_from_chars(0x02, 0x02, 0x00, 0x80);
712
713 chunk_t c0 = chunk_empty;
714 chunk_t c1 = chunk_from_chars(0x7f);
715 chunk_t c2 = chunk_from_chars(0x80);
716 chunk_t c3 = chunk_from_chars(0x00, 0x80);
717
718 testdata_t test[] = {
719 { b0, c0 },
720 { b1, c1 },
721 { b2, c2 },
722 { b2, c3 }
723 };
724
725 chunk_t a = chunk_empty;
726 int i;
727
728 for (i = 0; i < countof(test); i++)
729 {
730 a = asn1_integer("c", test[i].c);
731 ck_assert(chunk_equals(a, test[i].b));
732 chunk_free(&a);
733
734 a = asn1_integer("m", chunk_clone(test[i].c));
735 ck_assert(chunk_equals(a, test[i].b));
736 chunk_free(&a);
737 }
738 }
739 END_TEST
740
741 /*******************************************************************************
742 * parse_integer_uint64
743 */
744
745 START_TEST(test_asn1_parse_integer_uint64)
746 {
747 typedef struct {
748 u_int64_t n;
749 chunk_t chunk;
750 } testdata_t;
751
752
753 testdata_t test[] = {
754 { 67305985ULL, chunk_from_chars(
755 0x04, 0x03, 0x02, 0x01) },
756 { 578437695752307201ULL, chunk_from_chars(
757 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01) },
758 { 18446744073709551615ULL, chunk_from_chars(
759 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff) }
760 };
761
762 int i;
763
764 for (i = 0; i < countof(test); i++)
765 {
766 ck_assert(asn1_parse_integer_uint64(test[i].chunk) == test[i].n);
767 }
768 }
769 END_TEST
770
771 Suite *asn1_suite_create()
772 {
773 Suite *s;
774 TCase *tc;
775
776 s = suite_create("asn1");
777
778 tc = tcase_create("algorithmIdentifier");
779 tcase_add_test(tc, test_asn1_algorithmIdentifier);
780 suite_add_tcase(s, tc);
781
782 tc = tcase_create("parse_algorithmIdentifier");
783 tcase_add_test(tc, test_asn1_parse_algorithmIdentifier);
784 suite_add_tcase(s, tc);
785
786 tc = tcase_create("known_oid");
787 tcase_add_test(tc, test_asn1_known_oid);
788 suite_add_tcase(s, tc);
789
790 tc = tcase_create("build_known_oid");
791 tcase_add_test(tc, test_asn1_build_known_oid);
792 suite_add_tcase(s, tc);
793
794 tc = tcase_create("oid_from_string");
795 tcase_add_test(tc, test_asn1_oid_from_string);
796 suite_add_tcase(s, tc);
797
798 tc = tcase_create("oid_to_string");
799 tcase_add_test(tc, test_asn1_oid_to_string);
800 suite_add_tcase(s, tc);
801
802 tc = tcase_create("length");
803 tcase_add_test(tc, test_asn1_length);
804 suite_add_tcase(s, tc);
805
806 tc = tcase_create("unwrap");
807 tcase_add_test(tc, test_asn1_unwrap);
808 suite_add_tcase(s, tc);
809
810 tc = tcase_create("is_asn1");
811 tcase_add_test(tc, test_is_asn1);
812 suite_add_tcase(s, tc);
813
814 tc = tcase_create("is_printablestring");
815 tcase_add_test(tc, test_asn1_is_printablestring);
816 suite_add_tcase(s, tc);
817
818 tc = tcase_create("to_time");
819 tcase_add_test(tc, test_asn1_to_time);
820 suite_add_tcase(s, tc);
821
822 tc = tcase_create("from_time");
823 tcase_add_test(tc, test_asn1_from_time);
824 suite_add_tcase(s, tc);
825
826 tc = tcase_create("parse_time");
827 tcase_add_test(tc, test_asn1_parse_time);
828 suite_add_tcase(s, tc);
829
830 tc = tcase_create("build_object");
831 tcase_add_test(tc, test_asn1_build_object);
832 suite_add_tcase(s, tc);
833
834 tc = tcase_create("simple_object");
835 tcase_add_test(tc, test_asn1_simple_object);
836 suite_add_tcase(s, tc);
837
838 tc = tcase_create("parse_simple_object");
839 tcase_add_test(tc, test_asn1_parse_simple_object);
840 suite_add_tcase(s, tc);
841
842 tc = tcase_create("bitstring");
843 tcase_add_test(tc, test_asn1_bitstring);
844 suite_add_tcase(s, tc);
845
846 tc = tcase_create("integer");
847 tcase_add_test(tc, test_asn1_integer);
848 suite_add_tcase(s, tc);
849
850 tc = tcase_create("parse_integer_uint64");
851 tcase_add_test(tc, test_asn1_parse_integer_uint64);
852 suite_add_tcase(s, tc);
853
854 return s;
855 }