v3_asid.c 28 KB

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