2
0

v3_addr.c 41 KB

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