xref: /openbsd-src/regress/lib/libcrypto/asn1/asn1basic.c (revision 25c4e8bd056e974b28f4a0ffd39d76c190a56013)
1 /* $OpenBSD: asn1basic.c,v 1.9 2022/06/25 15:49:28 jsing Exp $ */
2 /*
3  * Copyright (c) 2017, 2021 Joel Sing <jsing@openbsd.org>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include <openssl/asn1.h>
19 
20 #include <err.h>
21 #include <stdio.h>
22 #include <string.h>
23 
24 #include "asn1_locl.h"
25 
26 static void
27 hexdump(const unsigned char *buf, size_t len)
28 {
29 	size_t i;
30 
31 	for (i = 1; i <= len; i++)
32 		fprintf(stderr, " 0x%02hhx,%s", buf[i - 1], i % 8 ? "" : "\n");
33 
34 	fprintf(stderr, "\n");
35 }
36 
37 static int
38 asn1_compare_bytes(const char *label, const unsigned char *d1, int len1,
39     const unsigned char *d2, int len2)
40 {
41 	if (len1 != len2) {
42 		fprintf(stderr, "FAIL: %s - byte lengths differ "
43 		    "(%i != %i)\n", label, len1, len2);
44 		fprintf(stderr, "Got:\n");
45 		hexdump(d1, len1);
46 		fprintf(stderr, "Want:\n");
47 		hexdump(d2, len2);
48 		return 0;
49 	}
50 	if (memcmp(d1, d2, len1) != 0) {
51 		fprintf(stderr, "FAIL: %s - bytes differ\n", label);
52 		fprintf(stderr, "Got:\n");
53 		hexdump(d1, len1);
54 		fprintf(stderr, "Want:\n");
55 		hexdump(d2, len2);
56 		return 0;
57 	}
58 	return 1;
59 }
60 
61 const uint8_t asn1_bit_string_primitive[] = {
62 	0x03, 0x07,
63 	0x04, 0x0a, 0x3b, 0x5f, 0x29, 0x1c, 0xd0,
64 };
65 
66 static int
67 asn1_bit_string_test(void)
68 {
69 	uint8_t bs[] = {0x0a, 0x3b, 0x5f, 0x29, 0x1c, 0xd0};
70 	ASN1_BIT_STRING *abs;
71 	uint8_t *p = NULL, *pp;
72 	const uint8_t *q;
73 	int bit, i, len;
74 	int failed = 1;
75 
76 	if ((abs = ASN1_BIT_STRING_new()) == NULL) {
77 		fprintf(stderr, "FAIL: ASN1_BIT_STRING_new() == NULL\n");
78 		goto failed;
79 	}
80 	if (!ASN1_BIT_STRING_set(abs, bs, sizeof(bs))) {
81 		fprintf(stderr, "FAIL: failed to set bit string\n");
82 		goto failed;
83 	}
84 
85 	if ((len = i2d_ASN1_BIT_STRING(abs, NULL)) < 0) {
86 		fprintf(stderr, "FAIL: i2d_ASN1_BIT_STRING with NULL\n");
87 		goto failed;
88 	}
89 	if ((p = malloc(len)) == NULL)
90 		errx(1, "malloc");
91 	memset(p, 0xbd, len);
92 	pp = p;
93 	if ((i2d_ASN1_BIT_STRING(abs, &pp)) != len) {
94 		fprintf(stderr, "FAIL: i2d_ASN1_BIT_STRING\n");
95 		goto failed;
96 	}
97 	if (!asn1_compare_bytes("BIT_STRING", p, len, asn1_bit_string_primitive,
98 	    sizeof(asn1_bit_string_primitive)))
99 		goto failed;
100 	if (pp != p + len) {
101 		fprintf(stderr, "FAIL: i2d_ASN1_BIT_STRING pp = %p, want %p\n",
102 		    pp, p + len);
103 		goto failed;
104 	}
105 
106 	/* Test primitive decoding. */
107 	q = p;
108 	if (d2i_ASN1_BIT_STRING(&abs, &q, len) == NULL) {
109 		fprintf(stderr, "FAIL: d2i_ASN1_BIT_STRING primitive\n");
110 		goto failed;
111 	}
112 	if (!asn1_compare_bytes("BIT_STRING primitive data", abs->data, abs->length,
113 	    bs, sizeof(bs)))
114 		goto failed;
115 	if (q != p + len) {
116 		fprintf(stderr, "FAIL: d2i_ASN1_BIT_STRING q = %p, want %p\n",
117 		    q, p + len);
118 		goto failed;
119 	}
120 
121 	/* Test ASN1_BIT_STRING_get_bit(). */
122 	for (i = 0; i < ((int)sizeof(bs) * 8); i++) {
123 		bit = (bs[i / 8] >> (7 - i % 8)) & 1;
124 
125 		if (ASN1_BIT_STRING_get_bit(abs, i) != bit) {
126 			fprintf(stderr, "FAIL: ASN1_BIT_STRING_get_bit(_, %d) "
127 			    "= %d, want %d\n", i,
128 			    ASN1_BIT_STRING_get_bit(abs, i), bit);
129 			goto failed;
130 		}
131 	}
132 
133 	/* Test ASN1_BIT_STRING_set_bit(). */
134 	for (i = 0; i < ((int)sizeof(bs) * 8); i++) {
135 		if (!ASN1_BIT_STRING_set_bit(abs, i, 1)) {
136 			fprintf(stderr, "FAIL: ASN1_BIT_STRING_set_bit 1\n");
137 			goto failed;
138 		}
139 	}
140 	for (i = ((int)sizeof(bs) * 8) - 1; i >= 0; i--) {
141 		bit = (bs[i / 8] >> (7 - i % 8)) & 1;
142 		if (bit == 1)
143 			continue;
144 		if (!ASN1_BIT_STRING_set_bit(abs, i, 0)) {
145 			fprintf(stderr, "FAIL: ASN1_BIT_STRING_set_bit\n");
146 			goto failed;
147 		}
148 	}
149 
150 	if ((i2d_ASN1_BIT_STRING(abs, NULL)) != len) {
151 		fprintf(stderr, "FAIL: i2d_ASN1_BIT_STRING\n");
152 		goto failed;
153 	}
154 
155 	memset(p, 0xbd, len);
156 	pp = p;
157 	if ((i2d_ASN1_BIT_STRING(abs, &pp)) != len) {
158 		fprintf(stderr, "FAIL: i2d_ASN1_BIT_STRING\n");
159 		goto failed;
160 	}
161 
162 	if (!asn1_compare_bytes("BIT_STRING set", p, len, asn1_bit_string_primitive,
163 	    sizeof(asn1_bit_string_primitive)))
164 		goto failed;
165 
166 	failed = 0;
167 
168  failed:
169 	ASN1_BIT_STRING_free(abs);
170 	free(p);
171 
172 	return failed;
173 }
174 
175 const uint8_t asn1_boolean_false[] = {
176 	0x01, 0x01, 0x00,
177 };
178 const uint8_t asn1_boolean_true[] = {
179 	0x01, 0x01, 0x01,
180 };
181 
182 static int
183 asn1_boolean_test(void)
184 {
185 	uint8_t *p = NULL, *pp;
186 	const uint8_t *q;
187 	int len;
188 	int failed = 1;
189 
190 	if ((len = i2d_ASN1_BOOLEAN(0, NULL)) < 0) {
191 		fprintf(stderr, "FAIL: i2d_ASN1_BOOLEAN false with NULL\n");
192 		goto failed;
193 	}
194 	if ((p = malloc(len)) == NULL)
195 		errx(1, "calloc");
196 	memset(p, 0xbd, len);
197 	pp = p;
198 	if ((i2d_ASN1_BOOLEAN(0, &pp)) != len) {
199 		fprintf(stderr, "FAIL: i2d_ASN1_BOOLEAN false\n");
200 		goto failed;
201 	}
202 	if (pp != p + len) {
203 		fprintf(stderr, "FAIL: i2d_ASN1_BOOLEAN pp = %p, want %p\n",
204 		    pp, p + len);
205 		goto failed;
206 	}
207 
208 	if (!asn1_compare_bytes("BOOLEAN false", p, len, asn1_boolean_false,
209 	    sizeof(asn1_boolean_false)))
210 		goto failed;
211 
212 	q = p;
213 	if (d2i_ASN1_BOOLEAN(NULL, &q, len) != 0) {
214 		fprintf(stderr, "FAIL: BOOLEAN false did not decode to 0\n");
215 		goto failed;
216 	}
217 	if (q != p + len) {
218 		fprintf(stderr, "FAIL: d2i_ASN1_BOOLEAN q = %p, want %p\n",
219 		    q, p + len);
220 		goto failed;
221 	}
222 
223 	free(p);
224 	p = NULL;
225 
226 	if ((len = i2d_ASN1_BOOLEAN(1, NULL)) < 0) {
227 		fprintf(stderr, "FAIL: i2d_ASN1_BOOLEAN true with NULL\n");
228 		goto failed;
229 	}
230 	if ((p = calloc(1, len)) == NULL)
231 		errx(1, "calloc");
232 	pp = p;
233 	if ((i2d_ASN1_BOOLEAN(1, &pp)) != len) {
234 		fprintf(stderr, "FAIL: i2d_ASN1_BOOLEAN true\n");
235 		goto failed;
236 	}
237 	if (pp != p + len) {
238 		fprintf(stderr, "FAIL: i2d_ASN1_BOOLEAN pp = %p, want %p\n",
239 		    pp, p + len);
240 		goto failed;
241 	}
242 
243 	if (!asn1_compare_bytes("BOOLEAN true", p, len, asn1_boolean_true,
244 	    sizeof(asn1_boolean_true)))
245 		goto failed;
246 
247 	q = p;
248 	if (d2i_ASN1_BOOLEAN(NULL, &q, len) != 1) {
249 		fprintf(stderr, "FAIL: BOOLEAN true did not decode to 1\n");
250 		goto failed;
251 	}
252 	if (q != p + len) {
253 		fprintf(stderr, "FAIL: d2i_ASN1_BOOLEAN q = %p, want %p\n",
254 		    q, p + len);
255 		goto failed;
256 	}
257 
258 	failed = 0;
259 
260  failed:
261 	free(p);
262 
263 	return failed;
264 }
265 
266 struct asn1_integer_test {
267 	long value;
268 	uint8_t content[64];
269 	size_t content_len;
270 	int content_neg;
271 	uint8_t der[64];
272 	size_t der_len;
273 	int want_error;
274 };
275 
276 struct asn1_integer_test asn1_integer_tests[] = {
277 	{
278 		.value = 0,
279 		.content = {0x00},
280 		.content_len = 1,
281 		.der = {0x02, 0x01, 0x00},
282 		.der_len = 3,
283 	},
284 	{
285 		.value = 1,
286 		.content = {0x01},
287 		.content_len = 1,
288 		.der = {0x02, 0x01, 0x01},
289 		.der_len = 3,
290 	},
291 	{
292 		.value = -1,
293 		.content = {0x01},
294 		.content_len = 1,
295 		.content_neg = 1,
296 		.der = {0x02, 0x01, 0xff},
297 		.der_len = 3,
298 	},
299 	{
300 		.value = 127,
301 		.content = {0x7f},
302 		.content_len = 1,
303 		.der = {0x02, 0x01, 0x7f},
304 		.der_len = 3,
305 	},
306 	{
307 		.value = -127,
308 		.content = {0x7f},
309 		.content_len = 1,
310 		.content_neg = 1,
311 		.der = {0x02, 0x01, 0x81},
312 		.der_len = 3,
313 	},
314 	{
315 		.value = 128,
316 		.content = {0x80},
317 		.content_len = 1,
318 		.der = {0x02, 0x02, 0x00, 0x80},
319 		.der_len = 4,
320 	},
321 	{
322 		.value = -128,
323 		.content = {0x80},
324 		.content_len = 1,
325 		.content_neg = 1,
326 		.der = {0x02, 0x01, 0x80},
327 		.der_len = 3,
328 	},
329 	{
330 		/* 2^64 */
331 		.content = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
332 		.content_len = 9,
333 		.der = {0x02, 0x09, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
334 		.der_len = 11,
335 	},
336 	{
337 		/* -2^64 */
338 		.content = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
339 		.content_len = 9,
340 		.content_neg = 1,
341 		.der = {0x02, 0x09, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
342 		.der_len = 11,
343 	},
344 	{
345 		/* Invalid length. */
346 		.der = {0x02, 0x00},
347 		.der_len = 2,
348 		.want_error = 1,
349 	},
350 	{
351 		/* Invalid padding. */
352 		.der = {0x02, 0x09, 0x00, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
353 		.der_len = 11,
354 		.want_error = 1,
355 	},
356 	{
357 		/* Invalid padding. */
358 		.der = {0x02, 0x09, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
359 		.der_len = 11,
360 		.want_error = 1,
361 	},
362 };
363 
364 #define N_ASN1_INTEGER_TESTS \
365     (sizeof(asn1_integer_tests) / sizeof(*asn1_integer_tests))
366 
367 static int
368 asn1_integer_set_test(struct asn1_integer_test *ait)
369 {
370 	ASN1_INTEGER *aint = NULL;
371 	uint8_t *p = NULL, *pp;
372 	int len;
373 	int failed = 1;
374 
375 	if ((aint = ASN1_INTEGER_new()) == NULL) {
376 		fprintf(stderr, "FAIL: ASN1_INTEGER_new() == NULL\n");
377 		goto failed;
378 	}
379 	if (!ASN1_INTEGER_set(aint, ait->value)) {
380 		fprintf(stderr, "FAIL: ASN1_INTEGER_(%ld) failed\n",
381 		    ait->value);
382 		goto failed;
383 	}
384 	if (ait->value != 0 &&
385 	    !asn1_compare_bytes("INTEGER set", aint->data, aint->length,
386 	    ait->content, ait->content_len))
387 		goto failed;
388 	if (ait->content_neg && aint->type != V_ASN1_NEG_INTEGER) {
389 		fprintf(stderr, "FAIL: Not V_ASN1_NEG_INTEGER\n");
390 		goto failed;
391 	}
392 	if (ASN1_INTEGER_get(aint) != ait->value) {
393 		fprintf(stderr, "FAIL: ASN1_INTEGER_get() = %ld, want %ld\n",
394 		    ASN1_INTEGER_get(aint), ait->value);
395 		goto failed;
396 	}
397 	if ((len = i2d_ASN1_INTEGER(aint, NULL)) < 0) {
398 		fprintf(stderr, "FAIL: i2d_ASN1_INTEGER() failed\n");
399 		goto failed;
400 	}
401 	if ((p = malloc(len)) == NULL)
402 		errx(1, "malloc");
403 	memset(p, 0xbd, len);
404 	pp = p;
405 	if ((len = i2d_ASN1_INTEGER(aint, &pp)) < 0) {
406 		fprintf(stderr, "FAIL: i2d_ASN1_INTEGER() failed\n");
407 		goto failed;
408 	}
409 	if (!asn1_compare_bytes("INTEGER set", p, len, ait->der,
410 	    ait->der_len))
411 		goto failed;
412 
413 	failed = 0;
414 
415  failed:
416 	ASN1_INTEGER_free(aint);
417 	free(p);
418 
419 	return failed;
420 }
421 
422 static int
423 asn1_integer_content_test(struct asn1_integer_test *ait)
424 {
425 	ASN1_INTEGER *aint = NULL;
426 	uint8_t *p = NULL, *pp;
427 	int len;
428 	int failed = 1;
429 
430 	if ((aint = ASN1_INTEGER_new()) == NULL) {
431 		fprintf(stderr, "FAIL: ASN1_INTEGER_new() == NULL\n");
432 		goto failed;
433 	}
434 	if ((aint->data = malloc(ait->content_len)) == NULL)
435 		errx(1, "malloc");
436 	memcpy(aint->data, ait->content, ait->content_len);
437 	aint->length = ait->content_len;
438 	if (ait->content_neg)
439 		aint->type = V_ASN1_NEG_INTEGER;
440 
441 	if ((len = i2d_ASN1_INTEGER(aint, NULL)) < 0) {
442 		fprintf(stderr, "FAIL: i2d_ASN1_INTEGER() failed\n");
443 		goto failed;
444 	}
445 	if ((p = malloc(len)) == NULL)
446 		errx(1, "malloc");
447 	memset(p, 0xbd, len);
448 	pp = p;
449 	if ((len = i2d_ASN1_INTEGER(aint, &pp)) < 0) {
450 		fprintf(stderr, "FAIL: i2d_ASN1_INTEGER() failed\n");
451 		goto failed;
452 	}
453 	if (!asn1_compare_bytes("INTEGER content", p, len, ait->der,
454 	    ait->der_len))
455 		goto failed;
456 	if (pp != p + len) {
457 		fprintf(stderr, "FAIL: i2d_ASN1_INTEGER pp = %p, want %p\n",
458 		    pp, p + len);
459 		goto failed;
460 	}
461 
462 	failed = 0;
463 
464  failed:
465 	ASN1_INTEGER_free(aint);
466 	free(p);
467 
468 	return failed;
469 }
470 
471 static int
472 asn1_integer_decode_test(struct asn1_integer_test *ait)
473 {
474 	ASN1_INTEGER *aint = NULL;
475 	const uint8_t *q;
476 	int failed = 1;
477 
478 	q = ait->der;
479 	if (d2i_ASN1_INTEGER(&aint, &q, ait->der_len) != NULL) {
480 		if (ait->want_error != 0) {
481 			fprintf(stderr, "FAIL: INTEGER decoded when it should "
482 			    "have failed\n");
483 			goto failed;
484 		}
485 		if (!asn1_compare_bytes("INTEGER content", aint->data,
486 		    aint->length, ait->content, ait->content_len))
487 			goto failed;
488 		if (q != ait->der + ait->der_len) {
489 			fprintf(stderr, "FAIL: d2i_ASN1_INTEGER q = %p, want %p\n",
490 			    q, ait->der + ait->der_len);
491 			goto failed;
492 		}
493 	} else if (ait->want_error == 0) {
494 		fprintf(stderr, "FAIL: INTEGER failed to decode\n");
495 		goto failed;
496 	}
497 
498 	failed = 0;
499 
500  failed:
501 	ASN1_INTEGER_free(aint);
502 
503 	return failed;
504 }
505 
506 static int
507 asn1_integer_set_val_test(void)
508 {
509 	ASN1_INTEGER *aint = NULL;
510 	uint64_t uval;
511 	int64_t val;
512 	int failed = 1;
513 
514 	if ((aint = ASN1_INTEGER_new()) == NULL) {
515 		fprintf(stderr, "FAIL: ASN1_INTEGER_new() == NULL\n");
516 		goto failed;
517 	}
518 
519 	if (!ASN1_INTEGER_set_uint64(aint, 0)) {
520 		fprintf(stderr, "FAIL: ASN_INTEGER_set_uint64() failed with "
521 		    "0\n");
522 		goto failed;
523 	}
524 	if (!ASN1_INTEGER_get_uint64(&uval, aint)) {
525 		fprintf(stderr, "FAIL: ASN_INTEGER_get_uint64() failed with "
526 		    "0\n");
527 		goto failed;
528 	}
529 	if (uval != 0) {
530 		fprintf(stderr, "FAIL: uval != 0\n");
531 		goto failed;
532 	}
533 
534 	if (!ASN1_INTEGER_set_uint64(aint, UINT64_MAX)) {
535 		fprintf(stderr, "FAIL: ASN_INTEGER_set_uint64() failed with "
536 		    "UINT64_MAX\n");
537 		goto failed;
538 	}
539 	if (!ASN1_INTEGER_get_uint64(&uval, aint)) {
540 		fprintf(stderr, "FAIL: ASN_INTEGER_get_uint64() failed with "
541 		    "UINT64_MAX\n");
542 		goto failed;
543 	}
544 	if (uval != UINT64_MAX) {
545 		fprintf(stderr, "FAIL: uval != UINT64_MAX\n");
546 		goto failed;
547 	}
548 	if (ASN1_INTEGER_get_int64(&val, aint)) {
549 		fprintf(stderr, "FAIL: ASN_INTEGER_get_int64() succeeded "
550 		    "with UINT64_MAX\n");
551 		goto failed;
552 	}
553 
554 	if (!ASN1_INTEGER_set_int64(aint, INT64_MIN)) {
555 		fprintf(stderr, "FAIL: ASN_INTEGER_set_int64() failed with "
556 		    "INT64_MIN\n");
557 		goto failed;
558 	}
559 	if (!ASN1_INTEGER_get_int64(&val, aint)) {
560 		fprintf(stderr, "FAIL: ASN_INTEGER_get_int64() failed with "
561 		    "INT64_MIN\n");
562 		goto failed;
563 	}
564 	if (val != INT64_MIN) {
565 		fprintf(stderr, "FAIL: val != INT64_MIN\n");
566 		goto failed;
567 	}
568 	if (ASN1_INTEGER_get_uint64(&uval, aint)) {
569 		fprintf(stderr, "FAIL: ASN_INTEGER_get_uint64() succeeded "
570 		    "with INT64_MIN\n");
571 		goto failed;
572 	}
573 
574 	if (!ASN1_INTEGER_set_int64(aint, INT64_MAX)) {
575 		fprintf(stderr, "FAIL: ASN_INTEGER_set_int64() failed with "
576 		    "INT64_MAX\n");
577 		goto failed;
578 	}
579 	if (!ASN1_INTEGER_get_int64(&val, aint)) {
580 		fprintf(stderr, "FAIL: ASN_INTEGER_get_int64() failed with "
581 		    "INT64_MAX\n");
582 		goto failed;
583 	}
584 	if (val != INT64_MAX) {
585 		fprintf(stderr, "FAIL: ASN_INTEGER_get_int64() failed with "
586 		    "INT64_MAX\n");
587 		goto failed;
588 	}
589 	if (!ASN1_INTEGER_get_uint64(&uval, aint)) {
590 		fprintf(stderr, "FAIL: ASN_INTEGER_get_uint64() failed with "
591 		    "INT64_MAX\n");
592 		goto failed;
593 	}
594 	if (uval != INT64_MAX) {
595 		fprintf(stderr, "FAIL: uval != INT64_MAX\n");
596 		goto failed;
597 	}
598 
599 	failed = 0;
600 
601  failed:
602 	ASN1_INTEGER_free(aint);
603 
604 	return failed;
605 }
606 
607 static int
608 asn1_integer_cmp_test(void)
609 {
610 	ASN1_INTEGER *a = NULL, *b = NULL;
611 	int failed = 1;
612 
613 	if ((a = ASN1_INTEGER_new()) == NULL)
614 		goto failed;
615 	if ((b = ASN1_INTEGER_new()) == NULL)
616 		goto failed;
617 
618 	if (ASN1_INTEGER_cmp(a, b) != 0) {
619 		fprintf(stderr, "FAIL: INTEGER 0 == 0");
620 		goto failed;
621 	}
622 
623 	if (!ASN1_INTEGER_set(b, 1)) {
624 		fprintf(stderr, "FAIL: failed to set INTEGER");
625 		goto failed;
626 	}
627 	if (ASN1_INTEGER_cmp(a, b) >= 0) {
628 		fprintf(stderr, "FAIL: INTEGER 0 < 1");
629 		goto failed;
630 	}
631 	if (ASN1_INTEGER_cmp(b, a) <= 0) {
632 		fprintf(stderr, "FAIL: INTEGER 1 > 0");
633 		goto failed;
634 	}
635 
636 	if (!ASN1_INTEGER_set(b, -1)) {
637 		fprintf(stderr, "FAIL: failed to set INTEGER");
638 		goto failed;
639 	}
640 	if (ASN1_INTEGER_cmp(a, b) <= 0) {
641 		fprintf(stderr, "FAIL: INTEGER 0 > -1");
642 		goto failed;
643 	}
644 	if (ASN1_INTEGER_cmp(b, a) >= 0) {
645 		fprintf(stderr, "FAIL: INTEGER -1 < 0");
646 		goto failed;
647 	}
648 
649 	if (!ASN1_INTEGER_set(a, 1)) {
650 		fprintf(stderr, "FAIL: failed to set INTEGER");
651 		goto failed;
652 	}
653 	if (ASN1_INTEGER_cmp(a, b) <= 0) {
654 		fprintf(stderr, "FAIL: INTEGER 1 > -1");
655 		goto failed;
656 	}
657 	if (ASN1_INTEGER_cmp(b, a) >= 0) {
658 		fprintf(stderr, "FAIL: INTEGER -1 < 1");
659 		goto failed;
660 	}
661 
662 	if (!ASN1_INTEGER_set(b, 1)) {
663 		fprintf(stderr, "FAIL: failed to set INTEGER");
664 		goto failed;
665 	}
666 	if (ASN1_INTEGER_cmp(a, b) != 0) {
667 		fprintf(stderr, "FAIL: INTEGER 1 == 1");
668 		goto failed;
669 	}
670 
671 	failed = 0;
672 
673  failed:
674 	ASN1_INTEGER_free(a);
675 	ASN1_INTEGER_free(b);
676 
677 	return failed;
678 }
679 
680 static int
681 asn1_integer_test(void)
682 {
683 	struct asn1_integer_test *ait;
684 	int failed = 0;
685 	size_t i;
686 
687 	for (i = 0; i < N_ASN1_INTEGER_TESTS; i++) {
688 		ait = &asn1_integer_tests[i];
689 		if (ait->content_len > 0 && ait->content_len <= 4)
690 			failed |= asn1_integer_set_test(ait);
691 		if (ait->content_len > 0)
692 			failed |= asn1_integer_content_test(ait);
693 		failed |= asn1_integer_decode_test(ait);
694 	}
695 
696 	failed |= asn1_integer_cmp_test();
697 	failed |= asn1_integer_set_val_test();
698 
699 	return failed;
700 }
701 
702 int
703 main(int argc, char **argv)
704 {
705 	int failed = 0;
706 
707 	failed |= asn1_bit_string_test();
708 	failed |= asn1_boolean_test();
709 	failed |= asn1_integer_test();
710 
711 	return (failed);
712 }
713