v3_addr.c 42 KB

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