v3_addr.c 36 KB

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