v3_addr.c 42 KB

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