summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/regress/lib/libcrypto/asn1/asn1basic.c249
1 files changed, 248 insertions, 1 deletions
diff --git a/src/regress/lib/libcrypto/asn1/asn1basic.c b/src/regress/lib/libcrypto/asn1/asn1basic.c
index 6b3c72f975..b22fdde534 100644
--- a/src/regress/lib/libcrypto/asn1/asn1basic.c
+++ b/src/regress/lib/libcrypto/asn1/asn1basic.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: asn1basic.c,v 1.4 2022/01/12 07:55:25 tb Exp $ */ 1/* $OpenBSD: asn1basic.c,v 1.5 2022/04/23 18:23:48 jsing Exp $ */
2/* 2/*
3 * Copyright (c) 2017, 2021 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2017, 2021 Joel Sing <jsing@openbsd.org>
4 * 4 *
@@ -234,6 +234,252 @@ asn1_boolean_test(void)
234 return failed; 234 return failed;
235} 235}
236 236
237struct asn1_integer_test {
238 long value;
239 uint8_t content[64];
240 size_t content_len;
241 int content_neg;
242 uint8_t der[64];
243 size_t der_len;
244 int want_error;
245};
246
247struct asn1_integer_test asn1_integer_tests[] = {
248 {
249 .value = 0,
250 .content = {0x00},
251 .content_len = 1,
252 .der = {0x02, 0x01, 0x00},
253 .der_len = 3,
254 },
255 {
256 .value = 1,
257 .content = {0x01},
258 .content_len = 1,
259 .der = {0x02, 0x01, 0x01},
260 .der_len = 3,
261 },
262 {
263 .value = -1,
264 .content = {0x01},
265 .content_len = 1,
266 .content_neg = 1,
267 .der = {0x02, 0x01, 0xff},
268 .der_len = 3,
269 },
270 {
271 .value = 127,
272 .content = {0x7f},
273 .content_len = 1,
274 .der = {0x02, 0x01, 0x7f},
275 .der_len = 3,
276 },
277 {
278 .value = -127,
279 .content = {0x7f},
280 .content_len = 1,
281 .content_neg = 1,
282 .der = {0x02, 0x01, 0x81},
283 .der_len = 3,
284 },
285 {
286 .value = 128,
287 .content = {0x80},
288 .content_len = 1,
289 .der = {0x02, 0x02, 0x00, 0x80},
290 .der_len = 4,
291 },
292 {
293 .value = -128,
294 .content = {0x80},
295 .content_len = 1,
296 .content_neg = 1,
297 .der = {0x02, 0x01, 0x80},
298 .der_len = 3,
299 },
300 {
301 /* 2^64 */
302 .content = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
303 .content_len = 9,
304 .der = {0x02, 0x09, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
305 .der_len = 11,
306 },
307 {
308 /* -2^64 */
309 .content = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
310 .content_len = 9,
311 .content_neg = 1,
312 .der = {0x02, 0x09, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
313 .der_len = 11,
314 },
315#if 0
316 {
317 /* Invalid length. */
318 .der = {0x02, 0x00},
319 .der_len = 2,
320 .want_error = 1,
321 },
322 {
323 /* Invalid padding. */
324 .der = {0x02, 0x09, 0x00, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
325 .der_len = 11,
326 .want_error = 1,
327 },
328 {
329 /* Invalid padding. */
330 .der = {0x02, 0x09, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
331 .der_len = 11,
332 .want_error = 1,
333 },
334#endif
335};
336
337#define N_ASN1_INTEGER_TESTS \
338 (sizeof(asn1_integer_tests) / sizeof(*asn1_integer_tests))
339
340static int
341asn1_integer_set_test(struct asn1_integer_test *ait)
342{
343 ASN1_INTEGER *aint = NULL;
344 uint8_t *p = NULL, *pp;
345 int len;
346 int failed = 1;
347
348 if ((aint = ASN1_INTEGER_new()) == NULL) {
349 fprintf(stderr, "FAIL: ASN1_INTEGER_new() == NULL\n");
350 goto failed;
351 }
352 if (!ASN1_INTEGER_set(aint, ait->value)) {
353 fprintf(stderr, "FAIL: ASN1_INTEGER_(%ld) failed\n",
354 ait->value);
355 goto failed;
356 }
357 if (ait->value != 0 &&
358 !asn1_compare_bytes("INTEGER set", aint->data, aint->length,
359 ait->content, ait->content_len))
360 goto failed;
361 if (ait->content_neg && aint->type != V_ASN1_NEG_INTEGER) {
362 fprintf(stderr, "FAIL: Not V_ASN1_NEG_INTEGER\n");
363 goto failed;
364 }
365 if ((len = i2d_ASN1_INTEGER(aint, NULL)) < 0) {
366 fprintf(stderr, "FAIL: i2d_ASN1_INTEGER() failed\n");
367 goto failed;
368 }
369 if ((p = malloc(len)) == NULL)
370 errx(1, "malloc");
371 memset(p, 0xbd, len);
372 pp = p;
373 if ((len = i2d_ASN1_INTEGER(aint, &pp)) < 0) {
374 fprintf(stderr, "FAIL: i2d_ASN1_INTEGER() failed\n");
375 goto failed;
376 }
377 if (!asn1_compare_bytes("INTEGER set", p, len, ait->der,
378 ait->der_len))
379 goto failed;
380
381 failed = 0;
382
383 failed:
384 ASN1_INTEGER_free(aint);
385 free(p);
386
387 return failed;
388}
389
390static int
391asn1_integer_content_test(struct asn1_integer_test *ait)
392{
393 ASN1_INTEGER *aint = NULL;
394 uint8_t *p = NULL, *pp;
395 int len;
396 int failed = 1;
397
398 if ((aint = ASN1_INTEGER_new()) == NULL) {
399 fprintf(stderr, "FAIL: ASN1_INTEGER_new() == NULL\n");
400 goto failed;
401 }
402 if ((aint->data = malloc(ait->content_len)) == NULL)
403 errx(1, "malloc");
404 memcpy(aint->data, ait->content, ait->content_len);
405 aint->length = ait->content_len;
406 if (ait->content_neg)
407 aint->type = V_ASN1_NEG_INTEGER;
408
409 if ((len = i2d_ASN1_INTEGER(aint, NULL)) < 0) {
410 fprintf(stderr, "FAIL: i2d_ASN1_INTEGER() failed\n");
411 goto failed;
412 }
413 if ((p = malloc(len)) == NULL)
414 errx(1, "malloc");
415 memset(p, 0xbd, len);
416 pp = p;
417 if ((len = i2d_ASN1_INTEGER(aint, &pp)) < 0) {
418 fprintf(stderr, "FAIL: i2d_ASN1_INTEGER() failed\n");
419 goto failed;
420 }
421 if (!asn1_compare_bytes("INTEGER content", p, len, ait->der,
422 ait->der_len))
423 goto failed;
424
425 failed = 0;
426
427 failed:
428 ASN1_INTEGER_free(aint);
429 free(p);
430
431 return failed;
432}
433
434static int
435asn1_integer_decode_test(struct asn1_integer_test *ait)
436{
437 ASN1_INTEGER *aint = NULL;
438 const uint8_t *q;
439 int failed = 1;
440
441 q = ait->der;
442 if (d2i_ASN1_INTEGER(&aint, &q, ait->der_len) != NULL) {
443 if (ait->want_error != 0) {
444 fprintf(stderr, "FAIL: INTEGER decoded when it should "
445 "have failed\n");
446 goto failed;
447 }
448 if (!asn1_compare_bytes("INTEGER content", aint->data,
449 aint->length, ait->content, ait->content_len))
450 goto failed;
451 } else if (ait->want_error == 0) {
452 fprintf(stderr, "FAIL: INTEGER failed to decode\n");
453 goto failed;
454 }
455
456 failed = 0;
457
458 failed:
459 ASN1_INTEGER_free(aint);
460
461 return failed;
462}
463
464static int
465asn1_integer_test(void)
466{
467 struct asn1_integer_test *ait;
468 int failed = 0;
469 size_t i;
470
471 for (i = 0; i < N_ASN1_INTEGER_TESTS; i++) {
472 ait = &asn1_integer_tests[i];
473 if (ait->content_len > 0 && ait->content_len <= 4)
474 failed |= asn1_integer_set_test(ait);
475 if (ait->content_len > 0)
476 failed |= asn1_integer_content_test(ait);
477 failed |= asn1_integer_decode_test(ait);
478 }
479
480 return failed;
481}
482
237int 483int
238main(int argc, char **argv) 484main(int argc, char **argv)
239{ 485{
@@ -241,6 +487,7 @@ main(int argc, char **argv)
241 487
242 failed |= asn1_bit_string_test(); 488 failed |= asn1_bit_string_test();
243 failed |= asn1_boolean_test(); 489 failed |= asn1_boolean_test();
490 failed |= asn1_integer_test();
244 491
245 return (failed); 492 return (failed);
246} 493}