v3nametest.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. /*
  2. * Copyright 2012-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <string.h>
  10. #include <openssl/e_os2.h>
  11. #include <openssl/x509.h>
  12. #include <openssl/x509v3.h>
  13. #include "internal/nelem.h"
  14. #include "testutil.h"
  15. #ifdef OPENSSL_SYS_WINDOWS
  16. # define strcasecmp _stricmp
  17. #endif
  18. static const char *const names[] = {
  19. "a", "b", ".", "*", "@",
  20. ".a", "a.", ".b", "b.", ".*", "*.", "*@", "@*", "a@", "@a", "b@", "..",
  21. "-example.com", "example-.com",
  22. "@@", "**", "*.com", "*com", "*.*.com", "*com", "com*", "*example.com",
  23. "*@example.com", "test@*.example.com", "example.com", "www.example.com",
  24. "test.www.example.com", "*.example.com", "*.www.example.com",
  25. "test.*.example.com", "www.*.com",
  26. ".www.example.com", "*www.example.com",
  27. "example.net", "xn--rger-koa.example.com",
  28. "*.xn--rger-koa.example.com", "www.xn--rger-koa.example.com",
  29. "*.good--example.com", "www.good--example.com",
  30. "*.xn--bar.com", "xn--foo.xn--bar.com",
  31. "a.example.com", "b.example.com",
  32. "postmaster@example.com", "Postmaster@example.com",
  33. "postmaster@EXAMPLE.COM",
  34. NULL
  35. };
  36. static const char *const exceptions[] = {
  37. "set CN: host: [*.example.com] matches [a.example.com]",
  38. "set CN: host: [*.example.com] matches [b.example.com]",
  39. "set CN: host: [*.example.com] matches [www.example.com]",
  40. "set CN: host: [*.example.com] matches [xn--rger-koa.example.com]",
  41. "set CN: host: [*.www.example.com] matches [test.www.example.com]",
  42. "set CN: host: [*.www.example.com] matches [.www.example.com]",
  43. "set CN: host: [*www.example.com] matches [www.example.com]",
  44. "set CN: host: [test.www.example.com] matches [.www.example.com]",
  45. "set CN: host: [*.xn--rger-koa.example.com] matches [www.xn--rger-koa.example.com]",
  46. "set CN: host: [*.xn--bar.com] matches [xn--foo.xn--bar.com]",
  47. "set CN: host: [*.good--example.com] matches [www.good--example.com]",
  48. "set CN: host-no-wildcards: [*.www.example.com] matches [.www.example.com]",
  49. "set CN: host-no-wildcards: [test.www.example.com] matches [.www.example.com]",
  50. "set emailAddress: email: [postmaster@example.com] does not match [Postmaster@example.com]",
  51. "set emailAddress: email: [postmaster@EXAMPLE.COM] does not match [Postmaster@example.com]",
  52. "set emailAddress: email: [Postmaster@example.com] does not match [postmaster@example.com]",
  53. "set emailAddress: email: [Postmaster@example.com] does not match [postmaster@EXAMPLE.COM]",
  54. "set dnsName: host: [*.example.com] matches [www.example.com]",
  55. "set dnsName: host: [*.example.com] matches [a.example.com]",
  56. "set dnsName: host: [*.example.com] matches [b.example.com]",
  57. "set dnsName: host: [*.example.com] matches [xn--rger-koa.example.com]",
  58. "set dnsName: host: [*.www.example.com] matches [test.www.example.com]",
  59. "set dnsName: host-no-wildcards: [*.www.example.com] matches [.www.example.com]",
  60. "set dnsName: host-no-wildcards: [test.www.example.com] matches [.www.example.com]",
  61. "set dnsName: host: [*.www.example.com] matches [.www.example.com]",
  62. "set dnsName: host: [*www.example.com] matches [www.example.com]",
  63. "set dnsName: host: [test.www.example.com] matches [.www.example.com]",
  64. "set dnsName: host: [*.xn--rger-koa.example.com] matches [www.xn--rger-koa.example.com]",
  65. "set dnsName: host: [*.xn--bar.com] matches [xn--foo.xn--bar.com]",
  66. "set dnsName: host: [*.good--example.com] matches [www.good--example.com]",
  67. "set rfc822Name: email: [postmaster@example.com] does not match [Postmaster@example.com]",
  68. "set rfc822Name: email: [Postmaster@example.com] does not match [postmaster@example.com]",
  69. "set rfc822Name: email: [Postmaster@example.com] does not match [postmaster@EXAMPLE.COM]",
  70. "set rfc822Name: email: [postmaster@EXAMPLE.COM] does not match [Postmaster@example.com]",
  71. NULL
  72. };
  73. static int is_exception(const char *msg)
  74. {
  75. const char *const *p;
  76. for (p = exceptions; *p; ++p)
  77. if (strcmp(msg, *p) == 0)
  78. return 1;
  79. return 0;
  80. }
  81. static int set_cn(X509 *crt, ...)
  82. {
  83. int ret = 0;
  84. X509_NAME *n = NULL;
  85. va_list ap;
  86. va_start(ap, crt);
  87. n = X509_NAME_new();
  88. if (n == NULL)
  89. goto out;
  90. while (1) {
  91. int nid;
  92. const char *name;
  93. nid = va_arg(ap, int);
  94. if (nid == 0)
  95. break;
  96. name = va_arg(ap, const char *);
  97. if (!X509_NAME_add_entry_by_NID(n, nid, MBSTRING_ASC,
  98. (unsigned char *)name, -1, -1, 1))
  99. goto out;
  100. }
  101. if (!X509_set_subject_name(crt, n))
  102. goto out;
  103. ret = 1;
  104. out:
  105. X509_NAME_free(n);
  106. va_end(ap);
  107. return ret;
  108. }
  109. /*-
  110. int X509_add_ext(X509 *x, X509_EXTENSION *ex, int loc);
  111. X509_EXTENSION *X509_EXTENSION_create_by_NID(X509_EXTENSION **ex,
  112. int nid, int crit, ASN1_OCTET_STRING *data);
  113. int X509_add_ext(X509 *x, X509_EXTENSION *ex, int loc);
  114. */
  115. static int set_altname(X509 *crt, ...)
  116. {
  117. int ret = 0;
  118. GENERAL_NAMES *gens = NULL;
  119. GENERAL_NAME *gen = NULL;
  120. ASN1_IA5STRING *ia5 = NULL;
  121. va_list ap;
  122. va_start(ap, crt);
  123. gens = sk_GENERAL_NAME_new_null();
  124. if (gens == NULL)
  125. goto out;
  126. while (1) {
  127. int type;
  128. const char *name;
  129. type = va_arg(ap, int);
  130. if (type == 0)
  131. break;
  132. name = va_arg(ap, const char *);
  133. gen = GENERAL_NAME_new();
  134. if (gen == NULL)
  135. goto out;
  136. ia5 = ASN1_IA5STRING_new();
  137. if (ia5 == NULL)
  138. goto out;
  139. if (!ASN1_STRING_set(ia5, name, -1))
  140. goto out;
  141. switch (type) {
  142. case GEN_EMAIL:
  143. case GEN_DNS:
  144. GENERAL_NAME_set0_value(gen, type, ia5);
  145. ia5 = NULL;
  146. break;
  147. default:
  148. abort();
  149. }
  150. sk_GENERAL_NAME_push(gens, gen);
  151. gen = NULL;
  152. }
  153. if (!X509_add1_ext_i2d(crt, NID_subject_alt_name, gens, 0, 0))
  154. goto out;
  155. ret = 1;
  156. out:
  157. ASN1_IA5STRING_free(ia5);
  158. GENERAL_NAME_free(gen);
  159. GENERAL_NAMES_free(gens);
  160. va_end(ap);
  161. return ret;
  162. }
  163. static int set_cn1(X509 *crt, const char *name)
  164. {
  165. return set_cn(crt, NID_commonName, name, 0);
  166. }
  167. static int set_cn_and_email(X509 *crt, const char *name)
  168. {
  169. return set_cn(crt, NID_commonName, name,
  170. NID_pkcs9_emailAddress, "dummy@example.com", 0);
  171. }
  172. static int set_cn2(X509 *crt, const char *name)
  173. {
  174. return set_cn(crt, NID_commonName, "dummy value",
  175. NID_commonName, name, 0);
  176. }
  177. static int set_cn3(X509 *crt, const char *name)
  178. {
  179. return set_cn(crt, NID_commonName, name,
  180. NID_commonName, "dummy value", 0);
  181. }
  182. static int set_email1(X509 *crt, const char *name)
  183. {
  184. return set_cn(crt, NID_pkcs9_emailAddress, name, 0);
  185. }
  186. static int set_email2(X509 *crt, const char *name)
  187. {
  188. return set_cn(crt, NID_pkcs9_emailAddress, "dummy@example.com",
  189. NID_pkcs9_emailAddress, name, 0);
  190. }
  191. static int set_email3(X509 *crt, const char *name)
  192. {
  193. return set_cn(crt, NID_pkcs9_emailAddress, name,
  194. NID_pkcs9_emailAddress, "dummy@example.com", 0);
  195. }
  196. static int set_email_and_cn(X509 *crt, const char *name)
  197. {
  198. return set_cn(crt, NID_pkcs9_emailAddress, name,
  199. NID_commonName, "www.example.org", 0);
  200. }
  201. static int set_altname_dns(X509 *crt, const char *name)
  202. {
  203. return set_altname(crt, GEN_DNS, name, 0);
  204. }
  205. static int set_altname_email(X509 *crt, const char *name)
  206. {
  207. return set_altname(crt, GEN_EMAIL, name, 0);
  208. }
  209. struct set_name_fn {
  210. int (*fn) (X509 *, const char *);
  211. const char *name;
  212. int host;
  213. int email;
  214. };
  215. static const struct set_name_fn name_fns[] = {
  216. {set_cn1, "set CN", 1, 0},
  217. {set_cn2, "set CN", 1, 0},
  218. {set_cn3, "set CN", 1, 0},
  219. {set_cn_and_email, "set CN", 1, 0},
  220. {set_email1, "set emailAddress", 0, 1},
  221. {set_email2, "set emailAddress", 0, 1},
  222. {set_email3, "set emailAddress", 0, 1},
  223. {set_email_and_cn, "set emailAddress", 0, 1},
  224. {set_altname_dns, "set dnsName", 1, 0},
  225. {set_altname_email, "set rfc822Name", 0, 1},
  226. };
  227. static X509 *make_cert(void)
  228. {
  229. X509 *crt = NULL;
  230. if (!TEST_ptr(crt = X509_new()))
  231. return NULL;
  232. if (!TEST_true(X509_set_version(crt, 2))) {
  233. X509_free(crt);
  234. return NULL;
  235. }
  236. return crt;
  237. }
  238. static int check_message(const struct set_name_fn *fn, const char *op,
  239. const char *nameincert, int match, const char *name)
  240. {
  241. char msg[1024];
  242. if (match < 0)
  243. return 1;
  244. BIO_snprintf(msg, sizeof(msg), "%s: %s: [%s] %s [%s]",
  245. fn->name, op, nameincert,
  246. match ? "matches" : "does not match", name);
  247. if (is_exception(msg))
  248. return 1;
  249. TEST_error("%s", msg);
  250. return 0;
  251. }
  252. static int run_cert(X509 *crt, const char *nameincert,
  253. const struct set_name_fn *fn)
  254. {
  255. const char *const *pname = names;
  256. int failed = 0;
  257. for (; *pname != NULL; ++pname) {
  258. int samename = strcasecmp(nameincert, *pname) == 0;
  259. size_t namelen = strlen(*pname);
  260. char *name = OPENSSL_malloc(namelen + 1);
  261. int match, ret;
  262. memcpy(name, *pname, namelen + 1);
  263. match = -1;
  264. if (!TEST_int_ge(ret = X509_check_host(crt, name, namelen, 0, NULL),
  265. 0)) {
  266. failed = 1;
  267. } else if (fn->host) {
  268. if (ret == 1 && !samename)
  269. match = 1;
  270. if (ret == 0 && samename)
  271. match = 0;
  272. } else if (ret == 1)
  273. match = 1;
  274. if (!TEST_true(check_message(fn, "host", nameincert, match, *pname)))
  275. failed = 1;
  276. match = -1;
  277. if (!TEST_int_ge(ret = X509_check_host(crt, name, namelen,
  278. X509_CHECK_FLAG_NO_WILDCARDS,
  279. NULL), 0)) {
  280. failed = 1;
  281. } else if (fn->host) {
  282. if (ret == 1 && !samename)
  283. match = 1;
  284. if (ret == 0 && samename)
  285. match = 0;
  286. } else if (ret == 1)
  287. match = 1;
  288. if (!TEST_true(check_message(fn, "host-no-wildcards",
  289. nameincert, match, *pname)))
  290. failed = 1;
  291. match = -1;
  292. ret = X509_check_email(crt, name, namelen, 0);
  293. if (fn->email) {
  294. if (ret && !samename)
  295. match = 1;
  296. if (!ret && samename && strchr(nameincert, '@') != NULL)
  297. match = 0;
  298. } else if (ret)
  299. match = 1;
  300. if (!TEST_true(check_message(fn, "email", nameincert, match, *pname)))
  301. failed = 1;
  302. OPENSSL_free(name);
  303. }
  304. return failed == 0;
  305. }
  306. static int call_run_cert(int i)
  307. {
  308. int failed = 0;
  309. const struct set_name_fn *pfn = &name_fns[i];
  310. X509 *crt;
  311. const char *const *pname;
  312. TEST_info("%s", pfn->name);
  313. for (pname = names; *pname != NULL; pname++) {
  314. if (!TEST_ptr(crt = make_cert())
  315. || !TEST_true(pfn->fn(crt, *pname))
  316. || !run_cert(crt, *pname, pfn))
  317. failed = 1;
  318. X509_free(crt);
  319. }
  320. return failed == 0;
  321. }
  322. static struct gennamedata {
  323. const unsigned char der[22];
  324. size_t derlen;
  325. } gennames[] = {
  326. {
  327. /*
  328. * [0] {
  329. * OBJECT_IDENTIFIER { 1.2.840.113554.4.1.72585.2.1 }
  330. * [0] {
  331. * SEQUENCE {}
  332. * }
  333. * }
  334. */
  335. {
  336. 0xa0, 0x13, 0x06, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04,
  337. 0x01, 0x84, 0xb7, 0x09, 0x02, 0x01, 0xa0, 0x02, 0x30, 0x00
  338. },
  339. 21
  340. }, {
  341. /*
  342. * [0] {
  343. * OBJECT_IDENTIFIER { 1.2.840.113554.4.1.72585.2.1 }
  344. * [0] {
  345. * [APPLICATION 0] {}
  346. * }
  347. * }
  348. */
  349. {
  350. 0xa0, 0x13, 0x06, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04,
  351. 0x01, 0x84, 0xb7, 0x09, 0x02, 0x01, 0xa0, 0x02, 0x60, 0x00
  352. },
  353. 21
  354. }, {
  355. /*
  356. * [0] {
  357. * OBJECT_IDENTIFIER { 1.2.840.113554.4.1.72585.2.1 }
  358. * [0] {
  359. * UTF8String { "a" }
  360. * }
  361. * }
  362. */
  363. {
  364. 0xa0, 0x14, 0x06, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04,
  365. 0x01, 0x84, 0xb7, 0x09, 0x02, 0x01, 0xa0, 0x03, 0x0c, 0x01, 0x61
  366. },
  367. 22
  368. }, {
  369. /*
  370. * [0] {
  371. * OBJECT_IDENTIFIER { 1.2.840.113554.4.1.72585.2.2 }
  372. * [0] {
  373. * UTF8String { "a" }
  374. * }
  375. * }
  376. */
  377. {
  378. 0xa0, 0x14, 0x06, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04,
  379. 0x01, 0x84, 0xb7, 0x09, 0x02, 0x02, 0xa0, 0x03, 0x0c, 0x01, 0x61
  380. },
  381. 22
  382. }, {
  383. /*
  384. * [0] {
  385. * OBJECT_IDENTIFIER { 1.2.840.113554.4.1.72585.2.1 }
  386. * [0] {
  387. * UTF8String { "b" }
  388. * }
  389. * }
  390. */
  391. {
  392. 0xa0, 0x14, 0x06, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04,
  393. 0x01, 0x84, 0xb7, 0x09, 0x02, 0x01, 0xa0, 0x03, 0x0c, 0x01, 0x62
  394. },
  395. 22
  396. }, {
  397. /*
  398. * [0] {
  399. * OBJECT_IDENTIFIER { 1.2.840.113554.4.1.72585.2.1 }
  400. * [0] {
  401. * BOOLEAN { TRUE }
  402. * }
  403. * }
  404. */
  405. {
  406. 0xa0, 0x14, 0x06, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04,
  407. 0x01, 0x84, 0xb7, 0x09, 0x02, 0x01, 0xa0, 0x03, 0x01, 0x01, 0xff
  408. },
  409. 22
  410. }, {
  411. /*
  412. * [0] {
  413. * OBJECT_IDENTIFIER { 1.2.840.113554.4.1.72585.2.1 }
  414. * [0] {
  415. * BOOLEAN { FALSE }
  416. * }
  417. * }
  418. */
  419. {
  420. 0xa0, 0x14, 0x06, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04,
  421. 0x01, 0x84, 0xb7, 0x09, 0x02, 0x01, 0xa0, 0x03, 0x01, 0x01, 0x00
  422. },
  423. 22
  424. }, {
  425. /* [1 PRIMITIVE] { "a" } */
  426. {
  427. 0x81, 0x01, 0x61
  428. },
  429. 3
  430. }, {
  431. /* [1 PRIMITIVE] { "b" } */
  432. {
  433. 0x81, 0x01, 0x62
  434. },
  435. 3
  436. }, {
  437. /* [2 PRIMITIVE] { "a" } */
  438. {
  439. 0x82, 0x01, 0x61
  440. },
  441. 3
  442. }, {
  443. /* [2 PRIMITIVE] { "b" } */
  444. {
  445. 0x82, 0x01, 0x62
  446. },
  447. 3
  448. }, {
  449. /*
  450. * [4] {
  451. * SEQUENCE {
  452. * SET {
  453. * SEQUENCE {
  454. * # commonName
  455. * OBJECT_IDENTIFIER { 2.5.4.3 }
  456. * UTF8String { "a" }
  457. * }
  458. * }
  459. * }
  460. * }
  461. */
  462. {
  463. 0xa4, 0x0e, 0x30, 0x0c, 0x31, 0x0a, 0x30, 0x08, 0x06, 0x03, 0x55,
  464. 0x04, 0x03, 0x0c, 0x01, 0x61
  465. },
  466. 16
  467. }, {
  468. /*
  469. * [4] {
  470. * SEQUENCE {
  471. * SET {
  472. * SEQUENCE {
  473. * # commonName
  474. * OBJECT_IDENTIFIER { 2.5.4.3 }
  475. * UTF8String { "b" }
  476. * }
  477. * }
  478. * }
  479. * }
  480. */
  481. {
  482. 0xa4, 0x0e, 0x30, 0x0c, 0x31, 0x0a, 0x30, 0x08, 0x06, 0x03, 0x55,
  483. 0x04, 0x03, 0x0c, 0x01, 0x62
  484. },
  485. 16
  486. }, {
  487. /*
  488. * [5] {
  489. * [1] {
  490. * UTF8String { "a" }
  491. * }
  492. * }
  493. */
  494. {
  495. 0xa5, 0x05, 0xa1, 0x03, 0x0c, 0x01, 0x61
  496. },
  497. 7
  498. }, {
  499. /*
  500. * [5] {
  501. * [1] {
  502. * UTF8String { "b" }
  503. * }
  504. * }
  505. */
  506. {
  507. 0xa5, 0x05, 0xa1, 0x03, 0x0c, 0x01, 0x62
  508. },
  509. 7
  510. }, {
  511. /*
  512. * [5] {
  513. * [0] {
  514. * UTF8String {}
  515. * }
  516. * [1] {
  517. * UTF8String { "a" }
  518. * }
  519. * }
  520. */
  521. {
  522. 0xa5, 0x09, 0xa0, 0x02, 0x0c, 0x00, 0xa1, 0x03, 0x0c, 0x01, 0x61
  523. },
  524. 11
  525. }, {
  526. /*
  527. * [5] {
  528. * [0] {
  529. * UTF8String { "a" }
  530. * }
  531. * [1] {
  532. * UTF8String { "a" }
  533. * }
  534. * }
  535. */
  536. {
  537. 0xa5, 0x0a, 0xa0, 0x03, 0x0c, 0x01, 0x61, 0xa1, 0x03, 0x0c, 0x01,
  538. 0x61
  539. },
  540. 12
  541. }, {
  542. /*
  543. * [5] {
  544. * [0] {
  545. * UTF8String { "b" }
  546. * }
  547. * [1] {
  548. * UTF8String { "a" }
  549. * }
  550. * }
  551. */
  552. {
  553. 0xa5, 0x0a, 0xa0, 0x03, 0x0c, 0x01, 0x62, 0xa1, 0x03, 0x0c, 0x01,
  554. 0x61
  555. },
  556. 12
  557. }, {
  558. /* [6 PRIMITIVE] { "a" } */
  559. {
  560. 0x86, 0x01, 0x61
  561. },
  562. 3
  563. }, {
  564. /* [6 PRIMITIVE] { "b" } */
  565. {
  566. 0x86, 0x01, 0x62
  567. },
  568. 3
  569. }, {
  570. /* [7 PRIMITIVE] { `11111111` } */
  571. {
  572. 0x87, 0x04, 0x11, 0x11, 0x11, 0x11
  573. },
  574. 6
  575. }, {
  576. /* [7 PRIMITIVE] { `22222222`} */
  577. {
  578. 0x87, 0x04, 0x22, 0x22, 0x22, 0x22
  579. },
  580. 6
  581. }, {
  582. /* [7 PRIMITIVE] { `11111111111111111111111111111111` } */
  583. {
  584. 0x87, 0x10, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  585. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11
  586. },
  587. 18
  588. }, {
  589. /* [7 PRIMITIVE] { `22222222222222222222222222222222` } */
  590. {
  591. 0x87, 0x10, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  592. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22
  593. },
  594. 18
  595. }, {
  596. /* [8 PRIMITIVE] { 1.2.840.113554.4.1.72585.2.1 } */
  597. {
  598. 0x88, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84,
  599. 0xb7, 0x09, 0x02, 0x01
  600. },
  601. 15
  602. }, {
  603. /* [8 PRIMITIVE] { 1.2.840.113554.4.1.72585.2.2 } */
  604. {
  605. 0x88, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84,
  606. 0xb7, 0x09, 0x02, 0x02
  607. },
  608. 15
  609. }
  610. };
  611. static int test_GENERAL_NAME_cmp(void)
  612. {
  613. size_t i, j;
  614. GENERAL_NAME **namesa = OPENSSL_malloc(sizeof(*namesa)
  615. * OSSL_NELEM(gennames));
  616. GENERAL_NAME **namesb = OPENSSL_malloc(sizeof(*namesb)
  617. * OSSL_NELEM(gennames));
  618. int testresult = 0;
  619. if (!TEST_ptr(namesa) || !TEST_ptr(namesb))
  620. goto end;
  621. for (i = 0; i < OSSL_NELEM(gennames); i++) {
  622. const unsigned char *derp = gennames[i].der;
  623. /*
  624. * We create two versions of each GENERAL_NAME so that we ensure when
  625. * we compare them they are always different pointers.
  626. */
  627. namesa[i] = d2i_GENERAL_NAME(NULL, &derp, gennames[i].derlen);
  628. derp = gennames[i].der;
  629. namesb[i] = d2i_GENERAL_NAME(NULL, &derp, gennames[i].derlen);
  630. if (!TEST_ptr(namesa[i]) || !TEST_ptr(namesb[i]))
  631. goto end;
  632. }
  633. /* Every name should be equal to itself and not equal to any others. */
  634. for (i = 0; i < OSSL_NELEM(gennames); i++) {
  635. for (j = 0; j < OSSL_NELEM(gennames); j++) {
  636. if (i == j) {
  637. if (!TEST_int_eq(GENERAL_NAME_cmp(namesa[i], namesb[j]), 0))
  638. goto end;
  639. } else {
  640. if (!TEST_int_ne(GENERAL_NAME_cmp(namesa[i], namesb[j]), 0))
  641. goto end;
  642. }
  643. }
  644. }
  645. testresult = 1;
  646. end:
  647. for (i = 0; i < OSSL_NELEM(gennames); i++) {
  648. if (namesa != NULL)
  649. GENERAL_NAME_free(namesa[i]);
  650. if (namesb != NULL)
  651. GENERAL_NAME_free(namesb[i]);
  652. }
  653. OPENSSL_free(namesa);
  654. OPENSSL_free(namesb);
  655. return testresult;
  656. }
  657. int setup_tests(void)
  658. {
  659. ADD_ALL_TESTS(call_run_cert, OSSL_NELEM(name_fns));
  660. ADD_TEST(test_GENERAL_NAME_cmp);
  661. return 1;
  662. }