Coverage Report

Created: 2025-12-31 06:58

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