2
0

v3ext.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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. if (!sk_IPAddressFamily_push(addr, f1))
  235. goto end;
  236. /* Shouldn't be able to canonize this as the len is > 3*/
  237. if (!TEST_false(X509v3_addr_canonize(addr)))
  238. goto end;
  239. /* Create a well formed IPAddressFamily */
  240. f1 = sk_IPAddressFamily_pop(addr);
  241. IPAddressFamily_free(f1);
  242. key[0] = (afi >> 8) & 0xFF;
  243. key[1] = afi & 0xFF;
  244. key[2] = 0x1;
  245. keylen = 3;
  246. if ((f1 = IPAddressFamily_new()) == NULL)
  247. goto end;
  248. if (f1->ipAddressChoice == NULL &&
  249. (f1->ipAddressChoice = IPAddressChoice_new()) == NULL)
  250. goto end;
  251. if (f1->addressFamily == NULL &&
  252. (f1->addressFamily = ASN1_OCTET_STRING_new()) == NULL)
  253. goto end;
  254. if (!ASN1_OCTET_STRING_set(f1->addressFamily, key, keylen))
  255. goto end;
  256. /* Mark this as inheritance so we skip some of the is_canonize checks */
  257. f1->ipAddressChoice->type = IPAddressChoice_inherit;
  258. if (!sk_IPAddressFamily_push(addr, f1))
  259. goto end;
  260. /* Should be able to canonize now */
  261. if (!TEST_true(X509v3_addr_canonize(addr)))
  262. goto end;
  263. testresult = 1;
  264. end:
  265. sk_IPAddressFamily_pop_free(addr, IPAddressFamily_free);
  266. ASN1_OCTET_STRING_free(ip1);
  267. ASN1_OCTET_STRING_free(ip2);
  268. return testresult;
  269. }
  270. static struct extvalues_st {
  271. const char *value;
  272. int pass;
  273. } extvalues[] = {
  274. /* No prefix is ok */
  275. { "sbgp-ipAddrBlock = IPv4:192.0.0.1\n", 1 },
  276. { "sbgp-ipAddrBlock = IPv4:192.0.0.0/0\n", 1 },
  277. { "sbgp-ipAddrBlock = IPv4:192.0.0.0/1\n", 1 },
  278. { "sbgp-ipAddrBlock = IPv4:192.0.0.0/32\n", 1 },
  279. /* Prefix is too long */
  280. { "sbgp-ipAddrBlock = IPv4:192.0.0.0/33\n", 0 },
  281. /* Unreasonably large prefix */
  282. { "sbgp-ipAddrBlock = IPv4:192.0.0.0/12341234\n", 0 },
  283. /* Invalid IP addresses */
  284. { "sbgp-ipAddrBlock = IPv4:192.0.0\n", 0 },
  285. { "sbgp-ipAddrBlock = IPv4:256.0.0.0\n", 0 },
  286. { "sbgp-ipAddrBlock = IPv4:-1.0.0.0\n", 0 },
  287. { "sbgp-ipAddrBlock = IPv4:192.0.0.0.0\n", 0 },
  288. { "sbgp-ipAddrBlock = IPv3:192.0.0.0\n", 0 },
  289. /* IPv6 */
  290. /* No prefix is ok */
  291. { "sbgp-ipAddrBlock = IPv6:2001:db8::\n", 1 },
  292. { "sbgp-ipAddrBlock = IPv6:2001::db8\n", 1 },
  293. { "sbgp-ipAddrBlock = IPv6:2001:0db8:0000:0000:0000:0000:0000:0000\n", 1 },
  294. { "sbgp-ipAddrBlock = IPv6:2001:db8::/0\n", 1 },
  295. { "sbgp-ipAddrBlock = IPv6:2001:db8::/1\n", 1 },
  296. { "sbgp-ipAddrBlock = IPv6:2001:db8::/32\n", 1 },
  297. { "sbgp-ipAddrBlock = IPv6:2001:0db8:0000:0000:0000:0000:0000:0000/32\n", 1 },
  298. { "sbgp-ipAddrBlock = IPv6:2001:db8::/128\n", 1 },
  299. /* Prefix is too long */
  300. { "sbgp-ipAddrBlock = IPv6:2001:db8::/129\n", 0 },
  301. /* Unreasonably large prefix */
  302. { "sbgp-ipAddrBlock = IPv6:2001:db8::/12341234\n", 0 },
  303. /* Invalid IP addresses */
  304. /* Not enough blocks of numbers */
  305. { "sbgp-ipAddrBlock = IPv6:2001:0db8:0000:0000:0000:0000:0000\n", 0 },
  306. /* Too many blocks of numbers */
  307. { "sbgp-ipAddrBlock = IPv6:2001:0db8:0000:0000:0000:0000:0000:0000:0000\n", 0 },
  308. /* First value too large */
  309. { "sbgp-ipAddrBlock = IPv6:1ffff:0db8:0000:0000:0000:0000:0000:0000\n", 0 },
  310. /* First value with invalid characters */
  311. { "sbgp-ipAddrBlock = IPv6:fffg:0db8:0000:0000:0000:0000:0000:0000\n", 0 },
  312. /* First value is negative */
  313. { "sbgp-ipAddrBlock = IPv6:-1:0db8:0000:0000:0000:0000:0000:0000\n", 0 }
  314. };
  315. static int test_ext_syntax(void)
  316. {
  317. size_t i;
  318. int testresult = 1;
  319. for (i = 0; i < OSSL_NELEM(extvalues); i++) {
  320. X509V3_CTX ctx;
  321. BIO *extbio = BIO_new_mem_buf(extvalues[i].value,
  322. strlen(extvalues[i].value));
  323. CONF *conf;
  324. long eline;
  325. if (!TEST_ptr(extbio))
  326. return 0 ;
  327. conf = NCONF_new_ex(NULL, NULL);
  328. if (!TEST_ptr(conf)) {
  329. BIO_free(extbio);
  330. return 0;
  331. }
  332. if (!TEST_long_gt(NCONF_load_bio(conf, extbio, &eline), 0)) {
  333. testresult = 0;
  334. } else {
  335. X509V3_set_ctx_test(&ctx);
  336. X509V3_set_nconf(&ctx, conf);
  337. if (extvalues[i].pass) {
  338. if (!TEST_true(X509V3_EXT_add_nconf(conf, &ctx, "default",
  339. NULL))) {
  340. TEST_info("Value: %s", extvalues[i].value);
  341. testresult = 0;
  342. }
  343. } else {
  344. ERR_set_mark();
  345. if (!TEST_false(X509V3_EXT_add_nconf(conf, &ctx, "default",
  346. NULL))) {
  347. testresult = 0;
  348. TEST_info("Value: %s", extvalues[i].value);
  349. ERR_clear_last_mark();
  350. } else {
  351. ERR_pop_to_mark();
  352. }
  353. }
  354. }
  355. BIO_free(extbio);
  356. NCONF_free(conf);
  357. }
  358. return testresult;
  359. }
  360. static int test_addr_subset(void)
  361. {
  362. int i;
  363. int ret = 0;
  364. IPAddrBlocks *addrEmpty = NULL;
  365. IPAddrBlocks *addr[3] = { NULL, NULL };
  366. ASN1_OCTET_STRING *ip1[3] = { NULL, NULL };
  367. ASN1_OCTET_STRING *ip2[3] = { NULL, NULL };
  368. int sz = OSSL_NELEM(addr);
  369. for (i = 0; i < sz; ++i) {
  370. /* Create the IPAddrBlocks with a good IPAddressFamily */
  371. if (!TEST_ptr(addr[i] = sk_IPAddressFamily_new_null())
  372. || !TEST_ptr(ip1[i] = a2i_IPADDRESS(ranges[i].ip1))
  373. || !TEST_ptr(ip2[i] = a2i_IPADDRESS(ranges[i].ip2))
  374. || !TEST_true(X509v3_addr_add_range(addr[i], ranges[i].afi, NULL,
  375. ip1[i]->data, ip2[i]->data)))
  376. goto end;
  377. }
  378. ret = TEST_ptr(addrEmpty = sk_IPAddressFamily_new_null())
  379. && TEST_true(X509v3_addr_subset(NULL, NULL))
  380. && TEST_true(X509v3_addr_subset(NULL, addr[0]))
  381. && TEST_true(X509v3_addr_subset(addrEmpty, addr[0]))
  382. && TEST_true(X509v3_addr_subset(addr[0], addr[0]))
  383. && TEST_true(X509v3_addr_subset(addr[0], addr[1]))
  384. && TEST_true(X509v3_addr_subset(addr[0], addr[2]))
  385. && TEST_true(X509v3_addr_subset(addr[1], addr[2]))
  386. && TEST_false(X509v3_addr_subset(addr[0], NULL))
  387. && TEST_false(X509v3_addr_subset(addr[1], addr[0]))
  388. && TEST_false(X509v3_addr_subset(addr[2], addr[1]))
  389. && TEST_false(X509v3_addr_subset(addr[0], addrEmpty));
  390. end:
  391. sk_IPAddressFamily_pop_free(addrEmpty, IPAddressFamily_free);
  392. for (i = 0; i < sz; ++i) {
  393. sk_IPAddressFamily_pop_free(addr[i], IPAddressFamily_free);
  394. ASN1_OCTET_STRING_free(ip1[i]);
  395. ASN1_OCTET_STRING_free(ip2[i]);
  396. }
  397. return ret;
  398. }
  399. #endif /* OPENSSL_NO_RFC3779 */
  400. OPT_TEST_DECLARE_USAGE("cert.pem\n")
  401. int setup_tests(void)
  402. {
  403. if (!test_skip_common_options()) {
  404. TEST_error("Error parsing test options\n");
  405. return 0;
  406. }
  407. if (!TEST_ptr(infile = test_get_argument(0)))
  408. return 0;
  409. ADD_TEST(test_pathlen);
  410. #ifndef OPENSSL_NO_RFC3779
  411. ADD_TEST(test_asid);
  412. ADD_TEST(test_addr_ranges);
  413. ADD_TEST(test_ext_syntax);
  414. ADD_TEST(test_addr_fam_len);
  415. ADD_TEST(test_addr_subset);
  416. #endif /* OPENSSL_NO_RFC3779 */
  417. return 1;
  418. }