Coverage Report

Created: 2025-06-13 06:58

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