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