Various RT doc fixes
[openssl.git] / crypto / x509v3 / pcy_tree.c
1 /*
2  * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
3  * 2004.
4  */
5 /* ====================================================================
6  * Copyright (c) 2004 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
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
18  *    distribution.
19  *
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/)"
24  *
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.
29  *
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.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
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  * ====================================================================
52  *
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).
56  *
57  */
58
59 #include "internal/cryptlib.h"
60 #include <openssl/x509.h>
61 #include <openssl/x509v3.h>
62
63 #include "pcy_int.h"
64
65 /*
66  * Enable this to print out the complete policy tree at various point during
67  * evaluation.
68  */
69
70 /*
71  * #define OPENSSL_POLICY_DEBUG
72  */
73
74 #ifdef OPENSSL_POLICY_DEBUG
75
76 static void expected_print(BIO *err, X509_POLICY_LEVEL *lev,
77                            X509_POLICY_NODE *node, int indent)
78 {
79     if ((lev->flags & X509_V_FLAG_INHIBIT_MAP)
80         || !(node->data->flags & POLICY_DATA_FLAG_MAP_MASK))
81         BIO_puts(err, "  Not Mapped\n");
82     else {
83         int i;
84         STACK_OF(ASN1_OBJECT) *pset = node->data->expected_policy_set;
85         ASN1_OBJECT *oid;
86         BIO_puts(err, "  Expected: ");
87         for (i = 0; i < sk_ASN1_OBJECT_num(pset); i++) {
88             oid = sk_ASN1_OBJECT_value(pset, i);
89             if (i)
90                 BIO_puts(err, ", ");
91             i2a_ASN1_OBJECT(err, oid);
92         }
93         BIO_puts(err, "\n");
94     }
95 }
96
97 static void tree_print(char *str, X509_POLICY_TREE *tree,
98                        X509_POLICY_LEVEL *curr)
99 {
100     X509_POLICY_LEVEL *plev;
101     X509_POLICY_NODE *node;
102     int i;
103     BIO *err;
104     err = BIO_new_fp(stderr, BIO_NOCLOSE);
105     if (err == NULL)
106         return;
107     if (!curr)
108         curr = tree->levels + tree->nlevel;
109     else
110         curr++;
111     BIO_printf(err, "Level print after %s\n", str);
112     BIO_printf(err, "Printing Up to Level %ld\n", curr - tree->levels);
113     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++) {
117             node = sk_X509_POLICY_NODE_value(plev->nodes, i);
118             X509_POLICY_NODE_print(err, node, 2);
119             expected_print(err, plev, node, 2);
120             BIO_printf(err, "  Flags: %x\n", node->data->flags);
121         }
122         if (plev->anyPolicy)
123             X509_POLICY_NODE_print(err, plev->anyPolicy, 2);
124     }
125
126     BIO_free(err);
127
128 }
129 #else
130
131 # define tree_print(a,b,c)      /* */
132
133 #endif
134
135 /*-
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.
143  */
144
145 static int tree_init(X509_POLICY_TREE **ptree, STACK_OF(X509) *certs,
146                      unsigned int flags)
147 {
148     X509_POLICY_TREE *tree;
149     X509_POLICY_LEVEL *level;
150     const X509_POLICY_CACHE *cache;
151     X509_POLICY_DATA *data = NULL;
152     X509 *x;
153     int ret = 1;
154     int i, n;
155     int explicit_policy;
156     int any_skip;
157     int map_skip;
158
159     *ptree = NULL;
160     n = sk_X509_num(certs);
161
162     if (flags & X509_V_FLAG_EXPLICIT_POLICY)
163         explicit_policy = 0;
164     else
165         explicit_policy = n + 1;
166
167     if (flags & X509_V_FLAG_INHIBIT_ANY)
168         any_skip = 0;
169     else
170         any_skip = n + 1;
171
172     if (flags & X509_V_FLAG_INHIBIT_MAP)
173         map_skip = 0;
174     else
175         map_skip = n + 1;
176
177     /* Can't do anything with just a trust anchor */
178     if (n == 1)
179         return 1;
180     /*
181      * First setup policy cache in all certificates apart from the trust
182      * anchor. Note any bad cache results on the way. Also can calculate
183      * explicit_policy value at this point.
184      */
185     for (i = n - 2; i >= 0; i--) {
186         uint32_t ex_flags;
187         x = sk_X509_value(certs, i);
188
189         /*
190          * Note, this modifies x->ex_flags.  If cache NULL something bad
191          * happened: return immediately
192          */
193         cache = policy_cache_set(x);
194         if (cache == NULL)
195             return 0;
196         /*
197          * If inconsistent extensions keep a note of it but continue
198          */
199         ex_flags = X509_get_extension_flags(x);
200         if (ex_flags & EXFLAG_INVALID_POLICY)
201             ret = -1;
202         /*
203          * Otherwise if we have no data (hence no CertificatePolicies) and
204          * haven't already set an inconsistent code note it.
205          */
206         else if ((ret == 1) && !cache->data)
207             ret = 2;
208         if (explicit_policy > 0) {
209             if (!(ex_flags & EXFLAG_SI))
210                 explicit_policy--;
211             if ((cache->explicit_skip != -1)
212                 && (cache->explicit_skip < explicit_policy))
213                 explicit_policy = cache->explicit_skip;
214         }
215     }
216
217     if (ret != 1) {
218         if (ret == 2 && !explicit_policy)
219             return 6;
220         return ret;
221     }
222
223     /* If we get this far initialize the tree */
224     tree = OPENSSL_zalloc(sizeof(*tree));
225     if (tree == NULL)
226         return 0;
227     tree->levels = OPENSSL_zalloc(sizeof(*tree->levels) * n);
228     if (tree->levels == NULL) {
229         OPENSSL_free(tree);
230         return 0;
231     }
232     tree->nlevel = n;
233     level = tree->levels;
234
235     /* Root data: initialize to anyPolicy */
236     data = policy_data_new(NULL, OBJ_nid2obj(NID_any_policy), 0);
237
238     if (data == NULL || !level_add_node(level, data, NULL, tree))
239         goto bad_tree;
240
241     for (i = n - 2; i >= 0; i--) {
242         uint32_t ex_flags;
243         level++;
244         x = sk_X509_value(certs, i);
245         ex_flags = X509_get_extension_flags(x);
246         cache = policy_cache_set(x);
247         X509_up_ref(x);
248         level->cert = x;
249
250         if (!cache->anyPolicy)
251             level->flags |= X509_V_FLAG_INHIBIT_ANY;
252
253         /* Determine inhibit any and inhibit map flags */
254         if (any_skip == 0) {
255             /*
256              * Any matching allowed if certificate is self issued and not the
257              * last in the chain.
258              */
259             if (!(ex_flags & EXFLAG_SI) || (i == 0))
260                 level->flags |= X509_V_FLAG_INHIBIT_ANY;
261         } else {
262             if (!(ex_flags & EXFLAG_SI))
263                 any_skip--;
264             if ((cache->any_skip >= 0)
265                 && (cache->any_skip < any_skip))
266                 any_skip = cache->any_skip;
267         }
268
269         if (map_skip == 0)
270             level->flags |= X509_V_FLAG_INHIBIT_MAP;
271         else {
272             if (!(ex_flags & EXFLAG_SI))
273                 map_skip--;
274             if ((cache->map_skip >= 0)
275                 && (cache->map_skip < map_skip))
276                 map_skip = cache->map_skip;
277         }
278
279     }
280
281     *ptree = tree;
282
283     if (explicit_policy)
284         return 1;
285     else
286         return 5;
287
288  bad_tree:
289
290     X509_policy_tree_free(tree);
291
292     return 0;
293
294 }
295
296 static int tree_link_matching_nodes(X509_POLICY_LEVEL *curr,
297                                     X509_POLICY_DATA *data)
298 {
299     X509_POLICY_LEVEL *last = curr - 1;
300     X509_POLICY_NODE *node;
301     int i, matched = 0;
302     /* Iterate through all in nodes linking matches */
303     for (i = 0; i < sk_X509_POLICY_NODE_num(last->nodes); i++) {
304         node = sk_X509_POLICY_NODE_value(last->nodes, i);
305         if (policy_node_match(last, node, data->valid_policy)) {
306             if (!level_add_node(curr, data, node, NULL))
307                 return 0;
308             matched = 1;
309         }
310     }
311     if (!matched && last->anyPolicy) {
312         if (!level_add_node(curr, data, last->anyPolicy, NULL))
313             return 0;
314     }
315     return 1;
316 }
317
318 /*
319  * This corresponds to RFC3280 6.1.3(d)(1): link any data from
320  * CertificatePolicies onto matching parent or anyPolicy if no match.
321  */
322
323 static int tree_link_nodes(X509_POLICY_LEVEL *curr,
324                            const X509_POLICY_CACHE *cache)
325 {
326     int i;
327     X509_POLICY_DATA *data;
328
329     for (i = 0; i < sk_X509_POLICY_DATA_num(cache->data); i++) {
330         data = sk_X509_POLICY_DATA_value(cache->data, i);
331         /* Look for matching nodes in previous level */
332         if (!tree_link_matching_nodes(curr, data))
333             return 0;
334     }
335     return 1;
336 }
337
338 /*
339  * This corresponds to RFC3280 6.1.3(d)(2): Create new data for any unmatched
340  * policies in the parent and link to anyPolicy.
341  */
342
343 static int tree_add_unmatched(X509_POLICY_LEVEL *curr,
344                               const X509_POLICY_CACHE *cache,
345                               const ASN1_OBJECT *id,
346                               X509_POLICY_NODE *node, X509_POLICY_TREE *tree)
347 {
348     X509_POLICY_DATA *data;
349     if (id == NULL)
350         id = node->data->valid_policy;
351     /*
352      * Create a new node with qualifiers from anyPolicy and id from unmatched
353      * node.
354      */
355     data = policy_data_new(NULL, id, node_critical(node));
356
357     if (data == NULL)
358         return 0;
359     /* Curr may not have anyPolicy */
360     data->qualifier_set = cache->anyPolicy->qualifier_set;
361     data->flags |= POLICY_DATA_FLAG_SHARED_QUALIFIERS;
362     if (!level_add_node(curr, data, node, tree)) {
363         policy_data_free(data);
364         return 0;
365     }
366
367     return 1;
368 }
369
370 static int tree_link_unmatched(X509_POLICY_LEVEL *curr,
371                                const X509_POLICY_CACHE *cache,
372                                X509_POLICY_NODE *node, X509_POLICY_TREE *tree)
373 {
374     const X509_POLICY_LEVEL *last = curr - 1;
375     int i;
376
377     if ((last->flags & X509_V_FLAG_INHIBIT_MAP)
378         || !(node->data->flags & POLICY_DATA_FLAG_MAPPED)) {
379         /* If no policy mapping: matched if one child present */
380         if (node->nchild)
381             return 1;
382         if (!tree_add_unmatched(curr, cache, NULL, node, tree))
383             return 0;
384         /* Add it */
385     } else {
386         /* If mapping: matched if one child per expected policy set */
387         STACK_OF(ASN1_OBJECT) *expset = node->data->expected_policy_set;
388         if (node->nchild == sk_ASN1_OBJECT_num(expset))
389             return 1;
390         /* Locate unmatched nodes */
391         for (i = 0; i < sk_ASN1_OBJECT_num(expset); i++) {
392             ASN1_OBJECT *oid = sk_ASN1_OBJECT_value(expset, i);
393             if (level_find_node(curr, node, oid))
394                 continue;
395             if (!tree_add_unmatched(curr, cache, oid, node, tree))
396                 return 0;
397         }
398
399     }
400
401     return 1;
402
403 }
404
405 static int tree_link_any(X509_POLICY_LEVEL *curr,
406                          const X509_POLICY_CACHE *cache,
407                          X509_POLICY_TREE *tree)
408 {
409     int i;
410     X509_POLICY_NODE *node;
411     X509_POLICY_LEVEL *last = curr - 1;
412
413     for (i = 0; i < sk_X509_POLICY_NODE_num(last->nodes); i++) {
414         node = sk_X509_POLICY_NODE_value(last->nodes, i);
415
416         if (!tree_link_unmatched(curr, cache, node, tree))
417             return 0;
418     }
419     /* Finally add link to anyPolicy */
420     if (last->anyPolicy) {
421         if (!level_add_node(curr, cache->anyPolicy, last->anyPolicy, NULL))
422             return 0;
423     }
424     return 1;
425 }
426
427 /*
428  * Prune the tree: delete any child mapped child data on the current level
429  * then proceed up the tree deleting any data with no children. If we ever
430  * have no data on a level we can halt because the tree will be empty.
431  */
432
433 static int tree_prune(X509_POLICY_TREE *tree, X509_POLICY_LEVEL *curr)
434 {
435     STACK_OF(X509_POLICY_NODE) *nodes;
436     X509_POLICY_NODE *node;
437     int i;
438     nodes = curr->nodes;
439     if (curr->flags & X509_V_FLAG_INHIBIT_MAP) {
440         for (i = sk_X509_POLICY_NODE_num(nodes) - 1; i >= 0; i--) {
441             node = sk_X509_POLICY_NODE_value(nodes, i);
442             /* Delete any mapped data: see RFC3280 XXXX */
443             if (node->data->flags & POLICY_DATA_FLAG_MAP_MASK) {
444                 node->parent->nchild--;
445                 OPENSSL_free(node);
446                 (void)sk_X509_POLICY_NODE_delete(nodes, i);
447             }
448         }
449     }
450
451     for (;;) {
452         --curr;
453         nodes = curr->nodes;
454         for (i = sk_X509_POLICY_NODE_num(nodes) - 1; i >= 0; i--) {
455             node = sk_X509_POLICY_NODE_value(nodes, i);
456             if (node->nchild == 0) {
457                 node->parent->nchild--;
458                 OPENSSL_free(node);
459                 (void)sk_X509_POLICY_NODE_delete(nodes, i);
460             }
461         }
462         if (curr->anyPolicy && !curr->anyPolicy->nchild) {
463             if (curr->anyPolicy->parent)
464                 curr->anyPolicy->parent->nchild--;
465             OPENSSL_free(curr->anyPolicy);
466             curr->anyPolicy = NULL;
467         }
468         if (curr == tree->levels) {
469             /* If we zapped anyPolicy at top then tree is empty */
470             if (!curr->anyPolicy)
471                 return 2;
472             return 1;
473         }
474     }
475
476     /* Unreachable */
477
478 }
479
480 static int tree_add_auth_node(STACK_OF(X509_POLICY_NODE) **pnodes,
481                               X509_POLICY_NODE *pcy)
482 {
483     if (*pnodes == NULL) {
484         *pnodes = policy_node_cmp_new();
485         if (*pnodes == NULL)
486             return 0;
487     } else if (sk_X509_POLICY_NODE_find(*pnodes, pcy) != -1)
488         return 1;
489
490     if (!sk_X509_POLICY_NODE_push(*pnodes, pcy))
491         return 0;
492
493     return 1;
494
495 }
496
497 /*
498  * Calculate the authority set based on policy tree. The 'pnodes' parameter
499  * is used as a store for the set of policy nodes used to calculate the user
500  * set. If the authority set is not anyPolicy then pnodes will just point to
501  * the authority set. If however the authority set is anyPolicy then the set
502  * of valid policies (other than anyPolicy) is store in pnodes. The return
503  * value of '2' is used in this case to indicate that pnodes should be freed.
504  */
505
506 static int tree_calculate_authority_set(X509_POLICY_TREE *tree,
507                                         STACK_OF(X509_POLICY_NODE) **pnodes)
508 {
509     X509_POLICY_LEVEL *curr;
510     X509_POLICY_NODE *node, *anyptr;
511     STACK_OF(X509_POLICY_NODE) **addnodes;
512     int i, j;
513     curr = tree->levels + tree->nlevel - 1;
514
515     /* If last level contains anyPolicy set is anyPolicy */
516     if (curr->anyPolicy) {
517         if (!tree_add_auth_node(&tree->auth_policies, curr->anyPolicy))
518             return 0;
519         addnodes = pnodes;
520     } else
521         /* Add policies to authority set */
522         addnodes = &tree->auth_policies;
523
524     curr = tree->levels;
525     for (i = 1; i < tree->nlevel; i++) {
526         /*
527          * If no anyPolicy node on this this level it can't appear on lower
528          * levels so end search.
529          */
530         if ((anyptr = curr->anyPolicy) == NULL)
531             break;
532         curr++;
533         for (j = 0; j < sk_X509_POLICY_NODE_num(curr->nodes); j++) {
534             node = sk_X509_POLICY_NODE_value(curr->nodes, j);
535             if ((node->parent == anyptr)
536                 && !tree_add_auth_node(addnodes, node))
537                 return 0;
538         }
539     }
540
541     if (addnodes == pnodes)
542         return 2;
543
544     *pnodes = tree->auth_policies;
545
546     return 1;
547 }
548
549 static int tree_calculate_user_set(X509_POLICY_TREE *tree,
550                                    STACK_OF(ASN1_OBJECT) *policy_oids,
551                                    STACK_OF(X509_POLICY_NODE) *auth_nodes)
552 {
553     int i;
554     X509_POLICY_NODE *node;
555     ASN1_OBJECT *oid;
556
557     X509_POLICY_NODE *anyPolicy;
558     X509_POLICY_DATA *extra;
559
560     /*
561      * Check if anyPolicy present in authority constrained policy set: this
562      * will happen if it is a leaf node.
563      */
564
565     if (sk_ASN1_OBJECT_num(policy_oids) <= 0)
566         return 1;
567
568     anyPolicy = tree->levels[tree->nlevel - 1].anyPolicy;
569
570     for (i = 0; i < sk_ASN1_OBJECT_num(policy_oids); i++) {
571         oid = sk_ASN1_OBJECT_value(policy_oids, i);
572         if (OBJ_obj2nid(oid) == NID_any_policy) {
573             tree->flags |= POLICY_FLAG_ANY_POLICY;
574             return 1;
575         }
576     }
577
578     for (i = 0; i < sk_ASN1_OBJECT_num(policy_oids); i++) {
579         oid = sk_ASN1_OBJECT_value(policy_oids, i);
580         node = tree_find_sk(auth_nodes, oid);
581         if (!node) {
582             if (!anyPolicy)
583                 continue;
584             /*
585              * Create a new node with policy ID from user set and qualifiers
586              * from anyPolicy.
587              */
588             extra = policy_data_new(NULL, oid, node_critical(anyPolicy));
589             if (extra == NULL)
590                 return 0;
591             extra->qualifier_set = anyPolicy->data->qualifier_set;
592             extra->flags = POLICY_DATA_FLAG_SHARED_QUALIFIERS
593                 | POLICY_DATA_FLAG_EXTRA_NODE;
594             node = level_add_node(NULL, extra, anyPolicy->parent, tree);
595         }
596         if (!tree->user_policies) {
597             tree->user_policies = sk_X509_POLICY_NODE_new_null();
598             if (!tree->user_policies)
599                 return 1;
600         }
601         if (!sk_X509_POLICY_NODE_push(tree->user_policies, node))
602             return 0;
603     }
604     return 1;
605
606 }
607
608 static int tree_evaluate(X509_POLICY_TREE *tree)
609 {
610     int ret, i;
611     X509_POLICY_LEVEL *curr = tree->levels + 1;
612     const X509_POLICY_CACHE *cache;
613
614     for (i = 1; i < tree->nlevel; i++, curr++) {
615         cache = policy_cache_set(curr->cert);
616         if (!tree_link_nodes(curr, cache))
617             return 0;
618
619         if (!(curr->flags & X509_V_FLAG_INHIBIT_ANY)
620             && !tree_link_any(curr, cache, tree))
621             return 0;
622         tree_print("before tree_prune()", tree, curr);
623         ret = tree_prune(tree, curr);
624         if (ret != 1)
625             return ret;
626     }
627
628     return 1;
629
630 }
631
632 static void exnode_free(X509_POLICY_NODE *node)
633 {
634     if (node->data && (node->data->flags & POLICY_DATA_FLAG_EXTRA_NODE))
635         OPENSSL_free(node);
636 }
637
638 void X509_policy_tree_free(X509_POLICY_TREE *tree)
639 {
640     X509_POLICY_LEVEL *curr;
641     int i;
642
643     if (!tree)
644         return;
645
646     sk_X509_POLICY_NODE_free(tree->auth_policies);
647     sk_X509_POLICY_NODE_pop_free(tree->user_policies, exnode_free);
648
649     for (i = 0, curr = tree->levels; i < tree->nlevel; i++, curr++) {
650         X509_free(curr->cert);
651         sk_X509_POLICY_NODE_pop_free(curr->nodes, policy_node_free);
652         policy_node_free(curr->anyPolicy);
653     }
654
655     sk_X509_POLICY_DATA_pop_free(tree->extra_data, policy_data_free);
656     OPENSSL_free(tree->levels);
657     OPENSSL_free(tree);
658
659 }
660
661 /*-
662  * Application policy checking function.
663  * Return codes:
664  *  0   Internal Error.
665  *  1   Successful.
666  * -1   One or more certificates contain invalid or inconsistent extensions
667  * -2   User constrained policy set empty and requireExplicit true.
668  */
669
670 int X509_policy_check(X509_POLICY_TREE **ptree, int *pexplicit_policy,
671                       STACK_OF(X509) *certs,
672                       STACK_OF(ASN1_OBJECT) *policy_oids, unsigned int flags)
673 {
674     int ret;
675     X509_POLICY_TREE *tree = NULL;
676     STACK_OF(X509_POLICY_NODE) *nodes, *auth_nodes = NULL;
677     *ptree = NULL;
678
679     *pexplicit_policy = 0;
680     ret = tree_init(&tree, certs, flags);
681
682     switch (ret) {
683
684         /* Tree empty requireExplicit False: OK */
685     case 2:
686         return 1;
687
688         /* Some internal error */
689     case -1:
690         return -1;
691
692         /* Some internal error */
693     case 0:
694         return 0;
695
696         /* Tree empty requireExplicit True: Error */
697
698     case 6:
699         *pexplicit_policy = 1;
700         return -2;
701
702         /* Tree OK requireExplicit True: OK and continue */
703     case 5:
704         *pexplicit_policy = 1;
705         break;
706
707         /* Tree OK: continue */
708
709     case 1:
710         if (!tree)
711             /*
712              * tree_init() returns success and a null tree
713              * if it's just looking at a trust anchor.
714              * I'm not sure that returning success here is
715              * correct, but I'm sure that reporting this
716              * as an internal error which our caller
717              * interprets as a malloc failure is wrong.
718              */
719             return 1;
720         break;
721     }
722
723     if (!tree)
724         goto error;
725     ret = tree_evaluate(tree);
726
727     tree_print("tree_evaluate()", tree, NULL);
728
729     if (ret <= 0)
730         goto error;
731
732     /* Return value 2 means tree empty */
733     if (ret == 2) {
734         X509_policy_tree_free(tree);
735         if (*pexplicit_policy)
736             return -2;
737         else
738             return 1;
739     }
740
741     /* Tree is not empty: continue */
742
743     ret = tree_calculate_authority_set(tree, &auth_nodes);
744
745     if (!ret)
746         goto error;
747
748     if (!tree_calculate_user_set(tree, policy_oids, auth_nodes))
749         goto error;
750
751     if (ret == 2)
752         sk_X509_POLICY_NODE_free(auth_nodes);
753
754     if (tree)
755         *ptree = tree;
756
757     if (*pexplicit_policy) {
758         nodes = X509_policy_tree_get0_user_policies(tree);
759         if (sk_X509_POLICY_NODE_num(nodes) <= 0)
760             return -2;
761     }
762
763     return 1;
764
765  error:
766
767     X509_policy_tree_free(tree);
768
769     return 0;
770
771 }