1 | /*
|
---|
2 | * Copyright 2004-2016 The OpenSSL Project Authors. All Rights Reserved.
|
---|
3 | *
|
---|
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
|
---|
8 | */
|
---|
9 |
|
---|
10 | #include <openssl/asn1.h>
|
---|
11 | #include <openssl/x509.h>
|
---|
12 | #include <openssl/x509v3.h>
|
---|
13 |
|
---|
14 | #include "pcy_int.h"
|
---|
15 |
|
---|
16 | static int node_cmp(const X509_POLICY_NODE *const *a,
|
---|
17 | const X509_POLICY_NODE *const *b)
|
---|
18 | {
|
---|
19 | return OBJ_cmp((*a)->data->valid_policy, (*b)->data->valid_policy);
|
---|
20 | }
|
---|
21 |
|
---|
22 | STACK_OF(X509_POLICY_NODE) *policy_node_cmp_new(void)
|
---|
23 | {
|
---|
24 | return sk_X509_POLICY_NODE_new(node_cmp);
|
---|
25 | }
|
---|
26 |
|
---|
27 | X509_POLICY_NODE *tree_find_sk(STACK_OF(X509_POLICY_NODE) *nodes,
|
---|
28 | const ASN1_OBJECT *id)
|
---|
29 | {
|
---|
30 | X509_POLICY_DATA n;
|
---|
31 | X509_POLICY_NODE l;
|
---|
32 | int idx;
|
---|
33 |
|
---|
34 | n.valid_policy = (ASN1_OBJECT *)id;
|
---|
35 | l.data = &n;
|
---|
36 |
|
---|
37 | idx = sk_X509_POLICY_NODE_find(nodes, &l);
|
---|
38 | if (idx == -1)
|
---|
39 | return NULL;
|
---|
40 |
|
---|
41 | return sk_X509_POLICY_NODE_value(nodes, idx);
|
---|
42 |
|
---|
43 | }
|
---|
44 |
|
---|
45 | X509_POLICY_NODE *level_find_node(const X509_POLICY_LEVEL *level,
|
---|
46 | const X509_POLICY_NODE *parent,
|
---|
47 | const ASN1_OBJECT *id)
|
---|
48 | {
|
---|
49 | X509_POLICY_NODE *node;
|
---|
50 | int i;
|
---|
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))
|
---|
55 | return node;
|
---|
56 | }
|
---|
57 | }
|
---|
58 | return NULL;
|
---|
59 | }
|
---|
60 |
|
---|
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)
|
---|
65 | {
|
---|
66 | X509_POLICY_NODE *node;
|
---|
67 |
|
---|
68 | node = OPENSSL_zalloc(sizeof(*node));
|
---|
69 | if (node == NULL)
|
---|
70 | return NULL;
|
---|
71 | node->data = data;
|
---|
72 | node->parent = parent;
|
---|
73 | if (level) {
|
---|
74 | if (OBJ_obj2nid(data->valid_policy) == NID_any_policy) {
|
---|
75 | if (level->anyPolicy)
|
---|
76 | goto node_error;
|
---|
77 | level->anyPolicy = node;
|
---|
78 | } else {
|
---|
79 |
|
---|
80 | if (level->nodes == NULL)
|
---|
81 | level->nodes = policy_node_cmp_new();
|
---|
82 | if (level->nodes == NULL)
|
---|
83 | goto node_error;
|
---|
84 | if (!sk_X509_POLICY_NODE_push(level->nodes, node))
|
---|
85 | goto node_error;
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | if (tree) {
|
---|
90 | if (tree->extra_data == NULL)
|
---|
91 | tree->extra_data = sk_X509_POLICY_DATA_new_null();
|
---|
92 | if (tree->extra_data == NULL)
|
---|
93 | goto node_error;
|
---|
94 | if (!sk_X509_POLICY_DATA_push(tree->extra_data, data))
|
---|
95 | goto node_error;
|
---|
96 | }
|
---|
97 |
|
---|
98 | if (parent)
|
---|
99 | parent->nchild++;
|
---|
100 |
|
---|
101 | return node;
|
---|
102 |
|
---|
103 | node_error:
|
---|
104 | policy_node_free(node);
|
---|
105 | return NULL;
|
---|
106 | }
|
---|
107 |
|
---|
108 | void policy_node_free(X509_POLICY_NODE *node)
|
---|
109 | {
|
---|
110 | OPENSSL_free(node);
|
---|
111 | }
|
---|
112 |
|
---|
113 | /*
|
---|
114 | * See if a policy node matches a policy OID. If mapping enabled look through
|
---|
115 | * expected policy set otherwise just valid policy.
|
---|
116 | */
|
---|
117 |
|
---|
118 | int policy_node_match(const X509_POLICY_LEVEL *lvl,
|
---|
119 | const X509_POLICY_NODE *node, const ASN1_OBJECT *oid)
|
---|
120 | {
|
---|
121 | int i;
|
---|
122 | ASN1_OBJECT *policy_oid;
|
---|
123 | const X509_POLICY_DATA *x = node->data;
|
---|
124 |
|
---|
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))
|
---|
128 | return 1;
|
---|
129 | return 0;
|
---|
130 | }
|
---|
131 |
|
---|
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))
|
---|
135 | return 1;
|
---|
136 | }
|
---|
137 | return 0;
|
---|
138 |
|
---|
139 | }
|
---|