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