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