您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

877 行
21 KiB

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