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