]>
Commit | Line | Data |
---|---|---|
4acc3e90 | 1 | /* pcy_tree.c */ |
0f113f3e MC |
2 | /* |
3 | * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project | |
4 | * 2004. | |
4acc3e90 DSH |
5 | */ |
6 | /* ==================================================================== | |
7 | * Copyright (c) 2004 The OpenSSL Project. All rights reserved. | |
8 | * | |
9 | * Redistribution and use in source and binary forms, with or without | |
10 | * modification, are permitted provided that the following conditions | |
11 | * are met: | |
12 | * | |
13 | * 1. Redistributions of source code must retain the above copyright | |
0f113f3e | 14 | * notice, this list of conditions and the following disclaimer. |
4acc3e90 DSH |
15 | * |
16 | * 2. Redistributions in binary form must reproduce the above copyright | |
17 | * notice, this list of conditions and the following disclaimer in | |
18 | * the documentation and/or other materials provided with the | |
19 | * distribution. | |
20 | * | |
21 | * 3. All advertising materials mentioning features or use of this | |
22 | * software must display the following acknowledgment: | |
23 | * "This product includes software developed by the OpenSSL Project | |
24 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" | |
25 | * | |
26 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | |
27 | * endorse or promote products derived from this software without | |
28 | * prior written permission. For written permission, please contact | |
29 | * licensing@OpenSSL.org. | |
30 | * | |
31 | * 5. Products derived from this software may not be called "OpenSSL" | |
32 | * nor may "OpenSSL" appear in their names without prior written | |
33 | * permission of the OpenSSL Project. | |
34 | * | |
35 | * 6. Redistributions of any form whatsoever must retain the following | |
36 | * acknowledgment: | |
37 | * "This product includes software developed by the OpenSSL Project | |
38 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" | |
39 | * | |
40 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | |
41 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
43 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | |
44 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
45 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
46 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
47 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
49 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
50 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
51 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
52 | * ==================================================================== | |
53 | * | |
54 | * This product includes cryptographic software written by Eric Young | |
55 | * (eay@cryptsoft.com). This product includes software written by Tim | |
56 | * Hudson (tjh@cryptsoft.com). | |
57 | * | |
58 | */ | |
59 | ||
b39fc560 | 60 | #include "internal/cryptlib.h" |
4acc3e90 DSH |
61 | #include <openssl/x509.h> |
62 | #include <openssl/x509v3.h> | |
63 | ||
64 | #include "pcy_int.h" | |
65 | ||
0f113f3e MC |
66 | /* |
67 | * Enable this to print out the complete policy tree at various point during | |
002e66c0 DSH |
68 | * evaluation. |
69 | */ | |
70 | ||
0f113f3e MC |
71 | /* |
72 | * #define OPENSSL_POLICY_DEBUG | |
73 | */ | |
002e66c0 DSH |
74 | |
75 | #ifdef OPENSSL_POLICY_DEBUG | |
76 | ||
77 | static void expected_print(BIO *err, X509_POLICY_LEVEL *lev, | |
0f113f3e MC |
78 | X509_POLICY_NODE *node, int indent) |
79 | { | |
80 | if ((lev->flags & X509_V_FLAG_INHIBIT_MAP) | |
81 | || !(node->data->flags & POLICY_DATA_FLAG_MAP_MASK)) | |
82 | BIO_puts(err, " Not Mapped\n"); | |
83 | else { | |
84 | int i; | |
85 | STACK_OF(ASN1_OBJECT) *pset = node->data->expected_policy_set; | |
86 | ASN1_OBJECT *oid; | |
87 | BIO_puts(err, " Expected: "); | |
88 | for (i = 0; i < sk_ASN1_OBJECT_num(pset); i++) { | |
89 | oid = sk_ASN1_OBJECT_value(pset, i); | |
90 | if (i) | |
91 | BIO_puts(err, ", "); | |
92 | i2a_ASN1_OBJECT(err, oid); | |
93 | } | |
94 | BIO_puts(err, "\n"); | |
95 | } | |
96 | } | |
002e66c0 DSH |
97 | |
98 | static void tree_print(char *str, X509_POLICY_TREE *tree, | |
0f113f3e MC |
99 | X509_POLICY_LEVEL *curr) |
100 | { | |
101 | X509_POLICY_LEVEL *plev; | |
102 | X509_POLICY_NODE *node; | |
103 | int i; | |
104 | BIO *err; | |
105 | err = BIO_new_fp(stderr, BIO_NOCLOSE); | |
106 | if (err == NULL) | |
107 | return; | |
108 | if (!curr) | |
109 | curr = tree->levels + tree->nlevel; | |
110 | else | |
111 | curr++; | |
112 | BIO_printf(err, "Level print after %s\n", str); | |
113 | BIO_printf(err, "Printing Up to Level %ld\n", curr - tree->levels); | |
114 | for (plev = tree->levels; plev != curr; plev++) { | |
115 | BIO_printf(err, "Level %ld, flags = %x\n", | |
116 | plev - tree->levels, plev->flags); | |
117 | for (i = 0; i < sk_X509_POLICY_NODE_num(plev->nodes); i++) { | |
118 | node = sk_X509_POLICY_NODE_value(plev->nodes, i); | |
119 | X509_POLICY_NODE_print(err, node, 2); | |
120 | expected_print(err, plev, node, 2); | |
121 | BIO_printf(err, " Flags: %x\n", node->data->flags); | |
122 | } | |
123 | if (plev->anyPolicy) | |
124 | X509_POLICY_NODE_print(err, plev->anyPolicy, 2); | |
125 | } | |
126 | ||
127 | BIO_free(err); | |
128 | ||
129 | } | |
002e66c0 DSH |
130 | #else |
131 | ||
0f113f3e | 132 | # define tree_print(a,b,c) /* */ |
002e66c0 DSH |
133 | |
134 | #endif | |
135 | ||
3a83462d MC |
136 | /*- |
137 | * Initialize policy tree. Return values: | |
478b50cf | 138 | * 0 Some internal error occurred. |
4acc3e90 DSH |
139 | * -1 Inconsistent or invalid extensions in certificates. |
140 | * 1 Tree initialized OK. | |
141 | * 2 Policy tree is empty. | |
142 | * 5 Tree OK and requireExplicitPolicy true. | |
143 | * 6 Tree empty and requireExplicitPolicy true. | |
144 | */ | |
145 | ||
146 | static int tree_init(X509_POLICY_TREE **ptree, STACK_OF(X509) *certs, | |
0f113f3e MC |
147 | unsigned int flags) |
148 | { | |
149 | X509_POLICY_TREE *tree; | |
150 | X509_POLICY_LEVEL *level; | |
151 | const X509_POLICY_CACHE *cache; | |
152 | X509_POLICY_DATA *data = NULL; | |
153 | X509 *x; | |
154 | int ret = 1; | |
155 | int i, n; | |
156 | int explicit_policy; | |
157 | int any_skip; | |
158 | int map_skip; | |
7aa0b022 | 159 | |
0f113f3e MC |
160 | *ptree = NULL; |
161 | n = sk_X509_num(certs); | |
4acc3e90 | 162 | |
0f113f3e MC |
163 | if (flags & X509_V_FLAG_EXPLICIT_POLICY) |
164 | explicit_policy = 0; | |
165 | else | |
166 | explicit_policy = n + 1; | |
167 | ||
168 | if (flags & X509_V_FLAG_INHIBIT_ANY) | |
169 | any_skip = 0; | |
170 | else | |
171 | any_skip = n + 1; | |
172 | ||
173 | if (flags & X509_V_FLAG_INHIBIT_MAP) | |
174 | map_skip = 0; | |
175 | else | |
176 | map_skip = n + 1; | |
177 | ||
178 | /* Can't do anything with just a trust anchor */ | |
179 | if (n == 1) | |
180 | return 1; | |
181 | /* | |
182 | * First setup policy cache in all certificates apart from the trust | |
183 | * anchor. Note any bad cache results on the way. Also can calculate | |
184 | * explicit_policy value at this point. | |
185 | */ | |
186 | for (i = n - 2; i >= 0; i--) { | |
a8d8e06b | 187 | uint32_t ex_flags; |
0f113f3e | 188 | x = sk_X509_value(certs, i); |
a8d8e06b | 189 | ex_flags = X509_get_extension_flags(x); |
0f113f3e MC |
190 | X509_check_purpose(x, -1, -1); |
191 | cache = policy_cache_set(x); | |
192 | /* If cache NULL something bad happened: return immediately */ | |
193 | if (cache == NULL) | |
194 | return 0; | |
195 | /* | |
196 | * If inconsistent extensions keep a note of it but continue | |
197 | */ | |
a8d8e06b | 198 | if (ex_flags & EXFLAG_INVALID_POLICY) |
0f113f3e MC |
199 | ret = -1; |
200 | /* | |
201 | * Otherwise if we have no data (hence no CertificatePolicies) and | |
202 | * haven't already set an inconsistent code note it. | |
203 | */ | |
204 | else if ((ret == 1) && !cache->data) | |
205 | ret = 2; | |
206 | if (explicit_policy > 0) { | |
a8d8e06b | 207 | if (!(ex_flags & EXFLAG_SI)) |
0f113f3e MC |
208 | explicit_policy--; |
209 | if ((cache->explicit_skip != -1) | |
210 | && (cache->explicit_skip < explicit_policy)) | |
211 | explicit_policy = cache->explicit_skip; | |
212 | } | |
213 | } | |
214 | ||
215 | if (ret != 1) { | |
216 | if (ret == 2 && !explicit_policy) | |
217 | return 6; | |
218 | return ret; | |
219 | } | |
220 | ||
221 | /* If we get this far initialize the tree */ | |
64b25758 | 222 | tree = OPENSSL_zalloc(sizeof(*tree)); |
90945fa3 | 223 | if (tree == NULL) |
0f113f3e | 224 | return 0; |
b51bce94 | 225 | tree->levels = OPENSSL_zalloc(sizeof(*tree->levels) * n); |
90945fa3 | 226 | if (tree->levels == NULL) { |
0f113f3e MC |
227 | OPENSSL_free(tree); |
228 | return 0; | |
229 | } | |
0f113f3e | 230 | tree->nlevel = n; |
0f113f3e MC |
231 | level = tree->levels; |
232 | ||
233 | /* Root data: initialize to anyPolicy */ | |
0f113f3e MC |
234 | data = policy_data_new(NULL, OBJ_nid2obj(NID_any_policy), 0); |
235 | ||
90945fa3 | 236 | if (data == NULL || !level_add_node(level, data, NULL, tree)) |
0f113f3e MC |
237 | goto bad_tree; |
238 | ||
239 | for (i = n - 2; i >= 0; i--) { | |
a8d8e06b | 240 | uint32_t ex_flags; |
0f113f3e MC |
241 | level++; |
242 | x = sk_X509_value(certs, i); | |
a8d8e06b | 243 | ex_flags = X509_get_extension_flags(x); |
0f113f3e | 244 | cache = policy_cache_set(x); |
05f0fb9f | 245 | X509_up_ref(x); |
0f113f3e MC |
246 | level->cert = x; |
247 | ||
248 | if (!cache->anyPolicy) | |
249 | level->flags |= X509_V_FLAG_INHIBIT_ANY; | |
250 | ||
251 | /* Determine inhibit any and inhibit map flags */ | |
252 | if (any_skip == 0) { | |
253 | /* | |
254 | * Any matching allowed if certificate is self issued and not the | |
255 | * last in the chain. | |
256 | */ | |
a8d8e06b | 257 | if (!(ex_flags & EXFLAG_SI) || (i == 0)) |
0f113f3e MC |
258 | level->flags |= X509_V_FLAG_INHIBIT_ANY; |
259 | } else { | |
a8d8e06b | 260 | if (!(ex_flags & EXFLAG_SI)) |
0f113f3e MC |
261 | any_skip--; |
262 | if ((cache->any_skip >= 0) | |
263 | && (cache->any_skip < any_skip)) | |
264 | any_skip = cache->any_skip; | |
265 | } | |
266 | ||
267 | if (map_skip == 0) | |
268 | level->flags |= X509_V_FLAG_INHIBIT_MAP; | |
269 | else { | |
a8d8e06b | 270 | if (!(ex_flags & EXFLAG_SI)) |
0f113f3e MC |
271 | map_skip--; |
272 | if ((cache->map_skip >= 0) | |
273 | && (cache->map_skip < map_skip)) | |
274 | map_skip = cache->map_skip; | |
275 | } | |
276 | ||
277 | } | |
278 | ||
279 | *ptree = tree; | |
280 | ||
281 | if (explicit_policy) | |
282 | return 1; | |
283 | else | |
284 | return 5; | |
285 | ||
286 | bad_tree: | |
287 | ||
288 | X509_policy_tree_free(tree); | |
289 | ||
290 | return 0; | |
291 | ||
292 | } | |
4acc3e90 | 293 | |
002e66c0 | 294 | static int tree_link_matching_nodes(X509_POLICY_LEVEL *curr, |
0f113f3e MC |
295 | const X509_POLICY_DATA *data) |
296 | { | |
297 | X509_POLICY_LEVEL *last = curr - 1; | |
298 | X509_POLICY_NODE *node; | |
299 | int i, matched = 0; | |
300 | /* Iterate through all in nodes linking matches */ | |
301 | for (i = 0; i < sk_X509_POLICY_NODE_num(last->nodes); i++) { | |
302 | node = sk_X509_POLICY_NODE_value(last->nodes, i); | |
303 | if (policy_node_match(last, node, data->valid_policy)) { | |
304 | if (!level_add_node(curr, data, node, NULL)) | |
305 | return 0; | |
306 | matched = 1; | |
307 | } | |
308 | } | |
309 | if (!matched && last->anyPolicy) { | |
310 | if (!level_add_node(curr, data, last->anyPolicy, NULL)) | |
311 | return 0; | |
312 | } | |
313 | return 1; | |
314 | } | |
315 | ||
316 | /* | |
317 | * This corresponds to RFC3280 6.1.3(d)(1): link any data from | |
318 | * CertificatePolicies onto matching parent or anyPolicy if no match. | |
4acc3e90 DSH |
319 | */ |
320 | ||
321 | static int tree_link_nodes(X509_POLICY_LEVEL *curr, | |
0f113f3e MC |
322 | const X509_POLICY_CACHE *cache) |
323 | { | |
324 | int i; | |
325 | X509_POLICY_DATA *data; | |
326 | ||
327 | for (i = 0; i < sk_X509_POLICY_DATA_num(cache->data); i++) { | |
328 | data = sk_X509_POLICY_DATA_value(cache->data, i); | |
0f113f3e MC |
329 | /* Look for matching nodes in previous level */ |
330 | if (!tree_link_matching_nodes(curr, data)) | |
331 | return 0; | |
332 | } | |
333 | return 1; | |
334 | } | |
335 | ||
336 | /* | |
337 | * This corresponds to RFC3280 6.1.3(d)(2): Create new data for any unmatched | |
338 | * policies in the parent and link to anyPolicy. | |
4acc3e90 DSH |
339 | */ |
340 | ||
002e66c0 | 341 | static int tree_add_unmatched(X509_POLICY_LEVEL *curr, |
0f113f3e MC |
342 | const X509_POLICY_CACHE *cache, |
343 | const ASN1_OBJECT *id, | |
344 | X509_POLICY_NODE *node, X509_POLICY_TREE *tree) | |
345 | { | |
346 | X509_POLICY_DATA *data; | |
347 | if (id == NULL) | |
348 | id = node->data->valid_policy; | |
349 | /* | |
350 | * Create a new node with qualifiers from anyPolicy and id from unmatched | |
351 | * node. | |
352 | */ | |
353 | data = policy_data_new(NULL, id, node_critical(node)); | |
354 | ||
355 | if (data == NULL) | |
356 | return 0; | |
357 | /* Curr may not have anyPolicy */ | |
358 | data->qualifier_set = cache->anyPolicy->qualifier_set; | |
359 | data->flags |= POLICY_DATA_FLAG_SHARED_QUALIFIERS; | |
360 | if (!level_add_node(curr, data, node, tree)) { | |
361 | policy_data_free(data); | |
362 | return 0; | |
363 | } | |
364 | ||
365 | return 1; | |
366 | } | |
002e66c0 DSH |
367 | |
368 | static int tree_link_unmatched(X509_POLICY_LEVEL *curr, | |
0f113f3e MC |
369 | const X509_POLICY_CACHE *cache, |
370 | X509_POLICY_NODE *node, X509_POLICY_TREE *tree) | |
371 | { | |
372 | const X509_POLICY_LEVEL *last = curr - 1; | |
373 | int i; | |
374 | ||
375 | if ((last->flags & X509_V_FLAG_INHIBIT_MAP) | |
376 | || !(node->data->flags & POLICY_DATA_FLAG_MAPPED)) { | |
377 | /* If no policy mapping: matched if one child present */ | |
378 | if (node->nchild) | |
379 | return 1; | |
380 | if (!tree_add_unmatched(curr, cache, NULL, node, tree)) | |
381 | return 0; | |
382 | /* Add it */ | |
383 | } else { | |
384 | /* If mapping: matched if one child per expected policy set */ | |
385 | STACK_OF(ASN1_OBJECT) *expset = node->data->expected_policy_set; | |
386 | if (node->nchild == sk_ASN1_OBJECT_num(expset)) | |
387 | return 1; | |
388 | /* Locate unmatched nodes */ | |
389 | for (i = 0; i < sk_ASN1_OBJECT_num(expset); i++) { | |
390 | ASN1_OBJECT *oid = sk_ASN1_OBJECT_value(expset, i); | |
391 | if (level_find_node(curr, node, oid)) | |
392 | continue; | |
393 | if (!tree_add_unmatched(curr, cache, oid, node, tree)) | |
394 | return 0; | |
395 | } | |
396 | ||
397 | } | |
398 | ||
399 | return 1; | |
400 | ||
401 | } | |
002e66c0 | 402 | |
4acc3e90 | 403 | static int tree_link_any(X509_POLICY_LEVEL *curr, |
0f113f3e MC |
404 | const X509_POLICY_CACHE *cache, |
405 | X509_POLICY_TREE *tree) | |
406 | { | |
407 | int i; | |
0f113f3e MC |
408 | X509_POLICY_NODE *node; |
409 | X509_POLICY_LEVEL *last = curr - 1; | |
410 | ||
411 | for (i = 0; i < sk_X509_POLICY_NODE_num(last->nodes); i++) { | |
412 | node = sk_X509_POLICY_NODE_value(last->nodes, i); | |
413 | ||
414 | if (!tree_link_unmatched(curr, cache, node, tree)) | |
415 | return 0; | |
0f113f3e MC |
416 | } |
417 | /* Finally add link to anyPolicy */ | |
418 | if (last->anyPolicy) { | |
419 | if (!level_add_node(curr, cache->anyPolicy, last->anyPolicy, NULL)) | |
420 | return 0; | |
421 | } | |
422 | return 1; | |
423 | } | |
424 | ||
425 | /* | |
426 | * Prune the tree: delete any child mapped child data on the current level | |
4acc3e90 DSH |
427 | * then proceed up the tree deleting any data with no children. If we ever |
428 | * have no data on a level we can halt because the tree will be empty. | |
429 | */ | |
430 | ||
431 | static int tree_prune(X509_POLICY_TREE *tree, X509_POLICY_LEVEL *curr) | |
0f113f3e MC |
432 | { |
433 | STACK_OF(X509_POLICY_NODE) *nodes; | |
434 | X509_POLICY_NODE *node; | |
435 | int i; | |
436 | nodes = curr->nodes; | |
437 | if (curr->flags & X509_V_FLAG_INHIBIT_MAP) { | |
438 | for (i = sk_X509_POLICY_NODE_num(nodes) - 1; i >= 0; i--) { | |
439 | node = sk_X509_POLICY_NODE_value(nodes, i); | |
440 | /* Delete any mapped data: see RFC3280 XXXX */ | |
441 | if (node->data->flags & POLICY_DATA_FLAG_MAP_MASK) { | |
442 | node->parent->nchild--; | |
443 | OPENSSL_free(node); | |
444 | (void)sk_X509_POLICY_NODE_delete(nodes, i); | |
445 | } | |
446 | } | |
447 | } | |
448 | ||
449 | for (;;) { | |
450 | --curr; | |
451 | nodes = curr->nodes; | |
452 | for (i = sk_X509_POLICY_NODE_num(nodes) - 1; i >= 0; i--) { | |
453 | node = sk_X509_POLICY_NODE_value(nodes, i); | |
454 | if (node->nchild == 0) { | |
455 | node->parent->nchild--; | |
456 | OPENSSL_free(node); | |
457 | (void)sk_X509_POLICY_NODE_delete(nodes, i); | |
458 | } | |
459 | } | |
460 | if (curr->anyPolicy && !curr->anyPolicy->nchild) { | |
461 | if (curr->anyPolicy->parent) | |
462 | curr->anyPolicy->parent->nchild--; | |
463 | OPENSSL_free(curr->anyPolicy); | |
464 | curr->anyPolicy = NULL; | |
465 | } | |
466 | if (curr == tree->levels) { | |
467 | /* If we zapped anyPolicy at top then tree is empty */ | |
468 | if (!curr->anyPolicy) | |
469 | return 2; | |
470 | return 1; | |
471 | } | |
472 | } | |
473 | ||
c6ef15c4 | 474 | /* Unreachable */ |
0f113f3e MC |
475 | |
476 | } | |
4acc3e90 DSH |
477 | |
478 | static int tree_add_auth_node(STACK_OF(X509_POLICY_NODE) **pnodes, | |
0f113f3e MC |
479 | X509_POLICY_NODE *pcy) |
480 | { | |
90945fa3 | 481 | if (*pnodes == NULL) { |
0f113f3e | 482 | *pnodes = policy_node_cmp_new(); |
90945fa3 | 483 | if (*pnodes == NULL) |
0f113f3e MC |
484 | return 0; |
485 | } else if (sk_X509_POLICY_NODE_find(*pnodes, pcy) != -1) | |
486 | return 1; | |
487 | ||
488 | if (!sk_X509_POLICY_NODE_push(*pnodes, pcy)) | |
489 | return 0; | |
490 | ||
491 | return 1; | |
492 | ||
493 | } | |
494 | ||
495 | /* | |
496 | * Calculate the authority set based on policy tree. The 'pnodes' parameter | |
497 | * is used as a store for the set of policy nodes used to calculate the user | |
498 | * set. If the authority set is not anyPolicy then pnodes will just point to | |
499 | * the authority set. If however the authority set is anyPolicy then the set | |
500 | * of valid policies (other than anyPolicy) is store in pnodes. The return | |
501 | * value of '2' is used in this case to indicate that pnodes should be freed. | |
4acc3e90 DSH |
502 | */ |
503 | ||
504 | static int tree_calculate_authority_set(X509_POLICY_TREE *tree, | |
0f113f3e MC |
505 | STACK_OF(X509_POLICY_NODE) **pnodes) |
506 | { | |
507 | X509_POLICY_LEVEL *curr; | |
508 | X509_POLICY_NODE *node, *anyptr; | |
509 | STACK_OF(X509_POLICY_NODE) **addnodes; | |
510 | int i, j; | |
511 | curr = tree->levels + tree->nlevel - 1; | |
512 | ||
513 | /* If last level contains anyPolicy set is anyPolicy */ | |
514 | if (curr->anyPolicy) { | |
515 | if (!tree_add_auth_node(&tree->auth_policies, curr->anyPolicy)) | |
516 | return 0; | |
517 | addnodes = pnodes; | |
518 | } else | |
519 | /* Add policies to authority set */ | |
520 | addnodes = &tree->auth_policies; | |
521 | ||
522 | curr = tree->levels; | |
523 | for (i = 1; i < tree->nlevel; i++) { | |
524 | /* | |
525 | * If no anyPolicy node on this this level it can't appear on lower | |
526 | * levels so end search. | |
527 | */ | |
75ebbd9a | 528 | if ((anyptr = curr->anyPolicy) == NULL) |
0f113f3e MC |
529 | break; |
530 | curr++; | |
531 | for (j = 0; j < sk_X509_POLICY_NODE_num(curr->nodes); j++) { | |
532 | node = sk_X509_POLICY_NODE_value(curr->nodes, j); | |
533 | if ((node->parent == anyptr) | |
534 | && !tree_add_auth_node(addnodes, node)) | |
535 | return 0; | |
536 | } | |
537 | } | |
538 | ||
539 | if (addnodes == pnodes) | |
540 | return 2; | |
541 | ||
542 | *pnodes = tree->auth_policies; | |
543 | ||
544 | return 1; | |
545 | } | |
4acc3e90 DSH |
546 | |
547 | static int tree_calculate_user_set(X509_POLICY_TREE *tree, | |
0f113f3e MC |
548 | STACK_OF(ASN1_OBJECT) *policy_oids, |
549 | STACK_OF(X509_POLICY_NODE) *auth_nodes) | |
550 | { | |
551 | int i; | |
552 | X509_POLICY_NODE *node; | |
553 | ASN1_OBJECT *oid; | |
554 | ||
555 | X509_POLICY_NODE *anyPolicy; | |
556 | X509_POLICY_DATA *extra; | |
557 | ||
558 | /* | |
559 | * Check if anyPolicy present in authority constrained policy set: this | |
560 | * will happen if it is a leaf node. | |
561 | */ | |
562 | ||
563 | if (sk_ASN1_OBJECT_num(policy_oids) <= 0) | |
564 | return 1; | |
565 | ||
566 | anyPolicy = tree->levels[tree->nlevel - 1].anyPolicy; | |
567 | ||
568 | for (i = 0; i < sk_ASN1_OBJECT_num(policy_oids); i++) { | |
569 | oid = sk_ASN1_OBJECT_value(policy_oids, i); | |
570 | if (OBJ_obj2nid(oid) == NID_any_policy) { | |
571 | tree->flags |= POLICY_FLAG_ANY_POLICY; | |
572 | return 1; | |
573 | } | |
574 | } | |
575 | ||
576 | for (i = 0; i < sk_ASN1_OBJECT_num(policy_oids); i++) { | |
577 | oid = sk_ASN1_OBJECT_value(policy_oids, i); | |
578 | node = tree_find_sk(auth_nodes, oid); | |
579 | if (!node) { | |
580 | if (!anyPolicy) | |
581 | continue; | |
582 | /* | |
583 | * Create a new node with policy ID from user set and qualifiers | |
584 | * from anyPolicy. | |
585 | */ | |
586 | extra = policy_data_new(NULL, oid, node_critical(anyPolicy)); | |
90945fa3 | 587 | if (extra == NULL) |
0f113f3e MC |
588 | return 0; |
589 | extra->qualifier_set = anyPolicy->data->qualifier_set; | |
590 | extra->flags = POLICY_DATA_FLAG_SHARED_QUALIFIERS | |
591 | | POLICY_DATA_FLAG_EXTRA_NODE; | |
592 | node = level_add_node(NULL, extra, anyPolicy->parent, tree); | |
593 | } | |
594 | if (!tree->user_policies) { | |
595 | tree->user_policies = sk_X509_POLICY_NODE_new_null(); | |
596 | if (!tree->user_policies) | |
597 | return 1; | |
598 | } | |
599 | if (!sk_X509_POLICY_NODE_push(tree->user_policies, node)) | |
600 | return 0; | |
601 | } | |
602 | return 1; | |
603 | ||
604 | } | |
4acc3e90 DSH |
605 | |
606 | static int tree_evaluate(X509_POLICY_TREE *tree) | |
0f113f3e MC |
607 | { |
608 | int ret, i; | |
609 | X509_POLICY_LEVEL *curr = tree->levels + 1; | |
610 | const X509_POLICY_CACHE *cache; | |
4acc3e90 | 611 | |
0f113f3e MC |
612 | for (i = 1; i < tree->nlevel; i++, curr++) { |
613 | cache = policy_cache_set(curr->cert); | |
614 | if (!tree_link_nodes(curr, cache)) | |
615 | return 0; | |
616 | ||
617 | if (!(curr->flags & X509_V_FLAG_INHIBIT_ANY) | |
618 | && !tree_link_any(curr, cache, tree)) | |
619 | return 0; | |
620 | tree_print("before tree_prune()", tree, curr); | |
621 | ret = tree_prune(tree, curr); | |
622 | if (ret != 1) | |
623 | return ret; | |
624 | } | |
625 | ||
626 | return 1; | |
4acc3e90 | 627 | |
0f113f3e MC |
628 | } |
629 | ||
630 | static void exnode_free(X509_POLICY_NODE *node) | |
631 | { | |
632 | if (node->data && (node->data->flags & POLICY_DATA_FLAG_EXTRA_NODE)) | |
633 | OPENSSL_free(node); | |
634 | } | |
4acc3e90 DSH |
635 | |
636 | void X509_policy_tree_free(X509_POLICY_TREE *tree) | |
0f113f3e MC |
637 | { |
638 | X509_POLICY_LEVEL *curr; | |
639 | int i; | |
4acc3e90 | 640 | |
0f113f3e MC |
641 | if (!tree) |
642 | return; | |
4acc3e90 | 643 | |
0f113f3e MC |
644 | sk_X509_POLICY_NODE_free(tree->auth_policies); |
645 | sk_X509_POLICY_NODE_pop_free(tree->user_policies, exnode_free); | |
4acc3e90 | 646 | |
0f113f3e | 647 | for (i = 0, curr = tree->levels; i < tree->nlevel; i++, curr++) { |
222561fe RS |
648 | X509_free(curr->cert); |
649 | sk_X509_POLICY_NODE_pop_free(curr->nodes, policy_node_free); | |
25aaa98a | 650 | policy_node_free(curr->anyPolicy); |
0f113f3e | 651 | } |
4acc3e90 | 652 | |
222561fe | 653 | sk_X509_POLICY_DATA_pop_free(tree->extra_data, policy_data_free); |
0f113f3e MC |
654 | OPENSSL_free(tree->levels); |
655 | OPENSSL_free(tree); | |
4acc3e90 | 656 | |
0f113f3e | 657 | } |
4acc3e90 | 658 | |
1d97c843 TH |
659 | /*- |
660 | * Application policy checking function. | |
4acc3e90 | 661 | * Return codes: |
0f113f3e | 662 | * 0 Internal Error. |
4acc3e90 DSH |
663 | * 1 Successful. |
664 | * -1 One or more certificates contain invalid or inconsistent extensions | |
0f113f3e | 665 | * -2 User constrained policy set empty and requireExplicit true. |
4acc3e90 DSH |
666 | */ |
667 | ||
b6a5fdb8 | 668 | int X509_policy_check(X509_POLICY_TREE **ptree, int *pexplicit_policy, |
0f113f3e MC |
669 | STACK_OF(X509) *certs, |
670 | STACK_OF(ASN1_OBJECT) *policy_oids, unsigned int flags) | |
671 | { | |
672 | int ret; | |
673 | X509_POLICY_TREE *tree = NULL; | |
674 | STACK_OF(X509_POLICY_NODE) *nodes, *auth_nodes = NULL; | |
675 | *ptree = NULL; | |
4acc3e90 | 676 | |
0f113f3e MC |
677 | *pexplicit_policy = 0; |
678 | ret = tree_init(&tree, certs, flags); | |
4acc3e90 | 679 | |
0f113f3e | 680 | switch (ret) { |
4acc3e90 | 681 | |
0f113f3e MC |
682 | /* Tree empty requireExplicit False: OK */ |
683 | case 2: | |
684 | return 1; | |
4acc3e90 | 685 | |
0f113f3e MC |
686 | /* Some internal error */ |
687 | case -1: | |
688 | return -1; | |
002e66c0 | 689 | |
0f113f3e MC |
690 | /* Some internal error */ |
691 | case 0: | |
692 | return 0; | |
4acc3e90 | 693 | |
0f113f3e | 694 | /* Tree empty requireExplicit True: Error */ |
4acc3e90 | 695 | |
0f113f3e MC |
696 | case 6: |
697 | *pexplicit_policy = 1; | |
698 | return -2; | |
4acc3e90 | 699 | |
0f113f3e MC |
700 | /* Tree OK requireExplicit True: OK and continue */ |
701 | case 5: | |
702 | *pexplicit_policy = 1; | |
703 | break; | |
4acc3e90 | 704 | |
0f113f3e | 705 | /* Tree OK: continue */ |
4acc3e90 | 706 | |
0f113f3e MC |
707 | case 1: |
708 | if (!tree) | |
709 | /* | |
710 | * tree_init() returns success and a null tree | |
711 | * if it's just looking at a trust anchor. | |
712 | * I'm not sure that returning success here is | |
713 | * correct, but I'm sure that reporting this | |
714 | * as an internal error which our caller | |
715 | * interprets as a malloc failure is wrong. | |
716 | */ | |
717 | return 1; | |
718 | break; | |
719 | } | |
4acc3e90 | 720 | |
0f113f3e MC |
721 | if (!tree) |
722 | goto error; | |
723 | ret = tree_evaluate(tree); | |
4acc3e90 | 724 | |
0f113f3e | 725 | tree_print("tree_evaluate()", tree, NULL); |
002e66c0 | 726 | |
0f113f3e MC |
727 | if (ret <= 0) |
728 | goto error; | |
4acc3e90 | 729 | |
0f113f3e MC |
730 | /* Return value 2 means tree empty */ |
731 | if (ret == 2) { | |
732 | X509_policy_tree_free(tree); | |
733 | if (*pexplicit_policy) | |
734 | return -2; | |
735 | else | |
736 | return 1; | |
737 | } | |
4acc3e90 | 738 | |
0f113f3e | 739 | /* Tree is not empty: continue */ |
4acc3e90 | 740 | |
0f113f3e | 741 | ret = tree_calculate_authority_set(tree, &auth_nodes); |
4acc3e90 | 742 | |
0f113f3e MC |
743 | if (!ret) |
744 | goto error; | |
4acc3e90 | 745 | |
0f113f3e MC |
746 | if (!tree_calculate_user_set(tree, policy_oids, auth_nodes)) |
747 | goto error; | |
4acc3e90 | 748 | |
0f113f3e MC |
749 | if (ret == 2) |
750 | sk_X509_POLICY_NODE_free(auth_nodes); | |
4acc3e90 | 751 | |
0f113f3e MC |
752 | if (tree) |
753 | *ptree = tree; | |
4acc3e90 | 754 | |
0f113f3e MC |
755 | if (*pexplicit_policy) { |
756 | nodes = X509_policy_tree_get0_user_policies(tree); | |
757 | if (sk_X509_POLICY_NODE_num(nodes) <= 0) | |
758 | return -2; | |
759 | } | |
4acc3e90 | 760 | |
0f113f3e | 761 | return 1; |
4acc3e90 | 762 | |
0f113f3e | 763 | error: |
4acc3e90 | 764 | |
0f113f3e | 765 | X509_policy_tree_free(tree); |
4acc3e90 | 766 | |
0f113f3e | 767 | return 0; |
4acc3e90 | 768 | |
0f113f3e | 769 | } |