2 * Copyright 2004-2023 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include "internal/cryptlib.h"
11 #include <openssl/trace.h>
12 #include <openssl/x509.h>
13 #include <openssl/x509v3.h>
15 #include "pcy_local.h"
18 * If the maximum number of nodes in the policy tree isn't defined, set it to
19 * a generous default of 1000 nodes.
21 * Defining this to be zero means unlimited policy tree growth which opens the
22 * door on CVE-2023-0464.
24 #ifndef OPENSSL_POLICY_TREE_NODES_MAX
25 # define OPENSSL_POLICY_TREE_NODES_MAX 1000
28 static void exnode_free(X509_POLICY_NODE *node);
30 static void expected_print(BIO *channel,
31 X509_POLICY_LEVEL *lev, X509_POLICY_NODE *node,
34 if ((lev->flags & X509_V_FLAG_INHIBIT_MAP)
35 || !(node->data->flags & POLICY_DATA_FLAG_MAP_MASK))
36 BIO_puts(channel, " Not Mapped\n");
40 STACK_OF(ASN1_OBJECT) *pset = node->data->expected_policy_set;
42 BIO_puts(channel, " Expected: ");
43 for (i = 0; i < sk_ASN1_OBJECT_num(pset); i++) {
44 oid = sk_ASN1_OBJECT_value(pset, i);
46 BIO_puts(channel, ", ");
47 i2a_ASN1_OBJECT(channel, oid);
49 BIO_puts(channel, "\n");
53 static void tree_print(BIO *channel,
54 char *str, X509_POLICY_TREE *tree,
55 X509_POLICY_LEVEL *curr)
57 X509_POLICY_LEVEL *plev;
60 curr = tree->levels + tree->nlevel;
64 BIO_printf(channel, "Level print after %s\n", str);
65 BIO_printf(channel, "Printing Up to Level %ld\n",
66 (long)(curr - tree->levels));
67 for (plev = tree->levels; plev != curr; plev++) {
70 BIO_printf(channel, "Level %ld, flags = %x\n",
71 (long)(plev - tree->levels), plev->flags);
72 for (i = 0; i < sk_X509_POLICY_NODE_num(plev->nodes); i++) {
73 X509_POLICY_NODE *node =
74 sk_X509_POLICY_NODE_value(plev->nodes, i);
76 X509_POLICY_NODE_print(channel, node, 2);
77 expected_print(channel, plev, node, 2);
78 BIO_printf(channel, " Flags: %x\n", node->data->flags);
81 X509_POLICY_NODE_print(channel, plev->anyPolicy, 2);
85 #define TREE_PRINT(str, tree, curr) \
86 OSSL_TRACE_BEGIN(X509V3_POLICY) { \
87 tree_print(trc_out, "before tree_prune()", tree, curr); \
88 } OSSL_TRACE_END(X509V3_POLICY)
91 * Return value: <= 0 on error, or positive bit mask:
93 * X509_PCY_TREE_VALID: valid tree
94 * X509_PCY_TREE_EMPTY: empty tree (including bare TA case)
95 * X509_PCY_TREE_EXPLICIT: explicit policy required
97 static int tree_init(X509_POLICY_TREE **ptree, STACK_OF(X509) *certs,
100 X509_POLICY_TREE *tree;
101 X509_POLICY_LEVEL *level;
102 const X509_POLICY_CACHE *cache;
103 X509_POLICY_DATA *data = NULL;
104 int ret = X509_PCY_TREE_VALID;
105 int n = sk_X509_num(certs) - 1; /* RFC5280 paths omit the TA */
106 int explicit_policy = (flags & X509_V_FLAG_EXPLICIT_POLICY) ? 0 : n+1;
107 int any_skip = (flags & X509_V_FLAG_INHIBIT_ANY) ? 0 : n+1;
108 int map_skip = (flags & X509_V_FLAG_INHIBIT_MAP) ? 0 : n+1;
113 /* Can't do anything with just a trust anchor */
115 return X509_PCY_TREE_EMPTY;
118 * First setup the policy cache in all n non-TA certificates, this will be
119 * used in X509_verify_cert() which will invoke the verify callback for all
120 * certificates with invalid policy extensions.
122 for (i = n - 1; i >= 0; i--) {
123 X509 *x = sk_X509_value(certs, i);
125 /* Call for side-effect of computing hash and caching extensions */
126 X509_check_purpose(x, -1, 0);
128 /* If cache is NULL, likely ENOMEM: return immediately */
129 if (ossl_policy_cache_set(x) == NULL)
130 return X509_PCY_TREE_INTERNAL;
134 * At this point check for invalid policies and required explicit policy.
135 * Note that the explicit_policy counter is a count-down to zero, with the
136 * requirement kicking in if and once it does that. The counter is
137 * decremented for every non-self-issued certificate in the path, but may
138 * be further reduced by policy constraints in a non-leaf certificate.
140 * The ultimate policy set is the intersection of all the policies along
141 * the path, if we hit a certificate with an empty policy set, and explicit
142 * policy is required we're done.
145 i >= 0 && (explicit_policy > 0 || (ret & X509_PCY_TREE_EMPTY) == 0);
147 X509 *x = sk_X509_value(certs, i);
148 uint32_t ex_flags = X509_get_extension_flags(x);
150 /* All the policies are already cached, we can return early */
151 if (ex_flags & EXFLAG_INVALID_POLICY)
152 return X509_PCY_TREE_INVALID;
154 /* Access the cache which we now know exists */
155 cache = ossl_policy_cache_set(x);
157 if ((ret & X509_PCY_TREE_VALID) && cache->data == NULL)
158 ret = X509_PCY_TREE_EMPTY;
159 if (explicit_policy > 0) {
160 if (!(ex_flags & EXFLAG_SI))
162 if ((cache->explicit_skip >= 0)
163 && (cache->explicit_skip < explicit_policy))
164 explicit_policy = cache->explicit_skip;
168 if (explicit_policy == 0)
169 ret |= X509_PCY_TREE_EXPLICIT;
170 if ((ret & X509_PCY_TREE_VALID) == 0)
173 /* If we get this far initialize the tree */
174 if ((tree = OPENSSL_zalloc(sizeof(*tree))) == NULL) {
175 ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
176 return X509_PCY_TREE_INTERNAL;
179 /* Limit the growth of the tree to mitigate CVE-2023-0464 */
180 tree->node_maximum = OPENSSL_POLICY_TREE_NODES_MAX;
183 * http://tools.ietf.org/html/rfc5280#section-6.1.2, figure 3.
185 * The top level is implicitly for the trust anchor with valid expected
186 * policies of anyPolicy. (RFC 5280 has the TA at depth 0 and the leaf at
187 * depth n, we have the leaf at depth 0 and the TA at depth n).
189 if ((tree->levels = OPENSSL_zalloc(sizeof(*tree->levels)*(n+1))) == NULL) {
191 ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
192 return X509_PCY_TREE_INTERNAL;
195 level = tree->levels;
196 if ((data = ossl_policy_data_new(NULL,
197 OBJ_nid2obj(NID_any_policy), 0)) == NULL)
199 if (ossl_policy_level_add_node(level, data, NULL, tree, 1) == NULL) {
200 ossl_policy_data_free(data);
205 * In this pass initialize all the tree levels and whether anyPolicy and
206 * policy mapping are inhibited at each level.
208 for (i = n - 1; i >= 0; i--) {
209 X509 *x = sk_X509_value(certs, i);
210 uint32_t ex_flags = X509_get_extension_flags(x);
212 /* Access the cache which we now know exists */
213 cache = ossl_policy_cache_set(x);
218 if (!cache->anyPolicy)
219 level->flags |= X509_V_FLAG_INHIBIT_ANY;
221 /* Determine inhibit any and inhibit map flags */
224 * Any matching allowed only if certificate is self issued and not
225 * the last in the chain.
227 if (!(ex_flags & EXFLAG_SI) || (i == 0))
228 level->flags |= X509_V_FLAG_INHIBIT_ANY;
230 if (!(ex_flags & EXFLAG_SI))
232 if ((cache->any_skip >= 0) && (cache->any_skip < any_skip))
233 any_skip = cache->any_skip;
237 level->flags |= X509_V_FLAG_INHIBIT_MAP;
239 if (!(ex_flags & EXFLAG_SI))
241 if ((cache->map_skip >= 0) && (cache->map_skip < map_skip))
242 map_skip = cache->map_skip;
250 X509_policy_tree_free(tree);
251 return X509_PCY_TREE_INTERNAL;
255 * Return value: 1 on success, 0 otherwise
257 static int tree_link_matching_nodes(X509_POLICY_LEVEL *curr,
258 X509_POLICY_DATA *data,
259 X509_POLICY_TREE *tree)
261 X509_POLICY_LEVEL *last = curr - 1;
264 /* Iterate through all in nodes linking matches */
265 for (i = 0; i < sk_X509_POLICY_NODE_num(last->nodes); i++) {
266 X509_POLICY_NODE *node = sk_X509_POLICY_NODE_value(last->nodes, i);
268 if (ossl_policy_node_match(last, node, data->valid_policy)) {
269 if (ossl_policy_level_add_node(curr, data, node, tree, 0) == NULL)
274 if (!matched && last->anyPolicy) {
275 if (ossl_policy_level_add_node(curr, data, last->anyPolicy, tree, 0) == NULL)
282 * This corresponds to RFC3280 6.1.3(d)(1): link any data from
283 * CertificatePolicies onto matching parent or anyPolicy if no match.
285 * Return value: 1 on success, 0 otherwise.
287 static int tree_link_nodes(X509_POLICY_LEVEL *curr,
288 const X509_POLICY_CACHE *cache,
289 X509_POLICY_TREE *tree)
293 for (i = 0; i < sk_X509_POLICY_DATA_num(cache->data); i++) {
294 X509_POLICY_DATA *data = sk_X509_POLICY_DATA_value(cache->data, i);
296 /* Look for matching nodes in previous level */
297 if (!tree_link_matching_nodes(curr, data, tree))
304 * This corresponds to RFC3280 6.1.3(d)(2): Create new data for any unmatched
305 * policies in the parent and link to anyPolicy.
307 * Return value: 1 on success, 0 otherwise.
309 static int tree_add_unmatched(X509_POLICY_LEVEL *curr,
310 const X509_POLICY_CACHE *cache,
311 const ASN1_OBJECT *id,
312 X509_POLICY_NODE *node, X509_POLICY_TREE *tree)
314 X509_POLICY_DATA *data;
317 id = node->data->valid_policy;
319 * Create a new node with qualifiers from anyPolicy and id from unmatched
322 if ((data = ossl_policy_data_new(NULL, id, node_critical(node))) == NULL)
325 /* Curr may not have anyPolicy */
326 data->qualifier_set = cache->anyPolicy->qualifier_set;
327 data->flags |= POLICY_DATA_FLAG_SHARED_QUALIFIERS;
328 if (ossl_policy_level_add_node(curr, data, node, tree, 1) == NULL) {
329 ossl_policy_data_free(data);
336 * Return value: 1 on success, 0 otherwise.
338 static int tree_link_unmatched(X509_POLICY_LEVEL *curr,
339 const X509_POLICY_CACHE *cache,
340 X509_POLICY_NODE *node, X509_POLICY_TREE *tree)
342 const X509_POLICY_LEVEL *last = curr - 1;
345 if ((last->flags & X509_V_FLAG_INHIBIT_MAP)
346 || !(node->data->flags & POLICY_DATA_FLAG_MAPPED)) {
347 /* If no policy mapping: matched if one child present */
350 if (!tree_add_unmatched(curr, cache, NULL, node, tree))
354 /* If mapping: matched if one child per expected policy set */
355 STACK_OF(ASN1_OBJECT) *expset = node->data->expected_policy_set;
356 if (node->nchild == sk_ASN1_OBJECT_num(expset))
358 /* Locate unmatched nodes */
359 for (i = 0; i < sk_ASN1_OBJECT_num(expset); i++) {
360 ASN1_OBJECT *oid = sk_ASN1_OBJECT_value(expset, i);
361 if (ossl_policy_level_find_node(curr, node, oid))
363 if (!tree_add_unmatched(curr, cache, oid, node, tree))
372 * Return value: 1 on success, 0 otherwise
374 static int tree_link_any(X509_POLICY_LEVEL *curr,
375 const X509_POLICY_CACHE *cache,
376 X509_POLICY_TREE *tree)
379 X509_POLICY_NODE *node;
380 X509_POLICY_LEVEL *last = curr - 1;
382 for (i = 0; i < sk_X509_POLICY_NODE_num(last->nodes); i++) {
383 node = sk_X509_POLICY_NODE_value(last->nodes, i);
385 if (!tree_link_unmatched(curr, cache, node, tree))
388 /* Finally add link to anyPolicy */
389 if (last->anyPolicy &&
390 ossl_policy_level_add_node(curr, cache->anyPolicy,
391 last->anyPolicy, tree, 0) == NULL)
397 * Prune the tree: delete any child mapped child data on the current level then
398 * proceed up the tree deleting any data with no children. If we ever have no
399 * data on a level we can halt because the tree will be empty.
401 * Return value: <= 0 error, otherwise one of:
403 * X509_PCY_TREE_VALID: valid tree
404 * X509_PCY_TREE_EMPTY: empty tree
406 static int tree_prune(X509_POLICY_TREE *tree, X509_POLICY_LEVEL *curr)
408 STACK_OF(X509_POLICY_NODE) *nodes;
409 X509_POLICY_NODE *node;
412 if (curr->flags & X509_V_FLAG_INHIBIT_MAP) {
413 for (i = sk_X509_POLICY_NODE_num(nodes) - 1; i >= 0; i--) {
414 node = sk_X509_POLICY_NODE_value(nodes, i);
415 /* Delete any mapped data: see RFC3280 XXXX */
416 if (node->data->flags & POLICY_DATA_FLAG_MAP_MASK) {
417 node->parent->nchild--;
419 (void)sk_X509_POLICY_NODE_delete(nodes, i);
427 for (i = sk_X509_POLICY_NODE_num(nodes) - 1; i >= 0; i--) {
428 node = sk_X509_POLICY_NODE_value(nodes, i);
429 if (node->nchild == 0) {
430 node->parent->nchild--;
432 (void)sk_X509_POLICY_NODE_delete(nodes, i);
435 if (curr->anyPolicy && !curr->anyPolicy->nchild) {
436 if (curr->anyPolicy->parent)
437 curr->anyPolicy->parent->nchild--;
438 OPENSSL_free(curr->anyPolicy);
439 curr->anyPolicy = NULL;
441 if (curr == tree->levels) {
442 /* If we zapped anyPolicy at top then tree is empty */
443 if (!curr->anyPolicy)
444 return X509_PCY_TREE_EMPTY;
448 return X509_PCY_TREE_VALID;
452 * Return value: 1 on success, 0 otherwise.
454 static int tree_add_auth_node(STACK_OF(X509_POLICY_NODE) **pnodes,
455 X509_POLICY_NODE *pcy)
457 if (*pnodes == NULL &&
458 (*pnodes = ossl_policy_node_cmp_new()) == NULL)
460 if (sk_X509_POLICY_NODE_find(*pnodes, pcy) >= 0)
462 return sk_X509_POLICY_NODE_push(*pnodes, pcy) != 0;
465 #define TREE_CALC_FAILURE 0
466 #define TREE_CALC_OK_NOFREE 1
467 #define TREE_CALC_OK_DOFREE 2
470 * Calculate the authority set based on policy tree. The 'pnodes' parameter is
471 * used as a store for the set of policy nodes used to calculate the user set.
472 * If the authority set is not anyPolicy then pnodes will just point to the
473 * authority set. If however the authority set is anyPolicy then the set of
474 * valid policies (other than anyPolicy) is store in pnodes.
477 * TREE_CALC_FAILURE on failure,
478 * TREE_CALC_OK_NOFREE on success and pnodes need not be freed,
479 * TREE_CALC_OK_DOFREE on success and pnodes needs to be freed
481 static int tree_calculate_authority_set(X509_POLICY_TREE *tree,
482 STACK_OF(X509_POLICY_NODE) **pnodes)
484 X509_POLICY_LEVEL *curr;
485 X509_POLICY_NODE *node, *anyptr;
486 STACK_OF(X509_POLICY_NODE) **addnodes;
488 curr = tree->levels + tree->nlevel - 1;
490 /* If last level contains anyPolicy set is anyPolicy */
491 if (curr->anyPolicy) {
492 if (!tree_add_auth_node(&tree->auth_policies, curr->anyPolicy))
493 return TREE_CALC_FAILURE;
496 /* Add policies to authority set */
497 addnodes = &tree->auth_policies;
500 for (i = 1; i < tree->nlevel; i++) {
502 * If no anyPolicy node on this level it can't appear on lower
503 * levels so end search.
505 if ((anyptr = curr->anyPolicy) == NULL)
508 for (j = 0; j < sk_X509_POLICY_NODE_num(curr->nodes); j++) {
509 node = sk_X509_POLICY_NODE_value(curr->nodes, j);
510 if ((node->parent == anyptr)
511 && !tree_add_auth_node(addnodes, node)) {
512 if (addnodes == pnodes) {
513 sk_X509_POLICY_NODE_free(*pnodes);
516 return TREE_CALC_FAILURE;
520 if (addnodes == pnodes)
521 return TREE_CALC_OK_DOFREE;
523 *pnodes = tree->auth_policies;
524 return TREE_CALC_OK_NOFREE;
528 * Return value: 1 on success, 0 otherwise.
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)
535 X509_POLICY_NODE *node;
537 X509_POLICY_NODE *anyPolicy;
538 X509_POLICY_DATA *extra;
541 * Check if anyPolicy present in authority constrained policy set: this
542 * will happen if it is a leaf node.
544 if (sk_ASN1_OBJECT_num(policy_oids) <= 0)
547 anyPolicy = tree->levels[tree->nlevel - 1].anyPolicy;
549 for (i = 0; i < sk_ASN1_OBJECT_num(policy_oids); i++) {
550 oid = sk_ASN1_OBJECT_value(policy_oids, i);
551 if (OBJ_obj2nid(oid) == NID_any_policy) {
552 tree->flags |= POLICY_FLAG_ANY_POLICY;
557 for (i = 0; i < sk_ASN1_OBJECT_num(policy_oids); i++) {
558 oid = sk_ASN1_OBJECT_value(policy_oids, i);
559 node = ossl_policy_tree_find_sk(auth_nodes, oid);
564 * Create a new node with policy ID from user set and qualifiers
567 extra = ossl_policy_data_new(NULL, oid, node_critical(anyPolicy));
570 extra->qualifier_set = anyPolicy->data->qualifier_set;
571 extra->flags = POLICY_DATA_FLAG_SHARED_QUALIFIERS
572 | POLICY_DATA_FLAG_EXTRA_NODE;
573 node = ossl_policy_level_add_node(NULL, extra, anyPolicy->parent,
576 ossl_policy_data_free(extra);
580 if (!tree->user_policies) {
581 tree->user_policies = sk_X509_POLICY_NODE_new_null();
582 if (!tree->user_policies) {
587 if (!sk_X509_POLICY_NODE_push(tree->user_policies, node)) {
596 * Return value: <= 0 error, otherwise one of:
597 * X509_PCY_TREE_VALID: valid tree
598 * X509_PCY_TREE_EMPTY: empty tree
599 * (see tree_prune()).
601 static int tree_evaluate(X509_POLICY_TREE *tree)
604 X509_POLICY_LEVEL *curr = tree->levels + 1;
605 const X509_POLICY_CACHE *cache;
607 for (i = 1; i < tree->nlevel; i++, curr++) {
608 cache = ossl_policy_cache_set(curr->cert);
609 if (!tree_link_nodes(curr, cache, tree))
610 return X509_PCY_TREE_INTERNAL;
612 if (!(curr->flags & X509_V_FLAG_INHIBIT_ANY)
613 && !tree_link_any(curr, cache, tree))
614 return X509_PCY_TREE_INTERNAL;
615 TREE_PRINT("before tree_prune()", tree, curr);
616 ret = tree_prune(tree, curr);
617 if (ret != X509_PCY_TREE_VALID)
620 return X509_PCY_TREE_VALID;
623 static void exnode_free(X509_POLICY_NODE *node)
625 if (node->data && (node->data->flags & POLICY_DATA_FLAG_EXTRA_NODE))
629 void X509_policy_tree_free(X509_POLICY_TREE *tree)
631 X509_POLICY_LEVEL *curr;
637 sk_X509_POLICY_NODE_free(tree->auth_policies);
638 sk_X509_POLICY_NODE_pop_free(tree->user_policies, exnode_free);
640 for (i = 0, curr = tree->levels; i < tree->nlevel; i++, curr++) {
641 X509_free(curr->cert);
642 sk_X509_POLICY_NODE_pop_free(curr->nodes, ossl_policy_node_free);
643 ossl_policy_node_free(curr->anyPolicy);
646 sk_X509_POLICY_DATA_pop_free(tree->extra_data, ossl_policy_data_free);
647 OPENSSL_free(tree->levels);
653 * Application policy checking function.
655 * X509_PCY_TREE_FAILURE: Failure to satisfy explicit policy
656 * X509_PCY_TREE_INVALID: Inconsistent or invalid extensions
657 * X509_PCY_TREE_INTERNAL: Internal error, most likely malloc
658 * X509_PCY_TREE_VALID: Success (null tree if empty or bare TA)
660 int X509_policy_check(X509_POLICY_TREE **ptree, int *pexplicit_policy,
661 STACK_OF(X509) *certs,
662 STACK_OF(ASN1_OBJECT) *policy_oids, unsigned int flags)
667 X509_POLICY_TREE *tree = NULL;
668 STACK_OF(X509_POLICY_NODE) *nodes, *auth_nodes = NULL;
671 *pexplicit_policy = 0;
672 init_ret = tree_init(&tree, certs, flags);
677 if ((init_ret & X509_PCY_TREE_EXPLICIT) == 0) {
678 if (init_ret & X509_PCY_TREE_EMPTY) {
679 X509_policy_tree_free(tree);
680 return X509_PCY_TREE_VALID;
683 *pexplicit_policy = 1;
684 /* Tree empty and requireExplicit True: Error */
685 if (init_ret & X509_PCY_TREE_EMPTY)
686 return X509_PCY_TREE_FAILURE;
689 ret = tree_evaluate(tree);
690 TREE_PRINT("tree_evaluate()", tree, NULL);
694 if (ret == X509_PCY_TREE_EMPTY) {
695 X509_policy_tree_free(tree);
696 if (init_ret & X509_PCY_TREE_EXPLICIT)
697 return X509_PCY_TREE_FAILURE;
698 return X509_PCY_TREE_VALID;
701 /* Tree is not empty: continue */
703 if ((calc_ret = tree_calculate_authority_set(tree, &auth_nodes)) == 0)
705 ret = tree_calculate_user_set(tree, policy_oids, auth_nodes);
706 if (calc_ret == TREE_CALC_OK_DOFREE)
707 sk_X509_POLICY_NODE_free(auth_nodes);
713 if (init_ret & X509_PCY_TREE_EXPLICIT) {
714 nodes = X509_policy_tree_get0_user_policies(tree);
715 if (sk_X509_POLICY_NODE_num(nodes) <= 0)
716 return X509_PCY_TREE_FAILURE;
718 return X509_PCY_TREE_VALID;
721 X509_policy_tree_free(tree);
722 return X509_PCY_TREE_INTERNAL;