v3_asid.c 26 KB

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