pcy_tree.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  1. /* pcy_tree.c */
  2. /*
  3. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
  4. * 2004.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 2004 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * licensing@OpenSSL.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This product includes cryptographic software written by Eric Young
  55. * (eay@cryptsoft.com). This product includes software written by Tim
  56. * Hudson (tjh@cryptsoft.com).
  57. *
  58. */
  59. #include "cryptlib.h"
  60. #include <openssl/x509.h>
  61. #include <openssl/x509v3.h>
  62. #include "pcy_int.h"
  63. /*
  64. * Enable this to print out the complete policy tree at various point during
  65. * evaluation.
  66. */
  67. /*
  68. * #define OPENSSL_POLICY_DEBUG
  69. */
  70. #ifdef OPENSSL_POLICY_DEBUG
  71. static void expected_print(BIO *err, X509_POLICY_LEVEL *lev,
  72. X509_POLICY_NODE *node, int indent)
  73. {
  74. if ((lev->flags & X509_V_FLAG_INHIBIT_MAP)
  75. || !(node->data->flags & POLICY_DATA_FLAG_MAP_MASK))
  76. BIO_puts(err, " Not Mapped\n");
  77. else {
  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. 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. BIO_printf(err, "Level %ld, flags = %x\n",
  107. plev - tree->levels, plev->flags);
  108. for (i = 0; i < sk_X509_POLICY_NODE_num(plev->nodes); i++) {
  109. node = sk_X509_POLICY_NODE_value(plev->nodes, i);
  110. X509_POLICY_NODE_print(err, node, 2);
  111. expected_print(err, plev, node, 2);
  112. BIO_printf(err, " Flags: %x\n", node->data->flags);
  113. }
  114. if (plev->anyPolicy)
  115. X509_POLICY_NODE_print(err, plev->anyPolicy, 2);
  116. }
  117. BIO_free(err);
  118. }
  119. #else
  120. # define tree_print(a,b,c) /* */
  121. #endif
  122. /*-
  123. * Initialize policy tree. Return values:
  124. * 0 Some internal error occurred.
  125. * -1 Inconsistent or invalid extensions in certificates.
  126. * 1 Tree initialized OK.
  127. * 2 Policy tree is empty.
  128. * 5 Tree OK and requireExplicitPolicy true.
  129. * 6 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. CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
  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 i, matched = 0;
  270. /* Iterate through all in nodes linking matches */
  271. for (i = 0; i < sk_X509_POLICY_NODE_num(last->nodes); i++) {
  272. node = sk_X509_POLICY_NODE_value(last->nodes, i);
  273. if (policy_node_match(last, node, data->valid_policy)) {
  274. if (!level_add_node(curr, data, node, NULL))
  275. return 0;
  276. matched = 1;
  277. }
  278. }
  279. if (!matched && last->anyPolicy) {
  280. if (!level_add_node(curr, data, last->anyPolicy, NULL))
  281. return 0;
  282. }
  283. return 1;
  284. }
  285. /*
  286. * This corresponds to RFC3280 6.1.3(d)(1): link any data from
  287. * CertificatePolicies onto matching parent or anyPolicy if no match.
  288. */
  289. static int tree_link_nodes(X509_POLICY_LEVEL *curr,
  290. const X509_POLICY_CACHE *cache)
  291. {
  292. int i;
  293. X509_POLICY_DATA *data;
  294. for (i = 0; i < sk_X509_POLICY_DATA_num(cache->data); i++) {
  295. data = sk_X509_POLICY_DATA_value(cache->data, i);
  296. /*
  297. * If a node is mapped any it doesn't have a corresponding
  298. * CertificatePolicies entry. However such an identical node would
  299. * be created if anyPolicy matching is enabled because there would be
  300. * no match with the parent valid_policy_set. So we create link
  301. * because then it will have the mapping flags right and we can prune
  302. * it later.
  303. */
  304. #if 0
  305. if ((data->flags & POLICY_DATA_FLAG_MAPPED_ANY)
  306. && !(curr->flags & X509_V_FLAG_INHIBIT_ANY))
  307. continue;
  308. #endif
  309. /* Look for matching nodes in previous level */
  310. if (!tree_link_matching_nodes(curr, data))
  311. return 0;
  312. }
  313. return 1;
  314. }
  315. /*
  316. * This corresponds to RFC3280 6.1.3(d)(2): Create new data for any unmatched
  317. * policies in the parent and link to anyPolicy.
  318. */
  319. static int tree_add_unmatched(X509_POLICY_LEVEL *curr,
  320. const X509_POLICY_CACHE *cache,
  321. const ASN1_OBJECT *id,
  322. X509_POLICY_NODE *node, X509_POLICY_TREE *tree)
  323. {
  324. X509_POLICY_DATA *data;
  325. if (id == NULL)
  326. id = node->data->valid_policy;
  327. /*
  328. * Create a new node with qualifiers from anyPolicy and id from unmatched
  329. * node.
  330. */
  331. data = policy_data_new(NULL, id, node_critical(node));
  332. if (data == NULL)
  333. return 0;
  334. /* Curr may not have anyPolicy */
  335. data->qualifier_set = cache->anyPolicy->qualifier_set;
  336. data->flags |= POLICY_DATA_FLAG_SHARED_QUALIFIERS;
  337. if (!level_add_node(curr, data, node, tree)) {
  338. policy_data_free(data);
  339. return 0;
  340. }
  341. return 1;
  342. }
  343. static int tree_link_unmatched(X509_POLICY_LEVEL *curr,
  344. const X509_POLICY_CACHE *cache,
  345. X509_POLICY_NODE *node, X509_POLICY_TREE *tree)
  346. {
  347. const X509_POLICY_LEVEL *last = curr - 1;
  348. int i;
  349. if ((last->flags & X509_V_FLAG_INHIBIT_MAP)
  350. || !(node->data->flags & POLICY_DATA_FLAG_MAPPED)) {
  351. /* If no policy mapping: matched if one child present */
  352. if (node->nchild)
  353. return 1;
  354. if (!tree_add_unmatched(curr, cache, NULL, node, tree))
  355. return 0;
  356. /* Add it */
  357. } else {
  358. /* If mapping: matched if one child per expected policy set */
  359. STACK_OF(ASN1_OBJECT) *expset = node->data->expected_policy_set;
  360. if (node->nchild == sk_ASN1_OBJECT_num(expset))
  361. return 1;
  362. /* Locate unmatched nodes */
  363. for (i = 0; i < sk_ASN1_OBJECT_num(expset); i++) {
  364. ASN1_OBJECT *oid = sk_ASN1_OBJECT_value(expset, i);
  365. if (level_find_node(curr, node, oid))
  366. continue;
  367. if (!tree_add_unmatched(curr, cache, oid, node, tree))
  368. return 0;
  369. }
  370. }
  371. return 1;
  372. }
  373. static int tree_link_any(X509_POLICY_LEVEL *curr,
  374. const X509_POLICY_CACHE *cache,
  375. X509_POLICY_TREE *tree)
  376. {
  377. int i;
  378. /*
  379. * X509_POLICY_DATA *data;
  380. */
  381. X509_POLICY_NODE *node;
  382. X509_POLICY_LEVEL *last = curr - 1;
  383. for (i = 0; i < sk_X509_POLICY_NODE_num(last->nodes); i++) {
  384. node = sk_X509_POLICY_NODE_value(last->nodes, i);
  385. if (!tree_link_unmatched(curr, cache, node, tree))
  386. return 0;
  387. #if 0
  388. /*
  389. * Skip any node with any children: we only want unmathced nodes.
  390. * Note: need something better for policy mapping because each node
  391. * may have multiple children
  392. */
  393. if (node->nchild)
  394. continue;
  395. /*
  396. * Create a new node with qualifiers from anyPolicy and id from
  397. * unmatched node.
  398. */
  399. data = policy_data_new(NULL, node->data->valid_policy,
  400. node_critical(node));
  401. if (data == NULL)
  402. return 0;
  403. /* Curr may not have anyPolicy */
  404. data->qualifier_set = cache->anyPolicy->qualifier_set;
  405. data->flags |= POLICY_DATA_FLAG_SHARED_QUALIFIERS;
  406. if (!level_add_node(curr, data, node, tree)) {
  407. policy_data_free(data);
  408. return 0;
  409. }
  410. #endif
  411. }
  412. /* Finally add link to anyPolicy */
  413. if (last->anyPolicy) {
  414. if (!level_add_node(curr, cache->anyPolicy, last->anyPolicy, NULL))
  415. return 0;
  416. }
  417. return 1;
  418. }
  419. /*
  420. * Prune the tree: delete any child mapped child data on the current level
  421. * then proceed up the tree deleting any data with no children. If we ever
  422. * have no data on a level we can halt because the tree will be empty.
  423. */
  424. static int tree_prune(X509_POLICY_TREE *tree, X509_POLICY_LEVEL *curr)
  425. {
  426. STACK_OF(X509_POLICY_NODE) *nodes;
  427. X509_POLICY_NODE *node;
  428. int i;
  429. nodes = curr->nodes;
  430. if (curr->flags & X509_V_FLAG_INHIBIT_MAP) {
  431. for (i = sk_X509_POLICY_NODE_num(nodes) - 1; i >= 0; i--) {
  432. node = sk_X509_POLICY_NODE_value(nodes, i);
  433. /* Delete any mapped data: see RFC3280 XXXX */
  434. if (node->data->flags & POLICY_DATA_FLAG_MAP_MASK) {
  435. node->parent->nchild--;
  436. OPENSSL_free(node);
  437. (void)sk_X509_POLICY_NODE_delete(nodes, i);
  438. }
  439. }
  440. }
  441. for (;;) {
  442. --curr;
  443. nodes = curr->nodes;
  444. for (i = sk_X509_POLICY_NODE_num(nodes) - 1; i >= 0; i--) {
  445. node = sk_X509_POLICY_NODE_value(nodes, i);
  446. if (node->nchild == 0) {
  447. node->parent->nchild--;
  448. OPENSSL_free(node);
  449. (void)sk_X509_POLICY_NODE_delete(nodes, i);
  450. }
  451. }
  452. if (curr->anyPolicy && !curr->anyPolicy->nchild) {
  453. if (curr->anyPolicy->parent)
  454. curr->anyPolicy->parent->nchild--;
  455. OPENSSL_free(curr->anyPolicy);
  456. curr->anyPolicy = NULL;
  457. }
  458. if (curr == tree->levels) {
  459. /* If we zapped anyPolicy at top then tree is empty */
  460. if (!curr->anyPolicy)
  461. return 2;
  462. return 1;
  463. }
  464. }
  465. return 1;
  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, pcy) != -1)
  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, j;
  495. curr = tree->levels + tree->nlevel - 1;
  496. /* If last level contains anyPolicy set is anyPolicy */
  497. if (curr->anyPolicy) {
  498. if (!tree_add_auth_node(&tree->auth_policies, curr->anyPolicy))
  499. return 0;
  500. addnodes = pnodes;
  501. } else
  502. /* Add policies to authority set */
  503. addnodes = &tree->auth_policies;
  504. curr = tree->levels;
  505. for (i = 1; i < tree->nlevel; i++) {
  506. /*
  507. * If no anyPolicy node on this this level it can't appear on lower
  508. * levels so end search.
  509. */
  510. if (!(anyptr = curr->anyPolicy))
  511. break;
  512. curr++;
  513. for (j = 0; j < sk_X509_POLICY_NODE_num(curr->nodes); j++) {
  514. node = sk_X509_POLICY_NODE_value(curr->nodes, j);
  515. if ((node->parent == anyptr)
  516. && !tree_add_auth_node(addnodes, node))
  517. return 0;
  518. }
  519. }
  520. if (addnodes == pnodes)
  521. return 2;
  522. *pnodes = tree->auth_policies;
  523. return 1;
  524. }
  525. static int tree_calculate_user_set(X509_POLICY_TREE *tree,
  526. STACK_OF(ASN1_OBJECT) *policy_oids,
  527. STACK_OF(X509_POLICY_NODE) *auth_nodes)
  528. {
  529. int i;
  530. X509_POLICY_NODE *node;
  531. ASN1_OBJECT *oid;
  532. X509_POLICY_NODE *anyPolicy;
  533. X509_POLICY_DATA *extra;
  534. /*
  535. * Check if anyPolicy present in authority constrained policy set: this
  536. * will happen if it is a leaf node.
  537. */
  538. if (sk_ASN1_OBJECT_num(policy_oids) <= 0)
  539. return 1;
  540. anyPolicy = tree->levels[tree->nlevel - 1].anyPolicy;
  541. for (i = 0; i < sk_ASN1_OBJECT_num(policy_oids); i++) {
  542. oid = sk_ASN1_OBJECT_value(policy_oids, i);
  543. if (OBJ_obj2nid(oid) == NID_any_policy) {
  544. tree->flags |= POLICY_FLAG_ANY_POLICY;
  545. return 1;
  546. }
  547. }
  548. for (i = 0; i < sk_ASN1_OBJECT_num(policy_oids); i++) {
  549. oid = sk_ASN1_OBJECT_value(policy_oids, i);
  550. node = tree_find_sk(auth_nodes, oid);
  551. if (!node) {
  552. if (!anyPolicy)
  553. continue;
  554. /*
  555. * Create a new node with policy ID from user set and qualifiers
  556. * from anyPolicy.
  557. */
  558. extra = policy_data_new(NULL, oid, node_critical(anyPolicy));
  559. if (!extra)
  560. return 0;
  561. extra->qualifier_set = anyPolicy->data->qualifier_set;
  562. extra->flags = POLICY_DATA_FLAG_SHARED_QUALIFIERS
  563. | POLICY_DATA_FLAG_EXTRA_NODE;
  564. node = level_add_node(NULL, extra, anyPolicy->parent, tree);
  565. }
  566. if (!tree->user_policies) {
  567. tree->user_policies = sk_X509_POLICY_NODE_new_null();
  568. if (!tree->user_policies)
  569. return 1;
  570. }
  571. if (!sk_X509_POLICY_NODE_push(tree->user_policies, node))
  572. return 0;
  573. }
  574. return 1;
  575. }
  576. static int tree_evaluate(X509_POLICY_TREE *tree)
  577. {
  578. int ret, i;
  579. X509_POLICY_LEVEL *curr = tree->levels + 1;
  580. const X509_POLICY_CACHE *cache;
  581. for (i = 1; i < tree->nlevel; i++, curr++) {
  582. cache = policy_cache_set(curr->cert);
  583. if (!tree_link_nodes(curr, cache))
  584. return 0;
  585. if (!(curr->flags & X509_V_FLAG_INHIBIT_ANY)
  586. && !tree_link_any(curr, cache, tree))
  587. return 0;
  588. tree_print("before tree_prune()", tree, curr);
  589. ret = tree_prune(tree, curr);
  590. if (ret != 1)
  591. return ret;
  592. }
  593. return 1;
  594. }
  595. static void exnode_free(X509_POLICY_NODE *node)
  596. {
  597. if (node->data && (node->data->flags & POLICY_DATA_FLAG_EXTRA_NODE))
  598. OPENSSL_free(node);
  599. }
  600. void X509_policy_tree_free(X509_POLICY_TREE *tree)
  601. {
  602. X509_POLICY_LEVEL *curr;
  603. int i;
  604. if (!tree)
  605. return;
  606. sk_X509_POLICY_NODE_free(tree->auth_policies);
  607. sk_X509_POLICY_NODE_pop_free(tree->user_policies, exnode_free);
  608. for (i = 0, curr = tree->levels; i < tree->nlevel; i++, curr++) {
  609. if (curr->cert)
  610. X509_free(curr->cert);
  611. if (curr->nodes)
  612. sk_X509_POLICY_NODE_pop_free(curr->nodes, policy_node_free);
  613. if (curr->anyPolicy)
  614. policy_node_free(curr->anyPolicy);
  615. }
  616. if (tree->extra_data)
  617. sk_X509_POLICY_DATA_pop_free(tree->extra_data, policy_data_free);
  618. OPENSSL_free(tree->levels);
  619. OPENSSL_free(tree);
  620. }
  621. /*-
  622. * Application policy checking function.
  623. * Return codes:
  624. * 0 Internal Error.
  625. * 1 Successful.
  626. * -1 One or more certificates contain invalid or inconsistent extensions
  627. * -2 User constrained policy set empty and requireExplicit true.
  628. */
  629. int X509_policy_check(X509_POLICY_TREE **ptree, int *pexplicit_policy,
  630. STACK_OF(X509) *certs,
  631. STACK_OF(ASN1_OBJECT) *policy_oids, unsigned int flags)
  632. {
  633. int ret;
  634. X509_POLICY_TREE *tree = NULL;
  635. STACK_OF(X509_POLICY_NODE) *nodes, *auth_nodes = NULL;
  636. *ptree = NULL;
  637. *pexplicit_policy = 0;
  638. ret = tree_init(&tree, certs, flags);
  639. switch (ret) {
  640. /* Tree empty requireExplicit False: OK */
  641. case 2:
  642. return 1;
  643. /* Some internal error */
  644. case -1:
  645. return -1;
  646. /* Some internal error */
  647. case 0:
  648. return 0;
  649. /* Tree empty requireExplicit True: Error */
  650. case 6:
  651. *pexplicit_policy = 1;
  652. return -2;
  653. /* Tree OK requireExplicit True: OK and continue */
  654. case 5:
  655. *pexplicit_policy = 1;
  656. break;
  657. /* Tree OK: continue */
  658. case 1:
  659. if (!tree)
  660. /*
  661. * tree_init() returns success and a null tree
  662. * if it's just looking at a trust anchor.
  663. * I'm not sure that returning success here is
  664. * correct, but I'm sure that reporting this
  665. * as an internal error which our caller
  666. * interprets as a malloc failure is wrong.
  667. */
  668. return 1;
  669. break;
  670. }
  671. if (!tree)
  672. goto error;
  673. ret = tree_evaluate(tree);
  674. tree_print("tree_evaluate()", tree, NULL);
  675. if (ret <= 0)
  676. goto error;
  677. /* Return value 2 means tree empty */
  678. if (ret == 2) {
  679. X509_policy_tree_free(tree);
  680. if (*pexplicit_policy)
  681. return -2;
  682. else
  683. return 1;
  684. }
  685. /* Tree is not empty: continue */
  686. ret = tree_calculate_authority_set(tree, &auth_nodes);
  687. if (!ret)
  688. goto error;
  689. if (!tree_calculate_user_set(tree, policy_oids, auth_nodes))
  690. goto error;
  691. if (ret == 2)
  692. sk_X509_POLICY_NODE_free(auth_nodes);
  693. if (tree)
  694. *ptree = tree;
  695. if (*pexplicit_policy) {
  696. nodes = X509_policy_tree_get0_user_policies(tree);
  697. if (sk_X509_POLICY_NODE_num(nodes) <= 0)
  698. return -2;
  699. }
  700. return 1;
  701. error:
  702. X509_policy_tree_free(tree);
  703. return 0;
  704. }