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