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