1 /*
2 * Copyright 2004-2024 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 X509_up_ref(x);
215 (++level)->cert = x;
216
217 if (!cache->anyPolicy)
218 level->flags |= X509_V_FLAG_INHIBIT_ANY;
219
220 /* Determine inhibit any and inhibit map flags */
221 if (any_skip == 0) {
222 /*
223 * Any matching allowed only if certificate is self issued and not
224 * the last in the chain.
225 */
226 if (!(ex_flags & EXFLAG_SI) || (i == 0))
227 level->flags |= X509_V_FLAG_INHIBIT_ANY;
228 } else {
229 if (!(ex_flags & EXFLAG_SI))
230 any_skip--;
231 if ((cache->any_skip >= 0) && (cache->any_skip < any_skip))
232 any_skip = cache->any_skip;
233 }
234
235 if (map_skip == 0)
236 level->flags |= X509_V_FLAG_INHIBIT_MAP;
237 else {
238 if (!(ex_flags & EXFLAG_SI))
239 map_skip--;
240 if ((cache->map_skip >= 0) && (cache->map_skip < map_skip))
241 map_skip = cache->map_skip;
242 }
243 }
244
245 *ptree = tree;
246 return ret;
247
248 bad_tree:
249 X509_policy_tree_free(tree);
250 return X509_PCY_TREE_INTERNAL;
251 }
252
253 /*
254 * Return value: 1 on success, 0 otherwise
255 */
tree_link_matching_nodes(X509_POLICY_LEVEL * curr,X509_POLICY_DATA * data,X509_POLICY_TREE * tree)256 static int tree_link_matching_nodes(X509_POLICY_LEVEL *curr,
257 X509_POLICY_DATA *data,
258 X509_POLICY_TREE *tree)
259 {
260 X509_POLICY_LEVEL *last = curr - 1;
261 int i, matched = 0;
262
263 /* Iterate through all in nodes linking matches */
264 for (i = 0; i < sk_X509_POLICY_NODE_num(last->nodes); i++) {
265 X509_POLICY_NODE *node = sk_X509_POLICY_NODE_value(last->nodes, i);
266
267 if (ossl_policy_node_match(last, node, data->valid_policy)) {
268 if (ossl_policy_level_add_node(curr, data, node, tree, 0) == NULL)
269 return 0;
270 matched = 1;
271 }
272 }
273 if (!matched && last->anyPolicy) {
274 if (ossl_policy_level_add_node(curr, data, last->anyPolicy, tree, 0) == NULL)
275 return 0;
276 }
277 return 1;
278 }
279
280 /*
281 * This corresponds to RFC3280 6.1.3(d)(1): link any data from
282 * CertificatePolicies onto matching parent or anyPolicy if no match.
283 *
284 * Return value: 1 on success, 0 otherwise.
285 */
tree_link_nodes(X509_POLICY_LEVEL * curr,const X509_POLICY_CACHE * cache,X509_POLICY_TREE * tree)286 static int tree_link_nodes(X509_POLICY_LEVEL *curr,
287 const X509_POLICY_CACHE *cache,
288 X509_POLICY_TREE *tree)
289 {
290 int i;
291
292 for (i = 0; i < sk_X509_POLICY_DATA_num(cache->data); i++) {
293 X509_POLICY_DATA *data = sk_X509_POLICY_DATA_value(cache->data, i);
294
295 /* Look for matching nodes in previous level */
296 if (!tree_link_matching_nodes(curr, data, tree))
297 return 0;
298 }
299 return 1;
300 }
301
302 /*
303 * This corresponds to RFC3280 6.1.3(d)(2): Create new data for any unmatched
304 * policies in the parent and link to anyPolicy.
305 *
306 * Return value: 1 on success, 0 otherwise.
307 */
tree_add_unmatched(X509_POLICY_LEVEL * curr,const X509_POLICY_CACHE * cache,const ASN1_OBJECT * id,X509_POLICY_NODE * node,X509_POLICY_TREE * tree)308 static int tree_add_unmatched(X509_POLICY_LEVEL *curr,
309 const X509_POLICY_CACHE *cache,
310 const ASN1_OBJECT *id,
311 X509_POLICY_NODE *node, X509_POLICY_TREE *tree)
312 {
313 X509_POLICY_DATA *data;
314
315 if (id == NULL)
316 id = node->data->valid_policy;
317 /*
318 * Create a new node with qualifiers from anyPolicy and id from unmatched
319 * node.
320 */
321 if ((data = ossl_policy_data_new(NULL, id, node_critical(node))) == NULL)
322 return 0;
323
324 /* Curr may not have anyPolicy */
325 data->qualifier_set = cache->anyPolicy->qualifier_set;
326 data->flags |= POLICY_DATA_FLAG_SHARED_QUALIFIERS;
327 if (ossl_policy_level_add_node(curr, data, node, tree, 1) == NULL) {
328 ossl_policy_data_free(data);
329 return 0;
330 }
331 return 1;
332 }
333
334 /*
335 * Return value: 1 on success, 0 otherwise.
336 */
tree_link_unmatched(X509_POLICY_LEVEL * curr,const X509_POLICY_CACHE * cache,X509_POLICY_NODE * node,X509_POLICY_TREE * tree)337 static int tree_link_unmatched(X509_POLICY_LEVEL *curr,
338 const X509_POLICY_CACHE *cache,
339 X509_POLICY_NODE *node, X509_POLICY_TREE *tree)
340 {
341 const X509_POLICY_LEVEL *last = curr - 1;
342 int i;
343
344 if ((last->flags & X509_V_FLAG_INHIBIT_MAP)
345 || !(node->data->flags & POLICY_DATA_FLAG_MAPPED)) {
346 /* If no policy mapping: matched if one child present */
347 if (node->nchild)
348 return 1;
349 if (!tree_add_unmatched(curr, cache, NULL, node, tree))
350 return 0;
351 /* Add it */
352 } else {
353 /* If mapping: matched if one child per expected policy set */
354 STACK_OF(ASN1_OBJECT) *expset = node->data->expected_policy_set;
355 if (node->nchild == sk_ASN1_OBJECT_num(expset))
356 return 1;
357 /* Locate unmatched nodes */
358 for (i = 0; i < sk_ASN1_OBJECT_num(expset); i++) {
359 ASN1_OBJECT *oid = sk_ASN1_OBJECT_value(expset, i);
360 if (ossl_policy_level_find_node(curr, node, oid))
361 continue;
362 if (!tree_add_unmatched(curr, cache, oid, node, tree))
363 return 0;
364 }
365
366 }
367 return 1;
368 }
369
370 /*
371 * Return value: 1 on success, 0 otherwise
372 */
tree_link_any(X509_POLICY_LEVEL * curr,const X509_POLICY_CACHE * cache,X509_POLICY_TREE * tree)373 static int tree_link_any(X509_POLICY_LEVEL *curr,
374 const X509_POLICY_CACHE *cache,
375 X509_POLICY_TREE *tree)
376 {
377 int i;
378 X509_POLICY_NODE *node;
379 X509_POLICY_LEVEL *last = curr - 1;
380
381 for (i = 0; i < sk_X509_POLICY_NODE_num(last->nodes); i++) {
382 node = sk_X509_POLICY_NODE_value(last->nodes, i);
383
384 if (!tree_link_unmatched(curr, cache, node, tree))
385 return 0;
386 }
387 /* Finally add link to anyPolicy */
388 if (last->anyPolicy &&
389 ossl_policy_level_add_node(curr, cache->anyPolicy,
390 last->anyPolicy, tree, 0) == NULL)
391 return 0;
392 return 1;
393 }
394
395 /*-
396 * Prune the tree: delete any child mapped child data on the current level then
397 * proceed up the tree deleting any data with no children. If we ever have no
398 * data on a level we can halt because the tree will be empty.
399 *
400 * Return value: <= 0 error, otherwise one of:
401 *
402 * X509_PCY_TREE_VALID: valid tree
403 * X509_PCY_TREE_EMPTY: empty tree
404 */
tree_prune(X509_POLICY_TREE * tree,X509_POLICY_LEVEL * curr)405 static int tree_prune(X509_POLICY_TREE *tree, X509_POLICY_LEVEL *curr)
406 {
407 STACK_OF(X509_POLICY_NODE) *nodes;
408 X509_POLICY_NODE *node;
409 int i;
410 nodes = curr->nodes;
411 if (curr->flags & X509_V_FLAG_INHIBIT_MAP) {
412 for (i = sk_X509_POLICY_NODE_num(nodes) - 1; i >= 0; i--) {
413 node = sk_X509_POLICY_NODE_value(nodes, i);
414 /* Delete any mapped data: see RFC3280 XXXX */
415 if (node->data->flags & POLICY_DATA_FLAG_MAP_MASK) {
416 node->parent->nchild--;
417 OPENSSL_free(node);
418 (void)sk_X509_POLICY_NODE_delete(nodes, i);
419 }
420 }
421 }
422
423 for (;;) {
424 --curr;
425 nodes = curr->nodes;
426 for (i = sk_X509_POLICY_NODE_num(nodes) - 1; i >= 0; i--) {
427 node = sk_X509_POLICY_NODE_value(nodes, i);
428 if (node->nchild == 0) {
429 node->parent->nchild--;
430 OPENSSL_free(node);
431 (void)sk_X509_POLICY_NODE_delete(nodes, i);
432 }
433 }
434 if (curr->anyPolicy && !curr->anyPolicy->nchild) {
435 if (curr->anyPolicy->parent)
436 curr->anyPolicy->parent->nchild--;
437 OPENSSL_free(curr->anyPolicy);
438 curr->anyPolicy = NULL;
439 }
440 if (curr == tree->levels) {
441 /* If we zapped anyPolicy at top then tree is empty */
442 if (!curr->anyPolicy)
443 return X509_PCY_TREE_EMPTY;
444 break;
445 }
446 }
447 return X509_PCY_TREE_VALID;
448 }
449
450 /*
451 * Return value: 1 on success, 0 otherwise.
452 */
tree_add_auth_node(STACK_OF (X509_POLICY_NODE)** pnodes,X509_POLICY_NODE * pcy)453 static int tree_add_auth_node(STACK_OF(X509_POLICY_NODE) **pnodes,
454 X509_POLICY_NODE *pcy)
455 {
456 if (*pnodes == NULL &&
457 (*pnodes = ossl_policy_node_cmp_new()) == NULL)
458 return 0;
459 if (sk_X509_POLICY_NODE_find(*pnodes, pcy) >= 0)
460 return 1;
461 return sk_X509_POLICY_NODE_push(*pnodes, pcy) != 0;
462 }
463
464 #define TREE_CALC_FAILURE 0
465 #define TREE_CALC_OK_NOFREE 1
466 #define TREE_CALC_OK_DOFREE 2
467
468 /*-
469 * Calculate the authority set based on policy tree. The 'pnodes' parameter is
470 * used as a store for the set of policy nodes used to calculate the user set.
471 * If the authority set is not anyPolicy then pnodes will just point to the
472 * authority set. If however the authority set is anyPolicy then the set of
473 * valid policies (other than anyPolicy) is store in pnodes.
474 *
475 * Return value:
476 * TREE_CALC_FAILURE on failure,
477 * TREE_CALC_OK_NOFREE on success and pnodes need not be freed,
478 * TREE_CALC_OK_DOFREE on success and pnodes needs to be freed
479 */
tree_calculate_authority_set(X509_POLICY_TREE * tree,STACK_OF (X509_POLICY_NODE)** pnodes)480 static int tree_calculate_authority_set(X509_POLICY_TREE *tree,
481 STACK_OF(X509_POLICY_NODE) **pnodes)
482 {
483 X509_POLICY_LEVEL *curr;
484 X509_POLICY_NODE *node, *anyptr;
485 STACK_OF(X509_POLICY_NODE) **addnodes;
486 int i, j;
487 curr = tree->levels + tree->nlevel - 1;
488
489 /* If last level contains anyPolicy set is anyPolicy */
490 if (curr->anyPolicy) {
491 if (!tree_add_auth_node(&tree->auth_policies, curr->anyPolicy))
492 return TREE_CALC_FAILURE;
493 addnodes = pnodes;
494 } else
495 /* Add policies to authority set */
496 addnodes = &tree->auth_policies;
497
498 curr = tree->levels;
499 for (i = 1; i < tree->nlevel; i++) {
500 /*
501 * If no anyPolicy node on this level it can't appear on lower
502 * levels so end search.
503 */
504 if ((anyptr = curr->anyPolicy) == NULL)
505 break;
506 curr++;
507 for (j = 0; j < sk_X509_POLICY_NODE_num(curr->nodes); j++) {
508 node = sk_X509_POLICY_NODE_value(curr->nodes, j);
509 if ((node->parent == anyptr)
510 && !tree_add_auth_node(addnodes, node)) {
511 if (addnodes == pnodes) {
512 sk_X509_POLICY_NODE_free(*pnodes);
513 *pnodes = NULL;
514 }
515 return TREE_CALC_FAILURE;
516 }
517 }
518 }
519 if (addnodes == pnodes)
520 return TREE_CALC_OK_DOFREE;
521
522 *pnodes = tree->auth_policies;
523 return TREE_CALC_OK_NOFREE;
524 }
525
526 /*
527 * Return value: 1 on success, 0 otherwise.
528 */
tree_calculate_user_set(X509_POLICY_TREE * tree,STACK_OF (ASN1_OBJECT)* policy_oids,STACK_OF (X509_POLICY_NODE)* auth_nodes)529 static int tree_calculate_user_set(X509_POLICY_TREE *tree,
530 STACK_OF(ASN1_OBJECT) *policy_oids,
531 STACK_OF(X509_POLICY_NODE) *auth_nodes)
532 {
533 int i;
534 X509_POLICY_NODE *node;
535 ASN1_OBJECT *oid;
536 X509_POLICY_NODE *anyPolicy;
537 X509_POLICY_DATA *extra;
538
539 /*
540 * Check if anyPolicy present in authority constrained policy set: this
541 * will happen if it is a leaf node.
542 */
543 if (sk_ASN1_OBJECT_num(policy_oids) <= 0)
544 return 1;
545
546 anyPolicy = tree->levels[tree->nlevel - 1].anyPolicy;
547
548 for (i = 0; i < sk_ASN1_OBJECT_num(policy_oids); i++) {
549 oid = sk_ASN1_OBJECT_value(policy_oids, i);
550 if (OBJ_obj2nid(oid) == NID_any_policy) {
551 tree->flags |= POLICY_FLAG_ANY_POLICY;
552 return 1;
553 }
554 }
555
556 for (i = 0; i < sk_ASN1_OBJECT_num(policy_oids); i++) {
557 oid = sk_ASN1_OBJECT_value(policy_oids, i);
558 node = ossl_policy_tree_find_sk(auth_nodes, oid);
559 if (!node) {
560 if (!anyPolicy)
561 continue;
562 /*
563 * Create a new node with policy ID from user set and qualifiers
564 * from anyPolicy.
565 */
566 extra = ossl_policy_data_new(NULL, oid, node_critical(anyPolicy));
567 if (extra == NULL)
568 return 0;
569 extra->qualifier_set = anyPolicy->data->qualifier_set;
570 extra->flags = POLICY_DATA_FLAG_SHARED_QUALIFIERS
571 | POLICY_DATA_FLAG_EXTRA_NODE;
572 node = ossl_policy_level_add_node(NULL, extra, anyPolicy->parent,
573 tree, 1);
574 if (node == NULL) {
575 ossl_policy_data_free(extra);
576 return 0;
577 }
578 }
579 if (!tree->user_policies) {
580 tree->user_policies = sk_X509_POLICY_NODE_new_null();
581 if (!tree->user_policies) {
582 exnode_free(node);
583 return 0;
584 }
585 }
586 if (!sk_X509_POLICY_NODE_push(tree->user_policies, node)) {
587 exnode_free(node);
588 return 0;
589 }
590 }
591 return 1;
592 }
593
594 /*-
595 * Return value: <= 0 error, otherwise one of:
596 * X509_PCY_TREE_VALID: valid tree
597 * X509_PCY_TREE_EMPTY: empty tree
598 * (see tree_prune()).
599 */
tree_evaluate(X509_POLICY_TREE * tree)600 static int tree_evaluate(X509_POLICY_TREE *tree)
601 {
602 int ret, i;
603 X509_POLICY_LEVEL *curr = tree->levels + 1;
604 const X509_POLICY_CACHE *cache;
605
606 for (i = 1; i < tree->nlevel; i++, curr++) {
607 cache = ossl_policy_cache_set(curr->cert);
608 if (!tree_link_nodes(curr, cache, tree))
609 return X509_PCY_TREE_INTERNAL;
610
611 if (!(curr->flags & X509_V_FLAG_INHIBIT_ANY)
612 && !tree_link_any(curr, cache, tree))
613 return X509_PCY_TREE_INTERNAL;
614 TREE_PRINT("before tree_prune()", tree, curr);
615 ret = tree_prune(tree, curr);
616 if (ret != X509_PCY_TREE_VALID)
617 return ret;
618 }
619 return X509_PCY_TREE_VALID;
620 }
621
exnode_free(X509_POLICY_NODE * node)622 static void exnode_free(X509_POLICY_NODE *node)
623 {
624 if (node->data && (node->data->flags & POLICY_DATA_FLAG_EXTRA_NODE))
625 OPENSSL_free(node);
626 }
627
X509_policy_tree_free(X509_POLICY_TREE * tree)628 void X509_policy_tree_free(X509_POLICY_TREE *tree)
629 {
630 X509_POLICY_LEVEL *curr;
631 int i;
632
633 if (!tree)
634 return;
635
636 sk_X509_POLICY_NODE_free(tree->auth_policies);
637 sk_X509_POLICY_NODE_pop_free(tree->user_policies, exnode_free);
638
639 for (i = 0, curr = tree->levels; i < tree->nlevel; i++, curr++) {
640 X509_free(curr->cert);
641 sk_X509_POLICY_NODE_pop_free(curr->nodes, ossl_policy_node_free);
642 ossl_policy_node_free(curr->anyPolicy);
643 }
644
645 sk_X509_POLICY_DATA_pop_free(tree->extra_data, ossl_policy_data_free);
646 OPENSSL_free(tree->levels);
647 OPENSSL_free(tree);
648
649 }
650
651 /*-
652 * Application policy checking function.
653 * Return codes:
654 * X509_PCY_TREE_FAILURE: Failure to satisfy explicit policy
655 * X509_PCY_TREE_INVALID: Inconsistent or invalid extensions
656 * X509_PCY_TREE_INTERNAL: Internal error, most likely malloc
657 * X509_PCY_TREE_VALID: Success (null tree if empty or bare TA)
658 */
X509_policy_check(X509_POLICY_TREE ** ptree,int * pexplicit_policy,STACK_OF (X509)* certs,STACK_OF (ASN1_OBJECT)* policy_oids,unsigned int flags)659 int X509_policy_check(X509_POLICY_TREE **ptree, int *pexplicit_policy,
660 STACK_OF(X509) *certs,
661 STACK_OF(ASN1_OBJECT) *policy_oids, unsigned int flags)
662 {
663 int init_ret;
664 int ret;
665 int calc_ret;
666 X509_POLICY_TREE *tree = NULL;
667 STACK_OF(X509_POLICY_NODE) *nodes, *auth_nodes = NULL;
668
669 *ptree = NULL;
670 *pexplicit_policy = 0;
671 init_ret = tree_init(&tree, certs, flags);
672
673 if (init_ret <= 0)
674 return init_ret;
675
676 if ((init_ret & X509_PCY_TREE_EXPLICIT) == 0) {
677 if (init_ret & X509_PCY_TREE_EMPTY) {
678 X509_policy_tree_free(tree);
679 return X509_PCY_TREE_VALID;
680 }
681 } else {
682 *pexplicit_policy = 1;
683 /* Tree empty and requireExplicit True: Error */
684 if (init_ret & X509_PCY_TREE_EMPTY)
685 return X509_PCY_TREE_FAILURE;
686 }
687
688 ret = tree_evaluate(tree);
689 TREE_PRINT("tree_evaluate()", tree, NULL);
690 if (ret <= 0)
691 goto error;
692
693 if (ret == X509_PCY_TREE_EMPTY) {
694 X509_policy_tree_free(tree);
695 if (init_ret & X509_PCY_TREE_EXPLICIT)
696 return X509_PCY_TREE_FAILURE;
697 return X509_PCY_TREE_VALID;
698 }
699
700 /* Tree is not empty: continue */
701
702 if ((calc_ret = tree_calculate_authority_set(tree, &auth_nodes)) == 0)
703 goto error;
704 sk_X509_POLICY_NODE_sort(auth_nodes);
705 ret = tree_calculate_user_set(tree, policy_oids, auth_nodes);
706 if (calc_ret == TREE_CALC_OK_DOFREE)
707 sk_X509_POLICY_NODE_free(auth_nodes);
708 if (!ret)
709 goto error;
710
711 *ptree = tree;
712
713 if (init_ret & X509_PCY_TREE_EXPLICIT) {
714 nodes = X509_policy_tree_get0_user_policies(tree);
715 if (sk_X509_POLICY_NODE_num(nodes) <= 0)
716 return X509_PCY_TREE_FAILURE;
717 }
718 return X509_PCY_TREE_VALID;
719
720 error:
721 X509_policy_tree_free(tree);
722 return X509_PCY_TREE_INTERNAL;
723 }
724