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