Coverage Report

Created: 2023-06-08 06:41

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