Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

830 wiersze
24 KiB

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