v3_asid.c 27 KB

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