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