pcy_tree.c 23 KB

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