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)
18 DEFINE_STACK_OF(X509_POLICY_NODE)
20 static void expected_print(BIO *channel,
21 X509_POLICY_LEVEL *lev, X509_POLICY_NODE *node,
24 if ((lev->flags & X509_V_FLAG_INHIBIT_MAP)
25 || !(node->data->flags & POLICY_DATA_FLAG_MAP_MASK))
26 BIO_puts(channel, " Not Mapped\n");
30 STACK_OF(ASN1_OBJECT) *pset = node->data->expected_policy_set;
32 BIO_puts(channel, " Expected: ");
33 for (i = 0; i < sk_ASN1_OBJECT_num(pset); i++) {
34 oid = sk_ASN1_OBJECT_value(pset, i);
36 BIO_puts(channel, ", ");
37 i2a_ASN1_OBJECT(channel, oid);
39 BIO_puts(channel, "\n");
43 static void tree_print(BIO *channel,
44 char *str, X509_POLICY_TREE *tree,
45 X509_POLICY_LEVEL *curr)
47 X509_POLICY_LEVEL *plev;
50 curr = tree->levels + tree->nlevel;
54 BIO_printf(channel, "Level print after %s\n", str);
55 BIO_printf(channel, "Printing Up to Level %ld\n",
56 (long)(curr - tree->levels));
57 for (plev = tree->levels; plev != curr; plev++) {
60 BIO_printf(channel, "Level %ld, flags = %x\n",
61 (long)(plev - tree->levels), plev->flags);
62 for (i = 0; i < sk_X509_POLICY_NODE_num(plev->nodes); i++) {
63 X509_POLICY_NODE *node =
64 sk_X509_POLICY_NODE_value(plev->nodes, i);
66 X509_POLICY_NODE_print(channel, node, 2);
67 expected_print(channel, plev, node, 2);
68 BIO_printf(channel, " Flags: %x\n", node->data->flags);
71 X509_POLICY_NODE_print(channel, plev->anyPolicy, 2);
75 #define TREE_PRINT(str, tree, curr) \
76 OSSL_TRACE_BEGIN(X509V3_POLICY) { \
77 tree_print(trc_out, "before tree_prune()", tree, curr); \
78 } OSSL_TRACE_END(X509V3_POLICY)
81 * Return value: <= 0 on error, or positive bit mask:
83 * X509_PCY_TREE_VALID: valid tree
84 * X509_PCY_TREE_EMPTY: empty tree (including bare TA case)
85 * X509_PCY_TREE_EXPLICIT: explicit policy required
87 static int tree_init(X509_POLICY_TREE **ptree, STACK_OF(X509) *certs,
90 X509_POLICY_TREE *tree;
91 X509_POLICY_LEVEL *level;
92 const X509_POLICY_CACHE *cache;
93 X509_POLICY_DATA *data = NULL;
94 int ret = X509_PCY_TREE_VALID;
95 int n = sk_X509_num(certs) - 1; /* RFC5280 paths omit the TA */
96 int explicit_policy = (flags & X509_V_FLAG_EXPLICIT_POLICY) ? 0 : n+1;
97 int any_skip = (flags & X509_V_FLAG_INHIBIT_ANY) ? 0 : n+1;
98 int map_skip = (flags & X509_V_FLAG_INHIBIT_MAP) ? 0 : n+1;
103 /* Can't do anything with just a trust anchor */
105 return X509_PCY_TREE_EMPTY;
108 * First setup the policy cache in all n non-TA certificates, this will be
109 * used in X509_verify_cert() which will invoke the verify callback for all
110 * certificates with invalid policy extensions.
112 for (i = n - 1; i >= 0; i--) {
113 X509 *x = sk_X509_value(certs, i);
115 /* Call for side-effect of computing hash and caching extensions */
116 X509_check_purpose(x, -1, 0);
118 /* If cache is NULL, likely ENOMEM: return immediately */
119 if (policy_cache_set(x) == NULL)
120 return X509_PCY_TREE_INTERNAL;
124 * At this point check for invalid policies and required explicit policy.
125 * Note that the explicit_policy counter is a count-down to zero, with the
126 * requirement kicking in if and once it does that. The counter is
127 * decremented for every non-self-issued certificate in the path, but may
128 * be further reduced by policy constraints in a non-leaf certificate.
130 * The ultimate policy set is the intersection of all the policies along
131 * the path, if we hit a certificate with an empty policy set, and explicit
132 * policy is required we're done.
135 i >= 0 && (explicit_policy > 0 || (ret & X509_PCY_TREE_EMPTY) == 0);
137 X509 *x = sk_X509_value(certs, i);
138 uint32_t ex_flags = X509_get_extension_flags(x);
140 /* All the policies are already cached, we can return early */
141 if (ex_flags & EXFLAG_INVALID_POLICY)
142 return X509_PCY_TREE_INVALID;
144 /* Access the cache which we now know exists */
145 cache = policy_cache_set(x);
147 if ((ret & X509_PCY_TREE_VALID) && cache->data == NULL)
148 ret = X509_PCY_TREE_EMPTY;
149 if (explicit_policy > 0) {
150 if (!(ex_flags & EXFLAG_SI))
152 if ((cache->explicit_skip >= 0)
153 && (cache->explicit_skip < explicit_policy))
154 explicit_policy = cache->explicit_skip;
158 if (explicit_policy == 0)
159 ret |= X509_PCY_TREE_EXPLICIT;
160 if ((ret & X509_PCY_TREE_VALID) == 0)
163 /* If we get this far initialize the tree */
164 if ((tree = OPENSSL_zalloc(sizeof(*tree))) == NULL) {
165 X509V3err(X509V3_F_TREE_INIT, ERR_R_MALLOC_FAILURE);
166 return X509_PCY_TREE_INTERNAL;
170 * http://tools.ietf.org/html/rfc5280#section-6.1.2, figure 3.
172 * The top level is implicitly for the trust anchor with valid expected
173 * policies of anyPolicy. (RFC 5280 has the TA at depth 0 and the leaf at
174 * depth n, we have the leaf at depth 0 and the TA at depth n).
176 if ((tree->levels = OPENSSL_zalloc(sizeof(*tree->levels)*(n+1))) == NULL) {
178 X509V3err(X509V3_F_TREE_INIT, ERR_R_MALLOC_FAILURE);
179 return X509_PCY_TREE_INTERNAL;
182 level = tree->levels;
183 if ((data = policy_data_new(NULL, OBJ_nid2obj(NID_any_policy), 0)) == NULL)
185 if (level_add_node(level, data, NULL, tree) == NULL) {
186 policy_data_free(data);
191 * In this pass initialize all the tree levels and whether anyPolicy and
192 * policy mapping are inhibited at each level.
194 for (i = n - 1; i >= 0; i--) {
195 X509 *x = sk_X509_value(certs, i);
196 uint32_t ex_flags = X509_get_extension_flags(x);
198 /* Access the cache which we now know exists */
199 cache = policy_cache_set(x);
204 if (!cache->anyPolicy)
205 level->flags |= X509_V_FLAG_INHIBIT_ANY;
207 /* Determine inhibit any and inhibit map flags */
210 * Any matching allowed only if certificate is self issued and not
211 * the last in the chain.
213 if (!(ex_flags & EXFLAG_SI) || (i == 0))
214 level->flags |= X509_V_FLAG_INHIBIT_ANY;
216 if (!(ex_flags & EXFLAG_SI))
218 if ((cache->any_skip >= 0) && (cache->any_skip < any_skip))
219 any_skip = cache->any_skip;
223 level->flags |= X509_V_FLAG_INHIBIT_MAP;
225 if (!(ex_flags & EXFLAG_SI))
227 if ((cache->map_skip >= 0) && (cache->map_skip < map_skip))
228 map_skip = cache->map_skip;
236 X509_policy_tree_free(tree);
237 return X509_PCY_TREE_INTERNAL;
241 * Return value: 1 on success, 0 otherwise
243 static int tree_link_matching_nodes(X509_POLICY_LEVEL *curr,
244 X509_POLICY_DATA *data)
246 X509_POLICY_LEVEL *last = curr - 1;
249 /* Iterate through all in nodes linking matches */
250 for (i = 0; i < sk_X509_POLICY_NODE_num(last->nodes); i++) {
251 X509_POLICY_NODE *node = sk_X509_POLICY_NODE_value(last->nodes, i);
253 if (policy_node_match(last, node, data->valid_policy)) {
254 if (level_add_node(curr, data, node, NULL) == NULL)
259 if (!matched && last->anyPolicy) {
260 if (level_add_node(curr, data, last->anyPolicy, NULL) == NULL)
267 * This corresponds to RFC3280 6.1.3(d)(1): link any data from
268 * CertificatePolicies onto matching parent or anyPolicy if no match.
270 * Return value: 1 on success, 0 otherwise.
272 static int tree_link_nodes(X509_POLICY_LEVEL *curr,
273 const X509_POLICY_CACHE *cache)
277 for (i = 0; i < sk_X509_POLICY_DATA_num(cache->data); i++) {
278 X509_POLICY_DATA *data = sk_X509_POLICY_DATA_value(cache->data, i);
280 /* Look for matching nodes in previous level */
281 if (!tree_link_matching_nodes(curr, data))
288 * This corresponds to RFC3280 6.1.3(d)(2): Create new data for any unmatched
289 * policies in the parent and link to anyPolicy.
291 * Return value: 1 on success, 0 otherwise.
293 static int tree_add_unmatched(X509_POLICY_LEVEL *curr,
294 const X509_POLICY_CACHE *cache,
295 const ASN1_OBJECT *id,
296 X509_POLICY_NODE *node, X509_POLICY_TREE *tree)
298 X509_POLICY_DATA *data;
301 id = node->data->valid_policy;
303 * Create a new node with qualifiers from anyPolicy and id from unmatched
306 if ((data = policy_data_new(NULL, id, node_critical(node))) == NULL)
309 /* Curr may not have anyPolicy */
310 data->qualifier_set = cache->anyPolicy->qualifier_set;
311 data->flags |= POLICY_DATA_FLAG_SHARED_QUALIFIERS;
312 if (level_add_node(curr, data, node, tree) == NULL) {
313 policy_data_free(data);
320 * Return value: 1 on success, 0 otherwise.
322 static int tree_link_unmatched(X509_POLICY_LEVEL *curr,
323 const X509_POLICY_CACHE *cache,
324 X509_POLICY_NODE *node, X509_POLICY_TREE *tree)
326 const X509_POLICY_LEVEL *last = curr - 1;
329 if ((last->flags & X509_V_FLAG_INHIBIT_MAP)
330 || !(node->data->flags & POLICY_DATA_FLAG_MAPPED)) {
331 /* If no policy mapping: matched if one child present */
334 if (!tree_add_unmatched(curr, cache, NULL, node, tree))
338 /* If mapping: matched if one child per expected policy set */
339 STACK_OF(ASN1_OBJECT) *expset = node->data->expected_policy_set;
340 if (node->nchild == sk_ASN1_OBJECT_num(expset))
342 /* Locate unmatched nodes */
343 for (i = 0; i < sk_ASN1_OBJECT_num(expset); i++) {
344 ASN1_OBJECT *oid = sk_ASN1_OBJECT_value(expset, i);
345 if (level_find_node(curr, node, oid))
347 if (!tree_add_unmatched(curr, cache, oid, node, tree))
356 * Return value: 1 on success, 0 otherwise
358 static int tree_link_any(X509_POLICY_LEVEL *curr,
359 const X509_POLICY_CACHE *cache,
360 X509_POLICY_TREE *tree)
363 X509_POLICY_NODE *node;
364 X509_POLICY_LEVEL *last = curr - 1;
366 for (i = 0; i < sk_X509_POLICY_NODE_num(last->nodes); i++) {
367 node = sk_X509_POLICY_NODE_value(last->nodes, i);
369 if (!tree_link_unmatched(curr, cache, node, tree))
372 /* Finally add link to anyPolicy */
373 if (last->anyPolicy &&
374 level_add_node(curr, cache->anyPolicy, last->anyPolicy, NULL) == NULL)
380 * Prune the tree: delete any child mapped child data on the current level then
381 * proceed up the tree deleting any data with no children. If we ever have no
382 * data on a level we can halt because the tree will be empty.
384 * Return value: <= 0 error, otherwise one of:
386 * X509_PCY_TREE_VALID: valid tree
387 * X509_PCY_TREE_EMPTY: empty tree
389 static int tree_prune(X509_POLICY_TREE *tree, X509_POLICY_LEVEL *curr)
391 STACK_OF(X509_POLICY_NODE) *nodes;
392 X509_POLICY_NODE *node;
395 if (curr->flags & X509_V_FLAG_INHIBIT_MAP) {
396 for (i = sk_X509_POLICY_NODE_num(nodes) - 1; i >= 0; i--) {
397 node = sk_X509_POLICY_NODE_value(nodes, i);
398 /* Delete any mapped data: see RFC3280 XXXX */
399 if (node->data->flags & POLICY_DATA_FLAG_MAP_MASK) {
400 node->parent->nchild--;
402 (void)sk_X509_POLICY_NODE_delete(nodes, i);
410 for (i = sk_X509_POLICY_NODE_num(nodes) - 1; i >= 0; i--) {
411 node = sk_X509_POLICY_NODE_value(nodes, i);
412 if (node->nchild == 0) {
413 node->parent->nchild--;
415 (void)sk_X509_POLICY_NODE_delete(nodes, i);
418 if (curr->anyPolicy && !curr->anyPolicy->nchild) {
419 if (curr->anyPolicy->parent)
420 curr->anyPolicy->parent->nchild--;
421 OPENSSL_free(curr->anyPolicy);
422 curr->anyPolicy = NULL;
424 if (curr == tree->levels) {
425 /* If we zapped anyPolicy at top then tree is empty */
426 if (!curr->anyPolicy)
427 return X509_PCY_TREE_EMPTY;
431 return X509_PCY_TREE_VALID;
435 * Return value: 1 on success, 0 otherwise.
437 static int tree_add_auth_node(STACK_OF(X509_POLICY_NODE) **pnodes,
438 X509_POLICY_NODE *pcy)
440 if (*pnodes == NULL &&
441 (*pnodes = policy_node_cmp_new()) == NULL)
443 if (sk_X509_POLICY_NODE_find(*pnodes, pcy) >= 0)
445 return sk_X509_POLICY_NODE_push(*pnodes, pcy) != 0;
448 #define TREE_CALC_FAILURE 0
449 #define TREE_CALC_OK_NOFREE 1
450 #define TREE_CALC_OK_DOFREE 2
453 * Calculate the authority set based on policy tree. The 'pnodes' parameter is
454 * used as a store for the set of policy nodes used to calculate the user set.
455 * If the authority set is not anyPolicy then pnodes will just point to the
456 * authority set. If however the authority set is anyPolicy then the set of
457 * valid policies (other than anyPolicy) is store in pnodes.
460 * TREE_CALC_FAILURE on failure,
461 * TREE_CALC_OK_NOFREE on success and pnodes need not be freed,
462 * TREE_CALC_OK_DOFREE on success and pnodes needs to be freed
464 static int tree_calculate_authority_set(X509_POLICY_TREE *tree,
465 STACK_OF(X509_POLICY_NODE) **pnodes)
467 X509_POLICY_LEVEL *curr;
468 X509_POLICY_NODE *node, *anyptr;
469 STACK_OF(X509_POLICY_NODE) **addnodes;
471 curr = tree->levels + tree->nlevel - 1;
473 /* If last level contains anyPolicy set is anyPolicy */
474 if (curr->anyPolicy) {
475 if (!tree_add_auth_node(&tree->auth_policies, curr->anyPolicy))
476 return TREE_CALC_FAILURE;
479 /* Add policies to authority set */
480 addnodes = &tree->auth_policies;
483 for (i = 1; i < tree->nlevel; i++) {
485 * If no anyPolicy node on this this level it can't appear on lower
486 * levels so end search.
488 if ((anyptr = curr->anyPolicy) == NULL)
491 for (j = 0; j < sk_X509_POLICY_NODE_num(curr->nodes); j++) {
492 node = sk_X509_POLICY_NODE_value(curr->nodes, j);
493 if ((node->parent == anyptr)
494 && !tree_add_auth_node(addnodes, node)) {
495 if (addnodes == pnodes) {
496 sk_X509_POLICY_NODE_free(*pnodes);
499 return TREE_CALC_FAILURE;
503 if (addnodes == pnodes)
504 return TREE_CALC_OK_DOFREE;
506 *pnodes = tree->auth_policies;
507 return TREE_CALC_OK_NOFREE;
511 * Return value: 1 on success, 0 otherwise.
513 static int tree_calculate_user_set(X509_POLICY_TREE *tree,
514 STACK_OF(ASN1_OBJECT) *policy_oids,
515 STACK_OF(X509_POLICY_NODE) *auth_nodes)
518 X509_POLICY_NODE *node;
520 X509_POLICY_NODE *anyPolicy;
521 X509_POLICY_DATA *extra;
524 * Check if anyPolicy present in authority constrained policy set: this
525 * will happen if it is a leaf node.
527 if (sk_ASN1_OBJECT_num(policy_oids) <= 0)
530 anyPolicy = tree->levels[tree->nlevel - 1].anyPolicy;
532 for (i = 0; i < sk_ASN1_OBJECT_num(policy_oids); i++) {
533 oid = sk_ASN1_OBJECT_value(policy_oids, i);
534 if (OBJ_obj2nid(oid) == NID_any_policy) {
535 tree->flags |= POLICY_FLAG_ANY_POLICY;
540 for (i = 0; i < sk_ASN1_OBJECT_num(policy_oids); i++) {
541 oid = sk_ASN1_OBJECT_value(policy_oids, i);
542 node = tree_find_sk(auth_nodes, oid);
547 * Create a new node with policy ID from user set and qualifiers
550 extra = policy_data_new(NULL, oid, node_critical(anyPolicy));
553 extra->qualifier_set = anyPolicy->data->qualifier_set;
554 extra->flags = POLICY_DATA_FLAG_SHARED_QUALIFIERS
555 | POLICY_DATA_FLAG_EXTRA_NODE;
556 node = level_add_node(NULL, extra, anyPolicy->parent, tree);
558 if (!tree->user_policies) {
559 tree->user_policies = sk_X509_POLICY_NODE_new_null();
560 if (!tree->user_policies)
563 if (!sk_X509_POLICY_NODE_push(tree->user_policies, node))
570 * Return value: <= 0 error, otherwise one of:
571 * X509_PCY_TREE_VALID: valid tree
572 * X509_PCY_TREE_EMPTY: empty tree
573 * (see tree_prune()).
575 static int tree_evaluate(X509_POLICY_TREE *tree)
578 X509_POLICY_LEVEL *curr = tree->levels + 1;
579 const X509_POLICY_CACHE *cache;
581 for (i = 1; i < tree->nlevel; i++, curr++) {
582 cache = policy_cache_set(curr->cert);
583 if (!tree_link_nodes(curr, cache))
584 return X509_PCY_TREE_INTERNAL;
586 if (!(curr->flags & X509_V_FLAG_INHIBIT_ANY)
587 && !tree_link_any(curr, cache, tree))
588 return X509_PCY_TREE_INTERNAL;
589 TREE_PRINT("before tree_prune()", tree, curr);
590 ret = tree_prune(tree, curr);
591 if (ret != X509_PCY_TREE_VALID)
594 return X509_PCY_TREE_VALID;
597 static void exnode_free(X509_POLICY_NODE *node)
599 if (node->data && (node->data->flags & POLICY_DATA_FLAG_EXTRA_NODE))
603 void X509_policy_tree_free(X509_POLICY_TREE *tree)
605 X509_POLICY_LEVEL *curr;
611 sk_X509_POLICY_NODE_free(tree->auth_policies);
612 sk_X509_POLICY_NODE_pop_free(tree->user_policies, exnode_free);
614 for (i = 0, curr = tree->levels; i < tree->nlevel; i++, curr++) {
615 X509_free(curr->cert);
616 sk_X509_POLICY_NODE_pop_free(curr->nodes, policy_node_free);
617 policy_node_free(curr->anyPolicy);
620 sk_X509_POLICY_DATA_pop_free(tree->extra_data, policy_data_free);
621 OPENSSL_free(tree->levels);
627 * Application policy checking function.
629 * X509_PCY_TREE_FAILURE: Failure to satisfy explicit policy
630 * X509_PCY_TREE_INVALID: Inconsistent or invalid extensions
631 * X509_PCY_TREE_INTERNAL: Internal error, most likely malloc
632 * X509_PCY_TREE_VALID: Success (null tree if empty or bare TA)
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)
641 X509_POLICY_TREE *tree = NULL;
642 STACK_OF(X509_POLICY_NODE) *nodes, *auth_nodes = NULL;
645 *pexplicit_policy = 0;
646 init_ret = tree_init(&tree, certs, flags);
651 if ((init_ret & X509_PCY_TREE_EXPLICIT) == 0) {
652 if (init_ret & X509_PCY_TREE_EMPTY) {
653 X509_policy_tree_free(tree);
654 return X509_PCY_TREE_VALID;
657 *pexplicit_policy = 1;
658 /* Tree empty and requireExplicit True: Error */
659 if (init_ret & X509_PCY_TREE_EMPTY)
660 return X509_PCY_TREE_FAILURE;
663 ret = tree_evaluate(tree);
664 TREE_PRINT("tree_evaluate()", tree, NULL);
668 if (ret == X509_PCY_TREE_EMPTY) {
669 X509_policy_tree_free(tree);
670 if (init_ret & X509_PCY_TREE_EXPLICIT)
671 return X509_PCY_TREE_FAILURE;
672 return X509_PCY_TREE_VALID;
675 /* Tree is not empty: continue */
677 if ((calc_ret = tree_calculate_authority_set(tree, &auth_nodes)) == 0)
679 ret = tree_calculate_user_set(tree, policy_oids, auth_nodes);
680 if (calc_ret == TREE_CALC_OK_DOFREE)
681 sk_X509_POLICY_NODE_free(auth_nodes);
687 if (init_ret & X509_PCY_TREE_EXPLICIT) {
688 nodes = X509_policy_tree_get0_user_policies(tree);
689 if (sk_X509_POLICY_NODE_num(nodes) <= 0)
690 return X509_PCY_TREE_FAILURE;
692 return X509_PCY_TREE_VALID;
695 X509_policy_tree_free(tree);
696 return X509_PCY_TREE_INTERNAL;