pcy_tree.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. /*
  2. * Copyright 2004-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. #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. X509V3err(X509V3_F_TREE_INIT, ERR_R_MALLOC_FAILURE);
  145. return X509_PCY_TREE_INTERNAL;
  146. }
  147. /*
  148. * http://tools.ietf.org/html/rfc5280#section-6.1.2, figure 3.
  149. *
  150. * The top level is implicitly for the trust anchor with valid expected
  151. * policies of anyPolicy. (RFC 5280 has the TA at depth 0 and the leaf at
  152. * depth n, we have the leaf at depth 0 and the TA at depth n).
  153. */
  154. if ((tree->levels = OPENSSL_zalloc(sizeof(*tree->levels)*(n+1))) == NULL) {
  155. OPENSSL_free(tree);
  156. X509V3err(X509V3_F_TREE_INIT, ERR_R_MALLOC_FAILURE);
  157. return X509_PCY_TREE_INTERNAL;
  158. }
  159. tree->nlevel = n+1;
  160. level = tree->levels;
  161. if ((data = policy_data_new(NULL, OBJ_nid2obj(NID_any_policy), 0)) == NULL)
  162. goto bad_tree;
  163. if (level_add_node(level, data, NULL, tree) == NULL) {
  164. policy_data_free(data);
  165. goto bad_tree;
  166. }
  167. /*
  168. * In this pass initialize all the tree levels and whether anyPolicy and
  169. * policy mapping are inhibited at each level.
  170. */
  171. for (i = n - 1; i >= 0; i--) {
  172. X509 *x = sk_X509_value(certs, i);
  173. uint32_t ex_flags = X509_get_extension_flags(x);
  174. /* Access the cache which we now know exists */
  175. cache = policy_cache_set(x);
  176. X509_up_ref(x);
  177. (++level)->cert = x;
  178. if (!cache->anyPolicy)
  179. level->flags |= X509_V_FLAG_INHIBIT_ANY;
  180. /* Determine inhibit any and inhibit map flags */
  181. if (any_skip == 0) {
  182. /*
  183. * Any matching allowed only if certificate is self issued and not
  184. * the last in the chain.
  185. */
  186. if (!(ex_flags & EXFLAG_SI) || (i == 0))
  187. level->flags |= X509_V_FLAG_INHIBIT_ANY;
  188. } else {
  189. if (!(ex_flags & EXFLAG_SI))
  190. any_skip--;
  191. if ((cache->any_skip >= 0) && (cache->any_skip < any_skip))
  192. any_skip = cache->any_skip;
  193. }
  194. if (map_skip == 0)
  195. level->flags |= X509_V_FLAG_INHIBIT_MAP;
  196. else {
  197. if (!(ex_flags & EXFLAG_SI))
  198. map_skip--;
  199. if ((cache->map_skip >= 0) && (cache->map_skip < map_skip))
  200. map_skip = cache->map_skip;
  201. }
  202. }
  203. *ptree = tree;
  204. return ret;
  205. bad_tree:
  206. X509_policy_tree_free(tree);
  207. return X509_PCY_TREE_INTERNAL;
  208. }
  209. /*
  210. * Return value: 1 on success, 0 otherwise
  211. */
  212. static int tree_link_matching_nodes(X509_POLICY_LEVEL *curr,
  213. X509_POLICY_DATA *data)
  214. {
  215. X509_POLICY_LEVEL *last = curr - 1;
  216. int i, matched = 0;
  217. /* Iterate through all in nodes linking matches */
  218. for (i = 0; i < sk_X509_POLICY_NODE_num(last->nodes); i++) {
  219. X509_POLICY_NODE *node = sk_X509_POLICY_NODE_value(last->nodes, i);
  220. if (policy_node_match(last, node, data->valid_policy)) {
  221. if (level_add_node(curr, data, node, NULL) == NULL)
  222. return 0;
  223. matched = 1;
  224. }
  225. }
  226. if (!matched && last->anyPolicy) {
  227. if (level_add_node(curr, data, last->anyPolicy, NULL) == NULL)
  228. return 0;
  229. }
  230. return 1;
  231. }
  232. /*
  233. * This corresponds to RFC3280 6.1.3(d)(1): link any data from
  234. * CertificatePolicies onto matching parent or anyPolicy if no match.
  235. *
  236. * Return value: 1 on success, 0 otherwise.
  237. */
  238. static int tree_link_nodes(X509_POLICY_LEVEL *curr,
  239. const X509_POLICY_CACHE *cache)
  240. {
  241. int i;
  242. for (i = 0; i < sk_X509_POLICY_DATA_num(cache->data); i++) {
  243. X509_POLICY_DATA *data = sk_X509_POLICY_DATA_value(cache->data, i);
  244. /* Look for matching nodes in previous level */
  245. if (!tree_link_matching_nodes(curr, data))
  246. return 0;
  247. }
  248. return 1;
  249. }
  250. /*
  251. * This corresponds to RFC3280 6.1.3(d)(2): Create new data for any unmatched
  252. * policies in the parent and link to anyPolicy.
  253. *
  254. * Return value: 1 on success, 0 otherwise.
  255. */
  256. static int tree_add_unmatched(X509_POLICY_LEVEL *curr,
  257. const X509_POLICY_CACHE *cache,
  258. const ASN1_OBJECT *id,
  259. X509_POLICY_NODE *node, X509_POLICY_TREE *tree)
  260. {
  261. X509_POLICY_DATA *data;
  262. if (id == NULL)
  263. id = node->data->valid_policy;
  264. /*
  265. * Create a new node with qualifiers from anyPolicy and id from unmatched
  266. * node.
  267. */
  268. if ((data = policy_data_new(NULL, id, node_critical(node))) == NULL)
  269. return 0;
  270. /* Curr may not have anyPolicy */
  271. data->qualifier_set = cache->anyPolicy->qualifier_set;
  272. data->flags |= POLICY_DATA_FLAG_SHARED_QUALIFIERS;
  273. if (level_add_node(curr, data, node, tree) == NULL) {
  274. policy_data_free(data);
  275. return 0;
  276. }
  277. return 1;
  278. }
  279. /*
  280. * Return value: 1 on success, 0 otherwise.
  281. */
  282. static int tree_link_unmatched(X509_POLICY_LEVEL *curr,
  283. const X509_POLICY_CACHE *cache,
  284. X509_POLICY_NODE *node, X509_POLICY_TREE *tree)
  285. {
  286. const X509_POLICY_LEVEL *last = curr - 1;
  287. int i;
  288. if ((last->flags & X509_V_FLAG_INHIBIT_MAP)
  289. || !(node->data->flags & POLICY_DATA_FLAG_MAPPED)) {
  290. /* If no policy mapping: matched if one child present */
  291. if (node->nchild)
  292. return 1;
  293. if (!tree_add_unmatched(curr, cache, NULL, node, tree))
  294. return 0;
  295. /* Add it */
  296. } else {
  297. /* If mapping: matched if one child per expected policy set */
  298. STACK_OF(ASN1_OBJECT) *expset = node->data->expected_policy_set;
  299. if (node->nchild == sk_ASN1_OBJECT_num(expset))
  300. return 1;
  301. /* Locate unmatched nodes */
  302. for (i = 0; i < sk_ASN1_OBJECT_num(expset); i++) {
  303. ASN1_OBJECT *oid = sk_ASN1_OBJECT_value(expset, i);
  304. if (level_find_node(curr, node, oid))
  305. continue;
  306. if (!tree_add_unmatched(curr, cache, oid, node, tree))
  307. return 0;
  308. }
  309. }
  310. return 1;
  311. }
  312. /*
  313. * Return value: 1 on success, 0 otherwise
  314. */
  315. static int tree_link_any(X509_POLICY_LEVEL *curr,
  316. const X509_POLICY_CACHE *cache,
  317. X509_POLICY_TREE *tree)
  318. {
  319. int i;
  320. X509_POLICY_NODE *node;
  321. X509_POLICY_LEVEL *last = curr - 1;
  322. for (i = 0; i < sk_X509_POLICY_NODE_num(last->nodes); i++) {
  323. node = sk_X509_POLICY_NODE_value(last->nodes, i);
  324. if (!tree_link_unmatched(curr, cache, node, tree))
  325. return 0;
  326. }
  327. /* Finally add link to anyPolicy */
  328. if (last->anyPolicy &&
  329. level_add_node(curr, cache->anyPolicy, last->anyPolicy, NULL) == NULL)
  330. return 0;
  331. return 1;
  332. }
  333. /*-
  334. * Prune the tree: delete any child mapped child data on the current level then
  335. * proceed up the tree deleting any data with no children. If we ever have no
  336. * data on a level we can halt because the tree will be empty.
  337. *
  338. * Return value: <= 0 error, otherwise one of:
  339. *
  340. * X509_PCY_TREE_VALID: valid tree
  341. * X509_PCY_TREE_EMPTY: empty tree
  342. */
  343. static int tree_prune(X509_POLICY_TREE *tree, X509_POLICY_LEVEL *curr)
  344. {
  345. STACK_OF(X509_POLICY_NODE) *nodes;
  346. X509_POLICY_NODE *node;
  347. int i;
  348. nodes = curr->nodes;
  349. if (curr->flags & X509_V_FLAG_INHIBIT_MAP) {
  350. for (i = sk_X509_POLICY_NODE_num(nodes) - 1; i >= 0; i--) {
  351. node = sk_X509_POLICY_NODE_value(nodes, i);
  352. /* Delete any mapped data: see RFC3280 XXXX */
  353. if (node->data->flags & POLICY_DATA_FLAG_MAP_MASK) {
  354. node->parent->nchild--;
  355. OPENSSL_free(node);
  356. (void)sk_X509_POLICY_NODE_delete(nodes, i);
  357. }
  358. }
  359. }
  360. for (;;) {
  361. --curr;
  362. nodes = curr->nodes;
  363. for (i = sk_X509_POLICY_NODE_num(nodes) - 1; i >= 0; i--) {
  364. node = sk_X509_POLICY_NODE_value(nodes, i);
  365. if (node->nchild == 0) {
  366. node->parent->nchild--;
  367. OPENSSL_free(node);
  368. (void)sk_X509_POLICY_NODE_delete(nodes, i);
  369. }
  370. }
  371. if (curr->anyPolicy && !curr->anyPolicy->nchild) {
  372. if (curr->anyPolicy->parent)
  373. curr->anyPolicy->parent->nchild--;
  374. OPENSSL_free(curr->anyPolicy);
  375. curr->anyPolicy = NULL;
  376. }
  377. if (curr == tree->levels) {
  378. /* If we zapped anyPolicy at top then tree is empty */
  379. if (!curr->anyPolicy)
  380. return X509_PCY_TREE_EMPTY;
  381. break;
  382. }
  383. }
  384. return X509_PCY_TREE_VALID;
  385. }
  386. /*
  387. * Return value: 1 on success, 0 otherwise.
  388. */
  389. static int tree_add_auth_node(STACK_OF(X509_POLICY_NODE) **pnodes,
  390. X509_POLICY_NODE *pcy)
  391. {
  392. if (*pnodes == NULL &&
  393. (*pnodes = policy_node_cmp_new()) == NULL)
  394. return 0;
  395. if (sk_X509_POLICY_NODE_find(*pnodes, pcy) >= 0)
  396. return 1;
  397. return sk_X509_POLICY_NODE_push(*pnodes, pcy) != 0;
  398. }
  399. #define TREE_CALC_FAILURE 0
  400. #define TREE_CALC_OK_NOFREE 1
  401. #define TREE_CALC_OK_DOFREE 2
  402. /*-
  403. * Calculate the authority set based on policy tree. The 'pnodes' parameter is
  404. * used as a store for the set of policy nodes used to calculate the user set.
  405. * If the authority set is not anyPolicy then pnodes will just point to the
  406. * authority set. If however the authority set is anyPolicy then the set of
  407. * valid policies (other than anyPolicy) is store in pnodes.
  408. *
  409. * Return value:
  410. * TREE_CALC_FAILURE on failure,
  411. * TREE_CALC_OK_NOFREE on success and pnodes need not be freed,
  412. * TREE_CALC_OK_DOFREE on success and pnodes needs to be freed
  413. */
  414. static int tree_calculate_authority_set(X509_POLICY_TREE *tree,
  415. STACK_OF(X509_POLICY_NODE) **pnodes)
  416. {
  417. X509_POLICY_LEVEL *curr;
  418. X509_POLICY_NODE *node, *anyptr;
  419. STACK_OF(X509_POLICY_NODE) **addnodes;
  420. int i, j;
  421. curr = tree->levels + tree->nlevel - 1;
  422. /* If last level contains anyPolicy set is anyPolicy */
  423. if (curr->anyPolicy) {
  424. if (!tree_add_auth_node(&tree->auth_policies, curr->anyPolicy))
  425. return TREE_CALC_FAILURE;
  426. addnodes = pnodes;
  427. } else
  428. /* Add policies to authority set */
  429. addnodes = &tree->auth_policies;
  430. curr = tree->levels;
  431. for (i = 1; i < tree->nlevel; i++) {
  432. /*
  433. * If no anyPolicy node on this this level it can't appear on lower
  434. * levels so end search.
  435. */
  436. if ((anyptr = curr->anyPolicy) == NULL)
  437. break;
  438. curr++;
  439. for (j = 0; j < sk_X509_POLICY_NODE_num(curr->nodes); j++) {
  440. node = sk_X509_POLICY_NODE_value(curr->nodes, j);
  441. if ((node->parent == anyptr)
  442. && !tree_add_auth_node(addnodes, node)) {
  443. if (addnodes == pnodes) {
  444. sk_X509_POLICY_NODE_free(*pnodes);
  445. *pnodes = NULL;
  446. }
  447. return TREE_CALC_FAILURE;
  448. }
  449. }
  450. }
  451. if (addnodes == pnodes)
  452. return TREE_CALC_OK_DOFREE;
  453. *pnodes = tree->auth_policies;
  454. return TREE_CALC_OK_NOFREE;
  455. }
  456. /*
  457. * Return value: 1 on success, 0 otherwise.
  458. */
  459. static int tree_calculate_user_set(X509_POLICY_TREE *tree,
  460. STACK_OF(ASN1_OBJECT) *policy_oids,
  461. STACK_OF(X509_POLICY_NODE) *auth_nodes)
  462. {
  463. int i;
  464. X509_POLICY_NODE *node;
  465. ASN1_OBJECT *oid;
  466. X509_POLICY_NODE *anyPolicy;
  467. X509_POLICY_DATA *extra;
  468. /*
  469. * Check if anyPolicy present in authority constrained policy set: this
  470. * will happen if it is a leaf node.
  471. */
  472. if (sk_ASN1_OBJECT_num(policy_oids) <= 0)
  473. return 1;
  474. anyPolicy = tree->levels[tree->nlevel - 1].anyPolicy;
  475. for (i = 0; i < sk_ASN1_OBJECT_num(policy_oids); i++) {
  476. oid = sk_ASN1_OBJECT_value(policy_oids, i);
  477. if (OBJ_obj2nid(oid) == NID_any_policy) {
  478. tree->flags |= POLICY_FLAG_ANY_POLICY;
  479. return 1;
  480. }
  481. }
  482. for (i = 0; i < sk_ASN1_OBJECT_num(policy_oids); i++) {
  483. oid = sk_ASN1_OBJECT_value(policy_oids, i);
  484. node = tree_find_sk(auth_nodes, oid);
  485. if (!node) {
  486. if (!anyPolicy)
  487. continue;
  488. /*
  489. * Create a new node with policy ID from user set and qualifiers
  490. * from anyPolicy.
  491. */
  492. extra = policy_data_new(NULL, oid, node_critical(anyPolicy));
  493. if (extra == NULL)
  494. return 0;
  495. extra->qualifier_set = anyPolicy->data->qualifier_set;
  496. extra->flags = POLICY_DATA_FLAG_SHARED_QUALIFIERS
  497. | POLICY_DATA_FLAG_EXTRA_NODE;
  498. node = level_add_node(NULL, extra, anyPolicy->parent, tree);
  499. }
  500. if (!tree->user_policies) {
  501. tree->user_policies = sk_X509_POLICY_NODE_new_null();
  502. if (!tree->user_policies)
  503. return 1;
  504. }
  505. if (!sk_X509_POLICY_NODE_push(tree->user_policies, node))
  506. return 0;
  507. }
  508. return 1;
  509. }
  510. /*-
  511. * Return value: <= 0 error, otherwise one of:
  512. * X509_PCY_TREE_VALID: valid tree
  513. * X509_PCY_TREE_EMPTY: empty tree
  514. * (see tree_prune()).
  515. */
  516. static int tree_evaluate(X509_POLICY_TREE *tree)
  517. {
  518. int ret, i;
  519. X509_POLICY_LEVEL *curr = tree->levels + 1;
  520. const X509_POLICY_CACHE *cache;
  521. for (i = 1; i < tree->nlevel; i++, curr++) {
  522. cache = policy_cache_set(curr->cert);
  523. if (!tree_link_nodes(curr, cache))
  524. return X509_PCY_TREE_INTERNAL;
  525. if (!(curr->flags & X509_V_FLAG_INHIBIT_ANY)
  526. && !tree_link_any(curr, cache, tree))
  527. return X509_PCY_TREE_INTERNAL;
  528. #ifdef OPENSSL_POLICY_DEBUG
  529. tree_print("before tree_prune()", tree, curr);
  530. #endif
  531. ret = tree_prune(tree, curr);
  532. if (ret != X509_PCY_TREE_VALID)
  533. return ret;
  534. }
  535. return X509_PCY_TREE_VALID;
  536. }
  537. static void exnode_free(X509_POLICY_NODE *node)
  538. {
  539. if (node->data && (node->data->flags & POLICY_DATA_FLAG_EXTRA_NODE))
  540. OPENSSL_free(node);
  541. }
  542. void X509_policy_tree_free(X509_POLICY_TREE *tree)
  543. {
  544. X509_POLICY_LEVEL *curr;
  545. int i;
  546. if (!tree)
  547. return;
  548. sk_X509_POLICY_NODE_free(tree->auth_policies);
  549. sk_X509_POLICY_NODE_pop_free(tree->user_policies, exnode_free);
  550. for (i = 0, curr = tree->levels; i < tree->nlevel; i++, curr++) {
  551. X509_free(curr->cert);
  552. sk_X509_POLICY_NODE_pop_free(curr->nodes, policy_node_free);
  553. policy_node_free(curr->anyPolicy);
  554. }
  555. sk_X509_POLICY_DATA_pop_free(tree->extra_data, policy_data_free);
  556. OPENSSL_free(tree->levels);
  557. OPENSSL_free(tree);
  558. }
  559. /*-
  560. * Application policy checking function.
  561. * Return codes:
  562. * X509_PCY_TREE_FAILURE: Failure to satisfy explicit policy
  563. * X509_PCY_TREE_INVALID: Inconsistent or invalid extensions
  564. * X509_PCY_TREE_INTERNAL: Internal error, most likely malloc
  565. * X509_PCY_TREE_VALID: Success (null tree if empty or bare TA)
  566. */
  567. int X509_policy_check(X509_POLICY_TREE **ptree, int *pexplicit_policy,
  568. STACK_OF(X509) *certs,
  569. STACK_OF(ASN1_OBJECT) *policy_oids, unsigned int flags)
  570. {
  571. int init_ret;
  572. int ret;
  573. int calc_ret;
  574. X509_POLICY_TREE *tree = NULL;
  575. STACK_OF(X509_POLICY_NODE) *nodes, *auth_nodes = NULL;
  576. *ptree = NULL;
  577. *pexplicit_policy = 0;
  578. init_ret = tree_init(&tree, certs, flags);
  579. if (init_ret <= 0)
  580. return init_ret;
  581. if ((init_ret & X509_PCY_TREE_EXPLICIT) == 0) {
  582. if (init_ret & X509_PCY_TREE_EMPTY) {
  583. X509_policy_tree_free(tree);
  584. return X509_PCY_TREE_VALID;
  585. }
  586. } else {
  587. *pexplicit_policy = 1;
  588. /* Tree empty and requireExplicit True: Error */
  589. if (init_ret & X509_PCY_TREE_EMPTY)
  590. return X509_PCY_TREE_FAILURE;
  591. }
  592. ret = tree_evaluate(tree);
  593. #ifdef OPENSSL_POLICY_DEBUG
  594. tree_print("tree_evaluate()", tree, NULL);
  595. #endif
  596. if (ret <= 0)
  597. goto error;
  598. if (ret == X509_PCY_TREE_EMPTY) {
  599. X509_policy_tree_free(tree);
  600. if (init_ret & X509_PCY_TREE_EXPLICIT)
  601. return X509_PCY_TREE_FAILURE;
  602. return X509_PCY_TREE_VALID;
  603. }
  604. /* Tree is not empty: continue */
  605. if ((calc_ret = tree_calculate_authority_set(tree, &auth_nodes)) == 0)
  606. goto error;
  607. ret = tree_calculate_user_set(tree, policy_oids, auth_nodes);
  608. if (calc_ret == TREE_CALC_OK_DOFREE)
  609. sk_X509_POLICY_NODE_free(auth_nodes);
  610. if (!ret)
  611. goto error;
  612. *ptree = tree;
  613. if (init_ret & X509_PCY_TREE_EXPLICIT) {
  614. nodes = X509_policy_tree_get0_user_policies(tree);
  615. if (sk_X509_POLICY_NODE_num(nodes) <= 0)
  616. return X509_PCY_TREE_FAILURE;
  617. }
  618. return X509_PCY_TREE_VALID;
  619. error:
  620. X509_policy_tree_free(tree);
  621. return X509_PCY_TREE_INTERNAL;
  622. }