Debian Codegraph @

external/boringssl/crypto/x509v3/pcy_tree.c

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