isaacpittman-hitachi / rpms / openssl

Forked from rpms/openssl 2 years ago
Clone

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

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