Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

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