Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

878 linhas
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 <openssl/mem.h>
  58. #include <openssl/obj.h>
  59. #include <openssl/stack.h>
  60. #include <openssl/thread.h>
  61. #include <openssl/x509.h>
  62. #include <openssl/x509v3.h>
  63. #include "pcy_int.h"
  64. /* Enable this to print out the complete policy tree at various point during
  65. * evaluation.
  66. */
  67. /*#define OPENSSL_POLICY_DEBUG*/
  68. #ifdef OPENSSL_POLICY_DEBUG
  69. static void expected_print(BIO *err, X509_POLICY_LEVEL *lev,
  70. X509_POLICY_NODE *node, int indent)
  71. {
  72. if ( (lev->flags & X509_V_FLAG_INHIBIT_MAP)
  73. || !(node->data->flags & POLICY_DATA_FLAG_MAP_MASK))
  74. BIO_puts(err, " Not Mapped\n");
  75. else
  76. {
  77. int i;
  78. STACK_OF(ASN1_OBJECT) *pset = node->data->expected_policy_set;
  79. ASN1_OBJECT *oid;
  80. BIO_puts(err, " Expected: ");
  81. for (i = 0; i < sk_ASN1_OBJECT_num(pset); i++)
  82. {
  83. oid = sk_ASN1_OBJECT_value(pset, i);
  84. if (i)
  85. BIO_puts(err, ", ");
  86. i2a_ASN1_OBJECT(err, oid);
  87. }
  88. BIO_puts(err, "\n");
  89. }
  90. }
  91. static void tree_print(char *str, X509_POLICY_TREE *tree,
  92. X509_POLICY_LEVEL *curr)
  93. {
  94. X509_POLICY_LEVEL *plev;
  95. X509_POLICY_NODE *node;
  96. int i;
  97. BIO *err;
  98. err = BIO_new_fp(stderr, BIO_NOCLOSE);
  99. if (!curr)
  100. curr = tree->levels + tree->nlevel;
  101. else
  102. curr++;
  103. BIO_printf(err, "Level print after %s\n", str);
  104. BIO_printf(err, "Printing Up to Level %ld\n", curr - tree->levels);
  105. for (plev = tree->levels; plev != curr; plev++)
  106. {
  107. BIO_printf(err, "Level %ld, flags = %x\n",
  108. plev - tree->levels, plev->flags);
  109. for (i = 0; i < sk_X509_POLICY_NODE_num(plev->nodes); i++)
  110. {
  111. node = sk_X509_POLICY_NODE_value(plev->nodes, i);
  112. X509_POLICY_NODE_print(err, node, 2);
  113. expected_print(err, plev, node, 2);
  114. BIO_printf(err, " Flags: %x\n", node->data->flags);
  115. }
  116. if (plev->anyPolicy)
  117. X509_POLICY_NODE_print(err, plev->anyPolicy, 2);
  118. }
  119. BIO_free(err);
  120. }
  121. #else
  122. #define tree_print(a,b,c) /* */
  123. #endif
  124. /* Initialize policy tree. Return values:
  125. * 0 Some internal error occured.
  126. * -1 Inconsistent or invalid extensions in certificates.
  127. * 1 Tree initialized OK.
  128. * 2 Policy tree is empty.
  129. * 5 Tree OK and requireExplicitPolicy true.
  130. * 6 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. /* First setup policy cache in all certificates apart from the
  167. * trust anchor. Note any bad cache results on the way. Also can
  168. * calculate explicit_policy value at this point.
  169. */
  170. for (i = n - 2; i >= 0; i--)
  171. {
  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. /* If inconsistent extensions keep a note of it but continue */
  179. if (x->ex_flags & EXFLAG_INVALID_POLICY)
  180. ret = -1;
  181. /* Otherwise if we have no data (hence no CertificatePolicies)
  182. * and haven't already set an inconsistent code note it.
  183. */
  184. else if ((ret == 1) && !cache->data)
  185. ret = 2;
  186. if (explicit_policy > 0)
  187. {
  188. if (!(x->ex_flags & EXFLAG_SI))
  189. explicit_policy--;
  190. if ((cache->explicit_skip != -1)
  191. && (cache->explicit_skip < explicit_policy))
  192. explicit_policy = cache->explicit_skip;
  193. }
  194. }
  195. if (ret != 1)
  196. {
  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. {
  213. OPENSSL_free(tree);
  214. return 0;
  215. }
  216. 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. {
  225. level++;
  226. x = sk_X509_value(certs, i);
  227. cache = policy_cache_set(x);
  228. CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
  229. level->cert = 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. return 1;
  490. }
  491. static int tree_add_auth_node(STACK_OF(X509_POLICY_NODE) **pnodes,
  492. X509_POLICY_NODE *pcy)
  493. {
  494. if (!*pnodes)
  495. {
  496. *pnodes = policy_node_cmp_new();
  497. if (!*pnodes)
  498. return 0;
  499. }
  500. else if (sk_X509_POLICY_NODE_find(*pnodes, NULL, pcy))
  501. return 1;
  502. if (!sk_X509_POLICY_NODE_push(*pnodes, pcy))
  503. return 0;
  504. return 1;
  505. }
  506. /* Calculate the authority set based on policy tree.
  507. * The 'pnodes' parameter is used as a store for the set of policy nodes
  508. * used to calculate the user set. If the authority set is not anyPolicy
  509. * then pnodes will just point to the authority set. If however the authority
  510. * set is anyPolicy then the set of valid policies (other than anyPolicy)
  511. * is store in pnodes. The return value of '2' is used in this case to indicate
  512. * that pnodes should be freed.
  513. */
  514. static int tree_calculate_authority_set(X509_POLICY_TREE *tree,
  515. STACK_OF(X509_POLICY_NODE) **pnodes)
  516. {
  517. X509_POLICY_LEVEL *curr;
  518. X509_POLICY_NODE *node, *anyptr;
  519. STACK_OF(X509_POLICY_NODE) **addnodes;
  520. int i;
  521. size_t j;
  522. curr = tree->levels + tree->nlevel - 1;
  523. /* If last level contains anyPolicy set is anyPolicy */
  524. if (curr->anyPolicy)
  525. {
  526. if (!tree_add_auth_node(&tree->auth_policies, curr->anyPolicy))
  527. return 0;
  528. addnodes = pnodes;
  529. }
  530. else
  531. /* Add policies to authority set */
  532. addnodes = &tree->auth_policies;
  533. curr = tree->levels;
  534. for (i = 1; i < tree->nlevel; i++)
  535. {
  536. /* If no anyPolicy node on this this level it can't
  537. * appear on lower levels so end search.
  538. */
  539. if (!(anyptr = curr->anyPolicy))
  540. break;
  541. curr++;
  542. for (j = 0; j < sk_X509_POLICY_NODE_num(curr->nodes); j++)
  543. {
  544. node = sk_X509_POLICY_NODE_value(curr->nodes, j);
  545. if ((node->parent == anyptr)
  546. && !tree_add_auth_node(addnodes, node))
  547. return 0;
  548. }
  549. }
  550. if (addnodes == pnodes)
  551. return 2;
  552. *pnodes = tree->auth_policies;
  553. return 1;
  554. }
  555. static int tree_calculate_user_set(X509_POLICY_TREE *tree,
  556. STACK_OF(ASN1_OBJECT) *policy_oids,
  557. STACK_OF(X509_POLICY_NODE) *auth_nodes)
  558. {
  559. size_t i;
  560. X509_POLICY_NODE *node;
  561. ASN1_OBJECT *oid;
  562. X509_POLICY_NODE *anyPolicy;
  563. X509_POLICY_DATA *extra;
  564. /* Check if anyPolicy present in authority constrained policy set:
  565. * this will happen if it is a leaf node.
  566. */
  567. if (sk_ASN1_OBJECT_num(policy_oids) <= 0)
  568. return 1;
  569. anyPolicy = tree->levels[tree->nlevel - 1].anyPolicy;
  570. for (i = 0; i < sk_ASN1_OBJECT_num(policy_oids); i++)
  571. {
  572. oid = sk_ASN1_OBJECT_value(policy_oids, i);
  573. if (OBJ_obj2nid(oid) == NID_any_policy)
  574. {
  575. tree->flags |= POLICY_FLAG_ANY_POLICY;
  576. return 1;
  577. }
  578. }
  579. for (i = 0; i < sk_ASN1_OBJECT_num(policy_oids); i++)
  580. {
  581. oid = sk_ASN1_OBJECT_value(policy_oids, i);
  582. node = tree_find_sk(auth_nodes, oid);
  583. if (!node)
  584. {
  585. if (!anyPolicy)
  586. continue;
  587. /* Create a new node with policy ID from user set
  588. * and qualifiers from anyPolicy.
  589. */
  590. extra = policy_data_new(NULL, oid,
  591. node_critical(anyPolicy));
  592. if (!extra)
  593. return 0;
  594. extra->qualifier_set = anyPolicy->data->qualifier_set;
  595. extra->flags = POLICY_DATA_FLAG_SHARED_QUALIFIERS
  596. | POLICY_DATA_FLAG_EXTRA_NODE;
  597. node = level_add_node(NULL, extra, anyPolicy->parent,
  598. tree);
  599. }
  600. if (!tree->user_policies)
  601. {
  602. tree->user_policies = sk_X509_POLICY_NODE_new_null();
  603. if (!tree->user_policies)
  604. return 1;
  605. }
  606. if (!sk_X509_POLICY_NODE_push(tree->user_policies, node))
  607. return 0;
  608. }
  609. return 1;
  610. }
  611. static int tree_evaluate(X509_POLICY_TREE *tree)
  612. {
  613. int ret, i;
  614. X509_POLICY_LEVEL *curr = tree->levels + 1;
  615. const X509_POLICY_CACHE *cache;
  616. for(i = 1; i < tree->nlevel; i++, curr++)
  617. {
  618. cache = policy_cache_set(curr->cert);
  619. if (!tree_link_nodes(curr, cache))
  620. return 0;
  621. if (!(curr->flags & X509_V_FLAG_INHIBIT_ANY)
  622. && !tree_link_any(curr, cache, tree))
  623. return 0;
  624. tree_print("before tree_prune()", tree, curr);
  625. ret = tree_prune(tree, curr);
  626. if (ret != 1)
  627. return ret;
  628. }
  629. return 1;
  630. }
  631. static void exnode_free(X509_POLICY_NODE *node)
  632. {
  633. if (node->data && (node->data->flags & POLICY_DATA_FLAG_EXTRA_NODE))
  634. OPENSSL_free(node);
  635. }
  636. void X509_policy_tree_free(X509_POLICY_TREE *tree)
  637. {
  638. X509_POLICY_LEVEL *curr;
  639. int i;
  640. if (!tree)
  641. return;
  642. sk_X509_POLICY_NODE_free(tree->auth_policies);
  643. sk_X509_POLICY_NODE_pop_free(tree->user_policies, exnode_free);
  644. for(i = 0, curr = tree->levels; i < tree->nlevel; i++, curr++)
  645. {
  646. if (curr->cert)
  647. X509_free(curr->cert);
  648. if (curr->nodes)
  649. sk_X509_POLICY_NODE_pop_free(curr->nodes,
  650. policy_node_free);
  651. if (curr->anyPolicy)
  652. policy_node_free(curr->anyPolicy);
  653. }
  654. if (tree->extra_data)
  655. sk_X509_POLICY_DATA_pop_free(tree->extra_data,
  656. policy_data_free);
  657. OPENSSL_free(tree->levels);
  658. OPENSSL_free(tree);
  659. }
  660. /* Application policy checking function.
  661. * Return codes:
  662. * 0 Internal Error.
  663. * 1 Successful.
  664. * -1 One or more certificates contain invalid or inconsistent extensions
  665. * -2 User constrained policy set empty and requireExplicit true.
  666. */
  667. int X509_policy_check(X509_POLICY_TREE **ptree, int *pexplicit_policy,
  668. STACK_OF(X509) *certs,
  669. STACK_OF(ASN1_OBJECT) *policy_oids,
  670. unsigned int flags)
  671. {
  672. int ret;
  673. X509_POLICY_TREE *tree = NULL;
  674. STACK_OF(X509_POLICY_NODE) *nodes, *auth_nodes = NULL;
  675. *ptree = NULL;
  676. *pexplicit_policy = 0;
  677. ret = tree_init(&tree, certs, flags);
  678. switch (ret)
  679. {
  680. /* Tree empty requireExplicit False: OK */
  681. case 2:
  682. return 1;
  683. /* Some internal error */
  684. case -1:
  685. return -1;
  686. /* Some internal error */
  687. case 0:
  688. return 0;
  689. /* Tree empty requireExplicit True: Error */
  690. case 6:
  691. *pexplicit_policy = 1;
  692. return -2;
  693. /* Tree OK requireExplicit True: OK and continue */
  694. case 5:
  695. *pexplicit_policy = 1;
  696. break;
  697. /* Tree OK: continue */
  698. case 1:
  699. if (!tree)
  700. /*
  701. * tree_init() returns success and a null tree
  702. * if it's just looking at a trust anchor.
  703. * I'm not sure that returning success here is
  704. * correct, but I'm sure that reporting this
  705. * as an internal error which our caller
  706. * interprets as a malloc failure is wrong.
  707. */
  708. return 1;
  709. break;
  710. }
  711. if (!tree) goto error;
  712. ret = tree_evaluate(tree);
  713. tree_print("tree_evaluate()", tree, NULL);
  714. if (ret <= 0)
  715. goto error;
  716. /* Return value 2 means tree empty */
  717. if (ret == 2)
  718. {
  719. X509_policy_tree_free(tree);
  720. if (*pexplicit_policy)
  721. return -2;
  722. else
  723. return 1;
  724. }
  725. /* Tree is not empty: continue */
  726. ret = tree_calculate_authority_set(tree, &auth_nodes);
  727. if (!ret)
  728. goto error;
  729. if (!tree_calculate_user_set(tree, policy_oids, auth_nodes))
  730. goto error;
  731. if (ret == 2)
  732. sk_X509_POLICY_NODE_free(auth_nodes);
  733. if (tree)
  734. *ptree = tree;
  735. if (*pexplicit_policy)
  736. {
  737. nodes = X509_policy_tree_get0_user_policies(tree);
  738. if (sk_X509_POLICY_NODE_num(nodes) <= 0)
  739. return -2;
  740. }
  741. return 1;
  742. error:
  743. X509_policy_tree_free(tree);
  744. return 0;
  745. }