]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/x509v3/pcy_tree.c
mark all block comments that need format preserving so that
[thirdparty/openssl.git] / crypto / x509v3 / pcy_tree.c
1 /* pcy_tree.c */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 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 "cryptlib.h"
60 #include <openssl/x509.h>
61 #include <openssl/x509v3.h>
62
63 #include "pcy_int.h"
64
65 /* Enable this to print out the complete policy tree at various point during
66 * evaluation.
67 */
68
69 /*#define OPENSSL_POLICY_DEBUG*/
70
71 #ifdef OPENSSL_POLICY_DEBUG
72
73 static void expected_print(BIO *err, X509_POLICY_LEVEL *lev,
74 X509_POLICY_NODE *node, int indent)
75 {
76 if ( (lev->flags & X509_V_FLAG_INHIBIT_MAP)
77 || !(node->data->flags & POLICY_DATA_FLAG_MAP_MASK))
78 BIO_puts(err, " Not Mapped\n");
79 else
80 {
81 int i;
82 STACK_OF(ASN1_OBJECT) *pset = node->data->expected_policy_set;
83 ASN1_OBJECT *oid;
84 BIO_puts(err, " Expected: ");
85 for (i = 0; i < sk_ASN1_OBJECT_num(pset); i++)
86 {
87 oid = sk_ASN1_OBJECT_value(pset, i);
88 if (i)
89 BIO_puts(err, ", ");
90 i2a_ASN1_OBJECT(err, oid);
91 }
92 BIO_puts(err, "\n");
93 }
94 }
95
96 static void tree_print(char *str, X509_POLICY_TREE *tree,
97 X509_POLICY_LEVEL *curr)
98 {
99 X509_POLICY_LEVEL *plev;
100 X509_POLICY_NODE *node;
101 int i;
102 BIO *err;
103 err = BIO_new_fp(stderr, BIO_NOCLOSE);
104 if (err == NULL)
105 return;
106 if (!curr)
107 curr = tree->levels + tree->nlevel;
108 else
109 curr++;
110 BIO_printf(err, "Level print after %s\n", str);
111 BIO_printf(err, "Printing Up to Level %ld\n", curr - tree->levels);
112 for (plev = tree->levels; plev != curr; plev++)
113 {
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 {
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 /* 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 *ptree = NULL;
159 n = sk_X509_num(certs);
160
161 #if 0
162 /* Disable policy mapping for now... */
163 flags |= X509_V_FLAG_INHIBIT_MAP;
164 #endif
165
166 if (flags & X509_V_FLAG_EXPLICIT_POLICY)
167 explicit_policy = 0;
168 else
169 explicit_policy = n + 1;
170
171 if (flags & X509_V_FLAG_INHIBIT_ANY)
172 any_skip = 0;
173 else
174 any_skip = n + 1;
175
176 if (flags & X509_V_FLAG_INHIBIT_MAP)
177 map_skip = 0;
178 else
179 map_skip = n + 1;
180
181 /* Can't do anything with just a trust anchor */
182 if (n == 1)
183 return 1;
184 /* First setup policy cache in all certificates apart from the
185 * trust anchor. Note any bad cache results on the way. Also can
186 * calculate explicit_policy value at this point.
187 */
188 for (i = n - 2; i >= 0; i--)
189 {
190 x = sk_X509_value(certs, i);
191 X509_check_purpose(x, -1, -1);
192 cache = policy_cache_set(x);
193 /* If cache NULL something bad happened: return immediately */
194 if (cache == NULL)
195 return 0;
196 /* If inconsistent extensions keep a note of it but continue */
197 if (x->ex_flags & EXFLAG_INVALID_POLICY)
198 ret = -1;
199 /* Otherwise if we have no data (hence no CertificatePolicies)
200 * and 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 {
206 if (!(x->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 {
216 if (ret == 2 && !explicit_policy)
217 return 6;
218 return ret;
219 }
220
221
222 /* If we get this far initialize the tree */
223
224 tree = OPENSSL_malloc(sizeof(X509_POLICY_TREE));
225
226 if (!tree)
227 return 0;
228
229 tree->flags = 0;
230 tree->levels = OPENSSL_malloc(sizeof(X509_POLICY_LEVEL) * n);
231 tree->nlevel = 0;
232 tree->extra_data = NULL;
233 tree->auth_policies = NULL;
234 tree->user_policies = NULL;
235
236 if (!tree->levels)
237 {
238 OPENSSL_free(tree);
239 return 0;
240 }
241
242 memset(tree->levels, 0, n * sizeof(X509_POLICY_LEVEL));
243
244 tree->nlevel = n;
245
246 level = tree->levels;
247
248 /* Root data: initialize to anyPolicy */
249
250 data = policy_data_new(NULL, OBJ_nid2obj(NID_any_policy), 0);
251
252 if (!data || !level_add_node(level, data, NULL, tree))
253 goto bad_tree;
254
255 for (i = n - 2; i >= 0; i--)
256 {
257 level++;
258 x = sk_X509_value(certs, i);
259 cache = policy_cache_set(x);
260 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
261 level->cert = x;
262
263 if (!cache->anyPolicy)
264 level->flags |= X509_V_FLAG_INHIBIT_ANY;
265
266 /* Determine inhibit any and inhibit map flags */
267 if (any_skip == 0)
268 {
269 /* Any matching allowed if certificate is self
270 * issued and not the last in the chain.
271 */
272 if (!(x->ex_flags & EXFLAG_SI) || (i == 0))
273 level->flags |= X509_V_FLAG_INHIBIT_ANY;
274 }
275 else
276 {
277 if (!(x->ex_flags & EXFLAG_SI))
278 any_skip--;
279 if ((cache->any_skip >= 0)
280 && (cache->any_skip < any_skip))
281 any_skip = cache->any_skip;
282 }
283
284 if (map_skip == 0)
285 level->flags |= X509_V_FLAG_INHIBIT_MAP;
286 else
287 {
288 if (!(x->ex_flags & EXFLAG_SI))
289 map_skip--;
290 if ((cache->map_skip >= 0)
291 && (cache->map_skip < map_skip))
292 map_skip = cache->map_skip;
293 }
294
295 }
296
297 *ptree = tree;
298
299 if (explicit_policy)
300 return 1;
301 else
302 return 5;
303
304 bad_tree:
305
306 X509_policy_tree_free(tree);
307
308 return 0;
309
310 }
311
312 static int tree_link_matching_nodes(X509_POLICY_LEVEL *curr,
313 const X509_POLICY_DATA *data)
314 {
315 X509_POLICY_LEVEL *last = curr - 1;
316 X509_POLICY_NODE *node;
317 int i, matched = 0;
318 /* Iterate through all in nodes linking matches */
319 for (i = 0; i < sk_X509_POLICY_NODE_num(last->nodes); i++)
320 {
321 node = sk_X509_POLICY_NODE_value(last->nodes, i);
322 if (policy_node_match(last, node, data->valid_policy))
323 {
324 if (!level_add_node(curr, data, node, NULL))
325 return 0;
326 matched = 1;
327 }
328 }
329 if (!matched && last->anyPolicy)
330 {
331 if (!level_add_node(curr, data, last->anyPolicy, NULL))
332 return 0;
333 }
334 return 1;
335 }
336
337 /* This corresponds to RFC3280 6.1.3(d)(1):
338 * link any data from CertificatePolicies onto matching parent
339 * or anyPolicy if no match.
340 */
341
342 static int tree_link_nodes(X509_POLICY_LEVEL *curr,
343 const X509_POLICY_CACHE *cache)
344 {
345 int i;
346 X509_POLICY_DATA *data;
347
348 for (i = 0; i < sk_X509_POLICY_DATA_num(cache->data); i++)
349 {
350 data = sk_X509_POLICY_DATA_value(cache->data, i);
351 /* If a node is mapped any it doesn't have a corresponding
352 * CertificatePolicies entry.
353 * However such an identical node would be created
354 * if anyPolicy matching is enabled because there would be
355 * no match with the parent valid_policy_set. So we create
356 * link because then it will have the mapping flags
357 * right and we can prune it later.
358 */
359 #if 0
360 if ((data->flags & POLICY_DATA_FLAG_MAPPED_ANY)
361 && !(curr->flags & X509_V_FLAG_INHIBIT_ANY))
362 continue;
363 #endif
364 /* Look for matching nodes in previous level */
365 if (!tree_link_matching_nodes(curr, data))
366 return 0;
367 }
368 return 1;
369 }
370
371 /* This corresponds to RFC3280 6.1.3(d)(2):
372 * Create new data for any unmatched policies in the parent and link
373 * to anyPolicy.
374 */
375
376 static int tree_add_unmatched(X509_POLICY_LEVEL *curr,
377 const X509_POLICY_CACHE *cache,
378 const ASN1_OBJECT *id,
379 X509_POLICY_NODE *node,
380 X509_POLICY_TREE *tree)
381 {
382 X509_POLICY_DATA *data;
383 if (id == NULL)
384 id = node->data->valid_policy;
385 /* Create a new node with qualifiers from anyPolicy and
386 * id from unmatched node.
387 */
388 data = policy_data_new(NULL, id, node_critical(node));
389
390 if (data == NULL)
391 return 0;
392 /* Curr may not have anyPolicy */
393 data->qualifier_set = cache->anyPolicy->qualifier_set;
394 data->flags |= POLICY_DATA_FLAG_SHARED_QUALIFIERS;
395 if (!level_add_node(curr, data, node, tree))
396 {
397 policy_data_free(data);
398 return 0;
399 }
400
401 return 1;
402 }
403
404 static int tree_link_unmatched(X509_POLICY_LEVEL *curr,
405 const X509_POLICY_CACHE *cache,
406 X509_POLICY_NODE *node,
407 X509_POLICY_TREE *tree)
408 {
409 const X509_POLICY_LEVEL *last = curr - 1;
410 int i;
411
412 if ( (last->flags & X509_V_FLAG_INHIBIT_MAP)
413 || !(node->data->flags & POLICY_DATA_FLAG_MAPPED))
414 {
415 /* If no policy mapping: matched if one child present */
416 if (node->nchild)
417 return 1;
418 if (!tree_add_unmatched(curr, cache, NULL, node, tree))
419 return 0;
420 /* Add it */
421 }
422 else
423 {
424 /* If mapping: matched if one child per expected policy set */
425 STACK_OF(ASN1_OBJECT) *expset = node->data->expected_policy_set;
426 if (node->nchild == sk_ASN1_OBJECT_num(expset))
427 return 1;
428 /* Locate unmatched nodes */
429 for (i = 0; i < sk_ASN1_OBJECT_num(expset); i++)
430 {
431 ASN1_OBJECT *oid = sk_ASN1_OBJECT_value(expset, i);
432 if (level_find_node(curr, node, oid))
433 continue;
434 if (!tree_add_unmatched(curr, cache, oid, node, tree))
435 return 0;
436 }
437
438 }
439
440 return 1;
441
442 }
443
444 static int tree_link_any(X509_POLICY_LEVEL *curr,
445 const X509_POLICY_CACHE *cache,
446 X509_POLICY_TREE *tree)
447 {
448 int i;
449 /*X509_POLICY_DATA *data;*/
450 X509_POLICY_NODE *node;
451 X509_POLICY_LEVEL *last = curr - 1;
452
453 for (i = 0; i < sk_X509_POLICY_NODE_num(last->nodes); i++)
454 {
455 node = sk_X509_POLICY_NODE_value(last->nodes, i);
456
457 if (!tree_link_unmatched(curr, cache, node, tree))
458 return 0;
459
460 #if 0
461
462 /* Skip any node with any children: we only want unmathced
463 * nodes.
464 *
465 * Note: need something better for policy mapping
466 * because each node may have multiple children
467 */
468 if (node->nchild)
469 continue;
470
471 /* Create a new node with qualifiers from anyPolicy and
472 * id from unmatched node.
473 */
474 data = policy_data_new(NULL, node->data->valid_policy,
475 node_critical(node));
476
477 if (data == NULL)
478 return 0;
479 /* Curr may not have anyPolicy */
480 data->qualifier_set = cache->anyPolicy->qualifier_set;
481 data->flags |= POLICY_DATA_FLAG_SHARED_QUALIFIERS;
482 if (!level_add_node(curr, data, node, tree))
483 {
484 policy_data_free(data);
485 return 0;
486 }
487
488 #endif
489
490 }
491 /* Finally add link to anyPolicy */
492 if (last->anyPolicy)
493 {
494 if (!level_add_node(curr, cache->anyPolicy,
495 last->anyPolicy, NULL))
496 return 0;
497 }
498 return 1;
499 }
500
501 /* Prune the tree: delete any child mapped child data on the current level
502 * then proceed up the tree deleting any data with no children. If we ever
503 * have no data on a level we can halt because the tree will be empty.
504 */
505
506 static int tree_prune(X509_POLICY_TREE *tree, X509_POLICY_LEVEL *curr)
507 {
508 STACK_OF(X509_POLICY_NODE) *nodes;
509 X509_POLICY_NODE *node;
510 int i;
511 nodes = curr->nodes;
512 if (curr->flags & X509_V_FLAG_INHIBIT_MAP)
513 {
514 for (i = sk_X509_POLICY_NODE_num(nodes) - 1; i >= 0; i--)
515 {
516 node = sk_X509_POLICY_NODE_value(nodes, i);
517 /* Delete any mapped data: see RFC3280 XXXX */
518 if (node->data->flags & POLICY_DATA_FLAG_MAP_MASK)
519 {
520 node->parent->nchild--;
521 OPENSSL_free(node);
522 (void)sk_X509_POLICY_NODE_delete(nodes,i);
523 }
524 }
525 }
526
527 for(;;) {
528 --curr;
529 nodes = curr->nodes;
530 for (i = sk_X509_POLICY_NODE_num(nodes) - 1; i >= 0; i--)
531 {
532 node = sk_X509_POLICY_NODE_value(nodes, i);
533 if (node->nchild == 0)
534 {
535 node->parent->nchild--;
536 OPENSSL_free(node);
537 (void)sk_X509_POLICY_NODE_delete(nodes, i);
538 }
539 }
540 if (curr->anyPolicy && !curr->anyPolicy->nchild)
541 {
542 if (curr->anyPolicy->parent)
543 curr->anyPolicy->parent->nchild--;
544 OPENSSL_free(curr->anyPolicy);
545 curr->anyPolicy = NULL;
546 }
547 if (curr == tree->levels)
548 {
549 /* If we zapped anyPolicy at top then tree is empty */
550 if (!curr->anyPolicy)
551 return 2;
552 return 1;
553 }
554 }
555
556 return 1;
557
558 }
559
560 static int tree_add_auth_node(STACK_OF(X509_POLICY_NODE) **pnodes,
561 X509_POLICY_NODE *pcy)
562 {
563 if (!*pnodes)
564 {
565 *pnodes = policy_node_cmp_new();
566 if (!*pnodes)
567 return 0;
568 }
569 else if (sk_X509_POLICY_NODE_find(*pnodes, pcy) != -1)
570 return 1;
571
572 if (!sk_X509_POLICY_NODE_push(*pnodes, pcy))
573 return 0;
574
575 return 1;
576
577 }
578
579 /* Calculate the authority set based on policy tree.
580 * The 'pnodes' parameter is used as a store for the set of policy nodes
581 * used to calculate the user set. If the authority set is not anyPolicy
582 * then pnodes will just point to the authority set. If however the authority
583 * set is anyPolicy then the set of valid policies (other than anyPolicy)
584 * is store in pnodes. The return value of '2' is used in this case to indicate
585 * that pnodes should be freed.
586 */
587
588 static int tree_calculate_authority_set(X509_POLICY_TREE *tree,
589 STACK_OF(X509_POLICY_NODE) **pnodes)
590 {
591 X509_POLICY_LEVEL *curr;
592 X509_POLICY_NODE *node, *anyptr;
593 STACK_OF(X509_POLICY_NODE) **addnodes;
594 int i, j;
595 curr = tree->levels + tree->nlevel - 1;
596
597 /* If last level contains anyPolicy set is anyPolicy */
598 if (curr->anyPolicy)
599 {
600 if (!tree_add_auth_node(&tree->auth_policies, curr->anyPolicy))
601 return 0;
602 addnodes = pnodes;
603 }
604 else
605 /* Add policies to authority set */
606 addnodes = &tree->auth_policies;
607
608 curr = tree->levels;
609 for (i = 1; i < tree->nlevel; i++)
610 {
611 /* If no anyPolicy node on this this level it can't
612 * appear on lower levels so end search.
613 */
614 if (!(anyptr = curr->anyPolicy))
615 break;
616 curr++;
617 for (j = 0; j < sk_X509_POLICY_NODE_num(curr->nodes); j++)
618 {
619 node = sk_X509_POLICY_NODE_value(curr->nodes, j);
620 if ((node->parent == anyptr)
621 && !tree_add_auth_node(addnodes, node))
622 return 0;
623 }
624 }
625
626 if (addnodes == pnodes)
627 return 2;
628
629 *pnodes = tree->auth_policies;
630
631 return 1;
632 }
633
634 static int tree_calculate_user_set(X509_POLICY_TREE *tree,
635 STACK_OF(ASN1_OBJECT) *policy_oids,
636 STACK_OF(X509_POLICY_NODE) *auth_nodes)
637 {
638 int i;
639 X509_POLICY_NODE *node;
640 ASN1_OBJECT *oid;
641
642 X509_POLICY_NODE *anyPolicy;
643 X509_POLICY_DATA *extra;
644
645 /* Check if anyPolicy present in authority constrained policy set:
646 * this will happen if it is a leaf node.
647 */
648
649 if (sk_ASN1_OBJECT_num(policy_oids) <= 0)
650 return 1;
651
652 anyPolicy = tree->levels[tree->nlevel - 1].anyPolicy;
653
654 for (i = 0; i < sk_ASN1_OBJECT_num(policy_oids); i++)
655 {
656 oid = sk_ASN1_OBJECT_value(policy_oids, i);
657 if (OBJ_obj2nid(oid) == NID_any_policy)
658 {
659 tree->flags |= POLICY_FLAG_ANY_POLICY;
660 return 1;
661 }
662 }
663
664 for (i = 0; i < sk_ASN1_OBJECT_num(policy_oids); i++)
665 {
666 oid = sk_ASN1_OBJECT_value(policy_oids, i);
667 node = tree_find_sk(auth_nodes, oid);
668 if (!node)
669 {
670 if (!anyPolicy)
671 continue;
672 /* Create a new node with policy ID from user set
673 * and qualifiers from anyPolicy.
674 */
675 extra = policy_data_new(NULL, oid,
676 node_critical(anyPolicy));
677 if (!extra)
678 return 0;
679 extra->qualifier_set = anyPolicy->data->qualifier_set;
680 extra->flags = POLICY_DATA_FLAG_SHARED_QUALIFIERS
681 | POLICY_DATA_FLAG_EXTRA_NODE;
682 node = level_add_node(NULL, extra, anyPolicy->parent,
683 tree);
684 }
685 if (!tree->user_policies)
686 {
687 tree->user_policies = sk_X509_POLICY_NODE_new_null();
688 if (!tree->user_policies)
689 return 1;
690 }
691 if (!sk_X509_POLICY_NODE_push(tree->user_policies, node))
692 return 0;
693 }
694 return 1;
695
696 }
697
698 static int tree_evaluate(X509_POLICY_TREE *tree)
699 {
700 int ret, i;
701 X509_POLICY_LEVEL *curr = tree->levels + 1;
702 const X509_POLICY_CACHE *cache;
703
704 for(i = 1; i < tree->nlevel; i++, curr++)
705 {
706 cache = policy_cache_set(curr->cert);
707 if (!tree_link_nodes(curr, cache))
708 return 0;
709
710 if (!(curr->flags & X509_V_FLAG_INHIBIT_ANY)
711 && !tree_link_any(curr, cache, tree))
712 return 0;
713 tree_print("before tree_prune()", tree, curr);
714 ret = tree_prune(tree, curr);
715 if (ret != 1)
716 return ret;
717 }
718
719 return 1;
720
721 }
722
723 static void exnode_free(X509_POLICY_NODE *node)
724 {
725 if (node->data && (node->data->flags & POLICY_DATA_FLAG_EXTRA_NODE))
726 OPENSSL_free(node);
727 }
728
729
730 void X509_policy_tree_free(X509_POLICY_TREE *tree)
731 {
732 X509_POLICY_LEVEL *curr;
733 int i;
734
735 if (!tree)
736 return;
737
738 sk_X509_POLICY_NODE_free(tree->auth_policies);
739 sk_X509_POLICY_NODE_pop_free(tree->user_policies, exnode_free);
740
741 for(i = 0, curr = tree->levels; i < tree->nlevel; i++, curr++)
742 {
743 if (curr->cert)
744 X509_free(curr->cert);
745 if (curr->nodes)
746 sk_X509_POLICY_NODE_pop_free(curr->nodes,
747 policy_node_free);
748 if (curr->anyPolicy)
749 policy_node_free(curr->anyPolicy);
750 }
751
752 if (tree->extra_data)
753 sk_X509_POLICY_DATA_pop_free(tree->extra_data,
754 policy_data_free);
755
756 OPENSSL_free(tree->levels);
757 OPENSSL_free(tree);
758
759 }
760
761 /*-
762 * Application policy checking function.
763 * Return codes:
764 * 0 Internal Error.
765 * 1 Successful.
766 * -1 One or more certificates contain invalid or inconsistent extensions
767 * -2 User constrained policy set empty and requireExplicit true.
768 */
769
770 int X509_policy_check(X509_POLICY_TREE **ptree, int *pexplicit_policy,
771 STACK_OF(X509) *certs,
772 STACK_OF(ASN1_OBJECT) *policy_oids,
773 unsigned int flags)
774 {
775 int ret;
776 X509_POLICY_TREE *tree = NULL;
777 STACK_OF(X509_POLICY_NODE) *nodes, *auth_nodes = NULL;
778 *ptree = NULL;
779
780 *pexplicit_policy = 0;
781 ret = tree_init(&tree, certs, flags);
782
783 switch (ret)
784 {
785
786 /* Tree empty requireExplicit False: OK */
787 case 2:
788 return 1;
789
790 /* Some internal error */
791 case -1:
792 return -1;
793
794 /* Some internal error */
795 case 0:
796 return 0;
797
798 /* Tree empty requireExplicit True: Error */
799
800 case 6:
801 *pexplicit_policy = 1;
802 return -2;
803
804 /* Tree OK requireExplicit True: OK and continue */
805 case 5:
806 *pexplicit_policy = 1;
807 break;
808
809 /* Tree OK: continue */
810
811 case 1:
812 if (!tree)
813 /*
814 * tree_init() returns success and a null tree
815 * if it's just looking at a trust anchor.
816 * I'm not sure that returning success here is
817 * correct, but I'm sure that reporting this
818 * as an internal error which our caller
819 * interprets as a malloc failure is wrong.
820 */
821 return 1;
822 break;
823 }
824
825 if (!tree) goto error;
826 ret = tree_evaluate(tree);
827
828 tree_print("tree_evaluate()", tree, NULL);
829
830 if (ret <= 0)
831 goto error;
832
833 /* Return value 2 means tree empty */
834 if (ret == 2)
835 {
836 X509_policy_tree_free(tree);
837 if (*pexplicit_policy)
838 return -2;
839 else
840 return 1;
841 }
842
843 /* Tree is not empty: continue */
844
845 ret = tree_calculate_authority_set(tree, &auth_nodes);
846
847 if (!ret)
848 goto error;
849
850 if (!tree_calculate_user_set(tree, policy_oids, auth_nodes))
851 goto error;
852
853 if (ret == 2)
854 sk_X509_POLICY_NODE_free(auth_nodes);
855
856 if (tree)
857 *ptree = tree;
858
859 if (*pexplicit_policy)
860 {
861 nodes = X509_policy_tree_get0_user_policies(tree);
862 if (sk_X509_POLICY_NODE_num(nodes) <= 0)
863 return -2;
864 }
865
866 return 1;
867
868 error:
869
870 X509_policy_tree_free(tree);
871
872 return 0;
873
874 }
875