v3_asid.c 26 KB

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