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