Blame SPECS/openssl-1.1.1-CVE-2020-1971.patch

Packit Service f3830c
diff --git a/crypto/asn1/asn1_err.c b/crypto/asn1/asn1_err.c
Packit Service f3830c
index 613f9ae713..cc0a59ca4c 100644
Packit Service f3830c
--- a/crypto/asn1/asn1_err.c
Packit Service f3830c
+++ b/crypto/asn1/asn1_err.c
Packit Service f3830c
@@ -1,6 +1,6 @@
Packit Service f3830c
 /*
Packit Service f3830c
  * Generated by util/mkerr.pl DO NOT EDIT
Packit Service f3830c
- * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
Packit Service f3830c
+ * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
Packit Service f3830c
  *
Packit Service f3830c
  * Licensed under the OpenSSL license (the "License").  You may not use
Packit Service f3830c
  * this file except in compliance with the License.  You can obtain a copy
Packit Service f3830c
@@ -49,6 +49,7 @@ static const ERR_STRING_DATA ASN1_str_functs[] = {
Packit Service f3830c
      "asn1_item_embed_d2i"},
Packit Service f3830c
     {ERR_PACK(ERR_LIB_ASN1, ASN1_F_ASN1_ITEM_EMBED_NEW, 0),
Packit Service f3830c
      "asn1_item_embed_new"},
Packit Service f3830c
+    {ERR_PACK(ERR_LIB_ASN1, ASN1_F_ASN1_ITEM_EX_I2D, 0), "ASN1_item_ex_i2d"},
Packit Service f3830c
     {ERR_PACK(ERR_LIB_ASN1, ASN1_F_ASN1_ITEM_FLAGS_I2D, 0),
Packit Service f3830c
      "asn1_item_flags_i2d"},
Packit Service f3830c
     {ERR_PACK(ERR_LIB_ASN1, ASN1_F_ASN1_ITEM_I2D_BIO, 0), "ASN1_item_i2d_bio"},
Packit Service f3830c
@@ -160,6 +161,7 @@ static const ERR_STRING_DATA ASN1_str_reasons[] = {
Packit Service f3830c
     "asn1 sig parse error"},
Packit Service f3830c
     {ERR_PACK(ERR_LIB_ASN1, 0, ASN1_R_AUX_ERROR), "aux error"},
Packit Service f3830c
     {ERR_PACK(ERR_LIB_ASN1, 0, ASN1_R_BAD_OBJECT_HEADER), "bad object header"},
Packit Service f3830c
+    {ERR_PACK(ERR_LIB_ASN1, 0, ASN1_R_BAD_TEMPLATE), "bad template"},
Packit Service f3830c
     {ERR_PACK(ERR_LIB_ASN1, 0, ASN1_R_BMPSTRING_IS_WRONG_LENGTH),
Packit Service f3830c
     "bmpstring is wrong length"},
Packit Service f3830c
     {ERR_PACK(ERR_LIB_ASN1, 0, ASN1_R_BN_LIB), "bn lib"},
Packit Service f3830c
diff --git a/crypto/asn1/tasn_dec.c b/crypto/asn1/tasn_dec.c
Packit Service f3830c
index 2332b204ed..1021705f43 100644
Packit Service f3830c
--- a/crypto/asn1/tasn_dec.c
Packit Service f3830c
+++ b/crypto/asn1/tasn_dec.c
Packit Service f3830c
@@ -182,6 +182,15 @@ static int asn1_item_embed_d2i(ASN1_VALUE **pval, const unsigned char **in,
Packit Service f3830c
                                      tag, aclass, opt, ctx);
Packit Service f3830c
 
Packit Service f3830c
     case ASN1_ITYPE_MSTRING:
Packit Service f3830c
+        /*
Packit Service f3830c
+         * It never makes sense for multi-strings to have implicit tagging, so
Packit Service f3830c
+         * if tag != -1, then this looks like an error in the template.
Packit Service f3830c
+         */
Packit Service f3830c
+        if (tag != -1) {
Packit Service f3830c
+            ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_BAD_TEMPLATE);
Packit Service f3830c
+            goto err;
Packit Service f3830c
+        }
Packit Service f3830c
+
Packit Service f3830c
         p = *in;
Packit Service f3830c
         /* Just read in tag and class */
Packit Service f3830c
         ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
Packit Service f3830c
@@ -199,6 +208,7 @@ static int asn1_item_embed_d2i(ASN1_VALUE **pval, const unsigned char **in,
Packit Service f3830c
             ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_MSTRING_NOT_UNIVERSAL);
Packit Service f3830c
             goto err;
Packit Service f3830c
         }
Packit Service f3830c
+
Packit Service f3830c
         /* Check tag matches bit map */
