v3_asid.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  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 3.2.
  59. */
  60. #include <stdio.h>
  61. #include <string.h>
  62. #include "internal/cryptlib.h"
  63. #include <openssl/conf.h>
  64. #include <openssl/asn1.h>
  65. #include <openssl/asn1t.h>
  66. #include <openssl/x509v3.h>
  67. #include <openssl/x509.h>
  68. #include <openssl/bn.h>
  69. /*
  70. * OpenSSL ASN.1 template translation of RFC 3779 3.2.3.
  71. */
  72. ASN1_SEQUENCE(ASRange) = {
  73. ASN1_SIMPLE(ASRange, min, ASN1_INTEGER),
  74. ASN1_SIMPLE(ASRange, max, ASN1_INTEGER)
  75. } ASN1_SEQUENCE_END(ASRange)
  76. ASN1_CHOICE(ASIdOrRange) = {
  77. ASN1_SIMPLE(ASIdOrRange, u.id, ASN1_INTEGER),
  78. ASN1_SIMPLE(ASIdOrRange, u.range, ASRange)
  79. } ASN1_CHOICE_END(ASIdOrRange)
  80. ASN1_CHOICE(ASIdentifierChoice) = {
  81. ASN1_SIMPLE(ASIdentifierChoice, u.inherit, ASN1_NULL),
  82. ASN1_SEQUENCE_OF(ASIdentifierChoice, u.asIdsOrRanges, ASIdOrRange)
  83. } ASN1_CHOICE_END(ASIdentifierChoice)
  84. ASN1_SEQUENCE(ASIdentifiers) = {
  85. ASN1_EXP_OPT(ASIdentifiers, asnum, ASIdentifierChoice, 0),
  86. ASN1_EXP_OPT(ASIdentifiers, rdi, ASIdentifierChoice, 1)
  87. } ASN1_SEQUENCE_END(ASIdentifiers)
  88. IMPLEMENT_ASN1_FUNCTIONS(ASRange)
  89. IMPLEMENT_ASN1_FUNCTIONS(ASIdOrRange)
  90. IMPLEMENT_ASN1_FUNCTIONS(ASIdentifierChoice)
  91. IMPLEMENT_ASN1_FUNCTIONS(ASIdentifiers)
  92. /*
  93. * i2r method for an ASIdentifierChoice.
  94. */
  95. static int i2r_ASIdentifierChoice(BIO *out,
  96. ASIdentifierChoice *choice,
  97. int indent, const char *msg)
  98. {
  99. int i;
  100. char *s;
  101. if (choice == NULL)
  102. return 1;
  103. BIO_printf(out, "%*s%s:\n", indent, "", msg);
  104. switch (choice->type) {
  105. case ASIdentifierChoice_inherit:
  106. BIO_printf(out, "%*sinherit\n", indent + 2, "");
  107. break;
  108. case ASIdentifierChoice_asIdsOrRanges:
  109. for (i = 0; i < sk_ASIdOrRange_num(choice->u.asIdsOrRanges); i++) {
  110. ASIdOrRange *aor =
  111. sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i);
  112. switch (aor->type) {
  113. case ASIdOrRange_id:
  114. if ((s = i2s_ASN1_INTEGER(NULL, aor->u.id)) == NULL)
  115. return 0;
  116. BIO_printf(out, "%*s%s\n", indent + 2, "", s);
  117. OPENSSL_free(s);
  118. break;
  119. case ASIdOrRange_range:
  120. if ((s = i2s_ASN1_INTEGER(NULL, aor->u.range->min)) == NULL)
  121. return 0;
  122. BIO_printf(out, "%*s%s-", indent + 2, "", s);
  123. OPENSSL_free(s);
  124. if ((s = i2s_ASN1_INTEGER(NULL, aor->u.range->max)) == NULL)
  125. return 0;
  126. BIO_printf(out, "%s\n", s);
  127. OPENSSL_free(s);
  128. break;
  129. default:
  130. return 0;
  131. }
  132. }
  133. break;
  134. default:
  135. return 0;
  136. }
  137. return 1;
  138. }
  139. /*
  140. * i2r method for an ASIdentifier extension.
  141. */
  142. static int i2r_ASIdentifiers(const X509V3_EXT_METHOD *method,
  143. void *ext, BIO *out, int indent)
  144. {
  145. ASIdentifiers *asid = ext;
  146. return (i2r_ASIdentifierChoice(out, asid->asnum, indent,
  147. "Autonomous System Numbers") &&
  148. i2r_ASIdentifierChoice(out, asid->rdi, indent,
  149. "Routing Domain Identifiers"));
  150. }
  151. /*
  152. * Sort comparision function for a sequence of ASIdOrRange elements.
  153. */
  154. static int ASIdOrRange_cmp(const ASIdOrRange *const *a_,
  155. const ASIdOrRange *const *b_)
  156. {
  157. const ASIdOrRange *a = *a_, *b = *b_;
  158. OPENSSL_assert((a->type == ASIdOrRange_id && a->u.id != NULL) ||
  159. (a->type == ASIdOrRange_range && a->u.range != NULL &&
  160. a->u.range->min != NULL && a->u.range->max != NULL));
  161. OPENSSL_assert((b->type == ASIdOrRange_id && b->u.id != NULL) ||
  162. (b->type == ASIdOrRange_range && b->u.range != NULL &&
  163. b->u.range->min != NULL && b->u.range->max != NULL));
  164. if (a->type == ASIdOrRange_id && b->type == ASIdOrRange_id)
  165. return ASN1_INTEGER_cmp(a->u.id, b->u.id);
  166. if (a->type == ASIdOrRange_range && b->type == ASIdOrRange_range) {
  167. int r = ASN1_INTEGER_cmp(a->u.range->min, b->u.range->min);
  168. return r != 0 ? r : ASN1_INTEGER_cmp(a->u.range->max,
  169. b->u.range->max);
  170. }
  171. if (a->type == ASIdOrRange_id)
  172. return ASN1_INTEGER_cmp(a->u.id, b->u.range->min);
  173. else
  174. return ASN1_INTEGER_cmp(a->u.range->min, b->u.id);
  175. }
  176. /*
  177. * Add an inherit element.
  178. */
  179. int v3_asid_add_inherit(ASIdentifiers *asid, int which)
  180. {
  181. ASIdentifierChoice **choice;
  182. if (asid == NULL)
  183. return 0;
  184. switch (which) {
  185. case V3_ASID_ASNUM:
  186. choice = &asid->asnum;
  187. break;
  188. case V3_ASID_RDI:
  189. choice = &asid->rdi;
  190. break;
  191. default:
  192. return 0;
  193. }
  194. if (*choice == NULL) {
  195. if ((*choice = ASIdentifierChoice_new()) == NULL)
  196. return 0;
  197. OPENSSL_assert((*choice)->u.inherit == NULL);
  198. if (((*choice)->u.inherit = ASN1_NULL_new()) == NULL)
  199. return 0;
  200. (*choice)->type = ASIdentifierChoice_inherit;
  201. }
  202. return (*choice)->type == ASIdentifierChoice_inherit;
  203. }
  204. /*
  205. * Add an ID or range to an ASIdentifierChoice.
  206. */
  207. int v3_asid_add_id_or_range(ASIdentifiers *asid,
  208. int which, ASN1_INTEGER *min, ASN1_INTEGER *max)
  209. {
  210. ASIdentifierChoice **choice;
  211. ASIdOrRange *aor;
  212. if (asid == NULL)
  213. return 0;
  214. switch (which) {
  215. case V3_ASID_ASNUM:
  216. choice = &asid->asnum;
  217. break;
  218. case V3_ASID_RDI:
  219. choice = &asid->rdi;
  220. break;
  221. default:
  222. return 0;
  223. }
  224. if (*choice != NULL && (*choice)->type == ASIdentifierChoice_inherit)
  225. return 0;
  226. if (*choice == NULL) {
  227. if ((*choice = ASIdentifierChoice_new()) == NULL)
  228. return 0;
  229. OPENSSL_assert((*choice)->u.asIdsOrRanges == NULL);
  230. (*choice)->u.asIdsOrRanges = sk_ASIdOrRange_new(ASIdOrRange_cmp);
  231. if ((*choice)->u.asIdsOrRanges == NULL)
  232. return 0;
  233. (*choice)->type = ASIdentifierChoice_asIdsOrRanges;
  234. }
  235. if ((aor = ASIdOrRange_new()) == NULL)
  236. return 0;
  237. if (max == NULL) {
  238. aor->type = ASIdOrRange_id;
  239. aor->u.id = min;
  240. } else {
  241. aor->type = ASIdOrRange_range;
  242. if ((aor->u.range = ASRange_new()) == NULL)
  243. goto err;
  244. ASN1_INTEGER_free(aor->u.range->min);
  245. aor->u.range->min = min;
  246. ASN1_INTEGER_free(aor->u.range->max);
  247. aor->u.range->max = max;
  248. }
  249. if (!(sk_ASIdOrRange_push((*choice)->u.asIdsOrRanges, aor)))
  250. goto err;
  251. return 1;
  252. err:
  253. ASIdOrRange_free(aor);
  254. return 0;
  255. }
  256. /*
  257. * Extract min and max values from an ASIdOrRange.
  258. */
  259. static void extract_min_max(ASIdOrRange *aor,
  260. ASN1_INTEGER **min, ASN1_INTEGER **max)
  261. {
  262. OPENSSL_assert(aor != NULL && min != NULL && max != NULL);
  263. switch (aor->type) {
  264. case ASIdOrRange_id:
  265. *min = aor->u.id;
  266. *max = aor->u.id;
  267. return;
  268. case ASIdOrRange_range:
  269. *min = aor->u.range->min;
  270. *max = aor->u.range->max;
  271. return;
  272. }
  273. }
  274. /*
  275. * Check whether an ASIdentifierChoice is in canonical form.
  276. */
  277. static int ASIdentifierChoice_is_canonical(ASIdentifierChoice *choice)
  278. {
  279. ASN1_INTEGER *a_max_plus_one = NULL;
  280. BIGNUM *bn = NULL;
  281. int i, ret = 0;
  282. /*
  283. * Empty element or inheritance is canonical.
  284. */
  285. if (choice == NULL || choice->type == ASIdentifierChoice_inherit)
  286. return 1;
  287. /*
  288. * If not a list, or if empty list, it's broken.
  289. */
  290. if (choice->type != ASIdentifierChoice_asIdsOrRanges ||
  291. sk_ASIdOrRange_num(choice->u.asIdsOrRanges) == 0)
  292. return 0;
  293. /*
  294. * It's a list, check it.
  295. */
  296. for (i = 0; i < sk_ASIdOrRange_num(choice->u.asIdsOrRanges) - 1; i++) {
  297. ASIdOrRange *a = sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i);
  298. ASIdOrRange *b = sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i + 1);
  299. ASN1_INTEGER *a_min = NULL, *a_max = NULL, *b_min = NULL, *b_max =
  300. NULL;
  301. extract_min_max(a, &a_min, &a_max);
  302. extract_min_max(b, &b_min, &b_max);
  303. /*
  304. * Punt misordered list, overlapping start, or inverted range.
  305. */
  306. if (ASN1_INTEGER_cmp(a_min, b_min) >= 0 ||
  307. ASN1_INTEGER_cmp(a_min, a_max) > 0 ||
  308. ASN1_INTEGER_cmp(b_min, b_max) > 0)
  309. goto done;
  310. /*
  311. * Calculate a_max + 1 to check for adjacency.
  312. */
  313. if ((bn == NULL && (bn = BN_new()) == NULL) ||
  314. ASN1_INTEGER_to_BN(a_max, bn) == NULL ||
  315. !BN_add_word(bn, 1) ||
  316. (a_max_plus_one =
  317. BN_to_ASN1_INTEGER(bn, a_max_plus_one)) == NULL) {
  318. X509V3err(X509V3_F_ASIDENTIFIERCHOICE_IS_CANONICAL,
  319. ERR_R_MALLOC_FAILURE);
  320. goto done;
  321. }
  322. /*
  323. * Punt if adjacent or overlapping.
  324. */
  325. if (ASN1_INTEGER_cmp(a_max_plus_one, b_min) >= 0)
  326. goto done;
  327. }
  328. /*
  329. * Check for inverted range.
  330. */
  331. i = sk_ASIdOrRange_num(choice->u.asIdsOrRanges) - 1;
  332. {
  333. ASIdOrRange *a = sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i);
  334. ASN1_INTEGER *a_min, *a_max;
  335. if (a != NULL && a->type == ASIdOrRange_range) {
  336. extract_min_max(a, &a_min, &a_max);
  337. if (ASN1_INTEGER_cmp(a_min, a_max) > 0)
  338. goto done;
  339. }
  340. }
  341. ret = 1;
  342. done:
  343. ASN1_INTEGER_free(a_max_plus_one);
  344. BN_free(bn);
  345. return ret;
  346. }
  347. /*
  348. * Check whether an ASIdentifier extension is in canonical form.
  349. */
  350. int v3_asid_is_canonical(ASIdentifiers *asid)
  351. {
  352. return (asid == NULL ||
  353. (ASIdentifierChoice_is_canonical(asid->asnum) &&
  354. ASIdentifierChoice_is_canonical(asid->rdi)));
  355. }
  356. /*
  357. * Whack an ASIdentifierChoice into canonical form.
  358. */
  359. static int ASIdentifierChoice_canonize(ASIdentifierChoice *choice)
  360. {
  361. ASN1_INTEGER *a_max_plus_one = NULL;
  362. BIGNUM *bn = NULL;
  363. int i, ret = 0;
  364. /*
  365. * Nothing to do for empty element or inheritance.
  366. */
  367. if (choice == NULL || choice->type == ASIdentifierChoice_inherit)
  368. return 1;
  369. /*
  370. * If not a list, or if empty list, it's broken.
  371. */
  372. if (choice->type != ASIdentifierChoice_asIdsOrRanges ||
  373. sk_ASIdOrRange_num(choice->u.asIdsOrRanges) == 0) {
  374. X509V3err(X509V3_F_ASIDENTIFIERCHOICE_CANONIZE,
  375. X509V3_R_EXTENSION_VALUE_ERROR);
  376. return 0;
  377. }
  378. /*
  379. * We have a non-empty list. Sort it.
  380. */
  381. sk_ASIdOrRange_sort(choice->u.asIdsOrRanges);
  382. /*
  383. * Now check for errors and suboptimal encoding, rejecting the
  384. * former and fixing the latter.
  385. */
  386. for (i = 0; i < sk_ASIdOrRange_num(choice->u.asIdsOrRanges) - 1; i++) {
  387. ASIdOrRange *a = sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i);
  388. ASIdOrRange *b = sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i + 1);
  389. ASN1_INTEGER *a_min = NULL, *a_max = NULL, *b_min = NULL, *b_max =
  390. NULL;
  391. extract_min_max(a, &a_min, &a_max);
  392. extract_min_max(b, &b_min, &b_max);
  393. /*
  394. * Make sure we're properly sorted (paranoia).
  395. */
  396. OPENSSL_assert(ASN1_INTEGER_cmp(a_min, b_min) <= 0);
  397. /*
  398. * Punt inverted ranges.
  399. */
  400. if (ASN1_INTEGER_cmp(a_min, a_max) > 0 ||
  401. ASN1_INTEGER_cmp(b_min, b_max) > 0)
  402. goto done;
  403. /*
  404. * Check for overlaps.
  405. */
  406. if (ASN1_INTEGER_cmp(a_max, b_min) >= 0) {
  407. X509V3err(X509V3_F_ASIDENTIFIERCHOICE_CANONIZE,
  408. X509V3_R_EXTENSION_VALUE_ERROR);
  409. goto done;
  410. }
  411. /*
  412. * Calculate a_max + 1 to check for adjacency.
  413. */
  414. if ((bn == NULL && (bn = BN_new()) == NULL) ||
  415. ASN1_INTEGER_to_BN(a_max, bn) == NULL ||
  416. !BN_add_word(bn, 1) ||
  417. (a_max_plus_one =
  418. BN_to_ASN1_INTEGER(bn, a_max_plus_one)) == NULL) {
  419. X509V3err(X509V3_F_ASIDENTIFIERCHOICE_CANONIZE,
  420. ERR_R_MALLOC_FAILURE);
  421. goto done;
  422. }
  423. /*
  424. * If a and b are adjacent, merge them.
  425. */
  426. if (ASN1_INTEGER_cmp(a_max_plus_one, b_min) == 0) {
  427. ASRange *r;
  428. switch (a->type) {
  429. case ASIdOrRange_id:
  430. if ((r = OPENSSL_malloc(sizeof(*r))) == NULL) {
  431. X509V3err(X509V3_F_ASIDENTIFIERCHOICE_CANONIZE,
  432. ERR_R_MALLOC_FAILURE);
  433. goto done;
  434. }
  435. r->min = a_min;
  436. r->max = b_max;
  437. a->type = ASIdOrRange_range;
  438. a->u.range = r;
  439. break;
  440. case ASIdOrRange_range:
  441. ASN1_INTEGER_free(a->u.range->max);
  442. a->u.range->max = b_max;
  443. break;
  444. }
  445. switch (b->type) {
  446. case ASIdOrRange_id:
  447. b->u.id = NULL;
  448. break;
  449. case ASIdOrRange_range:
  450. b->u.range->max = NULL;
  451. break;
  452. }
  453. ASIdOrRange_free(b);
  454. (void)sk_ASIdOrRange_delete(choice->u.asIdsOrRanges, i + 1);
  455. i--;
  456. continue;
  457. }
  458. }
  459. /*
  460. * Check for final inverted range.
  461. */
  462. i = sk_ASIdOrRange_num(choice->u.asIdsOrRanges) - 1;
  463. {
  464. ASIdOrRange *a = sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i);
  465. ASN1_INTEGER *a_min, *a_max;
  466. if (a != NULL && a->type == ASIdOrRange_range) {
  467. extract_min_max(a, &a_min, &a_max);
  468. if (ASN1_INTEGER_cmp(a_min, a_max) > 0)
  469. goto done;
  470. }
  471. }
  472. OPENSSL_assert(ASIdentifierChoice_is_canonical(choice)); /* Paranoia */
  473. ret = 1;
  474. done:
  475. ASN1_INTEGER_free(a_max_plus_one);
  476. BN_free(bn);
  477. return ret;
  478. }
  479. /*
  480. * Whack an ASIdentifier extension into canonical form.
  481. */
  482. int v3_asid_canonize(ASIdentifiers *asid)
  483. {
  484. return (asid == NULL ||
  485. (ASIdentifierChoice_canonize(asid->asnum) &&
  486. ASIdentifierChoice_canonize(asid->rdi)));
  487. }
  488. /*
  489. * v2i method for an ASIdentifier extension.
  490. */
  491. static void *v2i_ASIdentifiers(const struct v3_ext_method *method,
  492. struct v3_ext_ctx *ctx,
  493. STACK_OF(CONF_VALUE) *values)
  494. {
  495. ASN1_INTEGER *min = NULL, *max = NULL;
  496. ASIdentifiers *asid = NULL;
  497. int i;
  498. if ((asid = ASIdentifiers_new()) == NULL) {
  499. X509V3err(X509V3_F_V2I_ASIDENTIFIERS, ERR_R_MALLOC_FAILURE);
  500. return NULL;
  501. }
  502. for (i = 0; i < sk_CONF_VALUE_num(values); i++) {
  503. CONF_VALUE *val = sk_CONF_VALUE_value(values, i);
  504. int i1 = 0, i2 = 0, i3 = 0, is_range = 0, which = 0;
  505. /*
  506. * Figure out whether this is an AS or an RDI.
  507. */
  508. if (!name_cmp(val->name, "AS")) {
  509. which = V3_ASID_ASNUM;
  510. } else if (!name_cmp(val->name, "RDI")) {
  511. which = V3_ASID_RDI;
  512. } else {
  513. X509V3err(X509V3_F_V2I_ASIDENTIFIERS,
  514. X509V3_R_EXTENSION_NAME_ERROR);
  515. X509V3_conf_err(val);
  516. goto err;
  517. }
  518. /*
  519. * Handle inheritance.
  520. */
  521. if (strcmp(val->value, "inherit") == 0) {
  522. if (v3_asid_add_inherit(asid, which))
  523. continue;
  524. X509V3err(X509V3_F_V2I_ASIDENTIFIERS,
  525. X509V3_R_INVALID_INHERITANCE);
  526. X509V3_conf_err(val);
  527. goto err;
  528. }
  529. /*
  530. * Number, range, or mistake, pick it apart and figure out which.
  531. */
  532. i1 = strspn(val->value, "0123456789");
  533. if (val->value[i1] == '\0') {
  534. is_range = 0;
  535. } else {
  536. is_range = 1;
  537. i2 = i1 + strspn(val->value + i1, " \t");
  538. if (val->value[i2] != '-') {
  539. X509V3err(X509V3_F_V2I_ASIDENTIFIERS,
  540. X509V3_R_INVALID_ASNUMBER);
  541. X509V3_conf_err(val);
  542. goto err;
  543. }
  544. i2++;
  545. i2 = i2 + strspn(val->value + i2, " \t");
  546. i3 = i2 + strspn(val->value + i2, "0123456789");
  547. if (val->value[i3] != '\0') {
  548. X509V3err(X509V3_F_V2I_ASIDENTIFIERS,
  549. X509V3_R_INVALID_ASRANGE);
  550. X509V3_conf_err(val);
  551. goto err;
  552. }
  553. }
  554. /*
  555. * Syntax is ok, read and add it.
  556. */
  557. if (!is_range) {
  558. if (!X509V3_get_value_int(val, &min)) {
  559. X509V3err(X509V3_F_V2I_ASIDENTIFIERS, ERR_R_MALLOC_FAILURE);
  560. goto err;
  561. }
  562. } else {
  563. char *s = BUF_strdup(val->value);
  564. if (s == NULL) {
  565. X509V3err(X509V3_F_V2I_ASIDENTIFIERS, ERR_R_MALLOC_FAILURE);
  566. goto err;
  567. }
  568. s[i1] = '\0';
  569. min = s2i_ASN1_INTEGER(NULL, s);
  570. max = s2i_ASN1_INTEGER(NULL, s + i2);
  571. OPENSSL_free(s);
  572. if (min == NULL || max == NULL) {
  573. X509V3err(X509V3_F_V2I_ASIDENTIFIERS, ERR_R_MALLOC_FAILURE);
  574. goto err;
  575. }
  576. if (ASN1_INTEGER_cmp(min, max) > 0) {
  577. X509V3err(X509V3_F_V2I_ASIDENTIFIERS,
  578. X509V3_R_EXTENSION_VALUE_ERROR);
  579. goto err;
  580. }
  581. }
  582. if (!v3_asid_add_id_or_range(asid, which, min, max)) {
  583. X509V3err(X509V3_F_V2I_ASIDENTIFIERS, ERR_R_MALLOC_FAILURE);
  584. goto err;
  585. }
  586. min = max = NULL;
  587. }
  588. /*
  589. * Canonize the result, then we're done.
  590. */
  591. if (!v3_asid_canonize(asid))
  592. goto err;
  593. return asid;
  594. err:
  595. ASIdentifiers_free(asid);
  596. ASN1_INTEGER_free(min);
  597. ASN1_INTEGER_free(max);
  598. return NULL;
  599. }
  600. /*
  601. * OpenSSL dispatch.
  602. */
  603. const X509V3_EXT_METHOD v3_asid = {
  604. NID_sbgp_autonomousSysNum, /* nid */
  605. 0, /* flags */
  606. ASN1_ITEM_ref(ASIdentifiers), /* template */
  607. 0, 0, 0, 0, /* old functions, ignored */
  608. 0, /* i2s */
  609. 0, /* s2i */
  610. 0, /* i2v */
  611. v2i_ASIdentifiers, /* v2i */
  612. i2r_ASIdentifiers, /* i2r */
  613. 0, /* r2i */
  614. NULL /* extension-specific data */
  615. };
  616. /*
  617. * Figure out whether extension uses inheritance.
  618. */
  619. int v3_asid_inherits(ASIdentifiers *asid)
  620. {
  621. return (asid != NULL &&
  622. ((asid->asnum != NULL &&
  623. asid->asnum->type == ASIdentifierChoice_inherit) ||
  624. (asid->rdi != NULL &&
  625. asid->rdi->type == ASIdentifierChoice_inherit)));
  626. }
  627. /*
  628. * Figure out whether parent contains child.
  629. */
  630. static int asid_contains(ASIdOrRanges *parent, ASIdOrRanges *child)
  631. {
  632. ASN1_INTEGER *p_min = NULL, *p_max = NULL, *c_min = NULL, *c_max = NULL;
  633. int p, c;
  634. if (child == NULL || parent == child)
  635. return 1;
  636. if (parent == NULL)
  637. return 0;
  638. p = 0;
  639. for (c = 0; c < sk_ASIdOrRange_num(child); c++) {
  640. extract_min_max(sk_ASIdOrRange_value(child, c), &c_min, &c_max);
  641. for (;; p++) {
  642. if (p >= sk_ASIdOrRange_num(parent))
  643. return 0;
  644. extract_min_max(sk_ASIdOrRange_value(parent, p), &p_min, &p_max);
  645. if (ASN1_INTEGER_cmp(p_max, c_max) < 0)
  646. continue;
  647. if (ASN1_INTEGER_cmp(p_min, c_min) > 0)
  648. return 0;
  649. break;
  650. }
  651. }
  652. return 1;
  653. }
  654. /*
  655. * Test whether a is a subet of b.
  656. */
  657. int v3_asid_subset(ASIdentifiers *a, ASIdentifiers *b)
  658. {
  659. return (a == NULL ||
  660. a == b ||
  661. (b != NULL &&
  662. !v3_asid_inherits(a) &&
  663. !v3_asid_inherits(b) &&
  664. asid_contains(b->asnum->u.asIdsOrRanges,
  665. a->asnum->u.asIdsOrRanges) &&
  666. asid_contains(b->rdi->u.asIdsOrRanges,
  667. a->rdi->u.asIdsOrRanges)));
  668. }
  669. /*
  670. * Validation error handling via callback.
  671. */
  672. #define validation_err(_err_) \
  673. do { \
  674. if (ctx != NULL) { \
  675. ctx->error = _err_; \
  676. ctx->error_depth = i; \
  677. ctx->current_cert = x; \
  678. ret = ctx->verify_cb(0, ctx); \
  679. } else { \
  680. ret = 0; \
  681. } \
  682. if (!ret) \
  683. goto done; \
  684. } while (0)
  685. /*
  686. * Core code for RFC 3779 3.3 path validation.
  687. */
  688. static int v3_asid_validate_path_internal(X509_STORE_CTX *ctx,
  689. STACK_OF(X509) *chain,
  690. ASIdentifiers *ext)
  691. {
  692. ASIdOrRanges *child_as = NULL, *child_rdi = NULL;
  693. int i, ret = 1, inherit_as = 0, inherit_rdi = 0;
  694. X509 *x;
  695. OPENSSL_assert(chain != NULL && sk_X509_num(chain) > 0);
  696. OPENSSL_assert(ctx != NULL || ext != NULL);
  697. OPENSSL_assert(ctx == NULL || ctx->verify_cb != NULL);
  698. /*
  699. * Figure out where to start. If we don't have an extension to
  700. * check, we're done. Otherwise, check canonical form and
  701. * set up for walking up the chain.
  702. */
  703. if (ext != NULL) {
  704. i = -1;
  705. x = NULL;
  706. } else {
  707. i = 0;
  708. x = sk_X509_value(chain, i);
  709. OPENSSL_assert(x != NULL);
  710. if ((ext = x->rfc3779_asid) == NULL)
  711. goto done;
  712. }
  713. if (!v3_asid_is_canonical(ext))
  714. validation_err(X509_V_ERR_INVALID_EXTENSION);
  715. if (ext->asnum != NULL) {
  716. switch (ext->asnum->type) {
  717. case ASIdentifierChoice_inherit:
  718. inherit_as = 1;
  719. break;
  720. case ASIdentifierChoice_asIdsOrRanges:
  721. child_as = ext->asnum->u.asIdsOrRanges;
  722. break;
  723. }
  724. }
  725. if (ext->rdi != NULL) {
  726. switch (ext->rdi->type) {
  727. case ASIdentifierChoice_inherit:
  728. inherit_rdi = 1;
  729. break;
  730. case ASIdentifierChoice_asIdsOrRanges:
  731. child_rdi = ext->rdi->u.asIdsOrRanges;
  732. break;
  733. }
  734. }
  735. /*
  736. * Now walk up the chain. Extensions must be in canonical form, no
  737. * cert may list resources that its parent doesn't list.
  738. */
  739. for (i++; i < sk_X509_num(chain); i++) {
  740. x = sk_X509_value(chain, i);
  741. OPENSSL_assert(x != NULL);
  742. if (x->rfc3779_asid == NULL) {
  743. if (child_as != NULL || child_rdi != NULL)
  744. validation_err(X509_V_ERR_UNNESTED_RESOURCE);
  745. continue;
  746. }
  747. if (!v3_asid_is_canonical(x->rfc3779_asid))
  748. validation_err(X509_V_ERR_INVALID_EXTENSION);
  749. if (x->rfc3779_asid->asnum == NULL && child_as != NULL) {
  750. validation_err(X509_V_ERR_UNNESTED_RESOURCE);
  751. child_as = NULL;
  752. inherit_as = 0;
  753. }
  754. if (x->rfc3779_asid->asnum != NULL &&
  755. x->rfc3779_asid->asnum->type ==
  756. ASIdentifierChoice_asIdsOrRanges) {
  757. if (inherit_as
  758. || asid_contains(x->rfc3779_asid->asnum->u.asIdsOrRanges,
  759. child_as)) {
  760. child_as = x->rfc3779_asid->asnum->u.asIdsOrRanges;
  761. inherit_as = 0;
  762. } else {
  763. validation_err(X509_V_ERR_UNNESTED_RESOURCE);
  764. }
  765. }
  766. if (x->rfc3779_asid->rdi == NULL && child_rdi != NULL) {
  767. validation_err(X509_V_ERR_UNNESTED_RESOURCE);
  768. child_rdi = NULL;
  769. inherit_rdi = 0;
  770. }
  771. if (x->rfc3779_asid->rdi != NULL &&
  772. x->rfc3779_asid->rdi->type == ASIdentifierChoice_asIdsOrRanges) {
  773. if (inherit_rdi ||
  774. asid_contains(x->rfc3779_asid->rdi->u.asIdsOrRanges,
  775. child_rdi)) {
  776. child_rdi = x->rfc3779_asid->rdi->u.asIdsOrRanges;
  777. inherit_rdi = 0;
  778. } else {
  779. validation_err(X509_V_ERR_UNNESTED_RESOURCE);
  780. }
  781. }
  782. }
  783. /*
  784. * Trust anchor can't inherit.
  785. */
  786. OPENSSL_assert(x != NULL);
  787. if (x->rfc3779_asid != NULL) {
  788. if (x->rfc3779_asid->asnum != NULL &&
  789. x->rfc3779_asid->asnum->type == ASIdentifierChoice_inherit)
  790. validation_err(X509_V_ERR_UNNESTED_RESOURCE);
  791. if (x->rfc3779_asid->rdi != NULL &&
  792. x->rfc3779_asid->rdi->type == ASIdentifierChoice_inherit)
  793. validation_err(X509_V_ERR_UNNESTED_RESOURCE);
  794. }
  795. done:
  796. return ret;
  797. }
  798. #undef validation_err
  799. /*
  800. * RFC 3779 3.3 path validation -- called from X509_verify_cert().
  801. */
  802. int v3_asid_validate_path(X509_STORE_CTX *ctx)
  803. {
  804. return v3_asid_validate_path_internal(ctx, ctx->chain, NULL);
  805. }
  806. /*
  807. * RFC 3779 3.3 path validation of an extension.
  808. * Test whether chain covers extension.
  809. */
  810. int v3_asid_validate_resource_set(STACK_OF(X509) *chain,
  811. ASIdentifiers *ext, int allow_inheritance)
  812. {
  813. if (ext == NULL)
  814. return 1;
  815. if (chain == NULL || sk_X509_num(chain) == 0)
  816. return 0;
  817. if (!allow_inheritance && v3_asid_inherits(ext))
  818. return 0;
  819. return v3_asid_validate_path_internal(NULL, chain, ext);
  820. }