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>
65 /* Enable this to print out the complete policy tree at various point during
69 /*#define OPENSSL_POLICY_DEBUG*/
71 #ifdef OPENSSL_POLICY_DEBUG
73 static void expected_print(BIO
*err
, X509_POLICY_LEVEL
*lev
,
74 X509_POLICY_NODE
*node
, int indent
)
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");
82 STACK_OF(ASN1_OBJECT
) *pset
= node
->data
->expected_policy_set
;
84 BIO_puts(err
, " Expected: ");
85 for (i
= 0; i
< sk_ASN1_OBJECT_num(pset
); i
++)
87 oid
= sk_ASN1_OBJECT_value(pset
, i
);
90 i2a_ASN1_OBJECT(err
, oid
);
96 static void tree_print(char *str
, X509_POLICY_TREE
*tree
,
97 X509_POLICY_LEVEL
*curr
)
99 X509_POLICY_LEVEL
*plev
;
100 X509_POLICY_NODE
*node
;
103 err
= BIO_new_fp(stderr
, BIO_NOCLOSE
);
107 curr
= tree
->levels
+ tree
->nlevel
;
110 BIO_printf(err
, "Level print after %s\n", str
);
111 BIO_printf(err
, "Printing Up to Level %ld\n", curr
- tree
->levels
);
112 for (plev
= tree
->levels
; plev
!= curr
; plev
++)
114 BIO_printf(err
, "Level %ld, flags = %x\n",
115 plev
- tree
->levels
, plev
->flags
);
116 for (i
= 0; i
< sk_X509_POLICY_NODE_num(plev
->nodes
); i
++)
118 node
= sk_X509_POLICY_NODE_value(plev
->nodes
, i
);
119 X509_POLICY_NODE_print(err
, node
, 2);
120 expected_print(err
, plev
, node
, 2);
121 BIO_printf(err
, " Flags: %x\n", node
->data
->flags
);
124 X509_POLICY_NODE_print(err
, plev
->anyPolicy
, 2);
132 #define tree_print(a,b,c) /* */
136 /* Initialize policy tree. Return values:
137 * 0 Some internal error occurred.
138 * -1 Inconsistent or invalid extensions in certificates.
139 * 1 Tree initialized OK.
140 * 2 Policy tree is empty.
141 * 5 Tree OK and requireExplicitPolicy true.
142 * 6 Tree empty and requireExplicitPolicy true.
145 static int tree_init(X509_POLICY_TREE
**ptree
, STACK_OF(X509
) *certs
,
148 X509_POLICY_TREE
*tree
;
149 X509_POLICY_LEVEL
*level
;
150 const X509_POLICY_CACHE
*cache
;
151 X509_POLICY_DATA
*data
= NULL
;
159 n
= sk_X509_num(certs
);
162 /* Disable policy mapping for now... */
163 flags
|= X509_V_FLAG_INHIBIT_MAP
;
166 if (flags
& X509_V_FLAG_EXPLICIT_POLICY
)
169 explicit_policy
= n
+ 1;
171 if (flags
& X509_V_FLAG_INHIBIT_ANY
)
176 if (flags
& X509_V_FLAG_INHIBIT_MAP
)
181 /* Can't do anything with just a trust anchor */
184 /* First setup policy cache in all certificates apart from the
185 * trust anchor. Note any bad cache results on the way. Also can
186 * calculate explicit_policy value at this point.
188 for (i
= n
- 2; i
>= 0; i
--)
190 x
= sk_X509_value(certs
, i
);
191 X509_check_purpose(x
, -1, -1);
192 cache
= policy_cache_set(x
);
193 /* If cache NULL something bad happened: return immediately */
196 /* If inconsistent extensions keep a note of it but continue */
197 if (x
->ex_flags
& EXFLAG_INVALID_POLICY
)
199 /* Otherwise if we have no data (hence no CertificatePolicies)
200 * and haven't already set an inconsistent code note it.
202 else if ((ret
== 1) && !cache
->data
)
204 if (explicit_policy
> 0)
206 if (!(x
->ex_flags
& EXFLAG_SI
))
208 if ((cache
->explicit_skip
!= -1)
209 && (cache
->explicit_skip
< explicit_policy
))
210 explicit_policy
= cache
->explicit_skip
;
216 if (ret
== 2 && !explicit_policy
)
222 /* If we get this far initialize the tree */
224 tree
= OPENSSL_malloc(sizeof(X509_POLICY_TREE
));
230 tree
->levels
= OPENSSL_malloc(sizeof(X509_POLICY_LEVEL
) * n
);
232 tree
->extra_data
= NULL
;
233 tree
->auth_policies
= NULL
;
234 tree
->user_policies
= NULL
;
242 memset(tree
->levels
, 0, n
* sizeof(X509_POLICY_LEVEL
));
246 level
= tree
->levels
;
248 /* Root data: initialize to anyPolicy */
250 data
= policy_data_new(NULL
, OBJ_nid2obj(NID_any_policy
), 0);
252 if (!data
|| !level_add_node(level
, data
, NULL
, tree
))
255 for (i
= n
- 2; i
>= 0; i
--)
258 x
= sk_X509_value(certs
, i
);
259 cache
= policy_cache_set(x
);
260 CRYPTO_add(&x
->references
, 1, CRYPTO_LOCK_X509
);
263 if (!cache
->anyPolicy
)
264 level
->flags
|= X509_V_FLAG_INHIBIT_ANY
;
266 /* Determine inhibit any and inhibit map flags */
269 /* Any matching allowed if certificate is self
270 * issued and not the last in the chain.
272 if (!(x
->ex_flags
& EXFLAG_SI
) || (i
== 0))
273 level
->flags
|= X509_V_FLAG_INHIBIT_ANY
;
277 if (!(x
->ex_flags
& EXFLAG_SI
))
279 if ((cache
->any_skip
>= 0)
280 && (cache
->any_skip
< any_skip
))
281 any_skip
= cache
->any_skip
;
285 level
->flags
|= X509_V_FLAG_INHIBIT_MAP
;
288 if (!(x
->ex_flags
& EXFLAG_SI
))
290 if ((cache
->map_skip
>= 0)
291 && (cache
->map_skip
< map_skip
))
292 map_skip
= cache
->map_skip
;
306 X509_policy_tree_free(tree
);
312 static int tree_link_matching_nodes(X509_POLICY_LEVEL
*curr
,
313 const X509_POLICY_DATA
*data
)
315 X509_POLICY_LEVEL
*last
= curr
- 1;
316 X509_POLICY_NODE
*node
;
318 /* Iterate through all in nodes linking matches */
319 for (i
= 0; i
< sk_X509_POLICY_NODE_num(last
->nodes
); i
++)
321 node
= sk_X509_POLICY_NODE_value(last
->nodes
, i
);
322 if (policy_node_match(last
, node
, data
->valid_policy
))
324 if (!level_add_node(curr
, data
, node
, NULL
))
329 if (!matched
&& last
->anyPolicy
)
331 if (!level_add_node(curr
, data
, last
->anyPolicy
, NULL
))
337 /* This corresponds to RFC3280 6.1.3(d)(1):
338 * link any data from CertificatePolicies onto matching parent
339 * or anyPolicy if no match.
342 static int tree_link_nodes(X509_POLICY_LEVEL
*curr
,
343 const X509_POLICY_CACHE
*cache
)
346 X509_POLICY_DATA
*data
;
348 for (i
= 0; i
< sk_X509_POLICY_DATA_num(cache
->data
); i
++)
350 data
= sk_X509_POLICY_DATA_value(cache
->data
, i
);
351 /* If a node is mapped any it doesn't have a corresponding
352 * CertificatePolicies entry.
353 * However such an identical node would be created
354 * if anyPolicy matching is enabled because there would be
355 * no match with the parent valid_policy_set. So we create
356 * link because then it will have the mapping flags
357 * right and we can prune it later.
360 if ((data
->flags
& POLICY_DATA_FLAG_MAPPED_ANY
)
361 && !(curr
->flags
& X509_V_FLAG_INHIBIT_ANY
))
364 /* Look for matching nodes in previous level */
365 if (!tree_link_matching_nodes(curr
, data
))
371 /* This corresponds to RFC3280 6.1.3(d)(2):
372 * Create new data for any unmatched policies in the parent and link
376 static int tree_add_unmatched(X509_POLICY_LEVEL
*curr
,
377 const X509_POLICY_CACHE
*cache
,
378 const ASN1_OBJECT
*id
,
379 X509_POLICY_NODE
*node
,
380 X509_POLICY_TREE
*tree
)
382 X509_POLICY_DATA
*data
;
384 id
= node
->data
->valid_policy
;
385 /* Create a new node with qualifiers from anyPolicy and
386 * id from unmatched node.
388 data
= policy_data_new(NULL
, id
, node_critical(node
));
392 /* Curr may not have anyPolicy */
393 data
->qualifier_set
= cache
->anyPolicy
->qualifier_set
;
394 data
->flags
|= POLICY_DATA_FLAG_SHARED_QUALIFIERS
;
395 if (!level_add_node(curr
, data
, node
, tree
))
397 policy_data_free(data
);
404 static int tree_link_unmatched(X509_POLICY_LEVEL
*curr
,
405 const X509_POLICY_CACHE
*cache
,
406 X509_POLICY_NODE
*node
,
407 X509_POLICY_TREE
*tree
)
409 const X509_POLICY_LEVEL
*last
= curr
- 1;
412 if ( (last
->flags
& X509_V_FLAG_INHIBIT_MAP
)
413 || !(node
->data
->flags
& POLICY_DATA_FLAG_MAPPED
))
415 /* If no policy mapping: matched if one child present */
418 if (!tree_add_unmatched(curr
, cache
, NULL
, node
, tree
))
424 /* If mapping: matched if one child per expected policy set */
425 STACK_OF(ASN1_OBJECT
) *expset
= node
->data
->expected_policy_set
;
426 if (node
->nchild
== sk_ASN1_OBJECT_num(expset
))
428 /* Locate unmatched nodes */
429 for (i
= 0; i
< sk_ASN1_OBJECT_num(expset
); i
++)
431 ASN1_OBJECT
*oid
= sk_ASN1_OBJECT_value(expset
, i
);
432 if (level_find_node(curr
, node
, oid
))
434 if (!tree_add_unmatched(curr
, cache
, oid
, node
, tree
))
444 static int tree_link_any(X509_POLICY_LEVEL
*curr
,
445 const X509_POLICY_CACHE
*cache
,
446 X509_POLICY_TREE
*tree
)
449 /*X509_POLICY_DATA *data;*/
450 X509_POLICY_NODE
*node
;
451 X509_POLICY_LEVEL
*last
= curr
- 1;
453 for (i
= 0; i
< sk_X509_POLICY_NODE_num(last
->nodes
); i
++)
455 node
= sk_X509_POLICY_NODE_value(last
->nodes
, i
);
457 if (!tree_link_unmatched(curr
, cache
, node
, tree
))
462 /* Skip any node with any children: we only want unmathced
465 * Note: need something better for policy mapping
466 * because each node may have multiple children
471 /* Create a new node with qualifiers from anyPolicy and
472 * id from unmatched node.
474 data
= policy_data_new(NULL
, node
->data
->valid_policy
,
475 node_critical(node
));
479 /* Curr may not have anyPolicy */
480 data
->qualifier_set
= cache
->anyPolicy
->qualifier_set
;
481 data
->flags
|= POLICY_DATA_FLAG_SHARED_QUALIFIERS
;
482 if (!level_add_node(curr
, data
, node
, tree
))
484 policy_data_free(data
);
491 /* Finally add link to anyPolicy */
494 if (!level_add_node(curr
, cache
->anyPolicy
,
495 last
->anyPolicy
, NULL
))
501 /* Prune the tree: delete any child mapped child data on the current level
502 * then proceed up the tree deleting any data with no children. If we ever
503 * have no data on a level we can halt because the tree will be empty.
506 static int tree_prune(X509_POLICY_TREE
*tree
, X509_POLICY_LEVEL
*curr
)
508 STACK_OF(X509_POLICY_NODE
) *nodes
;
509 X509_POLICY_NODE
*node
;
512 if (curr
->flags
& X509_V_FLAG_INHIBIT_MAP
)
514 for (i
= sk_X509_POLICY_NODE_num(nodes
) - 1; i
>= 0; i
--)
516 node
= sk_X509_POLICY_NODE_value(nodes
, i
);
517 /* Delete any mapped data: see RFC3280 XXXX */
518 if (node
->data
->flags
& POLICY_DATA_FLAG_MAP_MASK
)
520 node
->parent
->nchild
--;
522 (void)sk_X509_POLICY_NODE_delete(nodes
,i
);
530 for (i
= sk_X509_POLICY_NODE_num(nodes
) - 1; i
>= 0; i
--)
532 node
= sk_X509_POLICY_NODE_value(nodes
, i
);
533 if (node
->nchild
== 0)
535 node
->parent
->nchild
--;
537 (void)sk_X509_POLICY_NODE_delete(nodes
, i
);
540 if (curr
->anyPolicy
&& !curr
->anyPolicy
->nchild
)
542 if (curr
->anyPolicy
->parent
)
543 curr
->anyPolicy
->parent
->nchild
--;
544 OPENSSL_free(curr
->anyPolicy
);
545 curr
->anyPolicy
= NULL
;
547 if (curr
== tree
->levels
)
549 /* If we zapped anyPolicy at top then tree is empty */
550 if (!curr
->anyPolicy
)
560 static int tree_add_auth_node(STACK_OF(X509_POLICY_NODE
) **pnodes
,
561 X509_POLICY_NODE
*pcy
)
565 *pnodes
= policy_node_cmp_new();
569 else if (sk_X509_POLICY_NODE_find(*pnodes
, pcy
) != -1)
572 if (!sk_X509_POLICY_NODE_push(*pnodes
, pcy
))
579 /* Calculate the authority set based on policy tree.
580 * The 'pnodes' parameter is used as a store for the set of policy nodes
581 * used to calculate the user set. If the authority set is not anyPolicy
582 * then pnodes will just point to the authority set. If however the authority
583 * set is anyPolicy then the set of valid policies (other than anyPolicy)
584 * is store in pnodes. The return value of '2' is used in this case to indicate
585 * that pnodes should be freed.
588 static int tree_calculate_authority_set(X509_POLICY_TREE
*tree
,
589 STACK_OF(X509_POLICY_NODE
) **pnodes
)
591 X509_POLICY_LEVEL
*curr
;
592 X509_POLICY_NODE
*node
, *anyptr
;
593 STACK_OF(X509_POLICY_NODE
) **addnodes
;
595 curr
= tree
->levels
+ tree
->nlevel
- 1;
597 /* If last level contains anyPolicy set is anyPolicy */
600 if (!tree_add_auth_node(&tree
->auth_policies
, curr
->anyPolicy
))
605 /* Add policies to authority set */
606 addnodes
= &tree
->auth_policies
;
609 for (i
= 1; i
< tree
->nlevel
; i
++)
611 /* If no anyPolicy node on this this level it can't
612 * appear on lower levels so end search.
614 if (!(anyptr
= curr
->anyPolicy
))
617 for (j
= 0; j
< sk_X509_POLICY_NODE_num(curr
->nodes
); j
++)
619 node
= sk_X509_POLICY_NODE_value(curr
->nodes
, j
);
620 if ((node
->parent
== anyptr
)
621 && !tree_add_auth_node(addnodes
, node
))
626 if (addnodes
== pnodes
)
629 *pnodes
= tree
->auth_policies
;
634 static int tree_calculate_user_set(X509_POLICY_TREE
*tree
,
635 STACK_OF(ASN1_OBJECT
) *policy_oids
,
636 STACK_OF(X509_POLICY_NODE
) *auth_nodes
)
639 X509_POLICY_NODE
*node
;
642 X509_POLICY_NODE
*anyPolicy
;
643 X509_POLICY_DATA
*extra
;
645 /* Check if anyPolicy present in authority constrained policy set:
646 * this will happen if it is a leaf node.
649 if (sk_ASN1_OBJECT_num(policy_oids
) <= 0)
652 anyPolicy
= tree
->levels
[tree
->nlevel
- 1].anyPolicy
;
654 for (i
= 0; i
< sk_ASN1_OBJECT_num(policy_oids
); i
++)
656 oid
= sk_ASN1_OBJECT_value(policy_oids
, i
);
657 if (OBJ_obj2nid(oid
) == NID_any_policy
)
659 tree
->flags
|= POLICY_FLAG_ANY_POLICY
;
664 for (i
= 0; i
< sk_ASN1_OBJECT_num(policy_oids
); i
++)
666 oid
= sk_ASN1_OBJECT_value(policy_oids
, i
);
667 node
= tree_find_sk(auth_nodes
, oid
);
672 /* Create a new node with policy ID from user set
673 * and qualifiers from anyPolicy.
675 extra
= policy_data_new(NULL
, oid
,
676 node_critical(anyPolicy
));
679 extra
->qualifier_set
= anyPolicy
->data
->qualifier_set
;
680 extra
->flags
= POLICY_DATA_FLAG_SHARED_QUALIFIERS
681 | POLICY_DATA_FLAG_EXTRA_NODE
;
682 node
= level_add_node(NULL
, extra
, anyPolicy
->parent
,
685 if (!tree
->user_policies
)
687 tree
->user_policies
= sk_X509_POLICY_NODE_new_null();
688 if (!tree
->user_policies
)
691 if (!sk_X509_POLICY_NODE_push(tree
->user_policies
, node
))
698 static int tree_evaluate(X509_POLICY_TREE
*tree
)
701 X509_POLICY_LEVEL
*curr
= tree
->levels
+ 1;
702 const X509_POLICY_CACHE
*cache
;
704 for(i
= 1; i
< tree
->nlevel
; i
++, curr
++)
706 cache
= policy_cache_set(curr
->cert
);
707 if (!tree_link_nodes(curr
, cache
))
710 if (!(curr
->flags
& X509_V_FLAG_INHIBIT_ANY
)
711 && !tree_link_any(curr
, cache
, tree
))
713 tree_print("before tree_prune()", tree
, curr
);
714 ret
= tree_prune(tree
, curr
);
723 static void exnode_free(X509_POLICY_NODE
*node
)
725 if (node
->data
&& (node
->data
->flags
& POLICY_DATA_FLAG_EXTRA_NODE
))
730 void X509_policy_tree_free(X509_POLICY_TREE
*tree
)
732 X509_POLICY_LEVEL
*curr
;
738 sk_X509_POLICY_NODE_free(tree
->auth_policies
);
739 sk_X509_POLICY_NODE_pop_free(tree
->user_policies
, exnode_free
);
741 for(i
= 0, curr
= tree
->levels
; i
< tree
->nlevel
; i
++, curr
++)
744 X509_free(curr
->cert
);
746 sk_X509_POLICY_NODE_pop_free(curr
->nodes
,
749 policy_node_free(curr
->anyPolicy
);
752 if (tree
->extra_data
)
753 sk_X509_POLICY_DATA_pop_free(tree
->extra_data
,
756 OPENSSL_free(tree
->levels
);
762 * Application policy checking function.
766 * -1 One or more certificates contain invalid or inconsistent extensions
767 * -2 User constrained policy set empty and requireExplicit true.
770 int X509_policy_check(X509_POLICY_TREE
**ptree
, int *pexplicit_policy
,
771 STACK_OF(X509
) *certs
,
772 STACK_OF(ASN1_OBJECT
) *policy_oids
,
776 X509_POLICY_TREE
*tree
= NULL
;
777 STACK_OF(X509_POLICY_NODE
) *nodes
, *auth_nodes
= NULL
;
780 *pexplicit_policy
= 0;
781 ret
= tree_init(&tree
, certs
, flags
);
786 /* Tree empty requireExplicit False: OK */
790 /* Some internal error */
794 /* Some internal error */
798 /* Tree empty requireExplicit True: Error */
801 *pexplicit_policy
= 1;
804 /* Tree OK requireExplicit True: OK and continue */
806 *pexplicit_policy
= 1;
809 /* Tree OK: continue */
814 * tree_init() returns success and a null tree
815 * if it's just looking at a trust anchor.
816 * I'm not sure that returning success here is
817 * correct, but I'm sure that reporting this
818 * as an internal error which our caller
819 * interprets as a malloc failure is wrong.
825 if (!tree
) goto error
;
826 ret
= tree_evaluate(tree
);
828 tree_print("tree_evaluate()", tree
, NULL
);
833 /* Return value 2 means tree empty */
836 X509_policy_tree_free(tree
);
837 if (*pexplicit_policy
)
843 /* Tree is not empty: continue */
845 ret
= tree_calculate_authority_set(tree
, &auth_nodes
);
850 if (!tree_calculate_user_set(tree
, policy_oids
, auth_nodes
))
854 sk_X509_POLICY_NODE_free(auth_nodes
);
859 if (*pexplicit_policy
)
861 nodes
= X509_policy_tree_get0_user_policies(tree
);
862 if (sk_X509_POLICY_NODE_num(nodes
) <= 0)
870 X509_policy_tree_free(tree
);