1 /* $NetBSD: check-template.c,v 1.3 2023/06/19 21:41:42 christos Exp $ */
2
3 /*
4 * Copyright (c) 1999 - 2005 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
6 * All rights reserved.
7 *
8 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 *
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 *
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 *
21 * 3. Neither the name of the Institute nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 */
37
38 #include <config.h>
39
40 #include <stdio.h>
41 #include <string.h>
42 #include <err.h>
43 #include <krb5/roken.h>
44
45 #include <krb5/asn1-common.h>
46 #include <krb5/asn1_err.h>
47 #include <krb5/der.h>
48 #include <test_asn1.h>
49
50 #include "check-common.h"
51 #include "der_locl.h"
52
53 static int
cmp_dummy(void * a,void * b)54 cmp_dummy (void *a, void *b)
55 {
56 return 0;
57 }
58
59 static int
test_uint64(void)60 test_uint64(void)
61 {
62 struct test_case tests[] = {
63 { NULL, 3, "\x02\x01\x00", "uint64 0" },
64 { NULL, 7, "\x02\x05\x01\xff\xff\xff\xff", "uint64 1" },
65 { NULL, 7, "\x02\x05\x02\x00\x00\x00\x00", "uint64 2" },
66 { NULL, 9, "\x02\x07\x7f\xff\xff\xff\xff\xff\xff", "uint64 3" },
67 { NULL, 10, "\x02\x08\x00\x80\x00\x00\x00\x00\x00\x00", "uint64 4" },
68 { NULL, 10, "\x02\x08\x7f\xff\xff\xff\xff\xff\xff\xff", "uint64 5" },
69 { NULL, 11, "\x02\x09\x00\xff\xff\xff\xff\xff\xff\xff\xff", "uint64 6" }
70 };
71
72 size_t i;
73 int ret = 0, ntests = sizeof(tests) / sizeof(*tests);
74 TESTuint64 values[] = { 0, 8589934591LL, 8589934592LL,
75 36028797018963967LL, 36028797018963968LL,
76 9223372036854775807LL, 18446744073709551615ULL };
77
78 for (i = 0; i < ntests; i++)
79 tests[i].val = &values[i];
80
81 if (sizeof(TESTuint64) != sizeof(uint64_t)) {
82 ret += 1;
83 printf("sizeof(TESTuint64) %d != sizeof(uint64_t) %d\n",
84 (int)sizeof(TESTuint64), (int)sizeof(uint64_t));
85 }
86
87 ret += generic_test (tests, ntests, sizeof(TESTuint64),
88 (generic_encode)encode_TESTuint64,
89 (generic_length)length_TESTuint64,
90 (generic_decode)decode_TESTuint64,
91 (generic_free)free_TESTuint64,
92 cmp_dummy,
93 NULL);
94 return ret;
95 }
96
97 static int
test_seqofseq(void)98 test_seqofseq(void)
99 {
100 struct test_case tests[] = {
101 { NULL, 2,
102 "\x30\x00",
103 "seqofseq 0" },
104 { NULL, 9,
105 "\x30\x07\x30\x05\xa0\x03\x02\x01\x00",
106 "seqofseq 1" },
107 { NULL, 16,
108 "\x30\x0e\x30\x05\xa0\x03\x02\x01\x00\x30\x05\xa0\x03\x02\x01\x01",
109 "seqofseq 2" }
110 };
111
112 int ret = 0, ntests = sizeof(tests) / sizeof(*tests);
113 TESTSeqOfSeq c0, c1, c2;
114 struct TESTSeqOfSeq_val i[2];
115
116 i[0].zero = 0;
117 i[1].zero = 1;
118
119 c0.len = 0;
120 c0.val = NULL;
121 tests[0].val = &c0;
122
123 c1.len = 1;
124 c1.val = i;
125 tests[1].val = &c1;
126
127 c2.len = 2;
128 c2.val = i;
129 tests[2].val = &c2;
130
131 ret += generic_test (tests, ntests, sizeof(TESTSeqOfSeq),
132 (generic_encode)encode_TESTSeqOfSeq,
133 (generic_length)length_TESTSeqOfSeq,
134 (generic_decode)decode_TESTSeqOfSeq,
135 (generic_free)free_TESTSeqOfSeq,
136 cmp_dummy,
137 NULL);
138 return ret;
139 }
140
141 static int
test_seqofseq2(void)142 test_seqofseq2(void)
143 {
144 struct test_case tests[] = {
145 { NULL, 2,
146 "\x30\x00",
147 "seqofseq2 0" },
148 { NULL, 11,
149 "\x30\x09\x30\x07\xa0\x05\x1b\x03\x65\x74\x74",
150 "seqofseq2 1" },
151 { NULL, 21,
152 "\x30\x13\x30\x07\xa0\x05\x1b\x03\x65\x74\x74\x30\x08\xa0"
153 "\x06\x1b\x04\x74\x76\x61\x61",
154 "seqofseq2 2" }
155 };
156
157 int ret = 0, ntests = sizeof(tests) / sizeof(*tests);
158 TESTSeqOfSeq2 c0, c1, c2;
159 struct TESTSeqOfSeq2_val i[2];
160
161 i[0].string = "ett";
162 i[1].string = "tvaa";
163
164 c0.len = 0;
165 c0.val = NULL;
166 tests[0].val = &c0;
167
168 c1.len = 1;
169 c1.val = i;
170 tests[1].val = &c1;
171
172 c2.len = 2;
173 c2.val = i;
174 tests[2].val = &c2;
175
176 ret += generic_test (tests, ntests, sizeof(TESTSeqOfSeq2),
177 (generic_encode)encode_TESTSeqOfSeq2,
178 (generic_length)length_TESTSeqOfSeq2,
179 (generic_decode)decode_TESTSeqOfSeq2,
180 (generic_free)free_TESTSeqOfSeq2,
181 cmp_dummy,
182 NULL);
183 return ret;
184 }
185
186 static int
test_seqof2(void)187 test_seqof2(void)
188 {
189 struct test_case tests[] = {
190 { NULL, 4,
191 "\x30\x02\x30\x00",
192 "seqof2 1" },
193 { NULL, 9,
194 "\x30\x07\x30\x05\x1b\x03\x66\x6f\x6f",
195 "seqof2 2" },
196 { NULL, 14,
197 "\x30\x0c\x30\x0a\x1b\x03\x66\x6f\x6f\x1b\x03\x62\x61\x72",
198 "seqof2 3" }
199 };
200
201 int ret = 0, ntests = sizeof(tests) / sizeof(*tests);
202 TESTSeqOf2 c0, c1, c2;
203 heim_general_string i[2];
204
205 i[0] = "foo";
206 i[1] = "bar";
207
208 c0.strings.val = NULL;
209 c0.strings.len = 0;
210 tests[0].val = &c0;
211
212 c1.strings.len = 1;
213 c1.strings.val = i;
214 tests[1].val = &c1;
215
216 c2.strings.len = 2;
217 c2.strings.val = i;
218 tests[2].val = &c2;
219
220 ret += generic_test (tests, ntests, sizeof(TESTSeqOf2),
221 (generic_encode)encode_TESTSeqOf2,
222 (generic_length)length_TESTSeqOf2,
223 (generic_decode)decode_TESTSeqOf2,
224 (generic_free)free_TESTSeqOf2,
225 cmp_dummy,
226 NULL);
227 return ret;
228 }
229
230 static int
test_seqof3(void)231 test_seqof3(void)
232 {
233 struct test_case tests[] = {
234 { NULL, 2,
235 "\x30\x00",
236 "seqof3 0" },
237 { NULL, 4,
238 "\x30\x02\x30\x00",
239 "seqof3 1" },
240 { NULL, 9,
241 "\x30\x07\x30\x05\x1b\x03\x66\x6f\x6f",
242 "seqof3 2" },
243 { NULL, 14,
244 "\x30\x0c\x30\x0a\x1b\x03\x66\x6f\x6f\x1b\x03\x62\x61\x72",
245 "seqof3 3" }
246 };
247
248 int ret = 0, ntests = sizeof(tests) / sizeof(*tests);
249 TESTSeqOf3 c0, c1, c2, c3;
250 struct TESTSeqOf3_strings s1, s2, s3;
251 heim_general_string i[2];
252
253 i[0] = "foo";
254 i[1] = "bar";
255
256 c0.strings = NULL;
257 tests[0].val = &c0;
258
259 s1.val = NULL;
260 s1.len = 0;
261 c1.strings = &s1;
262 tests[1].val = &c1;
263
264 s2.len = 1;
265 s2.val = i;
266 c2.strings = &s2;
267 tests[2].val = &c2;
268
269 s3.len = 2;
270 s3.val = i;
271 c3.strings = &s3;
272 tests[3].val = &c3;
273
274 ret += generic_test (tests, ntests, sizeof(TESTSeqOf3),
275 (generic_encode)encode_TESTSeqOf3,
276 (generic_length)length_TESTSeqOf3,
277 (generic_decode)decode_TESTSeqOf3,
278 (generic_free)free_TESTSeqOf3,
279 cmp_dummy,
280 NULL);
281 return ret;
282 }
283
284
285 static int
test_seqof4(void)286 test_seqof4(void)
287 {
288 struct test_case tests[] = {
289 { NULL, 2,
290 "\x30\x00",
291 "seq4 0" },
292 { NULL, 4,
293 "\x30\x02" "\xa1\x00",
294 "seq4 1" },
295 { NULL, 8,
296 "\x30\x06" "\xa0\x02\x30\x00" "\xa1\x00",
297 "seq4 2" },
298 { NULL, 2 + (2 + 0x18) + (2 + 0x27) + (2 + 0x31),
299 "\x30\x76" /* 2 SEQ */
300 "\xa0\x18\x30\x16" /* 4 [0] SEQ */
301 "\x30\x14" /* 2 SEQ */
302 "\x04\x00" /* 2 OCTET-STRING */
303 "\x04\x02\x01\x02" /* 4 OCTET-STRING */
304 "\x02\x01\x01" /* 3 INT */
305 "\x02\x09\x00\xff\xff\xff\xff\xff\xff\xff\xff"
306 /* 11 INT */
307 "\xa1\x27" /* 2 [1] IMPL SEQ */
308 "\x30\x25" /* 2 SEQ */
309 "\x02\x01\x01" /* 3 INT */
310 "\x02\x09\x00\xff\xff\xff\xff\xff\xff\xff\xff"
311 /* 11 INT */
312 "\x02\x09\x00\x80\x00\x00\x00\x00\x00\x00\x00"
313 /* 11 INT */
314 "\x04\x00" /* 2 OCTET-STRING */
315 "\x04\x02\x01\x02" /* 4 OCTET-STRING */
316 "\x04\x04\x00\x01\x02\x03" /* 6 OCTET-STRING */
317 "\xa2\x31" /* 2 [2] IMPL SEQ */
318 "\x30\x2f" /* 2 SEQ */
319 "\x04\x00" /* 2 OCTET-STRING */
320 "\x02\x01\x01" /* 3 INT */
321 "\x04\x02\x01\x02" /* 4 OCTET-STRING */
322 "\x02\x09\x00\xff\xff\xff\xff\xff\xff\xff\xff"
323 /* 11 INT */
324 "\x04\x04\x00\x01\x02\x03" /* 6 OCTET-STRING */
325 "\x02\x09\x00\x80\x00\x00\x00\x00\x00\x00\x00"
326 /* 11 INT */
327 "\x04\x01\x00" /* 3 OCTET-STRING */
328 "\x02\x05\x01\x00\x00\x00\x00", /* 7 INT */
329 "seq4 3" },
330 };
331
332 int ret = 0, ntests = sizeof(tests) / sizeof(*tests);
333 TESTSeqOf4 c[4];
334 struct TESTSeqOf4_b1 b1[4];
335 struct TESTSeqOf4_b2 b2[4];
336 struct TESTSeqOf4_b3 b3[4];
337 struct TESTSeqOf4_b1_val b1val[4];
338 struct TESTSeqOf4_b2_val b2val[4];
339 struct TESTSeqOf4_b3_val b3val[4];
340
341 c[0].b1 = NULL;
342 c[0].b2 = NULL;
343 c[0].b3 = NULL;
344 tests[0].val = &c[0];
345
346 b2[1].len = 0;
347 b2[1].val = NULL;
348 c[1].b1 = NULL;
349 c[1].b2 = &b2[1];
350 c[1].b3 = NULL;
351 tests[1].val = &c[1];
352
353 b1[2].len = 0;
354 b1[2].val = NULL;
355 b2[2].len = 0;
356 b2[2].val = NULL;
357 c[2].b1 = &b1[2];
358 c[2].b2 = &b2[2];
359 c[2].b3 = NULL;
360 tests[2].val = &c[2];
361
362 b1val[3].s1.data = "";
363 b1val[3].s1.length = 0;
364 b1val[3].u1 = 1LL;
365 b1val[3].s2.data = "\x01\x02";
366 b1val[3].s2.length = 2;
367 b1val[3].u2 = -1LL;
368
369 b2val[3].s1.data = "";
370 b2val[3].s1.length = 0;
371 b2val[3].u1 = 1LL;
372 b2val[3].s2.data = "\x01\x02";
373 b2val[3].s2.length = 2;
374 b2val[3].u2 = -1LL;
375 b2val[3].s3.data = "\x00\x01\x02\x03";
376 b2val[3].s3.length = 4;
377 b2val[3].u3 = 1ULL<<63;
378
379 b3val[3].s1.data = "";
380 b3val[3].s1.length = 0;
381 b3val[3].u1 = 1LL;
382 b3val[3].s2.data = "\x01\x02";
383 b3val[3].s2.length = 2;
384 b3val[3].u2 = -1LL;
385 b3val[3].s3.data = "\x00\x01\x02\x03";
386 b3val[3].s3.length = 4;
387 b3val[3].u3 = 1ULL<<63;
388 b3val[3].s4.data = "\x00";
389 b3val[3].s4.length = 1;
390 b3val[3].u4 = 1LL<<32;
391
392 b1[3].len = 1;
393 b1[3].val = &b1val[3];
394 b2[3].len = 1;
395 b2[3].val = &b2val[3];
396 b3[3].len = 1;
397 b3[3].val = &b3val[3];
398 c[3].b1 = &b1[3];
399 c[3].b2 = &b2[3];
400 c[3].b3 = &b3[3];
401 tests[3].val = &c[3];
402
403 ret += generic_test (tests, ntests, sizeof(TESTSeqOf4),
404 (generic_encode)encode_TESTSeqOf4,
405 (generic_length)length_TESTSeqOf4,
406 (generic_decode)decode_TESTSeqOf4,
407 (generic_free)free_TESTSeqOf4,
408 cmp_dummy,
409 NULL);
410 return ret;
411 }
412
413 static int
cmp_test_seqof5(void * a,void * b)414 cmp_test_seqof5 (void *a, void *b)
415 {
416 TESTSeqOf5 *aval = a;
417 TESTSeqOf5 *bval = b;
418
419 IF_OPT_COMPARE(aval, bval, outer) {
420 COMPARE_INTEGER(&aval->outer->inner, &bval->outer->inner, u0);
421 COMPARE_OCTET_STRING(&aval->outer->inner, &bval->outer->inner, s0);
422 COMPARE_INTEGER(&aval->outer->inner, &bval->outer->inner, u1);
423 COMPARE_OCTET_STRING(&aval->outer->inner, &bval->outer->inner, s1);
424 COMPARE_INTEGER(&aval->outer->inner, &bval->outer->inner, u2);
425 COMPARE_OCTET_STRING(&aval->outer->inner, &bval->outer->inner, s2);
426 COMPARE_INTEGER(&aval->outer->inner, &bval->outer->inner, u3);
427 COMPARE_OCTET_STRING(&aval->outer->inner, &bval->outer->inner, s3);
428 COMPARE_INTEGER(&aval->outer->inner, &bval->outer->inner, u4);
429 COMPARE_OCTET_STRING(&aval->outer->inner, &bval->outer->inner, s4);
430 COMPARE_INTEGER(&aval->outer->inner, &bval->outer->inner, u5);
431 COMPARE_OCTET_STRING(&aval->outer->inner, &bval->outer->inner, s5);
432 COMPARE_INTEGER(&aval->outer->inner, &bval->outer->inner, u6);
433 COMPARE_OCTET_STRING(&aval->outer->inner, &bval->outer->inner, s6);
434 COMPARE_INTEGER(&aval->outer->inner, &bval->outer->inner, u7);
435 COMPARE_OCTET_STRING(&aval->outer->inner, &bval->outer->inner, s7);
436 }
437 return 0;
438 }
439
440 static int
test_seqof5(void)441 test_seqof5(void)
442 {
443 struct test_case tests[] = {
444 { NULL, 2, "\x30\x00", "seq5 0" },
445 { NULL, 126,
446 "\x30\x7c" /* SEQ */
447 "\x30\x7a" /* SEQ */
448 "\x30\x78" /* SEQ */
449 "\x02\x01\x01" /* INT 1 */
450 "\x04\x06\x01\x01\x01\x01\x01\x01" /* "\0x1"x6 */
451 "\x02\x09\x00\xff\xff\xff\xff\xff\xff\xff\xfe" /* INT ~1 */
452 "\x04\x06\x02\x02\x02\x02\x02\x02" /* "\x02"x6 */
453 "\x02\x01\x02" /* INT 2 */
454 "\x04\x06\x03\x03\x03\x03\x03\x03" /* "\x03"x6 */
455 "\x02\x09\x00\xff\xff\xff\xff\xff\xff\xff\xfd" /* INT ~2 */
456 "\x04\x06\x04\x04\x04\x04\x04\x04" /* ... */
457 "\x02\x01\x03"
458 "\x04\x06\x05\x05\x05\x05\x05\x05"
459 "\x02\x09\x00\xff\xff\xff\xff\xff\xff\xff\xfc"
460 "\x04\x06\x06\x06\x06\x06\x06\x06"
461 "\x02\x01\x04"
462 "\x04\x06\x07\x07\x07\x07\x07\x07"
463 "\x02\x09\x00\xff\xff\xff\xff\xff\xff\xff\xfb"
464 "\x04\x06\x08\x08\x08\x08\x08\x08",
465 "seq5 1" },
466 };
467
468 int ret = 0, ntests = sizeof(tests) / sizeof(*tests);
469 TESTSeqOf5 c[2];
470 struct TESTSeqOf5_outer outer;
471 struct TESTSeqOf5_outer_inner inner;
472 TESTuint64 u[8];
473 heim_octet_string s[8];
474 int i;
475
476 c[0].outer = NULL;
477 tests[0].val = &c[0];
478
479 for (i = 0; i < 8; ++i) {
480 u[i] = (i&1) == 0 ? i/2+1 : ~(i/2+1);
481 s[i].data = memset(malloc(s[i].length = 6), i+1, 6);
482 }
483
484 inner.u0 = u[0]; inner.u1 = u[1]; inner.u2 = u[2]; inner.u3 = u[3];
485 inner.u4 = u[4]; inner.u5 = u[5]; inner.u6 = u[6]; inner.u7 = u[7];
486 inner.s0 = s[0]; inner.s1 = s[1]; inner.s2 = s[2]; inner.s3 = s[3];
487 inner.s4 = s[4]; inner.s5 = s[5]; inner.s6 = s[6]; inner.s7 = s[7];
488
489 outer.inner = inner;
490 c[1].outer = &outer;
491 tests[1].val = &c[1];
492
493 ret += generic_test (tests, ntests, sizeof(TESTSeqOf5),
494 (generic_encode)encode_TESTSeqOf5,
495 (generic_length)length_TESTSeqOf5,
496 (generic_decode)decode_TESTSeqOf5,
497 (generic_free)free_TESTSeqOf5,
498 cmp_test_seqof5,
499 NULL);
500
501 for (i = 0; i < 8; ++i)
502 free(s[i].data);
503
504 return ret;
505 }
506
507 int
main(int argc,char ** argv)508 main(int argc, char **argv)
509 {
510 int ret = 0;
511
512 ret += test_uint64();
513 ret += test_seqofseq();
514 ret += test_seqofseq2();
515 ret += test_seqof2();
516 ret += test_seqof3();
517 ret += test_seqof4();
518 ret += test_seqof5();
519
520 return ret;
521 }
522