Packit Service f3830c
         if (!(ASN1_tag2bit(otag) & it->utype)) {
Packit Service f3830c
             /* If OPTIONAL, assume this is OK */
Packit Service f3830c
@@ -215,6 +225,15 @@ static int asn1_item_embed_d2i(ASN1_VALUE **pval, const unsigned char **in,
Packit Service f3830c
         return ef->asn1_ex_d2i(pval, in, len, it, tag, aclass, opt, ctx);
Packit Service f3830c
 
Packit Service f3830c
     case ASN1_ITYPE_CHOICE:
Packit Service f3830c
+        /*
Packit Service f3830c
+         * It never makes sense for CHOICE types to have implicit tagging, so
Packit Service f3830c
+         * if tag != -1, then this looks like an error in the template.
Packit Service f3830c
+         */
Packit Service f3830c
+        if (tag != -1) {
Packit Service f3830c
+            ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_BAD_TEMPLATE);
Packit Service f3830c
+            goto err;
Packit Service f3830c
+        }
Packit Service f3830c
+
Packit Service f3830c
         if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
Packit Service f3830c
             goto auxerr;
Packit Service f3830c
         if (*pval) {
Packit Service f3830c
diff --git a/crypto/asn1/tasn_enc.c b/crypto/asn1/tasn_enc.c
Packit Service f3830c
index d600c7a538..52a051d5b1 100644
Packit Service f3830c
--- a/crypto/asn1/tasn_enc.c
Packit Service f3830c
+++ b/crypto/asn1/tasn_enc.c
Packit Service f3830c
@@ -103,9 +103,25 @@ int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
Packit Service f3830c
         return asn1_i2d_ex_primitive(pval, out, it, tag, aclass);
Packit Service f3830c
 
Packit Service f3830c
     case ASN1_ITYPE_MSTRING:
Packit Service f3830c
+        /*
Packit Service f3830c
+         * It never makes sense for multi-strings to have implicit tagging, so
Packit Service f3830c
+         * if tag != -1, then this looks like an error in the template.
Packit Service f3830c
+         */
Packit Service f3830c
+        if (tag != -1) {
Packit Service f3830c
+            ASN1err(ASN1_F_ASN1_ITEM_EX_I2D, ASN1_R_BAD_TEMPLATE);
Packit Service f3830c
+            return -1;
Packit Service f3830c
+        }
Packit Service f3830c
         return asn1_i2d_ex_primitive(pval, out, it, -1, aclass);
Packit Service f3830c
 
Packit Service f3830c
     case ASN1_ITYPE_CHOICE:
Packit Service f3830c
+        /*
Packit Service f3830c
+         * It never makes sense for CHOICE types to have implicit tagging, so
Packit Service f3830c
+         * if tag != -1, then this looks like an error in the template.
Packit Service f3830c
+         */
Packit Service f3830c
+        if (tag != -1) {
Packit Service f3830c
+            ASN1err(ASN1_F_ASN1_ITEM_EX_I2D, ASN1_R_BAD_TEMPLATE);
Packit Service f3830c
+            return -1;
Packit Service f3830c
+        }
Packit Service f3830c
         if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it, NULL))
Packit Service f3830c
             return 0;
Packit Service f3830c
         i = asn1_get_choice_selector(pval, it);
Packit Service f3830c
diff --git a/crypto/err/openssl.txt b/crypto/err/openssl.txt
Packit Service f3830c
index 0b5873ebbc..815460b24f 100644
Packit Service f3830c
--- a/crypto/err/openssl.txt
Packit Service f3830c
+++ b/crypto/err/openssl.txt
Packit Service f3830c
@@ -36,6 +36,7 @@ ASN1_F_ASN1_ITEM_D2I_FP:206:ASN1_item_d2i_fp
Packit Service f3830c
 ASN1_F_ASN1_ITEM_DUP:191:ASN1_item_dup
Packit Service f3830c
 ASN1_F_ASN1_ITEM_EMBED_D2I:120:asn1_item_embed_d2i
Packit Service f3830c
 ASN1_F_ASN1_ITEM_EMBED_NEW:121:asn1_item_embed_new
Packit Service f3830c
+ASN1_F_ASN1_ITEM_EX_I2D:144:ASN1_item_ex_i2d
Packit Service f3830c
 ASN1_F_ASN1_ITEM_FLAGS_I2D:118:asn1_item_flags_i2d
Packit Service f3830c
 ASN1_F_ASN1_ITEM_I2D_BIO:192:ASN1_item_i2d_bio
Packit Service f3830c
 ASN1_F_ASN1_ITEM_I2D_FP:193:ASN1_item_i2d_fp
Packit Service f3830c
@@ -1771,6 +1772,7 @@ ASN1_R_ASN1_PARSE_ERROR:203:asn1 parse error
Packit Service f3830c
 ASN1_R_ASN1_SIG_PARSE_ERROR:204:asn1 sig parse error
Packit Service f3830c
 ASN1_R_AUX_ERROR:100:aux error
Packit Service f3830c
 ASN1_R_BAD_OBJECT_HEADER:102:bad object header
Packit Service f3830c
+ASN1_R_BAD_TEMPLATE:230:bad template
Packit Service f3830c
 ASN1_R_BMPSTRING_IS_WRONG_LENGTH:214:bmpstring is wrong length
Packit Service f3830c
 ASN1_R_BN_LIB:105:bn lib
Packit Service f3830c
 ASN1_R_BOOLEAN_IS_WRONG_LENGTH:106:boolean is wrong length
Packit Service f3830c
diff --git a/crypto/x509v3/v3_genn.c b/crypto/x509v3/v3_genn.c
Packit Service f3830c
index 23e3bc4565..6f0a347cce 100644
Packit Service f3830c
--- a/crypto/x509v3/v3_genn.c
Packit Service f3830c
+++ b/crypto/x509v3/v3_genn.c
Packit Service f3830c
@@ -22,8 +22,9 @@ ASN1_SEQUENCE(OTHERNAME) = {
Packit Service f3830c
 IMPLEMENT_ASN1_FUNCTIONS(OTHERNAME)
Packit Service f3830c
 
Packit Service f3830c
 ASN1_SEQUENCE(EDIPARTYNAME) = {
Packit Service f3830c
-        ASN1_IMP_OPT(EDIPARTYNAME, nameAssigner, DIRECTORYSTRING, 0),
Packit Service f3830c
-        ASN1_IMP_OPT(EDIPARTYNAME, partyName, DIRECTORYSTRING, 1)
Packit Service f3830c
+        /* DirectoryString is a CHOICE type so use explicit tagging */
Packit Service f3830c
+        ASN1_EXP_OPT(EDIPARTYNAME, nameAssigner, DIRECTORYSTRING, 0),
Packit Service f3830c
+        ASN1_EXP(EDIPARTYNAME, partyName, DIRECTORYSTRING, 1)
Packit Service f3830c
 } ASN1_SEQUENCE_END(EDIPARTYNAME)
Packit Service f3830c
 
Packit Service f3830c
 IMPLEMENT_ASN1_FUNCTIONS(EDIPARTYNAME)
Packit Service f3830c
@@ -57,6 +58,37 @@ GENERAL_NAME *GENERAL_NAME_dup(GENERAL_NAME *a)
Packit Service f3830c
                                     (char *)a);
