9cfd19
diff -up openssl-1.0.2k/crypto/asn1/asn1_err.c.null-dereference openssl-1.0.2k/crypto/asn1/asn1_err.c
9cfd19
--- openssl-1.0.2k/crypto/asn1/asn1_err.c.null-dereference	2020-12-04 10:08:08.506247597 +0100
9cfd19
+++ openssl-1.0.2k/crypto/asn1/asn1_err.c	2020-12-04 10:12:31.901956486 +0100
9cfd19
@@ -1,6 +1,6 @@
9cfd19
 /* crypto/asn1/asn1_err.c */
9cfd19
 /* ====================================================================
9cfd19
- * Copyright (c) 1999-2014 The OpenSSL Project.  All rights reserved.
9cfd19
+ * Copyright (c) 1999-2020 The OpenSSL Project.  All rights reserved.
9cfd19
  *
9cfd19
  * Redistribution and use in source and binary forms, with or without
9cfd19
  * modification, are permitted provided that the following conditions
9cfd19
@@ -103,6 +103,7 @@ static ERR_STRING_DATA ASN1_str_functs[]
9cfd19
     {ERR_FUNC(ASN1_F_ASN1_ITEM_DUP), "ASN1_item_dup"},
9cfd19
     {ERR_FUNC(ASN1_F_ASN1_ITEM_EX_COMBINE_NEW), "ASN1_ITEM_EX_COMBINE_NEW"},
9cfd19
     {ERR_FUNC(ASN1_F_ASN1_ITEM_EX_D2I), "ASN1_ITEM_EX_D2I"},
9cfd19
+	{ERR_PACK(ERR_LIB_ASN1, ASN1_F_ASN1_ITEM_EX_I2D, 0), "ASN1_item_ex_i2d"},
9cfd19
     {ERR_FUNC(ASN1_F_ASN1_ITEM_I2D_BIO), "ASN1_item_i2d_bio"},
9cfd19
     {ERR_FUNC(ASN1_F_ASN1_ITEM_I2D_FP), "ASN1_item_i2d_fp"},
9cfd19
     {ERR_FUNC(ASN1_F_ASN1_ITEM_PACK), "ASN1_item_pack"},
9cfd19
@@ -202,6 +203,7 @@ static ERR_STRING_DATA ASN1_str_reasons[
9cfd19
     {ERR_REASON(ASN1_R_AUX_ERROR), "aux error"},
9cfd19
     {ERR_REASON(ASN1_R_BAD_CLASS), "bad class"},
9cfd19
     {ERR_REASON(ASN1_R_BAD_OBJECT_HEADER), "bad object header"},
9cfd19
+	{ERR_PACK(ERR_LIB_ASN1, 0, ASN1_R_BAD_TEMPLATE), "bad template"},
9cfd19
     {ERR_REASON(ASN1_R_BAD_PASSWORD_READ), "bad password read"},
9cfd19
     {ERR_REASON(ASN1_R_BAD_TAG), "bad tag"},
9cfd19
     {ERR_REASON(ASN1_R_BMPSTRING_IS_WRONG_LENGTH),
9cfd19
diff -up openssl-1.0.2k/crypto/asn1/asn1.h.null-dereference openssl-1.0.2k/crypto/asn1/asn1.h
9cfd19
--- openssl-1.0.2k/crypto/asn1/asn1.h.null-dereference	2020-12-04 11:00:06.896637900 +0100
9cfd19
+++ openssl-1.0.2k/crypto/asn1/asn1.h	2020-12-04 11:04:47.079562987 +0100
9cfd19
@@ -1202,6 +1202,7 @@ void ERR_load_ASN1_strings(void);
9cfd19
 # define ASN1_F_ASN1_ITEM_DUP                             191
9cfd19
 # define ASN1_F_ASN1_ITEM_EX_COMBINE_NEW                  121
9cfd19
 # define ASN1_F_ASN1_ITEM_EX_D2I                          120
9cfd19
+# define ASN1_F_ASN1_ITEM_EX_I2D                          231
9cfd19
 # define ASN1_F_ASN1_ITEM_I2D_BIO                         192
9cfd19
 # define ASN1_F_ASN1_ITEM_I2D_FP                          193
9cfd19
 # define ASN1_F_ASN1_ITEM_PACK                            198
9cfd19
@@ -1298,6 +1299,7 @@ void ERR_load_ASN1_strings(void);
9cfd19
 # define ASN1_R_AUX_ERROR                                 100
9cfd19
 # define ASN1_R_BAD_CLASS                                 101
9cfd19
 # define ASN1_R_BAD_OBJECT_HEADER                         102
9cfd19
+# define ASN1_R_BAD_TEMPLATE                              230
9cfd19
 # define ASN1_R_BAD_PASSWORD_READ                         103
9cfd19
 # define ASN1_R_BAD_TAG                                   104
9cfd19
 # define ASN1_R_BMPSTRING_IS_WRONG_LENGTH                 214
9cfd19
diff -up openssl-1.0.2k/crypto/asn1/tasn_dec.c.null-dereference openssl-1.0.2k/crypto/asn1/tasn_dec.c
9cfd19
--- openssl-1.0.2k/crypto/asn1/tasn_dec.c.null-dereference	2020-12-04 10:12:42.036057323 +0100
9cfd19
+++ openssl-1.0.2k/crypto/asn1/tasn_dec.c	2020-12-04 10:17:45.685035333 +0100
9cfd19
@@ -223,6 +223,15 @@ static int asn1_item_ex_d2i(ASN1_VALUE *
9cfd19
         break;
9cfd19
 
9cfd19
     case ASN1_ITYPE_MSTRING:
9cfd19
+        /*
9cfd19
+         * It never makes sense for multi-strings to have implicit tagging, so
9cfd19
+         * if tag != -1, then this looks like an error in the template.
9cfd19
+         */
9cfd19
+        if (tag != -1) {
9cfd19
+            ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_BAD_TEMPLATE);
9cfd19
+            goto err;
9cfd19
+        }
9cfd19
+
9cfd19
         p = *in;
