CTR, HASH and HMAC DRBGs in provider
[openssl.git] / crypto / x509 / pcy_tree.c
1 /*
2  * Copyright 2004-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (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
8  */
9
10 #include "internal/cryptlib.h"
11 #include <openssl/trace.h>
12 #include <openssl/x509.h>
13 #include <openssl/x509v3.h>
14
15 #include "pcy_local.h"
16
17 DEFINE_STACK_OF(ASN1_OBJECT)
18 DEFINE_STACK_OF(X509)
19 DEFINE_STACK_OF(X509_POLICY_NODE)
20
21 static void expected_print(BIO *channel,
22                            X509_POLICY_LEVEL *lev, X509_POLICY_NODE *node,
23                            int indent)
24 {
25     if ((lev->flags & X509_V_FLAG_INHIBIT_MAP)
26         || !(node->data->flags & POLICY_DATA_FLAG_MAP_MASK))
27         BIO_puts(channel, "  Not Mapped\n");
28     else {
29         int i;
30
31         STACK_OF(ASN1_OBJECT) *pset = node->data->expected_policy_set;
32         ASN1_OBJECT *oid;
33         BIO_puts(channel, "  Expected: ");
34         for (i = 0; i < sk_ASN1_OBJECT_num(pset); i++) {
35             oid = sk_ASN1_OBJECT_value(pset, i);
36             if (i)
37                 BIO_puts(channel, ", ");
38             i2a_ASN1_OBJECT(channel, oid);
39         }
40         BIO_puts(channel, "\n");
41     }
42 }
43
44 static void tree_print(BIO *channel,
45                        char *str, X509_POLICY_TREE *tree,
46                        X509_POLICY_LEVEL *curr)
47 {
48     X509_POLICY_LEVEL *plev;
49
50     if (!curr)
51         curr = tree->levels + tree->nlevel;
52     else
53         curr++;
54
55     BIO_printf(channel, "Level print after %s\n", str);
56     BIO_printf(channel, "Printing Up to Level %ld\n",
57                (long)(curr - tree->levels));
58     for (plev = tree->levels; plev != curr; plev++) {
59         int i;
60
61         BIO_printf(channel, "Level %ld, flags = %x\n",
62                    (long)(plev - tree->levels), plev->flags);
63         for (i = 0; i < sk_X509_POLICY_NODE_num(plev->nodes); i++) {
64             X509_POLICY_NODE *node =
65                 sk_X509_POLICY_NODE_value(plev->nodes, i);
66
67             X509_POLICY_NODE_print(channel, node, 2);
68             expected_print(channel, plev, node, 2);
69             BIO_printf(channel, "  Flags: %x\n", node->data->flags);
70         }
71         if (plev->anyPolicy)
72             X509_POLICY_NODE_print(channel, plev->anyPolicy, 2);
73     }
74 }
75
76 #define TREE_PRINT(str, tree, curr) \
77     OSSL_TRACE_BEGIN(X509V3_POLICY) { \
78         tree_print(trc_out, "before tree_prune()", tree, curr); \
79     } OSSL_TRACE_END(X509V3_POLICY)
80
81 /*-
82  * Return value: <= 0 on error, or positive bit mask:
83  *
84  * X509_PCY_TREE_VALID: valid tree
85  * X509_PCY_TREE_EMPTY: empty tree (including bare TA case)
86  * X509_PCY_TREE_EXPLICIT: explicit policy required
87  */
88 static int tree_init(X509_POLICY_TREE **ptree, STACK_OF(X509) *certs,
89                      unsigned int flags)
90 {
91     X509_POLICY_TREE *tree;
92     X509_POLICY_LEVEL *level;
93     const X509_POLICY_CACHE *cache;
94     X509_POLICY_DATA *data = NULL;
95     int ret = X509_PCY_TREE_VALID;
96     int n = sk_X509_num(certs) - 1; /* RFC5280 paths omit the TA */
97     int explicit_policy = (flags & X509_V_FLAG_EXPLICIT_POLICY) ? 0 : n+1;
98     int any_skip = (flags & X509_V_FLAG_INHIBIT_ANY) ? 0 : n+1;
99     int map_skip = (flags & X509_V_FLAG_INHIBIT_MAP) ? 0 : n+1;
100     int i;
101
102     *ptree = NULL;
103
104     /* Can't do anything with just a trust anchor */
105     if (n == 0)
106         return X509_PCY_TREE_EMPTY;
107
108     /*
109      * First setup the policy cache in all n non-TA certificates, this will be
110      * used in X509_verify_cert() which will invoke the verify callback for all
111      * certificates with invalid policy extensions.
112      */
113     for (i = n - 1; i >= 0; i--) {
114         X509 *x = sk_X509_value(certs, i);
115
116         /* Call for side-effect of computing hash and caching extensions */
117         X509_check_purpose(x, -1, 0);
118
119         /* If cache is NULL, likely ENOMEM: return immediately */
120         if (policy_cache_set(x) == NULL)
121             return X509_PCY_TREE_INTERNAL;
122     }
123
124     /*
125      * At this point check for invalid policies and required explicit policy.
126      * Note that the explicit_policy counter is a count-down to zero, with the
127      * requirement kicking in if and once it does that.  The counter is
128      * decremented for every non-self-issued certificate in the path, but may
129      * be further reduced by policy constraints in a non-leaf certificate.
130      *
131      * The ultimate policy set is the intersection of all the policies along
132      * the path, if we hit a certificate with an empty policy set, and explicit
133      * policy is required we're done.
134      */
135     for (i = n - 1;
136          i >= 0 && (explicit_policy > 0 || (ret & X509_PCY_TREE_EMPTY) == 0);
137          i--) {
138         X509 *x = sk_X509_value(certs, i);
139         uint32_t ex_flags = X509_get_extension_flags(x);
140
141         /* All the policies are already cached, we can return early */
142         if (ex_flags & EXFLAG_INVALID_POLICY)
143             return X509_PCY_TREE_INVALID;
144
145         /* Access the cache which we now know exists */
146         cache = policy_cache_set(x);
147
148         if ((ret & X509_PCY_TREE_VALID) && cache->data == NULL)
149             ret = X509_PCY_TREE_EMPTY;
150         if (explicit_policy > 0) {
151             if (!(ex_flags & EXFLAG_SI))
152                 explicit_policy--;
153             if ((cache->explicit_skip >= 0)
154                 && (cache->explicit_skip < explicit_policy))
155                 explicit_policy = cache->explicit_skip;
156         }
157     }
158
159     if (explicit_policy == 0)
160         ret |= X509_PCY_TREE_EXPLICIT;
161     if ((ret & X509_PCY_TREE_VALID) == 0)
162         return ret;
163
164     /* If we get this far initialize the tree */
165     if ((tree = OPENSSL_zalloc(sizeof(*tree))) == NULL) {
166         X509V3err(X509V3_F_TREE_INIT, ERR_R_MALLOC_FAILURE);
167         return X509_PCY_TREE_INTERNAL;
168     }
169
170     /*
171      * http://tools.ietf.org/html/rfc5280#section-6.1.2, figure 3.
172      *
173      * The top level is implicitly for the trust anchor with valid expected
174      * policies of anyPolicy.  (RFC 5280 has the TA at depth 0 and the leaf at
175      * depth n, we have the leaf at depth 0 and the TA at depth n).
176      */
177     if ((tree->levels = OPENSSL_zalloc(sizeof(*tree->levels)*(n+1))) == NULL) {
178         OPENSSL_free(tree);
179         X509V3err(X509V3_F_TREE_INIT, ERR_R_MALLOC_FAILURE);
180         return X509_PCY_TREE_INTERNAL;
181     }
182     tree->nlevel = n+1;
183     level = tree->levels;
184     if ((data = policy_data_new(NULL, OBJ_nid2obj(NID_any_policy), 0)) == NULL)
185         goto bad_tree;
186     if (level_add_node(level, data, NULL, tree) == NULL) {
187         policy_data_free(data);
188         goto bad_tree;
189     }
190
191     /*
192      * In this pass initialize all the tree levels and whether anyPolicy and
193      * policy mapping are inhibited at each level.
194      */
195     for (i = n - 1; i >= 0; i--) {
196         X509 *x = sk_X509_value(certs, i);
197         uint32_t ex_flags = X509_get_extension_flags(x);
198
199         /* Access the cache which we now know exists */
200         cache = policy_cache_set(x);
201
202         X509_up_ref(x);
203         (++level)->cert = x;
204
205         if (!cache->anyPolicy)
206             level->flags |= X509_V_FLAG_INHIBIT_ANY;
207
208         /* Determine inhibit any and inhibit map flags */
209         if (any_skip == 0) {
210             /*
211              * Any matching allowed only if certificate is self issued and not
212              * the last in the chain.
213              */
214             if (!(ex_flags & EXFLAG_SI) || (i == 0))
215                 level->flags |= X509_V_FLAG_INHIBIT_ANY;
216         } else {
217             if (!(ex_flags & EXFLAG_SI))
218                 any_skip--;
219             if ((cache->any_skip >= 0) && (cache->any_skip < any_skip))
220                 any_skip = cache->any_skip;
221         }
222
223         if (map_skip == 0)
224             level->flags |= X509_V_FLAG_INHIBIT_MAP;
225         else {
226             if (!(ex_flags & EXFLAG_SI))
227                 map_skip--;
228             if ((cache->map_skip >= 0) && (cache->map_skip < map_skip))
229                 map_skip = cache->map_skip;
230         }
231     }
232
233     *ptree = tree;
234     return ret;
235
236  bad_tree:
237     X509_policy_tree_free(tree);
238     return X509_PCY_TREE_INTERNAL;
239 }
240
241 /*
242  * Return value: 1 on success, 0 otherwise
243  */
244 static int tree_link_matching_nodes(X509_POLICY_LEVEL *curr,
245                                     X509_POLICY_DATA *data)
246 {
247     X509_POLICY_LEVEL *last = curr - 1;
248     int i, matched = 0;
249
250     /* Iterate through all in nodes linking matches */
251     for (i = 0; i < sk_X509_POLICY_NODE_num(last->nodes); i++) {
252         X509_POLICY_NODE *node = sk_X509_POLICY_NODE_value(last->nodes, i);
253
254         if (policy_node_match(last, node, data->valid_policy)) {
255             if (level_add_node(curr, data, node, NULL) == NULL)
256                 return 0;
257             matched = 1;
258         }
259     }
260     if (!matched && last->anyPolicy) {
261         if (level_add_node(curr, data, last->anyPolicy, NULL) == NULL)
262             return 0;
263     }
264     return 1;
265 }
266
267 /*
268  * This corresponds to RFC3280 6.1.3(d)(1): link any data from
269  * CertificatePolicies onto matching parent or anyPolicy if no match.
270  *
271  * Return value: 1 on success, 0 otherwise.
272  */
273 static int tree_link_nodes(X509_POLICY_LEVEL *curr,
274                            const X509_POLICY_CACHE *cache)
275 {
276     int i;
277
278     for (i = 0; i < sk_X509_POLICY_DATA_num(cache->data); i++) {
279         X509_POLICY_DATA *data = sk_X509_POLICY_DATA_value(cache->data, i);
280
281         /* Look for matching nodes in previous level */
282         if (!tree_link_matching_nodes(curr, data))
283             return 0;
284     }
285     return 1;
286 }
287
288 /*
289  * This corresponds to RFC3280 6.1.3(d)(2): Create new data for any unmatched
290  * policies in the parent and link to anyPolicy.
291  *
292  * Return value: 1 on success, 0 otherwise.
293  */
294 static int tree_add_unmatched(X509_POLICY_LEVEL *curr,
295                               const X509_POLICY_CACHE *cache,
296                               const ASN1_OBJECT *id,
297                               X509_POLICY_NODE *node, X509_POLICY_TREE *tree)
298 {
299     X509_POLICY_DATA *data;
300
301     if (id == NULL)
302         id = node->data->valid_policy;
303     /*
304      * Create a new node with qualifiers from anyPolicy and id from unmatched
305      * node.
306      */
307     if ((data = policy_data_new(NULL, id, node_critical(node))) == NULL)
308         return 0;
309
310     /* Curr may not have anyPolicy */
311     data->qualifier_set = cache->anyPolicy->qualifier_set;
312     data->flags |= POLICY_DATA_FLAG_SHARED_QUALIFIERS;
313     if (level_add_node(curr, data, node, tree) == NULL) {
314         policy_data_free(data);
315         return 0;
316     }
317     return 1;
318 }
319
320 /*
321  * Return value: 1 on success, 0 otherwise.
322  */
323 static int tree_link_unmatched(X509_POLICY_LEVEL *curr,
324                                const X509_POLICY_CACHE *cache,
325                                X509_POLICY_NODE *node, X509_POLICY_TREE *tree)
326 {
327     const X509_POLICY_LEVEL *last = curr - 1;
328     int i;
329
330     if ((last->flags & X509_V_FLAG_INHIBIT_MAP)
331         || !(node->data->flags & POLICY_DATA_FLAG_MAPPED)) {
332         /* If no policy mapping: matched if one child present */
333         if (node->nchild)
334             return 1;
335         if (!tree_add_unmatched(curr, cache, NULL, node, tree))
336             return 0;
337         /* Add it */
338     } else {
339         /* If mapping: matched if one child per expected policy set */
340         STACK_OF(ASN1_OBJECT) *expset = node->data->expected_policy_set;
341         if (node->nchild == sk_ASN1_OBJECT_num(expset))
342             return 1;
343         /* Locate unmatched nodes */
344         for (i = 0; i < sk_ASN1_OBJECT_num(expset); i++) {
345             ASN1_OBJECT *oid = sk_ASN1_OBJECT_value(expset, i);
346             if (level_find_node(curr, node, oid))
347                 continue;
348             if (!tree_add_unmatched(curr, cache, oid, node, tree))
349                 return 0;
350         }
351
352     }
353     return 1;
354 }
355
356 /*
357  * Return value: 1 on success, 0 otherwise
358  */
359 static int tree_link_any(X509_POLICY_LEVEL *curr,
360                          const X509_POLICY_CACHE *cache,
361                          X509_POLICY_TREE *tree)
362 {
363     int i;
364     X509_POLICY_NODE *node;
365     X509_POLICY_LEVEL *last = curr - 1;
366
367     for (i = 0; i < sk_X509_POLICY_NODE_num(last->nodes); i++) {
368         node = sk_X509_POLICY_NODE_value(last->nodes, i);
369
370         if (!tree_link_unmatched(curr, cache, node, tree))
371             return 0;
372     }
373     /* Finally add link to anyPolicy */
374     if (last->anyPolicy &&
375         level_add_node(curr, cache->anyPolicy, last->anyPolicy, NULL) == NULL)
376         return 0;
377     return 1;
378 }
379
380 /*-
381  * Prune the tree: delete any child mapped child data on the current level then
382  * proceed up the tree deleting any data with no children. If we ever have no
383  * data on a level we can halt because the tree will be empty.
384  *
385  * Return value: <= 0 error, otherwise one of:
386  *
387  * X509_PCY_TREE_VALID: valid tree
388  * X509_PCY_TREE_EMPTY: empty tree
389  */
390 static int tree_prune(X509_POLICY_TREE *tree, X509_POLICY_LEVEL *curr)
391 {
392     STACK_OF(X509_POLICY_NODE) *nodes;
393     X509_POLICY_NODE *node;
394     int i;
395     nodes = curr->nodes;
396     if (curr->flags & X509_V_FLAG_INHIBIT_MAP) {
397         for (i = sk_X509_POLICY_NODE_num(nodes) - 1; i >= 0; i--) {
398             node = sk_X509_POLICY_NODE_value(nodes, i);
399             /* Delete any mapped data: see RFC3280 XXXX */
400             if (node->data->flags & POLICY_DATA_FLAG_MAP_MASK) {
401                 node->parent->nchild--;
402                 OPENSSL_free(node);
403                 (void)sk_X509_POLICY_NODE_delete(nodes, i);
404             }
405         }
406     }
407
408     for (;;) {
409         --curr;
410         nodes = curr->nodes;
411         for (i = sk_X509_POLICY_NODE_num(nodes) - 1; i >= 0; i--) {
412             node = sk_X509_POLICY_NODE_value(nodes, i);
413             if (node->nchild == 0) {
414                 node->parent->nchild--;
415                 OPENSSL_free(node);
416                 (void)sk_X509_POLICY_NODE_delete(nodes, i);
417             }
418         }
419         if (curr->anyPolicy && !curr->anyPolicy->nchild) {
420             if (curr->anyPolicy->parent)
421                 curr->anyPolicy->parent->nchild--;
422             OPENSSL_free(curr->anyPolicy);
423             curr->anyPolicy = NULL;
424         }
425         if (curr == tree->levels) {
426             /* If we zapped anyPolicy at top then tree is empty */
427             if (!curr->anyPolicy)
428                 return X509_PCY_TREE_EMPTY;
429             break;
430         }
431     }
432     return X509_PCY_TREE_VALID;
433 }
434
435 /*
436  * Return value: 1 on success, 0 otherwise.
437  */
438 static int tree_add_auth_node(STACK_OF(X509_POLICY_NODE) **pnodes,
439                               X509_POLICY_NODE *pcy)
440 {
441     if (*pnodes == NULL &&
442         (*pnodes = policy_node_cmp_new()) == NULL)
443         return 0;
444     if (sk_X509_POLICY_NODE_find(*pnodes, pcy) >= 0)
445         return 1;
446     return sk_X509_POLICY_NODE_push(*pnodes, pcy) != 0;
447 }
448
449 #define TREE_CALC_FAILURE 0
450 #define TREE_CALC_OK_NOFREE 1
451 #define TREE_CALC_OK_DOFREE 2
452
453 /*-
454  * Calculate the authority set based on policy tree. The 'pnodes' parameter is
455  * used as a store for the set of policy nodes used to calculate the user set.
456  * If the authority set is not anyPolicy then pnodes will just point to the
457  * authority set. If however the authority set is anyPolicy then the set of
458  * valid policies (other than anyPolicy) is store in pnodes.
459  *
460  * Return value:
461  *  TREE_CALC_FAILURE on failure,
462  *  TREE_CALC_OK_NOFREE on success and pnodes need not be freed,
463  *  TREE_CALC_OK_DOFREE on success and pnodes needs to be freed
464  */
465 static int tree_calculate_authority_set(X509_POLICY_TREE *tree,
466                                         STACK_OF(X509_POLICY_NODE) **pnodes)
467 {
468     X509_POLICY_LEVEL *curr;
469     X509_POLICY_NODE *node, *anyptr;
470     STACK_OF(X509_POLICY_NODE) **addnodes;
471     int i, j;
472     curr = tree->levels + tree->nlevel - 1;
473
474     /* If last level contains anyPolicy set is anyPolicy */
475     if (curr->anyPolicy) {
476         if (!tree_add_auth_node(&tree->auth_policies, curr->anyPolicy))
477             return TREE_CALC_FAILURE;
478         addnodes = pnodes;
479     } else
480         /* Add policies to authority set */
481         addnodes = &tree->auth_policies;
482
483     curr = tree->levels;
484     for (i = 1; i < tree->nlevel; i++) {
485         /*
486          * If no anyPolicy node on this this level it can't appear on lower
487          * levels so end search.
488          */
489         if ((anyptr = curr->anyPolicy) == NULL)
490             break;
491         curr++;
492         for (j = 0; j < sk_X509_POLICY_NODE_num(curr->nodes); j++) {
493             node = sk_X509_POLICY_NODE_value(curr->nodes, j);
494             if ((node->parent == anyptr)
495                 && !tree_add_auth_node(addnodes, node)) {
496                 if (addnodes == pnodes) {
497                     sk_X509_POLICY_NODE_free(*pnodes);
498                     *pnodes = NULL;
499                 }
500                 return TREE_CALC_FAILURE;
501             }
502         }
503     }
504     if (addnodes == pnodes)
505         return TREE_CALC_OK_DOFREE;
506
507     *pnodes = tree->auth_policies;
508     return TREE_CALC_OK_NOFREE;
509 }
510
511 /*
512  * Return value: 1 on success, 0 otherwise.
513  */
514 static int tree_calculate_user_set(X509_POLICY_TREE *tree,
515                                    STACK_OF(ASN1_OBJECT) *policy_oids,
516                                    STACK_OF(X509_POLICY_NODE) *auth_nodes)
517 {
518     int i;
519     X509_POLICY_NODE *node;
520     ASN1_OBJECT *oid;
521     X509_POLICY_NODE *anyPolicy;
522     X509_POLICY_DATA *extra;
523
524     /*
525      * Check if anyPolicy present in authority constrained policy set: this
526      * will happen if it is a leaf node.
527      */
528     if (sk_ASN1_OBJECT_num(policy_oids) <= 0)
529         return 1;
530
531     anyPolicy = tree->levels[tree->nlevel - 1].anyPolicy;
532
533     for (i = 0; i < sk_ASN1_OBJECT_num(policy_oids); i++) {
534         oid = sk_ASN1_OBJECT_value(policy_oids, i);
535         if (OBJ_obj2nid(oid) == NID_any_policy) {
536             tree->flags |= POLICY_FLAG_ANY_POLICY;
537             return 1;
538         }
539     }
540
541     for (i = 0; i < sk_ASN1_OBJECT_num(policy_oids); i++) {
542         oid = sk_ASN1_OBJECT_value(policy_oids, i);
543         node = tree_find_sk(auth_nodes, oid);
544         if (!node) {
545             if (!anyPolicy)
546                 continue;
547             /*
548              * Create a new node with policy ID from user set and qualifiers
549              * from anyPolicy.
550              */
551             extra = policy_data_new(NULL, oid, node_critical(anyPolicy));
552             if (extra == NULL)
553                 return 0;
554             extra->qualifier_set = anyPolicy->data->qualifier_set;
555             extra->flags = POLICY_DATA_FLAG_SHARED_QUALIFIERS
556                 | POLICY_DATA_FLAG_EXTRA_NODE;
557             node = level_add_node(NULL, extra, anyPolicy->parent, tree);
558         }
559         if (!tree->user_policies) {
560             tree->user_policies = sk_X509_POLICY_NODE_new_null();
561             if (!tree->user_policies)
562                 return 1;
563         }
564         if (!sk_X509_POLICY_NODE_push(tree->user_policies, node))
565             return 0;
566     }
567     return 1;
568 }
569
570 /*-
571  * Return value: <= 0 error, otherwise one of:
572  *  X509_PCY_TREE_VALID: valid tree
573  *  X509_PCY_TREE_EMPTY: empty tree
574  * (see tree_prune()).
575  */
576 static int tree_evaluate(X509_POLICY_TREE *tree)
577 {
578     int ret, i;
579     X509_POLICY_LEVEL *curr = tree->levels + 1;
580     const X509_POLICY_CACHE *cache;
581
582     for (i = 1; i < tree->nlevel; i++, curr++) {
583         cache = policy_cache_set(curr->cert);
584         if (!tree_link_nodes(curr, cache))
585             return X509_PCY_TREE_INTERNAL;
586
587         if (!(curr->flags & X509_V_FLAG_INHIBIT_ANY)
588             && !tree_link_any(curr, cache, tree))
589             return X509_PCY_TREE_INTERNAL;
590         TREE_PRINT("before tree_prune()", tree, curr);
591         ret = tree_prune(tree, curr);
592         if (ret != X509_PCY_TREE_VALID)
593             return ret;
594     }
595     return X509_PCY_TREE_VALID;
596 }
597
598 static void exnode_free(X509_POLICY_NODE *node)
599 {
600     if (node->data && (node->data->flags & POLICY_DATA_FLAG_EXTRA_NODE))
601         OPENSSL_free(node);
602 }
603
604 void X509_policy_tree_free(X509_POLICY_TREE *tree)
605 {
606     X509_POLICY_LEVEL *curr;
607     int i;
608
609     if (!tree)
610         return;
611
612     sk_X509_POLICY_NODE_free(tree->auth_policies);
613     sk_X509_POLICY_NODE_pop_free(tree->user_policies, exnode_free);
614
615     for (i = 0, curr = tree->levels; i < tree->nlevel; i++, curr++) {
616         X509_free(curr->cert);
617         sk_X509_POLICY_NODE_pop_free(curr->nodes, policy_node_free);
618         policy_node_free(curr->anyPolicy);
619     }
620
621     sk_X509_POLICY_DATA_pop_free(tree->extra_data, policy_data_free);
622     OPENSSL_free(tree->levels);
623     OPENSSL_free(tree);
624
625 }
626
627 /*-
628  * Application policy checking function.
629  * Return codes:
630  *  X509_PCY_TREE_FAILURE:  Failure to satisfy explicit policy
631  *  X509_PCY_TREE_INVALID:  Inconsistent or invalid extensions
632  *  X509_PCY_TREE_INTERNAL: Internal error, most likely malloc
633  *  X509_PCY_TREE_VALID:    Success (null tree if empty or bare TA)
634  */
635 int X509_policy_check(X509_POLICY_TREE **ptree, int *pexplicit_policy,
636                       STACK_OF(X509) *certs,
637                       STACK_OF(ASN1_OBJECT) *policy_oids, unsigned int flags)
638 {
639     int init_ret;
640     int ret;
641     int calc_ret;
642     X509_POLICY_TREE *tree = NULL;
643     STACK_OF(X509_POLICY_NODE) *nodes, *auth_nodes = NULL;
644
645     *ptree = NULL;
646     *pexplicit_policy = 0;
647     init_ret = tree_init(&tree, certs, flags);
648
649     if (init_ret <= 0)
650         return init_ret;
651
652     if ((init_ret & X509_PCY_TREE_EXPLICIT) == 0) {
653         if (init_ret & X509_PCY_TREE_EMPTY) {
654             X509_policy_tree_free(tree);
655             return X509_PCY_TREE_VALID;
656         }
657     } else {
658         *pexplicit_policy = 1;
659         /* Tree empty and requireExplicit True: Error */
660         if (init_ret & X509_PCY_TREE_EMPTY)
661             return X509_PCY_TREE_FAILURE;
662     }
663
664     ret = tree_evaluate(tree);
665     TREE_PRINT("tree_evaluate()", tree, NULL);
666     if (ret <= 0)
667         goto error;
668
669     if (ret == X509_PCY_TREE_EMPTY) {
670         X509_policy_tree_free(tree);
671         if (init_ret & X509_PCY_TREE_EXPLICIT)
672             return X509_PCY_TREE_FAILURE;
673         return X509_PCY_TREE_VALID;
674     }
675
676     /* Tree is not empty: continue */
677
678     if ((calc_ret = tree_calculate_authority_set(tree, &auth_nodes)) == 0)
679         goto error;
680     ret = tree_calculate_user_set(tree, policy_oids, auth_nodes);
681     if (calc_ret == TREE_CALC_OK_DOFREE)
682         sk_X509_POLICY_NODE_free(auth_nodes);
683     if (!ret)
684         goto error;
685
686     *ptree = tree;
687
688     if (init_ret & X509_PCY_TREE_EXPLICIT) {
689         nodes = X509_policy_tree_get0_user_policies(tree);
690         if (sk_X509_POLICY_NODE_num(nodes) <= 0)
691             return X509_PCY_TREE_FAILURE;
692     }
693     return X509_PCY_TREE_VALID;
694
695  error:
696     X509_policy_tree_free(tree);
697     return X509_PCY_TREE_INTERNAL;
698 }