2 * Copyright 2004-2021 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"
17 static void expected_print(BIO *channel,
18 X509_POLICY_LEVEL *lev, X509_POLICY_NODE *node,
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");
27 STACK_OF(ASN1_OBJECT) *pset = node->data->expected_policy_set;
29 BIO_puts(channel, " Expected: ");
30 for (i = 0; i < sk_ASN1_OBJECT_num(pset); i++) {
31 oid = sk_ASN1_OBJECT_value(pset, i);
33 BIO_puts(channel, ", ");
34 i2a_ASN1_OBJECT(channel, oid);
36 BIO_puts(channel, "\n");
40 static void tree_print(BIO *channel,
41 char *str, X509_POLICY_TREE *tree,
42 X509_POLICY_LEVEL *curr)
44 X509_POLICY_LEVEL *plev;
47 curr = tree->levels + tree->nlevel;
51 BIO_printf(channel, "Level print after %s\n", str);
52 BIO_printf(channel, "Printing Up to Level %ld\n",
53 (long)(curr - tree->levels));
54 for (plev = tree->levels; plev != curr; plev++) {
57 BIO_printf(channel, "Level %ld, flags = %x\n",
58 (long)(plev - tree->levels), plev->flags);
59 for (i = 0; i < sk_X509_POLICY_NODE_num(plev->nodes); i++) {
60 X509_POLICY_NODE *node =
61 sk_X509_POLICY_NODE_value(plev->nodes, i);
63 X509_POLICY_NODE_print(channel, node, 2);
64 expected_print(channel, plev, node, 2);
65 BIO_printf(channel, " Flags: %x\n", node->data->flags);
68 X509_POLICY_NODE_print(channel, plev->anyPolicy, 2);
72 #define TREE_PRINT(str, tree, curr) \
73 OSSL_TRACE_BEGIN(X509V3_POLICY) { \
74 tree_print(trc_out, "before tree_prune()", tree, curr); \
75 } OSSL_TRACE_END(X509V3_POLICY)
78 * Return value: <= 0 on error, or positive bit mask:
80 * X509_PCY_TREE_VALID: valid tree
81 * X509_PCY_TREE_EMPTY: empty tree (including bare TA case)
82 * X509_PCY_TREE_EXPLICIT: explicit policy required
84 static int tree_init(X509_POLICY_TREE **ptree, STACK_OF(X509) *certs,
87 X509_POLICY_TREE *tree;
88 X509_POLICY_LEVEL *level;
89 const X509_POLICY_CACHE *cache;
90 X509_POLICY_DATA *data = NULL;
91 int ret = X509_PCY_TREE_VALID;
92 int n = sk_X509_num(certs) - 1; /* RFC5280 paths omit the TA */
93 int explicit_policy = (flags & X509_V_FLAG_EXPLICIT_POLICY) ? 0 : n+1;
94 int any_skip = (flags & X509_V_FLAG_INHIBIT_ANY) ? 0 : n+1;
95 int map_skip = (flags & X509_V_FLAG_INHIBIT_MAP) ? 0 : n+1;
100 /* Can't do anything with just a trust anchor */
102 return X509_PCY_TREE_EMPTY;
105 * First setup the policy cache in all n non-TA certificates, this will be
106 * used in X509_verify_cert() which will invoke the verify callback for all
107 * certificates with invalid policy extensions.
109 for (i = n - 1; i >= 0; i--) {
110 X509 *x = sk_X509_value(certs, i);
112 /* Call for side-effect of computing hash and caching extensions */
113 X509_check_purpose(x, -1, 0);
115 /* If cache is NULL, likely ENOMEM: return immediately */
116 if (ossl_policy_cache_set(x) == NULL)
117 return X509_PCY_TREE_INTERNAL;
121 * At this point check for invalid policies and required explicit policy.
122 * Note that the explicit_policy counter is a count-down to zero, with the
123 * requirement kicking in if and once it does that. The counter is
124 * decremented for every non-self-issued certificate in the path, but may
125 * be further reduced by policy constraints in a non-leaf certificate.
127 * The ultimate policy set is the intersection of all the policies along
128 * the path, if we hit a certificate with an empty policy set, and explicit
129 * policy is required we're done.
132 i >= 0 && (explicit_policy > 0 || (ret & X509_PCY_TREE_EMPTY) == 0);
134 X509 *x = sk_X509_value(certs, i);
135 uint32_t ex_flags = X509_get_extension_flags(x);
137 /* All the policies are already cached, we can return early */
138 if (ex_flags & EXFLAG_INVALID_POLICY)
139 return X509_PCY_TREE_INVALID;
141 /* Access the cache which we now know exists */
142 cache = ossl_policy_cache_set(x);
144 if ((ret & X509_PCY_TREE_VALID) && cache->data == NULL)
145 ret = X509_PCY_TREE_EMPTY;
146 if (explicit_policy > 0) {
147 if (!(ex_flags & EXFLAG_SI))
149 if ((cache->explicit_skip >= 0)
150 && (cache->explicit_skip < explicit_policy))
151 explicit_policy = cache->explicit_skip;
155 if (explicit_policy == 0)
156 ret |= X509_PCY_TREE_EXPLICIT;
157 if ((ret & X509_PCY_TREE_VALID) == 0)
160 /* If we get this far initialize the tree */
161 if ((tree = OPENSSL_zalloc(sizeof(*tree))) == NULL)
162 return X509_PCY_TREE_INTERNAL;
165 * http://tools.ietf.org/html/rfc5280#section-6.1.2, figure 3.
167 * The top level is implicitly for the trust anchor with valid expected
168 * policies of anyPolicy. (RFC 5280 has the TA at depth 0 and the leaf at
169 * depth n, we have the leaf at depth 0 and the TA at depth n).
171 if ((tree->levels = OPENSSL_zalloc(sizeof(*tree->levels)*(n+1))) == NULL) {
173 return X509_PCY_TREE_INTERNAL;
176 level = tree->levels;
177 if ((data = ossl_policy_data_new(NULL,
178 OBJ_nid2obj(NID_any_policy), 0)) == NULL)
180 if (ossl_policy_level_add_node(level, data, NULL, tree) == NULL) {
181 ossl_policy_data_free(data);
186 * In this pass initialize all the tree levels and whether anyPolicy and
187 * policy mapping are inhibited at each level.
189 for (i = n - 1; i >= 0; i--) {
190 X509 *x = sk_X509_value(certs, i);
191 uint32_t ex_flags = X509_get_extension_flags(x);
193 /* Access the cache which we now know exists */
194 cache = ossl_policy_cache_set(x);
199 if (!cache->anyPolicy)
200 level->flags |= X509_V_FLAG_INHIBIT_ANY;
202 /* Determine inhibit any and inhibit map flags */
205 * Any matching allowed only if certificate is self issued and not
206 * the last in the chain.
208 if (!(ex_flags & EXFLAG_SI) || (i == 0))
209 level->flags |= X509_V_FLAG_INHIBIT_ANY;
211 if (!(ex_flags & EXFLAG_SI))
213 if ((cache->any_skip >= 0) && (cache->any_skip < any_skip))
214 any_skip = cache->any_skip;
218 level->flags |= X509_V_FLAG_INHIBIT_MAP;
220 if (!(ex_flags & EXFLAG_SI))
222 if ((cache->map_skip >= 0) && (cache->map_skip < map_skip))
223 map_skip = cache->map_skip;
231 X509_policy_tree_free(tree);
232 return X509_PCY_TREE_INTERNAL;
236 * Return value: 1 on success, 0 otherwise
238 static int tree_link_matching_nodes(X509_POLICY_LEVEL *curr,
239 X509_POLICY_DATA *data)
241 X509_POLICY_LEVEL *last = curr - 1;
244 /* Iterate through all in nodes linking matches */
245 for (i = 0; i < sk_X509_POLICY_NODE_num(last->nodes); i++) {
246 X509_POLICY_NODE *node = sk_X509_POLICY_NODE_value(last->nodes, i);
248 if (ossl_policy_node_match(last, node, data->valid_policy)) {
249 if (ossl_policy_level_add_node(curr, data, node, NULL) == NULL)
254 if (!matched && last->anyPolicy) {
255 if (ossl_policy_level_add_node(curr, data, last->anyPolicy, NULL) == NULL)
262 * This corresponds to RFC3280 6.1.3(d)(1): link any data from
263 * CertificatePolicies onto matching parent or anyPolicy if no match.
265 * Return value: 1 on success, 0 otherwise.
267 static int tree_link_nodes(X509_POLICY_LEVEL *curr,
268 const X509_POLICY_CACHE *cache)
272 for (i = 0; i < sk_X509_POLICY_DATA_num(cache->data); i++) {
273 X509_POLICY_DATA *data = sk_X509_POLICY_DATA_value(cache->data, i);
275 /* Look for matching nodes in previous level */
276 if (!tree_link_matching_nodes(curr, data))
283 * This corresponds to RFC3280 6.1.3(d)(2): Create new data for any unmatched
284 * policies in the parent and link to anyPolicy.
286 * Return value: 1 on success, 0 otherwise.
288 static int tree_add_unmatched(X509_POLICY_LEVEL *curr,
289 const X509_POLICY_CACHE *cache,
290 const ASN1_OBJECT *id,
291 X509_POLICY_NODE *node, X509_POLICY_TREE *tree)
293 X509_POLICY_DATA *data;
296 id = node->data->valid_policy;
298 * Create a new node with qualifiers from anyPolicy and id from unmatched
301 if ((data = ossl_policy_data_new(NULL, id, node_critical(node))) == NULL)
304 /* Curr may not have anyPolicy */
305 data->qualifier_set = cache->anyPolicy->qualifier_set;
306 data->flags |= POLICY_DATA_FLAG_SHARED_QUALIFIERS;
307 if (ossl_policy_level_add_node(curr, data, node, tree) == NULL) {
308 ossl_policy_data_free(data);
315 * Return value: 1 on success, 0 otherwise.
317 static int tree_link_unmatched(X509_POLICY_LEVEL *curr,
318 const X509_POLICY_CACHE *cache,
319 X509_POLICY_NODE *node, X509_POLICY_TREE *tree)
321 const X509_POLICY_LEVEL *last = curr - 1;
324 if ((last->flags & X509_V_FLAG_INHIBIT_MAP)
325 || !(node->data->flags & POLICY_DATA_FLAG_MAPPED)) {
326 /* If no policy mapping: matched if one child present */
329 if (!tree_add_unmatched(curr, cache, NULL, node, tree))
333 /* If mapping: matched if one child per expected policy set */
334 STACK_OF(ASN1_OBJECT) *expset = node->data->expected_policy_set;
335 if (node->nchild == sk_ASN1_OBJECT_num(expset))
337 /* Locate unmatched nodes */
338 for (i = 0; i < sk_ASN1_OBJECT_num(expset); i++) {
339 ASN1_OBJECT *oid = sk_ASN1_OBJECT_value(expset, i);
340 if (ossl_policy_level_find_node(curr, node, oid))
342 if (!tree_add_unmatched(curr, cache, oid, node, tree))
351 * Return value: 1 on success, 0 otherwise
353 static int tree_link_any(X509_POLICY_LEVEL *curr,
354 const X509_POLICY_CACHE *cache,
355 X509_POLICY_TREE *tree)
358 X509_POLICY_NODE *node;
359 X509_POLICY_LEVEL *last = curr - 1;
361 for (i = 0; i < sk_X509_POLICY_NODE_num(last->nodes); i++) {
362 node = sk_X509_POLICY_NODE_value(last->nodes, i);
364 if (!tree_link_unmatched(curr, cache, node, tree))
367 /* Finally add link to anyPolicy */
368 if (last->anyPolicy &&
369 ossl_policy_level_add_node(curr, cache->anyPolicy,
370 last->anyPolicy, NULL) == NULL)
376 * Prune the tree: delete any child mapped child data on the current level then
377 * proceed up the tree deleting any data with no children. If we ever have no
378 * data on a level we can halt because the tree will be empty.
380 * Return value: <= 0 error, otherwise one of:
382 * X509_PCY_TREE_VALID: valid tree
383 * X509_PCY_TREE_EMPTY: empty tree
385 static int tree_prune(X509_POLICY_TREE *tree, X509_POLICY_LEVEL *curr)
387 STACK_OF(X509_POLICY_NODE) *nodes;
388 X509_POLICY_NODE *node;
391 if (curr->flags & X509_V_FLAG_INHIBIT_MAP) {
392 for (i = sk_X509_POLICY_NODE_num(nodes) - 1; i >= 0; i--) {
393 node = sk_X509_POLICY_NODE_value(nodes, i);
394 /* Delete any mapped data: see RFC3280 XXXX */
395 if (node->data->flags & POLICY_DATA_FLAG_MAP_MASK) {
396 node->parent->nchild--;
398 (void)sk_X509_POLICY_NODE_delete(nodes, i);
406 for (i = sk_X509_POLICY_NODE_num(nodes) - 1; i >= 0; i--) {
407 node = sk_X509_POLICY_NODE_value(nodes, i);
408 if (node->nchild == 0) {
409 node->parent->nchild--;
411 (void)sk_X509_POLICY_NODE_delete(nodes, i);
414 if (curr->anyPolicy && !curr->anyPolicy->nchild) {
415 if (curr->anyPolicy->parent)
416 curr->anyPolicy->parent->nchild--;
417 OPENSSL_free(curr->anyPolicy);
418 curr->anyPolicy = NULL;
420 if (curr == tree->levels) {
421 /* If we zapped anyPolicy at top then tree is empty */
422 if (!curr->anyPolicy)
423 return X509_PCY_TREE_EMPTY;
427 return X509_PCY_TREE_VALID;
431 * Return value: 1 on success, 0 otherwise.
433 static int tree_add_auth_node(STACK_OF(X509_POLICY_NODE) **pnodes,
434 X509_POLICY_NODE *pcy)
436 if (*pnodes == NULL &&
437 (*pnodes = ossl_policy_node_cmp_new()) == NULL)
439 if (sk_X509_POLICY_NODE_find(*pnodes, pcy) >= 0)
441 return sk_X509_POLICY_NODE_push(*pnodes, pcy) != 0;
444 #define TREE_CALC_FAILURE 0
445 #define TREE_CALC_OK_NOFREE 1
446 #define TREE_CALC_OK_DOFREE 2
449 * Calculate the authority set based on policy tree. The 'pnodes' parameter is
450 * used as a store for the set of policy nodes used to calculate the user set.
451 * If the authority set is not anyPolicy then pnodes will just point to the
452 * authority set. If however the authority set is anyPolicy then the set of
453 * valid policies (other than anyPolicy) is store in pnodes.
456 * TREE_CALC_FAILURE on failure,
457 * TREE_CALC_OK_NOFREE on success and pnodes need not be freed,
458 * TREE_CALC_OK_DOFREE on success and pnodes needs to be freed
460 static int tree_calculate_authority_set(X509_POLICY_TREE *tree,
461 STACK_OF(X509_POLICY_NODE) **pnodes)
463 X509_POLICY_LEVEL *curr;
464 X509_POLICY_NODE *node, *anyptr;
465 STACK_OF(X509_POLICY_NODE) **addnodes;
467 curr = tree->levels + tree->nlevel - 1;
469 /* If last level contains anyPolicy set is anyPolicy */
470 if (curr->anyPolicy) {
471 if (!tree_add_auth_node(&tree->auth_policies, curr->anyPolicy))
472 return TREE_CALC_FAILURE;
475 /* Add policies to authority set */
476 addnodes = &tree->auth_policies;
479 for (i = 1; i < tree->nlevel; i++) {
481 * If no anyPolicy node on this level it can't appear on lower
482 * levels so end search.
484 if ((anyptr = curr->anyPolicy) == NULL)
487 for (j = 0; j < sk_X509_POLICY_NODE_num(curr->nodes); j++) {
488 node = sk_X509_POLICY_NODE_value(curr->nodes, j);
489 if ((node->parent == anyptr)
490 && !tree_add_auth_node(addnodes, node)) {
491 if (addnodes == pnodes) {
492 sk_X509_POLICY_NODE_free(*pnodes);
495 return TREE_CALC_FAILURE;
499 if (addnodes == pnodes)
500 return TREE_CALC_OK_DOFREE;
502 *pnodes = tree->auth_policies;
503 return TREE_CALC_OK_NOFREE;
507 * Return value: 1 on success, 0 otherwise.
509 static int tree_calculate_user_set(X509_POLICY_TREE *tree,
510 STACK_OF(ASN1_OBJECT) *policy_oids,
511 STACK_OF(X509_POLICY_NODE) *auth_nodes)
514 X509_POLICY_NODE *node;
516 X509_POLICY_NODE *anyPolicy;
517 X509_POLICY_DATA *extra;
520 * Check if anyPolicy present in authority constrained policy set: this
521 * will happen if it is a leaf node.
523 if (sk_ASN1_OBJECT_num(policy_oids) <= 0)
526 anyPolicy = tree->levels[tree->nlevel - 1].anyPolicy;
528 for (i = 0; i < sk_ASN1_OBJECT_num(policy_oids); i++) {
529 oid = sk_ASN1_OBJECT_value(policy_oids, i);
530 if (OBJ_obj2nid(oid) == NID_any_policy) {
531 tree->flags |= POLICY_FLAG_ANY_POLICY;
536 for (i = 0; i < sk_ASN1_OBJECT_num(policy_oids); i++) {
537 oid = sk_ASN1_OBJECT_value(policy_oids, i);
538 node = ossl_policy_tree_find_sk(auth_nodes, oid);
543 * Create a new node with policy ID from user set and qualifiers
546 extra = ossl_policy_data_new(NULL, oid, node_critical(anyPolicy));
549 extra->qualifier_set = anyPolicy->data->qualifier_set;
550 extra->flags = POLICY_DATA_FLAG_SHARED_QUALIFIERS
551 | POLICY_DATA_FLAG_EXTRA_NODE;
552 node = ossl_policy_level_add_node(NULL, extra, anyPolicy->parent,
555 if (!tree->user_policies) {
556 tree->user_policies = sk_X509_POLICY_NODE_new_null();
557 if (!tree->user_policies)
560 if (!sk_X509_POLICY_NODE_push(tree->user_policies, node))
567 * Return value: <= 0 error, otherwise one of:
568 * X509_PCY_TREE_VALID: valid tree
569 * X509_PCY_TREE_EMPTY: empty tree
570 * (see tree_prune()).
572 static int tree_evaluate(X509_POLICY_TREE *tree)
575 X509_POLICY_LEVEL *curr = tree->levels + 1;
576 const X509_POLICY_CACHE *cache;
578 for (i = 1; i < tree->nlevel; i++, curr++) {
579 cache = ossl_policy_cache_set(curr->cert);
580 if (!tree_link_nodes(curr, cache))
581 return X509_PCY_TREE_INTERNAL;
583 if (!(curr->flags & X509_V_FLAG_INHIBIT_ANY)
584 && !tree_link_any(curr, cache, tree))
585 return X509_PCY_TREE_INTERNAL;
586 TREE_PRINT("before tree_prune()", tree, curr);
587 ret = tree_prune(tree, curr);
588 if (ret != X509_PCY_TREE_VALID)
591 return X509_PCY_TREE_VALID;
594 static void exnode_free(X509_POLICY_NODE *node)
596 if (node->data && (node->data->flags & POLICY_DATA_FLAG_EXTRA_NODE))
600 void X509_policy_tree_free(X509_POLICY_TREE *tree)
602 X509_POLICY_LEVEL *curr;
608 sk_X509_POLICY_NODE_free(tree->auth_policies);
609 sk_X509_POLICY_NODE_pop_free(tree->user_policies, exnode_free);
611 for (i = 0, curr = tree->levels; i < tree->nlevel; i++, curr++) {
612 X509_free(curr->cert);
613 sk_X509_POLICY_NODE_pop_free(curr->nodes, ossl_policy_node_free);
614 ossl_policy_node_free(curr->anyPolicy);
617 sk_X509_POLICY_DATA_pop_free(tree->extra_data, ossl_policy_data_free);
618 OPENSSL_free(tree->levels);
624 * Application policy checking function.
626 * X509_PCY_TREE_FAILURE: Failure to satisfy explicit policy
627 * X509_PCY_TREE_INVALID: Inconsistent or invalid extensions
628 * X509_PCY_TREE_INTERNAL: Internal error, most likely malloc
629 * X509_PCY_TREE_VALID: Success (null tree if empty or bare TA)
631 int X509_policy_check(X509_POLICY_TREE **ptree, int *pexplicit_policy,
632 STACK_OF(X509) *certs,
633 STACK_OF(ASN1_OBJECT) *policy_oids, unsigned int flags)
638 X509_POLICY_TREE *tree = NULL;
639 STACK_OF(X509_POLICY_NODE) *nodes, *auth_nodes = NULL;
642 *pexplicit_policy = 0;
643 init_ret = tree_init(&tree, certs, flags);
648 if ((init_ret & X509_PCY_TREE_EXPLICIT) == 0) {
649 if (init_ret & X509_PCY_TREE_EMPTY) {
650 X509_policy_tree_free(tree);
651 return X509_PCY_TREE_VALID;
654 *pexplicit_policy = 1;
655 /* Tree empty and requireExplicit True: Error */
656 if (init_ret & X509_PCY_TREE_EMPTY)
657 return X509_PCY_TREE_FAILURE;
660 ret = tree_evaluate(tree);
661 TREE_PRINT("tree_evaluate()", tree, NULL);
665 if (ret == X509_PCY_TREE_EMPTY) {
666 X509_policy_tree_free(tree);
667 if (init_ret & X509_PCY_TREE_EXPLICIT)
668 return X509_PCY_TREE_FAILURE;
669 return X509_PCY_TREE_VALID;
672 /* Tree is not empty: continue */
674 if ((calc_ret = tree_calculate_authority_set(tree, &auth_nodes)) == 0)
676 ret = tree_calculate_user_set(tree, policy_oids, auth_nodes);
677 if (calc_ret == TREE_CALC_OK_DOFREE)
678 sk_X509_POLICY_NODE_free(auth_nodes);
684 if (init_ret & X509_PCY_TREE_EXPLICIT) {
685 nodes = X509_policy_tree_get0_user_policies(tree);
686 if (sk_X509_POLICY_NODE_num(nodes) <= 0)
687 return X509_PCY_TREE_FAILURE;
689 return X509_PCY_TREE_VALID;
692 X509_policy_tree_free(tree);
693 return X509_PCY_TREE_INTERNAL;