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.

pcy_tree.c 24 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  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. level->cert = X509_up_ref(x);
  227. if (!cache->anyPolicy)
  228. level->flags |= X509_V_FLAG_INHIBIT_ANY;
  229. /* Determine inhibit any and inhibit map flags */
  230. if (any_skip == 0) {
  231. /*
  232. * Any matching allowed if certificate is self issued and not the
  233. * last in the chain.
  234. */
  235. if (!(x->ex_flags & EXFLAG_SI) || (i == 0))
  236. level->flags |= X509_V_FLAG_INHIBIT_ANY;
  237. } else {
  238. if (!(x->ex_flags & EXFLAG_SI))
  239. any_skip--;
  240. if ((cache->any_skip >= 0)
  241. && (cache->any_skip < any_skip))
  242. any_skip = cache->any_skip;
  243. }
  244. if (map_skip == 0)
  245. level->flags |= X509_V_FLAG_INHIBIT_MAP;
  246. else {
  247. if (!(x->ex_flags & EXFLAG_SI))
  248. map_skip--;
  249. if ((cache->map_skip >= 0)
  250. && (cache->map_skip < map_skip))
  251. map_skip = cache->map_skip;
  252. }
  253. }
  254. *ptree = tree;
  255. if (explicit_policy)
  256. return 1;
  257. else
  258. return 5;
  259. bad_tree:
  260. X509_policy_tree_free(tree);
  261. return 0;
  262. }
  263. static int tree_link_matching_nodes(X509_POLICY_LEVEL *curr,
  264. const X509_POLICY_DATA *data)
  265. {
  266. X509_POLICY_LEVEL *last = curr - 1;
  267. X509_POLICY_NODE *node;
  268. int matched = 0;
  269. size_t i;
  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. size_t 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. size_t 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 ((size_t)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. size_t 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. }
  466. static int tree_add_auth_node(STACK_OF(X509_POLICY_NODE) **pnodes,
  467. X509_POLICY_NODE *pcy)
  468. {
  469. if (!*pnodes) {
  470. *pnodes = policy_node_cmp_new();
  471. if (!*pnodes)
  472. return 0;
  473. } else if (sk_X509_POLICY_NODE_find(*pnodes, NULL, pcy))
  474. return 1;
  475. if (!sk_X509_POLICY_NODE_push(*pnodes, pcy))
  476. return 0;
  477. return 1;
  478. }
  479. /*
  480. * Calculate the authority set based on policy tree. The 'pnodes' parameter
  481. * is used as a store for the set of policy nodes used to calculate the user
  482. * set. If the authority set is not anyPolicy then pnodes will just point to
  483. * the authority set. If however the authority set is anyPolicy then the set
  484. * of valid policies (other than anyPolicy) is store in pnodes. The return
  485. * value of '2' is used in this case to indicate that pnodes should be freed.
  486. */
  487. static int tree_calculate_authority_set(X509_POLICY_TREE *tree,
  488. STACK_OF(X509_POLICY_NODE) **pnodes)
  489. {
  490. X509_POLICY_LEVEL *curr;
  491. X509_POLICY_NODE *node, *anyptr;
  492. STACK_OF(X509_POLICY_NODE) **addnodes;
  493. int i;
  494. size_t 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. size_t 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. Return codes: 0 Internal Error. 1
  623. * Successful. -1 One or more certificates contain invalid or inconsistent
  624. * extensions -2 User constrained policy set empty and requireExplicit true.
  625. */
  626. int X509_policy_check(X509_POLICY_TREE **ptree, int *pexplicit_policy,
  627. STACK_OF(X509) *certs,
  628. STACK_OF(ASN1_OBJECT) *policy_oids, unsigned int flags)
  629. {
  630. int ret;
  631. X509_POLICY_TREE *tree = NULL;
  632. STACK_OF(X509_POLICY_NODE) *nodes, *auth_nodes = NULL;
  633. *ptree = NULL;
  634. *pexplicit_policy = 0;
  635. ret = tree_init(&tree, certs, flags);
  636. switch (ret) {
  637. /* Tree empty requireExplicit False: OK */
  638. case 2:
  639. return 1;
  640. /* Some internal error */
  641. case -1:
  642. return -1;
  643. /* Some internal error */
  644. case 0:
  645. return 0;
  646. /* Tree empty requireExplicit True: Error */
  647. case 6:
  648. *pexplicit_policy = 1;
  649. return -2;
  650. /* Tree OK requireExplicit True: OK and continue */
  651. case 5:
  652. *pexplicit_policy = 1;
  653. break;
  654. /* Tree OK: continue */
  655. case 1:
  656. if (!tree)
  657. /*
  658. * tree_init() returns success and a null tree
  659. * if it's just looking at a trust anchor.
  660. * I'm not sure that returning success here is
  661. * correct, but I'm sure that reporting this
  662. * as an internal error which our caller
  663. * interprets as a malloc failure is wrong.
  664. */
  665. return 1;
  666. break;
  667. }
  668. if (!tree)
  669. goto error;
  670. ret = tree_evaluate(tree);
  671. tree_print("tree_evaluate()", tree, NULL);
  672. if (ret <= 0)
  673. goto error;
  674. /* Return value 2 means tree empty */
  675. if (ret == 2) {
  676. X509_policy_tree_free(tree);
  677. if (*pexplicit_policy)
  678. return -2;
  679. else
  680. return 1;
  681. }
  682. /* Tree is not empty: continue */
  683. ret = tree_calculate_authority_set(tree, &auth_nodes);
  684. if (!ret)
  685. goto error;
  686. if (!tree_calculate_user_set(tree, policy_oids, auth_nodes))
  687. goto error;
  688. if (ret == 2)
  689. sk_X509_POLICY_NODE_free(auth_nodes);
  690. if (tree)
  691. *ptree = tree;
  692. if (*pexplicit_policy) {
  693. nodes = X509_policy_tree_get0_user_policies(tree);
  694. if (sk_X509_POLICY_NODE_num(nodes) <= 0)
  695. return -2;
  696. }
  697. return 1;
  698. error:
  699. X509_policy_tree_free(tree);
  700. return 0;
  701. }