]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/x509v3/pcy_tree.c
Add X509_up_ref function.
[thirdparty/openssl.git] / crypto / x509v3 / pcy_tree.c
CommitLineData
4acc3e90 1/* pcy_tree.c */
0f113f3e
MC
2/*
3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
4 * 2004.
4acc3e90
DSH
5 */
6/* ====================================================================
7 * Copyright (c) 2004 The OpenSSL Project. All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
0f113f3e 14 * notice, this list of conditions and the following disclaimer.
4acc3e90
DSH
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 *
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
30 *
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
34 *
35 * 6. Redistributions of any form whatsoever must retain the following
36 * acknowledgment:
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
53 *
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
57 *
58 */
59
b39fc560 60#include "internal/cryptlib.h"
4acc3e90
DSH
61#include <openssl/x509.h>
62#include <openssl/x509v3.h>
63
64#include "pcy_int.h"
65
0f113f3e
MC
66/*
67 * Enable this to print out the complete policy tree at various point during
002e66c0
DSH
68 * evaluation.
69 */
70
0f113f3e
MC
71/*
72 * #define OPENSSL_POLICY_DEBUG
73 */
002e66c0
DSH
74
75#ifdef OPENSSL_POLICY_DEBUG
76
77static void expected_print(BIO *err, X509_POLICY_LEVEL *lev,
0f113f3e
MC
78 X509_POLICY_NODE *node, int indent)
79{
80 if ((lev->flags & X509_V_FLAG_INHIBIT_MAP)
81 || !(node->data->flags & POLICY_DATA_FLAG_MAP_MASK))
82 BIO_puts(err, " Not Mapped\n");
83 else {
84 int i;
85 STACK_OF(ASN1_OBJECT) *pset = node->data->expected_policy_set;
86 ASN1_OBJECT *oid;
87 BIO_puts(err, " Expected: ");
88 for (i = 0; i < sk_ASN1_OBJECT_num(pset); i++) {
89 oid = sk_ASN1_OBJECT_value(pset, i);
90 if (i)
91 BIO_puts(err, ", ");
92 i2a_ASN1_OBJECT(err, oid);
93 }
94 BIO_puts(err, "\n");
95 }
96}
002e66c0
DSH
97
98static void tree_print(char *str, X509_POLICY_TREE *tree,
0f113f3e
MC
99 X509_POLICY_LEVEL *curr)
100{
101 X509_POLICY_LEVEL *plev;
102 X509_POLICY_NODE *node;
103 int i;
104 BIO *err;
105 err = BIO_new_fp(stderr, BIO_NOCLOSE);
106 if (err == NULL)
107 return;
108 if (!curr)
109 curr = tree->levels + tree->nlevel;
110 else
111 curr++;
112 BIO_printf(err, "Level print after %s\n", str);
113 BIO_printf(err, "Printing Up to Level %ld\n", curr - tree->levels);
114 for (plev = tree->levels; plev != curr; plev++) {
115 BIO_printf(err, "Level %ld, flags = %x\n",
116 plev - tree->levels, plev->flags);
117 for (i = 0; i < sk_X509_POLICY_NODE_num(plev->nodes); i++) {
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}
002e66c0
DSH
130#else
131
0f113f3e 132# define tree_print(a,b,c) /* */
002e66c0
DSH
133
134#endif
135
3a83462d
MC
136/*-
137 * Initialize policy tree. Return values:
478b50cf 138 * 0 Some internal error occurred.
4acc3e90
DSH
139 * -1 Inconsistent or invalid extensions in certificates.
140 * 1 Tree initialized OK.
141 * 2 Policy tree is empty.
142 * 5 Tree OK and requireExplicitPolicy true.
143 * 6 Tree empty and requireExplicitPolicy true.
144 */
145
146static int tree_init(X509_POLICY_TREE **ptree, STACK_OF(X509) *certs,
0f113f3e
MC
147 unsigned int flags)
148{
149 X509_POLICY_TREE *tree;
150 X509_POLICY_LEVEL *level;
151 const X509_POLICY_CACHE *cache;
152 X509_POLICY_DATA *data = NULL;
153 X509 *x;
154 int ret = 1;
155 int i, n;
156 int explicit_policy;
157 int any_skip;
158 int map_skip;
7aa0b022 159
0f113f3e
MC
160 *ptree = NULL;
161 n = sk_X509_num(certs);
4acc3e90 162
0f113f3e
MC
163 if (flags & X509_V_FLAG_EXPLICIT_POLICY)
164 explicit_policy = 0;
165 else
166 explicit_policy = n + 1;
167
168 if (flags & X509_V_FLAG_INHIBIT_ANY)
169 any_skip = 0;
170 else
171 any_skip = n + 1;
172
173 if (flags & X509_V_FLAG_INHIBIT_MAP)
174 map_skip = 0;
175 else
176 map_skip = n + 1;
177
178 /* Can't do anything with just a trust anchor */
179 if (n == 1)
180 return 1;
181 /*
182 * First setup policy cache in all certificates apart from the trust
183 * anchor. Note any bad cache results on the way. Also can calculate
184 * explicit_policy value at this point.
185 */
186 for (i = n - 2; i >= 0; i--) {
187 x = sk_X509_value(certs, i);
188 X509_check_purpose(x, -1, -1);
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 (x->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 (!(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 if (ret == 2 && !explicit_policy)
215 return 6;
216 return ret;
217 }
218
219 /* If we get this far initialize the tree */
220
b4faea50 221 tree = OPENSSL_malloc(sizeof(*tree));
0f113f3e
MC
222
223 if (!tree)
224 return 0;
225
226 tree->flags = 0;
b4faea50 227 tree->levels = OPENSSL_malloc(sizeof(*tree->levels) * n);
0f113f3e
MC
228 tree->nlevel = 0;
229 tree->extra_data = NULL;
230 tree->auth_policies = NULL;
231 tree->user_policies = NULL;
232
233 if (!tree->levels) {
234 OPENSSL_free(tree);
235 return 0;
236 }
237
16f8d4eb 238 memset(tree->levels, 0, sizeof(*tree->levels) * n);
0f113f3e 239 tree->nlevel = n;
0f113f3e
MC
240 level = tree->levels;
241
242 /* Root data: initialize to anyPolicy */
0f113f3e
MC
243 data = policy_data_new(NULL, OBJ_nid2obj(NID_any_policy), 0);
244
245 if (!data || !level_add_node(level, data, NULL, tree))
246 goto bad_tree;
247
248 for (i = n - 2; i >= 0; i--) {
249 level++;
250 x = sk_X509_value(certs, i);
251 cache = policy_cache_set(x);
05f0fb9f 252 X509_up_ref(x);
0f113f3e
MC
253 level->cert = x;
254
255 if (!cache->anyPolicy)
256 level->flags |= X509_V_FLAG_INHIBIT_ANY;
257
258 /* Determine inhibit any and inhibit map flags */
259 if (any_skip == 0) {
260 /*
261 * Any matching allowed if certificate is self issued and not the
262 * last in the chain.
263 */
264 if (!(x->ex_flags & EXFLAG_SI) || (i == 0))
265 level->flags |= X509_V_FLAG_INHIBIT_ANY;
266 } else {
267 if (!(x->ex_flags & EXFLAG_SI))
268 any_skip--;
269 if ((cache->any_skip >= 0)
270 && (cache->any_skip < any_skip))
271 any_skip = cache->any_skip;
272 }
273
274 if (map_skip == 0)
275 level->flags |= X509_V_FLAG_INHIBIT_MAP;
276 else {
277 if (!(x->ex_flags & EXFLAG_SI))
278 map_skip--;
279 if ((cache->map_skip >= 0)
280 && (cache->map_skip < map_skip))
281 map_skip = cache->map_skip;
282 }
283
284 }
285
286 *ptree = tree;
287
288 if (explicit_policy)
289 return 1;
290 else
291 return 5;
292
293 bad_tree:
294
295 X509_policy_tree_free(tree);
296
297 return 0;
298
299}
4acc3e90 300
002e66c0 301static int tree_link_matching_nodes(X509_POLICY_LEVEL *curr,
0f113f3e
MC
302 const X509_POLICY_DATA *data)
303{
304 X509_POLICY_LEVEL *last = curr - 1;
305 X509_POLICY_NODE *node;
306 int i, matched = 0;
307 /* Iterate through all in nodes linking matches */
308 for (i = 0; i < sk_X509_POLICY_NODE_num(last->nodes); i++) {
309 node = sk_X509_POLICY_NODE_value(last->nodes, i);
310 if (policy_node_match(last, node, data->valid_policy)) {
311 if (!level_add_node(curr, data, node, NULL))
312 return 0;
313 matched = 1;
314 }
315 }
316 if (!matched && last->anyPolicy) {
317 if (!level_add_node(curr, data, last->anyPolicy, NULL))
318 return 0;
319 }
320 return 1;
321}
322
323/*
324 * This corresponds to RFC3280 6.1.3(d)(1): link any data from
325 * CertificatePolicies onto matching parent or anyPolicy if no match.
4acc3e90
DSH
326 */
327
328static int tree_link_nodes(X509_POLICY_LEVEL *curr,
0f113f3e
MC
329 const X509_POLICY_CACHE *cache)
330{
331 int i;
332 X509_POLICY_DATA *data;
333
334 for (i = 0; i < sk_X509_POLICY_DATA_num(cache->data); i++) {
335 data = sk_X509_POLICY_DATA_value(cache->data, i);
0f113f3e
MC
336 /* Look for matching nodes in previous level */
337 if (!tree_link_matching_nodes(curr, data))
338 return 0;
339 }
340 return 1;
341}
342
343/*
344 * This corresponds to RFC3280 6.1.3(d)(2): Create new data for any unmatched
345 * policies in the parent and link to anyPolicy.
4acc3e90
DSH
346 */
347
002e66c0 348static int tree_add_unmatched(X509_POLICY_LEVEL *curr,
0f113f3e
MC
349 const X509_POLICY_CACHE *cache,
350 const ASN1_OBJECT *id,
351 X509_POLICY_NODE *node, X509_POLICY_TREE *tree)
352{
353 X509_POLICY_DATA *data;
354 if (id == NULL)
355 id = node->data->valid_policy;
356 /*
357 * Create a new node with qualifiers from anyPolicy and id from unmatched
358 * node.
359 */
360 data = policy_data_new(NULL, id, node_critical(node));
361
362 if (data == NULL)
363 return 0;
364 /* Curr may not have anyPolicy */
365 data->qualifier_set = cache->anyPolicy->qualifier_set;
366 data->flags |= POLICY_DATA_FLAG_SHARED_QUALIFIERS;
367 if (!level_add_node(curr, data, node, tree)) {
368 policy_data_free(data);
369 return 0;
370 }
371
372 return 1;
373}
002e66c0
DSH
374
375static int tree_link_unmatched(X509_POLICY_LEVEL *curr,
0f113f3e
MC
376 const X509_POLICY_CACHE *cache,
377 X509_POLICY_NODE *node, X509_POLICY_TREE *tree)
378{
379 const X509_POLICY_LEVEL *last = curr - 1;
380 int i;
381
382 if ((last->flags & X509_V_FLAG_INHIBIT_MAP)
383 || !(node->data->flags & POLICY_DATA_FLAG_MAPPED)) {
384 /* If no policy mapping: matched if one child present */
385 if (node->nchild)
386 return 1;
387 if (!tree_add_unmatched(curr, cache, NULL, node, tree))
388 return 0;
389 /* Add it */
390 } else {
391 /* If mapping: matched if one child per expected policy set */
392 STACK_OF(ASN1_OBJECT) *expset = node->data->expected_policy_set;
393 if (node->nchild == sk_ASN1_OBJECT_num(expset))
394 return 1;
395 /* Locate unmatched nodes */
396 for (i = 0; i < sk_ASN1_OBJECT_num(expset); i++) {
397 ASN1_OBJECT *oid = sk_ASN1_OBJECT_value(expset, i);
398 if (level_find_node(curr, node, oid))
399 continue;
400 if (!tree_add_unmatched(curr, cache, oid, node, tree))
401 return 0;
402 }
403
404 }
405
406 return 1;
407
408}
002e66c0 409
4acc3e90 410static int tree_link_any(X509_POLICY_LEVEL *curr,
0f113f3e
MC
411 const X509_POLICY_CACHE *cache,
412 X509_POLICY_TREE *tree)
413{
414 int i;
0f113f3e
MC
415 X509_POLICY_NODE *node;
416 X509_POLICY_LEVEL *last = curr - 1;
417
418 for (i = 0; i < sk_X509_POLICY_NODE_num(last->nodes); i++) {
419 node = sk_X509_POLICY_NODE_value(last->nodes, i);
420
421 if (!tree_link_unmatched(curr, cache, node, tree))
422 return 0;
0f113f3e
MC
423 }
424 /* Finally add link to anyPolicy */
425 if (last->anyPolicy) {
426 if (!level_add_node(curr, cache->anyPolicy, last->anyPolicy, NULL))
427 return 0;
428 }
429 return 1;
430}
431
432/*
433 * Prune the tree: delete any child mapped child data on the current level
4acc3e90
DSH
434 * then proceed up the tree deleting any data with no children. If we ever
435 * have no data on a level we can halt because the tree will be empty.
436 */
437
438static int tree_prune(X509_POLICY_TREE *tree, X509_POLICY_LEVEL *curr)
0f113f3e
MC
439{
440 STACK_OF(X509_POLICY_NODE) *nodes;
441 X509_POLICY_NODE *node;
442 int i;
443 nodes = curr->nodes;
444 if (curr->flags & X509_V_FLAG_INHIBIT_MAP) {
445 for (i = sk_X509_POLICY_NODE_num(nodes) - 1; i >= 0; i--) {
446 node = sk_X509_POLICY_NODE_value(nodes, i);
447 /* Delete any mapped data: see RFC3280 XXXX */
448 if (node->data->flags & POLICY_DATA_FLAG_MAP_MASK) {
449 node->parent->nchild--;
450 OPENSSL_free(node);
451 (void)sk_X509_POLICY_NODE_delete(nodes, i);
452 }
453 }
454 }
455
456 for (;;) {
457 --curr;
458 nodes = curr->nodes;
459 for (i = sk_X509_POLICY_NODE_num(nodes) - 1; i >= 0; i--) {
460 node = sk_X509_POLICY_NODE_value(nodes, i);
461 if (node->nchild == 0) {
462 node->parent->nchild--;
463 OPENSSL_free(node);
464 (void)sk_X509_POLICY_NODE_delete(nodes, i);
465 }
466 }
467 if (curr->anyPolicy && !curr->anyPolicy->nchild) {
468 if (curr->anyPolicy->parent)
469 curr->anyPolicy->parent->nchild--;
470 OPENSSL_free(curr->anyPolicy);
471 curr->anyPolicy = NULL;
472 }
473 if (curr == tree->levels) {
474 /* If we zapped anyPolicy at top then tree is empty */
475 if (!curr->anyPolicy)
476 return 2;
477 return 1;
478 }
479 }
480
c6ef15c4 481 /* Unreachable */
0f113f3e
MC
482
483}
4acc3e90
DSH
484
485static int tree_add_auth_node(STACK_OF(X509_POLICY_NODE) **pnodes,
0f113f3e
MC
486 X509_POLICY_NODE *pcy)
487{
488 if (!*pnodes) {
489 *pnodes = policy_node_cmp_new();
490 if (!*pnodes)
491 return 0;
492 } else if (sk_X509_POLICY_NODE_find(*pnodes, pcy) != -1)
493 return 1;
494
495 if (!sk_X509_POLICY_NODE_push(*pnodes, pcy))
496 return 0;
497
498 return 1;
499
500}
501
502/*
503 * Calculate the authority set based on policy tree. The 'pnodes' parameter
504 * is used as a store for the set of policy nodes used to calculate the user
505 * set. If the authority set is not anyPolicy then pnodes will just point to
506 * the authority set. If however the authority set is anyPolicy then the set
507 * of valid policies (other than anyPolicy) is store in pnodes. The return
508 * value of '2' is used in this case to indicate that pnodes should be freed.
4acc3e90
DSH
509 */
510
511static int tree_calculate_authority_set(X509_POLICY_TREE *tree,
0f113f3e
MC
512 STACK_OF(X509_POLICY_NODE) **pnodes)
513{
514 X509_POLICY_LEVEL *curr;
515 X509_POLICY_NODE *node, *anyptr;
516 STACK_OF(X509_POLICY_NODE) **addnodes;
517 int i, j;
518 curr = tree->levels + tree->nlevel - 1;
519
520 /* If last level contains anyPolicy set is anyPolicy */
521 if (curr->anyPolicy) {
522 if (!tree_add_auth_node(&tree->auth_policies, curr->anyPolicy))
523 return 0;
524 addnodes = pnodes;
525 } else
526 /* Add policies to authority set */
527 addnodes = &tree->auth_policies;
528
529 curr = tree->levels;
530 for (i = 1; i < tree->nlevel; i++) {
531 /*
532 * If no anyPolicy node on this this level it can't appear on lower
533 * levels so end search.
534 */
75ebbd9a 535 if ((anyptr = curr->anyPolicy) == NULL)
0f113f3e
MC
536 break;
537 curr++;
538 for (j = 0; j < sk_X509_POLICY_NODE_num(curr->nodes); j++) {
539 node = sk_X509_POLICY_NODE_value(curr->nodes, j);
540 if ((node->parent == anyptr)
541 && !tree_add_auth_node(addnodes, node))
542 return 0;
543 }
544 }
545
546 if (addnodes == pnodes)
547 return 2;
548
549 *pnodes = tree->auth_policies;
550
551 return 1;
552}
4acc3e90
DSH
553
554static int tree_calculate_user_set(X509_POLICY_TREE *tree,
0f113f3e
MC
555 STACK_OF(ASN1_OBJECT) *policy_oids,
556 STACK_OF(X509_POLICY_NODE) *auth_nodes)
557{
558 int i;
559 X509_POLICY_NODE *node;
560 ASN1_OBJECT *oid;
561
562 X509_POLICY_NODE *anyPolicy;
563 X509_POLICY_DATA *extra;
564
565 /*
566 * Check if anyPolicy present in authority constrained policy set: this
567 * will happen if it is a leaf node.
568 */
569
570 if (sk_ASN1_OBJECT_num(policy_oids) <= 0)
571 return 1;
572
573 anyPolicy = tree->levels[tree->nlevel - 1].anyPolicy;
574
575 for (i = 0; i < sk_ASN1_OBJECT_num(policy_oids); i++) {
576 oid = sk_ASN1_OBJECT_value(policy_oids, i);
577 if (OBJ_obj2nid(oid) == NID_any_policy) {
578 tree->flags |= POLICY_FLAG_ANY_POLICY;
579 return 1;
580 }
581 }
582
583 for (i = 0; i < sk_ASN1_OBJECT_num(policy_oids); i++) {
584 oid = sk_ASN1_OBJECT_value(policy_oids, i);
585 node = tree_find_sk(auth_nodes, oid);
586 if (!node) {
587 if (!anyPolicy)
588 continue;
589 /*
590 * Create a new node with policy ID from user set and qualifiers
591 * from anyPolicy.
592 */
593 extra = policy_data_new(NULL, oid, node_critical(anyPolicy));
594 if (!extra)
595 return 0;
596 extra->qualifier_set = anyPolicy->data->qualifier_set;
597 extra->flags = POLICY_DATA_FLAG_SHARED_QUALIFIERS
598 | POLICY_DATA_FLAG_EXTRA_NODE;
599 node = level_add_node(NULL, extra, anyPolicy->parent, tree);
600 }
601 if (!tree->user_policies) {
602 tree->user_policies = sk_X509_POLICY_NODE_new_null();
603 if (!tree->user_policies)
604 return 1;
605 }
606 if (!sk_X509_POLICY_NODE_push(tree->user_policies, node))
607 return 0;
608 }
609 return 1;
610
611}
4acc3e90
DSH
612
613static int tree_evaluate(X509_POLICY_TREE *tree)
0f113f3e
MC
614{
615 int ret, i;
616 X509_POLICY_LEVEL *curr = tree->levels + 1;
617 const X509_POLICY_CACHE *cache;
4acc3e90 618
0f113f3e
MC
619 for (i = 1; i < tree->nlevel; i++, curr++) {
620 cache = policy_cache_set(curr->cert);
621 if (!tree_link_nodes(curr, cache))
622 return 0;
623
624 if (!(curr->flags & X509_V_FLAG_INHIBIT_ANY)
625 && !tree_link_any(curr, cache, tree))
626 return 0;
627 tree_print("before tree_prune()", tree, curr);
628 ret = tree_prune(tree, curr);
629 if (ret != 1)
630 return ret;
631 }
632
633 return 1;
4acc3e90 634
0f113f3e
MC
635}
636
637static void exnode_free(X509_POLICY_NODE *node)
638{
639 if (node->data && (node->data->flags & POLICY_DATA_FLAG_EXTRA_NODE))
640 OPENSSL_free(node);
641}
4acc3e90
DSH
642
643void X509_policy_tree_free(X509_POLICY_TREE *tree)
0f113f3e
MC
644{
645 X509_POLICY_LEVEL *curr;
646 int i;
4acc3e90 647
0f113f3e
MC
648 if (!tree)
649 return;
4acc3e90 650
0f113f3e
MC
651 sk_X509_POLICY_NODE_free(tree->auth_policies);
652 sk_X509_POLICY_NODE_pop_free(tree->user_policies, exnode_free);
4acc3e90 653
0f113f3e 654 for (i = 0, curr = tree->levels; i < tree->nlevel; i++, curr++) {
222561fe
RS
655 X509_free(curr->cert);
656 sk_X509_POLICY_NODE_pop_free(curr->nodes, policy_node_free);
25aaa98a 657 policy_node_free(curr->anyPolicy);
0f113f3e 658 }
4acc3e90 659
222561fe 660 sk_X509_POLICY_DATA_pop_free(tree->extra_data, policy_data_free);
0f113f3e
MC
661 OPENSSL_free(tree->levels);
662 OPENSSL_free(tree);
4acc3e90 663
0f113f3e 664}
4acc3e90 665
1d97c843
TH
666/*-
667 * Application policy checking function.
4acc3e90 668 * Return codes:
0f113f3e 669 * 0 Internal Error.
4acc3e90
DSH
670 * 1 Successful.
671 * -1 One or more certificates contain invalid or inconsistent extensions
0f113f3e 672 * -2 User constrained policy set empty and requireExplicit true.
4acc3e90
DSH
673 */
674
b6a5fdb8 675int X509_policy_check(X509_POLICY_TREE **ptree, int *pexplicit_policy,
0f113f3e
MC
676 STACK_OF(X509) *certs,
677 STACK_OF(ASN1_OBJECT) *policy_oids, unsigned int flags)
678{
679 int ret;
680 X509_POLICY_TREE *tree = NULL;
681 STACK_OF(X509_POLICY_NODE) *nodes, *auth_nodes = NULL;
682 *ptree = NULL;
4acc3e90 683
0f113f3e
MC
684 *pexplicit_policy = 0;
685 ret = tree_init(&tree, certs, flags);
4acc3e90 686
0f113f3e 687 switch (ret) {
4acc3e90 688
0f113f3e
MC
689 /* Tree empty requireExplicit False: OK */
690 case 2:
691 return 1;
4acc3e90 692
0f113f3e
MC
693 /* Some internal error */
694 case -1:
695 return -1;
002e66c0 696
0f113f3e
MC
697 /* Some internal error */
698 case 0:
699 return 0;
4acc3e90 700
0f113f3e 701 /* Tree empty requireExplicit True: Error */
4acc3e90 702
0f113f3e
MC
703 case 6:
704 *pexplicit_policy = 1;
705 return -2;
4acc3e90 706
0f113f3e
MC
707 /* Tree OK requireExplicit True: OK and continue */
708 case 5:
709 *pexplicit_policy = 1;
710 break;
4acc3e90 711
0f113f3e 712 /* Tree OK: continue */
4acc3e90 713
0f113f3e
MC
714 case 1:
715 if (!tree)
716 /*
717 * tree_init() returns success and a null tree
718 * if it's just looking at a trust anchor.
719 * I'm not sure that returning success here is
720 * correct, but I'm sure that reporting this
721 * as an internal error which our caller
722 * interprets as a malloc failure is wrong.
723 */
724 return 1;
725 break;
726 }
4acc3e90 727
0f113f3e
MC
728 if (!tree)
729 goto error;
730 ret = tree_evaluate(tree);
4acc3e90 731
0f113f3e 732 tree_print("tree_evaluate()", tree, NULL);
002e66c0 733
0f113f3e
MC
734 if (ret <= 0)
735 goto error;
4acc3e90 736
0f113f3e
MC
737 /* Return value 2 means tree empty */
738 if (ret == 2) {
739 X509_policy_tree_free(tree);
740 if (*pexplicit_policy)
741 return -2;
742 else
743 return 1;
744 }
4acc3e90 745
0f113f3e 746 /* Tree is not empty: continue */
4acc3e90 747
0f113f3e 748 ret = tree_calculate_authority_set(tree, &auth_nodes);
4acc3e90 749
0f113f3e
MC
750 if (!ret)
751 goto error;
4acc3e90 752
0f113f3e
MC
753 if (!tree_calculate_user_set(tree, policy_oids, auth_nodes))
754 goto error;
4acc3e90 755
0f113f3e
MC
756 if (ret == 2)
757 sk_X509_POLICY_NODE_free(auth_nodes);
4acc3e90 758
0f113f3e
MC
759 if (tree)
760 *ptree = tree;
4acc3e90 761
0f113f3e
MC
762 if (*pexplicit_policy) {
763 nodes = X509_policy_tree_get0_user_policies(tree);
764 if (sk_X509_POLICY_NODE_num(nodes) <= 0)
765 return -2;
766 }
4acc3e90 767
0f113f3e 768 return 1;
4acc3e90 769
0f113f3e 770 error:
4acc3e90 771
0f113f3e 772 X509_policy_tree_free(tree);
4acc3e90 773
0f113f3e 774 return 0;
4acc3e90 775
0f113f3e 776}