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