v3_addr.c 42 KB

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