pcy_tree.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. /*
  2. * Copyright 2004-2023 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. #include "internal/cryptlib.h"
  10. #include <openssl/trace.h>
  11. #include <openssl/x509.h>
  12. #include <openssl/x509v3.h>
  13. #include "pcy_local.h"
  14. /*
  15. * If the maximum number of nodes in the policy tree isn't defined, set it to
  16. * a generous default of 1000 nodes.
  17. *
  18. * Defining this to be zero means unlimited policy tree growth which opens the
  19. * door on CVE-2023-0464.
  20. */
  21. #ifndef OPENSSL_POLICY_TREE_NODES_MAX
  22. # define OPENSSL_POLICY_TREE_NODES_MAX 1000
  23. #endif
  24. static void exnode_free(X509_POLICY_NODE *node);
  25. static void expected_print(BIO *channel,
  26. X509_POLICY_LEVEL *lev, X509_POLICY_NODE *node,
  27. int indent)
  28. {
  29. if ((lev->flags & X509_V_FLAG_INHIBIT_MAP)
  30. || !(node->data->flags & POLICY_DATA_FLAG_MAP_MASK))
  31. BIO_puts(channel, " Not Mapped\n");
  32. else {
  33. int i;
  34. STACK_OF(ASN1_OBJECT) *pset = node->data->expected_policy_set;
  35. ASN1_OBJECT *oid;
  36. BIO_puts(channel, " Expected: ");
  37. for (i = 0; i < sk_ASN1_OBJECT_num(pset); i++) {
  38. oid = sk_ASN1_OBJECT_value(pset, i);
  39. if (i)
  40. BIO_puts(channel, ", ");
  41. i2a_ASN1_OBJECT(channel, oid);
  42. }
  43. BIO_puts(channel, "\n");
  44. }
  45. }
  46. static void tree_print(BIO *channel,
  47. char *str, X509_POLICY_TREE *tree,
  48. X509_POLICY_LEVEL *curr)
  49. {
  50. X509_POLICY_LEVEL *plev;
  51. if (!curr)
  52. curr = tree->levels + tree->nlevel;
  53. else
  54. curr++;
  55. BIO_printf(channel, "Level print after %s\n", str);
  56. BIO_printf(channel, "Printing Up to Level %ld\n",
  57. (long)(curr - tree->levels));
  58. for (plev = tree->levels; plev != curr; plev++) {
  59. int i;
  60. BIO_printf(channel, "Level %ld, flags = %x\n",
  61. (long)(plev - tree->levels), plev->flags);
  62. for (i = 0; i < sk_X509_POLICY_NODE_num(plev->nodes); i++) {
  63. X509_POLICY_NODE *node =
  64. sk_X509_POLICY_NODE_value(plev->nodes, i);
  65. X509_POLICY_NODE_print(channel, node, 2);
  66. expected_print(channel, plev, node, 2);
  67. BIO_printf(channel, " Flags: %x\n", node->data->flags);
  68. }
  69. if (plev->anyPolicy)
  70. X509_POLICY_NODE_print(channel, plev->anyPolicy, 2);
  71. }
  72. }
  73. #define TREE_PRINT(str, tree, curr) \
  74. OSSL_TRACE_BEGIN(X509V3_POLICY) { \
  75. tree_print(trc_out, "before tree_prune()", tree, curr); \
  76. } OSSL_TRACE_END(X509V3_POLICY)
  77. /*-
  78. * Return value: <= 0 on error, or positive bit mask:
  79. *
  80. * X509_PCY_TREE_VALID: valid tree
  81. * X509_PCY_TREE_EMPTY: empty tree (including bare TA case)
  82. * X509_PCY_TREE_EXPLICIT: explicit policy required
  83. */
  84. static int tree_init(X509_POLICY_TREE **ptree, STACK_OF(X509) *certs,
  85. unsigned int flags)
  86. {
  87. X509_POLICY_TREE *tree;
  88. X509_POLICY_LEVEL *level;
  89. const X509_POLICY_CACHE *cache;
  90. X509_POLICY_DATA *data = NULL;
  91. int ret = X509_PCY_TREE_VALID;
  92. int n = sk_X509_num(certs) - 1; /* RFC5280 paths omit the TA */
  93. int explicit_policy = (flags & X509_V_FLAG_EXPLICIT_POLICY) ? 0 : n+1;
  94. int any_skip = (flags & X509_V_FLAG_INHIBIT_ANY) ? 0 : n+1;
  95. int map_skip = (flags & X509_V_FLAG_INHIBIT_MAP) ? 0 : n+1;
  96. int i;
  97. *ptree = NULL;
  98. /* Can't do anything with just a trust anchor */
  99. if (n == 0)
  100. return X509_PCY_TREE_EMPTY;
  101. /*
  102. * First setup the policy cache in all n non-TA certificates, this will be
  103. * used in X509_verify_cert() which will invoke the verify callback for all
  104. * certificates with invalid policy extensions.
  105. */
  106. for (i = n - 1; i >= 0; i--) {
  107. X509 *x = sk_X509_value(certs, i);
  108. /* Call for side-effect of computing hash and caching extensions */
  109. X509_check_purpose(x, -1, 0);
  110. /* If cache is NULL, likely ENOMEM: return immediately */
  111. if (ossl_policy_cache_set(x) == NULL)
  112. return X509_PCY_TREE_INTERNAL;
  113. }
  114. /*
  115. * At this point check for invalid policies and required explicit policy.
  116. * Note that the explicit_policy counter is a count-down to zero, with the
  117. * requirement kicking in if and once it does that. The counter is
  118. * decremented for every non-self-issued certificate in the path, but may
  119. * be further reduced by policy constraints in a non-leaf certificate.
  120. *
  121. * The ultimate policy set is the intersection of all the policies along
  122. * the path, if we hit a certificate with an empty policy set, and explicit
  123. * policy is required we're done.
  124. */
  125. for (i = n - 1;
  126. i >= 0 && (explicit_policy > 0 || (ret & X509_PCY_TREE_EMPTY) == 0);
  127. i--) {
  128. X509 *x = sk_X509_value(certs, i);
  129. uint32_t ex_flags = X509_get_extension_flags(x);
  130. /* All the policies are already cached, we can return early */
  131. if (ex_flags & EXFLAG_INVALID_POLICY)
  132. return X509_PCY_TREE_INVALID;
  133. /* Access the cache which we now know exists */
  134. cache = ossl_policy_cache_set(x);
  135. if ((ret & X509_PCY_TREE_VALID) && cache->data == NULL)
  136. ret = X509_PCY_TREE_EMPTY;
  137. if (explicit_policy > 0) {
  138. if (!(ex_flags & EXFLAG_SI))
  139. explicit_policy--;
  140. if ((cache->explicit_skip >= 0)
  141. && (cache->explicit_skip < explicit_policy))
  142. explicit_policy = cache->explicit_skip;
  143. }
  144. }
  145. if (explicit_policy == 0)
  146. ret |= X509_PCY_TREE_EXPLICIT;
  147. if ((ret & X509_PCY_TREE_VALID) == 0)
  148. return ret;
  149. /* If we get this far initialize the tree */
  150. if ((tree = OPENSSL_zalloc(sizeof(*tree))) == NULL)
  151. return X509_PCY_TREE_INTERNAL;
  152. /* Limit the growth of the tree to mitigate CVE-2023-0464 */
  153. tree->node_maximum = OPENSSL_POLICY_TREE_NODES_MAX;
  154. /*
  155. * http://tools.ietf.org/html/rfc5280#section-6.1.2, figure 3.
  156. *
  157. * The top level is implicitly for the trust anchor with valid expected
  158. * policies of anyPolicy. (RFC 5280 has the TA at depth 0 and the leaf at
  159. * depth n, we have the leaf at depth 0 and the TA at depth n).
  160. */
  161. if ((tree->levels = OPENSSL_zalloc(sizeof(*tree->levels)*(n+1))) == NULL) {
  162. OPENSSL_free(tree);
  163. return X509_PCY_TREE_INTERNAL;
  164. }
  165. tree->nlevel = n+1;
  166. level = tree->levels;
  167. if ((data = ossl_policy_data_new(NULL,
  168. OBJ_nid2obj(NID_any_policy), 0)) == NULL)
  169. goto bad_tree;
  170. if (ossl_policy_level_add_node(level, data, NULL, tree, 1) == NULL) {
  171. ossl_policy_data_free(data);
  172. goto bad_tree;
  173. }
  174. /*
  175. * In this pass initialize all the tree levels and whether anyPolicy and
  176. * policy mapping are inhibited at each level.
  177. */
  178. for (i = n - 1; i >= 0; i--) {
  179. X509 *x = sk_X509_value(certs, i);
  180. uint32_t ex_flags = X509_get_extension_flags(x);
  181. /* Access the cache which we now know exists */
  182. cache = ossl_policy_cache_set(x);
  183. X509_up_ref(x);
  184. (++level)->cert = x;
  185. if (!cache->anyPolicy)
  186. level->flags |= X509_V_FLAG_INHIBIT_ANY;
  187. /* Determine inhibit any and inhibit map flags */
  188. if (any_skip == 0) {
  189. /*
  190. * Any matching allowed only if certificate is self issued and not
  191. * the last in the chain.
  192. */
  193. if (!(ex_flags & EXFLAG_SI) || (i == 0))
  194. level->flags |= X509_V_FLAG_INHIBIT_ANY;
  195. } else {
  196. if (!(ex_flags & EXFLAG_SI))
  197. any_skip--;
  198. if ((cache->any_skip >= 0) && (cache->any_skip < any_skip))
  199. any_skip = cache->any_skip;
  200. }
  201. if (map_skip == 0)
  202. level->flags |= X509_V_FLAG_INHIBIT_MAP;
  203. else {
  204. if (!(ex_flags & EXFLAG_SI))
  205. map_skip--;
  206. if ((cache->map_skip >= 0) && (cache->map_skip < map_skip))
  207. map_skip = cache->map_skip;
  208. }
  209. }
  210. *ptree = tree;
  211. return ret;
  212. bad_tree:
  213. X509_policy_tree_free(tree);
  214. return X509_PCY_TREE_INTERNAL;
  215. }
  216. /*
  217. * Return value: 1 on success, 0 otherwise
  218. */
  219. static int tree_link_matching_nodes(X509_POLICY_LEVEL *curr,
  220. X509_POLICY_DATA *data,
  221. X509_POLICY_TREE *tree)
  222. {
  223. X509_POLICY_LEVEL *last = curr - 1;
  224. int i, matched = 0;
  225. /* Iterate through all in nodes linking matches */
  226. for (i = 0; i < sk_X509_POLICY_NODE_num(last->nodes); i++) {
  227. X509_POLICY_NODE *node = sk_X509_POLICY_NODE_value(last->nodes, i);
  228. if (ossl_policy_node_match(last, node, data->valid_policy)) {
  229. if (ossl_policy_level_add_node(curr, data, node, tree, 0) == NULL)
  230. return 0;
  231. matched = 1;
  232. }
  233. }
  234. if (!matched && last->anyPolicy) {
  235. if (ossl_policy_level_add_node(curr, data, last->anyPolicy, tree, 0) == NULL)
  236. return 0;
  237. }
  238. return 1;
  239. }
  240. /*
  241. * This corresponds to RFC3280 6.1.3(d)(1): link any data from
  242. * CertificatePolicies onto matching parent or anyPolicy if no match.
  243. *
  244. * Return value: 1 on success, 0 otherwise.
  245. */
  246. static int tree_link_nodes(X509_POLICY_LEVEL *curr,
  247. const X509_POLICY_CACHE *cache,
  248. X509_POLICY_TREE *tree)
  249. {
  250. int i;
  251. for (i = 0; i < sk_X509_POLICY_DATA_num(cache->data); i++) {
  252. X509_POLICY_DATA *data = sk_X509_POLICY_DATA_value(cache->data, i);
  253. /* Look for matching nodes in previous level */
  254. if (!tree_link_matching_nodes(curr, data, tree))
  255. return 0;
  256. }
  257. return 1;
  258. }
  259. /*
  260. * This corresponds to RFC3280 6.1.3(d)(2): Create new data for any unmatched
  261. * policies in the parent and link to anyPolicy.
  262. *
  263. * Return value: 1 on success, 0 otherwise.
  264. */
  265. static int tree_add_unmatched(X509_POLICY_LEVEL *curr,
  266. const X509_POLICY_CACHE *cache,
  267. const ASN1_OBJECT *id,
  268. X509_POLICY_NODE *node, X509_POLICY_TREE *tree)
  269. {
  270. X509_POLICY_DATA *data;
  271. if (id == NULL)
  272. id = node->data->valid_policy;
  273. /*
  274. * Create a new node with qualifiers from anyPolicy and id from unmatched
  275. * node.
  276. */
  277. if ((data = ossl_policy_data_new(NULL, id, node_critical(node))) == NULL)
  278. return 0;
  279. /* Curr may not have anyPolicy */
  280. data->qualifier_set = cache->anyPolicy->qualifier_set;
  281. data->flags |= POLICY_DATA_FLAG_SHARED_QUALIFIERS;
  282. if (ossl_policy_level_add_node(curr, data, node, tree, 1) == NULL) {
  283. ossl_policy_data_free(data);
  284. return 0;
  285. }
  286. return 1;
  287. }
  288. /*
  289. * Return value: 1 on success, 0 otherwise.
  290. */
  291. static int tree_link_unmatched(X509_POLICY_LEVEL *curr,
  292. const X509_POLICY_CACHE *cache,
  293. X509_POLICY_NODE *node, X509_POLICY_TREE *tree)
  294. {
  295. const X509_POLICY_LEVEL *last = curr - 1;
  296. int i;
  297. if ((last->flags & X509_V_FLAG_INHIBIT_MAP)
  298. || !(node->data->flags & POLICY_DATA_FLAG_MAPPED)) {
  299. /* If no policy mapping: matched if one child present */
  300. if (node->nchild)
  301. return 1;
  302. if (!tree_add_unmatched(curr, cache, NULL, node, tree))
  303. return 0;
  304. /* Add it */
  305. } else {
  306. /* If mapping: matched if one child per expected policy set */
  307. STACK_OF(ASN1_OBJECT) *expset = node->data->expected_policy_set;
  308. if (node->nchild == sk_ASN1_OBJECT_num(expset))
  309. return 1;
  310. /* Locate unmatched nodes */
  311. for (i = 0; i < sk_ASN1_OBJECT_num(expset); i++) {
  312. ASN1_OBJECT *oid = sk_ASN1_OBJECT_value(expset, i);
  313. if (ossl_policy_level_find_node(curr, node, oid))
  314. continue;
  315. if (!tree_add_unmatched(curr, cache, oid, node, tree))
  316. return 0;
  317. }
  318. }
  319. return 1;
  320. }
  321. /*
  322. * Return value: 1 on success, 0 otherwise
  323. */
  324. static int tree_link_any(X509_POLICY_LEVEL *curr,
  325. const X509_POLICY_CACHE *cache,
  326. X509_POLICY_TREE *tree)
  327. {
  328. int i;
  329. X509_POLICY_NODE *node;
  330. X509_POLICY_LEVEL *last = curr - 1;
  331. for (i = 0; i < sk_X509_POLICY_NODE_num(last->nodes); i++) {
  332. node = sk_X509_POLICY_NODE_value(last->nodes, i);
  333. if (!tree_link_unmatched(curr, cache, node, tree))
  334. return 0;
  335. }
  336. /* Finally add link to anyPolicy */
  337. if (last->anyPolicy &&
  338. ossl_policy_level_add_node(curr, cache->anyPolicy,
  339. last->anyPolicy, tree, 0) == NULL)
  340. return 0;
  341. return 1;
  342. }
  343. /*-
  344. * Prune the tree: delete any child mapped child data on the current level then
  345. * proceed up the tree deleting any data with no children. If we ever have no
  346. * data on a level we can halt because the tree will be empty.
  347. *
  348. * Return value: <= 0 error, otherwise one of:
  349. *
  350. * X509_PCY_TREE_VALID: valid tree
  351. * X509_PCY_TREE_EMPTY: empty tree
  352. */
  353. static int tree_prune(X509_POLICY_TREE *tree, X509_POLICY_LEVEL *curr)
  354. {
  355. STACK_OF(X509_POLICY_NODE) *nodes;
  356. X509_POLICY_NODE *node;
  357. int i;
  358. nodes = curr->nodes;
  359. if (curr->flags & X509_V_FLAG_INHIBIT_MAP) {
  360. for (i = sk_X509_POLICY_NODE_num(nodes) - 1; i >= 0; i--) {
  361. node = sk_X509_POLICY_NODE_value(nodes, i);
  362. /* Delete any mapped data: see RFC3280 XXXX */
  363. if (node->data->flags & POLICY_DATA_FLAG_MAP_MASK) {
  364. node->parent->nchild--;
  365. OPENSSL_free(node);
  366. (void)sk_X509_POLICY_NODE_delete(nodes, i);
  367. }
  368. }
  369. }
  370. for (;;) {
  371. --curr;
  372. nodes = curr->nodes;
  373. for (i = sk_X509_POLICY_NODE_num(nodes) - 1; i >= 0; i--) {
  374. node = sk_X509_POLICY_NODE_value(nodes, i);
  375. if (node->nchild == 0) {
  376. node->parent->nchild--;
  377. OPENSSL_free(node);
  378. (void)sk_X509_POLICY_NODE_delete(nodes, i);
  379. }
  380. }
  381. if (curr->anyPolicy && !curr->anyPolicy->nchild) {
  382. if (curr->anyPolicy->parent)
  383. curr->anyPolicy->parent->nchild--;
  384. OPENSSL_free(curr->anyPolicy);
  385. curr->anyPolicy = NULL;
  386. }
  387. if (curr == tree->levels) {
  388. /* If we zapped anyPolicy at top then tree is empty */
  389. if (!curr->anyPolicy)
  390. return X509_PCY_TREE_EMPTY;
  391. break;
  392. }
  393. }
  394. return X509_PCY_TREE_VALID;
  395. }
  396. /*
  397. * Return value: 1 on success, 0 otherwise.
  398. */
  399. static int tree_add_auth_node(STACK_OF(X509_POLICY_NODE) **pnodes,
  400. X509_POLICY_NODE *pcy)
  401. {
  402. if (*pnodes == NULL &&
  403. (*pnodes = ossl_policy_node_cmp_new()) == NULL)
  404. return 0;
  405. if (sk_X509_POLICY_NODE_find(*pnodes, pcy) >= 0)
  406. return 1;
  407. return sk_X509_POLICY_NODE_push(*pnodes, pcy) != 0;
  408. }
  409. #define TREE_CALC_FAILURE 0
  410. #define TREE_CALC_OK_NOFREE 1
  411. #define TREE_CALC_OK_DOFREE 2
  412. /*-
  413. * Calculate the authority set based on policy tree. The 'pnodes' parameter is
  414. * used as a store for the set of policy nodes used to calculate the user set.
  415. * If the authority set is not anyPolicy then pnodes will just point to the
  416. * authority set. If however the authority set is anyPolicy then the set of
  417. * valid policies (other than anyPolicy) is store in pnodes.
  418. *
  419. * Return value:
  420. * TREE_CALC_FAILURE on failure,
  421. * TREE_CALC_OK_NOFREE on success and pnodes need not be freed,
  422. * TREE_CALC_OK_DOFREE on success and pnodes needs to be freed
  423. */
  424. static int tree_calculate_authority_set(X509_POLICY_TREE *tree,
  425. STACK_OF(X509_POLICY_NODE) **pnodes)
  426. {
  427. X509_POLICY_LEVEL *curr;
  428. X509_POLICY_NODE *node, *anyptr;
  429. STACK_OF(X509_POLICY_NODE) **addnodes;
  430. int i, j;
  431. curr = tree->levels + tree->nlevel - 1;
  432. /* If last level contains anyPolicy set is anyPolicy */
  433. if (curr->anyPolicy) {
  434. if (!tree_add_auth_node(&tree->auth_policies, curr->anyPolicy))
  435. return TREE_CALC_FAILURE;
  436. addnodes = pnodes;
  437. } else
  438. /* Add policies to authority set */
  439. addnodes = &tree->auth_policies;
  440. curr = tree->levels;
  441. for (i = 1; i < tree->nlevel; i++) {
  442. /*
  443. * If no anyPolicy node on this level it can't appear on lower
  444. * levels so end search.
  445. */
  446. if ((anyptr = curr->anyPolicy) == NULL)
  447. break;
  448. curr++;
  449. for (j = 0; j < sk_X509_POLICY_NODE_num(curr->nodes); j++) {
  450. node = sk_X509_POLICY_NODE_value(curr->nodes, j);
  451. if ((node->parent == anyptr)
  452. && !tree_add_auth_node(addnodes, node)) {
  453. if (addnodes == pnodes) {
  454. sk_X509_POLICY_NODE_free(*pnodes);
  455. *pnodes = NULL;
  456. }
  457. return TREE_CALC_FAILURE;
  458. }
  459. }
  460. }
  461. if (addnodes == pnodes)
  462. return TREE_CALC_OK_DOFREE;
  463. *pnodes = tree->auth_policies;
  464. return TREE_CALC_OK_NOFREE;
  465. }
  466. /*
  467. * Return value: 1 on success, 0 otherwise.
  468. */
  469. static int tree_calculate_user_set(X509_POLICY_TREE *tree,
  470. STACK_OF(ASN1_OBJECT) *policy_oids,
  471. STACK_OF(X509_POLICY_NODE) *auth_nodes)
  472. {
  473. int i;
  474. X509_POLICY_NODE *node;
  475. ASN1_OBJECT *oid;
  476. X509_POLICY_NODE *anyPolicy;
  477. X509_POLICY_DATA *extra;
  478. /*
  479. * Check if anyPolicy present in authority constrained policy set: this
  480. * will happen if it is a leaf node.
  481. */
  482. if (sk_ASN1_OBJECT_num(policy_oids) <= 0)
  483. return 1;
  484. anyPolicy = tree->levels[tree->nlevel - 1].anyPolicy;
  485. for (i = 0; i < sk_ASN1_OBJECT_num(policy_oids); i++) {
  486. oid = sk_ASN1_OBJECT_value(policy_oids, i);
  487. if (OBJ_obj2nid(oid) == NID_any_policy) {
  488. tree->flags |= POLICY_FLAG_ANY_POLICY;
  489. return 1;
  490. }
  491. }
  492. for (i = 0; i < sk_ASN1_OBJECT_num(policy_oids); i++) {
  493. oid = sk_ASN1_OBJECT_value(policy_oids, i);
  494. node = ossl_policy_tree_find_sk(auth_nodes, oid);
  495. if (!node) {
  496. if (!anyPolicy)
  497. continue;
  498. /*
  499. * Create a new node with policy ID from user set and qualifiers
  500. * from anyPolicy.
  501. */
  502. extra = ossl_policy_data_new(NULL, oid, node_critical(anyPolicy));
  503. if (extra == NULL)
  504. return 0;
  505. extra->qualifier_set = anyPolicy->data->qualifier_set;
  506. extra->flags = POLICY_DATA_FLAG_SHARED_QUALIFIERS
  507. | POLICY_DATA_FLAG_EXTRA_NODE;
  508. node = ossl_policy_level_add_node(NULL, extra, anyPolicy->parent,
  509. tree, 1);
  510. if (node == NULL) {
  511. ossl_policy_data_free(extra);
  512. return 0;
  513. }
  514. }
  515. if (!tree->user_policies) {
  516. tree->user_policies = sk_X509_POLICY_NODE_new_null();
  517. if (!tree->user_policies) {
  518. exnode_free(node);
  519. return 0;
  520. }
  521. }
  522. if (!sk_X509_POLICY_NODE_push(tree->user_policies, node)) {
  523. exnode_free(node);
  524. return 0;
  525. }
  526. }
  527. return 1;
  528. }
  529. /*-
  530. * Return value: <= 0 error, otherwise one of:
  531. * X509_PCY_TREE_VALID: valid tree
  532. * X509_PCY_TREE_EMPTY: empty tree
  533. * (see tree_prune()).
  534. */
  535. static int tree_evaluate(X509_POLICY_TREE *tree)
  536. {
  537. int ret, i;
  538. X509_POLICY_LEVEL *curr = tree->levels + 1;
  539. const X509_POLICY_CACHE *cache;
  540. for (i = 1; i < tree->nlevel; i++, curr++) {
  541. cache = ossl_policy_cache_set(curr->cert);
  542. if (!tree_link_nodes(curr, cache, tree))
  543. return X509_PCY_TREE_INTERNAL;
  544. if (!(curr->flags & X509_V_FLAG_INHIBIT_ANY)
  545. && !tree_link_any(curr, cache, tree))
  546. return X509_PCY_TREE_INTERNAL;
  547. TREE_PRINT("before tree_prune()", tree, curr);
  548. ret = tree_prune(tree, curr);
  549. if (ret != X509_PCY_TREE_VALID)
  550. return ret;
  551. }
  552. return X509_PCY_TREE_VALID;
  553. }
  554. static void exnode_free(X509_POLICY_NODE *node)
  555. {
  556. if (node->data && (node->data->flags & POLICY_DATA_FLAG_EXTRA_NODE))
  557. OPENSSL_free(node);
  558. }
  559. void X509_policy_tree_free(X509_POLICY_TREE *tree)
  560. {
  561. X509_POLICY_LEVEL *curr;
  562. int i;
  563. if (!tree)
  564. return;
  565. sk_X509_POLICY_NODE_free(tree->auth_policies);
  566. sk_X509_POLICY_NODE_pop_free(tree->user_policies, exnode_free);
  567. for (i = 0, curr = tree->levels; i < tree->nlevel; i++, curr++) {
  568. X509_free(curr->cert);
  569. sk_X509_POLICY_NODE_pop_free(curr->nodes, ossl_policy_node_free);
  570. ossl_policy_node_free(curr->anyPolicy);
  571. }
  572. sk_X509_POLICY_DATA_pop_free(tree->extra_data, ossl_policy_data_free);
  573. OPENSSL_free(tree->levels);
  574. OPENSSL_free(tree);
  575. }
  576. /*-
  577. * Application policy checking function.
  578. * Return codes:
  579. * X509_PCY_TREE_FAILURE: Failure to satisfy explicit policy
  580. * X509_PCY_TREE_INVALID: Inconsistent or invalid extensions
  581. * X509_PCY_TREE_INTERNAL: Internal error, most likely malloc
  582. * X509_PCY_TREE_VALID: Success (null tree if empty or bare TA)
  583. */
  584. int X509_policy_check(X509_POLICY_TREE **ptree, int *pexplicit_policy,
  585. STACK_OF(X509) *certs,
  586. STACK_OF(ASN1_OBJECT) *policy_oids, unsigned int flags)
  587. {
  588. int init_ret;
  589. int ret;
  590. int calc_ret;
  591. X509_POLICY_TREE *tree = NULL;
  592. STACK_OF(X509_POLICY_NODE) *nodes, *auth_nodes = NULL;
  593. *ptree = NULL;
  594. *pexplicit_policy = 0;
  595. init_ret = tree_init(&tree, certs, flags);
  596. if (init_ret <= 0)
  597. return init_ret;
  598. if ((init_ret & X509_PCY_TREE_EXPLICIT) == 0) {
  599. if (init_ret & X509_PCY_TREE_EMPTY) {
  600. X509_policy_tree_free(tree);
  601. return X509_PCY_TREE_VALID;
  602. }
  603. } else {
  604. *pexplicit_policy = 1;
  605. /* Tree empty and requireExplicit True: Error */
  606. if (init_ret & X509_PCY_TREE_EMPTY)
  607. return X509_PCY_TREE_FAILURE;
  608. }
  609. ret = tree_evaluate(tree);
  610. TREE_PRINT("tree_evaluate()", tree, NULL);
  611. if (ret <= 0)
  612. goto error;
  613. if (ret == X509_PCY_TREE_EMPTY) {
  614. X509_policy_tree_free(tree);
  615. if (init_ret & X509_PCY_TREE_EXPLICIT)
  616. return X509_PCY_TREE_FAILURE;
  617. return X509_PCY_TREE_VALID;
  618. }
  619. /* Tree is not empty: continue */
  620. if ((calc_ret = tree_calculate_authority_set(tree, &auth_nodes)) == 0)
  621. goto error;
  622. sk_X509_POLICY_NODE_sort(auth_nodes);
  623. ret = tree_calculate_user_set(tree, policy_oids, auth_nodes);
  624. if (calc_ret == TREE_CALC_OK_DOFREE)
  625. sk_X509_POLICY_NODE_free(auth_nodes);
  626. if (!ret)
  627. goto error;
  628. *ptree = tree;
  629. if (init_ret & X509_PCY_TREE_EXPLICIT) {
  630. nodes = X509_policy_tree_get0_user_policies(tree);
  631. if (sk_X509_POLICY_NODE_num(nodes) <= 0)
  632. return X509_PCY_TREE_FAILURE;
  633. }
  634. return X509_PCY_TREE_VALID;
  635. error:
  636. X509_policy_tree_free(tree);
  637. return X509_PCY_TREE_INTERNAL;
  638. }