1 /* $OpenBSD: pcy_tree.c,v 1.15 2015/07/18 00:01:05 beck Exp $ */
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).
59 #include <openssl/x509.h>
60 #include <openssl/x509v3.h>
64 /* Enable this to print out the complete policy tree at various point during
68 /*#define OPENSSL_POLICY_DEBUG*/
70 #ifdef OPENSSL_POLICY_DEBUG
73 expected_print(BIO
*err
, X509_POLICY_LEVEL
*lev
, X509_POLICY_NODE
*node
,
76 if ((lev
->flags
& X509_V_FLAG_INHIBIT_MAP
) ||
77 !(node
->data
->flags
& POLICY_DATA_FLAG_MAP_MASK
))
78 BIO_puts(err
, " Not Mapped\n");
81 STACK_OF(ASN1_OBJECT
) *pset
= node
->data
->expected_policy_set
;
83 BIO_puts(err
, " Expected: ");
84 for (i
= 0; i
< sk_ASN1_OBJECT_num(pset
); i
++) {
85 oid
= sk_ASN1_OBJECT_value(pset
, i
);
88 i2a_ASN1_OBJECT(err
, oid
);
95 tree_print(char *str
, X509_POLICY_TREE
*tree
, X509_POLICY_LEVEL
*curr
)
97 X509_POLICY_LEVEL
*plev
;
98 X509_POLICY_NODE
*node
;
102 err
= BIO_new_fp(stderr
, BIO_NOCLOSE
);
104 curr
= tree
->levels
+ tree
->nlevel
;
107 BIO_printf(err
, "Level print after %s\n", str
);
108 BIO_printf(err
, "Printing Up to Level %ld\n", curr
- tree
->levels
);
109 for (plev
= tree
->levels
; plev
!= curr
; plev
++) {
110 BIO_printf(err
, "Level %ld, flags = %x\n",
111 plev
- tree
->levels
, plev
->flags
);
112 for (i
= 0; i
< sk_X509_POLICY_NODE_num(plev
->nodes
); i
++) {
113 node
= sk_X509_POLICY_NODE_value(plev
->nodes
, i
);
114 X509_POLICY_NODE_print(err
, node
, 2);
115 expected_print(err
, plev
, node
, 2);
116 BIO_printf(err
, " Flags: %x\n", node
->data
->flags
);
119 X509_POLICY_NODE_print(err
, plev
->anyPolicy
, 2);
126 #define tree_print(a,b,c) /* */
130 /* Initialize policy tree. Return values:
131 * 0 Some internal error occured.
132 * -1 Inconsistent or invalid extensions in certificates.
133 * 1 Tree initialized OK.
134 * 2 Policy tree is empty.
135 * 5 Tree OK and requireExplicitPolicy true.
136 * 6 Tree empty and requireExplicitPolicy true.
140 tree_init(X509_POLICY_TREE
**ptree
, STACK_OF(X509
) *certs
, unsigned int flags
)
142 X509_POLICY_TREE
*tree
;
143 X509_POLICY_LEVEL
*level
;
144 const X509_POLICY_CACHE
*cache
;
145 X509_POLICY_DATA
*data
= NULL
;
154 n
= sk_X509_num(certs
);
156 if (flags
& X509_V_FLAG_EXPLICIT_POLICY
)
159 explicit_policy
= n
+ 1;
161 if (flags
& X509_V_FLAG_INHIBIT_ANY
)
166 if (flags
& X509_V_FLAG_INHIBIT_MAP
)
171 /* Can't do anything with just a trust anchor */
174 /* First setup policy cache in all certificates apart from the
175 * trust anchor. Note any bad cache results on the way. Also can
176 * calculate explicit_policy value at this point.
178 for (i
= n
- 2; i
>= 0; i
--) {
179 x
= sk_X509_value(certs
, i
);
180 X509_check_purpose(x
, -1, -1);
181 cache
= policy_cache_set(x
);
182 /* If cache NULL something bad happened: return immediately */
185 /* If inconsistent extensions keep a note of it but continue */
186 if (x
->ex_flags
& EXFLAG_INVALID_POLICY
)
188 /* Otherwise if we have no data (hence no CertificatePolicies)
189 * and haven't already set an inconsistent code note it.
191 else if ((ret
== 1) && !cache
->data
)
193 if (explicit_policy
> 0) {
194 if (!(x
->ex_flags
& EXFLAG_SI
))
196 if ((cache
->explicit_skip
!= -1) &&
197 (cache
->explicit_skip
< explicit_policy
))
198 explicit_policy
= cache
->explicit_skip
;
203 if (ret
== 2 && !explicit_policy
)
209 /* If we get this far initialize the tree */
211 tree
= malloc(sizeof(X509_POLICY_TREE
));
217 tree
->levels
= calloc(n
, sizeof(X509_POLICY_LEVEL
));
219 tree
->extra_data
= NULL
;
220 tree
->auth_policies
= NULL
;
221 tree
->user_policies
= NULL
;
230 level
= tree
->levels
;
232 /* Root data: initialize to anyPolicy */
234 data
= policy_data_new(NULL
, OBJ_nid2obj(NID_any_policy
), 0);
236 if (!data
|| !level_add_node(level
, data
, NULL
, tree
, NULL
))
239 for (i
= n
- 2; i
>= 0; i
--) {
241 x
= sk_X509_value(certs
, i
);
242 cache
= policy_cache_set(x
);
243 CRYPTO_add(&x
->references
, 1, CRYPTO_LOCK_X509
);
246 if (!cache
->anyPolicy
)
247 level
->flags
|= X509_V_FLAG_INHIBIT_ANY
;
249 /* Determine inhibit any and inhibit map flags */
251 /* Any matching allowed if certificate is self
252 * issued and not the last in the chain.
254 if (!(x
->ex_flags
& EXFLAG_SI
) || (i
== 0))
255 level
->flags
|= X509_V_FLAG_INHIBIT_ANY
;
257 if (!(x
->ex_flags
& EXFLAG_SI
))
259 if ((cache
->any_skip
>= 0) &&
260 (cache
->any_skip
< any_skip
))
261 any_skip
= cache
->any_skip
;
265 level
->flags
|= X509_V_FLAG_INHIBIT_MAP
;
267 if (!(x
->ex_flags
& EXFLAG_SI
))
269 if ((cache
->map_skip
>= 0) &&
270 (cache
->map_skip
< map_skip
))
271 map_skip
= cache
->map_skip
;
284 X509_policy_tree_free(tree
);
290 tree_link_matching_nodes(X509_POLICY_LEVEL
*curr
, const X509_POLICY_DATA
*data
)
292 X509_POLICY_LEVEL
*last
= curr
- 1;
293 X509_POLICY_NODE
*node
;
296 /* Iterate through all in nodes linking matches */
297 for (i
= 0; i
< sk_X509_POLICY_NODE_num(last
->nodes
); i
++) {
298 node
= sk_X509_POLICY_NODE_value(last
->nodes
, i
);
299 if (policy_node_match(last
, node
, data
->valid_policy
)) {
300 if (!level_add_node(curr
, data
, node
, NULL
, NULL
))
305 if (!matched
&& last
->anyPolicy
) {
306 if (!level_add_node(curr
, data
, last
->anyPolicy
, NULL
, NULL
))
312 /* This corresponds to RFC3280 6.1.3(d)(1):
313 * link any data from CertificatePolicies onto matching parent
314 * or anyPolicy if no match.
318 tree_link_nodes(X509_POLICY_LEVEL
*curr
, const X509_POLICY_CACHE
*cache
)
321 X509_POLICY_DATA
*data
;
323 for (i
= 0; i
< sk_X509_POLICY_DATA_num(cache
->data
); i
++) {
324 data
= sk_X509_POLICY_DATA_value(cache
->data
, i
);
325 /* Look for matching nodes in previous level */
326 if (!tree_link_matching_nodes(curr
, data
))
332 /* This corresponds to RFC3280 6.1.3(d)(2):
333 * Create new data for any unmatched policies in the parent and link
338 tree_add_unmatched(X509_POLICY_LEVEL
*curr
, const X509_POLICY_CACHE
*cache
,
339 const ASN1_OBJECT
*id
, X509_POLICY_NODE
*node
, X509_POLICY_TREE
*tree
)
341 X509_POLICY_DATA
*data
;
344 id
= node
->data
->valid_policy
;
345 /* Create a new node with qualifiers from anyPolicy and
346 * id from unmatched node.
348 data
= policy_data_new(NULL
, id
, node_critical(node
));
352 /* Curr may not have anyPolicy */
353 data
->qualifier_set
= cache
->anyPolicy
->qualifier_set
;
354 data
->flags
|= POLICY_DATA_FLAG_SHARED_QUALIFIERS
;
355 if (!level_add_node(curr
, data
, node
, tree
, NULL
)) {
356 policy_data_free(data
);
364 tree_link_unmatched(X509_POLICY_LEVEL
*curr
, const X509_POLICY_CACHE
*cache
,
365 X509_POLICY_NODE
*node
, X509_POLICY_TREE
*tree
)
367 const X509_POLICY_LEVEL
*last
= curr
- 1;
370 if ((last
->flags
& X509_V_FLAG_INHIBIT_MAP
) ||
371 !(node
->data
->flags
& POLICY_DATA_FLAG_MAPPED
)) {
372 /* If no policy mapping: matched if one child present */
375 if (!tree_add_unmatched(curr
, cache
, NULL
, node
, tree
))
379 /* If mapping: matched if one child per expected policy set */
380 STACK_OF(ASN1_OBJECT
) *expset
= node
->data
->expected_policy_set
;
381 if (node
->nchild
== sk_ASN1_OBJECT_num(expset
))
383 /* Locate unmatched nodes */
384 for (i
= 0; i
< sk_ASN1_OBJECT_num(expset
); i
++) {
385 ASN1_OBJECT
*oid
= sk_ASN1_OBJECT_value(expset
, i
);
386 if (level_find_node(curr
, node
, oid
))
388 if (!tree_add_unmatched(curr
, cache
, oid
, node
, tree
))
397 tree_link_any(X509_POLICY_LEVEL
*curr
, const X509_POLICY_CACHE
*cache
,
398 X509_POLICY_TREE
*tree
)
401 X509_POLICY_NODE
*node
;
402 X509_POLICY_LEVEL
*last
= curr
- 1;
404 for (i
= 0; i
< sk_X509_POLICY_NODE_num(last
->nodes
); i
++) {
405 node
= sk_X509_POLICY_NODE_value(last
->nodes
, i
);
407 if (!tree_link_unmatched(curr
, cache
, node
, tree
))
410 /* Finally add link to anyPolicy */
411 if (last
->anyPolicy
) {
412 if (!level_add_node(curr
, cache
->anyPolicy
,
413 last
->anyPolicy
, NULL
, NULL
))
419 /* Prune the tree: delete any child mapped child data on the current level
420 * then proceed up the tree deleting any data with no children. If we ever
421 * have no data on a level we can halt because the tree will be empty.
425 tree_prune(X509_POLICY_TREE
*tree
, X509_POLICY_LEVEL
*curr
)
427 STACK_OF(X509_POLICY_NODE
) *nodes
;
428 X509_POLICY_NODE
*node
;
432 if (curr
->flags
& X509_V_FLAG_INHIBIT_MAP
) {
433 for (i
= sk_X509_POLICY_NODE_num(nodes
) - 1; i
>= 0; i
--) {
434 node
= sk_X509_POLICY_NODE_value(nodes
, i
);
435 /* Delete any mapped data: see RFC3280 XXXX */
436 if (node
->data
->flags
& POLICY_DATA_FLAG_MAP_MASK
) {
437 node
->parent
->nchild
--;
439 (void)sk_X509_POLICY_NODE_delete(nodes
, i
);
447 for (i
= sk_X509_POLICY_NODE_num(nodes
) - 1; i
>= 0; i
--) {
448 node
= sk_X509_POLICY_NODE_value(nodes
, i
);
449 if (node
->nchild
== 0) {
450 node
->parent
->nchild
--;
452 (void)sk_X509_POLICY_NODE_delete(nodes
, i
);
455 if (curr
->anyPolicy
&& !curr
->anyPolicy
->nchild
) {
456 if (curr
->anyPolicy
->parent
)
457 curr
->anyPolicy
->parent
->nchild
--;
458 free(curr
->anyPolicy
);
459 curr
->anyPolicy
= NULL
;
461 if (curr
== tree
->levels
) {
462 /* If we zapped anyPolicy at top then tree is empty */
463 if (!curr
->anyPolicy
)
473 tree_add_auth_node(STACK_OF(X509_POLICY_NODE
) **pnodes
, X509_POLICY_NODE
*pcy
)
476 *pnodes
= policy_node_cmp_new();
479 } else if (sk_X509_POLICY_NODE_find(*pnodes
, pcy
) != -1)
482 if (!sk_X509_POLICY_NODE_push(*pnodes
, pcy
))
488 /* Calculate the authority set based on policy tree.
489 * The 'pnodes' parameter is used as a store for the set of policy nodes
490 * used to calculate the user set. If the authority set is not anyPolicy
491 * then pnodes will just point to the authority set. If however the authority
492 * set is anyPolicy then the set of valid policies (other than anyPolicy)
493 * is store in pnodes. The return value of '2' is used in this case to indicate
494 * that pnodes should be freed.
498 tree_calculate_authority_set(X509_POLICY_TREE
*tree
,
499 STACK_OF(X509_POLICY_NODE
) **pnodes
)
501 X509_POLICY_LEVEL
*curr
;
502 X509_POLICY_NODE
*node
, *anyptr
;
503 STACK_OF(X509_POLICY_NODE
) **addnodes
;
506 curr
= tree
->levels
+ tree
->nlevel
- 1;
508 /* If last level contains anyPolicy set is anyPolicy */
509 if (curr
->anyPolicy
) {
510 if (!tree_add_auth_node(&tree
->auth_policies
, curr
->anyPolicy
))
514 /* Add policies to authority set */
515 addnodes
= &tree
->auth_policies
;
518 for (i
= 1; i
< tree
->nlevel
; i
++) {
519 /* If no anyPolicy node on this this level it can't
520 * appear on lower levels so end search.
522 if (!(anyptr
= curr
->anyPolicy
))
525 for (j
= 0; j
< sk_X509_POLICY_NODE_num(curr
->nodes
); j
++) {
526 node
= sk_X509_POLICY_NODE_value(curr
->nodes
, j
);
527 if ((node
->parent
== anyptr
) &&
528 !tree_add_auth_node(addnodes
, node
))
533 if (addnodes
== pnodes
)
536 *pnodes
= tree
->auth_policies
;
542 tree_calculate_user_set(X509_POLICY_TREE
*tree
,
543 STACK_OF(ASN1_OBJECT
) *policy_oids
, STACK_OF(X509_POLICY_NODE
) *auth_nodes
)
546 X509_POLICY_NODE
*node
;
548 X509_POLICY_NODE
*anyPolicy
;
549 X509_POLICY_DATA
*extra
;
551 /* Check if anyPolicy present in authority constrained policy set:
552 * this will happen if it is a leaf node.
555 if (sk_ASN1_OBJECT_num(policy_oids
) <= 0)
558 anyPolicy
= tree
->levels
[tree
->nlevel
- 1].anyPolicy
;
560 for (i
= 0; i
< sk_ASN1_OBJECT_num(policy_oids
); i
++) {
561 oid
= sk_ASN1_OBJECT_value(policy_oids
, i
);
562 if (OBJ_obj2nid(oid
) == NID_any_policy
) {
563 tree
->flags
|= POLICY_FLAG_ANY_POLICY
;
568 for (i
= 0; i
< sk_ASN1_OBJECT_num(policy_oids
); i
++) {
569 oid
= sk_ASN1_OBJECT_value(policy_oids
, i
);
570 node
= tree_find_sk(auth_nodes
, oid
);
574 /* Create a new node with policy ID from user set
575 * and qualifiers from anyPolicy.
577 extra
= policy_data_new(NULL
, oid
,
578 node_critical(anyPolicy
));
581 extra
->qualifier_set
= anyPolicy
->data
->qualifier_set
;
582 extra
->flags
= POLICY_DATA_FLAG_SHARED_QUALIFIERS
|
583 POLICY_DATA_FLAG_EXTRA_NODE
;
584 (void) level_add_node(NULL
, extra
, anyPolicy
->parent
,
587 if (!tree
->user_policies
) {
588 tree
->user_policies
= sk_X509_POLICY_NODE_new_null();
589 if (!tree
->user_policies
)
592 if (!sk_X509_POLICY_NODE_push(tree
->user_policies
, node
))
599 tree_evaluate(X509_POLICY_TREE
*tree
)
602 X509_POLICY_LEVEL
*curr
= tree
->levels
+ 1;
603 const X509_POLICY_CACHE
*cache
;
605 for (i
= 1; i
< tree
->nlevel
; i
++, curr
++) {
606 cache
= policy_cache_set(curr
->cert
);
607 if (!tree_link_nodes(curr
, cache
))
610 if (!(curr
->flags
& X509_V_FLAG_INHIBIT_ANY
) &&
611 !tree_link_any(curr
, cache
, tree
))
613 tree_print("before tree_prune()", tree
, curr
);
614 ret
= tree_prune(tree
, curr
);
623 exnode_free(X509_POLICY_NODE
*node
)
625 if (node
->data
&& (node
->data
->flags
& POLICY_DATA_FLAG_EXTRA_NODE
))
630 X509_policy_tree_free(X509_POLICY_TREE
*tree
)
632 X509_POLICY_LEVEL
*curr
;
638 sk_X509_POLICY_NODE_free(tree
->auth_policies
);
639 sk_X509_POLICY_NODE_pop_free(tree
->user_policies
, exnode_free
);
641 for (i
= 0, curr
= tree
->levels
; i
< tree
->nlevel
; i
++, curr
++) {
642 X509_free(curr
->cert
);
644 sk_X509_POLICY_NODE_pop_free(curr
->nodes
,
647 policy_node_free(curr
->anyPolicy
);
650 if (tree
->extra_data
)
651 sk_X509_POLICY_DATA_pop_free(tree
->extra_data
,
658 /* Application policy checking function.
662 * -1 One or more certificates contain invalid or inconsistent extensions
663 * -2 User constrained policy set empty and requireExplicit true.
667 X509_policy_check(X509_POLICY_TREE
**ptree
, int *pexplicit_policy
,
668 STACK_OF(X509
) *certs
, STACK_OF(ASN1_OBJECT
) *policy_oids
,
672 X509_POLICY_TREE
*tree
= NULL
;
673 STACK_OF(X509_POLICY_NODE
) *nodes
, *auth_nodes
= NULL
;
676 *pexplicit_policy
= 0;
677 ret
= tree_init(&tree
, certs
, flags
);
681 /* Tree empty requireExplicit False: OK */
685 /* Some internal error */
689 /* Some internal error */
693 /* Tree empty requireExplicit True: Error */
696 *pexplicit_policy
= 1;
699 /* Tree OK requireExplicit True: OK and continue */
701 *pexplicit_policy
= 1;
704 /* Tree OK: continue */
709 * tree_init() returns success and a null tree
710 * if it's just looking at a trust anchor.
711 * I'm not sure that returning success here is
712 * correct, but I'm sure that reporting this
713 * as an internal error which our caller
714 * interprets as a malloc failure is wrong.
722 ret
= tree_evaluate(tree
);
724 tree_print("tree_evaluate()", tree
, NULL
);
729 /* Return value 2 means tree empty */
731 X509_policy_tree_free(tree
);
732 if (*pexplicit_policy
)
738 /* Tree is not empty: continue */
740 ret
= tree_calculate_authority_set(tree
, &auth_nodes
);
744 ret2
= tree_calculate_user_set(tree
, policy_oids
, auth_nodes
);
746 /* Return value 2 means auth_nodes needs to be freed */
748 sk_X509_POLICY_NODE_free(auth_nodes
);
756 if (*pexplicit_policy
) {
757 nodes
= X509_policy_tree_get0_user_policies(tree
);
758 if (sk_X509_POLICY_NODE_num(nodes
) <= 0)
765 X509_policy_tree_free(tree
);