v3_addr.c 35 KB

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