v3ext.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /*
  2. * Copyright 2016-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 <stdio.h>
  10. #include <string.h>
  11. #include <openssl/x509.h>
  12. #include <openssl/x509v3.h>
  13. #include <openssl/pem.h>
  14. #include <openssl/err.h>
  15. #include "internal/nelem.h"
  16. #include "testutil.h"
  17. static const char *infile;
  18. static int test_pathlen(void)
  19. {
  20. X509 *x = NULL;
  21. BIO *b = NULL;
  22. long pathlen;
  23. int ret = 0;
  24. if (!TEST_ptr(b = BIO_new_file(infile, "r"))
  25. || !TEST_ptr(x = PEM_read_bio_X509(b, NULL, NULL, NULL))
  26. || !TEST_int_eq(pathlen = X509_get_pathlen(x), 6))
  27. goto end;
  28. ret = 1;
  29. end:
  30. BIO_free(b);
  31. X509_free(x);
  32. return ret;
  33. }
  34. #ifndef OPENSSL_NO_RFC3779
  35. static int test_asid(void)
  36. {
  37. ASN1_INTEGER *val1 = NULL, *val2 = NULL;
  38. ASIdentifiers *asid1 = ASIdentifiers_new(), *asid2 = ASIdentifiers_new(),
  39. *asid3 = ASIdentifiers_new(), *asid4 = ASIdentifiers_new();
  40. int testresult = 0;
  41. if (!TEST_ptr(asid1)
  42. || !TEST_ptr(asid2)
  43. || !TEST_ptr(asid3))
  44. goto err;
  45. if (!TEST_ptr(val1 = ASN1_INTEGER_new())
  46. || !TEST_true(ASN1_INTEGER_set_int64(val1, 64496)))
  47. goto err;
  48. if (!TEST_true(X509v3_asid_add_id_or_range(asid1, V3_ASID_ASNUM, val1, NULL)))
  49. goto err;
  50. val1 = NULL;
  51. if (!TEST_ptr(val2 = ASN1_INTEGER_new())
  52. || !TEST_true(ASN1_INTEGER_set_int64(val2, 64497)))
  53. goto err;
  54. if (!TEST_true(X509v3_asid_add_id_or_range(asid2, V3_ASID_ASNUM, val2, NULL)))
  55. goto err;
  56. val2 = NULL;
  57. if (!TEST_ptr(val1 = ASN1_INTEGER_new())
  58. || !TEST_true(ASN1_INTEGER_set_int64(val1, 64496))
  59. || !TEST_ptr(val2 = ASN1_INTEGER_new())
  60. || !TEST_true(ASN1_INTEGER_set_int64(val2, 64497)))
  61. goto err;
  62. /*
  63. * Just tests V3_ASID_ASNUM for now. Could be extended at some point to also
  64. * test V3_ASID_RDI if we think it is worth it.
  65. */
  66. if (!TEST_true(X509v3_asid_add_id_or_range(asid3, V3_ASID_ASNUM, val1, val2)))
  67. goto err;
  68. val1 = val2 = NULL;
  69. /* Actual subsets */
  70. if (!TEST_true(X509v3_asid_subset(NULL, NULL))
  71. || !TEST_true(X509v3_asid_subset(NULL, asid1))
  72. || !TEST_true(X509v3_asid_subset(asid1, asid1))
  73. || !TEST_true(X509v3_asid_subset(asid2, asid2))
  74. || !TEST_true(X509v3_asid_subset(asid1, asid3))
  75. || !TEST_true(X509v3_asid_subset(asid2, asid3))
  76. || !TEST_true(X509v3_asid_subset(asid3, asid3))
  77. || !TEST_true(X509v3_asid_subset(asid4, asid1))
  78. || !TEST_true(X509v3_asid_subset(asid4, asid2))
  79. || !TEST_true(X509v3_asid_subset(asid4, asid3)))
  80. goto err;
  81. /* Not subsets */
  82. if (!TEST_false(X509v3_asid_subset(asid1, NULL))
  83. || !TEST_false(X509v3_asid_subset(asid1, asid2))
  84. || !TEST_false(X509v3_asid_subset(asid2, asid1))
  85. || !TEST_false(X509v3_asid_subset(asid3, asid1))
  86. || !TEST_false(X509v3_asid_subset(asid3, asid2))
  87. || !TEST_false(X509v3_asid_subset(asid1, asid4))
  88. || !TEST_false(X509v3_asid_subset(asid2, asid4))
  89. || !TEST_false(X509v3_asid_subset(asid3, asid4)))
  90. goto err;
  91. testresult = 1;
  92. err:
  93. ASN1_INTEGER_free(val1);
  94. ASN1_INTEGER_free(val2);
  95. ASIdentifiers_free(asid1);
  96. ASIdentifiers_free(asid2);
  97. ASIdentifiers_free(asid3);
  98. ASIdentifiers_free(asid4);
  99. return testresult;
  100. }
  101. static struct ip_ranges_st {
  102. const unsigned int afi;
  103. const char *ip1;
  104. const char *ip2;
  105. int rorp;
  106. } ranges[] = {
  107. { IANA_AFI_IPV4, "192.168.0.0", "192.168.0.1", IPAddressOrRange_addressPrefix},
  108. { IANA_AFI_IPV4, "192.168.0.0", "192.168.0.2", IPAddressOrRange_addressRange},
  109. { IANA_AFI_IPV4, "192.168.0.0", "192.168.0.3", IPAddressOrRange_addressPrefix},
  110. { IANA_AFI_IPV4, "192.168.0.0", "192.168.0.254", IPAddressOrRange_addressRange},
  111. { IANA_AFI_IPV4, "192.168.0.0", "192.168.0.255", IPAddressOrRange_addressPrefix},
  112. { IANA_AFI_IPV4, "192.168.0.1", "192.168.0.255", IPAddressOrRange_addressRange},
  113. { IANA_AFI_IPV4, "192.168.0.1", "192.168.0.1", IPAddressOrRange_addressPrefix},
  114. { IANA_AFI_IPV4, "192.168.0.0", "192.168.255.255", IPAddressOrRange_addressPrefix},
  115. { IANA_AFI_IPV4, "192.168.1.0", "192.168.255.255", IPAddressOrRange_addressRange},
  116. { IANA_AFI_IPV6, "2001:0db8::0", "2001:0db8::1", IPAddressOrRange_addressPrefix},
  117. { IANA_AFI_IPV6, "2001:0db8::0", "2001:0db8::2", IPAddressOrRange_addressRange},
  118. { IANA_AFI_IPV6, "2001:0db8::0", "2001:0db8::3", IPAddressOrRange_addressPrefix},
  119. { IANA_AFI_IPV6, "2001:0db8::0", "2001:0db8::fffe", IPAddressOrRange_addressRange},
  120. { IANA_AFI_IPV6, "2001:0db8::0", "2001:0db8::ffff", IPAddressOrRange_addressPrefix},
  121. { IANA_AFI_IPV6, "2001:0db8::1", "2001:0db8::ffff", IPAddressOrRange_addressRange},
  122. { IANA_AFI_IPV6, "2001:0db8::1", "2001:0db8::1", IPAddressOrRange_addressPrefix},
  123. { IANA_AFI_IPV6, "2001:0db8::0:0", "2001:0db8::ffff:ffff", IPAddressOrRange_addressPrefix},
  124. { IANA_AFI_IPV6, "2001:0db8::1:0", "2001:0db8::ffff:ffff", IPAddressOrRange_addressRange}
  125. };
  126. static int check_addr(IPAddrBlocks *addr, int type)
  127. {
  128. IPAddressFamily *fam;
  129. IPAddressOrRange *aorr;
  130. if (!TEST_int_eq(sk_IPAddressFamily_num(addr), 1))
  131. return 0;
  132. fam = sk_IPAddressFamily_value(addr, 0);
  133. if (!TEST_ptr(fam))
  134. return 0;
  135. if (!TEST_int_eq(fam->ipAddressChoice->type, IPAddressChoice_addressesOrRanges))
  136. return 0;
  137. if (!TEST_int_eq(sk_IPAddressOrRange_num(fam->ipAddressChoice->u.addressesOrRanges), 1))
  138. return 0;
  139. aorr = sk_IPAddressOrRange_value(fam->ipAddressChoice->u.addressesOrRanges, 0);
  140. if (!TEST_ptr(aorr))
  141. return 0;
  142. if (!TEST_int_eq(aorr->type, type))
  143. return 0;
  144. return 1;
  145. }
  146. static int test_addr_ranges(void)
  147. {
  148. IPAddrBlocks *addr = NULL;
  149. ASN1_OCTET_STRING *ip1 = NULL, *ip2 = NULL;
  150. size_t i;
  151. int testresult = 0;
  152. for (i = 0; i < OSSL_NELEM(ranges); i++) {
  153. addr = sk_IPAddressFamily_new_null();
  154. if (!TEST_ptr(addr))
  155. goto end;
  156. /*
  157. * Has the side effect of installing the comparison function onto the
  158. * stack.
  159. */
  160. if (!TEST_true(X509v3_addr_canonize(addr)))
  161. goto end;
  162. ip1 = a2i_IPADDRESS(ranges[i].ip1);
  163. if (!TEST_ptr(ip1))
  164. goto end;
  165. if (!TEST_true(ip1->length == 4 || ip1->length == 16))
  166. goto end;
  167. ip2 = a2i_IPADDRESS(ranges[i].ip2);
  168. if (!TEST_ptr(ip2))
  169. goto end;
  170. if (!TEST_int_eq(ip2->length, ip1->length))
  171. goto end;
  172. if (!TEST_true(memcmp(ip1->data, ip2->data, ip1->length) <= 0))
  173. goto end;
  174. if (!TEST_true(X509v3_addr_add_range(addr, ranges[i].afi, NULL, ip1->data, ip2->data)))
  175. goto end;
  176. if (!TEST_true(X509v3_addr_is_canonical(addr)))
  177. goto end;
  178. if (!check_addr(addr, ranges[i].rorp))
  179. goto end;
  180. sk_IPAddressFamily_pop_free(addr, IPAddressFamily_free);
  181. addr = NULL;
  182. ASN1_OCTET_STRING_free(ip1);
  183. ASN1_OCTET_STRING_free(ip2);
  184. ip1 = ip2 = NULL;
  185. }
  186. testresult = 1;
  187. end:
  188. sk_IPAddressFamily_pop_free(addr, IPAddressFamily_free);
  189. ASN1_OCTET_STRING_free(ip1);
  190. ASN1_OCTET_STRING_free(ip2);
  191. return testresult;
  192. }
  193. static int test_addr_fam_len(void)
  194. {
  195. int testresult = 0;
  196. IPAddrBlocks *addr = NULL;
  197. IPAddressFamily *f1 = NULL;
  198. ASN1_OCTET_STRING *ip1 = NULL, *ip2 = NULL;
  199. unsigned char key[6];
  200. unsigned int keylen;
  201. unsigned afi = IANA_AFI_IPV4;
  202. /* Create the IPAddrBlocks with a good IPAddressFamily */
  203. addr = sk_IPAddressFamily_new_null();
  204. if (!TEST_ptr(addr))
  205. goto end;
  206. ip1 = a2i_IPADDRESS(ranges[0].ip1);
  207. if (!TEST_ptr(ip1))
  208. goto end;
  209. ip2 = a2i_IPADDRESS(ranges[0].ip2);
  210. if (!TEST_ptr(ip2))
  211. goto end;
  212. if (!TEST_true(X509v3_addr_add_range(addr, ranges[0].afi, NULL, ip1->data, ip2->data)))
  213. goto end;
  214. if (!TEST_true(X509v3_addr_is_canonical(addr)))
  215. goto end;
  216. /* Create our malformed IPAddressFamily */
  217. key[0] = (afi >> 8) & 0xFF;
  218. key[1] = afi & 0xFF;
  219. key[2] = 0xD;
  220. key[3] = 0xE;
  221. key[4] = 0xA;
  222. key[5] = 0xD;
  223. keylen = 6;
  224. if ((f1 = IPAddressFamily_new()) == NULL)
  225. goto end;
  226. if (f1->ipAddressChoice == NULL &&
  227. (f1->ipAddressChoice = IPAddressChoice_new()) == NULL)
  228. goto end;
  229. if (f1->addressFamily == NULL &&
  230. (f1->addressFamily = ASN1_OCTET_STRING_new()) == NULL)
  231. goto end;
  232. if (!ASN1_OCTET_STRING_set(f1->addressFamily, key, keylen))
  233. goto end;
  234. /* Push and transfer memory ownership to stack */
  235. if (!sk_IPAddressFamily_push(addr, f1))
  236. goto end;
  237. f1 = NULL;
  238. /* Shouldn't be able to canonize this as the len is > 3*/
  239. if (!TEST_false(X509v3_addr_canonize(addr)))
  240. goto end;
  241. /* Pop and free the new stack element */
  242. IPAddressFamily_free(sk_IPAddressFamily_pop(addr));
  243. /* Create a well-formed IPAddressFamily */
  244. key[0] = (afi >> 8) & 0xFF;
  245. key[1] = afi & 0xFF;
  246. key[2] = 0x1;
  247. keylen = 3;
  248. if ((f1 = IPAddressFamily_new()) == NULL)
  249. goto end;
  250. if (f1->ipAddressChoice == NULL &&
  251. (f1->ipAddressChoice = IPAddressChoice_new()) == NULL)
  252. goto end;
  253. if (f1->addressFamily == NULL &&
  254. (f1->addressFamily = ASN1_OCTET_STRING_new()) == NULL)
  255. goto end;
  256. if (!ASN1_OCTET_STRING_set(f1->addressFamily, key, keylen))
  257. goto end;
  258. /* Mark this as inheritance so we skip some of the is_canonize checks */
  259. f1->ipAddressChoice->type = IPAddressChoice_inherit;
  260. /* Push and transfer memory ownership to stack */
  261. if (!sk_IPAddressFamily_push(addr, f1))
  262. goto end;
  263. f1 = NULL;
  264. /* Should be able to canonize now */
  265. if (!TEST_true(X509v3_addr_canonize(addr)))
  266. goto end;
  267. testresult = 1;
  268. end:
  269. /* Free stack and any memory owned by detached element */
  270. IPAddressFamily_free(f1);
  271. sk_IPAddressFamily_pop_free(addr, IPAddressFamily_free);
  272. ASN1_OCTET_STRING_free(ip1);
  273. ASN1_OCTET_STRING_free(ip2);
  274. return testresult;
  275. }
  276. static struct extvalues_st {
  277. const char *value;
  278. int pass;
  279. } extvalues[] = {
  280. /* No prefix is ok */
  281. { "sbgp-ipAddrBlock = IPv4:192.0.0.1\n", 1 },
  282. { "sbgp-ipAddrBlock = IPv4:192.0.0.0/0\n", 1 },
  283. { "sbgp-ipAddrBlock = IPv4:192.0.0.0/1\n", 1 },
  284. { "sbgp-ipAddrBlock = IPv4:192.0.0.0/32\n", 1 },
  285. /* Prefix is too long */
  286. { "sbgp-ipAddrBlock = IPv4:192.0.0.0/33\n", 0 },
  287. /* Unreasonably large prefix */
  288. { "sbgp-ipAddrBlock = IPv4:192.0.0.0/12341234\n", 0 },
  289. /* Invalid IP addresses */
  290. { "sbgp-ipAddrBlock = IPv4:192.0.0\n", 0 },
  291. { "sbgp-ipAddrBlock = IPv4:256.0.0.0\n", 0 },
  292. { "sbgp-ipAddrBlock = IPv4:-1.0.0.0\n", 0 },
  293. { "sbgp-ipAddrBlock = IPv4:192.0.0.0.0\n", 0 },
  294. { "sbgp-ipAddrBlock = IPv3:192.0.0.0\n", 0 },
  295. /* IPv6 */
  296. /* No prefix is ok */
  297. { "sbgp-ipAddrBlock = IPv6:2001:db8::\n", 1 },
  298. { "sbgp-ipAddrBlock = IPv6:2001::db8\n", 1 },
  299. { "sbgp-ipAddrBlock = IPv6:2001:0db8:0000:0000:0000:0000:0000:0000\n", 1 },
  300. { "sbgp-ipAddrBlock = IPv6:2001:db8::/0\n", 1 },
  301. { "sbgp-ipAddrBlock = IPv6:2001:db8::/1\n", 1 },
  302. { "sbgp-ipAddrBlock = IPv6:2001:db8::/32\n", 1 },
  303. { "sbgp-ipAddrBlock = IPv6:2001:0db8:0000:0000:0000:0000:0000:0000/32\n", 1 },
  304. { "sbgp-ipAddrBlock = IPv6:2001:db8::/128\n", 1 },
  305. /* Prefix is too long */
  306. { "sbgp-ipAddrBlock = IPv6:2001:db8::/129\n", 0 },
  307. /* Unreasonably large prefix */
  308. { "sbgp-ipAddrBlock = IPv6:2001:db8::/12341234\n", 0 },
  309. /* Invalid IP addresses */
  310. /* Not enough blocks of numbers */
  311. { "sbgp-ipAddrBlock = IPv6:2001:0db8:0000:0000:0000:0000:0000\n", 0 },
  312. /* Too many blocks of numbers */
  313. { "sbgp-ipAddrBlock = IPv6:2001:0db8:0000:0000:0000:0000:0000:0000:0000\n", 0 },
  314. /* First value too large */
  315. { "sbgp-ipAddrBlock = IPv6:1ffff:0db8:0000:0000:0000:0000:0000:0000\n", 0 },
  316. /* First value with invalid characters */
  317. { "sbgp-ipAddrBlock = IPv6:fffg:0db8:0000:0000:0000:0000:0000:0000\n", 0 },
  318. /* First value is negative */
  319. { "sbgp-ipAddrBlock = IPv6:-1:0db8:0000:0000:0000:0000:0000:0000\n", 0 }
  320. };
  321. static int test_ext_syntax(void)
  322. {
  323. size_t i;
  324. int testresult = 1;
  325. for (i = 0; i < OSSL_NELEM(extvalues); i++) {
  326. X509V3_CTX ctx;
  327. BIO *extbio = BIO_new_mem_buf(extvalues[i].value,
  328. strlen(extvalues[i].value));
  329. CONF *conf;
  330. long eline;
  331. if (!TEST_ptr(extbio))
  332. return 0 ;
  333. conf = NCONF_new_ex(NULL, NULL);
  334. if (!TEST_ptr(conf)) {
  335. BIO_free(extbio);
  336. return 0;
  337. }
  338. if (!TEST_long_gt(NCONF_load_bio(conf, extbio, &eline), 0)) {
  339. testresult = 0;
  340. } else {
  341. X509V3_set_ctx_test(&ctx);
  342. X509V3_set_nconf(&ctx, conf);
  343. if (extvalues[i].pass) {
  344. if (!TEST_true(X509V3_EXT_add_nconf(conf, &ctx, "default",
  345. NULL))) {
  346. TEST_info("Value: %s", extvalues[i].value);
  347. testresult = 0;
  348. }
  349. } else {
  350. ERR_set_mark();
  351. if (!TEST_false(X509V3_EXT_add_nconf(conf, &ctx, "default",
  352. NULL))) {
  353. testresult = 0;
  354. TEST_info("Value: %s", extvalues[i].value);
  355. ERR_clear_last_mark();
  356. } else {
  357. ERR_pop_to_mark();
  358. }
  359. }
  360. }
  361. BIO_free(extbio);
  362. NCONF_free(conf);
  363. }
  364. return testresult;
  365. }
  366. static int test_addr_subset(void)
  367. {
  368. int i;
  369. int ret = 0;
  370. IPAddrBlocks *addrEmpty = NULL;
  371. IPAddrBlocks *addr[3] = { NULL, NULL };
  372. ASN1_OCTET_STRING *ip1[3] = { NULL, NULL };
  373. ASN1_OCTET_STRING *ip2[3] = { NULL, NULL };
  374. int sz = OSSL_NELEM(addr);
  375. for (i = 0; i < sz; ++i) {
  376. /* Create the IPAddrBlocks with a good IPAddressFamily */
  377. if (!TEST_ptr(addr[i] = sk_IPAddressFamily_new_null())
  378. || !TEST_ptr(ip1[i] = a2i_IPADDRESS(ranges[i].ip1))
  379. || !TEST_ptr(ip2[i] = a2i_IPADDRESS(ranges[i].ip2))
  380. || !TEST_true(X509v3_addr_add_range(addr[i], ranges[i].afi, NULL,
  381. ip1[i]->data, ip2[i]->data)))
  382. goto end;
  383. }
  384. ret = TEST_ptr(addrEmpty = sk_IPAddressFamily_new_null())
  385. && TEST_true(X509v3_addr_subset(NULL, NULL))
  386. && TEST_true(X509v3_addr_subset(NULL, addr[0]))
  387. && TEST_true(X509v3_addr_subset(addrEmpty, addr[0]))
  388. && TEST_true(X509v3_addr_subset(addr[0], addr[0]))
  389. && TEST_true(X509v3_addr_subset(addr[0], addr[1]))
  390. && TEST_true(X509v3_addr_subset(addr[0], addr[2]))
  391. && TEST_true(X509v3_addr_subset(addr[1], addr[2]))
  392. && TEST_false(X509v3_addr_subset(addr[0], NULL))
  393. && TEST_false(X509v3_addr_subset(addr[1], addr[0]))
  394. && TEST_false(X509v3_addr_subset(addr[2], addr[1]))
  395. && TEST_false(X509v3_addr_subset(addr[0], addrEmpty));
  396. end:
  397. sk_IPAddressFamily_pop_free(addrEmpty, IPAddressFamily_free);
  398. for (i = 0; i < sz; ++i) {
  399. sk_IPAddressFamily_pop_free(addr[i], IPAddressFamily_free);
  400. ASN1_OCTET_STRING_free(ip1[i]);
  401. ASN1_OCTET_STRING_free(ip2[i]);
  402. }
  403. return ret;
  404. }
  405. #endif /* OPENSSL_NO_RFC3779 */
  406. OPT_TEST_DECLARE_USAGE("cert.pem\n")
  407. int setup_tests(void)
  408. {
  409. if (!test_skip_common_options()) {
  410. TEST_error("Error parsing test options\n");
  411. return 0;
  412. }
  413. if (!TEST_ptr(infile = test_get_argument(0)))
  414. return 0;
  415. ADD_TEST(test_pathlen);
  416. #ifndef OPENSSL_NO_RFC3779
  417. ADD_TEST(test_asid);
  418. ADD_TEST(test_addr_ranges);
  419. ADD_TEST(test_ext_syntax);
  420. ADD_TEST(test_addr_fam_len);
  421. ADD_TEST(test_addr_subset);
  422. #endif /* OPENSSL_NO_RFC3779 */
  423. return 1;
  424. }