Coverage Report

Created: 2025-12-31 06:58

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