Packit Service f3830c
 }
Packit Service f3830c
 
Packit Service f3830c
+static int edipartyname_cmp(const EDIPARTYNAME *a, const EDIPARTYNAME *b)
Packit Service f3830c
+{
Packit Service f3830c
+    int res;
Packit Service f3830c
+
Packit Service f3830c
+    if (a == NULL || b == NULL) {
Packit Service f3830c
+        /*
Packit Service f3830c
+         * Shouldn't be possible in a valid GENERAL_NAME, but we handle it
Packit Service f3830c
+         * anyway. OTHERNAME_cmp treats NULL != NULL so we do the same here
Packit Service f3830c
+         */
Packit Service f3830c
+        return -1;
Packit Service f3830c
+    }
Packit Service f3830c
+    if (a->nameAssigner == NULL && b->nameAssigner != NULL)
Packit Service f3830c
+        return -1;
Packit Service f3830c
+    if (a->nameAssigner != NULL && b->nameAssigner == NULL)
Packit Service f3830c
+        return 1;
Packit Service f3830c
+    /* If we get here then both have nameAssigner set, or both unset */
Packit Service f3830c
+    if (a->nameAssigner != NULL) {
Packit Service f3830c
+        res = ASN1_STRING_cmp(a->nameAssigner, b->nameAssigner);
Packit Service f3830c
+        if (res != 0)
Packit Service f3830c
+            return res;
Packit Service f3830c
+    }
Packit Service f3830c
+    /*
Packit Service f3830c
+     * partyName is required, so these should never be NULL. We treat it in
Packit Service f3830c
+     * the same way as the a == NULL || b == NULL case above
Packit Service f3830c
+     */
Packit Service f3830c
+    if (a->partyName == NULL || b->partyName == NULL)
Packit Service f3830c
+        return -1;
Packit Service f3830c
+
Packit Service f3830c
+    return ASN1_STRING_cmp(a->partyName, b->partyName);
Packit Service f3830c
+}
Packit Service f3830c
+
Packit Service f3830c
 /* Returns 0 if they are equal, != 0 otherwise. */
Packit Service f3830c
 int GENERAL_NAME_cmp(GENERAL_NAME *a, GENERAL_NAME *b)
