pcy_tree.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. /*
  2. * Copyright 2004-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include "internal/cryptlib.h"
  10. #include <openssl/trace.h>
  11. #include <openssl/x509.h>
  12. #include <openssl/x509v3.h>
  13. #include "pcy_local.h"
  14. DEFINE_STACK_OF(ASN1_OBJECT)
  15. DEFINE_STACK_OF(X509)
  16. DEFINE_STACK_OF(X509_POLICY_NODE)
  17. static void expected_print(BIO *channel,
  18. X509_POLICY_LEVEL *lev, X509_POLICY_NODE *node,
  19. int indent)
  20. {
  21. if ((lev->flags & X509_V_FLAG_INHIBIT_MAP)
  22. || !(node->data->flags & POLICY_DATA_FLAG_MAP_MASK))
  23. BIO_puts(channel, " Not Mapped\n");
  24. else {
  25. int i;
  26. STACK_OF(ASN1_OBJECT) *pset = node->data->expected_policy_set;
  27. ASN1_OBJECT *oid;
  28. BIO_puts(channel, " Expected: ");
  29. for (i = 0; i < sk_ASN1_OBJECT_num(pset); i++) {
  30. oid = sk_ASN1_OBJECT_value(pset, i);
  31. if (i)
  32. BIO_puts(channel, ", ");
  33. i2a_ASN1_OBJECT(channel, oid);
  34. }
  35. BIO_puts(channel, "\n");
  36. }
  37. }
  38. static void tree_print(BIO *channel,
  39. char *str, X509_POLICY_TREE *tree,
  40. X509_POLICY_LEVEL *curr)
  41. {
  42. X509_POLICY_LEVEL *plev;
  43. if (!curr)
  44. curr = tree->levels + tree->nlevel;
  45. else
  46. curr++;
  47. BIO_printf(channel, "Level print after %s\n", str);
  48. BIO_printf(channel, "Printing Up to Level %ld\n",
  49. (long)(curr - tree->levels));
  50. for (plev = tree->levels; plev != curr; plev++) {
  51. int i;
  52. BIO_printf(channel, "Level %ld, flags = %x\n",
  53. (long)(plev - tree->levels), plev->flags);
  54. for (i = 0; i < sk_X509_POLICY_NODE_num(plev->nodes); i++) {
  55. X509_POLICY_NODE *node =
  56. sk_X509_POLICY_NODE_value(plev->nodes, i);
  57. X509_POLICY_NODE_print(channel, node, 2);
  58. expected_print(channel, plev, node, 2);
  59. BIO_printf(channel, " Flags: %x\n", node->data->flags);
  60. }
  61. if (plev->anyPolicy)
  62. X509_POLICY_NODE_print(channel, plev->anyPolicy, 2);
  63. }
  64. }
  65. #define TREE_PRINT(str, tree, curr) \
  66. OSSL_TRACE_BEGIN(X509V3_POLICY) { \
  67. tree_print(trc_out, "before tree_prune()", tree, curr); \
  68. } OSSL_TRACE_END(X509V3_POLICY)
  69. /*-
  70. * Return value: <= 0 on error, or positive bit mask:
  71. *
  72. * X509_PCY_TREE_VALID: valid tree
  73. * X509_PCY_TREE_EMPTY: empty tree (including bare TA case)
  74. * X509_PCY_TREE_EXPLICIT: explicit policy required
  75. */
  76. static int tree_init(X509_POLICY_TREE **ptree, STACK_OF(X509) *certs,
  77. unsigned int flags)
  78. {
  79. X509_POLICY_TREE *tree;
  80. X509_POLICY_LEVEL *level;
  81. const X509_POLICY_CACHE *cache;
  82. X509_POLICY_DATA *data = NULL;
  83. int ret = X509_PCY_TREE_VALID;
  84. int n = sk_X509_num(certs) - 1; /* RFC5280 paths omit the TA */
  85. int explicit_policy = (flags & X509_V_FLAG_EXPLICIT_POLICY) ? 0 : n+1;
  86. int any_skip = (flags & X509_V_FLAG_INHIBIT_ANY) ? 0 : n+1;
  87. int map_skip = (flags & X509_V_FLAG_INHIBIT_MAP) ? 0 : n+1;
  88. int i;
  89. *ptree = NULL;
  90. /* Can't do anything with just a trust anchor */
  91. if (n == 0)
  92. return X509_PCY_TREE_EMPTY;
  93. /*
  94. * First setup the policy cache in all n non-TA certificates, this will be
  95. * used in X509_verify_cert() which will invoke the verify callback for all
  96. * certificates with invalid policy extensions.
  97. */
  98. for (i = n - 1; i >= 0; i--) {
  99. X509 *x = sk_X509_value(certs, i);
  100. /* Call for side-effect of computing hash and caching extensions */
  101. X509_check_purpose(x, -1, 0);
  102. /* If cache is NULL, likely ENOMEM: return immediately */
  103. if (policy_cache_set(x) == NULL)
  104. return X509_PCY_TREE_INTERNAL;
  105. }
  106. /*
  107. * At this point check for invalid policies and required explicit policy.
  108. * Note that the explicit_policy counter is a count-down to zero, with the
  109. * requirement kicking in if and once it does that. The counter is
  110. * decremented for every non-self-issued certificate in the path, but may
  111. * be further reduced by policy constraints in a non-leaf certificate.
  112. *
  113. * The ultimate policy set is the intersection of all the policies along
  114. * the path, if we hit a certificate with an empty policy set, and explicit
  115. * policy is required we're done.
  116. */
  117. for (i = n - 1;
  118. i >= 0 && (explicit_policy > 0 || (ret & X509_PCY_TREE_EMPTY) == 0);
  119. i--) {
  120. X509 *x = sk_X509_value(certs, i);
  121. uint32_t ex_flags = X509_get_extension_flags(x);
  122. /* All the policies are already cached, we can return early */
  123. if (ex_flags & EXFLAG_INVALID_POLICY)
  124. return X509_PCY_TREE_INVALID;
  125. /* Access the cache which we now know exists */
  126. cache = policy_cache_set(x);
  127. if ((ret & X509_PCY_TREE_VALID) && cache->data == NULL)
  128. ret = X509_PCY_TREE_EMPTY;
  129. if (explicit_policy > 0) {
  130. if (!(ex_flags & EXFLAG_SI))
  131. explicit_policy--;
  132. if ((cache->explicit_skip >= 0)
  133. && (cache->explicit_skip < explicit_policy))
  134. explicit_policy = cache->explicit_skip;
  135. }
  136. }
  137. if (explicit_policy == 0)
  138. ret |= X509_PCY_TREE_EXPLICIT;
  139. if ((ret & X509_PCY_TREE_VALID) == 0)
  140. return ret;
  141. /* If we get this far initialize the tree */
  142. if ((tree = OPENSSL_zalloc(sizeof(*tree))) == NULL) {
  143. X509V3err(X509V3_F_TREE_INIT, ERR_R_MALLOC_FAILURE);
  144. return X509_PCY_TREE_INTERNAL;
  145. }
  146. /*
  147. * http://tools.ietf.org/html/rfc5280#section-6.1.2, figure 3.
  148. *
  149. * The top level is implicitly for the trust anchor with valid expected
  150. * policies of anyPolicy. (RFC 5280 has the TA at depth 0 and the leaf at
  151. * depth n, we have the leaf at depth 0 and the TA at depth n).
  152. */
  153. if ((tree->levels = OPENSSL_zalloc(sizeof(*tree->levels)*(n+1))) == NULL) {
  154. OPENSSL_free(tree);
  155. X509V3err(X509V3_F_TREE_INIT, ERR_R_MALLOC_FAILURE);
  156. return X509_PCY_TREE_INTERNAL;
  157. }
  158. tree->nlevel = n+1;
  159. level = tree->levels;
  160. if ((data = policy_data_new(NULL, OBJ_nid2obj(NID_any_policy), 0)) == NULL)
  161. goto bad_tree;
  162. if (level_add_node(level, data, NULL, tree) == NULL) {
  163. policy_data_free(data);
  164. goto bad_tree;
  165. }
  166. /*
  167. * In this pass initialize all the tree levels and whether anyPolicy and
  168. * policy mapping are inhibited at each level.
  169. */
  170. for (i = n - 1; i >= 0; i--) {
  171. X509 *x = sk_X509_value(certs, i);
  172. uint32_t ex_flags = X509_get_extension_flags(x);
  173. /* Access the cache which we now know exists */
  174. cache = policy_cache_set(x);
  175. X509_up_ref(x);
  176. (++level)->cert = x;
  177. if (!cache->anyPolicy)
  178. level->flags |= X509_V_FLAG_INHIBIT_ANY;
  179. /* Determine inhibit any and inhibit map flags */
  180. if (any_skip == 0) {
  181. /*
  182. * Any matching allowed only if certificate is self issued and not
  183. * the last in the chain.
  184. */
  185. if (!(ex_flags & EXFLAG_SI) || (i == 0))
  186. level->flags |= X509_V_FLAG_INHIBIT_ANY;
  187. } else {
  188. if (!(ex_flags & EXFLAG_SI))
  189. any_skip--;
  190. if ((cache->any_skip >= 0) && (cache->any_skip < any_skip))
  191. any_skip = cache->any_skip;
  192. }
  193. if (map_skip == 0)
  194. level->flags |= X509_V_FLAG_INHIBIT_MAP;
  195. else {
  196. if (!(ex_flags & EXFLAG_SI))
  197. map_skip--;
  198. if ((cache->map_skip >= 0) && (cache->map_skip < map_skip))
  199. map_skip = cache->map_skip;
  200. }
  201. }
  202. *ptree = tree;
  203. return ret;
  204. bad_tree:
  205. X509_policy_tree_free(tree);
  206. return X509_PCY_TREE_INTERNAL;
  207. }
  208. /*
  209. * Return value: 1 on success, 0 otherwise
  210. */
  211. static int tree_link_matching_nodes(X509_POLICY_LEVEL *curr,
  212. X509_POLICY_DATA *data)
  213. {
  214. X509_POLICY_LEVEL *last = curr - 1;
  215. int i, matched = 0;
  216. /* Iterate through all in nodes linking matches */
  217. for (i = 0; i < sk_X509_POLICY_NODE_num(last->nodes); i++) {
  218. X509_POLICY_NODE *node = sk_X509_POLICY_NODE_value(last->nodes, i);
  219. if (policy_node_match(last, node, data->valid_policy)) {
  220. if (level_add_node(curr, data, node, NULL) == NULL)
  221. return 0;
  222. matched = 1;
  223. }
  224. }
  225. if (!matched && last->anyPolicy) {
  226. if (level_add_node(curr, data, last->anyPolicy, NULL) == NULL)
  227. return 0;
  228. }
  229. return 1;
  230. }
  231. /*
  232. * This corresponds to RFC3280 6.1.3(d)(1): link any data from
  233. * CertificatePolicies onto matching parent or anyPolicy if no match.
  234. *
  235. * Return value: 1 on success, 0 otherwise.
  236. */
  237. static int tree_link_nodes(X509_POLICY_LEVEL *curr,
  238. const X509_POLICY_CACHE *cache)
  239. {
  240. int i;
  241. for (i = 0; i < sk_X509_POLICY_DATA_num(cache->data); i++) {
  242. X509_POLICY_DATA *data = sk_X509_POLICY_DATA_value(cache->data, i);
  243. /* Look for matching nodes in previous level */
  244. if (!tree_link_matching_nodes(curr, data))
  245. return 0;
  246. }
  247. return 1;
  248. }
  249. /*
  250. * This corresponds to RFC3280 6.1.3(d)(2): Create new data for any unmatched
  251. * policies in the parent and link to anyPolicy.
  252. *
  253. * Return value: 1 on success, 0 otherwise.
  254. */
  255. static int tree_add_unmatched(X509_POLICY_LEVEL *curr,
  256. const X509_POLICY_CACHE *cache,
  257. const ASN1_OBJECT *id,
  258. X509_POLICY_NODE *node, X509_POLICY_TREE *tree)
  259. {
  260. X509_POLICY_DATA *data;
  261. if (id == NULL)
  262. id = node->data->valid_policy;
  263. /*
  264. * Create a new node with qualifiers from anyPolicy and id from unmatched
  265. * node.
  266. */
  267. if ((data = policy_data_new(NULL, id, node_critical(node))) == NULL)
  268. return 0;
  269. /* Curr may not have anyPolicy */
  270. data->qualifier_set = cache->anyPolicy->qualifier_set;
  271. data->flags |= POLICY_DATA_FLAG_SHARED_QUALIFIERS;
  272. if (level_add_node(curr, data, node, tree) == NULL) {
  273. policy_data_free(data);
  274. return 0;
  275. }
  276. return 1;
  277. }
  278. /*
  279. * Return value: 1 on success, 0 otherwise.
  280. */
  281. static int tree_link_unmatched(X509_POLICY_LEVEL *curr,
  282. const X509_POLICY_CACHE *cache,
  283. X509_POLICY_NODE *node, X509_POLICY_TREE *tree)
  284. {
  285. const X509_POLICY_LEVEL *last = curr - 1;
  286. int i;
  287. if ((last->flags & X509_V_FLAG_INHIBIT_MAP)
  288. || !(node->data->flags & POLICY_DATA_FLAG_MAPPED)) {
  289. /* If no policy mapping: matched if one child present */
  290. if (node->nchild)
  291. return 1;
  292. if (!tree_add_unmatched(curr, cache, NULL, node, tree))
  293. return 0;
  294. /* Add it */
  295. } else {
  296. /* If mapping: matched if one child per expected policy set */
  297. STACK_OF(ASN1_OBJECT) *expset = node->data->expected_policy_set;
  298. if (node->nchild == sk_ASN1_OBJECT_num(expset))
  299. return 1;
  300. /* Locate unmatched nodes */
  301. for (i = 0; i < sk_ASN1_OBJECT_num(expset); i++) {
  302. ASN1_OBJECT *oid = sk_ASN1_OBJECT_value(expset, i);
  303. if (level_find_node(curr, node, oid))
  304. continue;
  305. if (!tree_add_unmatched(curr, cache, oid, node, tree))
  306. return 0;
  307. }
  308. }
  309. return 1;
  310. }
  311. /*
  312. * Return value: 1 on success, 0 otherwise
  313. */
  314. static int tree_link_any(X509_POLICY_LEVEL *curr,
  315. const X509_POLICY_CACHE *cache,
  316. X509_POLICY_TREE *tree)
  317. {
  318. int i;
  319. X509_POLICY_NODE *node;
  320. X509_POLICY_LEVEL *last = curr - 1;
  321. for (i = 0; i < sk_X509_POLICY_NODE_num(last->nodes); i++) {
  322. node = sk_X509_POLICY_NODE_value(last->nodes, i);
  323. if (!tree_link_unmatched(curr, cache, node, tree))
  324. return 0;
  325. }
  326. /* Finally add link to anyPolicy */
  327. if (last->anyPolicy &&
  328. level_add_node(curr, cache->anyPolicy, last->anyPolicy, NULL) == NULL)
  329. return 0;
  330. return 1;
  331. }
  332. /*-
  333. * Prune the tree: delete any child mapped child data on the current level then
  334. * proceed up the tree deleting any data with no children. If we ever have no
  335. * data on a level we can halt because the tree will be empty.
  336. *
  337. * Return value: <= 0 error, otherwise one of:
  338. *
  339. * X509_PCY_TREE_VALID: valid tree
  340. * X509_PCY_TREE_EMPTY: empty tree
  341. */
  342. static int tree_prune(X509_POLICY_TREE *tree, X509_POLICY_LEVEL *curr)
  343. {
  344. STACK_OF(X509_POLICY_NODE) *nodes;
  345. X509_POLICY_NODE *node;
  346. int i;
  347. nodes = curr->nodes;
  348. if (curr->flags & X509_V_FLAG_INHIBIT_MAP) {
  349. for (i = sk_X509_POLICY_NODE_num(nodes) - 1; i >= 0; i--) {
  350. node = sk_X509_POLICY_NODE_value(nodes, i);
  351. /* Delete any mapped data: see RFC3280 XXXX */
  352. if (node->data->flags & POLICY_DATA_FLAG_MAP_MASK) {
  353. node->parent->nchild--;
  354. OPENSSL_free(node);
  355. (void)sk_X509_POLICY_NODE_delete(nodes, i);
  356. }
  357. }
  358. }
  359. for (;;) {
  360. --curr;
  361. nodes = curr->nodes;
  362. for (i = sk_X509_POLICY_NODE_num(nodes) - 1; i >= 0; i--) {
  363. node = sk_X509_POLICY_NODE_value(nodes, i);
  364. if (node->nchild == 0) {
  365. node->parent->nchild--;
  366. OPENSSL_free(node);
  367. (void)sk_X509_POLICY_NODE_delete(nodes, i);
  368. }
  369. }
  370. if (curr->anyPolicy && !curr->anyPolicy->nchild) {
  371. if (curr->anyPolicy->parent)
  372. curr->anyPolicy->parent->nchild--;
  373. OPENSSL_free(curr->anyPolicy);
  374. curr->anyPolicy = NULL;
  375. }
  376. if (curr == tree->levels) {
  377. /* If we zapped anyPolicy at top then tree is empty */
  378. if (!curr->anyPolicy)
  379. return X509_PCY_TREE_EMPTY;
  380. break;
  381. }
  382. }
  383. return X509_PCY_TREE_VALID;
  384. }
  385. /*
  386. * Return value: 1 on success, 0 otherwise.
  387. */
  388. static int tree_add_auth_node(STACK_OF(X509_POLICY_NODE) **pnodes,
  389. X509_POLICY_NODE *pcy)
  390. {
  391. if (*pnodes == NULL &&
  392. (*pnodes = policy_node_cmp_new()) == NULL)
  393. return 0;
  394. if (sk_X509_POLICY_NODE_find(*pnodes, pcy) >= 0)
  395. return 1;
  396. return sk_X509_POLICY_NODE_push(*pnodes, pcy) != 0;
  397. }
  398. #define TREE_CALC_FAILURE 0
  399. #define TREE_CALC_OK_NOFREE 1
  400. #define TREE_CALC_OK_DOFREE 2
  401. /*-
  402. * Calculate the authority set based on policy tree. The 'pnodes' parameter is
  403. * used as a store for the set of policy nodes used to calculate the user set.
  404. * If the authority set is not anyPolicy then pnodes will just point to the
  405. * authority set. If however the authority set is anyPolicy then the set of
  406. * valid policies (other than anyPolicy) is store in pnodes.
  407. *
  408. * Return value:
  409. * TREE_CALC_FAILURE on failure,
  410. * TREE_CALC_OK_NOFREE on success and pnodes need not be freed,
  411. * TREE_CALC_OK_DOFREE on success and pnodes needs to be freed
  412. */
  413. static int tree_calculate_authority_set(X509_POLICY_TREE *tree,
  414. STACK_OF(X509_POLICY_NODE) **pnodes)
  415. {
  416. X509_POLICY_LEVEL *curr;
  417. X509_POLICY_NODE *node, *anyptr;
  418. STACK_OF(X509_POLICY_NODE) **addnodes;
  419. int i, j;
  420. curr = tree->levels + tree->nlevel - 1;
  421. /* If last level contains anyPolicy set is anyPolicy */
  422. if (curr->anyPolicy) {
  423. if (!tree_add_auth_node(&tree->auth_policies, curr->anyPolicy))
  424. return TREE_CALC_FAILURE;
  425. addnodes = pnodes;
  426. } else
  427. /* Add policies to authority set */
  428. addnodes = &tree->auth_policies;
  429. curr = tree->levels;
  430. for (i = 1; i < tree->nlevel; i++) {
  431. /*
  432. * If no anyPolicy node on this this level it can't appear on lower
  433. * levels so end search.
  434. */
  435. if ((anyptr = curr->anyPolicy) == NULL)
  436. break;
  437. curr++;
  438. for (j = 0; j < sk_X509_POLICY_NODE_num(curr->nodes); j++) {
  439. node = sk_X509_POLICY_NODE_value(curr->nodes, j);
  440. if ((node->parent == anyptr)
  441. && !tree_add_auth_node(addnodes, node)) {
  442. if (addnodes == pnodes) {
  443. sk_X509_POLICY_NODE_free(*pnodes);
  444. *pnodes = NULL;
  445. }
  446. return TREE_CALC_FAILURE;
  447. }
  448. }
  449. }
  450. if (addnodes == pnodes)
  451. return TREE_CALC_OK_DOFREE;
  452. *pnodes = tree->auth_policies;
  453. return TREE_CALC_OK_NOFREE;
  454. }
  455. /*
  456. * Return value: 1 on success, 0 otherwise.
  457. */
  458. static int tree_calculate_user_set(X509_POLICY_TREE *tree,
  459. STACK_OF(ASN1_OBJECT) *policy_oids,
  460. STACK_OF(X509_POLICY_NODE) *auth_nodes)
  461. {
  462. int i;
  463. X509_POLICY_NODE *node;
  464. ASN1_OBJECT *oid;
  465. X509_POLICY_NODE *anyPolicy;
  466. X509_POLICY_DATA *extra;
  467. /*
  468. * Check if anyPolicy present in authority constrained policy set: this
  469. * will happen if it is a leaf node.
  470. */
  471. if (sk_ASN1_OBJECT_num(policy_oids) <= 0)
  472. return 1;
  473. anyPolicy = tree->levels[tree->nlevel - 1].anyPolicy;
  474. for (i = 0; i < sk_ASN1_OBJECT_num(policy_oids); i++) {
  475. oid = sk_ASN1_OBJECT_value(policy_oids, i);
  476. if (OBJ_obj2nid(oid) == NID_any_policy) {
  477. tree->flags |= POLICY_FLAG_ANY_POLICY;
  478. return 1;
  479. }
  480. }
  481. for (i = 0; i < sk_ASN1_OBJECT_num(policy_oids); i++) {
  482. oid = sk_ASN1_OBJECT_value(policy_oids, i);
  483. node = tree_find_sk(auth_nodes, oid);
  484. if (!node) {
  485. if (!anyPolicy)
  486. continue;
  487. /*
  488. * Create a new node with policy ID from user set and qualifiers
  489. * from anyPolicy.
  490. */
  491. extra = policy_data_new(NULL, oid, node_critical(anyPolicy));
  492. if (extra == NULL)
  493. return 0;
  494. extra->qualifier_set = anyPolicy->data->qualifier_set;
  495. extra->flags = POLICY_DATA_FLAG_SHARED_QUALIFIERS
  496. | POLICY_DATA_FLAG_EXTRA_NODE;
  497. node = level_add_node(NULL, extra, anyPolicy->parent, tree);
  498. }
  499. if (!tree->user_policies) {
  500. tree->user_policies = sk_X509_POLICY_NODE_new_null();
  501. if (!tree->user_policies)
  502. return 1;
  503. }
  504. if (!sk_X509_POLICY_NODE_push(tree->user_policies, node))
  505. return 0;
  506. }
  507. return 1;
  508. }
  509. /*-
  510. * Return value: <= 0 error, otherwise one of:
  511. * X509_PCY_TREE_VALID: valid tree
  512. * X509_PCY_TREE_EMPTY: empty tree
  513. * (see tree_prune()).
  514. */
  515. static int tree_evaluate(X509_POLICY_TREE *tree)
  516. {
  517. int ret, i;
  518. X509_POLICY_LEVEL *curr = tree->levels + 1;
  519. const X509_POLICY_CACHE *cache;
  520. for (i = 1; i < tree->nlevel; i++, curr++) {
  521. cache = policy_cache_set(curr->cert);
  522. if (!tree_link_nodes(curr, cache))
  523. return X509_PCY_TREE_INTERNAL;
  524. if (!(curr->flags & X509_V_FLAG_INHIBIT_ANY)
  525. && !tree_link_any(curr, cache, tree))
  526. return X509_PCY_TREE_INTERNAL;
  527. TREE_PRINT("before tree_prune()", tree, curr);
  528. ret = tree_prune(tree, curr);
  529. if (ret != X509_PCY_TREE_VALID)
  530. return ret;
  531. }
  532. return X509_PCY_TREE_VALID;
  533. }
  534. static void exnode_free(X509_POLICY_NODE *node)
  535. {
  536. if (node->data && (node->data->flags & POLICY_DATA_FLAG_EXTRA_NODE))
  537. OPENSSL_free(node);
  538. }
  539. void X509_policy_tree_free(X509_POLICY_TREE *tree)
  540. {
  541. X509_POLICY_LEVEL *curr;
  542. int i;
  543. if (!tree)
  544. return;
  545. sk_X509_POLICY_NODE_free(tree->auth_policies);
  546. sk_X509_POLICY_NODE_pop_free(tree->user_policies, exnode_free);
  547. for (i = 0, curr = tree->levels; i < tree->nlevel; i++, curr++) {
  548. X509_free(curr->cert);
  549. sk_X509_POLICY_NODE_pop_free(curr->nodes, policy_node_free);
  550. policy_node_free(curr->anyPolicy);
  551. }
  552. sk_X509_POLICY_DATA_pop_free(tree->extra_data, policy_data_free);
  553. OPENSSL_free(tree->levels);
  554. OPENSSL_free(tree);
  555. }
  556. /*-
  557. * Application policy checking function.
  558. * Return codes:
  559. * X509_PCY_TREE_FAILURE: Failure to satisfy explicit policy
  560. * X509_PCY_TREE_INVALID: Inconsistent or invalid extensions
  561. * X509_PCY_TREE_INTERNAL: Internal error, most likely malloc
  562. * X509_PCY_TREE_VALID: Success (null tree if empty or bare TA)
  563. */
  564. int X509_policy_check(X509_POLICY_TREE **ptree, int *pexplicit_policy,
  565. STACK_OF(X509) *certs,
  566. STACK_OF(ASN1_OBJECT) *policy_oids, unsigned int flags)
  567. {
  568. int init_ret;
  569. int ret;
  570. int calc_ret;
  571. X509_POLICY_TREE *tree = NULL;
  572. STACK_OF(X509_POLICY_NODE) *nodes, *auth_nodes = NULL;
  573. *ptree = NULL;
  574. *pexplicit_policy = 0;
  575. init_ret = tree_init(&tree, certs, flags);
  576. if (init_ret <= 0)
  577. return init_ret;
  578. if ((init_ret & X509_PCY_TREE_EXPLICIT) == 0) {
  579. if (init_ret & X509_PCY_TREE_EMPTY) {
  580. X509_policy_tree_free(tree);
  581. return X509_PCY_TREE_VALID;
  582. }
  583. } else {
  584. *pexplicit_policy = 1;
  585. /* Tree empty and requireExplicit True: Error */
  586. if (init_ret & X509_PCY_TREE_EMPTY)
  587. return X509_PCY_TREE_FAILURE;
  588. }
  589. ret = tree_evaluate(tree);
  590. TREE_PRINT("tree_evaluate()", tree, NULL);
  591. if (ret <= 0)
  592. goto error;
  593. if (ret == X509_PCY_TREE_EMPTY) {
  594. X509_policy_tree_free(tree);
  595. if (init_ret & X509_PCY_TREE_EXPLICIT)
  596. return X509_PCY_TREE_FAILURE;
  597. return X509_PCY_TREE_VALID;
  598. }
  599. /* Tree is not empty: continue */
  600. if ((calc_ret = tree_calculate_authority_set(tree, &auth_nodes)) == 0)
  601. goto error;
  602. ret = tree_calculate_user_set(tree, policy_oids, auth_nodes);
  603. if (calc_ret == TREE_CALC_OK_DOFREE)
  604. sk_X509_POLICY_NODE_free(auth_nodes);
  605. if (!ret)
  606. goto error;
  607. *ptree = tree;
  608. if (init_ret & X509_PCY_TREE_EXPLICIT) {
  609. nodes = X509_policy_tree_get0_user_policies(tree);
  610. if (sk_X509_POLICY_NODE_num(nodes) <= 0)
  611. return X509_PCY_TREE_FAILURE;
  612. }
  613. return X509_PCY_TREE_VALID;
  614. error:
  615. X509_policy_tree_free(tree);
  616. return X509_PCY_TREE_INTERNAL;
  617. }