v3_addr.c 41 KB

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