9cfd19
         /* Just read in tag and class */
9cfd19
         ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
9cfd19
@@ -240,6 +249,7 @@ static int asn1_item_ex_d2i(ASN1_VALUE *
9cfd19
             ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_MSTRING_NOT_UNIVERSAL);
9cfd19
             goto err;
9cfd19
         }
9cfd19
+
9cfd19
         /* Check tag matches bit map */
9cfd19
         if (!(ASN1_tag2bit(otag) & it->utype)) {
9cfd19
             /* If OPTIONAL, assume this is OK */
9cfd19
@@ -316,6 +326,15 @@ static int asn1_item_ex_d2i(ASN1_VALUE *
9cfd19
         goto err;
9cfd19
 
9cfd19
     case ASN1_ITYPE_CHOICE:
9cfd19
+        /*
9cfd19
+         * It never makes sense for CHOICE types to have implicit tagging, so
9cfd19
+         * if tag != -1, then this looks like an error in the template.
9cfd19
+         */
9cfd19
+        if (tag != -1) {
9cfd19
+            ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_BAD_TEMPLATE);
9cfd19
+            goto err;
9cfd19
+        }
9cfd19
+
9cfd19
         if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
9cfd19
             goto auxerr;
9cfd19
         if (*pval) {
9cfd19
diff -up openssl-1.0.2k/crypto/asn1/tasn_enc.c.null-dereference openssl-1.0.2k/crypto/asn1/tasn_enc.c
9cfd19
--- openssl-1.0.2k/crypto/asn1/tasn_enc.c.null-dereference	2020-12-04 10:18:30.261472002 +0100
9cfd19
+++ openssl-1.0.2k/crypto/asn1/tasn_enc.c	2020-12-04 10:21:14.310078987 +0100
9cfd19
@@ -151,9 +151,25 @@ int ASN1_item_ex_i2d(ASN1_VALUE **pval,
9cfd19
         break;
9cfd19
 
9cfd19
     case ASN1_ITYPE_MSTRING:
9cfd19
+        /*
9cfd19
+         * It never makes sense for multi-strings to have implicit tagging, so
9cfd19
+         * if tag != -1, then this looks like an error in the template.
9cfd19
+         */
9cfd19
+        if (tag != -1) {
9cfd19
+            ASN1err(ASN1_F_ASN1_ITEM_EX_I2D, ASN1_R_BAD_TEMPLATE);
9cfd19
+            return -1;
9cfd19
+        }
9cfd19
         return asn1_i2d_ex_primitive(pval, out, it, -1, aclass);
9cfd19
 
9cfd19
     case ASN1_ITYPE_CHOICE:
9cfd19
+        /*
9cfd19
+         * It never makes sense for CHOICE types to have implicit tagging, so
9cfd19
+         * if tag != -1, then this looks like an error in the template.
9cfd19
+         */
9cfd19
+        if (tag != -1) {
9cfd19
+            ASN1err(ASN1_F_ASN1_ITEM_EX_I2D, ASN1_R_BAD_TEMPLATE);
9cfd19
+            return -1;
9cfd19
+        }
9cfd19
         if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it, NULL))
9cfd19
             return 0;
9cfd19
         i = asn1_get_choice_selector(pval, it);
9cfd19
diff -up openssl-1.0.2k/crypto/x509v3/v3_genn.c.null-dereference openssl-1.0.2k/crypto/x509v3/v3_genn.c
9cfd19
--- openssl-1.0.2k/crypto/x509v3/v3_genn.c.null-dereference	2020-12-04 10:28:02.374237945 +0100
9cfd19
+++ openssl-1.0.2k/crypto/x509v3/v3_genn.c	2020-12-04 10:36:51.156138263 +0100
9cfd19
@@ -72,8 +72,9 @@ ASN1_SEQUENCE(OTHERNAME) = {
9cfd19
 IMPLEMENT_ASN1_FUNCTIONS(OTHERNAME)
9cfd19
 
9cfd19
 ASN1_SEQUENCE(EDIPARTYNAME) = {
9cfd19
-        ASN1_IMP_OPT(EDIPARTYNAME, nameAssigner, DIRECTORYSTRING, 0),
9cfd19
-        ASN1_IMP_OPT(EDIPARTYNAME, partyName, DIRECTORYSTRING, 1)
9cfd19
+        /* DirectoryString is a CHOICE type so use explicit tagging */
9cfd19
+        ASN1_EXP_OPT(EDIPARTYNAME, nameAssigner, DIRECTORYSTRING, 0),
9cfd19
+        ASN1_EXP(EDIPARTYNAME, partyName, DIRECTORYSTRING, 1)
9cfd19
 } ASN1_SEQUENCE_END(EDIPARTYNAME)
9cfd19
 
9cfd19
 IMPLEMENT_ASN1_FUNCTIONS(EDIPARTYNAME)
9cfd19
@@ -107,6 +108,37 @@ GENERAL_NAME *GENERAL_NAME_dup(GENERAL_N
9cfd19
                                     (char *)a);
9cfd19
 }
9cfd19
 
9cfd19
+static int edipartyname_cmp(const EDIPARTYNAME *a, const EDIPARTYNAME *b)
9cfd19
+{
9cfd19
+    int res;
9cfd19
+
9cfd19
+    if (a == NULL || b == NULL) {
9cfd19
+        /*
9cfd19
+         * Shouldn't be possible in a valid GENERAL_NAME, but we handle it
9cfd19
+         * anyway. OTHERNAME_cmp treats NULL != NULL so we do the same here
9cfd19
+         */
9cfd19
+        return -1;
9cfd19
+    }
9cfd19
+    if (a->nameAssigner == NULL && b->nameAssigner != NULL)
9cfd19
+        return -1;
9cfd19
+    if (a->nameAssigner != NULL && b->nameAssigner == NULL)
9cfd19
+        return 1;
9cfd19
+    /* If we get here then both have nameAssigner set, or both unset */
9cfd19
+    if (a->nameAssigner != NULL) {
9cfd19
+        res = ASN1_STRING_cmp(a->nameAssigner, b->nameAssigner);
9cfd19
+        if (res != 0)
9cfd19
+            return res;
9cfd19
+    }
9cfd19
+    /*
9cfd19
+     * partyName is required, so these should never be NULL. We treat it in
9cfd19
+     * the same way as the a == NULL || b == NULL case above
9cfd19
+     */
9cfd19
+    if (a->partyName == NULL || b->partyName == NULL)
9cfd19
+        return -1;
9cfd19
+
9cfd19
+    return ASN1_STRING_cmp(a->partyName, b->partyName);
9cfd19
+}
9cfd19
+
9cfd19
 /* Returns 0 if they are equal, != 0 otherwise. */
9cfd19
 int GENERAL_NAME_cmp(GENERAL_NAME *a, GENERAL_NAME *b)
9cfd19
 {
9cfd19
@@ -116,8 +148,11 @@ int GENERAL_NAME_cmp(GENERAL_NAME *a, GE
9cfd19
         return -1;
9cfd19
     switch (a->type) {
9cfd19
     case GEN_X400:
9cfd19
+        result = ASN1_TYPE_cmp(a->d.x400Address, b->d.x400Address);
9cfd19
+        break;
9cfd19
+
9cfd19
     case GEN_EDIPARTY:
9cfd19
-        result = ASN1_TYPE_cmp(a->d.other, b->d.other);
9cfd19
+        result = edipartyname_cmp(a->d.ediPartyName, b->d.ediPartyName);
9cfd19
         break;
9cfd19
 
9cfd19
     case GEN_OTHERNAME:
9cfd19
@@ -164,8 +199,11 @@ void GENERAL_NAME_set0_value(GENERAL_NAM
9cfd19
 {
9cfd19
     switch (type) {
9cfd19
     case GEN_X400:
9cfd19
+        a->d.x400Address = value;
9cfd19
+        break;
9cfd19
+
9cfd19
     case GEN_EDIPARTY:
9cfd19
-        a->d.other = value;
9cfd19
+        a->d.ediPartyName = value;
9cfd19
         break;
9cfd19
 
9cfd19
     case GEN_OTHERNAME:
9cfd19
@@ -199,8 +237,10 @@ void *GENERAL_NAME_get0_value(GENERAL_NA
9cfd19
         *ptype = a->type;
9cfd19
     switch (a->type) {
9cfd19
     case GEN_X400:
9cfd19
+        return a->d.x400Address;
9cfd19
+
9cfd19
     case GEN_EDIPARTY:
9cfd19
-        return a->d.other;
9cfd19
+        return a->d.ediPartyName;
9cfd19
 
9cfd19
     case GEN_OTHERNAME:
9cfd19
         return a->d.otherName;
9cfd19
diff -up openssl-1.0.2k/crypto/x509v3/v3nametest.c.null-dereference openssl-1.0.2k/crypto/x509v3/v3nametest.c
9cfd19
--- openssl-1.0.2k/crypto/x509v3/v3nametest.c.null-dereference	2020-12-04 10:28:02.374237945 +0100
9cfd19
+++ openssl-1.0.2k/crypto/x509v3/v3nametest.c	2020-12-04 10:36:51.156138263 +0100
9cfd19
@@ -321,6 +321,356 @@ static void run_cert(X509 *crt, const ch
9cfd19
     }
9cfd19
 }
9cfd19
 
9cfd19
+struct gennamedata {
9cfd19
+    const unsigned char der[22];
9cfd19
+    size_t derlen;
9cfd19
+} gennames[] = {
9cfd19
+    {
9cfd19
+        /*
9cfd19
+        * [0] {
9cfd19
+        *   OBJECT_IDENTIFIER { 1.2.840.113554.4.1.72585.2.1 }
9cfd19
+        *   [0] {
9cfd19
+        *     SEQUENCE {}
9cfd19
+        *   }
9cfd19
+        * }
9cfd19
+        */
9cfd19
+        {
9cfd19
+            0xa0, 0x13, 0x06, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04,
9cfd19
+            0x01, 0x84, 0xb7, 0x09, 0x02, 0x01, 0xa0, 0x02, 0x30, 0x00
9cfd19
+        },
9cfd19
+        21
9cfd19
+    }, {
9cfd19
+        /*
9cfd19
+        * [0] {
9cfd19
+        *   OBJECT_IDENTIFIER { 1.2.840.113554.4.1.72585.2.1 }
9cfd19
+        *   [0] {
9cfd19
+        *     [APPLICATION 0] {}
9cfd19
+        *   }
9cfd19
+        * }
9cfd19
+        */
9cfd19
+        {
9cfd19
+            0xa0, 0x13, 0x06, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04,
9cfd19
+            0x01, 0x84, 0xb7, 0x09, 0x02, 0x01, 0xa0, 0x02, 0x60, 0x00
9cfd19
+        },
9cfd19
+        21
9cfd19
+    }, {
9cfd19
+        /*
9cfd19
+        * [0] {
9cfd19
+        *   OBJECT_IDENTIFIER { 1.2.840.113554.4.1.72585.2.1 }
9cfd19
+        *   [0] {
9cfd19
+        *     UTF8String { "a" }
9cfd19
+        *   }
9cfd19
+        * }
9cfd19
+        */
9cfd19
+        {
9cfd19
+            0xa0, 0x14, 0x06, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04,
9cfd19
+            0x01, 0x84, 0xb7, 0x09, 0x02, 0x01, 0xa0, 0x03, 0x0c, 0x01, 0x61
9cfd19
+        },
9cfd19
+        22
9cfd19
+    }, {
9cfd19
+        /*
9cfd19
+        * [0] {
9cfd19
+        *   OBJECT_IDENTIFIER { 1.2.840.113554.4.1.72585.2.2 }
9cfd19
+        *   [0] {
9cfd19
+        *     UTF8String { "a" }
9cfd19
+        *   }
9cfd19
+        * }
9cfd19
+        */
9cfd19
+        {
9cfd19
+            0xa0, 0x14, 0x06, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04,
9cfd19
+            0x01, 0x84, 0xb7, 0x09, 0x02, 0x02, 0xa0, 0x03, 0x0c, 0x01, 0x61
9cfd19
+        },
9cfd19
+        22
9cfd19
+    }, {
9cfd19
+        /*
9cfd19
+        * [0] {
9cfd19
+        *   OBJECT_IDENTIFIER { 1.2.840.113554.4.1.72585.2.1 }
9cfd19
+        *   [0] {
9cfd19
+        *     UTF8String { "b" }
9cfd19
+        *   }
9cfd19
+        * }
9cfd19
+        */
9cfd19
+        {
9cfd19
+            0xa0, 0x14, 0x06, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04,
9cfd19
+            0x01, 0x84, 0xb7, 0x09, 0x02, 0x01, 0xa0, 0x03, 0x0c, 0x01, 0x62
9cfd19
+        },
9cfd19
+        22
9cfd19
+    }, {
9cfd19
+        /*
9cfd19
+        * [0] {
9cfd19
+        *   OBJECT_IDENTIFIER { 1.2.840.113554.4.1.72585.2.1 }
9cfd19
+        *   [0] {
9cfd19
+        *     BOOLEAN { TRUE }
9cfd19
+        *   }
9cfd19
+        * }
9cfd19
+        */
9cfd19
+        {
9cfd19
+            0xa0, 0x14, 0x06, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04,
9cfd19
+            0x01, 0x84, 0xb7, 0x09, 0x02, 0x01, 0xa0, 0x03, 0x01, 0x01, 0xff
9cfd19
+        },
9cfd19
+        22
9cfd19
+    }, {
9cfd19
+        /*
9cfd19
+        * [0] {
9cfd19
+        *   OBJECT_IDENTIFIER { 1.2.840.113554.4.1.72585.2.1 }
9cfd19
+        *   [0] {
9cfd19
+        *     BOOLEAN { FALSE }
9cfd19
+        *   }
9cfd19
+        * }
9cfd19
+        */
9cfd19
+        {
9cfd19
+            0xa0, 0x14, 0x06, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04,
9cfd19
+            0x01, 0x84, 0xb7, 0x09, 0x02, 0x01, 0xa0, 0x03, 0x01, 0x01, 0x00
9cfd19
+        },
9cfd19
+        22
9cfd19
+    }, {
9cfd19
+        /* [1 PRIMITIVE] { "a" } */
9cfd19
+        {
9cfd19
+            0x81, 0x01, 0x61
9cfd19
+        },
9cfd19
+        3
9cfd19
+    }, {
9cfd19
+        /* [1 PRIMITIVE] { "b" } */
9cfd19
+        {
9cfd19
+            0x81, 0x01, 0x62
9cfd19
+        },
9cfd19
+        3
9cfd19
+    }, {
9cfd19
+        /* [2 PRIMITIVE] { "a" } */
9cfd19
+        {
9cfd19
+            0x82, 0x01, 0x61
9cfd19
+        },
9cfd19
+        3
9cfd19
+    }, {
9cfd19
+        /* [2 PRIMITIVE] { "b" } */
9cfd19
+        {
9cfd19
+            0x82, 0x01, 0x62
9cfd19
+        },
9cfd19
+        3
9cfd19
+    }, {
9cfd19
+        /*
9cfd19
+        * [4] {
9cfd19
+        *   SEQUENCE {
9cfd19
+        *     SET {
9cfd19
+        *       SEQUENCE {
9cfd19
+        *         # commonName
9cfd19
+        *         OBJECT_IDENTIFIER { 2.5.4.3 }
9cfd19
+        *         UTF8String { "a" }
9cfd19
+        *       }
9cfd19
+        *     }
9cfd19
+        *   }
9cfd19
+        * }
9cfd19
+        */
9cfd19
+        {
9cfd19
+            0xa4, 0x0e, 0x30, 0x0c, 0x31, 0x0a, 0x30, 0x08, 0x06, 0x03, 0x55,
9cfd19
+            0x04, 0x03, 0x0c, 0x01, 0x61
9cfd19
+        },
9cfd19
+        16
9cfd19
+    }, {
9cfd19
+        /*
9cfd19
+        * [4] {
9cfd19
+        *   SEQUENCE {
9cfd19
+        *     SET {
9cfd19
+        *       SEQUENCE {
9cfd19
+        *         # commonName
9cfd19
+        *         OBJECT_IDENTIFIER { 2.5.4.3 }
9cfd19
+        *         UTF8String { "b" }
9cfd19
+        *       }
9cfd19
+        *     }
9cfd19
+        *   }
9cfd19
+        * }
9cfd19
+        */
9cfd19
+        {
9cfd19
+            0xa4, 0x0e, 0x30, 0x0c, 0x31, 0x0a, 0x30, 0x08, 0x06, 0x03, 0x55,
9cfd19
+            0x04, 0x03, 0x0c, 0x01, 0x62
9cfd19
+        },
9cfd19
+        16
9cfd19
+    }, {
9cfd19
+        /*
9cfd19
+        * [5] {
9cfd19
+        *   [1] {
9cfd19
+        *     UTF8String { "a" }
9cfd19
+        *   }
9cfd19
+        * }
9cfd19
+        */
9cfd19
+        {
9cfd19
+            0xa5, 0x05, 0xa1, 0x03, 0x0c, 0x01, 0x61
9cfd19
+        },
9cfd19
+        7
9cfd19
+    }, {
9cfd19
+        /*
9cfd19
+        * [5] {
9cfd19
+        *   [1] {
9cfd19
+        *     UTF8String { "b" }
9cfd19
+        *   }
9cfd19
+        * }
9cfd19
+        */
9cfd19
+        {
9cfd19
+            0xa5, 0x05, 0xa1, 0x03, 0x0c, 0x01, 0x62
9cfd19
+        },
9cfd19
+        7
9cfd19
+    }, {
9cfd19
+        /*
9cfd19
+        * [5] {
9cfd19
+        *   [0] {
9cfd19
+        *     UTF8String {}
9cfd19
+        *   }
9cfd19
+        *   [1] {
9cfd19
+        *     UTF8String { "a" }
9cfd19
+        *   }
9cfd19
+        * }
9cfd19
+        */
9cfd19
+        {
9cfd19
+            0xa5, 0x09, 0xa0, 0x02, 0x0c, 0x00, 0xa1, 0x03, 0x0c, 0x01, 0x61
9cfd19
+        },
9cfd19
+        11
9cfd19
+    }, {
9cfd19
+        /*
9cfd19
+        * [5] {
9cfd19
+        *   [0] {
9cfd19
+        *     UTF8String { "a" }
9cfd19
+        *   }
9cfd19
+        *   [1] {
9cfd19
+        *     UTF8String { "a" }
9cfd19
+        *   }
9cfd19
+        * }
9cfd19
+        */
9cfd19
+        {
9cfd19
+            0xa5, 0x0a, 0xa0, 0x03, 0x0c, 0x01, 0x61, 0xa1, 0x03, 0x0c, 0x01,
9cfd19
+            0x61
9cfd19
+        },
9cfd19
+        12
9cfd19
+    }, {
9cfd19
+        /*
9cfd19
+        * [5] {
9cfd19
+        *   [0] {
9cfd19
+        *     UTF8String { "b" }
9cfd19
+        *   }
9cfd19
+        *   [1] {
9cfd19
+        *     UTF8String { "a" }
9cfd19
+        *   }
9cfd19
+        * }
9cfd19
+        */
9cfd19
+        {
9cfd19
+            0xa5, 0x0a, 0xa0, 0x03, 0x0c, 0x01, 0x62, 0xa1, 0x03, 0x0c, 0x01,
9cfd19
+            0x61
9cfd19
+        },
9cfd19
+        12
9cfd19
+    }, {
9cfd19
+        /* [6 PRIMITIVE] { "a" } */
9cfd19
+        {
9cfd19
+            0x86, 0x01, 0x61
9cfd19
+        },
9cfd19
+        3
9cfd19
+    }, {
9cfd19
+        /* [6 PRIMITIVE] { "b" } */
9cfd19
+        {
9cfd19
+            0x86, 0x01, 0x62
9cfd19
+        },
9cfd19
+        3
9cfd19
+    }, {
9cfd19
+        /* [7 PRIMITIVE] { `11111111` } */
9cfd19
+        {
9cfd19
+            0x87, 0x04, 0x11, 0x11, 0x11, 0x11
9cfd19
+        },
9cfd19
+        6
9cfd19
+    }, {
9cfd19
+        /* [7 PRIMITIVE] { `22222222`} */
9cfd19
+        {
9cfd19
+            0x87, 0x04, 0x22, 0x22, 0x22, 0x22
9cfd19
+        },
9cfd19
+        6
9cfd19
+    }, {
9cfd19
+        /* [7 PRIMITIVE] { `11111111111111111111111111111111` } */
9cfd19
+        {
9cfd19
+            0x87, 0x10, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
9cfd19
+            0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11
9cfd19
+        },
9cfd19
+        18
9cfd19
+    }, {
9cfd19
+        /* [7 PRIMITIVE] { `22222222222222222222222222222222` } */
9cfd19
+        {
9cfd19
+            0x87, 0x10, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
9cfd19
+            0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22
9cfd19
+        },
9cfd19
+        18
9cfd19
+    }, {
9cfd19
+        /* [8 PRIMITIVE] { 1.2.840.113554.4.1.72585.2.1 } */
9cfd19
+        {
9cfd19
+            0x88, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84,
9cfd19
+            0xb7, 0x09, 0x02, 0x01
9cfd19
+        },
9cfd19
+        15
9cfd19
+    }, {
9cfd19
+        /* [8 PRIMITIVE] { 1.2.840.113554.4.1.72585.2.2 } */
9cfd19
+        {
9cfd19
+            0x88, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84,
9cfd19
+            0xb7, 0x09, 0x02, 0x02
9cfd19
+        },
9cfd19
+        15
9cfd19
+    }
9cfd19
+};
9cfd19
+
9cfd19
+#define OSSL_NELEM(x) (sizeof(x)/sizeof((x)[0]))
9cfd19
+
9cfd19
+static int test_GENERAL_NAME_cmp(void)
9cfd19
+{
9cfd19
+    size_t i, j;
9cfd19
+    GENERAL_NAME **namesa = OPENSSL_malloc(sizeof(*namesa)
9cfd19
+                                           * OSSL_NELEM(gennames));
9cfd19
+    GENERAL_NAME **namesb = OPENSSL_malloc(sizeof(*namesb)
9cfd19
+                                           * OSSL_NELEM(gennames));
9cfd19
+    int testresult = 0;
9cfd19
+
9cfd19
+    if (namesa == NULL || namesb == NULL)
9cfd19
+        goto end;
9cfd19
+
9cfd19
+    for (i = 0; i < OSSL_NELEM(gennames); i++) {
9cfd19
+        const unsigned char *derp = gennames[i].der;
9cfd19
+
9cfd19
+        /*
9cfd19
+         * We create two versions of each GENERAL_NAME so that we ensure when
9cfd19
+         * we compare them they are always different pointers.
9cfd19
+         */
9cfd19
+        namesa[i] = d2i_GENERAL_NAME(NULL, &derp, gennames[i].derlen);
9cfd19
+        derp = gennames[i].der;
9cfd19
+        namesb[i] = d2i_GENERAL_NAME(NULL, &derp, gennames[i].derlen);
9cfd19
+        if (namesa[i] == NULL || namesb[i] == NULL)
9cfd19
+            goto end;
9cfd19
+    }
9cfd19
+
9cfd19
+    /* Every name should be equal to itself and not equal to any others. */
9cfd19
+    for (i = 0; i < OSSL_NELEM(gennames); i++) {
9cfd19
+        for (j = 0; j < OSSL_NELEM(gennames); j++) {
9cfd19
+            if (i == j) {
9cfd19
+                if (GENERAL_NAME_cmp(namesa[i], namesb[j]) != 0)
9cfd19
+                    goto end;
9cfd19
+            } else {
9cfd19
+                if (GENERAL_NAME_cmp(namesa[i], namesb[j]) == 0)
9cfd19
+                    goto end;
9cfd19
+            }
9cfd19
+        }
9cfd19
+    }
9cfd19
+    testresult = 1;
9cfd19
+
9cfd19
+ end:
9cfd19
+    for (i = 0; i < OSSL_NELEM(gennames); i++) {
9cfd19
+        if (namesa != NULL)
9cfd19
+            GENERAL_NAME_free(namesa[i]);
9cfd19
+        if (namesb != NULL)
9cfd19
+            GENERAL_NAME_free(namesb[i]);
9cfd19
+    }
9cfd19
+    OPENSSL_free(namesa);
9cfd19
+    OPENSSL_free(namesb);
9cfd19
+
9cfd19
+    if (!testresult)
9cfd19
+        fprintf(stderr, "test of GENERAL_NAME_cmp failed\n");
9cfd19
+
9cfd19
+    return testresult;
9cfd19
+}
9cfd19
+
9cfd19
+
9cfd19
+
9cfd19
 int main(void)
9cfd19
 {
9cfd19
     const struct set_name_fn *pfn = name_fns;
9cfd19
@@ -342,5 +692,8 @@ int main(void)
9cfd19
         }
9cfd19
         ++pfn;
9cfd19
     }
9cfd19
+
9cfd19
+    errors += !test_GENERAL_NAME_cmp();
9cfd19
+
9cfd19
     return errors > 0 ? 1 : 0;
9cfd19
 }