From ed3e01bfc26fc8b45757d9ece7961f4aa32331a3 Mon Sep 17 00:00:00 2001 From: jsing Date: Sun, 28 Aug 2022 17:59:57 +0000 Subject: [PATCH] Add regress test for the encoding of an ASN1_INTEGER with NULL data. --- regress/lib/libcrypto/asn1/asn1basic.c | 39 +++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/regress/lib/libcrypto/asn1/asn1basic.c b/regress/lib/libcrypto/asn1/asn1basic.c index f3b768407dc..9ab23e7849e 100644 --- a/regress/lib/libcrypto/asn1/asn1basic.c +++ b/regress/lib/libcrypto/asn1/asn1basic.c @@ -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 * @@ -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; -- 2.20.1