v3_asid.c 22 KB

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