3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
6 /* ====================================================================
7 * Copyright (c) 2004 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
61 #include <openssl/x509.h>
62 #include <openssl/x509v3.h>
67 * Initialize policy tree. Return values:
68 * 0 Some internal error occured.
69 * -1 Inconsistent or invalid extensions in certificates.
70 * 1 Tree initialized OK.
71 * 2 Policy tree is empty.
72 * 5 Tree OK and requireExplicitPolicy true.
73 * 6 Tree empty and requireExplicitPolicy true.
76 static int tree_init(X509_POLICY_TREE **ptree, STACK_OF(X509) *certs,
79 X509_POLICY_TREE *tree;
80 X509_POLICY_LEVEL *level;
81 const X509_POLICY_CACHE *cache;
82 X509_POLICY_DATA *data = NULL;
90 n = sk_X509_num(certs);
92 /* Disable policy mapping for now... */
93 flags |= X509_V_FLAG_INHIBIT_MAP;
95 if (flags & X509_V_FLAG_EXPLICIT_POLICY)
98 explicit_policy = n + 1;
100 if (flags & X509_V_FLAG_INHIBIT_ANY)
105 if (flags & X509_V_FLAG_INHIBIT_MAP)
110 /* Can't do anything with just a trust anchor */
114 * First setup policy cache in all certificates apart from the trust
115 * anchor. Note any bad cache results on the way. Also can calculate
116 * explicit_policy value at this point.
118 for (i = n - 2; i >= 0; i--) {
119 x = sk_X509_value(certs, i);
120 X509_check_purpose(x, -1, -1);
121 cache = policy_cache_set(x);
122 /* If cache NULL something bad happened: return immediately */
126 * If inconsistent extensions keep a note of it but continue
128 if (x->ex_flags & EXFLAG_INVALID_POLICY)
131 * Otherwise if we have no data (hence no CertificatePolicies) and
132 * haven't already set an inconsistent code note it.
134 else if ((ret == 1) && !cache->data)
136 if (explicit_policy > 0) {
137 if (!(x->ex_flags & EXFLAG_SI))
139 if ((cache->explicit_skip != -1)
140 && (cache->explicit_skip < explicit_policy))
141 explicit_policy = cache->explicit_skip;
146 if (ret == 2 && !explicit_policy)
151 /* If we get this far initialize the tree */
153 tree = OPENSSL_malloc(sizeof(X509_POLICY_TREE));
159 tree->levels = OPENSSL_malloc(sizeof(X509_POLICY_LEVEL) * n);
161 tree->extra_data = NULL;
162 tree->auth_policies = NULL;
163 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--) {
185 x = sk_X509_value(certs, i);
186 cache = policy_cache_set(x);
188 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
191 if (!cache->anyPolicy)
192 level->flags |= X509_V_FLAG_INHIBIT_ANY;
194 /* Determine inhibit any and inhibit map flags */
197 * Any matching allowed if certificate is self issued and not the
200 if (!(x->ex_flags & EXFLAG_SI) || (i == 0))
201 level->flags |= X509_V_FLAG_INHIBIT_ANY;
203 if (!(x->ex_flags & EXFLAG_SI))
205 if ((cache->any_skip >= 0)
206 && (cache->any_skip < any_skip))
207 any_skip = cache->any_skip;
211 level->flags |= X509_V_FLAG_INHIBIT_MAP;
214 if ((cache->map_skip >= 0)
215 && (cache->map_skip < map_skip))
216 map_skip = cache->map_skip;
230 X509_policy_tree_free(tree);
237 * This corresponds to RFC3280 XXXX XXXXX: link any data from
238 * CertificatePolicies onto matching parent or anyPolicy if no match.
241 static int tree_link_nodes(X509_POLICY_LEVEL *curr,
242 const X509_POLICY_CACHE *cache)
245 X509_POLICY_LEVEL *last;
246 X509_POLICY_DATA *data;
247 X509_POLICY_NODE *parent;
249 for (i = 0; i < sk_X509_POLICY_DATA_num(cache->data); i++) {
250 data = sk_X509_POLICY_DATA_value(cache->data, i);
252 * If a node is mapped any it doesn't have a corresponding
253 * CertificatePolicies entry. However such an identical node would
254 * be created if anyPolicy matching is enabled because there would be
255 * no match with the parent valid_policy_set. So we create link
256 * because then it will have the mapping flags right and we can prune
259 if ((data->flags & POLICY_DATA_FLAG_MAPPED_ANY)
260 && !(curr->flags & X509_V_FLAG_INHIBIT_ANY))
262 /* Look for matching node in parent */
263 parent = level_find_node(last, data->valid_policy);
264 /* If no match link to anyPolicy */
266 parent = last->anyPolicy;
267 if (parent && !level_add_node(curr, data, parent, NULL))
274 * This corresponds to RFC3280 XXXX XXXXX: Create new data for any unmatched
275 * policies in the parent and link to anyPolicy.
278 static int tree_link_any(X509_POLICY_LEVEL *curr,
279 const X509_POLICY_CACHE *cache,
280 X509_POLICY_TREE *tree)
283 X509_POLICY_DATA *data;
284 X509_POLICY_NODE *node;
285 X509_POLICY_LEVEL *last;
289 for (i = 0; i < sk_X509_POLICY_NODE_num(last->nodes); i++) {
290 node = sk_X509_POLICY_NODE_value(last->nodes, i);
293 * Skip any node with any children: we only want unmathced nodes.
294 * Note: need something better for policy mapping because each node
295 * may have multiple children
300 * Create a new node with qualifiers from anyPolicy and id from
303 data = policy_data_new(NULL, node->data->valid_policy,
304 node_critical(node));
308 /* Curr may not have anyPolicy */
309 data->qualifier_set = cache->anyPolicy->qualifier_set;
310 data->flags |= POLICY_DATA_FLAG_SHARED_QUALIFIERS;
311 if (!level_add_node(curr, data, node, tree)) {
312 policy_data_free(data);
316 /* Finally add link to anyPolicy */
317 if (last->anyPolicy) {
318 if (!level_add_node(curr, cache->anyPolicy, last->anyPolicy, NULL))
325 * Prune the tree: delete any child mapped child data on the current level
326 * then proceed up the tree deleting any data with no children. If we ever
327 * have no data on a level we can halt because the tree will be empty.
330 static int tree_prune(X509_POLICY_TREE *tree, X509_POLICY_LEVEL *curr)
332 X509_POLICY_NODE *node;
334 for (i = sk_X509_POLICY_NODE_num(curr->nodes) - 1; i >= 0; i--) {
335 node = sk_X509_POLICY_NODE_value(curr->nodes, i);
336 /* Delete any mapped data: see RFC3280 XXXX */
337 if (node->data->flags & POLICY_DATA_FLAG_MAP_MASK) {
338 node->parent->nchild--;
340 (void)sk_X509_POLICY_NODE_delete(curr->nodes, i);
346 for (i = sk_X509_POLICY_NODE_num(curr->nodes) - 1; i >= 0; i--) {
347 node = sk_X509_POLICY_NODE_value(curr->nodes, i);
348 if (node->nchild == 0) {
349 node->parent->nchild--;
351 (void)sk_X509_POLICY_NODE_delete(curr->nodes, i);
354 if (curr->anyPolicy && !curr->anyPolicy->nchild) {
355 if (curr->anyPolicy->parent)
356 curr->anyPolicy->parent->nchild--;
357 OPENSSL_free(curr->anyPolicy);
358 curr->anyPolicy = NULL;
360 if (curr == tree->levels) {
361 /* If we zapped anyPolicy at top then tree is empty */
362 if (!curr->anyPolicy)
372 static int tree_add_auth_node(STACK_OF(X509_POLICY_NODE) **pnodes,
373 X509_POLICY_NODE *pcy)
376 *pnodes = policy_node_cmp_new();
379 } else if (sk_X509_POLICY_NODE_find(*pnodes, pcy) != -1)
382 if (!sk_X509_POLICY_NODE_push(*pnodes, pcy))
390 * Calculate the authority set based on policy tree. The 'pnodes' parameter
391 * is used as a store for the set of policy nodes used to calculate the user
392 * set. If the authority set is not anyPolicy then pnodes will just point to
393 * the authority set. If however the authority set is anyPolicy then the set
394 * of valid policies (other than anyPolicy) is store in pnodes. The return
395 * value of '2' is used in this case to indicate that pnodes should be freed.
398 static int tree_calculate_authority_set(X509_POLICY_TREE *tree,
399 STACK_OF(X509_POLICY_NODE) **pnodes)
401 X509_POLICY_LEVEL *curr;
402 X509_POLICY_NODE *node, *anyptr;
403 STACK_OF(X509_POLICY_NODE) **addnodes;
405 curr = tree->levels + tree->nlevel - 1;
407 /* If last level contains anyPolicy set is anyPolicy */
408 if (curr->anyPolicy) {
409 if (!tree_add_auth_node(&tree->auth_policies, curr->anyPolicy))
413 /* Add policies to authority set */
414 addnodes = &tree->auth_policies;
417 for (i = 1; i < tree->nlevel; i++) {
419 * If no anyPolicy node on this this level it can't appear on lower
420 * levels so end search.
422 if (!(anyptr = curr->anyPolicy))
425 for (j = 0; j < sk_X509_POLICY_NODE_num(curr->nodes); j++) {
426 node = sk_X509_POLICY_NODE_value(curr->nodes, j);
427 if ((node->parent == anyptr)
428 && !tree_add_auth_node(addnodes, node))
433 if (addnodes == pnodes)
436 *pnodes = tree->auth_policies;
441 static int tree_calculate_user_set(X509_POLICY_TREE *tree,
442 STACK_OF(ASN1_OBJECT) *policy_oids,
443 STACK_OF(X509_POLICY_NODE) *auth_nodes)
446 X509_POLICY_NODE *node;
449 X509_POLICY_NODE *anyPolicy;
450 X509_POLICY_DATA *extra;
453 * Check if anyPolicy present in authority constrained policy set: this
454 * will happen if it is a leaf node.
457 if (sk_ASN1_OBJECT_num(policy_oids) <= 0)
460 anyPolicy = tree->levels[tree->nlevel - 1].anyPolicy;
462 for (i = 0; i < sk_ASN1_OBJECT_num(policy_oids); i++) {
463 oid = sk_ASN1_OBJECT_value(policy_oids, i);
464 if (OBJ_obj2nid(oid) == NID_any_policy) {
465 tree->flags |= POLICY_FLAG_ANY_POLICY;
470 for (i = 0; i < sk_ASN1_OBJECT_num(policy_oids); i++) {
471 oid = sk_ASN1_OBJECT_value(policy_oids, i);
472 node = tree_find_sk(auth_nodes, oid);
477 * Create a new node with policy ID from user set and qualifiers
480 extra = policy_data_new(NULL, oid, node_critical(anyPolicy));
483 extra->qualifier_set = anyPolicy->data->qualifier_set;
484 extra->flags = POLICY_DATA_FLAG_SHARED_QUALIFIERS
485 | POLICY_DATA_FLAG_EXTRA_NODE;
486 node = level_add_node(NULL, extra, anyPolicy->parent, tree);
488 if (!tree->user_policies) {
489 tree->user_policies = sk_X509_POLICY_NODE_new_null();
490 if (!tree->user_policies)
493 if (!sk_X509_POLICY_NODE_push(tree->user_policies, node))
500 static int tree_evaluate(X509_POLICY_TREE *tree)
503 X509_POLICY_LEVEL *curr = tree->levels + 1;
504 const X509_POLICY_CACHE *cache;
506 for (i = 1; i < tree->nlevel; i++, curr++) {
507 cache = policy_cache_set(curr->cert);
508 if (!tree_link_nodes(curr, cache))
511 if (!(curr->flags & X509_V_FLAG_INHIBIT_ANY)
512 && !tree_link_any(curr, cache, tree))
514 ret = tree_prune(tree, curr);
523 static void exnode_free(X509_POLICY_NODE *node)
525 if (node->data && (node->data->flags & POLICY_DATA_FLAG_EXTRA_NODE))
529 void X509_policy_tree_free(X509_POLICY_TREE *tree)
531 X509_POLICY_LEVEL *curr;
537 sk_X509_POLICY_NODE_free(tree->auth_policies);
538 sk_X509_POLICY_NODE_pop_free(tree->user_policies, exnode_free);
540 for (i = 0, curr = tree->levels; i < tree->nlevel; i++, curr++) {
542 X509_free(curr->cert);
544 sk_X509_POLICY_NODE_pop_free(curr->nodes, policy_node_free);
546 policy_node_free(curr->anyPolicy);
549 if (tree->extra_data)
550 sk_X509_POLICY_DATA_pop_free(tree->extra_data, policy_data_free);
552 OPENSSL_free(tree->levels);
558 * Application policy checking function.
562 * -1 One or more certificates contain invalid or inconsistent extensions
563 * -2 User constrained policy set empty and requireExplicit true.
566 int X509_policy_check(X509_POLICY_TREE **ptree, int *pexplicit_policy,
567 STACK_OF(X509) *certs,
568 STACK_OF(ASN1_OBJECT) *policy_oids, unsigned int flags)
571 X509_POLICY_TREE *tree = NULL;
572 STACK_OF(X509_POLICY_NODE) *nodes, *auth_nodes = NULL;
575 *pexplicit_policy = 0;
576 ret = tree_init(&tree, certs, flags);
580 /* Tree empty requireExplicit False: OK */
584 /* Some internal error */
588 /* Some internal error */
592 /* Tree empty requireExplicit True: Error */
595 *pexplicit_policy = 1;
598 /* Tree OK requireExplicit True: OK and continue */
600 *pexplicit_policy = 1;
603 /* Tree OK: continue */
608 * tree_init() returns success and a null tree
609 * if it's just looking at a trust anchor.
610 * I'm not sure that returning success here is
611 * correct, but I'm sure that reporting this
612 * as an internal error which our caller
613 * interprets as a malloc failure is wrong.
621 ret = tree_evaluate(tree);
626 /* Return value 2 means tree empty */
628 X509_policy_tree_free(tree);
629 if (*pexplicit_policy)
635 /* Tree is not empty: continue */
637 ret = tree_calculate_authority_set(tree, &auth_nodes);
642 if (!tree_calculate_user_set(tree, policy_oids, auth_nodes))
646 sk_X509_POLICY_NODE_free(auth_nodes);
651 if (*pexplicit_policy) {
652 nodes = X509_policy_tree_get0_user_policies(tree);
653 if (sk_X509_POLICY_NODE_num(nodes) <= 0)
661 X509_policy_tree_free(tree);