v3_asid.c 26 KB

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