summaryrefslogtreecommitdiff
path: root/test/asn1_encode_test.c
diff options
context:
space:
mode:
authorMatt Caswell <matt@openssl.org>2020-11-30 14:46:47 +0000
committerMatt Caswell <matt@openssl.org>2020-12-08 10:17:03 +0000
commit22b88fc9c0e22545401c0b34d24843883ea73fec (patch)
treecebba9d6f899d265df56c2aec21482177662fe34 /test/asn1_encode_test.c
parent97ab3c4b538840037812c8d9164d09a1f4bf11a1 (diff)
downloadopenssl-new-22b88fc9c0e22545401c0b34d24843883ea73fec.tar.gz
Add a test for encoding/decoding using an invalid ASN.1 Template
If you have a CHOICE type that it must use explicit tagging - otherwise the template is invalid. We add tests for this. Reviewed-by: Tomas Mraz <tmraz@fedoraproject.org>
Diffstat (limited to 'test/asn1_encode_test.c')
-rw-r--r--test/asn1_encode_test.c33
1 files changed, 33 insertions, 0 deletions
diff --git a/test/asn1_encode_test.c b/test/asn1_encode_test.c
index 4ff8a20ff4..27f522b222 100644
--- a/test/asn1_encode_test.c
+++ b/test/asn1_encode_test.c
@@ -856,6 +856,38 @@ static int test_uint64(void)
return test_intern(&uint64_test_package);
}
+typedef struct {
+ ASN1_STRING *invalidDirString;
+} INVALIDTEMPLATE;
+
+ASN1_SEQUENCE(INVALIDTEMPLATE) = {
+ /*
+ * DirectoryString is a CHOICE type so it must use explicit tagging -
+ * but we deliberately use implicit here, which makes this template invalid.
+ */
+ ASN1_IMP(INVALIDTEMPLATE, invalidDirString, DIRECTORYSTRING, 12)
+} static_ASN1_SEQUENCE_END(INVALIDTEMPLATE)
+
+IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(INVALIDTEMPLATE)
+IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(INVALIDTEMPLATE)
+
+static int test_invalid_template(void)
+{
+ INVALIDTEMPLATE *temp = INVALIDTEMPLATE_new();
+ int ret;
+
+ if (!TEST_ptr(temp))
+ return 0;
+
+ ret = i2d_INVALIDTEMPLATE(temp, NULL);
+
+ INVALIDTEMPLATE_free(temp);
+
+ /* We expect the i2d operation to fail */
+ return ret < 0;
+}
+
+
int setup_tests(void)
{
#ifndef OPENSSL_NO_DEPRECATED_3_0
@@ -866,5 +898,6 @@ int setup_tests(void)
ADD_TEST(test_uint32);
ADD_TEST(test_int64);
ADD_TEST(test_uint64);
+ ADD_TEST(test_invalid_template);
return 1;
}