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