Backport of: From c3e7925c31675ad42d9e7d1974e98c10d4fef5df Mon Sep 17 00:00:00 2001 From: Matt Caswell Date: Mon, 30 Nov 2020 13:50:52 +0000 Subject: [PATCH] Add a test for GENERAL_NAME_cmp Based on a boringssl test contributed by David Benjamin --- test/v3nametest.c | 344 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 344 insertions(+) --- a/crypto/x509v3/v3nametest.c +++ b/crypto/x509v3/v3nametest.c @@ -3,6 +3,8 @@ #include "../e_os.h" #include +#define OSSL_NELEM(x) (sizeof(x)/sizeof(x[0])) + static const char *const names[] = { "a", "b", ".", "*", "@", ".a", "a.", ".b", "b.", ".*", "*.", "*@", "@*", "a@", "@a", "b@", "..", @@ -321,6 +323,349 @@ static void run_cert(X509 *crt, const ch } } +struct gennamedata { + const unsigned char der[22]; + size_t derlen; +} gennames[] = { + { + /* + * [0] { + * OBJECT_IDENTIFIER { 1.2.840.113554.4.1.72585.2.1 } + * [0] { + * SEQUENCE {} + * } + * } + */ + { + 0xa0, 0x13, 0x06, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, + 0x01, 0x84, 0xb7, 0x09, 0x02, 0x01, 0xa0, 0x02, 0x30, 0x00 + }, + 21 + }, { + /* + * [0] { + * OBJECT_IDENTIFIER { 1.2.840.113554.4.1.72585.2.1 } + * [0] { + * [APPLICATION 0] {} + * } + * } + */ + { + 0xa0, 0x13, 0x06, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, + 0x01, 0x84, 0xb7, 0x09, 0x02, 0x01, 0xa0, 0x02, 0x60, 0x00 + }, + 21 + }, { + /* + * [0] { + * OBJECT_IDENTIFIER { 1.2.840.113554.4.1.72585.2.1 } + * [0] { + * UTF8String { "a" } + * } + * } + */ + { + 0xa0, 0x14, 0x06, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, + 0x01, 0x84, 0xb7, 0x09, 0x02, 0x01, 0xa0, 0x03, 0x0c, 0x01, 0x61 + }, + 22 + }, { + /* + * [0] { + * OBJECT_IDENTIFIER { 1.2.840.113554.4.1.72585.2.2 } + * [0] { + * UTF8String { "a" } + * } + * } + */ + { + 0xa0, 0x14, 0x06, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, + 0x01, 0x84, 0xb7, 0x09, 0x02, 0x02, 0xa0, 0x03, 0x0c, 0x01, 0x61 + }, + 22 + }, { + /* + * [0] { + * OBJECT_IDENTIFIER { 1.2.840.113554.4.1.72585.2.1 } + * [0] { + * UTF8String { "b" } + * } + * } + */ + { + 0xa0, 0x14, 0x06, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, + 0x01, 0x84, 0xb7, 0x09, 0x02, 0x01, 0xa0, 0x03, 0x0c, 0x01, 0x62 + }, + 22 + }, { + /* + * [0] { + * OBJECT_IDENTIFIER { 1.2.840.113554.4.1.72585.2.1 } + * [0] { + * BOOLEAN { TRUE } + * } + * } + */ + { + 0xa0, 0x14, 0x06, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, + 0x01, 0x84, 0xb7, 0x09, 0x02, 0x01, 0xa0, 0x03, 0x01, 0x01, 0xff + }, + 22 + }, { + /* + * [0] { + * OBJECT_IDENTIFIER { 1.2.840.113554.4.1.72585.2.1 } + * [0] { + * BOOLEAN { FALSE } + * } + * } + */ + { + 0xa0, 0x14, 0x06, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, + 0x01, 0x84, 0xb7, 0x09, 0x02, 0x01, 0xa0, 0x03, 0x01, 0x01, 0x00 + }, + 22 + }, { + /* [1 PRIMITIVE] { "a" } */ + { + 0x81, 0x01, 0x61 + }, + 3 + }, { + /* [1 PRIMITIVE] { "b" } */ + { + 0x81, 0x01, 0x62 + }, + 3 + }, { + /* [2 PRIMITIVE] { "a" } */ + { + 0x82, 0x01, 0x61 + }, + 3 + }, { + /* [2 PRIMITIVE] { "b" } */ + { + 0x82, 0x01, 0x62 + }, + 3 + }, { + /* + * [4] { + * SEQUENCE { + * SET { + * SEQUENCE { + * # commonName + * OBJECT_IDENTIFIER { 2.5.4.3 } + * UTF8String { "a" } + * } + * } + * } + * } + */ + { + 0xa4, 0x0e, 0x30, 0x0c, 0x31, 0x0a, 0x30, 0x08, 0x06, 0x03, 0x55, + 0x04, 0x03, 0x0c, 0x01, 0x61 + }, + 16 + }, { + /* + * [4] { + * SEQUENCE { + * SET { + * SEQUENCE { + * # commonName + * OBJECT_IDENTIFIER { 2.5.4.3 } + * UTF8String { "b" } + * } + * } + * } + * } + */ + { + 0xa4, 0x0e, 0x30, 0x0c, 0x31, 0x0a, 0x30, 0x08, 0x06, 0x03, 0x55, + 0x04, 0x03, 0x0c, 0x01, 0x62 + }, + 16 + }, { + /* + * [5] { + * [1] { + * UTF8String { "a" } + * } + * } + */ + { + 0xa5, 0x05, 0xa1, 0x03, 0x0c, 0x01, 0x61 + }, + 7 + }, { + /* + * [5] { + * [1] { + * UTF8String { "b" } + * } + * } + */ + { + 0xa5, 0x05, 0xa1, 0x03, 0x0c, 0x01, 0x62 + }, + 7 + }, { + /* + * [5] { + * [0] { + * UTF8String {} + * } + * [1] { + * UTF8String { "a" } + * } + * } + */ + { + 0xa5, 0x09, 0xa0, 0x02, 0x0c, 0x00, 0xa1, 0x03, 0x0c, 0x01, 0x61 + }, + 11 + }, { + /* + * [5] { + * [0] { + * UTF8String { "a" } + * } + * [1] { + * UTF8String { "a" } + * } + * } + */ + { + 0xa5, 0x0a, 0xa0, 0x03, 0x0c, 0x01, 0x61, 0xa1, 0x03, 0x0c, 0x01, + 0x61 + }, + 12 + }, { + /* + * [5] { + * [0] { + * UTF8String { "b" } + * } + * [1] { + * UTF8String { "a" } + * } + * } + */ + { + 0xa5, 0x0a, 0xa0, 0x03, 0x0c, 0x01, 0x62, 0xa1, 0x03, 0x0c, 0x01, + 0x61 + }, + 12 + }, { + /* [6 PRIMITIVE] { "a" } */ + { + 0x86, 0x01, 0x61 + }, + 3 + }, { + /* [6 PRIMITIVE] { "b" } */ + { + 0x86, 0x01, 0x62 + }, + 3 + }, { + /* [7 PRIMITIVE] { `11111111` } */ + { + 0x87, 0x04, 0x11, 0x11, 0x11, 0x11 + }, + 6 + }, { + /* [7 PRIMITIVE] { `22222222`} */ + { + 0x87, 0x04, 0x22, 0x22, 0x22, 0x22 + }, + 6 + }, { + /* [7 PRIMITIVE] { `11111111111111111111111111111111` } */ + { + 0x87, 0x10, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, + 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11 + }, + 18 + }, { + /* [7 PRIMITIVE] { `22222222222222222222222222222222` } */ + { + 0x87, 0x10, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, + 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22 + }, + 18 + }, { + /* [8 PRIMITIVE] { 1.2.840.113554.4.1.72585.2.1 } */ + { + 0x88, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84, + 0xb7, 0x09, 0x02, 0x01 + }, + 15 + }, { + /* [8 PRIMITIVE] { 1.2.840.113554.4.1.72585.2.2 } */ + { + 0x88, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84, + 0xb7, 0x09, 0x02, 0x02 + }, + 15 + } +}; + +static int test_GENERAL_NAME_cmp(void) +{ + size_t i, j; + GENERAL_NAME **namesa = OPENSSL_malloc(sizeof(*namesa) + * OSSL_NELEM(gennames)); + GENERAL_NAME **namesb = OPENSSL_malloc(sizeof(*namesb) + * OSSL_NELEM(gennames)); + int testresult = 0; + + if (namesa == NULL || namesb == NULL) + goto end; + + for (i = 0; i < OSSL_NELEM(gennames); i++) { + const unsigned char *derp = gennames[i].der; + + /* + * We create two versions of each GENERAL_NAME so that we ensure when + * we compare them they are always different pointers. + */ + namesa[i] = d2i_GENERAL_NAME(NULL, &derp, gennames[i].derlen); + derp = gennames[i].der; + namesb[i] = d2i_GENERAL_NAME(NULL, &derp, gennames[i].derlen); + if (namesa[i] == NULL || namesb[i] == NULL) + goto end; + } + + /* Every name should be equal to itself and not equal to any others. */ + for (i = 0; i < OSSL_NELEM(gennames); i++) { + for (j = 0; j < OSSL_NELEM(gennames); j++) { + if (i == j) { + if (GENERAL_NAME_cmp(namesa[i], namesb[j]) != 0) + goto end; + } else { + if (GENERAL_NAME_cmp(namesa[i], namesb[j]) == 0) + goto end; + } + } + } + testresult = 1; + + end: + for (i = 0; i < OSSL_NELEM(gennames); i++) { + if (namesa != NULL) + GENERAL_NAME_free(namesa[i]); + if (namesb != NULL) + GENERAL_NAME_free(namesb[i]); + } + OPENSSL_free(namesa); + OPENSSL_free(namesb); + + return testresult; +} + int main(void) { const struct set_name_fn *pfn = name_fns; @@ -342,5 +687,11 @@ int main(void) } ++pfn; } + + if (!test_GENERAL_NAME_cmp()) { + fprintf(stderr, "test_GENERAL_NAME_cmp failed\n"); + return 1; + } + return errors > 0 ? 1 : 0; }