v3_asid.c 22 KB

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