2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 2004 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
60 #include <openssl/x509.h>
61 #include <openssl/x509v3.h>
66 * Initialize policy tree. Return values:
67 * 0 Some internal error occured.
68 * -1 Inconsistent or invalid extensions in certificates.
69 * 1 Tree initialized OK.
70 * 2 Policy tree is empty.
71 * 5 Tree OK and requireExplicitPolicy true.
72 * 6 Tree empty and requireExplicitPolicy true.
75 static int tree_init(X509_POLICY_TREE **ptree, STACK_OF(X509) *certs,
78 X509_POLICY_TREE *tree;
79 X509_POLICY_LEVEL *level;
80 const X509_POLICY_CACHE *cache;
81 X509_POLICY_DATA *data = NULL;
89 n = sk_X509_num(certs);
91 /* Disable policy mapping for now... */
92 flags |= X509_V_FLAG_INHIBIT_MAP;
94 if (flags & X509_V_FLAG_EXPLICIT_POLICY)
97 explicit_policy = n + 1;
99 if (flags & X509_V_FLAG_INHIBIT_ANY)
104 if (flags & X509_V_FLAG_INHIBIT_MAP)
109 /* Can't do anything with just a trust anchor */
112 /* First setup policy cache in all certificates apart from the
113 * trust anchor. Note any bad cache results on the way. Also can
114 * calculate explicit_policy value at this point.
116 for (i = n - 2; i >= 0; i--)
118 x = sk_X509_value(certs, i);
119 X509_check_purpose(x, -1, -1);
120 cache = policy_cache_set(x);
121 /* If cache NULL something bad happened: return immediately */
124 /* If inconsistent extensions keep a note of it but continue */
125 if (x->ex_flags & EXFLAG_INVALID_POLICY)
127 /* Otherwise if we have no data (hence no CertificatePolicies)
128 * and haven't already set an inconsistent code note it.
130 else if ((ret == 1) && !cache->data)
132 if (explicit_policy > 0)
134 if (!(x->ex_flags & EXFLAG_SI))
136 if ((cache->explicit_skip != -1)
137 && (cache->explicit_skip < explicit_policy))
138 explicit_policy = cache->explicit_skip;
144 if (ret == 2 && !explicit_policy)
150 /* If we get this far initialize the tree */
152 tree = OPENSSL_malloc(sizeof(X509_POLICY_TREE));
158 tree->levels = OPENSSL_malloc(sizeof(X509_POLICY_LEVEL) * n);
160 tree->extra_data = NULL;
161 tree->auth_policies = NULL;
162 tree->user_policies = NULL;
170 memset(tree->levels, 0, n * sizeof(X509_POLICY_LEVEL));
174 level = tree->levels;
176 /* Root data: initialize to anyPolicy */
178 data = policy_data_new(NULL, OBJ_nid2obj(NID_any_policy), 0);
180 if (!data || !level_add_node(level, data, NULL, tree))
183 for (i = n - 2; i >= 0; i--)
186 x = sk_X509_value(certs, i);
187 cache = policy_cache_set(x);
189 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
192 if (!cache->anyPolicy)
193 level->flags |= X509_V_FLAG_INHIBIT_ANY;
195 /* Determine inhibit any and inhibit map flags */
198 /* Any matching allowed if certificate is self
199 * issued and not the last in the chain.
201 if (!(x->ex_flags & EXFLAG_SI) || (i == 0))
202 level->flags |= X509_V_FLAG_INHIBIT_ANY;
206 if (!(x->ex_flags & EXFLAG_SI))
208 if ((cache->any_skip >= 0)
209 && (cache->any_skip < any_skip))
210 any_skip = cache->any_skip;
214 level->flags |= X509_V_FLAG_INHIBIT_MAP;
218 if ((cache->map_skip >= 0)
219 && (cache->map_skip < map_skip))
220 map_skip = cache->map_skip;
235 X509_policy_tree_free(tree);
241 /* This corresponds to RFC3280 XXXX XXXXX:
242 * link any data from CertificatePolicies onto matching parent
243 * or anyPolicy if no match.
246 static int tree_link_nodes(X509_POLICY_LEVEL *curr,
247 const X509_POLICY_CACHE *cache)
250 X509_POLICY_LEVEL *last;
251 X509_POLICY_DATA *data;
252 X509_POLICY_NODE *parent;
254 for (i = 0; i < sk_X509_POLICY_DATA_num(cache->data); i++)
256 data = sk_X509_POLICY_DATA_value(cache->data, i);
257 /* If a node is mapped any it doesn't have a corresponding
258 * CertificatePolicies entry.
259 * However such an identical node would be created
260 * if anyPolicy matching is enabled because there would be
261 * no match with the parent valid_policy_set. So we create
262 * link because then it will have the mapping flags
263 * right and we can prune it later.
265 if ((data->flags & POLICY_DATA_FLAG_MAPPED_ANY)
266 && !(curr->flags & X509_V_FLAG_INHIBIT_ANY))
268 /* Look for matching node in parent */
269 parent = level_find_node(last, data->valid_policy);
270 /* If no match link to anyPolicy */
272 parent = last->anyPolicy;
273 if (parent && !level_add_node(curr, data, parent, NULL))
279 /* This corresponds to RFC3280 XXXX XXXXX:
280 * Create new data for any unmatched policies in the parent and link
284 static int tree_link_any(X509_POLICY_LEVEL *curr,
285 const X509_POLICY_CACHE *cache,
286 X509_POLICY_TREE *tree)
289 X509_POLICY_DATA *data;
290 X509_POLICY_NODE *node;
291 X509_POLICY_LEVEL *last;
295 for (i = 0; i < sk_X509_POLICY_NODE_num(last->nodes); i++)
297 node = sk_X509_POLICY_NODE_value(last->nodes, i);
299 /* Skip any node with any children: we only want unmathced
302 * Note: need something better for policy mapping
303 * because each node may have multiple children
307 /* Create a new node with qualifiers from anyPolicy and
308 * id from unmatched node.
310 data = policy_data_new(NULL, node->data->valid_policy,
311 node_critical(node));
315 /* Curr may not have anyPolicy */
316 data->qualifier_set = cache->anyPolicy->qualifier_set;
317 data->flags |= POLICY_DATA_FLAG_SHARED_QUALIFIERS;
318 if (!level_add_node(curr, data, node, tree))
320 policy_data_free(data);
324 /* Finally add link to anyPolicy */
327 if (!level_add_node(curr, cache->anyPolicy,
328 last->anyPolicy, NULL))
334 /* Prune the tree: delete any child mapped child data on the current level
335 * then proceed up the tree deleting any data with no children. If we ever
336 * have no data on a level we can halt because the tree will be empty.
339 static int tree_prune(X509_POLICY_TREE *tree, X509_POLICY_LEVEL *curr)
341 X509_POLICY_NODE *node;
343 for (i = sk_X509_POLICY_NODE_num(curr->nodes) - 1; i >= 0; i--)
345 node = sk_X509_POLICY_NODE_value(curr->nodes, i);
346 /* Delete any mapped data: see RFC3280 XXXX */
347 if (node->data->flags & POLICY_DATA_FLAG_MAP_MASK)
349 node->parent->nchild--;
351 (void)sk_X509_POLICY_NODE_delete(curr->nodes, i);
357 for (i = sk_X509_POLICY_NODE_num(curr->nodes) - 1; i >= 0; i--)
359 node = sk_X509_POLICY_NODE_value(curr->nodes, i);
360 if (node->nchild == 0)
362 node->parent->nchild--;
364 (void)sk_X509_POLICY_NODE_delete(curr->nodes, i);
367 if (curr->anyPolicy && !curr->anyPolicy->nchild)
369 if (curr->anyPolicy->parent)
370 curr->anyPolicy->parent->nchild--;
371 OPENSSL_free(curr->anyPolicy);
372 curr->anyPolicy = NULL;
374 if (curr == tree->levels)
376 /* If we zapped anyPolicy at top then tree is empty */
377 if (!curr->anyPolicy)
387 static int tree_add_auth_node(STACK_OF(X509_POLICY_NODE) **pnodes,
388 X509_POLICY_NODE *pcy)
392 *pnodes = policy_node_cmp_new();
396 else if (sk_X509_POLICY_NODE_find(*pnodes, pcy) != -1)
399 if (!sk_X509_POLICY_NODE_push(*pnodes, pcy))
406 /* Calculate the authority set based on policy tree.
407 * The 'pnodes' parameter is used as a store for the set of policy nodes
408 * used to calculate the user set. If the authority set is not anyPolicy
409 * then pnodes will just point to the authority set. If however the authority
410 * set is anyPolicy then the set of valid policies (other than anyPolicy)
411 * is store in pnodes. The return value of '2' is used in this case to indicate
412 * that pnodes should be freed.
415 static int tree_calculate_authority_set(X509_POLICY_TREE *tree,
416 STACK_OF(X509_POLICY_NODE) **pnodes)
418 X509_POLICY_LEVEL *curr;
419 X509_POLICY_NODE *node, *anyptr;
420 STACK_OF(X509_POLICY_NODE) **addnodes;
422 curr = tree->levels + tree->nlevel - 1;
424 /* If last level contains anyPolicy set is anyPolicy */
427 if (!tree_add_auth_node(&tree->auth_policies, curr->anyPolicy))
432 /* Add policies to authority set */
433 addnodes = &tree->auth_policies;
436 for (i = 1; i < tree->nlevel; i++)
438 /* If no anyPolicy node on this this level it can't
439 * appear on lower levels so end search.
441 if (!(anyptr = curr->anyPolicy))
444 for (j = 0; j < sk_X509_POLICY_NODE_num(curr->nodes); j++)
446 node = sk_X509_POLICY_NODE_value(curr->nodes, j);
447 if ((node->parent == anyptr)
448 && !tree_add_auth_node(addnodes, node))
453 if (addnodes == pnodes)
456 *pnodes = tree->auth_policies;
461 static int tree_calculate_user_set(X509_POLICY_TREE *tree,
462 STACK_OF(ASN1_OBJECT) *policy_oids,
463 STACK_OF(X509_POLICY_NODE) *auth_nodes)
466 X509_POLICY_NODE *node;
469 X509_POLICY_NODE *anyPolicy;
470 X509_POLICY_DATA *extra;
472 /* Check if anyPolicy present in authority constrained policy set:
473 * this will happen if it is a leaf node.
476 if (sk_ASN1_OBJECT_num(policy_oids) <= 0)
479 anyPolicy = tree->levels[tree->nlevel - 1].anyPolicy;
481 for (i = 0; i < sk_ASN1_OBJECT_num(policy_oids); i++)
483 oid = sk_ASN1_OBJECT_value(policy_oids, i);
484 if (OBJ_obj2nid(oid) == NID_any_policy)
486 tree->flags |= POLICY_FLAG_ANY_POLICY;
491 for (i = 0; i < sk_ASN1_OBJECT_num(policy_oids); i++)
493 oid = sk_ASN1_OBJECT_value(policy_oids, i);
494 node = tree_find_sk(auth_nodes, oid);
499 /* Create a new node with policy ID from user set
500 * and qualifiers from anyPolicy.
502 extra = policy_data_new(NULL, oid,
503 node_critical(anyPolicy));
506 extra->qualifier_set = anyPolicy->data->qualifier_set;
507 extra->flags = POLICY_DATA_FLAG_SHARED_QUALIFIERS
508 | POLICY_DATA_FLAG_EXTRA_NODE;
509 node = level_add_node(NULL, extra, anyPolicy->parent,
512 if (!tree->user_policies)
514 tree->user_policies = sk_X509_POLICY_NODE_new_null();
515 if (!tree->user_policies)
518 if (!sk_X509_POLICY_NODE_push(tree->user_policies, node))
525 static int tree_evaluate(X509_POLICY_TREE *tree)
528 X509_POLICY_LEVEL *curr = tree->levels + 1;
529 const X509_POLICY_CACHE *cache;
531 for(i = 1; i < tree->nlevel; i++, curr++)
533 cache = policy_cache_set(curr->cert);
534 if (!tree_link_nodes(curr, cache))
537 if (!(curr->flags & X509_V_FLAG_INHIBIT_ANY)
538 && !tree_link_any(curr, cache, tree))
540 ret = tree_prune(tree, curr);
549 static void exnode_free(X509_POLICY_NODE *node)
551 if (node->data && (node->data->flags & POLICY_DATA_FLAG_EXTRA_NODE))
556 void X509_policy_tree_free(X509_POLICY_TREE *tree)
558 X509_POLICY_LEVEL *curr;
564 sk_X509_POLICY_NODE_free(tree->auth_policies);
565 sk_X509_POLICY_NODE_pop_free(tree->user_policies, exnode_free);
567 for(i = 0, curr = tree->levels; i < tree->nlevel; i++, curr++)
570 X509_free(curr->cert);
572 sk_X509_POLICY_NODE_pop_free(curr->nodes,
575 policy_node_free(curr->anyPolicy);
578 if (tree->extra_data)
579 sk_X509_POLICY_DATA_pop_free(tree->extra_data,
582 OPENSSL_free(tree->levels);
588 * Application policy checking function.
592 * -1 One or more certificates contain invalid or inconsistent extensions
593 * -2 User constrained policy set empty and requireExplicit true.
596 int X509_policy_check(X509_POLICY_TREE **ptree, int *pexplicit_policy,
597 STACK_OF(X509) *certs,
598 STACK_OF(ASN1_OBJECT) *policy_oids,
602 X509_POLICY_TREE *tree = NULL;
603 STACK_OF(X509_POLICY_NODE) *nodes, *auth_nodes = NULL;
606 *pexplicit_policy = 0;
607 ret = tree_init(&tree, certs, flags);
613 /* Tree empty requireExplicit False: OK */
617 /* Some internal error */
621 /* Some internal error */
625 /* Tree empty requireExplicit True: Error */
628 *pexplicit_policy = 1;
631 /* Tree OK requireExplicit True: OK and continue */
633 *pexplicit_policy = 1;
636 /* Tree OK: continue */
641 * tree_init() returns success and a null tree
642 * if it's just looking at a trust anchor.
643 * I'm not sure that returning success here is
644 * correct, but I'm sure that reporting this
645 * as an internal error which our caller
646 * interprets as a malloc failure is wrong.
652 if (!tree) goto error;
653 ret = tree_evaluate(tree);
658 /* Return value 2 means tree empty */
661 X509_policy_tree_free(tree);
662 if (*pexplicit_policy)
668 /* Tree is not empty: continue */
670 ret = tree_calculate_authority_set(tree, &auth_nodes);
675 if (!tree_calculate_user_set(tree, policy_oids, auth_nodes))
679 sk_X509_POLICY_NODE_free(auth_nodes);
684 if (*pexplicit_policy)
686 nodes = X509_policy_tree_get0_user_policies(tree);
687 if (sk_X509_POLICY_NODE_num(nodes) <= 0)
695 X509_policy_tree_free(tree);