Coverage Report

Created: 2025-08-26 06:04

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