1 /*
2 * Copyright 2004-2023 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 #include "internal/cryptlib.h"
11 #include <openssl/x509.h>
12 #include <openssl/x509v3.h>
13
14 #include "pcy_local.h"
15
16 /*
17 * If the maximum number of nodes in the policy tree isn't defined, set it to
18 * a generous default of 1000 nodes.
19 *
20 * Defining this to be zero means unlimited policy tree growth which opens the
21 * door on CVE-2023-0464.
22 */
23
24 #ifndef OPENSSL_POLICY_TREE_NODES_MAX
25 # define OPENSSL_POLICY_TREE_NODES_MAX 1000
26 #endif
27
28 static void exnode_free(X509_POLICY_NODE *node);
29
30 /*
31 * Enable this to print out the complete policy tree at various point during
32 * evaluation.
33 */
34
35 /*
36 * #define OPENSSL_POLICY_DEBUG
37 */
38
39 #ifdef OPENSSL_POLICY_DEBUG
40
expected_print(BIO * err,X509_POLICY_LEVEL * lev,X509_POLICY_NODE * node,int indent)41 static void expected_print(BIO *err, X509_POLICY_LEVEL *lev,
42 X509_POLICY_NODE *node, int indent)
43 {
44 if ((lev->flags & X509_V_FLAG_INHIBIT_MAP)
45 || !(node->data->flags & POLICY_DATA_FLAG_MAP_MASK))
46 BIO_puts(err, " Not Mapped\n");
47 else {
48 int i;
49 STACK_OF(ASN1_OBJECT) *pset = node->data->expected_policy_set;
50 ASN1_OBJECT *oid;
51 BIO_puts(err, " Expected: ");
52 for (i = 0; i < sk_ASN1_OBJECT_num(pset); i++) {
53 oid = sk_ASN1_OBJECT_value(pset, i);
54 if (i)
55 BIO_puts(err, ", ");
56 i2a_ASN1_OBJECT(err, oid);
57 }
58 BIO_puts(err, "\n");
59 }
60 }
61
tree_print(char * str,X509_POLICY_TREE * tree,X509_POLICY_LEVEL * curr)62 static void tree_print(char *str, X509_POLICY_TREE *tree,
63 X509_POLICY_LEVEL *curr)
64 {
65 BIO *err = BIO_new_fp(stderr, BIO_NOCLOSE);
66 X509_POLICY_LEVEL *plev;
67
68 if (err == NULL)
69 return;
70 if (!curr)
71 curr = tree->levels + tree->nlevel;
72 else
73 curr++;
74
75 BIO_printf(err, "Level print after %s\n", str);
76 BIO_printf(err, "Printing Up to Level %ld\n", curr - tree->levels);
77 for (plev = tree->levels; plev != curr; plev++) {
78 int i;
79
80 BIO_printf(err, "Level %ld, flags = %x\n",
81 (long)(plev - tree->levels), plev->flags);
82 for (i = 0; i < sk_X509_POLICY_NODE_num(plev->nodes); i++) {
83 X509_POLICY_NODE *node = sk_X509_POLICY_NODE_value(plev->nodes, i);
84
85 X509_POLICY_NODE_print(err, node, 2);
86 expected_print(err, plev, node, 2);
87 BIO_printf(err, " Flags: %x\n", node->data->flags);
88 }
89 if (plev->anyPolicy)
90 X509_POLICY_NODE_print(err, plev->anyPolicy, 2);
91 }
92 BIO_free(err);
93 }
94 #endif
95
96 /*-
97 * Return value: <= 0 on error, or positive bit mask:
98 *
99 * X509_PCY_TREE_VALID: valid tree
100 * X509_PCY_TREE_EMPTY: empty tree (including bare TA case)
101 * X509_PCY_TREE_EXPLICIT: explicit policy required
102 */
tree_init(X509_POLICY_TREE ** ptree,STACK_OF (X509)* certs,unsigned int flags)103 static int tree_init(X509_POLICY_TREE **ptree, STACK_OF(X509) *certs,
104 unsigned int flags)
105 {
106 X509_POLICY_TREE *tree;
107 X509_POLICY_LEVEL *level;
108 const X509_POLICY_CACHE *cache;
109 X509_POLICY_DATA *data = NULL;
110 int ret = X509_PCY_TREE_VALID;
111 int n = sk_X509_num(certs) - 1; /* RFC5280 paths omit the TA */
112 int explicit_policy = (flags & X509_V_FLAG_EXPLICIT_POLICY) ? 0 : n+1;
113 int any_skip = (flags & X509_V_FLAG_INHIBIT_ANY) ? 0 : n+1;
114 int map_skip = (flags & X509_V_FLAG_INHIBIT_MAP) ? 0 : n+1;
115 int i;
116
117 *ptree = NULL;
118
119 /* Can't do anything with just a trust anchor */
120 if (n == 0)
121 return X509_PCY_TREE_EMPTY;
122
123 /*
124 * First setup the policy cache in all n non-TA certificates, this will be
125 * used in X509_verify_cert() which will invoke the verify callback for all
126 * certificates with invalid policy extensions.
127 */
128 for (i = n - 1; i >= 0; i--) {
129 X509 *x = sk_X509_value(certs, i);
130
131 /* Call for side-effect of computing hash and caching extensions */
132 X509_check_purpose(x, -1, 0);
133
134 /* If cache is NULL, likely ENOMEM: return immediately */
135 if (policy_cache_set(x) == NULL)
136 return X509_PCY_TREE_INTERNAL;
137 }
138
139 /*
140 * At this point check for invalid policies and required explicit policy.
141 * Note that the explicit_policy counter is a count-down to zero, with the
142 * requirement kicking in if and once it does that. The counter is
143 * decremented for every non-self-issued certificate in the path, but may
144 * be further reduced by policy constraints in a non-leaf certificate.
145 *
146 * The ultimate policy set is the intersection of all the policies along
147 * the path, if we hit a certificate with an empty policy set, and explicit
148 * policy is required we're done.
149 */
150 for (i = n - 1;
151 i >= 0 && (explicit_policy > 0 || (ret & X509_PCY_TREE_EMPTY) == 0);
152 i--) {
153 X509 *x = sk_X509_value(certs, i);
154 uint32_t ex_flags = X509_get_extension_flags(x);
155
156 /* All the policies are already cached, we can return early */
157 if (ex_flags & EXFLAG_INVALID_POLICY)
158 return X509_PCY_TREE_INVALID;
159
160 /* Access the cache which we now know exists */
161 cache = policy_cache_set(x);
162
163 if ((ret & X509_PCY_TREE_VALID) && cache->data == NULL)
164 ret = X509_PCY_TREE_EMPTY;
165 if (explicit_policy > 0) {
166 if (!(ex_flags & EXFLAG_SI))
167 explicit_policy--;
168 if ((cache->explicit_skip >= 0)
169 && (cache->explicit_skip < explicit_policy))
170 explicit_policy = cache->explicit_skip;
171 }
172 }
173
174 if (explicit_policy == 0)
175 ret |= X509_PCY_TREE_EXPLICIT;
176 if ((ret & X509_PCY_TREE_VALID) == 0)
177 return ret;
178
179 /* If we get this far initialize the tree */
180 if ((tree = OPENSSL_zalloc(sizeof(*tree))) == NULL) {
181 X509V3err(X509V3_F_TREE_INIT, ERR_R_MALLOC_FAILURE);
182 return X509_PCY_TREE_INTERNAL;
183 }
184
185 /* Limit the growth of the tree to mitigate CVE-2023-0464 */
186 tree->node_maximum = OPENSSL_POLICY_TREE_NODES_MAX;
187
188 /*
189 * http://tools.ietf.org/html/rfc5280#section-6.1.2, figure 3.
190 *
191 * The top level is implicitly for the trust anchor with valid expected
192 * policies of anyPolicy. (RFC 5280 has the TA at depth 0 and the leaf at
193 * depth n, we have the leaf at depth 0 and the TA at depth n).
194 */
195 if ((tree->levels = OPENSSL_zalloc(sizeof(*tree->levels)*(n+1))) == NULL) {
196 OPENSSL_free(tree);
197 X509V3err(X509V3_F_TREE_INIT, ERR_R_MALLOC_FAILURE);
198 return X509_PCY_TREE_INTERNAL;
199 }
200 tree->nlevel = n+1;
201 level = tree->levels;
202 if ((data = policy_data_new(NULL, OBJ_nid2obj(NID_any_policy), 0)) == NULL)
203 goto bad_tree;
204 if (level_add_node(level, data, NULL, tree, 1) == NULL) {
205 policy_data_free(data);
206 goto bad_tree;
207 }
208
209 /*
210 * In this pass initialize all the tree levels and whether anyPolicy and
211 * policy mapping are inhibited at each level.
212 */
213 for (i = n - 1; i >= 0; i--) {
214 X509 *x = sk_X509_value(certs, i);
215 uint32_t ex_flags = X509_get_extension_flags(x);
216
217 /* Access the cache which we now know exists */
218 cache = policy_cache_set(x);
219
220 X509_up_ref(x);
221 (++level)->cert = x;
222
223 if (!cache->anyPolicy)
224 level->flags |= X509_V_FLAG_INHIBIT_ANY;
225
226 /* Determine inhibit any and inhibit map flags */
227 if (any_skip == 0) {
228 /*
229 * Any matching allowed only if certificate is self issued and not
230 * the last in the chain.
231 */
232 if (!(ex_flags & EXFLAG_SI) || (i == 0))
233 level->flags |= X509_V_FLAG_INHIBIT_ANY;
234 } else {
235 if (!(ex_flags & EXFLAG_SI))
236 any_skip--;
237 if ((cache->any_skip >= 0) && (cache->any_skip < any_skip))
238 any_skip = cache->any_skip;
239 }
240
241 if (map_skip == 0)
242 level->flags |= X509_V_FLAG_INHIBIT_MAP;
243 else {
244 if (!(ex_flags & EXFLAG_SI))
245 map_skip--;
246 if ((cache->map_skip >= 0) && (cache->map_skip < map_skip))
247 map_skip = cache->map_skip;
248 }
249 }
250
251 *ptree = tree;
252 return ret;
253
254 bad_tree:
255 X509_policy_tree_free(tree);
256 return X509_PCY_TREE_INTERNAL;
257 }
258
259 /*
260 * Return value: 1 on success, 0 otherwise
261 */
tree_link_matching_nodes(X509_POLICY_LEVEL * curr,X509_POLICY_DATA * data,X509_POLICY_TREE * tree)262 static int tree_link_matching_nodes(X509_POLICY_LEVEL *curr,
263 X509_POLICY_DATA *data,
264 X509_POLICY_TREE *tree)
265 {
266 X509_POLICY_LEVEL *last = curr - 1;
267 int i, matched = 0;
268
269 /* Iterate through all in nodes linking matches */
270 for (i = 0; i < sk_X509_POLICY_NODE_num(last->nodes); i++) {
271 X509_POLICY_NODE *node = sk_X509_POLICY_NODE_value(last->nodes, i);
272
273 if (policy_node_match(last, node, data->valid_policy)) {
274 if (level_add_node(curr, data, node, tree, 0) == NULL)
275 return 0;
276 matched = 1;
277 }
278 }
279 if (!matched && last->anyPolicy) {
280 if (level_add_node(curr, data, last->anyPolicy, tree, 0) == NULL)
281 return 0;
282 }
283 return 1;
284 }
285
286 /*
287 * This corresponds to RFC3280 6.1.3(d)(1): link any data from
288 * CertificatePolicies onto matching parent or anyPolicy if no match.
289 *
290 * Return value: 1 on success, 0 otherwise.
291 */
tree_link_nodes(X509_POLICY_LEVEL * curr,const X509_POLICY_CACHE * cache,X509_POLICY_TREE * tree)292 static int tree_link_nodes(X509_POLICY_LEVEL *curr,
293 const X509_POLICY_CACHE *cache,
294 X509_POLICY_TREE *tree)
295 {
296 int i;
297
298 for (i = 0; i < sk_X509_POLICY_DATA_num(cache->data); i++) {
299 X509_POLICY_DATA *data = sk_X509_POLICY_DATA_value(cache->data, i);
300
301 /* Look for matching nodes in previous level */
302 if (!tree_link_matching_nodes(curr, data, tree))
303 return 0;
304 }
305 return 1;
306 }
307
308 /*
309 * This corresponds to RFC3280 6.1.3(d)(2): Create new data for any unmatched
310 * policies in the parent and link to anyPolicy.
311 *
312 * Return value: 1 on success, 0 otherwise.
313 */
tree_add_unmatched(X509_POLICY_LEVEL * curr,const X509_POLICY_CACHE * cache,const ASN1_OBJECT * id,X509_POLICY_NODE * node,X509_POLICY_TREE * tree)314 static int tree_add_unmatched(X509_POLICY_LEVEL *curr,
315 const X509_POLICY_CACHE *cache,
316 const ASN1_OBJECT *id,
317 X509_POLICY_NODE *node, X509_POLICY_TREE *tree)
318 {
319 X509_POLICY_DATA *data;
320
321 if (id == NULL)
322 id = node->data->valid_policy;
323 /*
324 * Create a new node with qualifiers from anyPolicy and id from unmatched
325 * node.
326 */
327 if ((data = policy_data_new(NULL, id, node_critical(node))) == NULL)
328 return 0;
329
330 /* Curr may not have anyPolicy */
331 data->qualifier_set = cache->anyPolicy->qualifier_set;
332 data->flags |= POLICY_DATA_FLAG_SHARED_QUALIFIERS;
333 if (level_add_node(curr, data, node, tree, 1) == NULL) {
334 policy_data_free(data);
335 return 0;
336 }
337 return 1;
338 }
339
340 /*
341 * Return value: 1 on success, 0 otherwise.
342 */
tree_link_unmatched(X509_POLICY_LEVEL * curr,const X509_POLICY_CACHE * cache,X509_POLICY_NODE * node,X509_POLICY_TREE * tree)343 static int tree_link_unmatched(X509_POLICY_LEVEL *curr,
344 const X509_POLICY_CACHE *cache,
345 X509_POLICY_NODE *node, X509_POLICY_TREE *tree)
346 {
347 const X509_POLICY_LEVEL *last = curr - 1;
348 int i;
349
350 if ((last->flags & X509_V_FLAG_INHIBIT_MAP)
351 || !(node->data->flags & POLICY_DATA_FLAG_MAPPED)) {
352 /* If no policy mapping: matched if one child present */
353 if (node->nchild)
354 return 1;
355 if (!tree_add_unmatched(curr, cache, NULL, node, tree))
356 return 0;
357 /* Add it */
358 } else {
359 /* If mapping: matched if one child per expected policy set */
360 STACK_OF(ASN1_OBJECT) *expset = node->data->expected_policy_set;
361 if (node->nchild == sk_ASN1_OBJECT_num(expset))
362 return 1;
363 /* Locate unmatched nodes */
364 for (i = 0; i < sk_ASN1_OBJECT_num(expset); i++) {
365 ASN1_OBJECT *oid = sk_ASN1_OBJECT_value(expset, i);
366 if (level_find_node(curr, node, oid))
367 continue;
368 if (!tree_add_unmatched(curr, cache, oid, node, tree))
369 return 0;
370 }
371
372 }
373 return 1;
374 }
375
376 /*
377 * Return value: 1 on success, 0 otherwise
378 */
tree_link_any(X509_POLICY_LEVEL * curr,const X509_POLICY_CACHE * cache,X509_POLICY_TREE * tree)379 static int tree_link_any(X509_POLICY_LEVEL *curr,
380 const X509_POLICY_CACHE *cache,
381 X509_POLICY_TREE *tree)
382 {
383 int i;
384 X509_POLICY_NODE *node;
385 X509_POLICY_LEVEL *last = curr - 1;
386
387 for (i = 0; i < sk_X509_POLICY_NODE_num(last->nodes); i++) {
388 node = sk_X509_POLICY_NODE_value(last->nodes, i);
389
390 if (!tree_link_unmatched(curr, cache, node, tree))
391 return 0;
392 }
393 /* Finally add link to anyPolicy */
394 if (last->anyPolicy &&
395 level_add_node(curr, cache->anyPolicy, last->anyPolicy, tree, 0) == NULL)
396 return 0;
397 return 1;
398 }
399
400 /*-
401 * Prune the tree: delete any child mapped child data on the current level then
402 * proceed up the tree deleting any data with no children. If we ever have no
403 * data on a level we can halt because the tree will be empty.
404 *
405 * Return value: <= 0 error, otherwise one of:
406 *
407 * X509_PCY_TREE_VALID: valid tree
408 * X509_PCY_TREE_EMPTY: empty tree
409 */
tree_prune(X509_POLICY_TREE * tree,X509_POLICY_LEVEL * curr)410 static int tree_prune(X509_POLICY_TREE *tree, X509_POLICY_LEVEL *curr)
411 {
412 STACK_OF(X509_POLICY_NODE) *nodes;
413 X509_POLICY_NODE *node;
414 int i;
415 nodes = curr->nodes;
416 if (curr->flags & X509_V_FLAG_INHIBIT_MAP) {
417 for (i = sk_X509_POLICY_NODE_num(nodes) - 1; i >= 0; i--) {
418 node = sk_X509_POLICY_NODE_value(nodes, i);
419 /* Delete any mapped data: see RFC3280 XXXX */
420 if (node->data->flags & POLICY_DATA_FLAG_MAP_MASK) {
421 node->parent->nchild--;
422 OPENSSL_free(node);
423 (void)sk_X509_POLICY_NODE_delete(nodes, i);
424 }
425 }
426 }
427
428 for (;;) {
429 --curr;
430 nodes = curr->nodes;
431 for (i = sk_X509_POLICY_NODE_num(nodes) - 1; i >= 0; i--) {
432 node = sk_X509_POLICY_NODE_value(nodes, i);
433 if (node->nchild == 0) {
434 node->parent->nchild--;
435 OPENSSL_free(node);
436 (void)sk_X509_POLICY_NODE_delete(nodes, i);
437 }
438 }
439 if (curr->anyPolicy && !curr->anyPolicy->nchild) {
440 if (curr->anyPolicy->parent)
441 curr->anyPolicy->parent->nchild--;
442 OPENSSL_free(curr->anyPolicy);
443 curr->anyPolicy = NULL;
444 }
445 if (curr == tree->levels) {
446 /* If we zapped anyPolicy at top then tree is empty */
447 if (!curr->anyPolicy)
448 return X509_PCY_TREE_EMPTY;
449 break;
450 }
451 }
452 return X509_PCY_TREE_VALID;
453 }
454
455 /*
456 * Return value: 1 on success, 0 otherwise.
457 */
tree_add_auth_node(STACK_OF (X509_POLICY_NODE)** pnodes,X509_POLICY_NODE * pcy)458 static int tree_add_auth_node(STACK_OF(X509_POLICY_NODE) **pnodes,
459 X509_POLICY_NODE *pcy)
460 {
461 if (*pnodes == NULL &&
462 (*pnodes = policy_node_cmp_new()) == NULL)
463 return 0;
464 if (sk_X509_POLICY_NODE_find(*pnodes, pcy) >= 0)
465 return 1;
466 return sk_X509_POLICY_NODE_push(*pnodes, pcy) != 0;
467 }
468
469 #define TREE_CALC_FAILURE 0
470 #define TREE_CALC_OK_NOFREE 1
471 #define TREE_CALC_OK_DOFREE 2
472
473 /*-
474 * Calculate the authority set based on policy tree. The 'pnodes' parameter is
475 * used as a store for the set of policy nodes used to calculate the user set.
476 * If the authority set is not anyPolicy then pnodes will just point to the
477 * authority set. If however the authority set is anyPolicy then the set of
478 * valid policies (other than anyPolicy) is store in pnodes.
479 *
480 * Return value:
481 * TREE_CALC_FAILURE on failure,
482 * TREE_CALC_OK_NOFREE on success and pnodes need not be freed,
483 * TREE_CALC_OK_DOFREE on success and pnodes needs to be freed
484 */
tree_calculate_authority_set(X509_POLICY_TREE * tree,STACK_OF (X509_POLICY_NODE)** pnodes)485 static int tree_calculate_authority_set(X509_POLICY_TREE *tree,
486 STACK_OF(X509_POLICY_NODE) **pnodes)
487 {
488 X509_POLICY_LEVEL *curr;
489 X509_POLICY_NODE *node, *anyptr;
490 STACK_OF(X509_POLICY_NODE) **addnodes;
491 int i, j;
492 curr = tree->levels + tree->nlevel - 1;
493
494 /* If last level contains anyPolicy set is anyPolicy */
495 if (curr->anyPolicy) {
496 if (!tree_add_auth_node(&tree->auth_policies, curr->anyPolicy))
497 return TREE_CALC_FAILURE;
498 addnodes = pnodes;
499 } else
500 /* Add policies to authority set */
501 addnodes = &tree->auth_policies;
502
503 curr = tree->levels;
504 for (i = 1; i < tree->nlevel; i++) {
505 /*
506 * If no anyPolicy node on this this level it can't appear on lower
507 * levels so end search.
508 */
509 if ((anyptr = curr->anyPolicy) == NULL)
510 break;
511 curr++;
512 for (j = 0; j < sk_X509_POLICY_NODE_num(curr->nodes); j++) {
513 node = sk_X509_POLICY_NODE_value(curr->nodes, j);
514 if ((node->parent == anyptr)
515 && !tree_add_auth_node(addnodes, node)) {
516 if (addnodes == pnodes) {
517 sk_X509_POLICY_NODE_free(*pnodes);
518 *pnodes = NULL;
519 }
520 return TREE_CALC_FAILURE;
521 }
522 }
523 }
524 if (addnodes == pnodes)
525 return TREE_CALC_OK_DOFREE;
526
527 *pnodes = tree->auth_policies;
528 return TREE_CALC_OK_NOFREE;
529 }
530
531 /*
532 * Return value: 1 on success, 0 otherwise.
533 */
tree_calculate_user_set(X509_POLICY_TREE * tree,STACK_OF (ASN1_OBJECT)* policy_oids,STACK_OF (X509_POLICY_NODE)* auth_nodes)534 static int tree_calculate_user_set(X509_POLICY_TREE *tree,
535 STACK_OF(ASN1_OBJECT) *policy_oids,
536 STACK_OF(X509_POLICY_NODE) *auth_nodes)
537 {
538 int i;
539 X509_POLICY_NODE *node;
540 ASN1_OBJECT *oid;
541 X509_POLICY_NODE *anyPolicy;
542 X509_POLICY_DATA *extra;
543
544 /*
545 * Check if anyPolicy present in authority constrained policy set: this
546 * will happen if it is a leaf node.
547 */
548 if (sk_ASN1_OBJECT_num(policy_oids) <= 0)
549 return 1;
550
551 anyPolicy = tree->levels[tree->nlevel - 1].anyPolicy;
552
553 for (i = 0; i < sk_ASN1_OBJECT_num(policy_oids); i++) {
554 oid = sk_ASN1_OBJECT_value(policy_oids, i);
555 if (OBJ_obj2nid(oid) == NID_any_policy) {
556 tree->flags |= POLICY_FLAG_ANY_POLICY;
557 return 1;
558 }
559 }
560
561 for (i = 0; i < sk_ASN1_OBJECT_num(policy_oids); i++) {
562 oid = sk_ASN1_OBJECT_value(policy_oids, i);
563 node = tree_find_sk(auth_nodes, oid);
564 if (!node) {
565 if (!anyPolicy)
566 continue;
567 /*
568 * Create a new node with policy ID from user set and qualifiers
569 * from anyPolicy.
570 */
571 extra = policy_data_new(NULL, oid, node_critical(anyPolicy));
572 if (extra == NULL)
573 return 0;
574 extra->qualifier_set = anyPolicy->data->qualifier_set;
575 extra->flags = POLICY_DATA_FLAG_SHARED_QUALIFIERS
576 | POLICY_DATA_FLAG_EXTRA_NODE;
577 node = level_add_node(NULL, extra, anyPolicy->parent,
578 tree, 1);
579 if (node == NULL) {
580 policy_data_free(extra);
581 return 0;
582 }
583 }
584 if (!tree->user_policies) {
585 tree->user_policies = sk_X509_POLICY_NODE_new_null();
586 if (!tree->user_policies) {
587 exnode_free(node);
588 return 0;
589 }
590 }
591 if (!sk_X509_POLICY_NODE_push(tree->user_policies, node)) {
592 exnode_free(node);
593 return 0;
594 }
595 }
596 return 1;
597 }
598
599 /*-
600 * Return value: <= 0 error, otherwise one of:
601 * X509_PCY_TREE_VALID: valid tree
602 * X509_PCY_TREE_EMPTY: empty tree
603 * (see tree_prune()).
604 */
tree_evaluate(X509_POLICY_TREE * tree)605 static int tree_evaluate(X509_POLICY_TREE *tree)
606 {
607 int ret, i;
608 X509_POLICY_LEVEL *curr = tree->levels + 1;
609 const X509_POLICY_CACHE *cache;
610
611 for (i = 1; i < tree->nlevel; i++, curr++) {
612 cache = policy_cache_set(curr->cert);
613 if (!tree_link_nodes(curr, cache, tree))
614 return X509_PCY_TREE_INTERNAL;
615
616 if (!(curr->flags & X509_V_FLAG_INHIBIT_ANY)
617 && !tree_link_any(curr, cache, tree))
618 return X509_PCY_TREE_INTERNAL;
619 #ifdef OPENSSL_POLICY_DEBUG
620 tree_print("before tree_prune()", tree, curr);
621 #endif
622 ret = tree_prune(tree, curr);
623 if (ret != X509_PCY_TREE_VALID)
624 return ret;
625 }
626 return X509_PCY_TREE_VALID;
627 }
628
exnode_free(X509_POLICY_NODE * node)629 static void exnode_free(X509_POLICY_NODE *node)
630 {
631 if (node->data && (node->data->flags & POLICY_DATA_FLAG_EXTRA_NODE))
632 OPENSSL_free(node);
633 }
634
X509_policy_tree_free(X509_POLICY_TREE * tree)635 void X509_policy_tree_free(X509_POLICY_TREE *tree)
636 {
637 X509_POLICY_LEVEL *curr;
638 int i;
639
640 if (!tree)
641 return;
642
643 sk_X509_POLICY_NODE_free(tree->auth_policies);
644 sk_X509_POLICY_NODE_pop_free(tree->user_policies, exnode_free);
645
646 for (i = 0, curr = tree->levels; i < tree->nlevel; i++, curr++) {
647 X509_free(curr->cert);
648 sk_X509_POLICY_NODE_pop_free(curr->nodes, policy_node_free);
649 policy_node_free(curr->anyPolicy);
650 }
651
652 sk_X509_POLICY_DATA_pop_free(tree->extra_data, policy_data_free);
653 OPENSSL_free(tree->levels);
654 OPENSSL_free(tree);
655
656 }
657
658 /*-
659 * Application policy checking function.
660 * Return codes:
661 * X509_PCY_TREE_FAILURE: Failure to satisfy explicit policy
662 * X509_PCY_TREE_INVALID: Inconsistent or invalid extensions
663 * X509_PCY_TREE_INTERNAL: Internal error, most likely malloc
664 * X509_PCY_TREE_VALID: Success (null tree if empty or bare TA)
665 */
X509_policy_check(X509_POLICY_TREE ** ptree,int * pexplicit_policy,STACK_OF (X509)* certs,STACK_OF (ASN1_OBJECT)* policy_oids,unsigned int flags)666 int X509_policy_check(X509_POLICY_TREE **ptree, int *pexplicit_policy,
667 STACK_OF(X509) *certs,
668 STACK_OF(ASN1_OBJECT) *policy_oids, unsigned int flags)
669 {
670 int init_ret;
671 int ret;
672 int calc_ret;
673 X509_POLICY_TREE *tree = NULL;
674 STACK_OF(X509_POLICY_NODE) *nodes, *auth_nodes = NULL;
675
676 *ptree = NULL;
677 *pexplicit_policy = 0;
678 init_ret = tree_init(&tree, certs, flags);
679
680 if (init_ret <= 0)
681 return init_ret;
682
683 if ((init_ret & X509_PCY_TREE_EXPLICIT) == 0) {
684 if (init_ret & X509_PCY_TREE_EMPTY) {
685 X509_policy_tree_free(tree);
686 return X509_PCY_TREE_VALID;
687 }
688 } else {
689 *pexplicit_policy = 1;
690 /* Tree empty and requireExplicit True: Error */
691 if (init_ret & X509_PCY_TREE_EMPTY)
692 return X509_PCY_TREE_FAILURE;
693 }
694
695 ret = tree_evaluate(tree);
696 #ifdef OPENSSL_POLICY_DEBUG
697 tree_print("tree_evaluate()", tree, NULL);
698 #endif
699 if (ret <= 0)
700 goto error;
701
702 if (ret == X509_PCY_TREE_EMPTY) {
703 X509_policy_tree_free(tree);
704 if (init_ret & X509_PCY_TREE_EXPLICIT)
705 return X509_PCY_TREE_FAILURE;
706 return X509_PCY_TREE_VALID;
707 }
708
709 /* Tree is not empty: continue */
710
711 if ((calc_ret = tree_calculate_authority_set(tree, &auth_nodes)) == 0)
712 goto error;
713 ret = tree_calculate_user_set(tree, policy_oids, auth_nodes);
714 if (calc_ret == TREE_CALC_OK_DOFREE)
715 sk_X509_POLICY_NODE_free(auth_nodes);
716 if (!ret)
717 goto error;
718
719 *ptree = tree;
720
721 if (init_ret & X509_PCY_TREE_EXPLICIT) {
722 nodes = X509_policy_tree_get0_user_policies(tree);
723 if (sk_X509_POLICY_NODE_num(nodes) <= 0)
724 return X509_PCY_TREE_FAILURE;
725 }
726 return X509_PCY_TREE_VALID;
727
728 error:
729 X509_policy_tree_free(tree);
730 return X509_PCY_TREE_INTERNAL;
731 }
732