1 /* $OpenBSD: pcy_tree.c,v 1.17 2016/11/05 15:21:20 miod 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 if ((err
= BIO_new_fp(stderr
, BIO_NOCLOSE
)) == NULL
)
106 curr
= tree
->levels
+ tree
->nlevel
;
109 BIO_printf(err
, "Level print after %s\n", str
);
110 BIO_printf(err
, "Printing Up to Level %ld\n", curr
- tree
->levels
);
111 for (plev
= tree
->levels
; plev
!= curr
; plev
++) {
112 BIO_printf(err
, "Level %ld, flags = %x\n",
113 plev
- tree
->levels
, plev
->flags
);
114 for (i
= 0; i
< sk_X509_POLICY_NODE_num(plev
->nodes
); i
++) {
115 node
= sk_X509_POLICY_NODE_value(plev
->nodes
, i
);
116 X509_POLICY_NODE_print(err
, node
, 2);
117 expected_print(err
, plev
, node
, 2);
118 BIO_printf(err
, " Flags: %x\n", node
->data
->flags
);
121 X509_POLICY_NODE_print(err
, plev
->anyPolicy
, 2);
128 #define tree_print(a,b,c) /* */
132 /* Initialize policy tree. Return values:
133 * 0 Some internal error occured.
134 * -1 Inconsistent or invalid extensions in certificates.
135 * 1 Tree initialized OK.
136 * 2 Policy tree is empty.
137 * 5 Tree OK and requireExplicitPolicy true.
138 * 6 Tree empty and requireExplicitPolicy true.
142 tree_init(X509_POLICY_TREE
**ptree
, STACK_OF(X509
) *certs
, unsigned int flags
)
144 X509_POLICY_TREE
*tree
;
145 X509_POLICY_LEVEL
*level
;
146 const X509_POLICY_CACHE
*cache
;
147 X509_POLICY_DATA
*data
= NULL
;
156 n
= sk_X509_num(certs
);
158 if (flags
& X509_V_FLAG_EXPLICIT_POLICY
)
161 explicit_policy
= n
+ 1;
163 if (flags
& X509_V_FLAG_INHIBIT_ANY
)
168 if (flags
& X509_V_FLAG_INHIBIT_MAP
)
173 /* Can't do anything with just a trust anchor */
176 /* First setup policy cache in all certificates apart from the
177 * trust anchor. Note any bad cache results on the way. Also can
178 * calculate explicit_policy value at this point.
180 for (i
= n
- 2; i
>= 0; i
--) {
181 x
= sk_X509_value(certs
, i
);
182 X509_check_purpose(x
, -1, -1);
183 cache
= policy_cache_set(x
);
184 /* If cache NULL something bad happened: return immediately */
187 /* If inconsistent extensions keep a note of it but continue */
188 if (x
->ex_flags
& EXFLAG_INVALID_POLICY
)
190 /* Otherwise if we have no data (hence no CertificatePolicies)
191 * and haven't already set an inconsistent code note it.
193 else if ((ret
== 1) && !cache
->data
)
195 if (explicit_policy
> 0) {
196 if (!(x
->ex_flags
& EXFLAG_SI
))
198 if ((cache
->explicit_skip
!= -1) &&
199 (cache
->explicit_skip
< explicit_policy
))
200 explicit_policy
= cache
->explicit_skip
;
205 if (ret
== 2 && !explicit_policy
)
211 /* If we get this far initialize the tree */
213 tree
= malloc(sizeof(X509_POLICY_TREE
));
219 tree
->levels
= calloc(n
, sizeof(X509_POLICY_LEVEL
));
221 tree
->extra_data
= NULL
;
222 tree
->auth_policies
= NULL
;
223 tree
->user_policies
= NULL
;
232 level
= tree
->levels
;
234 /* Root data: initialize to anyPolicy */
236 data
= policy_data_new(NULL
, OBJ_nid2obj(NID_any_policy
), 0);
238 if (!data
|| !level_add_node(level
, data
, NULL
, tree
, NULL
))
241 for (i
= n
- 2; i
>= 0; i
--) {
243 x
= sk_X509_value(certs
, i
);
244 cache
= policy_cache_set(x
);
245 CRYPTO_add(&x
->references
, 1, CRYPTO_LOCK_X509
);
248 if (!cache
->anyPolicy
)
249 level
->flags
|= X509_V_FLAG_INHIBIT_ANY
;
251 /* Determine inhibit any and inhibit map flags */
253 /* Any matching allowed if certificate is self
254 * issued and not the last in the chain.
256 if (!(x
->ex_flags
& EXFLAG_SI
) || (i
== 0))
257 level
->flags
|= X509_V_FLAG_INHIBIT_ANY
;
259 if (!(x
->ex_flags
& EXFLAG_SI
))
261 if ((cache
->any_skip
>= 0) &&
262 (cache
->any_skip
< any_skip
))
263 any_skip
= cache
->any_skip
;
267 level
->flags
|= X509_V_FLAG_INHIBIT_MAP
;
269 if (!(x
->ex_flags
& EXFLAG_SI
))
271 if ((cache
->map_skip
>= 0) &&
272 (cache
->map_skip
< map_skip
))
273 map_skip
= cache
->map_skip
;
286 X509_policy_tree_free(tree
);
292 tree_link_matching_nodes(X509_POLICY_LEVEL
*curr
, const X509_POLICY_DATA
*data
)
294 X509_POLICY_LEVEL
*last
= curr
- 1;
295 X509_POLICY_NODE
*node
;
298 /* Iterate through all in nodes linking matches */
299 for (i
= 0; i
< sk_X509_POLICY_NODE_num(last
->nodes
); i
++) {
300 node
= sk_X509_POLICY_NODE_value(last
->nodes
, i
);
301 if (policy_node_match(last
, node
, data
->valid_policy
)) {
302 if (!level_add_node(curr
, data
, node
, NULL
, NULL
))
307 if (!matched
&& last
->anyPolicy
) {
308 if (!level_add_node(curr
, data
, last
->anyPolicy
, NULL
, NULL
))
314 /* This corresponds to RFC3280 6.1.3(d)(1):
315 * link any data from CertificatePolicies onto matching parent
316 * or anyPolicy if no match.
320 tree_link_nodes(X509_POLICY_LEVEL
*curr
, const X509_POLICY_CACHE
*cache
)
323 X509_POLICY_DATA
*data
;
325 for (i
= 0; i
< sk_X509_POLICY_DATA_num(cache
->data
); i
++) {
326 data
= sk_X509_POLICY_DATA_value(cache
->data
, i
);
327 /* Look for matching nodes in previous level */
328 if (!tree_link_matching_nodes(curr
, data
))
334 /* This corresponds to RFC3280 6.1.3(d)(2):
335 * Create new data for any unmatched policies in the parent and link
340 tree_add_unmatched(X509_POLICY_LEVEL
*curr
, const X509_POLICY_CACHE
*cache
,
341 const ASN1_OBJECT
*id
, X509_POLICY_NODE
*node
, X509_POLICY_TREE
*tree
)
343 X509_POLICY_DATA
*data
;
346 id
= node
->data
->valid_policy
;
347 /* Create a new node with qualifiers from anyPolicy and
348 * id from unmatched node.
350 data
= policy_data_new(NULL
, id
, node_critical(node
));
354 /* Curr may not have anyPolicy */
355 data
->qualifier_set
= cache
->anyPolicy
->qualifier_set
;
356 data
->flags
|= POLICY_DATA_FLAG_SHARED_QUALIFIERS
;
357 if (!level_add_node(curr
, data
, node
, tree
, NULL
)) {
358 policy_data_free(data
);
366 tree_link_unmatched(X509_POLICY_LEVEL
*curr
, const X509_POLICY_CACHE
*cache
,
367 X509_POLICY_NODE
*node
, X509_POLICY_TREE
*tree
)
369 const X509_POLICY_LEVEL
*last
= curr
- 1;
372 if ((last
->flags
& X509_V_FLAG_INHIBIT_MAP
) ||
373 !(node
->data
->flags
& POLICY_DATA_FLAG_MAPPED
)) {
374 /* If no policy mapping: matched if one child present */
377 if (!tree_add_unmatched(curr
, cache
, NULL
, node
, tree
))
381 /* If mapping: matched if one child per expected policy set */
382 STACK_OF(ASN1_OBJECT
) *expset
= node
->data
->expected_policy_set
;
383 if (node
->nchild
== sk_ASN1_OBJECT_num(expset
))
385 /* Locate unmatched nodes */
386 for (i
= 0; i
< sk_ASN1_OBJECT_num(expset
); i
++) {
387 ASN1_OBJECT
*oid
= sk_ASN1_OBJECT_value(expset
, i
);
388 if (level_find_node(curr
, node
, oid
))
390 if (!tree_add_unmatched(curr
, cache
, oid
, node
, tree
))
399 tree_link_any(X509_POLICY_LEVEL
*curr
, const X509_POLICY_CACHE
*cache
,
400 X509_POLICY_TREE
*tree
)
403 X509_POLICY_NODE
*node
;
404 X509_POLICY_LEVEL
*last
= curr
- 1;
406 for (i
= 0; i
< sk_X509_POLICY_NODE_num(last
->nodes
); i
++) {
407 node
= sk_X509_POLICY_NODE_value(last
->nodes
, i
);
409 if (!tree_link_unmatched(curr
, cache
, node
, tree
))
412 /* Finally add link to anyPolicy */
413 if (last
->anyPolicy
) {
414 if (!level_add_node(curr
, cache
->anyPolicy
,
415 last
->anyPolicy
, NULL
, NULL
))
421 /* Prune the tree: delete any child mapped child data on the current level
422 * then proceed up the tree deleting any data with no children. If we ever
423 * have no data on a level we can halt because the tree will be empty.
427 tree_prune(X509_POLICY_TREE
*tree
, X509_POLICY_LEVEL
*curr
)
429 STACK_OF(X509_POLICY_NODE
) *nodes
;
430 X509_POLICY_NODE
*node
;
434 if (curr
->flags
& X509_V_FLAG_INHIBIT_MAP
) {
435 for (i
= sk_X509_POLICY_NODE_num(nodes
) - 1; i
>= 0; i
--) {
436 node
= sk_X509_POLICY_NODE_value(nodes
, i
);
437 /* Delete any mapped data: see RFC3280 XXXX */
438 if (node
->data
->flags
& POLICY_DATA_FLAG_MAP_MASK
) {
439 node
->parent
->nchild
--;
441 (void)sk_X509_POLICY_NODE_delete(nodes
, i
);
449 for (i
= sk_X509_POLICY_NODE_num(nodes
) - 1; i
>= 0; i
--) {
450 node
= sk_X509_POLICY_NODE_value(nodes
, i
);
451 if (node
->nchild
== 0) {
452 node
->parent
->nchild
--;
454 (void)sk_X509_POLICY_NODE_delete(nodes
, i
);
457 if (curr
->anyPolicy
&& !curr
->anyPolicy
->nchild
) {
458 if (curr
->anyPolicy
->parent
)
459 curr
->anyPolicy
->parent
->nchild
--;
460 free(curr
->anyPolicy
);
461 curr
->anyPolicy
= NULL
;
463 if (curr
== tree
->levels
) {
464 /* If we zapped anyPolicy at top then tree is empty */
465 if (!curr
->anyPolicy
)
475 tree_add_auth_node(STACK_OF(X509_POLICY_NODE
) **pnodes
, X509_POLICY_NODE
*pcy
)
478 *pnodes
= policy_node_cmp_new();
481 } else if (sk_X509_POLICY_NODE_find(*pnodes
, pcy
) != -1)
484 if (!sk_X509_POLICY_NODE_push(*pnodes
, pcy
))
490 /* Calculate the authority set based on policy tree.
491 * The 'pnodes' parameter is used as a store for the set of policy nodes
492 * used to calculate the user set. If the authority set is not anyPolicy
493 * then pnodes will just point to the authority set. If however the authority
494 * set is anyPolicy then the set of valid policies (other than anyPolicy)
495 * is store in pnodes. The return value of '2' is used in this case to indicate
496 * that pnodes should be freed.
500 tree_calculate_authority_set(X509_POLICY_TREE
*tree
,
501 STACK_OF(X509_POLICY_NODE
) **pnodes
)
503 X509_POLICY_LEVEL
*curr
;
504 X509_POLICY_NODE
*node
, *anyptr
;
505 STACK_OF(X509_POLICY_NODE
) **addnodes
;
508 curr
= tree
->levels
+ tree
->nlevel
- 1;
510 /* If last level contains anyPolicy set is anyPolicy */
511 if (curr
->anyPolicy
) {
512 if (!tree_add_auth_node(&tree
->auth_policies
, curr
->anyPolicy
))
516 /* Add policies to authority set */
517 addnodes
= &tree
->auth_policies
;
520 for (i
= 1; i
< tree
->nlevel
; i
++) {
521 /* If no anyPolicy node on this this level it can't
522 * appear on lower levels so end search.
524 if (!(anyptr
= curr
->anyPolicy
))
527 for (j
= 0; j
< sk_X509_POLICY_NODE_num(curr
->nodes
); j
++) {
528 node
= sk_X509_POLICY_NODE_value(curr
->nodes
, j
);
529 if ((node
->parent
== anyptr
) &&
530 !tree_add_auth_node(addnodes
, node
))
535 if (addnodes
== pnodes
)
538 *pnodes
= tree
->auth_policies
;
544 tree_calculate_user_set(X509_POLICY_TREE
*tree
,
545 STACK_OF(ASN1_OBJECT
) *policy_oids
, STACK_OF(X509_POLICY_NODE
) *auth_nodes
)
548 X509_POLICY_NODE
*node
;
550 X509_POLICY_NODE
*anyPolicy
;
551 X509_POLICY_DATA
*extra
;
553 /* Check if anyPolicy present in authority constrained policy set:
554 * this will happen if it is a leaf node.
557 if (sk_ASN1_OBJECT_num(policy_oids
) <= 0)
560 anyPolicy
= tree
->levels
[tree
->nlevel
- 1].anyPolicy
;
562 for (i
= 0; i
< sk_ASN1_OBJECT_num(policy_oids
); i
++) {
563 oid
= sk_ASN1_OBJECT_value(policy_oids
, i
);
564 if (OBJ_obj2nid(oid
) == NID_any_policy
) {
565 tree
->flags
|= POLICY_FLAG_ANY_POLICY
;
570 for (i
= 0; i
< sk_ASN1_OBJECT_num(policy_oids
); i
++) {
571 oid
= sk_ASN1_OBJECT_value(policy_oids
, i
);
572 node
= tree_find_sk(auth_nodes
, oid
);
576 /* Create a new node with policy ID from user set
577 * and qualifiers from anyPolicy.
579 extra
= policy_data_new(NULL
, oid
,
580 node_critical(anyPolicy
));
583 extra
->qualifier_set
= anyPolicy
->data
->qualifier_set
;
584 extra
->flags
= POLICY_DATA_FLAG_SHARED_QUALIFIERS
|
585 POLICY_DATA_FLAG_EXTRA_NODE
;
586 (void) level_add_node(NULL
, extra
, anyPolicy
->parent
,
589 if (!tree
->user_policies
) {
590 tree
->user_policies
= sk_X509_POLICY_NODE_new_null();
591 if (!tree
->user_policies
)
594 if (!sk_X509_POLICY_NODE_push(tree
->user_policies
, node
))
601 tree_evaluate(X509_POLICY_TREE
*tree
)
604 X509_POLICY_LEVEL
*curr
= tree
->levels
+ 1;
605 const X509_POLICY_CACHE
*cache
;
607 for (i
= 1; i
< tree
->nlevel
; i
++, curr
++) {
608 cache
= policy_cache_set(curr
->cert
);
609 if (!tree_link_nodes(curr
, cache
))
612 if (!(curr
->flags
& X509_V_FLAG_INHIBIT_ANY
) &&
613 !tree_link_any(curr
, cache
, tree
))
615 tree_print("before tree_prune()", tree
, curr
);
616 ret
= tree_prune(tree
, curr
);
625 exnode_free(X509_POLICY_NODE
*node
)
627 if (node
->data
&& (node
->data
->flags
& POLICY_DATA_FLAG_EXTRA_NODE
))
632 X509_policy_tree_free(X509_POLICY_TREE
*tree
)
634 X509_POLICY_LEVEL
*curr
;
640 sk_X509_POLICY_NODE_free(tree
->auth_policies
);
641 sk_X509_POLICY_NODE_pop_free(tree
->user_policies
, exnode_free
);
643 for (i
= 0, curr
= tree
->levels
; i
< tree
->nlevel
; i
++, curr
++) {
644 X509_free(curr
->cert
);
646 sk_X509_POLICY_NODE_pop_free(curr
->nodes
,
649 policy_node_free(curr
->anyPolicy
);
652 if (tree
->extra_data
)
653 sk_X509_POLICY_DATA_pop_free(tree
->extra_data
,
660 /* Application policy checking function.
664 * -1 One or more certificates contain invalid or inconsistent extensions
665 * -2 User constrained policy set empty and requireExplicit true.
669 X509_policy_check(X509_POLICY_TREE
**ptree
, int *pexplicit_policy
,
670 STACK_OF(X509
) *certs
, STACK_OF(ASN1_OBJECT
) *policy_oids
,
674 X509_POLICY_TREE
*tree
= NULL
;
675 STACK_OF(X509_POLICY_NODE
) *nodes
, *auth_nodes
= NULL
;
678 *pexplicit_policy
= 0;
679 ret
= tree_init(&tree
, certs
, flags
);
683 /* Tree empty requireExplicit False: OK */
687 /* Some internal error */
691 /* Some internal error */
695 /* Tree empty requireExplicit True: Error */
698 *pexplicit_policy
= 1;
701 /* Tree OK requireExplicit True: OK and continue */
703 *pexplicit_policy
= 1;
706 /* Tree OK: continue */
711 * tree_init() returns success and a null tree
712 * if it's just looking at a trust anchor.
713 * I'm not sure that returning success here is
714 * correct, but I'm sure that reporting this
715 * as an internal error which our caller
716 * interprets as a malloc failure is wrong.
724 ret
= tree_evaluate(tree
);
726 tree_print("tree_evaluate()", tree
, NULL
);
731 /* Return value 2 means tree empty */
733 X509_policy_tree_free(tree
);
734 if (*pexplicit_policy
)
740 /* Tree is not empty: continue */
742 ret
= tree_calculate_authority_set(tree
, &auth_nodes
);
746 ret2
= tree_calculate_user_set(tree
, policy_oids
, auth_nodes
);
748 /* Return value 2 means auth_nodes needs to be freed */
750 sk_X509_POLICY_NODE_free(auth_nodes
);
758 if (*pexplicit_policy
) {
759 nodes
= X509_policy_tree_get0_user_policies(tree
);
760 if (sk_X509_POLICY_NODE_num(nodes
) <= 0)
767 X509_policy_tree_free(tree
);