2 * Copyright 2004-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #include "internal/cryptlib.h"
11 #include <openssl/x509.h>
12 #include <openssl/x509v3.h>
17 * Enable this to print out the complete policy tree at various point during
22 * #define OPENSSL_POLICY_DEBUG
25 #ifdef OPENSSL_POLICY_DEBUG
27 static void expected_print(BIO
*err
, X509_POLICY_LEVEL
*lev
,
28 X509_POLICY_NODE
*node
, int indent
)
30 if ((lev
->flags
& X509_V_FLAG_INHIBIT_MAP
)
31 || !(node
->data
->flags
& POLICY_DATA_FLAG_MAP_MASK
))
32 BIO_puts(err
, " Not Mapped\n");
35 STACK_OF(ASN1_OBJECT
) *pset
= node
->data
->expected_policy_set
;
37 BIO_puts(err
, " Expected: ");
38 for (i
= 0; i
< sk_ASN1_OBJECT_num(pset
); i
++) {
39 oid
= sk_ASN1_OBJECT_value(pset
, i
);
42 i2a_ASN1_OBJECT(err
, oid
);
48 static void tree_print(char *str
, X509_POLICY_TREE
*tree
,
49 X509_POLICY_LEVEL
*curr
)
51 BIO
*err
= BIO_new_fp(stderr
, BIO_NOCLOSE
);
52 X509_POLICY_LEVEL
*plev
;
57 curr
= tree
->levels
+ tree
->nlevel
;
61 BIO_printf(err
, "Level print after %s\n", str
);
62 BIO_printf(err
, "Printing Up to Level %ld\n", curr
- tree
->levels
);
63 for (plev
= tree
->levels
; plev
!= curr
; plev
++) {
66 BIO_printf(err
, "Level %ld, flags = %x\n",
67 (long)(plev
- tree
->levels
), plev
->flags
);
68 for (i
= 0; i
< sk_X509_POLICY_NODE_num(plev
->nodes
); i
++) {
69 X509_POLICY_NODE
*node
= sk_X509_POLICY_NODE_value(plev
->nodes
, i
);
71 X509_POLICY_NODE_print(err
, node
, 2);
72 expected_print(err
, plev
, node
, 2);
73 BIO_printf(err
, " Flags: %x\n", node
->data
->flags
);
76 X509_POLICY_NODE_print(err
, plev
->anyPolicy
, 2);
83 * Return value: <= 0 on error, or positive bit mask:
85 * X509_PCY_TREE_VALID: valid tree
86 * X509_PCY_TREE_EMPTY: empty tree (including bare TA case)
87 * X509_PCY_TREE_EXPLICIT: explicit policy required
89 static int tree_init(X509_POLICY_TREE
**ptree
, STACK_OF(X509
) *certs
,
92 X509_POLICY_TREE
*tree
;
93 X509_POLICY_LEVEL
*level
;
94 const X509_POLICY_CACHE
*cache
;
95 X509_POLICY_DATA
*data
= NULL
;
96 int ret
= X509_PCY_TREE_VALID
;
97 int n
= sk_X509_num(certs
) - 1; /* RFC5280 paths omit the TA */
98 int explicit_policy
= (flags
& X509_V_FLAG_EXPLICIT_POLICY
) ? 0 : n
+1;
99 int any_skip
= (flags
& X509_V_FLAG_INHIBIT_ANY
) ? 0 : n
+1;
100 int map_skip
= (flags
& X509_V_FLAG_INHIBIT_MAP
) ? 0 : n
+1;
105 /* Can't do anything with just a trust anchor */
107 return X509_PCY_TREE_EMPTY
;
110 * First setup the policy cache in all n non-TA certificates, this will be
111 * used in X509_verify_cert() which will invoke the verify callback for all
112 * certificates with invalid policy extensions.
114 for (i
= n
- 1; i
>= 0; i
--) {
115 X509
*x
= sk_X509_value(certs
, i
);
117 /* Call for side-effect of computing hash and caching extensions */
118 X509_check_purpose(x
, -1, 0);
120 /* If cache is NULL, likely ENOMEM: return immediately */
121 if (policy_cache_set(x
) == NULL
)
122 return X509_PCY_TREE_INTERNAL
;
126 * At this point check for invalid policies and required explicit policy.
127 * Note that the explicit_policy counter is a count-down to zero, with the
128 * requirement kicking in if and once it does that. The counter is
129 * decremented for every non-self-issued certificate in the path, but may
130 * be further reduced by policy constraints in a non-leaf certificate.
132 * The ultimate policy set is the intersection of all the policies along
133 * the path, if we hit a certificate with an empty policy set, and explicit
134 * policy is required we're done.
137 i
>= 0 && (explicit_policy
> 0 || (ret
& X509_PCY_TREE_EMPTY
) == 0);
139 X509
*x
= sk_X509_value(certs
, i
);
140 uint32_t ex_flags
= X509_get_extension_flags(x
);
142 /* All the policies are already cached, we can return early */
143 if (ex_flags
& EXFLAG_INVALID_POLICY
)
144 return X509_PCY_TREE_INVALID
;
146 /* Access the cache which we now know exists */
147 cache
= policy_cache_set(x
);
149 if ((ret
& X509_PCY_TREE_VALID
) && cache
->data
== NULL
)
150 ret
= X509_PCY_TREE_EMPTY
;
151 if (explicit_policy
> 0) {
152 if (!(ex_flags
& EXFLAG_SI
))
154 if ((cache
->explicit_skip
>= 0)
155 && (cache
->explicit_skip
< explicit_policy
))
156 explicit_policy
= cache
->explicit_skip
;
160 if (explicit_policy
== 0)
161 ret
|= X509_PCY_TREE_EXPLICIT
;
162 if ((ret
& X509_PCY_TREE_VALID
) == 0)
165 /* If we get this far initialize the tree */
166 if ((tree
= OPENSSL_zalloc(sizeof(*tree
))) == NULL
) {
167 X509V3err(X509V3_F_TREE_INIT
, ERR_R_MALLOC_FAILURE
);
168 return X509_PCY_TREE_INTERNAL
;
172 * http://tools.ietf.org/html/rfc5280#section-6.1.2, figure 3.
174 * The top level is implicitly for the trust anchor with valid expected
175 * policies of anyPolicy. (RFC 5280 has the TA at depth 0 and the leaf at
176 * depth n, we have the leaf at depth 0 and the TA at depth n).
178 if ((tree
->levels
= OPENSSL_zalloc(sizeof(*tree
->levels
)*(n
+1))) == NULL
) {
180 X509V3err(X509V3_F_TREE_INIT
, ERR_R_MALLOC_FAILURE
);
181 return X509_PCY_TREE_INTERNAL
;
184 level
= tree
->levels
;
185 if ((data
= policy_data_new(NULL
, OBJ_nid2obj(NID_any_policy
), 0)) == NULL
)
187 if (level_add_node(level
, data
, NULL
, tree
) == NULL
) {
188 policy_data_free(data
);
193 * In this pass initialize all the tree levels and whether anyPolicy and
194 * policy mapping are inhibited at each level.
196 for (i
= n
- 1; i
>= 0; i
--) {
197 X509
*x
= sk_X509_value(certs
, i
);
198 uint32_t ex_flags
= X509_get_extension_flags(x
);
200 /* Access the cache which we now know exists */
201 cache
= policy_cache_set(x
);
206 if (!cache
->anyPolicy
)
207 level
->flags
|= X509_V_FLAG_INHIBIT_ANY
;
209 /* Determine inhibit any and inhibit map flags */
212 * Any matching allowed only if certificate is self issued and not
213 * the last in the chain.
215 if (!(ex_flags
& EXFLAG_SI
) || (i
== 0))
216 level
->flags
|= X509_V_FLAG_INHIBIT_ANY
;
218 if (!(ex_flags
& EXFLAG_SI
))
220 if ((cache
->any_skip
>= 0) && (cache
->any_skip
< any_skip
))
221 any_skip
= cache
->any_skip
;
225 level
->flags
|= X509_V_FLAG_INHIBIT_MAP
;
227 if (!(ex_flags
& EXFLAG_SI
))
229 if ((cache
->map_skip
>= 0) && (cache
->map_skip
< map_skip
))
230 map_skip
= cache
->map_skip
;
238 X509_policy_tree_free(tree
);
239 return X509_PCY_TREE_INTERNAL
;
243 * Return value: 1 on success, 0 otherwise
245 static int tree_link_matching_nodes(X509_POLICY_LEVEL
*curr
,
246 X509_POLICY_DATA
*data
)
248 X509_POLICY_LEVEL
*last
= curr
- 1;
251 /* Iterate through all in nodes linking matches */
252 for (i
= 0; i
< sk_X509_POLICY_NODE_num(last
->nodes
); i
++) {
253 X509_POLICY_NODE
*node
= sk_X509_POLICY_NODE_value(last
->nodes
, i
);
255 if (policy_node_match(last
, node
, data
->valid_policy
)) {
256 if (level_add_node(curr
, data
, node
, NULL
) == NULL
)
261 if (!matched
&& last
->anyPolicy
) {
262 if (level_add_node(curr
, data
, last
->anyPolicy
, NULL
) == NULL
)
269 * This corresponds to RFC3280 6.1.3(d)(1): link any data from
270 * CertificatePolicies onto matching parent or anyPolicy if no match.
272 * Return value: 1 on success, 0 otherwise.
274 static int tree_link_nodes(X509_POLICY_LEVEL
*curr
,
275 const X509_POLICY_CACHE
*cache
)
279 for (i
= 0; i
< sk_X509_POLICY_DATA_num(cache
->data
); i
++) {
280 X509_POLICY_DATA
*data
= sk_X509_POLICY_DATA_value(cache
->data
, i
);
282 /* Look for matching nodes in previous level */
283 if (!tree_link_matching_nodes(curr
, data
))
290 * This corresponds to RFC3280 6.1.3(d)(2): Create new data for any unmatched
291 * policies in the parent and link to anyPolicy.
293 * Return value: 1 on success, 0 otherwise.
295 static int tree_add_unmatched(X509_POLICY_LEVEL
*curr
,
296 const X509_POLICY_CACHE
*cache
,
297 const ASN1_OBJECT
*id
,
298 X509_POLICY_NODE
*node
, X509_POLICY_TREE
*tree
)
300 X509_POLICY_DATA
*data
;
303 id
= node
->data
->valid_policy
;
305 * Create a new node with qualifiers from anyPolicy and id from unmatched
308 if ((data
= policy_data_new(NULL
, id
, node_critical(node
))) == NULL
)
311 /* Curr may not have anyPolicy */
312 data
->qualifier_set
= cache
->anyPolicy
->qualifier_set
;
313 data
->flags
|= POLICY_DATA_FLAG_SHARED_QUALIFIERS
;
314 if (level_add_node(curr
, data
, node
, tree
) == NULL
) {
315 policy_data_free(data
);
322 * Return value: 1 on success, 0 otherwise.
324 static int tree_link_unmatched(X509_POLICY_LEVEL
*curr
,
325 const X509_POLICY_CACHE
*cache
,
326 X509_POLICY_NODE
*node
, X509_POLICY_TREE
*tree
)
328 const X509_POLICY_LEVEL
*last
= curr
- 1;
331 if ((last
->flags
& X509_V_FLAG_INHIBIT_MAP
)
332 || !(node
->data
->flags
& POLICY_DATA_FLAG_MAPPED
)) {
333 /* If no policy mapping: matched if one child present */
336 if (!tree_add_unmatched(curr
, cache
, NULL
, node
, tree
))
340 /* If mapping: matched if one child per expected policy set */
341 STACK_OF(ASN1_OBJECT
) *expset
= node
->data
->expected_policy_set
;
342 if (node
->nchild
== sk_ASN1_OBJECT_num(expset
))
344 /* Locate unmatched nodes */
345 for (i
= 0; i
< sk_ASN1_OBJECT_num(expset
); i
++) {
346 ASN1_OBJECT
*oid
= sk_ASN1_OBJECT_value(expset
, i
);
347 if (level_find_node(curr
, node
, oid
))
349 if (!tree_add_unmatched(curr
, cache
, oid
, node
, tree
))
358 * Return value: 1 on success, 0 otherwise
360 static int tree_link_any(X509_POLICY_LEVEL
*curr
,
361 const X509_POLICY_CACHE
*cache
,
362 X509_POLICY_TREE
*tree
)
365 X509_POLICY_NODE
*node
;
366 X509_POLICY_LEVEL
*last
= curr
- 1;
368 for (i
= 0; i
< sk_X509_POLICY_NODE_num(last
->nodes
); i
++) {
369 node
= sk_X509_POLICY_NODE_value(last
->nodes
, i
);
371 if (!tree_link_unmatched(curr
, cache
, node
, tree
))
374 /* Finally add link to anyPolicy */
375 if (last
->anyPolicy
&&
376 level_add_node(curr
, cache
->anyPolicy
, last
->anyPolicy
, NULL
) == NULL
)
382 * Prune the tree: delete any child mapped child data on the current level then
383 * proceed up the tree deleting any data with no children. If we ever have no
384 * data on a level we can halt because the tree will be empty.
386 * Return value: <= 0 error, otherwise one of:
388 * X509_PCY_TREE_VALID: valid tree
389 * X509_PCY_TREE_EMPTY: empty tree
391 static int tree_prune(X509_POLICY_TREE
*tree
, X509_POLICY_LEVEL
*curr
)
393 STACK_OF(X509_POLICY_NODE
) *nodes
;
394 X509_POLICY_NODE
*node
;
397 if (curr
->flags
& X509_V_FLAG_INHIBIT_MAP
) {
398 for (i
= sk_X509_POLICY_NODE_num(nodes
) - 1; i
>= 0; i
--) {
399 node
= sk_X509_POLICY_NODE_value(nodes
, i
);
400 /* Delete any mapped data: see RFC3280 XXXX */
401 if (node
->data
->flags
& POLICY_DATA_FLAG_MAP_MASK
) {
402 node
->parent
->nchild
--;
404 (void)sk_X509_POLICY_NODE_delete(nodes
, i
);
412 for (i
= sk_X509_POLICY_NODE_num(nodes
) - 1; i
>= 0; i
--) {
413 node
= sk_X509_POLICY_NODE_value(nodes
, i
);
414 if (node
->nchild
== 0) {
415 node
->parent
->nchild
--;
417 (void)sk_X509_POLICY_NODE_delete(nodes
, i
);
420 if (curr
->anyPolicy
&& !curr
->anyPolicy
->nchild
) {
421 if (curr
->anyPolicy
->parent
)
422 curr
->anyPolicy
->parent
->nchild
--;
423 OPENSSL_free(curr
->anyPolicy
);
424 curr
->anyPolicy
= NULL
;
426 if (curr
== tree
->levels
) {
427 /* If we zapped anyPolicy at top then tree is empty */
428 if (!curr
->anyPolicy
)
429 return X509_PCY_TREE_EMPTY
;
433 return X509_PCY_TREE_VALID
;
437 * Return value: 1 on success, 0 otherwise.
439 static int tree_add_auth_node(STACK_OF(X509_POLICY_NODE
) **pnodes
,
440 X509_POLICY_NODE
*pcy
)
442 if (*pnodes
== NULL
&&
443 (*pnodes
= policy_node_cmp_new()) == NULL
)
445 if (sk_X509_POLICY_NODE_find(*pnodes
, pcy
) != -1)
447 return sk_X509_POLICY_NODE_push(*pnodes
, pcy
) != 0;
450 #define TREE_CALC_FAILURE 0
451 #define TREE_CALC_OK_NOFREE 1
452 #define TREE_CALC_OK_DOFREE 2
455 * Calculate the authority set based on policy tree. The 'pnodes' parameter is
456 * used as a store for the set of policy nodes used to calculate the user set.
457 * If the authority set is not anyPolicy then pnodes will just point to the
458 * authority set. If however the authority set is anyPolicy then the set of
459 * valid policies (other than anyPolicy) is store in pnodes.
462 * TREE_CALC_FAILURE on failure,
463 * TREE_CALC_OK_NOFREE on success and pnodes need not be freed,
464 * TREE_CALC_OK_DOFREE on success and pnodes needs to be freed
466 static int tree_calculate_authority_set(X509_POLICY_TREE
*tree
,
467 STACK_OF(X509_POLICY_NODE
) **pnodes
)
469 X509_POLICY_LEVEL
*curr
;
470 X509_POLICY_NODE
*node
, *anyptr
;
471 STACK_OF(X509_POLICY_NODE
) **addnodes
;
473 curr
= tree
->levels
+ tree
->nlevel
- 1;
475 /* If last level contains anyPolicy set is anyPolicy */
476 if (curr
->anyPolicy
) {
477 if (!tree_add_auth_node(&tree
->auth_policies
, curr
->anyPolicy
))
478 return TREE_CALC_FAILURE
;
481 /* Add policies to authority set */
482 addnodes
= &tree
->auth_policies
;
485 for (i
= 1; i
< tree
->nlevel
; i
++) {
487 * If no anyPolicy node on this this level it can't appear on lower
488 * levels so end search.
490 if ((anyptr
= curr
->anyPolicy
) == NULL
)
493 for (j
= 0; j
< sk_X509_POLICY_NODE_num(curr
->nodes
); j
++) {
494 node
= sk_X509_POLICY_NODE_value(curr
->nodes
, j
);
495 if ((node
->parent
== anyptr
)
496 && !tree_add_auth_node(addnodes
, node
)) {
497 if (addnodes
== pnodes
) {
498 sk_X509_POLICY_NODE_free(*pnodes
);
501 return TREE_CALC_FAILURE
;
505 if (addnodes
== pnodes
)
506 return TREE_CALC_OK_DOFREE
;
508 *pnodes
= tree
->auth_policies
;
509 return TREE_CALC_OK_NOFREE
;
513 * Return value: 1 on success, 0 otherwise.
515 static int tree_calculate_user_set(X509_POLICY_TREE
*tree
,
516 STACK_OF(ASN1_OBJECT
) *policy_oids
,
517 STACK_OF(X509_POLICY_NODE
) *auth_nodes
)
520 X509_POLICY_NODE
*node
;
522 X509_POLICY_NODE
*anyPolicy
;
523 X509_POLICY_DATA
*extra
;
526 * Check if anyPolicy present in authority constrained policy set: this
527 * will happen if it is a leaf node.
529 if (sk_ASN1_OBJECT_num(policy_oids
) <= 0)
532 anyPolicy
= tree
->levels
[tree
->nlevel
- 1].anyPolicy
;
534 for (i
= 0; i
< sk_ASN1_OBJECT_num(policy_oids
); i
++) {
535 oid
= sk_ASN1_OBJECT_value(policy_oids
, i
);
536 if (OBJ_obj2nid(oid
) == NID_any_policy
) {
537 tree
->flags
|= POLICY_FLAG_ANY_POLICY
;
542 for (i
= 0; i
< sk_ASN1_OBJECT_num(policy_oids
); i
++) {
543 oid
= sk_ASN1_OBJECT_value(policy_oids
, i
);
544 node
= tree_find_sk(auth_nodes
, oid
);
549 * Create a new node with policy ID from user set and qualifiers
552 extra
= policy_data_new(NULL
, oid
, node_critical(anyPolicy
));
555 extra
->qualifier_set
= anyPolicy
->data
->qualifier_set
;
556 extra
->flags
= POLICY_DATA_FLAG_SHARED_QUALIFIERS
557 | POLICY_DATA_FLAG_EXTRA_NODE
;
558 node
= level_add_node(NULL
, extra
, anyPolicy
->parent
, tree
);
560 if (!tree
->user_policies
) {
561 tree
->user_policies
= sk_X509_POLICY_NODE_new_null();
562 if (!tree
->user_policies
)
565 if (!sk_X509_POLICY_NODE_push(tree
->user_policies
, node
))
572 * Return value: <= 0 error, otherwise one of:
573 * X509_PCY_TREE_VALID: valid tree
574 * X509_PCY_TREE_EMPTY: empty tree
575 * (see tree_prune()).
577 static int tree_evaluate(X509_POLICY_TREE
*tree
)
580 X509_POLICY_LEVEL
*curr
= tree
->levels
+ 1;
581 const X509_POLICY_CACHE
*cache
;
583 for (i
= 1; i
< tree
->nlevel
; i
++, curr
++) {
584 cache
= policy_cache_set(curr
->cert
);
585 if (!tree_link_nodes(curr
, cache
))
586 return X509_PCY_TREE_INTERNAL
;
588 if (!(curr
->flags
& X509_V_FLAG_INHIBIT_ANY
)
589 && !tree_link_any(curr
, cache
, tree
))
590 return X509_PCY_TREE_INTERNAL
;
591 #ifdef OPENSSL_POLICY_DEBUG
592 tree_print("before tree_prune()", tree
, curr
);
594 ret
= tree_prune(tree
, curr
);
595 if (ret
!= X509_PCY_TREE_VALID
)
598 return X509_PCY_TREE_VALID
;
601 static void exnode_free(X509_POLICY_NODE
*node
)
603 if (node
->data
&& (node
->data
->flags
& POLICY_DATA_FLAG_EXTRA_NODE
))
607 void X509_policy_tree_free(X509_POLICY_TREE
*tree
)
609 X509_POLICY_LEVEL
*curr
;
615 sk_X509_POLICY_NODE_free(tree
->auth_policies
);
616 sk_X509_POLICY_NODE_pop_free(tree
->user_policies
, exnode_free
);
618 for (i
= 0, curr
= tree
->levels
; i
< tree
->nlevel
; i
++, curr
++) {
619 X509_free(curr
->cert
);
620 sk_X509_POLICY_NODE_pop_free(curr
->nodes
, policy_node_free
);
621 policy_node_free(curr
->anyPolicy
);
624 sk_X509_POLICY_DATA_pop_free(tree
->extra_data
, policy_data_free
);
625 OPENSSL_free(tree
->levels
);
631 * Application policy checking function.
633 * X509_PCY_TREE_FAILURE: Failure to satisfy explicit policy
634 * X509_PCY_TREE_INVALID: Inconsistent or invalid extensions
635 * X509_PCY_TREE_INTERNAL: Internal error, most likely malloc
636 * X509_PCY_TREE_VALID: Success (null tree if empty or bare TA)
638 int X509_policy_check(X509_POLICY_TREE
**ptree
, int *pexplicit_policy
,
639 STACK_OF(X509
) *certs
,
640 STACK_OF(ASN1_OBJECT
) *policy_oids
, unsigned int flags
)
645 X509_POLICY_TREE
*tree
= NULL
;
646 STACK_OF(X509_POLICY_NODE
) *nodes
, *auth_nodes
= NULL
;
649 *pexplicit_policy
= 0;
650 init_ret
= tree_init(&tree
, certs
, flags
);
655 if ((init_ret
& X509_PCY_TREE_EXPLICIT
) == 0) {
656 if (init_ret
& X509_PCY_TREE_EMPTY
) {
657 X509_policy_tree_free(tree
);
658 return X509_PCY_TREE_VALID
;
661 *pexplicit_policy
= 1;
662 /* Tree empty and requireExplicit True: Error */
663 if (init_ret
& X509_PCY_TREE_EMPTY
)
664 return X509_PCY_TREE_FAILURE
;
667 ret
= tree_evaluate(tree
);
668 #ifdef OPENSSL_POLICY_DEBUG
669 tree_print("tree_evaluate()", tree
, NULL
);
674 if (ret
== X509_PCY_TREE_EMPTY
) {
675 X509_policy_tree_free(tree
);
676 if (init_ret
& X509_PCY_TREE_EXPLICIT
)
677 return X509_PCY_TREE_FAILURE
;
678 return X509_PCY_TREE_VALID
;
681 /* Tree is not empty: continue */
683 if ((calc_ret
= tree_calculate_authority_set(tree
, &auth_nodes
)) == 0)
685 ret
= tree_calculate_user_set(tree
, policy_oids
, auth_nodes
);
686 if (calc_ret
== TREE_CALC_OK_DOFREE
)
687 sk_X509_POLICY_NODE_free(auth_nodes
);
693 if (init_ret
& X509_PCY_TREE_EXPLICIT
) {
694 nodes
= X509_policy_tree_get0_user_policies(tree
);
695 if (sk_X509_POLICY_NODE_num(nodes
) <= 0)
696 return X509_PCY_TREE_FAILURE
;
698 return X509_PCY_TREE_VALID
;
701 X509_policy_tree_free(tree
);
702 return X509_PCY_TREE_INTERNAL
;