v3_addr.c 40 KB

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