Remove /* foo.c */ comments
[oweals/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         ex_flags = X509_get_extension_flags(x);
189         X509_check_purpose(x, -1, -1);
190         cache = policy_cache_set(x);
191         /* If cache NULL something bad happened: return immediately */
192         if (cache == NULL)
193             return 0;
194         /*
195          * If inconsistent extensions keep a note of it but continue
196          */
197         if (ex_flags & EXFLAG_INVALID_POLICY)
198             ret = -1;
199         /*
200          * Otherwise if we have no data (hence no CertificatePolicies) and
201          * haven't already set an inconsistent code note it.
202          */
203         else if ((ret == 1) && !cache->data)
204             ret = 2;
205         if (explicit_policy > 0) {
206             if (!(ex_flags & EXFLAG_SI))
207                 explicit_policy--;
208             if ((cache->explicit_skip != -1)
209                 && (cache->explicit_skip < explicit_policy))
210                 explicit_policy = cache->explicit_skip;
211         }
212     }
213
214     if (ret != 1) {
215         if (ret == 2 && !explicit_policy)
216             return 6;
217         return ret;
218     }
219
220     /* If we get this far initialize the tree */
221     tree = OPENSSL_zalloc(sizeof(*tree));
222     if (tree == NULL)
223         return 0;
224     tree->levels = OPENSSL_zalloc(sizeof(*tree->levels) * n);
225     if (tree->levels == NULL) {
226         OPENSSL_free(tree);
227         return 0;
228     }
229     tree->nlevel = n;
230     level = tree->levels;
231
232     /* Root data: initialize to anyPolicy */
233     data = policy_data_new(NULL, OBJ_nid2obj(NID_any_policy), 0);
234
235     if (data == NULL || !level_add_node(level, data, NULL, tree))
236         goto bad_tree;
237
238     for (i = n - 2; i >= 0; i--) {
239         uint32_t ex_flags;
240         level++;
241         x = sk_X509_value(certs, i);
242         ex_flags = X509_get_extension_flags(x);
243         cache = policy_cache_set(x);
244         X509_up_ref(x);
245         level->cert = x;
246
247         if (!cache->anyPolicy)
248             level->flags |= X509_V_FLAG_INHIBIT_ANY;
249
250         /* Determine inhibit any and inhibit map flags */
251         if (any_skip == 0) {
252             /*
253              * Any matching allowed if certificate is self issued and not the
254              * last in the chain.
255              */
256             if (!(ex_flags & EXFLAG_SI) || (i == 0))
257                 level->flags |= X509_V_FLAG_INHIBIT_ANY;
258         } else {
259             if (!(ex_flags & EXFLAG_SI))
260                 any_skip--;
261             if ((cache->any_skip >= 0)
262                 && (cache->any_skip < any_skip))
263                 any_skip = cache->any_skip;
264         }
265
266         if (map_skip == 0)
267             level->flags |= X509_V_FLAG_INHIBIT_MAP;
268         else {
269             if (!(ex_flags & EXFLAG_SI))
270                 map_skip--;
271             if ((cache->map_skip >= 0)
272                 && (cache->map_skip < map_skip))
273                 map_skip = cache->map_skip;
274         }
275
276     }
277
278     *ptree = tree;
279
280     if (explicit_policy)
281         return 1;
282     else
283         return 5;
284
285  bad_tree:
286
287     X509_policy_tree_free(tree);
288
289     return 0;
290
291 }
292
293 static int tree_link_matching_nodes(X509_POLICY_LEVEL *curr,
294                                     X509_POLICY_DATA *data)
295 {
296     X509_POLICY_LEVEL *last = curr - 1;
297     X509_POLICY_NODE *node;
298     int i, matched = 0;
299     /* Iterate through all in nodes linking matches */
300     for (i = 0; i < sk_X509_POLICY_NODE_num(last->nodes); i++) {
301         node = sk_X509_POLICY_NODE_value(last->nodes, i);
302         if (policy_node_match(last, node, data->valid_policy)) {
303             if (!level_add_node(curr, data, node, NULL))
304                 return 0;
305             matched = 1;
306         }
307     }
308     if (!matched && last->anyPolicy) {
309         if (!level_add_node(curr, data, last->anyPolicy, NULL))
310             return 0;
311     }
312     return 1;
313 }
314
315 /*
316  * This corresponds to RFC3280 6.1.3(d)(1): link any data from
317  * CertificatePolicies onto matching parent or anyPolicy if no match.
318  */
319
320 static int tree_link_nodes(X509_POLICY_LEVEL *curr,
321                            const X509_POLICY_CACHE *cache)
322 {
323     int i;
324     X509_POLICY_DATA *data;
325
326     for (i = 0; i < sk_X509_POLICY_DATA_num(cache->data); i++) {
327         data = sk_X509_POLICY_DATA_value(cache->data, i);
328         /* Look for matching nodes in previous level */
329         if (!tree_link_matching_nodes(curr, data))
330             return 0;
331     }
332     return 1;
333 }
334
335 /*
336  * This corresponds to RFC3280 6.1.3(d)(2): Create new data for any unmatched
337  * policies in the parent and link to anyPolicy.
338  */
339
340 static int tree_add_unmatched(X509_POLICY_LEVEL *curr,
341                               const X509_POLICY_CACHE *cache,
342                               const ASN1_OBJECT *id,
343                               X509_POLICY_NODE *node, X509_POLICY_TREE *tree)
344 {
345     X509_POLICY_DATA *data;
346     if (id == NULL)
347         id = node->data->valid_policy;
348     /*
349      * Create a new node with qualifiers from anyPolicy and id from unmatched
350      * node.
351      */
352     data = policy_data_new(NULL, id, node_critical(node));
353
354     if (data == NULL)
355         return 0;
356     /* Curr may not have anyPolicy */
357     data->qualifier_set = cache->anyPolicy->qualifier_set;
358     data->flags |= POLICY_DATA_FLAG_SHARED_QUALIFIERS;
359     if (!level_add_node(curr, data, node, tree)) {
360         policy_data_free(data);
361         return 0;
362     }
363
364     return 1;
365 }
366
367 static int tree_link_unmatched(X509_POLICY_LEVEL *curr,
368                                const X509_POLICY_CACHE *cache,
369                                X509_POLICY_NODE *node, X509_POLICY_TREE *tree)
370 {
371     const X509_POLICY_LEVEL *last = curr - 1;
372     int i;
373
374     if ((last->flags & X509_V_FLAG_INHIBIT_MAP)
375         || !(node->data->flags & POLICY_DATA_FLAG_MAPPED)) {
376         /* If no policy mapping: matched if one child present */
377         if (node->nchild)
378             return 1;
379         if (!tree_add_unmatched(curr, cache, NULL, node, tree))
380             return 0;
381         /* Add it */
382     } else {
383         /* If mapping: matched if one child per expected policy set */
384         STACK_OF(ASN1_OBJECT) *expset = node->data->expected_policy_set;
385         if (node->nchild == sk_ASN1_OBJECT_num(expset))
386             return 1;
387         /* Locate unmatched nodes */
388         for (i = 0; i < sk_ASN1_OBJECT_num(expset); i++) {
389             ASN1_OBJECT *oid = sk_ASN1_OBJECT_value(expset, i);
390             if (level_find_node(curr, node, oid))
391                 continue;
392             if (!tree_add_unmatched(curr, cache, oid, node, tree))
393                 return 0;
394         }
395
396     }
397
398     return 1;
399
400 }
401
402 static int tree_link_any(X509_POLICY_LEVEL *curr,
403                          const X509_POLICY_CACHE *cache,
404                          X509_POLICY_TREE *tree)
405 {
406     int i;
407     X509_POLICY_NODE *node;
408     X509_POLICY_LEVEL *last = curr - 1;
409
410     for (i = 0; i < sk_X509_POLICY_NODE_num(last->nodes); i++) {
411         node = sk_X509_POLICY_NODE_value(last->nodes, i);
412
413         if (!tree_link_unmatched(curr, cache, node, tree))
414             return 0;
415     }
416     /* Finally add link to anyPolicy */
417     if (last->anyPolicy) {
418         if (!level_add_node(curr, cache->anyPolicy, last->anyPolicy, NULL))
419             return 0;
420     }
421     return 1;
422 }
423
424 /*
425  * Prune the tree: delete any child mapped child data on the current level
426  * then proceed up the tree deleting any data with no children. If we ever
427  * have no data on a level we can halt because the tree will be empty.
428  */
429
430 static int tree_prune(X509_POLICY_TREE *tree, X509_POLICY_LEVEL *curr)
431 {
432     STACK_OF(X509_POLICY_NODE) *nodes;
433     X509_POLICY_NODE *node;
434     int i;
435     nodes = curr->nodes;
436     if (curr->flags & X509_V_FLAG_INHIBIT_MAP) {
437         for (i = sk_X509_POLICY_NODE_num(nodes) - 1; i >= 0; i--) {
438             node = sk_X509_POLICY_NODE_value(nodes, i);
439             /* Delete any mapped data: see RFC3280 XXXX */
440             if (node->data->flags & POLICY_DATA_FLAG_MAP_MASK) {
441                 node->parent->nchild--;
442                 OPENSSL_free(node);
443                 (void)sk_X509_POLICY_NODE_delete(nodes, i);
444             }
445         }
446     }
447
448     for (;;) {
449         --curr;
450         nodes = curr->nodes;
451         for (i = sk_X509_POLICY_NODE_num(nodes) - 1; i >= 0; i--) {
452             node = sk_X509_POLICY_NODE_value(nodes, i);
453             if (node->nchild == 0) {
454                 node->parent->nchild--;
455                 OPENSSL_free(node);
456                 (void)sk_X509_POLICY_NODE_delete(nodes, i);
457             }
458         }
459         if (curr->anyPolicy && !curr->anyPolicy->nchild) {
460             if (curr->anyPolicy->parent)
461                 curr->anyPolicy->parent->nchild--;
462             OPENSSL_free(curr->anyPolicy);
463             curr->anyPolicy = NULL;
464         }
465         if (curr == tree->levels) {
466             /* If we zapped anyPolicy at top then tree is empty */
467             if (!curr->anyPolicy)
468                 return 2;
469             return 1;
470         }
471     }
472
473     /* Unreachable */
474
475 }
476
477 static int tree_add_auth_node(STACK_OF(X509_POLICY_NODE) **pnodes,
478                               X509_POLICY_NODE *pcy)
479 {
480     if (*pnodes == NULL) {
481         *pnodes = policy_node_cmp_new();
482         if (*pnodes == NULL)
483             return 0;
484     } else if (sk_X509_POLICY_NODE_find(*pnodes, pcy) != -1)
485         return 1;
486
487     if (!sk_X509_POLICY_NODE_push(*pnodes, pcy))
488         return 0;
489
490     return 1;
491
492 }
493
494 /*
495  * Calculate the authority set based on policy tree. The 'pnodes' parameter
496  * is used as a store for the set of policy nodes used to calculate the user
497  * set. If the authority set is not anyPolicy then pnodes will just point to
498  * the authority set. If however the authority set is anyPolicy then the set
499  * of valid policies (other than anyPolicy) is store in pnodes. The return
500  * value of '2' is used in this case to indicate that pnodes should be freed.
501  */
502
503 static int tree_calculate_authority_set(X509_POLICY_TREE *tree,
504                                         STACK_OF(X509_POLICY_NODE) **pnodes)
505 {
506     X509_POLICY_LEVEL *curr;
507     X509_POLICY_NODE *node, *anyptr;
508     STACK_OF(X509_POLICY_NODE) **addnodes;
509     int i, j;
510     curr = tree->levels + tree->nlevel - 1;
511
512     /* If last level contains anyPolicy set is anyPolicy */
513     if (curr->anyPolicy) {
514         if (!tree_add_auth_node(&tree->auth_policies, curr->anyPolicy))
515             return 0;
516         addnodes = pnodes;
517     } else
518         /* Add policies to authority set */
519         addnodes = &tree->auth_policies;
520
521     curr = tree->levels;
522     for (i = 1; i < tree->nlevel; i++) {
523         /*
524          * If no anyPolicy node on this this level it can't appear on lower
525          * levels so end search.
526          */
527         if ((anyptr = curr->anyPolicy) == NULL)
528             break;
529         curr++;
530         for (j = 0; j < sk_X509_POLICY_NODE_num(curr->nodes); j++) {
531             node = sk_X509_POLICY_NODE_value(curr->nodes, j);
532             if ((node->parent == anyptr)
533                 && !tree_add_auth_node(addnodes, node))
534                 return 0;
535         }
536     }
537
538     if (addnodes == pnodes)
539         return 2;
540
541     *pnodes = tree->auth_policies;
542
543     return 1;
544 }
545
546 static int tree_calculate_user_set(X509_POLICY_TREE *tree,
547                                    STACK_OF(ASN1_OBJECT) *policy_oids,
548                                    STACK_OF(X509_POLICY_NODE) *auth_nodes)
549 {
550     int i;
551     X509_POLICY_NODE *node;
552     ASN1_OBJECT *oid;
553
554     X509_POLICY_NODE *anyPolicy;
555     X509_POLICY_DATA *extra;
556
557     /*
558      * Check if anyPolicy present in authority constrained policy set: this
559      * will happen if it is a leaf node.
560      */
561
562     if (sk_ASN1_OBJECT_num(policy_oids) <= 0)
563         return 1;
564
565     anyPolicy = tree->levels[tree->nlevel - 1].anyPolicy;
566
567     for (i = 0; i < sk_ASN1_OBJECT_num(policy_oids); i++) {
568         oid = sk_ASN1_OBJECT_value(policy_oids, i);
569         if (OBJ_obj2nid(oid) == NID_any_policy) {
570             tree->flags |= POLICY_FLAG_ANY_POLICY;
571             return 1;
572         }
573     }
574
575     for (i = 0; i < sk_ASN1_OBJECT_num(policy_oids); i++) {
576         oid = sk_ASN1_OBJECT_value(policy_oids, i);
577         node = tree_find_sk(auth_nodes, oid);
578         if (!node) {
579             if (!anyPolicy)
580                 continue;
581             /*
582              * Create a new node with policy ID from user set and qualifiers
583              * from anyPolicy.
584              */
585             extra = policy_data_new(NULL, oid, node_critical(anyPolicy));
586             if (extra == NULL)
587                 return 0;
588             extra->qualifier_set = anyPolicy->data->qualifier_set;
589             extra->flags = POLICY_DATA_FLAG_SHARED_QUALIFIERS
590                 | POLICY_DATA_FLAG_EXTRA_NODE;
591             node = level_add_node(NULL, extra, anyPolicy->parent, tree);
592         }
593         if (!tree->user_policies) {
594             tree->user_policies = sk_X509_POLICY_NODE_new_null();
595             if (!tree->user_policies)
596                 return 1;
597         }
598         if (!sk_X509_POLICY_NODE_push(tree->user_policies, node))
599             return 0;
600     }
601     return 1;
602
603 }
604
605 static int tree_evaluate(X509_POLICY_TREE *tree)
606 {
607     int ret, i;
608     X509_POLICY_LEVEL *curr = tree->levels + 1;
609     const X509_POLICY_CACHE *cache;
610
611     for (i = 1; i < tree->nlevel; i++, curr++) {
612         cache = policy_cache_set(curr->cert);
613         if (!tree_link_nodes(curr, cache))
614             return 0;
615
616         if (!(curr->flags & X509_V_FLAG_INHIBIT_ANY)
617             && !tree_link_any(curr, cache, tree))
618             return 0;
619         tree_print("before tree_prune()", tree, curr);
620         ret = tree_prune(tree, curr);
621         if (ret != 1)
622             return ret;
623     }
624
625     return 1;
626
627 }
628
629 static void exnode_free(X509_POLICY_NODE *node)
630 {
631     if (node->data && (node->data->flags & POLICY_DATA_FLAG_EXTRA_NODE))
632         OPENSSL_free(node);
633 }
634
635 void X509_policy_tree_free(X509_POLICY_TREE *tree)
636 {
637     X509_POLICY_LEVEL *curr;
638     int i;
639
640     if (!tree)
641         return;
642
643     sk_X509_POLICY_NODE_free(tree->auth_policies);
644     sk_X509_POLICY_NODE_pop_free(tree->user_policies, exnode_free);
645
646     for (i = 0, curr = tree->levels; i < tree->nlevel; i++, curr++) {
647         X509_free(curr->cert);
648         sk_X509_POLICY_NODE_pop_free(curr->nodes, policy_node_free);
649         policy_node_free(curr->anyPolicy);
650     }
651
652     sk_X509_POLICY_DATA_pop_free(tree->extra_data, policy_data_free);
653     OPENSSL_free(tree->levels);
654     OPENSSL_free(tree);
655
656 }
657
658 /*-
659  * Application policy checking function.
660  * Return codes:
661  *  0   Internal Error.
662  *  1   Successful.
663  * -1   One or more certificates contain invalid or inconsistent extensions
664  * -2   User constrained policy set empty and requireExplicit true.
665  */
666
667 int X509_policy_check(X509_POLICY_TREE **ptree, int *pexplicit_policy,
668                       STACK_OF(X509) *certs,
669                       STACK_OF(ASN1_OBJECT) *policy_oids, unsigned int flags)
670 {
671     int ret;
672     X509_POLICY_TREE *tree = NULL;
673     STACK_OF(X509_POLICY_NODE) *nodes, *auth_nodes = NULL;
674     *ptree = NULL;
675
676     *pexplicit_policy = 0;
677     ret = tree_init(&tree, certs, flags);
678
679     switch (ret) {
680
681         /* Tree empty requireExplicit False: OK */
682     case 2:
683         return 1;
684
685         /* Some internal error */
686     case -1:
687         return -1;
688
689         /* Some internal error */
690     case 0:
691         return 0;
692
693         /* Tree empty requireExplicit True: Error */
694
695     case 6:
696         *pexplicit_policy = 1;
697         return -2;
698
699         /* Tree OK requireExplicit True: OK and continue */
700     case 5:
701         *pexplicit_policy = 1;
702         break;
703
704         /* Tree OK: continue */
705
706     case 1:
707         if (!tree)
708             /*
709              * tree_init() returns success and a null tree
710              * if it's just looking at a trust anchor.
711              * I'm not sure that returning success here is
712              * correct, but I'm sure that reporting this
713              * as an internal error which our caller
714              * interprets as a malloc failure is wrong.
715              */
716             return 1;
717         break;
718     }
719
720     if (!tree)
721         goto error;
722     ret = tree_evaluate(tree);
723
724     tree_print("tree_evaluate()", tree, NULL);
725
726     if (ret <= 0)
727         goto error;
728
729     /* Return value 2 means tree empty */
730     if (ret == 2) {
731         X509_policy_tree_free(tree);
732         if (*pexplicit_policy)
733             return -2;
734         else
735             return 1;
736     }
737
738     /* Tree is not empty: continue */
739
740     ret = tree_calculate_authority_set(tree, &auth_nodes);
741
742     if (!ret)
743         goto error;
744
745     if (!tree_calculate_user_set(tree, policy_oids, auth_nodes))
746         goto error;
747
748     if (ret == 2)
749         sk_X509_POLICY_NODE_free(auth_nodes);
750
751     if (tree)
752         *ptree = tree;
753
754     if (*pexplicit_policy) {
755         nodes = X509_policy_tree_get0_user_policies(tree);
756         if (sk_X509_POLICY_NODE_num(nodes) <= 0)
757             return -2;
758     }
759
760     return 1;
761
762  error:
763
764     X509_policy_tree_free(tree);
765
766     return 0;
767
768 }