2 * Copyright 2004-2020 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 DEFINE_STACK_OF(ASN1_OBJECT)
19 DEFINE_STACK_OF(X509_POLICY_NODE)
21 static void expected_print(BIO *channel,
22 X509_POLICY_LEVEL *lev, X509_POLICY_NODE *node,
25 if ((lev->flags & X509_V_FLAG_INHIBIT_MAP)
26 || !(node->data->flags & POLICY_DATA_FLAG_MAP_MASK))
27 BIO_puts(channel, " Not Mapped\n");
31 STACK_OF(ASN1_OBJECT) *pset = node->data->expected_policy_set;
33 BIO_puts(channel, " Expected: ");
34 for (i = 0; i < sk_ASN1_OBJECT_num(pset); i++) {
35 oid = sk_ASN1_OBJECT_value(pset, i);
37 BIO_puts(channel, ", ");
38 i2a_ASN1_OBJECT(channel, oid);
40 BIO_puts(channel, "\n");
44 static void tree_print(BIO *channel,
45 char *str, X509_POLICY_TREE *tree,
46 X509_POLICY_LEVEL *curr)
48 X509_POLICY_LEVEL *plev;
51 curr = tree->levels + tree->nlevel;
55 BIO_printf(channel, "Level print after %s\n", str);
56 BIO_printf(channel, "Printing Up to Level %ld\n",
57 (long)(curr - tree->levels));
58 for (plev = tree->levels; plev != curr; plev++) {
61 BIO_printf(channel, "Level %ld, flags = %x\n",
62 (long)(plev - tree->levels), plev->flags);
63 for (i = 0; i < sk_X509_POLICY_NODE_num(plev->nodes); i++) {
64 X509_POLICY_NODE *node =
65 sk_X509_POLICY_NODE_value(plev->nodes, i);
67 X509_POLICY_NODE_print(channel, node, 2);
68 expected_print(channel, plev, node, 2);
69 BIO_printf(channel, " Flags: %x\n", node->data->flags);
72 X509_POLICY_NODE_print(channel, plev->anyPolicy, 2);
76 #define TREE_PRINT(str, tree, curr) \
77 OSSL_TRACE_BEGIN(X509V3_POLICY) { \
78 tree_print(trc_out, "before tree_prune()", tree, curr); \
79 } OSSL_TRACE_END(X509V3_POLICY)
82 * Return value: <= 0 on error, or positive bit mask:
84 * X509_PCY_TREE_VALID: valid tree
85 * X509_PCY_TREE_EMPTY: empty tree (including bare TA case)
86 * X509_PCY_TREE_EXPLICIT: explicit policy required
88 static int tree_init(X509_POLICY_TREE **ptree, STACK_OF(X509) *certs,
91 X509_POLICY_TREE *tree;
92 X509_POLICY_LEVEL *level;
93 const X509_POLICY_CACHE *cache;
94 X509_POLICY_DATA *data = NULL;
95 int ret = X509_PCY_TREE_VALID;
96 int n = sk_X509_num(certs) - 1; /* RFC5280 paths omit the TA */
97 int explicit_policy = (flags & X509_V_FLAG_EXPLICIT_POLICY) ? 0 : n+1;
98 int any_skip = (flags & X509_V_FLAG_INHIBIT_ANY) ? 0 : n+1;
99 int map_skip = (flags & X509_V_FLAG_INHIBIT_MAP) ? 0 : n+1;
104 /* Can't do anything with just a trust anchor */
106 return X509_PCY_TREE_EMPTY;
109 * First setup the policy cache in all n non-TA certificates, this will be
110 * used in X509_verify_cert() which will invoke the verify callback for all
111 * certificates with invalid policy extensions.
113 for (i = n - 1; i >= 0; i--) {
114 X509 *x = sk_X509_value(certs, i);
116 /* Call for side-effect of computing hash and caching extensions */
117 X509_check_purpose(x, -1, 0);
119 /* If cache is NULL, likely ENOMEM: return immediately */
120 if (policy_cache_set(x) == NULL)
121 return X509_PCY_TREE_INTERNAL;
125 * At this point check for invalid policies and required explicit policy.
126 * Note that the explicit_policy counter is a count-down to zero, with the
127 * requirement kicking in if and once it does that. The counter is
128 * decremented for every non-self-issued certificate in the path, but may
129 * be further reduced by policy constraints in a non-leaf certificate.
131 * The ultimate policy set is the intersection of all the policies along
132 * the path, if we hit a certificate with an empty policy set, and explicit
133 * policy is required we're done.
136 i >= 0 && (explicit_policy > 0 || (ret & X509_PCY_TREE_EMPTY) == 0);
138 X509 *x = sk_X509_value(certs, i);
139 uint32_t ex_flags = X509_get_extension_flags(x);
141 /* All the policies are already cached, we can return early */
142 if (ex_flags & EXFLAG_INVALID_POLICY)
143 return X509_PCY_TREE_INVALID;
145 /* Access the cache which we now know exists */
146 cache = policy_cache_set(x);
148 if ((ret & X509_PCY_TREE_VALID) && cache->data == NULL)
149 ret = X509_PCY_TREE_EMPTY;
150 if (explicit_policy > 0) {
151 if (!(ex_flags & EXFLAG_SI))
153 if ((cache->explicit_skip >= 0)
154 && (cache->explicit_skip < explicit_policy))
155 explicit_policy = cache->explicit_skip;
159 if (explicit_policy == 0)
160 ret |= X509_PCY_TREE_EXPLICIT;
161 if ((ret & X509_PCY_TREE_VALID) == 0)
164 /* If we get this far initialize the tree */
165 if ((tree = OPENSSL_zalloc(sizeof(*tree))) == NULL) {
166 X509V3err(X509V3_F_TREE_INIT, ERR_R_MALLOC_FAILURE);
167 return X509_PCY_TREE_INTERNAL;
171 * http://tools.ietf.org/html/rfc5280#section-6.1.2, figure 3.
173 * The top level is implicitly for the trust anchor with valid expected
174 * policies of anyPolicy. (RFC 5280 has the TA at depth 0 and the leaf at
175 * depth n, we have the leaf at depth 0 and the TA at depth n).
177 if ((tree->levels = OPENSSL_zalloc(sizeof(*tree->levels)*(n+1))) == NULL) {
179 X509V3err(X509V3_F_TREE_INIT, ERR_R_MALLOC_FAILURE);
180 return X509_PCY_TREE_INTERNAL;
183 level = tree->levels;
184 if ((data = policy_data_new(NULL, OBJ_nid2obj(NID_any_policy), 0)) == NULL)
186 if (level_add_node(level, data, NULL, tree) == NULL) {
187 policy_data_free(data);
192 * In this pass initialize all the tree levels and whether anyPolicy and
193 * policy mapping are inhibited at each level.
195 for (i = n - 1; i >= 0; i--) {
196 X509 *x = sk_X509_value(certs, i);
197 uint32_t ex_flags = X509_get_extension_flags(x);
199 /* Access the cache which we now know exists */
200 cache = policy_cache_set(x);
205 if (!cache->anyPolicy)
206 level->flags |= X509_V_FLAG_INHIBIT_ANY;
208 /* Determine inhibit any and inhibit map flags */
211 * Any matching allowed only if certificate is self issued and not
212 * the last in the chain.
214 if (!(ex_flags & EXFLAG_SI) || (i == 0))
215 level->flags |= X509_V_FLAG_INHIBIT_ANY;
217 if (!(ex_flags & EXFLAG_SI))
219 if ((cache->any_skip >= 0) && (cache->any_skip < any_skip))
220 any_skip = cache->any_skip;
224 level->flags |= X509_V_FLAG_INHIBIT_MAP;
226 if (!(ex_flags & EXFLAG_SI))
228 if ((cache->map_skip >= 0) && (cache->map_skip < map_skip))
229 map_skip = cache->map_skip;
237 X509_policy_tree_free(tree);
238 return X509_PCY_TREE_INTERNAL;
242 * Return value: 1 on success, 0 otherwise
244 static int tree_link_matching_nodes(X509_POLICY_LEVEL *curr,
245 X509_POLICY_DATA *data)
247 X509_POLICY_LEVEL *last = curr - 1;
250 /* Iterate through all in nodes linking matches */
251 for (i = 0; i < sk_X509_POLICY_NODE_num(last->nodes); i++) {
252 X509_POLICY_NODE *node = sk_X509_POLICY_NODE_value(last->nodes, i);
254 if (policy_node_match(last, node, data->valid_policy)) {
255 if (level_add_node(curr, data, node, NULL) == NULL)
260 if (!matched && last->anyPolicy) {
261 if (level_add_node(curr, data, last->anyPolicy, NULL) == NULL)
268 * This corresponds to RFC3280 6.1.3(d)(1): link any data from
269 * CertificatePolicies onto matching parent or anyPolicy if no match.
271 * Return value: 1 on success, 0 otherwise.
273 static int tree_link_nodes(X509_POLICY_LEVEL *curr,
274 const X509_POLICY_CACHE *cache)
278 for (i = 0; i < sk_X509_POLICY_DATA_num(cache->data); i++) {
279 X509_POLICY_DATA *data = sk_X509_POLICY_DATA_value(cache->data, i);
281 /* Look for matching nodes in previous level */
282 if (!tree_link_matching_nodes(curr, data))
289 * This corresponds to RFC3280 6.1.3(d)(2): Create new data for any unmatched
290 * policies in the parent and link to anyPolicy.
292 * Return value: 1 on success, 0 otherwise.
294 static int tree_add_unmatched(X509_POLICY_LEVEL *curr,
295 const X509_POLICY_CACHE *cache,
296 const ASN1_OBJECT *id,
297 X509_POLICY_NODE *node, X509_POLICY_TREE *tree)
299 X509_POLICY_DATA *data;
302 id = node->data->valid_policy;
304 * Create a new node with qualifiers from anyPolicy and id from unmatched
307 if ((data = policy_data_new(NULL, id, node_critical(node))) == NULL)
310 /* Curr may not have anyPolicy */
311 data->qualifier_set = cache->anyPolicy->qualifier_set;
312 data->flags |= POLICY_DATA_FLAG_SHARED_QUALIFIERS;
313 if (level_add_node(curr, data, node, tree) == NULL) {
314 policy_data_free(data);
321 * Return value: 1 on success, 0 otherwise.
323 static int tree_link_unmatched(X509_POLICY_LEVEL *curr,
324 const X509_POLICY_CACHE *cache,
325 X509_POLICY_NODE *node, X509_POLICY_TREE *tree)
327 const X509_POLICY_LEVEL *last = curr - 1;
330 if ((last->flags & X509_V_FLAG_INHIBIT_MAP)
331 || !(node->data->flags & POLICY_DATA_FLAG_MAPPED)) {
332 /* If no policy mapping: matched if one child present */
335 if (!tree_add_unmatched(curr, cache, NULL, node, tree))
339 /* If mapping: matched if one child per expected policy set */
340 STACK_OF(ASN1_OBJECT) *expset = node->data->expected_policy_set;
341 if (node->nchild == sk_ASN1_OBJECT_num(expset))
343 /* Locate unmatched nodes */
344 for (i = 0; i < sk_ASN1_OBJECT_num(expset); i++) {
345 ASN1_OBJECT *oid = sk_ASN1_OBJECT_value(expset, i);
346 if (level_find_node(curr, node, oid))
348 if (!tree_add_unmatched(curr, cache, oid, node, tree))
357 * Return value: 1 on success, 0 otherwise
359 static int tree_link_any(X509_POLICY_LEVEL *curr,
360 const X509_POLICY_CACHE *cache,
361 X509_POLICY_TREE *tree)
364 X509_POLICY_NODE *node;
365 X509_POLICY_LEVEL *last = curr - 1;
367 for (i = 0; i < sk_X509_POLICY_NODE_num(last->nodes); i++) {
368 node = sk_X509_POLICY_NODE_value(last->nodes, i);
370 if (!tree_link_unmatched(curr, cache, node, tree))
373 /* Finally add link to anyPolicy */
374 if (last->anyPolicy &&
375 level_add_node(curr, cache->anyPolicy, last->anyPolicy, NULL) == NULL)
381 * Prune the tree: delete any child mapped child data on the current level then
382 * proceed up the tree deleting any data with no children. If we ever have no
383 * data on a level we can halt because the tree will be empty.
385 * Return value: <= 0 error, otherwise one of:
387 * X509_PCY_TREE_VALID: valid tree
388 * X509_PCY_TREE_EMPTY: empty tree
390 static int tree_prune(X509_POLICY_TREE *tree, X509_POLICY_LEVEL *curr)
392 STACK_OF(X509_POLICY_NODE) *nodes;
393 X509_POLICY_NODE *node;
396 if (curr->flags & X509_V_FLAG_INHIBIT_MAP) {
397 for (i = sk_X509_POLICY_NODE_num(nodes) - 1; i >= 0; i--) {
398 node = sk_X509_POLICY_NODE_value(nodes, i);
399 /* Delete any mapped data: see RFC3280 XXXX */
400 if (node->data->flags & POLICY_DATA_FLAG_MAP_MASK) {
401 node->parent->nchild--;
403 (void)sk_X509_POLICY_NODE_delete(nodes, i);
411 for (i = sk_X509_POLICY_NODE_num(nodes) - 1; i >= 0; i--) {
412 node = sk_X509_POLICY_NODE_value(nodes, i);
413 if (node->nchild == 0) {
414 node->parent->nchild--;
416 (void)sk_X509_POLICY_NODE_delete(nodes, i);
419 if (curr->anyPolicy && !curr->anyPolicy->nchild) {
420 if (curr->anyPolicy->parent)
421 curr->anyPolicy->parent->nchild--;
422 OPENSSL_free(curr->anyPolicy);
423 curr->anyPolicy = NULL;
425 if (curr == tree->levels) {
426 /* If we zapped anyPolicy at top then tree is empty */
427 if (!curr->anyPolicy)
428 return X509_PCY_TREE_EMPTY;
432 return X509_PCY_TREE_VALID;
436 * Return value: 1 on success, 0 otherwise.
438 static int tree_add_auth_node(STACK_OF(X509_POLICY_NODE) **pnodes,
439 X509_POLICY_NODE *pcy)
441 if (*pnodes == NULL &&
442 (*pnodes = policy_node_cmp_new()) == NULL)
444 if (sk_X509_POLICY_NODE_find(*pnodes, pcy) >= 0)
446 return sk_X509_POLICY_NODE_push(*pnodes, pcy) != 0;
449 #define TREE_CALC_FAILURE 0
450 #define TREE_CALC_OK_NOFREE 1
451 #define TREE_CALC_OK_DOFREE 2
454 * Calculate the authority set based on policy tree. The 'pnodes' parameter is
455 * used as a store for the set of policy nodes used to calculate the user set.
456 * If the authority set is not anyPolicy then pnodes will just point to the
457 * authority set. If however the authority set is anyPolicy then the set of
458 * valid policies (other than anyPolicy) is store in pnodes.
461 * TREE_CALC_FAILURE on failure,
462 * TREE_CALC_OK_NOFREE on success and pnodes need not be freed,
463 * TREE_CALC_OK_DOFREE on success and pnodes needs to be freed
465 static int tree_calculate_authority_set(X509_POLICY_TREE *tree,
466 STACK_OF(X509_POLICY_NODE) **pnodes)
468 X509_POLICY_LEVEL *curr;
469 X509_POLICY_NODE *node, *anyptr;
470 STACK_OF(X509_POLICY_NODE) **addnodes;
472 curr = tree->levels + tree->nlevel - 1;
474 /* If last level contains anyPolicy set is anyPolicy */
475 if (curr->anyPolicy) {
476 if (!tree_add_auth_node(&tree->auth_policies, curr->anyPolicy))
477 return TREE_CALC_FAILURE;
480 /* Add policies to authority set */
481 addnodes = &tree->auth_policies;
484 for (i = 1; i < tree->nlevel; i++) {
486 * If no anyPolicy node on this this level it can't appear on lower
487 * levels so end search.
489 if ((anyptr = curr->anyPolicy) == NULL)
492 for (j = 0; j < sk_X509_POLICY_NODE_num(curr->nodes); j++) {
493 node = sk_X509_POLICY_NODE_value(curr->nodes, j);
494 if ((node->parent == anyptr)
495 && !tree_add_auth_node(addnodes, node)) {
496 if (addnodes == pnodes) {
497 sk_X509_POLICY_NODE_free(*pnodes);
500 return TREE_CALC_FAILURE;
504 if (addnodes == pnodes)
505 return TREE_CALC_OK_DOFREE;
507 *pnodes = tree->auth_policies;
508 return TREE_CALC_OK_NOFREE;
512 * Return value: 1 on success, 0 otherwise.
514 static int tree_calculate_user_set(X509_POLICY_TREE *tree,
515 STACK_OF(ASN1_OBJECT) *policy_oids,
516 STACK_OF(X509_POLICY_NODE) *auth_nodes)
519 X509_POLICY_NODE *node;
521 X509_POLICY_NODE *anyPolicy;
522 X509_POLICY_DATA *extra;
525 * Check if anyPolicy present in authority constrained policy set: this
526 * will happen if it is a leaf node.
528 if (sk_ASN1_OBJECT_num(policy_oids) <= 0)
531 anyPolicy = tree->levels[tree->nlevel - 1].anyPolicy;
533 for (i = 0; i < sk_ASN1_OBJECT_num(policy_oids); i++) {
534 oid = sk_ASN1_OBJECT_value(policy_oids, i);
535 if (OBJ_obj2nid(oid) == NID_any_policy) {
536 tree->flags |= POLICY_FLAG_ANY_POLICY;
541 for (i = 0; i < sk_ASN1_OBJECT_num(policy_oids); i++) {
542 oid = sk_ASN1_OBJECT_value(policy_oids, i);
543 node = tree_find_sk(auth_nodes, oid);
548 * Create a new node with policy ID from user set and qualifiers
551 extra = policy_data_new(NULL, oid, node_critical(anyPolicy));
554 extra->qualifier_set = anyPolicy->data->qualifier_set;
555 extra->flags = POLICY_DATA_FLAG_SHARED_QUALIFIERS
556 | POLICY_DATA_FLAG_EXTRA_NODE;
557 node = level_add_node(NULL, extra, anyPolicy->parent, tree);
559 if (!tree->user_policies) {
560 tree->user_policies = sk_X509_POLICY_NODE_new_null();
561 if (!tree->user_policies)
564 if (!sk_X509_POLICY_NODE_push(tree->user_policies, node))
571 * Return value: <= 0 error, otherwise one of:
572 * X509_PCY_TREE_VALID: valid tree
573 * X509_PCY_TREE_EMPTY: empty tree
574 * (see tree_prune()).
576 static int tree_evaluate(X509_POLICY_TREE *tree)
579 X509_POLICY_LEVEL *curr = tree->levels + 1;
580 const X509_POLICY_CACHE *cache;
582 for (i = 1; i < tree->nlevel; i++, curr++) {
583 cache = policy_cache_set(curr->cert);
584 if (!tree_link_nodes(curr, cache))
585 return X509_PCY_TREE_INTERNAL;
587 if (!(curr->flags & X509_V_FLAG_INHIBIT_ANY)
588 && !tree_link_any(curr, cache, tree))
589 return X509_PCY_TREE_INTERNAL;
590 TREE_PRINT("before tree_prune()", tree, curr);
591 ret = tree_prune(tree, curr);
592 if (ret != X509_PCY_TREE_VALID)
595 return X509_PCY_TREE_VALID;
598 static void exnode_free(X509_POLICY_NODE *node)
600 if (node->data && (node->data->flags & POLICY_DATA_FLAG_EXTRA_NODE))
604 void X509_policy_tree_free(X509_POLICY_TREE *tree)
606 X509_POLICY_LEVEL *curr;
612 sk_X509_POLICY_NODE_free(tree->auth_policies);
613 sk_X509_POLICY_NODE_pop_free(tree->user_policies, exnode_free);
615 for (i = 0, curr = tree->levels; i < tree->nlevel; i++, curr++) {
616 X509_free(curr->cert);
617 sk_X509_POLICY_NODE_pop_free(curr->nodes, policy_node_free);
618 policy_node_free(curr->anyPolicy);
621 sk_X509_POLICY_DATA_pop_free(tree->extra_data, policy_data_free);
622 OPENSSL_free(tree->levels);
628 * Application policy checking function.
630 * X509_PCY_TREE_FAILURE: Failure to satisfy explicit policy
631 * X509_PCY_TREE_INVALID: Inconsistent or invalid extensions
632 * X509_PCY_TREE_INTERNAL: Internal error, most likely malloc
633 * X509_PCY_TREE_VALID: Success (null tree if empty or bare TA)
635 int X509_policy_check(X509_POLICY_TREE **ptree, int *pexplicit_policy,
636 STACK_OF(X509) *certs,
637 STACK_OF(ASN1_OBJECT) *policy_oids, unsigned int flags)
642 X509_POLICY_TREE *tree = NULL;
643 STACK_OF(X509_POLICY_NODE) *nodes, *auth_nodes = NULL;
646 *pexplicit_policy = 0;
647 init_ret = tree_init(&tree, certs, flags);
652 if ((init_ret & X509_PCY_TREE_EXPLICIT) == 0) {
653 if (init_ret & X509_PCY_TREE_EMPTY) {
654 X509_policy_tree_free(tree);
655 return X509_PCY_TREE_VALID;
658 *pexplicit_policy = 1;
659 /* Tree empty and requireExplicit True: Error */
660 if (init_ret & X509_PCY_TREE_EMPTY)
661 return X509_PCY_TREE_FAILURE;
664 ret = tree_evaluate(tree);
665 TREE_PRINT("tree_evaluate()", tree, NULL);
669 if (ret == X509_PCY_TREE_EMPTY) {
670 X509_policy_tree_free(tree);
671 if (init_ret & X509_PCY_TREE_EXPLICIT)
672 return X509_PCY_TREE_FAILURE;
673 return X509_PCY_TREE_VALID;
676 /* Tree is not empty: continue */
678 if ((calc_ret = tree_calculate_authority_set(tree, &auth_nodes)) == 0)
680 ret = tree_calculate_user_set(tree, policy_oids, auth_nodes);
681 if (calc_ret == TREE_CALC_OK_DOFREE)
682 sk_X509_POLICY_NODE_free(auth_nodes);
688 if (init_ret & X509_PCY_TREE_EXPLICIT) {
689 nodes = X509_policy_tree_get0_user_policies(tree);
690 if (sk_X509_POLICY_NODE_num(nodes) <= 0)
691 return X509_PCY_TREE_FAILURE;
693 return X509_PCY_TREE_VALID;
696 X509_policy_tree_free(tree);
697 return X509_PCY_TREE_INTERNAL;