pcy_tree.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  1. /*
  2. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
  3. * 2004.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 2004 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * licensing@OpenSSL.org.
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. *
  53. * This product includes cryptographic software written by Eric Young
  54. * (eay@cryptsoft.com). This product includes software written by Tim
  55. * Hudson (tjh@cryptsoft.com).
  56. *
  57. */
  58. #include <string.h>
  59. #include <openssl/mem.h>
  60. #include <openssl/obj.h>
  61. #include <openssl/stack.h>
  62. #include <openssl/thread.h>
  63. #include <openssl/x509.h>
  64. #include <openssl/x509v3.h>
  65. #include "pcy_int.h"
  66. #include "../internal.h"
  67. /*
  68. * Enable this to print out the complete policy tree at various point during
  69. * evaluation.
  70. */
  71. /*
  72. * #define OPENSSL_POLICY_DEBUG
  73. */
  74. #ifdef OPENSSL_POLICY_DEBUG
  75. static void expected_print(BIO *err, X509_POLICY_LEVEL *lev,
  76. X509_POLICY_NODE *node, int indent)
  77. {
  78. if ((lev->flags & X509_V_FLAG_INHIBIT_MAP)
  79. || !(node->data->flags & POLICY_DATA_FLAG_MAP_MASK))
  80. BIO_puts(err, " Not Mapped\n");
  81. else {
  82. int i;
  83. STACK_OF(ASN1_OBJECT) *pset = node->data->expected_policy_set;
  84. ASN1_OBJECT *oid;
  85. BIO_puts(err, " Expected: ");
  86. for (i = 0; i < sk_ASN1_OBJECT_num(pset); i++) {
  87. oid = sk_ASN1_OBJECT_value(pset, i);
  88. if (i)
  89. BIO_puts(err, ", ");
  90. i2a_ASN1_OBJECT(err, oid);
  91. }
  92. BIO_puts(err, "\n");
  93. }
  94. }
  95. static void tree_print(char *str, X509_POLICY_TREE *tree,
  96. X509_POLICY_LEVEL *curr)
  97. {
  98. X509_POLICY_LEVEL *plev;
  99. X509_POLICY_NODE *node;
  100. int i;
  101. BIO *err;
  102. err = BIO_new_fp(stderr, BIO_NOCLOSE);
  103. if (!curr)
  104. curr = tree->levels + tree->nlevel;
  105. else
  106. curr++;
  107. BIO_printf(err, "Level print after %s\n", str);
  108. BIO_printf(err, "Printing Up to Level %ld\n", curr - tree->levels);
  109. for (plev = tree->levels; plev != curr; plev++) {
  110. BIO_printf(err, "Level %ld, flags = %x\n",
  111. plev - tree->levels, plev->flags);
  112. for (i = 0; i < sk_X509_POLICY_NODE_num(plev->nodes); i++) {
  113. node = sk_X509_POLICY_NODE_value(plev->nodes, i);
  114. X509_POLICY_NODE_print(err, node, 2);
  115. expected_print(err, plev, node, 2);
  116. BIO_printf(err, " Flags: %x\n", node->data->flags);
  117. }
  118. if (plev->anyPolicy)
  119. X509_POLICY_NODE_print(err, plev->anyPolicy, 2);
  120. }
  121. BIO_free(err);
  122. }
  123. #else
  124. # define tree_print(a,b,c) /* */
  125. #endif
  126. /*
  127. * Initialize policy tree. Return values: 0 Some internal error occured. -1
  128. * Inconsistent or invalid extensions in certificates. 1 Tree initialized
  129. * OK. 2 Policy tree is empty. 5 Tree OK and requireExplicitPolicy true. 6
  130. * Tree empty and requireExplicitPolicy true.
  131. */
  132. static int tree_init(X509_POLICY_TREE **ptree, STACK_OF(X509) *certs,
  133. unsigned int flags)
  134. {
  135. X509_POLICY_TREE *tree;
  136. X509_POLICY_LEVEL *level;
  137. const X509_POLICY_CACHE *cache;
  138. X509_POLICY_DATA *data = NULL;
  139. X509 *x;
  140. int ret = 1;
  141. int i, n;
  142. int explicit_policy;
  143. int any_skip;
  144. int map_skip;
  145. *ptree = NULL;
  146. n = sk_X509_num(certs);
  147. #if 0
  148. /* Disable policy mapping for now... */
  149. flags |= X509_V_FLAG_INHIBIT_MAP;
  150. #endif
  151. if (flags & X509_V_FLAG_EXPLICIT_POLICY)
  152. explicit_policy = 0;
  153. else
  154. explicit_policy = n + 1;
  155. if (flags & X509_V_FLAG_INHIBIT_ANY)
  156. any_skip = 0;
  157. else
  158. any_skip = n + 1;
  159. if (flags & X509_V_FLAG_INHIBIT_MAP)
  160. map_skip = 0;
  161. else
  162. map_skip = n + 1;
  163. /* Can't do anything with just a trust anchor */
  164. if (n == 1)
  165. return 1;
  166. /*
  167. * First setup policy cache in all certificates apart from the trust
  168. * anchor. Note any bad cache results on the way. Also can calculate
  169. * explicit_policy value at this point.
  170. */
  171. for (i = n - 2; i >= 0; i--) {
  172. x = sk_X509_value(certs, i);
  173. X509_check_purpose(x, -1, -1);
  174. cache = policy_cache_set(x);
  175. /* If cache NULL something bad happened: return immediately */
  176. if (cache == NULL)
  177. return 0;
  178. /*
  179. * If inconsistent extensions keep a note of it but continue
  180. */
  181. if (x->ex_flags & EXFLAG_INVALID_POLICY)
  182. ret = -1;
  183. /*
  184. * Otherwise if we have no data (hence no CertificatePolicies) and
  185. * haven't already set an inconsistent code note it.
  186. */
  187. else if ((ret == 1) && !cache->data)
  188. ret = 2;
  189. if (explicit_policy > 0) {
  190. if (!(x->ex_flags & EXFLAG_SI))
  191. explicit_policy--;
  192. if ((cache->explicit_skip != -1)
  193. && (cache->explicit_skip < explicit_policy))
  194. explicit_policy = cache->explicit_skip;
  195. }
  196. }
  197. if (ret != 1) {
  198. if (ret == 2 && !explicit_policy)
  199. return 6;
  200. return ret;
  201. }
  202. /* If we get this far initialize the tree */
  203. tree = OPENSSL_malloc(sizeof(X509_POLICY_TREE));
  204. if (!tree)
  205. return 0;
  206. tree->flags = 0;
  207. tree->levels = OPENSSL_malloc(sizeof(X509_POLICY_LEVEL) * n);
  208. tree->nlevel = 0;
  209. tree->extra_data = NULL;
  210. tree->auth_policies = NULL;
  211. tree->user_policies = NULL;
  212. if (!tree->levels) {
  213. OPENSSL_free(tree);
  214. return 0;
  215. }
  216. OPENSSL_memset(tree->levels, 0, n * sizeof(X509_POLICY_LEVEL));
  217. tree->nlevel = n;
  218. level = tree->levels;
  219. /* Root data: initialize to anyPolicy */
  220. data = policy_data_new(NULL, OBJ_nid2obj(NID_any_policy), 0);
  221. if (!data || !level_add_node(level, data, NULL, tree))
  222. goto bad_tree;
  223. for (i = n - 2; i >= 0; i--) {
  224. level++;
  225. x = sk_X509_value(certs, i);
  226. cache = policy_cache_set(x);
  227. X509_up_ref(x);
  228. level->cert = x;
  229. if (!cache->anyPolicy)
  230. level->flags |= X509_V_FLAG_INHIBIT_ANY;
  231. /* Determine inhibit any and inhibit map flags */
  232. if (any_skip == 0) {
  233. /*
  234. * Any matching allowed if certificate is self issued and not the
  235. * last in the chain.
  236. */
  237. if (!(x->ex_flags & EXFLAG_SI) || (i == 0))
  238. level->flags |= X509_V_FLAG_INHIBIT_ANY;
  239. } else {
  240. if (!(x->ex_flags & EXFLAG_SI))
  241. any_skip--;
  242. if ((cache->any_skip >= 0)
  243. && (cache->any_skip < any_skip))
  244. any_skip = cache->any_skip;
  245. }
  246. if (map_skip == 0)
  247. level->flags |= X509_V_FLAG_INHIBIT_MAP;
  248. else {
  249. if (!(x->ex_flags & EXFLAG_SI))
  250. map_skip--;
  251. if ((cache->map_skip >= 0)
  252. && (cache->map_skip < map_skip))
  253. map_skip = cache->map_skip;
  254. }
  255. }
  256. *ptree = tree;
  257. if (explicit_policy)
  258. return 1;
  259. else
  260. return 5;
  261. bad_tree:
  262. X509_policy_tree_free(tree);
  263. return 0;
  264. }
  265. static int tree_link_matching_nodes(X509_POLICY_LEVEL *curr,
  266. const X509_POLICY_DATA *data)
  267. {
  268. X509_POLICY_LEVEL *last = curr - 1;
  269. X509_POLICY_NODE *node;
  270. int matched = 0;
  271. size_t i;
  272. /* Iterate through all in nodes linking matches */
  273. for (i = 0; i < sk_X509_POLICY_NODE_num(last->nodes); i++) {
  274. node = sk_X509_POLICY_NODE_value(last->nodes, i);
  275. if (policy_node_match(last, node, data->valid_policy)) {
  276. if (!level_add_node(curr, data, node, NULL))
  277. return 0;
  278. matched = 1;
  279. }
  280. }
  281. if (!matched && last->anyPolicy) {
  282. if (!level_add_node(curr, data, last->anyPolicy, NULL))
  283. return 0;
  284. }
  285. return 1;
  286. }
  287. /*
  288. * This corresponds to RFC3280 6.1.3(d)(1): link any data from
  289. * CertificatePolicies onto matching parent or anyPolicy if no match.
  290. */
  291. static int tree_link_nodes(X509_POLICY_LEVEL *curr,
  292. const X509_POLICY_CACHE *cache)
  293. {
  294. size_t i;
  295. X509_POLICY_DATA *data;
  296. for (i = 0; i < sk_X509_POLICY_DATA_num(cache->data); i++) {
  297. data = sk_X509_POLICY_DATA_value(cache->data, i);
  298. /*
  299. * If a node is mapped any it doesn't have a corresponding
  300. * CertificatePolicies entry. However such an identical node would
  301. * be created if anyPolicy matching is enabled because there would be
  302. * no match with the parent valid_policy_set. So we create link
  303. * because then it will have the mapping flags right and we can prune
  304. * it later.
  305. */
  306. #if 0
  307. if ((data->flags & POLICY_DATA_FLAG_MAPPED_ANY)
  308. && !(curr->flags & X509_V_FLAG_INHIBIT_ANY))
  309. continue;
  310. #endif
  311. /* Look for matching nodes in previous level */
  312. if (!tree_link_matching_nodes(curr, data))
  313. return 0;
  314. }
  315. return 1;
  316. }
  317. /*
  318. * This corresponds to RFC3280 6.1.3(d)(2): Create new data for any unmatched
  319. * policies in the parent and link to anyPolicy.
  320. */
  321. static int tree_add_unmatched(X509_POLICY_LEVEL *curr,
  322. const X509_POLICY_CACHE *cache,
  323. const ASN1_OBJECT *id,
  324. X509_POLICY_NODE *node, X509_POLICY_TREE *tree)
  325. {
  326. X509_POLICY_DATA *data;
  327. if (id == NULL)
  328. id = node->data->valid_policy;
  329. /*
  330. * Create a new node with qualifiers from anyPolicy and id from unmatched
  331. * node.
  332. */
  333. data = policy_data_new(NULL, id, node_critical(node));
  334. if (data == NULL)
  335. return 0;
  336. /* Curr may not have anyPolicy */
  337. data->qualifier_set = cache->anyPolicy->qualifier_set;
  338. data->flags |= POLICY_DATA_FLAG_SHARED_QUALIFIERS;
  339. if (!level_add_node(curr, data, node, tree)) {
  340. policy_data_free(data);
  341. return 0;
  342. }
  343. return 1;
  344. }
  345. static int tree_link_unmatched(X509_POLICY_LEVEL *curr,
  346. const X509_POLICY_CACHE *cache,
  347. X509_POLICY_NODE *node, X509_POLICY_TREE *tree)
  348. {
  349. const X509_POLICY_LEVEL *last = curr - 1;
  350. size_t i;
  351. if ((last->flags & X509_V_FLAG_INHIBIT_MAP)
  352. || !(node->data->flags & POLICY_DATA_FLAG_MAPPED)) {
  353. /* If no policy mapping: matched if one child present */
  354. if (node->nchild)
  355. return 1;
  356. if (!tree_add_unmatched(curr, cache, NULL, node, tree))
  357. return 0;
  358. /* Add it */
  359. } else {
  360. /* If mapping: matched if one child per expected policy set */
  361. STACK_OF(ASN1_OBJECT) *expset = node->data->expected_policy_set;
  362. if ((size_t)node->nchild == sk_ASN1_OBJECT_num(expset))
  363. return 1;
  364. /* Locate unmatched nodes */
  365. for (i = 0; i < sk_ASN1_OBJECT_num(expset); i++) {
  366. ASN1_OBJECT *oid = sk_ASN1_OBJECT_value(expset, i);
  367. if (level_find_node(curr, node, oid))
  368. continue;
  369. if (!tree_add_unmatched(curr, cache, oid, node, tree))
  370. return 0;
  371. }
  372. }
  373. return 1;
  374. }
  375. static int tree_link_any(X509_POLICY_LEVEL *curr,
  376. const X509_POLICY_CACHE *cache,
  377. X509_POLICY_TREE *tree)
  378. {
  379. size_t i;
  380. /*
  381. * X509_POLICY_DATA *data;
  382. */
  383. X509_POLICY_NODE *node;
  384. X509_POLICY_LEVEL *last = curr - 1;
  385. for (i = 0; i < sk_X509_POLICY_NODE_num(last->nodes); i++) {
  386. node = sk_X509_POLICY_NODE_value(last->nodes, i);
  387. if (!tree_link_unmatched(curr, cache, node, tree))
  388. return 0;
  389. #if 0
  390. /*
  391. * Skip any node with any children: we only want unmathced nodes.
  392. * Note: need something better for policy mapping because each node
  393. * may have multiple children
  394. */
  395. if (node->nchild)
  396. continue;
  397. /*
  398. * Create a new node with qualifiers from anyPolicy and id from
  399. * unmatched node.
  400. */
  401. data = policy_data_new(NULL, node->data->valid_policy,
  402. node_critical(node));
  403. if (data == NULL)
  404. return 0;
  405. /* Curr may not have anyPolicy */
  406. data->qualifier_set = cache->anyPolicy->qualifier_set;
  407. data->flags |= POLICY_DATA_FLAG_SHARED_QUALIFIERS;
  408. if (!level_add_node(curr, data, node, tree)) {
  409. policy_data_free(data);
  410. return 0;
  411. }
  412. #endif
  413. }
  414. /* Finally add link to anyPolicy */
  415. if (last->anyPolicy) {
  416. if (!level_add_node(curr, cache->anyPolicy, last->anyPolicy, NULL))
  417. return 0;
  418. }
  419. return 1;
  420. }
  421. /*
  422. * Prune the tree: delete any child mapped child data on the current level
  423. * then proceed up the tree deleting any data with no children. If we ever
  424. * have no data on a level we can halt because the tree will be empty.
  425. */
  426. static int tree_prune(X509_POLICY_TREE *tree, X509_POLICY_LEVEL *curr)
  427. {
  428. STACK_OF(X509_POLICY_NODE) *nodes;
  429. X509_POLICY_NODE *node;
  430. int i;
  431. nodes = curr->nodes;
  432. if (curr->flags & X509_V_FLAG_INHIBIT_MAP) {
  433. for (i = sk_X509_POLICY_NODE_num(nodes) - 1; i >= 0; i--) {
  434. node = sk_X509_POLICY_NODE_value(nodes, i);
  435. /* Delete any mapped data: see RFC3280 XXXX */
  436. if (node->data->flags & POLICY_DATA_FLAG_MAP_MASK) {
  437. node->parent->nchild--;
  438. OPENSSL_free(node);
  439. (void)sk_X509_POLICY_NODE_delete(nodes, i);
  440. }
  441. }
  442. }
  443. for (;;) {
  444. --curr;
  445. nodes = curr->nodes;
  446. for (i = sk_X509_POLICY_NODE_num(nodes) - 1; i >= 0; i--) {
  447. node = sk_X509_POLICY_NODE_value(nodes, i);
  448. if (node->nchild == 0) {
  449. node->parent->nchild--;
  450. OPENSSL_free(node);
  451. (void)sk_X509_POLICY_NODE_delete(nodes, i);
  452. }
  453. }
  454. if (curr->anyPolicy && !curr->anyPolicy->nchild) {
  455. if (curr->anyPolicy->parent)
  456. curr->anyPolicy->parent->nchild--;
  457. OPENSSL_free(curr->anyPolicy);
  458. curr->anyPolicy = NULL;
  459. }
  460. if (curr == tree->levels) {
  461. /* If we zapped anyPolicy at top then tree is empty */
  462. if (!curr->anyPolicy)
  463. return 2;
  464. return 1;
  465. }
  466. }
  467. }
  468. static int tree_add_auth_node(STACK_OF(X509_POLICY_NODE) **pnodes,
  469. X509_POLICY_NODE *pcy)
  470. {
  471. if (!*pnodes) {
  472. *pnodes = policy_node_cmp_new();
  473. if (!*pnodes)
  474. return 0;
  475. } else if (sk_X509_POLICY_NODE_find(*pnodes, NULL, pcy))
  476. return 1;
  477. if (!sk_X509_POLICY_NODE_push(*pnodes, pcy))
  478. return 0;
  479. return 1;
  480. }
  481. /*
  482. * Calculate the authority set based on policy tree. The 'pnodes' parameter
  483. * is used as a store for the set of policy nodes used to calculate the user
  484. * set. If the authority set is not anyPolicy then pnodes will just point to
  485. * the authority set. If however the authority set is anyPolicy then the set
  486. * of valid policies (other than anyPolicy) is store in pnodes. The return
  487. * value of '2' is used in this case to indicate that pnodes should be freed.
  488. */
  489. static int tree_calculate_authority_set(X509_POLICY_TREE *tree,
  490. STACK_OF(X509_POLICY_NODE) **pnodes)
  491. {
  492. X509_POLICY_LEVEL *curr;
  493. X509_POLICY_NODE *node, *anyptr;
  494. STACK_OF(X509_POLICY_NODE) **addnodes;
  495. int i;
  496. size_t j;
  497. curr = tree->levels + tree->nlevel - 1;
  498. /* If last level contains anyPolicy set is anyPolicy */
  499. if (curr->anyPolicy) {
  500. if (!tree_add_auth_node(&tree->auth_policies, curr->anyPolicy))
  501. return 0;
  502. addnodes = pnodes;
  503. } else
  504. /* Add policies to authority set */
  505. addnodes = &tree->auth_policies;
  506. curr = tree->levels;
  507. for (i = 1; i < tree->nlevel; i++) {
  508. /*
  509. * If no anyPolicy node on this this level it can't appear on lower
  510. * levels so end search.
  511. */
  512. if (!(anyptr = curr->anyPolicy))
  513. break;
  514. curr++;
  515. for (j = 0; j < sk_X509_POLICY_NODE_num(curr->nodes); j++) {
  516. node = sk_X509_POLICY_NODE_value(curr->nodes, j);
  517. if ((node->parent == anyptr)
  518. && !tree_add_auth_node(addnodes, node))
  519. return 0;
  520. }
  521. }
  522. if (addnodes == pnodes)
  523. return 2;
  524. *pnodes = tree->auth_policies;
  525. return 1;
  526. }
  527. static int tree_calculate_user_set(X509_POLICY_TREE *tree,
  528. STACK_OF(ASN1_OBJECT) *policy_oids,
  529. STACK_OF(X509_POLICY_NODE) *auth_nodes)
  530. {
  531. size_t i;
  532. X509_POLICY_NODE *node;
  533. ASN1_OBJECT *oid;
  534. X509_POLICY_NODE *anyPolicy;
  535. X509_POLICY_DATA *extra;
  536. /*
  537. * Check if anyPolicy present in authority constrained policy set: this
  538. * will happen if it is a leaf node.
  539. */
  540. if (sk_ASN1_OBJECT_num(policy_oids) <= 0)
  541. return 1;
  542. anyPolicy = tree->levels[tree->nlevel - 1].anyPolicy;
  543. for (i = 0; i < sk_ASN1_OBJECT_num(policy_oids); i++) {
  544. oid = sk_ASN1_OBJECT_value(policy_oids, i);
  545. if (OBJ_obj2nid(oid) == NID_any_policy) {
  546. tree->flags |= POLICY_FLAG_ANY_POLICY;
  547. return 1;
  548. }
  549. }
  550. for (i = 0; i < sk_ASN1_OBJECT_num(policy_oids); i++) {
  551. oid = sk_ASN1_OBJECT_value(policy_oids, i);
  552. node = tree_find_sk(auth_nodes, oid);
  553. if (!node) {
  554. if (!anyPolicy)
  555. continue;
  556. /*
  557. * Create a new node with policy ID from user set and qualifiers
  558. * from anyPolicy.
  559. */
  560. extra = policy_data_new(NULL, oid, node_critical(anyPolicy));
  561. if (!extra)
  562. return 0;
  563. extra->qualifier_set = anyPolicy->data->qualifier_set;
  564. extra->flags = POLICY_DATA_FLAG_SHARED_QUALIFIERS
  565. | POLICY_DATA_FLAG_EXTRA_NODE;
  566. node = level_add_node(NULL, extra, anyPolicy->parent, tree);
  567. }
  568. if (!tree->user_policies) {
  569. tree->user_policies = sk_X509_POLICY_NODE_new_null();
  570. if (!tree->user_policies)
  571. return 1;
  572. }
  573. if (!sk_X509_POLICY_NODE_push(tree->user_policies, node))
  574. return 0;
  575. }
  576. return 1;
  577. }
  578. static int tree_evaluate(X509_POLICY_TREE *tree)
  579. {
  580. int ret, i;
  581. X509_POLICY_LEVEL *curr = tree->levels + 1;
  582. const X509_POLICY_CACHE *cache;
  583. for (i = 1; i < tree->nlevel; i++, curr++) {
  584. cache = policy_cache_set(curr->cert);
  585. if (!tree_link_nodes(curr, cache))
  586. return 0;
  587. if (!(curr->flags & X509_V_FLAG_INHIBIT_ANY)
  588. && !tree_link_any(curr, cache, tree))
  589. return 0;
  590. tree_print("before tree_prune()", tree, curr);
  591. ret = tree_prune(tree, curr);
  592. if (ret != 1)
  593. return ret;
  594. }
  595. return 1;
  596. }
  597. static void exnode_free(X509_POLICY_NODE *node)
  598. {
  599. if (node->data && (node->data->flags & POLICY_DATA_FLAG_EXTRA_NODE))
  600. OPENSSL_free(node);
  601. }
  602. void X509_policy_tree_free(X509_POLICY_TREE *tree)
  603. {
  604. X509_POLICY_LEVEL *curr;
  605. int i;
  606. if (!tree)
  607. return;
  608. sk_X509_POLICY_NODE_free(tree->auth_policies);
  609. sk_X509_POLICY_NODE_pop_free(tree->user_policies, exnode_free);
  610. for (i = 0, curr = tree->levels; i < tree->nlevel; i++, curr++) {
  611. if (curr->cert)
  612. X509_free(curr->cert);
  613. if (curr->nodes)
  614. sk_X509_POLICY_NODE_pop_free(curr->nodes, policy_node_free);
  615. if (curr->anyPolicy)
  616. policy_node_free(curr->anyPolicy);
  617. }
  618. if (tree->extra_data)
  619. sk_X509_POLICY_DATA_pop_free(tree->extra_data, policy_data_free);
  620. OPENSSL_free(tree->levels);
  621. OPENSSL_free(tree);
  622. }
  623. /*
  624. * Application policy checking function. Return codes: 0 Internal Error. 1
  625. * Successful. -1 One or more certificates contain invalid or inconsistent
  626. * extensions -2 User constrained policy set empty and requireExplicit true.
  627. */
  628. int X509_policy_check(X509_POLICY_TREE **ptree, int *pexplicit_policy,
  629. STACK_OF(X509) *certs,
  630. STACK_OF(ASN1_OBJECT) *policy_oids, unsigned int flags)
  631. {
  632. int ret;
  633. X509_POLICY_TREE *tree = NULL;
  634. STACK_OF(X509_POLICY_NODE) *nodes, *auth_nodes = NULL;
  635. *ptree = NULL;
  636. *pexplicit_policy = 0;
  637. ret = tree_init(&tree, certs, flags);
  638. switch (ret) {
  639. /* Tree empty requireExplicit False: OK */
  640. case 2:
  641. return 1;
  642. /* Some internal error */
  643. case -1:
  644. return -1;
  645. /* Some internal error */
  646. case 0:
  647. return 0;
  648. /* Tree empty requireExplicit True: Error */
  649. case 6:
  650. *pexplicit_policy = 1;
  651. return -2;
  652. /* Tree OK requireExplicit True: OK and continue */
  653. case 5:
  654. *pexplicit_policy = 1;
  655. break;
  656. /* Tree OK: continue */
  657. case 1:
  658. if (!tree)
  659. /*
  660. * tree_init() returns success and a null tree
  661. * if it's just looking at a trust anchor.
  662. * I'm not sure that returning success here is
  663. * correct, but I'm sure that reporting this
  664. * as an internal error which our caller
  665. * interprets as a malloc failure is wrong.
  666. */
  667. return 1;
  668. break;
  669. }
  670. if (!tree)
  671. goto error;
  672. ret = tree_evaluate(tree);
  673. tree_print("tree_evaluate()", tree, NULL);
  674. if (ret <= 0)
  675. goto error;
  676. /* Return value 2 means tree empty */
  677. if (ret == 2) {
  678. X509_policy_tree_free(tree);
  679. if (*pexplicit_policy)
  680. return -2;
  681. else
  682. return 1;
  683. }
  684. /* Tree is not empty: continue */
  685. ret = tree_calculate_authority_set(tree, &auth_nodes);
  686. if (!ret)
  687. goto error;
  688. if (!tree_calculate_user_set(tree, policy_oids, auth_nodes))
  689. goto error;
  690. if (ret == 2)
  691. sk_X509_POLICY_NODE_free(auth_nodes);
  692. if (tree)
  693. *ptree = tree;
  694. if (*pexplicit_policy) {
  695. nodes = X509_policy_tree_get0_user_policies(tree);
  696. if (sk_X509_POLICY_NODE_num(nodes) <= 0)
  697. return -2;
  698. }
  699. return 1;
  700. error:
  701. X509_policy_tree_free(tree);
  702. return 0;
  703. }