Add regress test for the encoding of an ASN1_INTEGER with NULL data.
authorjsing <jsing@openbsd.org>
Sun, 28 Aug 2022 17:59:57 +0000 (17:59 +0000)
committerjsing <jsing@openbsd.org>
Sun, 28 Aug 2022 17:59:57 +0000 (17:59 +0000)
regress/lib/libcrypto/asn1/asn1basic.c

index f3b7684..9ab23e7 100644 (file)
@@ -1,4 +1,4 @@
-/* $OpenBSD: asn1basic.c,v 1.9 2022/06/25 15:49:28 jsing Exp $ */
+/* $OpenBSD: asn1basic.c,v 1.10 2022/08/28 17:59:57 jsing Exp $ */
 /*
  * Copyright (c) 2017, 2021 Joel Sing <jsing@openbsd.org>
  *
@@ -677,6 +677,42 @@ asn1_integer_cmp_test(void)
        return failed;
 }
 
+static int
+asn1_integer_null_data_test(void)
+{
+       const uint8_t der[] = {0x02, 0x01, 0x00};
+       ASN1_INTEGER *aint = NULL;
+       uint8_t *p = NULL, *pp;
+       int len;
+       int failed = 0;
+
+       if ((aint = ASN1_INTEGER_new()) == NULL) {
+               fprintf(stderr, "FAIL: ASN1_INTEGER_new() == NULL\n");
+               goto failed;
+       }
+       if ((len = i2d_ASN1_INTEGER(aint, NULL)) < 0) {
+               fprintf(stderr, "FAIL: i2d_ASN1_INTEGER() failed\n");
+               goto failed;
+       }
+       if ((p = calloc(1, len)) == NULL)
+               errx(1, "calloc");
+       pp = p;
+       if ((len = i2d_ASN1_INTEGER(aint, &pp)) < 0) {
+               fprintf(stderr, "FAIL: i2d_ASN1_INTEGER() failed\n");
+               goto failed;
+       }
+       if (!asn1_compare_bytes("INTEGER NULL data", p, len, der, sizeof(der)))
+               goto failed;
+
+       failed = 0;
+
+ failed:
+       ASN1_INTEGER_free(aint);
+       free(p);
+
+       return failed;
+}
+
 static int
 asn1_integer_test(void)
 {
@@ -694,6 +730,7 @@ asn1_integer_test(void)
        }
 
        failed |= asn1_integer_cmp_test();
+       failed |= asn1_integer_null_data_test();
        failed |= asn1_integer_set_val_test();
 
        return failed;