Packit Service f3830c
 {
Packit Service f3830c
@@ -66,8 +98,11 @@ int GENERAL_NAME_cmp(GENERAL_NAME *a, GENERAL_NAME *b)
Packit Service f3830c
         return -1;
Packit Service f3830c
     switch (a->type) {
Packit Service f3830c
     case GEN_X400:
Packit Service f3830c
+        result = ASN1_TYPE_cmp(a->d.x400Address, b->d.x400Address);
Packit Service f3830c
+        break;
Packit Service f3830c
+
Packit Service f3830c
     case GEN_EDIPARTY:
Packit Service f3830c
-        result = ASN1_TYPE_cmp(a->d.other, b->d.other);
Packit Service f3830c
+        result = edipartyname_cmp(a->d.ediPartyName, b->d.ediPartyName);
Packit Service f3830c
         break;
Packit Service f3830c
 
Packit Service f3830c
     case GEN_OTHERNAME:
Packit Service f3830c
@@ -114,8 +149,11 @@ void GENERAL_NAME_set0_value(GENERAL_NAME *a, int type, void *value)
Packit Service f3830c
 {
Packit Service f3830c
     switch (type) {
Packit Service f3830c
     case GEN_X400:
Packit Service f3830c
+        a->d.x400Address = value;
Packit Service f3830c
+        break;
Packit Service f3830c
+
Packit Service f3830c
     case GEN_EDIPARTY:
Packit Service f3830c
-        a->d.other = value;
Packit Service f3830c
+        a->d.ediPartyName = value;
Packit Service f3830c
         break;
Packit Service f3830c
 
Packit Service f3830c
     case GEN_OTHERNAME:
Packit Service f3830c
@@ -149,8 +187,10 @@ void *GENERAL_NAME_get0_value(const GENERAL_NAME *a, int *ptype)
Packit Service f3830c
         *ptype = a->type;
Packit Service f3830c
     switch (a->type) {
Packit Service f3830c
     case GEN_X400:
Packit Service f3830c
+        return a->d.x400Address;
Packit Service f3830c
+
Packit Service f3830c
     case GEN_EDIPARTY:
Packit Service f3830c
-        return a->d.other;
Packit Service f3830c
+        return a->d.ediPartyName;
Packit Service f3830c
 
Packit Service f3830c
     case GEN_OTHERNAME:
Packit Service f3830c
         return a->d.otherName;
Packit Service f3830c
diff --git a/include/openssl/asn1err.h b/include/openssl/asn1err.h
Packit Service f3830c
index faed5a5518..e1ad1fefec 100644
Packit Service f3830c
--- a/include/openssl/asn1err.h
Packit Service f3830c
+++ b/include/openssl/asn1err.h
Packit Service f3830c
@@ -1,6 +1,6 @@
Packit Service f3830c
 /*
Packit Service f3830c
  * Generated by util/mkerr.pl DO NOT EDIT
Packit Service f3830c
- * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
Packit Service f3830c
+ * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
Packit Service f3830c
  *
Packit Service f3830c
  * Licensed under the OpenSSL license (the "License").  You may not use
Packit Service f3830c
  * this file except in compliance with the License.  You can obtain a copy
Packit Service f3830c
@@ -11,9 +11,7 @@
Packit Service f3830c
 #ifndef HEADER_ASN1ERR_H
Packit Service f3830c
 # define HEADER_ASN1ERR_H
Packit Service f3830c
 
Packit Service f3830c
-# ifndef HEADER_SYMHACKS_H
Packit Service f3830c
-#  include <openssl/symhacks.h>
Packit Service f3830c
-# endif
Packit Service f3830c
+# include <openssl/symhacks.h>
Packit Service f3830c
 
Packit Service f3830c
 # ifdef  __cplusplus
Packit Service f3830c
 extern "C"
Packit Service f3830c
@@ -53,6 +51,7 @@ int ERR_load_ASN1_strings(void);
Packit Service f3830c
 # define ASN1_F_ASN1_ITEM_DUP                             191
Packit Service f3830c
 # define ASN1_F_ASN1_ITEM_EMBED_D2I                       120
Packit Service f3830c
 # define ASN1_F_ASN1_ITEM_EMBED_NEW                       121
Packit Service f3830c
+# define ASN1_F_ASN1_ITEM_EX_I2D                          144
Packit Service f3830c
 # define ASN1_F_ASN1_ITEM_FLAGS_I2D                       118
Packit Service f3830c
 # define ASN1_F_ASN1_ITEM_I2D_BIO                         192
Packit Service f3830c
 # define ASN1_F_ASN1_ITEM_I2D_FP                          193
Packit Service f3830c
@@ -145,6 +144,7 @@ int ERR_load_ASN1_strings(void);
Packit Service f3830c
 # define ASN1_R_ASN1_SIG_PARSE_ERROR                      204
Packit Service f3830c
 # define ASN1_R_AUX_ERROR                                 100
Packit Service f3830c
 # define ASN1_R_BAD_OBJECT_HEADER                         102
Packit Service f3830c
+# define ASN1_R_BAD_TEMPLATE                              230
Packit Service f3830c
 # define ASN1_R_BMPSTRING_IS_WRONG_LENGTH                 214
Packit Service f3830c
 # define ASN1_R_BN_LIB                                    105
Packit Service f3830c
 # define ASN1_R_BOOLEAN_IS_WRONG_LENGTH                   106
Packit Service f3830c
diff --git a/test/asn1_decode_test.c b/test/asn1_decode_test.c
Packit Service f3830c
index 369023d5f1..94a22c6682 100644
Packit Service f3830c
--- a/test/asn1_decode_test.c
Packit Service f3830c
+++ b/test/asn1_decode_test.c
Packit Service f3830c
@@ -160,6 +160,41 @@ static int test_uint64(void)
Packit Service f3830c
     return 1;
Packit Service f3830c
 }
Packit Service f3830c
 
Packit Service f3830c
+typedef struct {
Packit Service f3830c
+    ASN1_STRING *invalidDirString;
Packit Service f3830c
+} INVALIDTEMPLATE;
Packit Service f3830c
+
Packit Service f3830c
+ASN1_SEQUENCE(INVALIDTEMPLATE) = {
Packit Service f3830c
+    /*
Packit Service f3830c
+     * DirectoryString is a CHOICE type so it must use explicit tagging -
Packit Service f3830c
+     * but we deliberately use implicit here, which makes this template invalid.
Packit Service f3830c
+     */
Packit Service f3830c
+    ASN1_IMP(INVALIDTEMPLATE, invalidDirString, DIRECTORYSTRING, 12)
Packit Service f3830c
+} static_ASN1_SEQUENCE_END(INVALIDTEMPLATE)
Packit Service f3830c
+
Packit Service f3830c
+IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(INVALIDTEMPLATE)
Packit Service f3830c
+IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(INVALIDTEMPLATE)
Packit Service f3830c
+
Packit Service f3830c
+/* Empty sequence for invalid template test */
Packit Service f3830c
+static unsigned char t_invalid_template[] = {
Packit Service f3830c
+    0x30, 0x03,                  /* SEQUENCE tag + length */
Packit Service f3830c
+    0x0c, 0x01, 0x41             /* UTF8String, length 1, "A" */
Packit Service f3830c
+};
Packit Service f3830c
+
Packit Service f3830c
+static int test_invalid_template(void)
Packit Service f3830c
+{
Packit Service f3830c
+    const unsigned char *p = t_invalid_template;
Packit Service f3830c
+    INVALIDTEMPLATE *tmp = d2i_INVALIDTEMPLATE(NULL, &p,
Packit Service f3830c
+                                               sizeof(t_invalid_template));
Packit Service f3830c
+
Packit Service f3830c
+    /* We expect a NULL pointer return */
Packit Service f3830c
+    if (TEST_ptr_null(tmp))
Packit Service f3830c
+        return 1;
Packit Service f3830c
+
Packit Service f3830c
+    INVALIDTEMPLATE_free(tmp);
Packit Service f3830c
+    return 0;
Packit Service f3830c
+}
Packit Service f3830c
+
Packit Service f3830c
 int setup_tests(void)
Packit Service f3830c
 {
Packit Service f3830c
 #if OPENSSL_API_COMPAT < 0x10200000L
Packit Service f3830c
@@ -169,5 +204,6 @@ int setup_tests(void)
Packit Service f3830c
     ADD_TEST(test_uint32);
Packit Service f3830c
     ADD_TEST(test_int64);
Packit Service f3830c
     ADD_TEST(test_uint64);
Packit Service f3830c
+    ADD_TEST(test_invalid_template);
Packit Service f3830c
     return 1;
Packit Service f3830c
 }
Packit Service f3830c
diff --git a/test/asn1_encode_test.c b/test/asn1_encode_test.c
Packit Service f3830c
index ed920a4d66..afbd18be6f 100644
Packit Service f3830c
--- a/test/asn1_encode_test.c
Packit Service f3830c
+++ b/test/asn1_encode_test.c
Packit Service f3830c
@@ -856,6 +856,38 @@ static int test_uint64(void)
Packit Service f3830c
     return test_intern(&uint64_test_package);
Packit Service f3830c
 }
Packit Service f3830c
 
Packit Service f3830c
+typedef struct {
Packit Service f3830c
+    ASN1_STRING *invalidDirString;
Packit Service f3830c
+} INVALIDTEMPLATE;
Packit Service f3830c
+
Packit Service f3830c
+ASN1_SEQUENCE(INVALIDTEMPLATE) = {
Packit Service f3830c
+    /*
Packit Service f3830c
+     * DirectoryString is a CHOICE type so it must use explicit tagging -
Packit Service f3830c
+     * but we deliberately use implicit here, which makes this template invalid.
Packit Service f3830c
+     */
Packit Service f3830c
+    ASN1_IMP(INVALIDTEMPLATE, invalidDirString, DIRECTORYSTRING, 12)
Packit Service f3830c
+} static_ASN1_SEQUENCE_END(INVALIDTEMPLATE)
Packit Service f3830c
+
Packit Service f3830c
+IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(INVALIDTEMPLATE)
Packit Service f3830c
+IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(INVALIDTEMPLATE)
Packit Service f3830c
+
Packit Service f3830c
+static int test_invalid_template(void)
Packit Service f3830c
+{
Packit Service f3830c
+    INVALIDTEMPLATE *temp = INVALIDTEMPLATE_new();
Packit Service f3830c
+    int ret;
Packit Service f3830c
+
Packit Service f3830c
+    if (!TEST_ptr(temp))
Packit Service f3830c
+        return 0;
Packit Service f3830c
+
Packit Service f3830c
+    ret = i2d_INVALIDTEMPLATE(temp, NULL);
Packit Service f3830c
+
Packit Service f3830c
+    INVALIDTEMPLATE_free(temp);
Packit Service f3830c
+
Packit Service f3830c
+    /* We expect the i2d operation to fail */
Packit Service f3830c
+    return ret < 0;
Packit Service f3830c
+}
Packit Service f3830c
+
Packit Service f3830c
+
Packit Service f3830c
 int setup_tests(void)
Packit Service f3830c
 {
Packit Service f3830c
 #if OPENSSL_API_COMPAT < 0x10200000L
Packit Service f3830c
@@ -866,5 +898,6 @@ int setup_tests(void)
Packit Service f3830c
     ADD_TEST(test_uint32);
Packit Service f3830c
     ADD_TEST(test_int64);
Packit Service f3830c
     ADD_TEST(test_uint64);
Packit Service f3830c
+    ADD_TEST(test_invalid_template);
Packit Service f3830c
     return 1;
Packit Service f3830c
 }
Packit Service f3830c
diff --git a/test/v3nametest.c b/test/v3nametest.c
Packit Service f3830c
index 86f3829aed..4c8af92ce9 100644
Packit Service f3830c
--- a/test/v3nametest.c
Packit Service f3830c
+++ b/test/v3nametest.c
Packit Service f3830c
@@ -359,8 +359,352 @@ static int call_run_cert(int i)
Packit Service f3830c
     return failed == 0;
Packit Service f3830c
 }
Packit Service f3830c
 
Packit Service f3830c
+struct gennamedata {
Packit Service f3830c
+    const unsigned char der[22];
Packit Service f3830c
+    size_t derlen;
Packit Service f3830c
+} gennames[] = {
Packit Service f3830c
+    {
Packit Service f3830c
+        /*
Packit Service f3830c
+        * [0] {
Packit Service f3830c
+        *   OBJECT_IDENTIFIER { 1.2.840.113554.4.1.72585.2.1 }
Packit Service f3830c
+        *   [0] {
Packit Service f3830c
+        *     SEQUENCE {}
Packit Service f3830c
+        *   }
Packit Service f3830c
+        * }
Packit Service f3830c
+        */
Packit Service f3830c
+        {
Packit Service f3830c
+            0xa0, 0x13, 0x06, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04,
Packit Service f3830c
+            0x01, 0x84, 0xb7, 0x09, 0x02, 0x01, 0xa0, 0x02, 0x30, 0x00
Packit Service f3830c
+        },
Packit Service f3830c
+        21
Packit Service f3830c
+    }, {
Packit Service f3830c
+        /*
Packit Service f3830c
+        * [0] {
Packit Service f3830c
+        *   OBJECT_IDENTIFIER { 1.2.840.113554.4.1.72585.2.1 }
Packit Service f3830c
+        *   [0] {
Packit Service f3830c
+        *     [APPLICATION 0] {}
Packit Service f3830c
+        *   }
Packit Service f3830c
+        * }
Packit Service f3830c
+        */
Packit Service f3830c
+        {
Packit Service f3830c
+            0xa0, 0x13, 0x06, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04,
Packit Service f3830c
+            0x01, 0x84, 0xb7, 0x09, 0x02, 0x01, 0xa0, 0x02, 0x60, 0x00
Packit Service f3830c
+        },
Packit Service f3830c
+        21
Packit Service f3830c
+    }, {
Packit Service f3830c
+        /*
Packit Service f3830c
+        * [0] {
Packit Service f3830c
+        *   OBJECT_IDENTIFIER { 1.2.840.113554.4.1.72585.2.1 }
Packit Service f3830c
+        *   [0] {
Packit Service f3830c
+        *     UTF8String { "a" }
Packit Service f3830c
+        *   }
Packit Service f3830c
+        * }
Packit Service f3830c
+        */
Packit Service f3830c
+        {
Packit Service f3830c
+            0xa0, 0x14, 0x06, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04,
Packit Service f3830c
+            0x01, 0x84, 0xb7, 0x09, 0x02, 0x01, 0xa0, 0x03, 0x0c, 0x01, 0x61
Packit Service f3830c
+        },
Packit Service f3830c
+        22
Packit Service f3830c
+    }, {
Packit Service f3830c
+        /*
Packit Service f3830c
+        * [0] {
Packit Service f3830c
+        *   OBJECT_IDENTIFIER { 1.2.840.113554.4.1.72585.2.2 }
Packit Service f3830c
+        *   [0] {
Packit Service f3830c
+        *     UTF8String { "a" }
Packit Service f3830c
+        *   }
Packit Service f3830c
+        * }
Packit Service f3830c
+        */
Packit Service f3830c
+        {
Packit Service f3830c
+            0xa0, 0x14, 0x06, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04,
Packit Service f3830c
+            0x01, 0x84, 0xb7, 0x09, 0x02, 0x02, 0xa0, 0x03, 0x0c, 0x01, 0x61
Packit Service f3830c
+        },
Packit Service f3830c
+        22
Packit Service f3830c
+    }, {
Packit Service f3830c
+        /*
Packit Service f3830c
+        * [0] {
Packit Service f3830c
+        *   OBJECT_IDENTIFIER { 1.2.840.113554.4.1.72585.2.1 }
Packit Service f3830c
+        *   [0] {
Packit Service f3830c
+        *     UTF8String { "b" }
Packit Service f3830c
+        *   }
Packit Service f3830c
+        * }
Packit Service f3830c
+        */
Packit Service f3830c
+        {
Packit Service f3830c
+            0xa0, 0x14, 0x06, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04,
Packit Service f3830c
+            0x01, 0x84, 0xb7, 0x09, 0x02, 0x01, 0xa0, 0x03, 0x0c, 0x01, 0x62
Packit Service f3830c
+        },
Packit Service f3830c
+        22
Packit Service f3830c
+    }, {
Packit Service f3830c
+        /*
Packit Service f3830c
+        * [0] {
Packit Service f3830c
+        *   OBJECT_IDENTIFIER { 1.2.840.113554.4.1.72585.2.1 }
Packit Service f3830c
+        *   [0] {
Packit Service f3830c
+        *     BOOLEAN { TRUE }
Packit Service f3830c
+        *   }
Packit Service f3830c
+        * }
Packit Service f3830c
+        */
Packit Service f3830c
+        {
Packit Service f3830c
+            0xa0, 0x14, 0x06, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04,
Packit Service f3830c
+            0x01, 0x84, 0xb7, 0x09, 0x02, 0x01, 0xa0, 0x03, 0x01, 0x01, 0xff
Packit Service f3830c
+        },
Packit Service f3830c
+        22
Packit Service f3830c
+    }, {
Packit Service f3830c
+        /*
Packit Service f3830c
+        * [0] {
Packit Service f3830c
+        *   OBJECT_IDENTIFIER { 1.2.840.113554.4.1.72585.2.1 }
Packit Service f3830c
+        *   [0] {
Packit Service f3830c
+        *     BOOLEAN { FALSE }
Packit Service f3830c
+        *   }
Packit Service f3830c
+        * }
Packit Service f3830c
+        */
Packit Service f3830c
+        {
Packit Service f3830c
+            0xa0, 0x14, 0x06, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04,
Packit Service f3830c
+            0x01, 0x84, 0xb7, 0x09, 0x02, 0x01, 0xa0, 0x03, 0x01, 0x01, 0x00
Packit Service f3830c
+        },
Packit Service f3830c
+        22
Packit Service f3830c
+    }, {
Packit Service f3830c
+        /* [1 PRIMITIVE] { "a" } */
Packit Service f3830c
+        {
Packit Service f3830c
+            0x81, 0x01, 0x61
Packit Service f3830c
+        },
Packit Service f3830c
+        3
Packit Service f3830c
+    }, {
Packit Service f3830c
+        /* [1 PRIMITIVE] { "b" } */
Packit Service f3830c
+        {
Packit Service f3830c
+            0x81, 0x01, 0x62
Packit Service f3830c
+        },
Packit Service f3830c
+        3
Packit Service f3830c
+    }, {
Packit Service f3830c
+        /* [2 PRIMITIVE] { "a" } */
Packit Service f3830c
+        {
Packit Service f3830c
+            0x82, 0x01, 0x61
Packit Service f3830c
+        },
Packit Service f3830c
+        3
Packit Service f3830c
+    }, {
Packit Service f3830c
+        /* [2 PRIMITIVE] { "b" } */
Packit Service f3830c
+        {
Packit Service f3830c
+            0x82, 0x01, 0x62
Packit Service f3830c
+        },
Packit Service f3830c
+        3
Packit Service f3830c
+    }, {
Packit Service f3830c
+        /*
Packit Service f3830c
+        * [4] {
Packit Service f3830c
+        *   SEQUENCE {
Packit Service f3830c
+        *     SET {
Packit Service f3830c
+        *       SEQUENCE {
Packit Service f3830c
+        *         # commonName
Packit Service f3830c
+        *         OBJECT_IDENTIFIER { 2.5.4.3 }
Packit Service f3830c
+        *         UTF8String { "a" }
Packit Service f3830c
+        *       }
Packit Service f3830c
+        *     }
Packit Service f3830c
+        *   }
Packit Service f3830c
+        * }
Packit Service f3830c
+        */
Packit Service f3830c
+        {
Packit Service f3830c
+            0xa4, 0x0e, 0x30, 0x0c, 0x31, 0x0a, 0x30, 0x08, 0x06, 0x03, 0x55,
Packit Service f3830c
+            0x04, 0x03, 0x0c, 0x01, 0x61
Packit Service f3830c
+        },
Packit Service f3830c
+        16
Packit Service f3830c
+    }, {
Packit Service f3830c
+        /*
Packit Service f3830c
+        * [4] {
Packit Service f3830c
+        *   SEQUENCE {
Packit Service f3830c
+        *     SET {
Packit Service f3830c
+        *       SEQUENCE {
Packit Service f3830c
+        *         # commonName
Packit Service f3830c
+        *         OBJECT_IDENTIFIER { 2.5.4.3 }
Packit Service f3830c
+        *         UTF8String { "b" }
Packit Service f3830c
+        *       }
Packit Service f3830c
+        *     }
Packit Service f3830c
+        *   }
Packit Service f3830c
+        * }
Packit Service f3830c
+        */
Packit Service f3830c
+        {
Packit Service f3830c
+            0xa4, 0x0e, 0x30, 0x0c, 0x31, 0x0a, 0x30, 0x08, 0x06, 0x03, 0x55,
Packit Service f3830c
+            0x04, 0x03, 0x0c, 0x01, 0x62
Packit Service f3830c
+        },
Packit Service f3830c
+        16
Packit Service f3830c
+    }, {
Packit Service f3830c
+        /*
Packit Service f3830c
+        * [5] {
Packit Service f3830c
+        *   [1] {
Packit Service f3830c
+        *     UTF8String { "a" }
Packit Service f3830c
+        *   }
Packit Service f3830c
+        * }
Packit Service f3830c
+        */
Packit Service f3830c
+        {
Packit Service f3830c
+            0xa5, 0x05, 0xa1, 0x03, 0x0c, 0x01, 0x61
Packit Service f3830c
+        },
Packit Service f3830c
+        7
Packit Service f3830c
+    }, {
Packit Service f3830c
+        /*
Packit Service f3830c
+        * [5] {
Packit Service f3830c
+        *   [1] {
Packit Service f3830c
+        *     UTF8String { "b" }
Packit Service f3830c
+        *   }
Packit Service f3830c
+        * }
Packit Service f3830c
+        */
Packit Service f3830c
+        {
Packit Service f3830c
+            0xa5, 0x05, 0xa1, 0x03, 0x0c, 0x01, 0x62
Packit Service f3830c
+        },
Packit Service f3830c
+        7
Packit Service f3830c
+    }, {
Packit Service f3830c
+        /*
Packit Service f3830c
+        * [5] {
Packit Service f3830c
+        *   [0] {
Packit Service f3830c
+        *     UTF8String {}
Packit Service f3830c
+        *   }
Packit Service f3830c
+        *   [1] {
Packit Service f3830c
+        *     UTF8String { "a" }
Packit Service f3830c
+        *   }
Packit Service f3830c
+        * }
Packit Service f3830c
+        */
Packit Service f3830c
+        {
Packit Service f3830c
+            0xa5, 0x09, 0xa0, 0x02, 0x0c, 0x00, 0xa1, 0x03, 0x0c, 0x01, 0x61
Packit Service f3830c
+        },
Packit Service f3830c
+        11
Packit Service f3830c
+    }, {
Packit Service f3830c
+        /*
Packit Service f3830c
+        * [5] {
Packit Service f3830c
+        *   [0] {
Packit Service f3830c
+        *     UTF8String { "a" }
Packit Service f3830c
+        *   }
Packit Service f3830c
+        *   [1] {
Packit Service f3830c
+        *     UTF8String { "a" }
Packit Service f3830c
+        *   }
Packit Service f3830c
+        * }
Packit Service f3830c
+        */
Packit Service f3830c
+        {
Packit Service f3830c
+            0xa5, 0x0a, 0xa0, 0x03, 0x0c, 0x01, 0x61, 0xa1, 0x03, 0x0c, 0x01,
Packit Service f3830c
+            0x61
Packit Service f3830c
+        },
Packit Service f3830c
+        12
Packit Service f3830c
+    }, {
Packit Service f3830c
+        /*
Packit Service f3830c
+        * [5] {
Packit Service f3830c
+        *   [0] {
Packit Service f3830c
+        *     UTF8String { "b" }
Packit Service f3830c
+        *   }
Packit Service f3830c
+        *   [1] {
Packit Service f3830c
+        *     UTF8String { "a" }
Packit Service f3830c
+        *   }
Packit Service f3830c
+        * }
Packit Service f3830c
+        */
Packit Service f3830c
+        {
Packit Service f3830c
+            0xa5, 0x0a, 0xa0, 0x03, 0x0c, 0x01, 0x62, 0xa1, 0x03, 0x0c, 0x01,
Packit Service f3830c
+            0x61
Packit Service f3830c
+        },
Packit Service f3830c
+        12
Packit Service f3830c
+    }, {
Packit Service f3830c
+        /* [6 PRIMITIVE] { "a" } */
Packit Service f3830c
+        {
Packit Service f3830c
+            0x86, 0x01, 0x61
Packit Service f3830c
+        },
Packit Service f3830c
+        3
Packit Service f3830c
+    }, {
Packit Service f3830c
+        /* [6 PRIMITIVE] { "b" } */
Packit Service f3830c
+        {
Packit Service f3830c
+            0x86, 0x01, 0x62
Packit Service f3830c
+        },
Packit Service f3830c
+        3
Packit Service f3830c
+    }, {
Packit Service f3830c
+        /* [7 PRIMITIVE] { `11111111` } */
Packit Service f3830c
+        {
Packit Service f3830c
+            0x87, 0x04, 0x11, 0x11, 0x11, 0x11
Packit Service f3830c
+        },
Packit Service f3830c
+        6
Packit Service f3830c
+    }, {
Packit Service f3830c
+        /* [7 PRIMITIVE] { `22222222`} */
Packit Service f3830c
+        {
Packit Service f3830c
+            0x87, 0x04, 0x22, 0x22, 0x22, 0x22
Packit Service f3830c
+        },
Packit Service f3830c
+        6
Packit Service f3830c
+    }, {
Packit Service f3830c
+        /* [7 PRIMITIVE] { `11111111111111111111111111111111` } */
Packit Service f3830c
+        {
Packit Service f3830c
+            0x87, 0x10, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
Packit Service f3830c
+            0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11
Packit Service f3830c
+        },
Packit Service f3830c
+        18
Packit Service f3830c
+    }, {
Packit Service f3830c
+        /* [7 PRIMITIVE] { `22222222222222222222222222222222` } */
Packit Service f3830c
+        {
Packit Service f3830c
+            0x87, 0x10, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
Packit Service f3830c
+            0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22
Packit Service f3830c
+        },
Packit Service f3830c
+        18
Packit Service f3830c
+    }, {
Packit Service f3830c
+        /* [8 PRIMITIVE] { 1.2.840.113554.4.1.72585.2.1 } */
Packit Service f3830c
+        {
Packit Service f3830c
+            0x88, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84,
Packit Service f3830c
+            0xb7, 0x09, 0x02, 0x01
Packit Service f3830c
+        },
Packit Service f3830c
+        15
Packit Service f3830c
+    }, {
Packit Service f3830c
+        /* [8 PRIMITIVE] { 1.2.840.113554.4.1.72585.2.2 } */
Packit Service f3830c
+        {
Packit Service f3830c
+            0x88, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84,
Packit Service f3830c
+            0xb7, 0x09, 0x02, 0x02
Packit Service f3830c
+        },
Packit Service f3830c
+        15
Packit Service f3830c
+    }
Packit Service f3830c
+};
Packit Service f3830c
+
Packit Service f3830c
+static int test_GENERAL_NAME_cmp(void)
Packit Service f3830c
+{
Packit Service f3830c
+    size_t i, j;
Packit Service f3830c
+    GENERAL_NAME **namesa = OPENSSL_malloc(sizeof(*namesa)
Packit Service f3830c
+                                           * OSSL_NELEM(gennames));
Packit Service f3830c
+    GENERAL_NAME **namesb = OPENSSL_malloc(sizeof(*namesb)
Packit Service f3830c
+                                           * OSSL_NELEM(gennames));
Packit Service f3830c
+    int testresult = 0;
Packit Service f3830c
+
Packit Service f3830c
+    if (!TEST_ptr(namesa) || !TEST_ptr(namesb))
Packit Service f3830c
+        goto end;
Packit Service f3830c
+
Packit Service f3830c
+    for (i = 0; i < OSSL_NELEM(gennames); i++) {
Packit Service f3830c
+        const unsigned char *derp = gennames[i].der;
Packit Service f3830c
+
Packit Service f3830c
+        /*
Packit Service f3830c
+         * We create two versions of each GENERAL_NAME so that we ensure when
Packit Service f3830c
+         * we compare them they are always different pointers.
Packit Service f3830c
+         */
Packit Service f3830c
+        namesa[i] = d2i_GENERAL_NAME(NULL, &derp, gennames[i].derlen);
Packit Service f3830c
+        derp = gennames[i].der;
Packit Service f3830c
+        namesb[i] = d2i_GENERAL_NAME(NULL, &derp, gennames[i].derlen);
Packit Service f3830c
+        if (!TEST_ptr(namesa[i]) || !TEST_ptr(namesb[i]))
Packit Service f3830c
+            goto end;
Packit Service f3830c
+    }
Packit Service f3830c
+
Packit Service f3830c
+    /* Every name should be equal to itself and not equal to any others. */
Packit Service f3830c
+    for (i = 0; i < OSSL_NELEM(gennames); i++) {
Packit Service f3830c
+        for (j = 0; j < OSSL_NELEM(gennames); j++) {
Packit Service f3830c
+            if (i == j) {
Packit Service f3830c
+                if (!TEST_int_eq(GENERAL_NAME_cmp(namesa[i], namesb[j]), 0))
Packit Service f3830c
+                    goto end;
Packit Service f3830c
+            } else {
Packit Service f3830c
+                if (!TEST_int_ne(GENERAL_NAME_cmp(namesa[i], namesb[j]), 0))
Packit Service f3830c
+                    goto end;
Packit Service f3830c
+            }
Packit Service f3830c
+        }
Packit Service f3830c
+    }
Packit Service f3830c
+    testresult = 1;
Packit Service f3830c
+
Packit Service f3830c
+ end:
Packit Service f3830c
+    for (i = 0; i < OSSL_NELEM(gennames); i++) {
Packit Service f3830c
+        if (namesa != NULL)
Packit Service f3830c
+            GENERAL_NAME_free(namesa[i]);
Packit Service f3830c
+        if (namesb != NULL)
Packit Service f3830c
+            GENERAL_NAME_free(namesb[i]);
Packit Service f3830c
+    }
Packit Service f3830c
+    OPENSSL_free(namesa);
Packit Service f3830c
+    OPENSSL_free(namesb);
Packit Service f3830c
+
Packit Service f3830c
+    return testresult;
Packit Service f3830c
+}
Packit Service f3830c
+
Packit Service f3830c
 int setup_tests(void)
Packit Service f3830c
 {
Packit Service f3830c
     ADD_ALL_TESTS(call_run_cert, OSSL_NELEM(name_fns));
Packit Service f3830c
+    ADD_TEST(test_GENERAL_NAME_cmp);
Packit Service f3830c
     return 1;
Packit Service f3830c
 }