2 * Copyright 2004-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #include <openssl/asn1.h>
11 #include <openssl/x509.h>
12 #include <openssl/x509v3.h>
16 static int node_cmp(const X509_POLICY_NODE *const *a,
17 const X509_POLICY_NODE *const *b)
19 return OBJ_cmp((*a)->data->valid_policy, (*b)->data->valid_policy);
22 STACK_OF(X509_POLICY_NODE) *policy_node_cmp_new(void)
24 return sk_X509_POLICY_NODE_new(node_cmp);
27 X509_POLICY_NODE *tree_find_sk(STACK_OF(X509_POLICY_NODE) *nodes,
28 const ASN1_OBJECT *id)
34 n.valid_policy = (ASN1_OBJECT *)id;
37 idx = sk_X509_POLICY_NODE_find(nodes, &l);
41 return sk_X509_POLICY_NODE_value(nodes, idx);
45 X509_POLICY_NODE *level_find_node(const X509_POLICY_LEVEL *level,
46 const X509_POLICY_NODE *parent,
47 const ASN1_OBJECT *id)
49 X509_POLICY_NODE *node;
51 for (i = 0; i < sk_X509_POLICY_NODE_num(level->nodes); i++) {
52 node = sk_X509_POLICY_NODE_value(level->nodes, i);
53 if (node->parent == parent) {
54 if (!OBJ_cmp(node->data->valid_policy, id))
61 X509_POLICY_NODE *level_add_node(X509_POLICY_LEVEL *level,
62 X509_POLICY_DATA *data,
63 X509_POLICY_NODE *parent,
64 X509_POLICY_TREE *tree)
66 X509_POLICY_NODE *node;
68 node = OPENSSL_zalloc(sizeof(*node));
72 node->parent = parent;
74 if (OBJ_obj2nid(data->valid_policy) == NID_any_policy) {
77 level->anyPolicy = node;
80 if (level->nodes == NULL)
81 level->nodes = policy_node_cmp_new();
82 if (level->nodes == NULL)
84 if (!sk_X509_POLICY_NODE_push(level->nodes, node))
90 if (tree->extra_data == NULL)
91 tree->extra_data = sk_X509_POLICY_DATA_new_null();
92 if (tree->extra_data == NULL)
94 if (!sk_X509_POLICY_DATA_push(tree->extra_data, data))
104 policy_node_free(node);
108 void policy_node_free(X509_POLICY_NODE *node)
114 * See if a policy node matches a policy OID. If mapping enabled look through
115 * expected policy set otherwise just valid policy.
118 int policy_node_match(const X509_POLICY_LEVEL *lvl,
119 const X509_POLICY_NODE *node, const ASN1_OBJECT *oid)
122 ASN1_OBJECT *policy_oid;
123 const X509_POLICY_DATA *x = node->data;
125 if ((lvl->flags & X509_V_FLAG_INHIBIT_MAP)
126 || !(x->flags & POLICY_DATA_FLAG_MAP_MASK)) {
127 if (!OBJ_cmp(x->valid_policy, oid))
132 for (i = 0; i < sk_ASN1_OBJECT_num(x->expected_policy_set); i++) {
133 policy_oid = sk_ASN1_OBJECT_value(x->expected_policy_set, i);
134 if (!OBJ_cmp(policy_oid, oid))