v3ext.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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 struct extvalues_st {
  194. const char *value;
  195. int pass;
  196. } extvalues[] = {
  197. /* No prefix is ok */
  198. { "sbgp-ipAddrBlock = IPv4:192.0.0.1\n", 1 },
  199. { "sbgp-ipAddrBlock = IPv4:192.0.0.0/0\n", 1 },
  200. { "sbgp-ipAddrBlock = IPv4:192.0.0.0/1\n", 1 },
  201. { "sbgp-ipAddrBlock = IPv4:192.0.0.0/32\n", 1 },
  202. /* Prefix is too long */
  203. { "sbgp-ipAddrBlock = IPv4:192.0.0.0/33\n", 0 },
  204. /* Unreasonably large prefix */
  205. { "sbgp-ipAddrBlock = IPv4:192.0.0.0/12341234\n", 0 },
  206. /* Invalid IP addresses */
  207. { "sbgp-ipAddrBlock = IPv4:192.0.0\n", 0 },
  208. { "sbgp-ipAddrBlock = IPv4:256.0.0.0\n", 0 },
  209. { "sbgp-ipAddrBlock = IPv4:-1.0.0.0\n", 0 },
  210. { "sbgp-ipAddrBlock = IPv4:192.0.0.0.0\n", 0 },
  211. { "sbgp-ipAddrBlock = IPv3:192.0.0.0\n", 0 },
  212. /* IPv6 */
  213. /* No prefix is ok */
  214. { "sbgp-ipAddrBlock = IPv6:2001:db8::\n", 1 },
  215. { "sbgp-ipAddrBlock = IPv6:2001::db8\n", 1 },
  216. { "sbgp-ipAddrBlock = IPv6:2001:0db8:0000:0000:0000:0000:0000:0000\n", 1 },
  217. { "sbgp-ipAddrBlock = IPv6:2001:db8::/0\n", 1 },
  218. { "sbgp-ipAddrBlock = IPv6:2001:db8::/1\n", 1 },
  219. { "sbgp-ipAddrBlock = IPv6:2001:db8::/32\n", 1 },
  220. { "sbgp-ipAddrBlock = IPv6:2001:0db8:0000:0000:0000:0000:0000:0000/32\n", 1 },
  221. { "sbgp-ipAddrBlock = IPv6:2001:db8::/128\n", 1 },
  222. /* Prefix is too long */
  223. { "sbgp-ipAddrBlock = IPv6:2001:db8::/129\n", 0 },
  224. /* Unreasonably large prefix */
  225. { "sbgp-ipAddrBlock = IPv6:2001:db8::/12341234\n", 0 },
  226. /* Invalid IP addresses */
  227. /* Not enough blocks of numbers */
  228. { "sbgp-ipAddrBlock = IPv6:2001:0db8:0000:0000:0000:0000:0000\n", 0 },
  229. /* Too many blocks of numbers */
  230. { "sbgp-ipAddrBlock = IPv6:2001:0db8:0000:0000:0000:0000:0000:0000:0000\n", 0 },
  231. /* First value too large */
  232. { "sbgp-ipAddrBlock = IPv6:1ffff:0db8:0000:0000:0000:0000:0000:0000\n", 0 },
  233. /* First value with invalid characters */
  234. { "sbgp-ipAddrBlock = IPv6:fffg:0db8:0000:0000:0000:0000:0000:0000\n", 0 },
  235. /* First value is negative */
  236. { "sbgp-ipAddrBlock = IPv6:-1:0db8:0000:0000:0000:0000:0000:0000\n", 0 }
  237. };
  238. static int test_ext_syntax(void)
  239. {
  240. size_t i;
  241. int testresult = 1;
  242. for (i = 0; i < OSSL_NELEM(extvalues); i++) {
  243. X509V3_CTX ctx;
  244. BIO *extbio = BIO_new_mem_buf(extvalues[i].value,
  245. strlen(extvalues[i].value));
  246. CONF *conf;
  247. long eline;
  248. if (!TEST_ptr(extbio))
  249. return 0 ;
  250. conf = NCONF_new_ex(NULL, NULL);
  251. if (!TEST_ptr(conf)) {
  252. BIO_free(extbio);
  253. return 0;
  254. }
  255. if (!TEST_long_gt(NCONF_load_bio(conf, extbio, &eline), 0)) {
  256. testresult = 0;
  257. } else {
  258. X509V3_set_ctx_test(&ctx);
  259. X509V3_set_nconf(&ctx, conf);
  260. if (extvalues[i].pass) {
  261. if (!TEST_true(X509V3_EXT_add_nconf(conf, &ctx, "default",
  262. NULL))) {
  263. TEST_info("Value: %s", extvalues[i].value);
  264. testresult = 0;
  265. }
  266. } else {
  267. ERR_set_mark();
  268. if (!TEST_false(X509V3_EXT_add_nconf(conf, &ctx, "default",
  269. NULL))) {
  270. testresult = 0;
  271. TEST_info("Value: %s", extvalues[i].value);
  272. ERR_clear_last_mark();
  273. } else {
  274. ERR_pop_to_mark();
  275. }
  276. }
  277. }
  278. BIO_free(extbio);
  279. NCONF_free(conf);
  280. }
  281. return testresult;
  282. }
  283. #endif /* OPENSSL_NO_RFC3779 */
  284. OPT_TEST_DECLARE_USAGE("cert.pem\n")
  285. int setup_tests(void)
  286. {
  287. if (!test_skip_common_options()) {
  288. TEST_error("Error parsing test options\n");
  289. return 0;
  290. }
  291. if (!TEST_ptr(infile = test_get_argument(0)))
  292. return 0;
  293. ADD_TEST(test_pathlen);
  294. #ifndef OPENSSL_NO_RFC3779
  295. ADD_TEST(test_asid);
  296. ADD_TEST(test_addr_ranges);
  297. ADD_TEST(test_ext_syntax);
  298. #endif /* OPENSSL_NO_RFC3779 */
  299. return 1;
  300. }