v3_asid.c 26 KB

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