2
0

pcy_tree.c 22 KB

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