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