2
0

v3_addr.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309
  1. /*
  2. * Copyright 2006-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. /*
  10. * Implementation of RFC 3779 section 2.2.
  11. */
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include "internal/cryptlib.h"
  15. #include <openssl/conf.h>
  16. #include <openssl/asn1.h>
  17. #include <openssl/asn1t.h>
  18. #include <openssl/buffer.h>
  19. #include <openssl/x509v3.h>
  20. #include "crypto/x509.h"
  21. #include "ext_dat.h"
  22. #include "x509_local.h"
  23. #ifndef OPENSSL_NO_RFC3779
  24. /*
  25. * OpenSSL ASN.1 template translation of RFC 3779 2.2.3.
  26. */
  27. ASN1_SEQUENCE(IPAddressRange) = {
  28. ASN1_SIMPLE(IPAddressRange, min, ASN1_BIT_STRING),
  29. ASN1_SIMPLE(IPAddressRange, max, ASN1_BIT_STRING)
  30. } ASN1_SEQUENCE_END(IPAddressRange)
  31. ASN1_CHOICE(IPAddressOrRange) = {
  32. ASN1_SIMPLE(IPAddressOrRange, u.addressPrefix, ASN1_BIT_STRING),
  33. ASN1_SIMPLE(IPAddressOrRange, u.addressRange, IPAddressRange)
  34. } ASN1_CHOICE_END(IPAddressOrRange)
  35. ASN1_CHOICE(IPAddressChoice) = {
  36. ASN1_SIMPLE(IPAddressChoice, u.inherit, ASN1_NULL),
  37. ASN1_SEQUENCE_OF(IPAddressChoice, u.addressesOrRanges, IPAddressOrRange)
  38. } ASN1_CHOICE_END(IPAddressChoice)
  39. ASN1_SEQUENCE(IPAddressFamily) = {
  40. ASN1_SIMPLE(IPAddressFamily, addressFamily, ASN1_OCTET_STRING),
  41. ASN1_SIMPLE(IPAddressFamily, ipAddressChoice, IPAddressChoice)
  42. } ASN1_SEQUENCE_END(IPAddressFamily)
  43. ASN1_ITEM_TEMPLATE(IPAddrBlocks) =
  44. ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0,
  45. IPAddrBlocks, IPAddressFamily)
  46. static_ASN1_ITEM_TEMPLATE_END(IPAddrBlocks)
  47. IMPLEMENT_ASN1_FUNCTIONS(IPAddressRange)
  48. IMPLEMENT_ASN1_FUNCTIONS(IPAddressOrRange)
  49. IMPLEMENT_ASN1_FUNCTIONS(IPAddressChoice)
  50. IMPLEMENT_ASN1_FUNCTIONS(IPAddressFamily)
  51. /*
  52. * How much buffer space do we need for a raw address?
  53. */
  54. #define ADDR_RAW_BUF_LEN 16
  55. /*
  56. * What's the address length associated with this AFI?
  57. */
  58. static int length_from_afi(const unsigned afi)
  59. {
  60. switch (afi) {
  61. case IANA_AFI_IPV4:
  62. return 4;
  63. case IANA_AFI_IPV6:
  64. return 16;
  65. default:
  66. return 0;
  67. }
  68. }
  69. /*
  70. * Extract the AFI from an IPAddressFamily.
  71. */
  72. unsigned int X509v3_addr_get_afi(const IPAddressFamily *f)
  73. {
  74. if (f == NULL
  75. || f->addressFamily == NULL
  76. || f->addressFamily->data == NULL
  77. || f->addressFamily->length < 2)
  78. return 0;
  79. return (f->addressFamily->data[0] << 8) | f->addressFamily->data[1];
  80. }
  81. /*
  82. * Expand the bitstring form of an address into a raw byte array.
  83. * At the moment this is coded for simplicity, not speed.
  84. */
  85. static int addr_expand(unsigned char *addr,
  86. const ASN1_BIT_STRING *bs,
  87. const int length, const unsigned char fill)
  88. {
  89. if (bs->length < 0 || bs->length > length)
  90. return 0;
  91. if (bs->length > 0) {
  92. memcpy(addr, bs->data, bs->length);
  93. if ((bs->flags & 7) != 0) {
  94. unsigned char mask = 0xFF >> (8 - (bs->flags & 7));
  95. if (fill == 0)
  96. addr[bs->length - 1] &= ~mask;
  97. else
  98. addr[bs->length - 1] |= mask;
  99. }
  100. }
  101. memset(addr + bs->length, fill, length - bs->length);
  102. return 1;
  103. }
  104. /*
  105. * Extract the prefix length from a bitstring.
  106. */
  107. #define addr_prefixlen(bs) ((int) ((bs)->length * 8 - ((bs)->flags & 7)))
  108. /*
  109. * i2r handler for one address bitstring.
  110. */
  111. static int i2r_address(BIO *out,
  112. const unsigned afi,
  113. const unsigned char fill, const ASN1_BIT_STRING *bs)
  114. {
  115. unsigned char addr[ADDR_RAW_BUF_LEN];
  116. int i, n;
  117. if (bs->length < 0)
  118. return 0;
  119. switch (afi) {
  120. case IANA_AFI_IPV4:
  121. if (!addr_expand(addr, bs, 4, fill))
  122. return 0;
  123. BIO_printf(out, "%d.%d.%d.%d", addr[0], addr[1], addr[2], addr[3]);
  124. break;
  125. /* TODO possibly combine with ipaddr_to_asc() */
  126. case IANA_AFI_IPV6:
  127. if (!addr_expand(addr, bs, 16, fill))
  128. return 0;
  129. for (n = 16; n > 1 && addr[n - 1] == 0x00 && addr[n - 2] == 0x00;
  130. n -= 2) ;
  131. for (i = 0; i < n; i += 2)
  132. BIO_printf(out, "%x%s", (addr[i] << 8) | addr[i + 1],
  133. (i < 14 ? ":" : ""));
  134. if (i < 16)
  135. BIO_puts(out, ":");
  136. if (i == 0)
  137. BIO_puts(out, ":");
  138. break;
  139. default:
  140. for (i = 0; i < bs->length; i++)
  141. BIO_printf(out, "%s%02x", (i > 0 ? ":" : ""), bs->data[i]);
  142. BIO_printf(out, "[%d]", (int)(bs->flags & 7));
  143. break;
  144. }
  145. return 1;
  146. }
  147. /*
  148. * i2r handler for a sequence of addresses and ranges.
  149. */
  150. static int i2r_IPAddressOrRanges(BIO *out,
  151. const int indent,
  152. const IPAddressOrRanges *aors,
  153. const unsigned afi)
  154. {
  155. int i;
  156. for (i = 0; i < sk_IPAddressOrRange_num(aors); i++) {
  157. const IPAddressOrRange *aor = sk_IPAddressOrRange_value(aors, i);
  158. BIO_printf(out, "%*s", indent, "");
  159. switch (aor->type) {
  160. case IPAddressOrRange_addressPrefix:
  161. if (!i2r_address(out, afi, 0x00, aor->u.addressPrefix))
  162. return 0;
  163. BIO_printf(out, "/%d\n", addr_prefixlen(aor->u.addressPrefix));
  164. continue;
  165. case IPAddressOrRange_addressRange:
  166. if (!i2r_address(out, afi, 0x00, aor->u.addressRange->min))
  167. return 0;
  168. BIO_puts(out, "-");
  169. if (!i2r_address(out, afi, 0xFF, aor->u.addressRange->max))
  170. return 0;
  171. BIO_puts(out, "\n");
  172. continue;
  173. }
  174. }
  175. return 1;
  176. }
  177. /*
  178. * i2r handler for an IPAddrBlocks extension.
  179. */
  180. static int i2r_IPAddrBlocks(const X509V3_EXT_METHOD *method,
  181. void *ext, BIO *out, int indent)
  182. {
  183. const IPAddrBlocks *addr = ext;
  184. int i;
  185. for (i = 0; i < sk_IPAddressFamily_num(addr); i++) {
  186. IPAddressFamily *f = sk_IPAddressFamily_value(addr, i);
  187. const unsigned int afi = X509v3_addr_get_afi(f);
  188. switch (afi) {
  189. case IANA_AFI_IPV4:
  190. BIO_printf(out, "%*sIPv4", indent, "");
  191. break;
  192. case IANA_AFI_IPV6:
  193. BIO_printf(out, "%*sIPv6", indent, "");
  194. break;
  195. default:
  196. BIO_printf(out, "%*sUnknown AFI %u", indent, "", afi);
  197. break;
  198. }
  199. if (f->addressFamily->length > 2) {
  200. switch (f->addressFamily->data[2]) {
  201. case 1:
  202. BIO_puts(out, " (Unicast)");
  203. break;
  204. case 2:
  205. BIO_puts(out, " (Multicast)");
  206. break;
  207. case 3:
  208. BIO_puts(out, " (Unicast/Multicast)");
  209. break;
  210. case 4:
  211. BIO_puts(out, " (MPLS)");
  212. break;
  213. case 64:
  214. BIO_puts(out, " (Tunnel)");
  215. break;
  216. case 65:
  217. BIO_puts(out, " (VPLS)");
  218. break;
  219. case 66:
  220. BIO_puts(out, " (BGP MDT)");
  221. break;
  222. case 128:
  223. BIO_puts(out, " (MPLS-labeled VPN)");
  224. break;
  225. default:
  226. BIO_printf(out, " (Unknown SAFI %u)",
  227. (unsigned)f->addressFamily->data[2]);
  228. break;
  229. }
  230. }
  231. switch (f->ipAddressChoice->type) {
  232. case IPAddressChoice_inherit:
  233. BIO_puts(out, ": inherit\n");
  234. break;
  235. case IPAddressChoice_addressesOrRanges:
  236. BIO_puts(out, ":\n");
  237. if (!i2r_IPAddressOrRanges(out,
  238. indent + 2,
  239. f->ipAddressChoice->
  240. u.addressesOrRanges, afi))
  241. return 0;
  242. break;
  243. }
  244. }
  245. return 1;
  246. }
  247. /*
  248. * Sort comparison function for a sequence of IPAddressOrRange
  249. * elements.
  250. *
  251. * There's no sane answer we can give if addr_expand() fails, and an
  252. * assertion failure on externally supplied data is seriously uncool,
  253. * so we just arbitrarily declare that if given invalid inputs this
  254. * function returns -1. If this messes up your preferred sort order
  255. * for garbage input, tough noogies.
  256. */
  257. static int IPAddressOrRange_cmp(const IPAddressOrRange *a,
  258. const IPAddressOrRange *b, const int length)
  259. {
  260. unsigned char addr_a[ADDR_RAW_BUF_LEN], addr_b[ADDR_RAW_BUF_LEN];
  261. int prefixlen_a = 0, prefixlen_b = 0;
  262. int r;
  263. switch (a->type) {
  264. case IPAddressOrRange_addressPrefix:
  265. if (!addr_expand(addr_a, a->u.addressPrefix, length, 0x00))
  266. return -1;
  267. prefixlen_a = addr_prefixlen(a->u.addressPrefix);
  268. break;
  269. case IPAddressOrRange_addressRange:
  270. if (!addr_expand(addr_a, a->u.addressRange->min, length, 0x00))
  271. return -1;
  272. prefixlen_a = length * 8;
  273. break;
  274. }
  275. switch (b->type) {
  276. case IPAddressOrRange_addressPrefix:
  277. if (!addr_expand(addr_b, b->u.addressPrefix, length, 0x00))
  278. return -1;
  279. prefixlen_b = addr_prefixlen(b->u.addressPrefix);
  280. break;
  281. case IPAddressOrRange_addressRange:
  282. if (!addr_expand(addr_b, b->u.addressRange->min, length, 0x00))
  283. return -1;
  284. prefixlen_b = length * 8;
  285. break;
  286. }
  287. if ((r = memcmp(addr_a, addr_b, length)) != 0)
  288. return r;
  289. else
  290. return prefixlen_a - prefixlen_b;
  291. }
  292. /*
  293. * IPv4-specific closure over IPAddressOrRange_cmp, since sk_sort()
  294. * comparison routines are only allowed two arguments.
  295. */
  296. static int v4IPAddressOrRange_cmp(const IPAddressOrRange *const *a,
  297. const IPAddressOrRange *const *b)
  298. {
  299. return IPAddressOrRange_cmp(*a, *b, 4);
  300. }
  301. /*
  302. * IPv6-specific closure over IPAddressOrRange_cmp, since sk_sort()
  303. * comparison routines are only allowed two arguments.
  304. */
  305. static int v6IPAddressOrRange_cmp(const IPAddressOrRange *const *a,
  306. const IPAddressOrRange *const *b)
  307. {
  308. return IPAddressOrRange_cmp(*a, *b, 16);
  309. }
  310. /*
  311. * Calculate whether a range collapses to a prefix.
  312. * See last paragraph of RFC 3779 2.2.3.7.
  313. */
  314. static int range_should_be_prefix(const unsigned char *min,
  315. const unsigned char *max, const int length)
  316. {
  317. unsigned char mask;
  318. int i, j;
  319. if (memcmp(min, max, length) <= 0)
  320. return -1;
  321. for (i = 0; i < length && min[i] == max[i]; i++) ;
  322. for (j = length - 1; j >= 0 && min[j] == 0x00 && max[j] == 0xFF; j--) ;
  323. if (i < j)
  324. return -1;
  325. if (i > j)
  326. return i * 8;
  327. mask = min[i] ^ max[i];
  328. switch (mask) {
  329. case 0x01:
  330. j = 7;
  331. break;
  332. case 0x03:
  333. j = 6;
  334. break;
  335. case 0x07:
  336. j = 5;
  337. break;
  338. case 0x0F:
  339. j = 4;
  340. break;
  341. case 0x1F:
  342. j = 3;
  343. break;
  344. case 0x3F:
  345. j = 2;
  346. break;
  347. case 0x7F:
  348. j = 1;
  349. break;
  350. default:
  351. return -1;
  352. }
  353. if ((min[i] & mask) != 0 || (max[i] & mask) != mask)
  354. return -1;
  355. else
  356. return i * 8 + j;
  357. }
  358. /*
  359. * Construct a prefix.
  360. */
  361. static int make_addressPrefix(IPAddressOrRange **result,
  362. unsigned char *addr, const int prefixlen)
  363. {
  364. int bytelen = (prefixlen + 7) / 8, bitlen = prefixlen % 8;
  365. IPAddressOrRange *aor = IPAddressOrRange_new();
  366. if (aor == NULL)
  367. return 0;
  368. aor->type = IPAddressOrRange_addressPrefix;
  369. if (aor->u.addressPrefix == NULL &&
  370. (aor->u.addressPrefix = ASN1_BIT_STRING_new()) == NULL)
  371. goto err;
  372. if (!ASN1_BIT_STRING_set(aor->u.addressPrefix, addr, bytelen))
  373. goto err;
  374. aor->u.addressPrefix->flags &= ~7;
  375. aor->u.addressPrefix->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  376. if (bitlen > 0) {
  377. aor->u.addressPrefix->data[bytelen - 1] &= ~(0xFF >> bitlen);
  378. aor->u.addressPrefix->flags |= 8 - bitlen;
  379. }
  380. *result = aor;
  381. return 1;
  382. err:
  383. IPAddressOrRange_free(aor);
  384. return 0;
  385. }
  386. /*
  387. * Construct a range. If it can be expressed as a prefix,
  388. * return a prefix instead. Doing this here simplifies
  389. * the rest of the code considerably.
  390. */
  391. static int make_addressRange(IPAddressOrRange **result,
  392. unsigned char *min,
  393. unsigned char *max, const int length)
  394. {
  395. IPAddressOrRange *aor;
  396. int i, prefixlen;
  397. if ((prefixlen = range_should_be_prefix(min, max, length)) >= 0)
  398. return make_addressPrefix(result, min, prefixlen);
  399. if ((aor = IPAddressOrRange_new()) == NULL)
  400. return 0;
  401. aor->type = IPAddressOrRange_addressRange;
  402. if ((aor->u.addressRange = IPAddressRange_new()) == NULL)
  403. goto err;
  404. if (aor->u.addressRange->min == NULL &&
  405. (aor->u.addressRange->min = ASN1_BIT_STRING_new()) == NULL)
  406. goto err;
  407. if (aor->u.addressRange->max == NULL &&
  408. (aor->u.addressRange->max = ASN1_BIT_STRING_new()) == NULL)
  409. goto err;
  410. for (i = length; i > 0 && min[i - 1] == 0x00; --i) ;
  411. if (!ASN1_BIT_STRING_set(aor->u.addressRange->min, min, i))
  412. goto err;
  413. aor->u.addressRange->min->flags &= ~7;
  414. aor->u.addressRange->min->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  415. if (i > 0) {
  416. unsigned char b = min[i - 1];
  417. int j = 1;
  418. while ((b & (0xFFU >> j)) != 0)
  419. ++j;
  420. aor->u.addressRange->min->flags |= 8 - j;
  421. }
  422. for (i = length; i > 0 && max[i - 1] == 0xFF; --i) ;
  423. if (!ASN1_BIT_STRING_set(aor->u.addressRange->max, max, i))
  424. goto err;
  425. aor->u.addressRange->max->flags &= ~7;
  426. aor->u.addressRange->max->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  427. if (i > 0) {
  428. unsigned char b = max[i - 1];
  429. int j = 1;
  430. while ((b & (0xFFU >> j)) != (0xFFU >> j))
  431. ++j;
  432. aor->u.addressRange->max->flags |= 8 - j;
  433. }
  434. *result = aor;
  435. return 1;
  436. err:
  437. IPAddressOrRange_free(aor);
  438. return 0;
  439. }
  440. /*
  441. * Construct a new address family or find an existing one.
  442. */
  443. static IPAddressFamily *make_IPAddressFamily(IPAddrBlocks *addr,
  444. const unsigned afi,
  445. const unsigned *safi)
  446. {
  447. IPAddressFamily *f;
  448. unsigned char key[3];
  449. int keylen;
  450. int i;
  451. key[0] = (afi >> 8) & 0xFF;
  452. key[1] = afi & 0xFF;
  453. if (safi != NULL) {
  454. key[2] = *safi & 0xFF;
  455. keylen = 3;
  456. } else {
  457. keylen = 2;
  458. }
  459. for (i = 0; i < sk_IPAddressFamily_num(addr); i++) {
  460. f = sk_IPAddressFamily_value(addr, i);
  461. if (f->addressFamily->length == keylen &&
  462. !memcmp(f->addressFamily->data, key, keylen))
  463. return f;
  464. }
  465. if ((f = IPAddressFamily_new()) == NULL)
  466. goto err;
  467. if (f->ipAddressChoice == NULL &&
  468. (f->ipAddressChoice = IPAddressChoice_new()) == NULL)
  469. goto err;
  470. if (f->addressFamily == NULL &&
  471. (f->addressFamily = ASN1_OCTET_STRING_new()) == NULL)
  472. goto err;
  473. if (!ASN1_OCTET_STRING_set(f->addressFamily, key, keylen))
  474. goto err;
  475. if (!sk_IPAddressFamily_push(addr, f))
  476. goto err;
  477. return f;
  478. err:
  479. IPAddressFamily_free(f);
  480. return NULL;
  481. }
  482. /*
  483. * Add an inheritance element.
  484. */
  485. int X509v3_addr_add_inherit(IPAddrBlocks *addr,
  486. const unsigned afi, const unsigned *safi)
  487. {
  488. IPAddressFamily *f = make_IPAddressFamily(addr, afi, safi);
  489. if (f == NULL ||
  490. f->ipAddressChoice == NULL ||
  491. (f->ipAddressChoice->type == IPAddressChoice_addressesOrRanges &&
  492. f->ipAddressChoice->u.addressesOrRanges != NULL))
  493. return 0;
  494. if (f->ipAddressChoice->type == IPAddressChoice_inherit &&
  495. f->ipAddressChoice->u.inherit != NULL)
  496. return 1;
  497. if (f->ipAddressChoice->u.inherit == NULL &&
  498. (f->ipAddressChoice->u.inherit = ASN1_NULL_new()) == NULL)
  499. return 0;
  500. f->ipAddressChoice->type = IPAddressChoice_inherit;
  501. return 1;
  502. }
  503. /*
  504. * Construct an IPAddressOrRange sequence, or return an existing one.
  505. */
  506. static IPAddressOrRanges *make_prefix_or_range(IPAddrBlocks *addr,
  507. const unsigned afi,
  508. const unsigned *safi)
  509. {
  510. IPAddressFamily *f = make_IPAddressFamily(addr, afi, safi);
  511. IPAddressOrRanges *aors = NULL;
  512. if (f == NULL ||
  513. f->ipAddressChoice == NULL ||
  514. (f->ipAddressChoice->type == IPAddressChoice_inherit &&
  515. f->ipAddressChoice->u.inherit != NULL))
  516. return NULL;
  517. if (f->ipAddressChoice->type == IPAddressChoice_addressesOrRanges)
  518. aors = f->ipAddressChoice->u.addressesOrRanges;
  519. if (aors != NULL)
  520. return aors;
  521. if ((aors = sk_IPAddressOrRange_new_null()) == NULL)
  522. return NULL;
  523. switch (afi) {
  524. case IANA_AFI_IPV4:
  525. (void)sk_IPAddressOrRange_set_cmp_func(aors, v4IPAddressOrRange_cmp);
  526. break;
  527. case IANA_AFI_IPV6:
  528. (void)sk_IPAddressOrRange_set_cmp_func(aors, v6IPAddressOrRange_cmp);
  529. break;
  530. }
  531. f->ipAddressChoice->type = IPAddressChoice_addressesOrRanges;
  532. f->ipAddressChoice->u.addressesOrRanges = aors;
  533. return aors;
  534. }
  535. /*
  536. * Add a prefix.
  537. */
  538. int X509v3_addr_add_prefix(IPAddrBlocks *addr,
  539. const unsigned afi,
  540. const unsigned *safi,
  541. unsigned char *a, const int prefixlen)
  542. {
  543. IPAddressOrRanges *aors = make_prefix_or_range(addr, afi, safi);
  544. IPAddressOrRange *aor;
  545. if (aors == NULL || !make_addressPrefix(&aor, a, prefixlen))
  546. return 0;
  547. if (sk_IPAddressOrRange_push(aors, aor))
  548. return 1;
  549. IPAddressOrRange_free(aor);
  550. return 0;
  551. }
  552. /*
  553. * Add a range.
  554. */
  555. int X509v3_addr_add_range(IPAddrBlocks *addr,
  556. const unsigned afi,
  557. const unsigned *safi,
  558. unsigned char *min, unsigned char *max)
  559. {
  560. IPAddressOrRanges *aors = make_prefix_or_range(addr, afi, safi);
  561. IPAddressOrRange *aor;
  562. int length = length_from_afi(afi);
  563. if (aors == NULL)
  564. return 0;
  565. if (!make_addressRange(&aor, min, max, length))
  566. return 0;
  567. if (sk_IPAddressOrRange_push(aors, aor))
  568. return 1;
  569. IPAddressOrRange_free(aor);
  570. return 0;
  571. }
  572. /*
  573. * Extract min and max values from an IPAddressOrRange.
  574. */
  575. static int extract_min_max(IPAddressOrRange *aor,
  576. unsigned char *min, unsigned char *max, int length)
  577. {
  578. if (aor == NULL || min == NULL || max == NULL)
  579. return 0;
  580. switch (aor->type) {
  581. case IPAddressOrRange_addressPrefix:
  582. return (addr_expand(min, aor->u.addressPrefix, length, 0x00) &&
  583. addr_expand(max, aor->u.addressPrefix, length, 0xFF));
  584. case IPAddressOrRange_addressRange:
  585. return (addr_expand(min, aor->u.addressRange->min, length, 0x00) &&
  586. addr_expand(max, aor->u.addressRange->max, length, 0xFF));
  587. }
  588. return 0;
  589. }
  590. /*
  591. * Public wrapper for extract_min_max().
  592. */
  593. int X509v3_addr_get_range(IPAddressOrRange *aor,
  594. const unsigned afi,
  595. unsigned char *min,
  596. unsigned char *max, const int length)
  597. {
  598. int afi_length = length_from_afi(afi);
  599. if (aor == NULL || min == NULL || max == NULL ||
  600. afi_length == 0 || length < afi_length ||
  601. (aor->type != IPAddressOrRange_addressPrefix &&
  602. aor->type != IPAddressOrRange_addressRange) ||
  603. !extract_min_max(aor, min, max, afi_length))
  604. return 0;
  605. return afi_length;
  606. }
  607. /*
  608. * Sort comparison function for a sequence of IPAddressFamily.
  609. *
  610. * The last paragraph of RFC 3779 2.2.3.3 is slightly ambiguous about
  611. * the ordering: I can read it as meaning that IPv6 without a SAFI
  612. * comes before IPv4 with a SAFI, which seems pretty weird. The
  613. * examples in appendix B suggest that the author intended the
  614. * null-SAFI rule to apply only within a single AFI, which is what I
  615. * would have expected and is what the following code implements.
  616. */
  617. static int IPAddressFamily_cmp(const IPAddressFamily *const *a_,
  618. const IPAddressFamily *const *b_)
  619. {
  620. const ASN1_OCTET_STRING *a = (*a_)->addressFamily;
  621. const ASN1_OCTET_STRING *b = (*b_)->addressFamily;
  622. int len = ((a->length <= b->length) ? a->length : b->length);
  623. int cmp = memcmp(a->data, b->data, len);
  624. return cmp ? cmp : a->length - b->length;
  625. }
  626. /*
  627. * Check whether an IPAddrBLocks is in canonical form.
  628. */
  629. int X509v3_addr_is_canonical(IPAddrBlocks *addr)
  630. {
  631. unsigned char a_min[ADDR_RAW_BUF_LEN], a_max[ADDR_RAW_BUF_LEN];
  632. unsigned char b_min[ADDR_RAW_BUF_LEN], b_max[ADDR_RAW_BUF_LEN];
  633. IPAddressOrRanges *aors;
  634. int i, j, k;
  635. /*
  636. * Empty extension is canonical.
  637. */
  638. if (addr == NULL)
  639. return 1;
  640. /*
  641. * Check whether the top-level list is in order.
  642. */
  643. for (i = 0; i < sk_IPAddressFamily_num(addr) - 1; i++) {
  644. const IPAddressFamily *a = sk_IPAddressFamily_value(addr, i);
  645. const IPAddressFamily *b = sk_IPAddressFamily_value(addr, i + 1);
  646. if (IPAddressFamily_cmp(&a, &b) >= 0)
  647. return 0;
  648. }
  649. /*
  650. * Top level's ok, now check each address family.
  651. */
  652. for (i = 0; i < sk_IPAddressFamily_num(addr); i++) {
  653. IPAddressFamily *f = sk_IPAddressFamily_value(addr, i);
  654. int length = length_from_afi(X509v3_addr_get_afi(f));
  655. /*
  656. * Inheritance is canonical. Anything other than inheritance or
  657. * a SEQUENCE OF IPAddressOrRange is an ASN.1 error or something.
  658. */
  659. if (f == NULL || f->ipAddressChoice == NULL)
  660. return 0;
  661. switch (f->ipAddressChoice->type) {
  662. case IPAddressChoice_inherit:
  663. continue;
  664. case IPAddressChoice_addressesOrRanges:
  665. break;
  666. default:
  667. return 0;
  668. }
  669. /*
  670. * It's an IPAddressOrRanges sequence, check it.
  671. */
  672. aors = f->ipAddressChoice->u.addressesOrRanges;
  673. if (sk_IPAddressOrRange_num(aors) == 0)
  674. return 0;
  675. for (j = 0; j < sk_IPAddressOrRange_num(aors) - 1; j++) {
  676. IPAddressOrRange *a = sk_IPAddressOrRange_value(aors, j);
  677. IPAddressOrRange *b = sk_IPAddressOrRange_value(aors, j + 1);
  678. if (!extract_min_max(a, a_min, a_max, length) ||
  679. !extract_min_max(b, b_min, b_max, length))
  680. return 0;
  681. /*
  682. * Punt misordered list, overlapping start, or inverted range.
  683. */
  684. if (memcmp(a_min, b_min, length) >= 0 ||
  685. memcmp(a_min, a_max, length) > 0 ||
  686. memcmp(b_min, b_max, length) > 0)
  687. return 0;
  688. /*
  689. * Punt if adjacent or overlapping. Check for adjacency by
  690. * subtracting one from b_min first.
  691. */
  692. for (k = length - 1; k >= 0 && b_min[k]-- == 0x00; k--) ;
  693. if (memcmp(a_max, b_min, length) >= 0)
  694. return 0;
  695. /*
  696. * Check for range that should be expressed as a prefix.
  697. */
  698. if (a->type == IPAddressOrRange_addressRange &&
  699. range_should_be_prefix(a_min, a_max, length) >= 0)
  700. return 0;
  701. }
  702. /*
  703. * Check range to see if it's inverted or should be a
  704. * prefix.
  705. */
  706. j = sk_IPAddressOrRange_num(aors) - 1;
  707. {
  708. IPAddressOrRange *a = sk_IPAddressOrRange_value(aors, j);
  709. if (a != NULL && a->type == IPAddressOrRange_addressRange) {
  710. if (!extract_min_max(a, a_min, a_max, length))
  711. return 0;
  712. if (memcmp(a_min, a_max, length) > 0 ||
  713. range_should_be_prefix(a_min, a_max, length) >= 0)
  714. return 0;
  715. }
  716. }
  717. }
  718. /*
  719. * If we made it through all that, we're happy.
  720. */
  721. return 1;
  722. }
  723. /*
  724. * Whack an IPAddressOrRanges into canonical form.
  725. */
  726. static int IPAddressOrRanges_canonize(IPAddressOrRanges *aors,
  727. const unsigned afi)
  728. {
  729. int i, j, length = length_from_afi(afi);
  730. /*
  731. * Sort the IPAddressOrRanges sequence.
  732. */
  733. sk_IPAddressOrRange_sort(aors);
  734. /*
  735. * Clean up representation issues, punt on duplicates or overlaps.
  736. */
  737. for (i = 0; i < sk_IPAddressOrRange_num(aors) - 1; i++) {
  738. IPAddressOrRange *a = sk_IPAddressOrRange_value(aors, i);
  739. IPAddressOrRange *b = sk_IPAddressOrRange_value(aors, i + 1);
  740. unsigned char a_min[ADDR_RAW_BUF_LEN], a_max[ADDR_RAW_BUF_LEN];
  741. unsigned char b_min[ADDR_RAW_BUF_LEN], b_max[ADDR_RAW_BUF_LEN];
  742. if (!extract_min_max(a, a_min, a_max, length) ||
  743. !extract_min_max(b, b_min, b_max, length))
  744. return 0;
  745. /*
  746. * Punt inverted ranges.
  747. */
  748. if (memcmp(a_min, a_max, length) > 0 ||
  749. memcmp(b_min, b_max, length) > 0)
  750. return 0;
  751. /*
  752. * Punt overlaps.
  753. */
  754. if (memcmp(a_max, b_min, length) >= 0)
  755. return 0;
  756. /*
  757. * Merge if a and b are adjacent. We check for
  758. * adjacency by subtracting one from b_min first.
  759. */
  760. for (j = length - 1; j >= 0 && b_min[j]-- == 0x00; j--) ;
  761. if (memcmp(a_max, b_min, length) == 0) {
  762. IPAddressOrRange *merged;
  763. if (!make_addressRange(&merged, a_min, b_max, length))
  764. return 0;
  765. (void)sk_IPAddressOrRange_set(aors, i, merged);
  766. (void)sk_IPAddressOrRange_delete(aors, i + 1);
  767. IPAddressOrRange_free(a);
  768. IPAddressOrRange_free(b);
  769. --i;
  770. continue;
  771. }
  772. }
  773. /*
  774. * Check for inverted final range.
  775. */
  776. j = sk_IPAddressOrRange_num(aors) - 1;
  777. {
  778. IPAddressOrRange *a = sk_IPAddressOrRange_value(aors, j);
  779. if (a != NULL && a->type == IPAddressOrRange_addressRange) {
  780. unsigned char a_min[ADDR_RAW_BUF_LEN], a_max[ADDR_RAW_BUF_LEN];
  781. if (!extract_min_max(a, a_min, a_max, length))
  782. return 0;
  783. if (memcmp(a_min, a_max, length) > 0)
  784. return 0;
  785. }
  786. }
  787. return 1;
  788. }
  789. /*
  790. * Whack an IPAddrBlocks extension into canonical form.
  791. */
  792. int X509v3_addr_canonize(IPAddrBlocks *addr)
  793. {
  794. int i;
  795. for (i = 0; i < sk_IPAddressFamily_num(addr); i++) {
  796. IPAddressFamily *f = sk_IPAddressFamily_value(addr, i);
  797. if (f->ipAddressChoice->type == IPAddressChoice_addressesOrRanges &&
  798. !IPAddressOrRanges_canonize(f->ipAddressChoice->
  799. u.addressesOrRanges,
  800. X509v3_addr_get_afi(f)))
  801. return 0;
  802. }
  803. (void)sk_IPAddressFamily_set_cmp_func(addr, IPAddressFamily_cmp);
  804. sk_IPAddressFamily_sort(addr);
  805. if (!ossl_assert(X509v3_addr_is_canonical(addr)))
  806. return 0;
  807. return 1;
  808. }
  809. /*
  810. * v2i handler for the IPAddrBlocks extension.
  811. */
  812. static void *v2i_IPAddrBlocks(const struct v3_ext_method *method,
  813. struct v3_ext_ctx *ctx,
  814. STACK_OF(CONF_VALUE) *values)
  815. {
  816. static const char v4addr_chars[] = "0123456789.";
  817. static const char v6addr_chars[] = "0123456789.:abcdefABCDEF";
  818. IPAddrBlocks *addr = NULL;
  819. char *s = NULL, *t;
  820. int i;
  821. if ((addr = sk_IPAddressFamily_new(IPAddressFamily_cmp)) == NULL) {
  822. ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
  823. return NULL;
  824. }
  825. for (i = 0; i < sk_CONF_VALUE_num(values); i++) {
  826. CONF_VALUE *val = sk_CONF_VALUE_value(values, i);
  827. unsigned char min[ADDR_RAW_BUF_LEN], max[ADDR_RAW_BUF_LEN];
  828. unsigned afi, *safi = NULL, safi_;
  829. const char *addr_chars = NULL;
  830. int prefixlen, i1, i2, delim, length;
  831. if (!v3_name_cmp(val->name, "IPv4")) {
  832. afi = IANA_AFI_IPV4;
  833. } else if (!v3_name_cmp(val->name, "IPv6")) {
  834. afi = IANA_AFI_IPV6;
  835. } else if (!v3_name_cmp(val->name, "IPv4-SAFI")) {
  836. afi = IANA_AFI_IPV4;
  837. safi = &safi_;
  838. } else if (!v3_name_cmp(val->name, "IPv6-SAFI")) {
  839. afi = IANA_AFI_IPV6;
  840. safi = &safi_;
  841. } else {
  842. ERR_raise_data(ERR_LIB_X509V3, X509V3_R_EXTENSION_NAME_ERROR,
  843. "%s", val->name);
  844. goto err;
  845. }
  846. switch (afi) {
  847. case IANA_AFI_IPV4:
  848. addr_chars = v4addr_chars;
  849. break;
  850. case IANA_AFI_IPV6:
  851. addr_chars = v6addr_chars;
  852. break;
  853. }
  854. length = length_from_afi(afi);
  855. /*
  856. * Handle SAFI, if any, and OPENSSL_strdup() so we can null-terminate
  857. * the other input values.
  858. */
  859. if (safi != NULL) {
  860. *safi = strtoul(val->value, &t, 0);
  861. t += strspn(t, " \t");
  862. if (*safi > 0xFF || *t++ != ':') {
  863. ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_SAFI);
  864. X509V3_conf_add_error_name_value(val);
  865. goto err;
  866. }
  867. t += strspn(t, " \t");
  868. s = OPENSSL_strdup(t);
  869. } else {
  870. s = OPENSSL_strdup(val->value);
  871. }
  872. if (s == NULL) {
  873. ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
  874. goto err;
  875. }
  876. /*
  877. * Check for inheritance. Not worth additional complexity to
  878. * optimize this (seldom-used) case.
  879. */
  880. if (strcmp(s, "inherit") == 0) {
  881. if (!X509v3_addr_add_inherit(addr, afi, safi)) {
  882. ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_INHERITANCE);
  883. X509V3_conf_add_error_name_value(val);
  884. goto err;
  885. }
  886. OPENSSL_free(s);
  887. s = NULL;
  888. continue;
  889. }
  890. i1 = strspn(s, addr_chars);
  891. i2 = i1 + strspn(s + i1, " \t");
  892. delim = s[i2++];
  893. s[i1] = '\0';
  894. if (a2i_ipadd(min, s) != length) {
  895. ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_IPADDRESS);
  896. X509V3_conf_add_error_name_value(val);
  897. goto err;
  898. }
  899. switch (delim) {
  900. case '/':
  901. prefixlen = (int)strtoul(s + i2, &t, 10);
  902. if (t == s + i2 || *t != '\0') {
  903. ERR_raise(ERR_LIB_X509V3, X509V3_R_EXTENSION_VALUE_ERROR);
  904. X509V3_conf_add_error_name_value(val);
  905. goto err;
  906. }
  907. if (!X509v3_addr_add_prefix(addr, afi, safi, min, prefixlen)) {
  908. ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
  909. goto err;
  910. }
  911. break;
  912. case '-':
  913. i1 = i2 + strspn(s + i2, " \t");
  914. i2 = i1 + strspn(s + i1, addr_chars);
  915. if (i1 == i2 || s[i2] != '\0') {
  916. ERR_raise(ERR_LIB_X509V3, X509V3_R_EXTENSION_VALUE_ERROR);
  917. X509V3_conf_add_error_name_value(val);
  918. goto err;
  919. }
  920. if (a2i_ipadd(max, s + i1) != length) {
  921. ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_IPADDRESS);
  922. X509V3_conf_add_error_name_value(val);
  923. goto err;
  924. }
  925. if (memcmp(min, max, length_from_afi(afi)) > 0) {
  926. ERR_raise(ERR_LIB_X509V3, X509V3_R_EXTENSION_VALUE_ERROR);
  927. X509V3_conf_add_error_name_value(val);
  928. goto err;
  929. }
  930. if (!X509v3_addr_add_range(addr, afi, safi, min, max)) {
  931. ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
  932. goto err;
  933. }
  934. break;
  935. case '\0':
  936. if (!X509v3_addr_add_prefix(addr, afi, safi, min, length * 8)) {
  937. ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
  938. goto err;
  939. }
  940. break;
  941. default:
  942. ERR_raise(ERR_LIB_X509V3, X509V3_R_EXTENSION_VALUE_ERROR);
  943. X509V3_conf_add_error_name_value(val);
  944. goto err;
  945. }
  946. OPENSSL_free(s);
  947. s = NULL;
  948. }
  949. /*
  950. * Canonize the result, then we're done.
  951. */
  952. if (!X509v3_addr_canonize(addr))
  953. goto err;
  954. return addr;
  955. err:
  956. OPENSSL_free(s);
  957. sk_IPAddressFamily_pop_free(addr, IPAddressFamily_free);
  958. return NULL;
  959. }
  960. /*
  961. * OpenSSL dispatch
  962. */
  963. const X509V3_EXT_METHOD v3_addr = {
  964. NID_sbgp_ipAddrBlock, /* nid */
  965. 0, /* flags */
  966. ASN1_ITEM_ref(IPAddrBlocks), /* template */
  967. 0, 0, 0, 0, /* old functions, ignored */
  968. 0, /* i2s */
  969. 0, /* s2i */
  970. 0, /* i2v */
  971. v2i_IPAddrBlocks, /* v2i */
  972. i2r_IPAddrBlocks, /* i2r */
  973. 0, /* r2i */
  974. NULL /* extension-specific data */
  975. };
  976. /*
  977. * Figure out whether extension sues inheritance.
  978. */
  979. int X509v3_addr_inherits(IPAddrBlocks *addr)
  980. {
  981. int i;
  982. if (addr == NULL)
  983. return 0;
  984. for (i = 0; i < sk_IPAddressFamily_num(addr); i++) {
  985. IPAddressFamily *f = sk_IPAddressFamily_value(addr, i);
  986. if (f->ipAddressChoice->type == IPAddressChoice_inherit)
  987. return 1;
  988. }
  989. return 0;
  990. }
  991. /*
  992. * Figure out whether parent contains child.
  993. */
  994. static int addr_contains(IPAddressOrRanges *parent,
  995. IPAddressOrRanges *child, int length)
  996. {
  997. unsigned char p_min[ADDR_RAW_BUF_LEN], p_max[ADDR_RAW_BUF_LEN];
  998. unsigned char c_min[ADDR_RAW_BUF_LEN], c_max[ADDR_RAW_BUF_LEN];
  999. int p, c;
  1000. if (child == NULL || parent == child)
  1001. return 1;
  1002. if (parent == NULL)
  1003. return 0;
  1004. p = 0;
  1005. for (c = 0; c < sk_IPAddressOrRange_num(child); c++) {
  1006. if (!extract_min_max(sk_IPAddressOrRange_value(child, c),
  1007. c_min, c_max, length))
  1008. return -1;
  1009. for (;; p++) {
  1010. if (p >= sk_IPAddressOrRange_num(parent))
  1011. return 0;
  1012. if (!extract_min_max(sk_IPAddressOrRange_value(parent, p),
  1013. p_min, p_max, length))
  1014. return 0;
  1015. if (memcmp(p_max, c_max, length) < 0)
  1016. continue;
  1017. if (memcmp(p_min, c_min, length) > 0)
  1018. return 0;
  1019. break;
  1020. }
  1021. }
  1022. return 1;
  1023. }
  1024. /*
  1025. * Test whether a is a subset of b.
  1026. */
  1027. int X509v3_addr_subset(IPAddrBlocks *a, IPAddrBlocks *b)
  1028. {
  1029. int i;
  1030. if (a == NULL || a == b)
  1031. return 1;
  1032. if (b == NULL || X509v3_addr_inherits(a) || X509v3_addr_inherits(b))
  1033. return 0;
  1034. (void)sk_IPAddressFamily_set_cmp_func(b, IPAddressFamily_cmp);
  1035. for (i = 0; i < sk_IPAddressFamily_num(a); i++) {
  1036. IPAddressFamily *fa = sk_IPAddressFamily_value(a, i);
  1037. int j = sk_IPAddressFamily_find(b, fa);
  1038. IPAddressFamily *fb;
  1039. fb = sk_IPAddressFamily_value(b, j);
  1040. if (fb == NULL)
  1041. return 0;
  1042. if (!addr_contains(fb->ipAddressChoice->u.addressesOrRanges,
  1043. fa->ipAddressChoice->u.addressesOrRanges,
  1044. length_from_afi(X509v3_addr_get_afi(fb))))
  1045. return 0;
  1046. }
  1047. return 1;
  1048. }
  1049. /*
  1050. * Validation error handling via callback.
  1051. */
  1052. #define validation_err(_err_) \
  1053. do { \
  1054. if (ctx != NULL) { \
  1055. ctx->error = _err_; \
  1056. ctx->error_depth = i; \
  1057. ctx->current_cert = x; \
  1058. ret = ctx->verify_cb(0, ctx); \
  1059. } else { \
  1060. ret = 0; \
  1061. } \
  1062. if (!ret) \
  1063. goto done; \
  1064. } while (0)
  1065. /*
  1066. * Core code for RFC 3779 2.3 path validation.
  1067. *
  1068. * Returns 1 for success, 0 on error.
  1069. *
  1070. * When returning 0, ctx->error MUST be set to an appropriate value other than
  1071. * X509_V_OK.
  1072. */
  1073. static int addr_validate_path_internal(X509_STORE_CTX *ctx,
  1074. STACK_OF(X509) *chain,
  1075. IPAddrBlocks *ext)
  1076. {
  1077. IPAddrBlocks *child = NULL;
  1078. int i, j, ret = 1;
  1079. X509 *x;
  1080. if (!ossl_assert(chain != NULL && sk_X509_num(chain) > 0)
  1081. || !ossl_assert(ctx != NULL || ext != NULL)
  1082. || !ossl_assert(ctx == NULL || ctx->verify_cb != NULL)) {
  1083. if (ctx != NULL)
  1084. ctx->error = X509_V_ERR_UNSPECIFIED;
  1085. return 0;
  1086. }
  1087. /*
  1088. * Figure out where to start. If we don't have an extension to
  1089. * check, we're done. Otherwise, check canonical form and
  1090. * set up for walking up the chain.
  1091. */
  1092. if (ext != NULL) {
  1093. i = -1;
  1094. x = NULL;
  1095. } else {
  1096. i = 0;
  1097. x = sk_X509_value(chain, i);
  1098. if ((ext = x->rfc3779_addr) == NULL)
  1099. goto done;
  1100. }
  1101. if (!X509v3_addr_is_canonical(ext))
  1102. validation_err(X509_V_ERR_INVALID_EXTENSION);
  1103. (void)sk_IPAddressFamily_set_cmp_func(ext, IPAddressFamily_cmp);
  1104. if ((child = sk_IPAddressFamily_dup(ext)) == NULL) {
  1105. ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
  1106. if (ctx != NULL)
  1107. ctx->error = X509_V_ERR_OUT_OF_MEM;
  1108. ret = 0;
  1109. goto done;
  1110. }
  1111. /*
  1112. * Now walk up the chain. No cert may list resources that its
  1113. * parent doesn't list.
  1114. */
  1115. for (i++; i < sk_X509_num(chain); i++) {
  1116. x = sk_X509_value(chain, i);
  1117. if (!X509v3_addr_is_canonical(x->rfc3779_addr))
  1118. validation_err(X509_V_ERR_INVALID_EXTENSION);
  1119. if (x->rfc3779_addr == NULL) {
  1120. for (j = 0; j < sk_IPAddressFamily_num(child); j++) {
  1121. IPAddressFamily *fc = sk_IPAddressFamily_value(child, j);
  1122. if (fc->ipAddressChoice->type != IPAddressChoice_inherit) {
  1123. validation_err(X509_V_ERR_UNNESTED_RESOURCE);
  1124. break;
  1125. }
  1126. }
  1127. continue;
  1128. }
  1129. (void)sk_IPAddressFamily_set_cmp_func(x->rfc3779_addr,
  1130. IPAddressFamily_cmp);
  1131. for (j = 0; j < sk_IPAddressFamily_num(child); j++) {
  1132. IPAddressFamily *fc = sk_IPAddressFamily_value(child, j);
  1133. int k = sk_IPAddressFamily_find(x->rfc3779_addr, fc);
  1134. IPAddressFamily *fp =
  1135. sk_IPAddressFamily_value(x->rfc3779_addr, k);
  1136. if (fp == NULL) {
  1137. if (fc->ipAddressChoice->type ==
  1138. IPAddressChoice_addressesOrRanges) {
  1139. validation_err(X509_V_ERR_UNNESTED_RESOURCE);
  1140. break;
  1141. }
  1142. continue;
  1143. }
  1144. if (fp->ipAddressChoice->type ==
  1145. IPAddressChoice_addressesOrRanges) {
  1146. if (fc->ipAddressChoice->type == IPAddressChoice_inherit
  1147. || addr_contains(fp->ipAddressChoice->u.addressesOrRanges,
  1148. fc->ipAddressChoice->u.addressesOrRanges,
  1149. length_from_afi(X509v3_addr_get_afi(fc))))
  1150. (void)sk_IPAddressFamily_set(child, j, fp);
  1151. else
  1152. validation_err(X509_V_ERR_UNNESTED_RESOURCE);
  1153. }
  1154. }
  1155. }
  1156. /*
  1157. * Trust anchor can't inherit.
  1158. */
  1159. if (x->rfc3779_addr != NULL) {
  1160. for (j = 0; j < sk_IPAddressFamily_num(x->rfc3779_addr); j++) {
  1161. IPAddressFamily *fp =
  1162. sk_IPAddressFamily_value(x->rfc3779_addr, j);
  1163. if (fp->ipAddressChoice->type == IPAddressChoice_inherit
  1164. && sk_IPAddressFamily_find(child, fp) >= 0)
  1165. validation_err(X509_V_ERR_UNNESTED_RESOURCE);
  1166. }
  1167. }
  1168. done:
  1169. sk_IPAddressFamily_free(child);
  1170. return ret;
  1171. }
  1172. #undef validation_err
  1173. /*
  1174. * RFC 3779 2.3 path validation -- called from X509_verify_cert().
  1175. */
  1176. int X509v3_addr_validate_path(X509_STORE_CTX *ctx)
  1177. {
  1178. if (ctx->chain == NULL
  1179. || sk_X509_num(ctx->chain) == 0
  1180. || ctx->verify_cb == NULL) {
  1181. ctx->error = X509_V_ERR_UNSPECIFIED;
  1182. return 0;
  1183. }
  1184. return addr_validate_path_internal(ctx, ctx->chain, NULL);
  1185. }
  1186. /*
  1187. * RFC 3779 2.3 path validation of an extension.
  1188. * Test whether chain covers extension.
  1189. */
  1190. int X509v3_addr_validate_resource_set(STACK_OF(X509) *chain,
  1191. IPAddrBlocks *ext, int allow_inheritance)
  1192. {
  1193. if (ext == NULL)
  1194. return 1;
  1195. if (chain == NULL || sk_X509_num(chain) == 0)
  1196. return 0;
  1197. if (!allow_inheritance && X509v3_addr_inherits(ext))
  1198. return 0;
  1199. return addr_validate_path_internal(NULL, chain, ext);
  1200. }
  1201. #endif /* OPENSSL_NO_RFC3779 */