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