]>
Commit | Line | Data |
---|---|---|
0f113f3e | 1 | /* |
d2e9e320 | 2 | * Copyright 2004-2016 The OpenSSL Project Authors. All Rights Reserved. |
4acc3e90 | 3 | * |
d2e9e320 RS |
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 | |
4acc3e90 DSH |
8 | */ |
9 | ||
4acc3e90 DSH |
10 | #include <openssl/asn1.h> |
11 | #include <openssl/x509.h> | |
12 | #include <openssl/x509v3.h> | |
13 | ||
14 | #include "pcy_int.h" | |
15 | ||
0f113f3e MC |
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 | } | |
4acc3e90 DSH |
21 | |
22 | STACK_OF(X509_POLICY_NODE) *policy_node_cmp_new(void) | |
0f113f3e MC |
23 | { |
24 | return sk_X509_POLICY_NODE_new(node_cmp); | |
25 | } | |
4acc3e90 DSH |
26 | |
27 | X509_POLICY_NODE *tree_find_sk(STACK_OF(X509_POLICY_NODE) *nodes, | |
0f113f3e MC |
28 | const ASN1_OBJECT *id) |
29 | { | |
30 | X509_POLICY_DATA n; | |
31 | X509_POLICY_NODE l; | |
32 | int idx; | |
4acc3e90 | 33 | |
0f113f3e MC |
34 | n.valid_policy = (ASN1_OBJECT *)id; |
35 | l.data = &n; | |
4acc3e90 | 36 | |
0f113f3e MC |
37 | idx = sk_X509_POLICY_NODE_find(nodes, &l); |
38 | if (idx == -1) | |
39 | return NULL; | |
4acc3e90 | 40 | |
0f113f3e | 41 | return sk_X509_POLICY_NODE_value(nodes, idx); |
4acc3e90 | 42 | |
0f113f3e | 43 | } |
4acc3e90 DSH |
44 | |
45 | X509_POLICY_NODE *level_find_node(const X509_POLICY_LEVEL *level, | |
0f113f3e MC |
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 | } | |
4acc3e90 DSH |
60 | |
61 | X509_POLICY_NODE *level_add_node(X509_POLICY_LEVEL *level, | |
4a640fb6 | 62 | X509_POLICY_DATA *data, |
0f113f3e MC |
63 | X509_POLICY_NODE *parent, |
64 | X509_POLICY_TREE *tree) | |
65 | { | |
66 | X509_POLICY_NODE *node; | |
64b25758 RS |
67 | |
68 | node = OPENSSL_zalloc(sizeof(*node)); | |
90945fa3 | 69 | if (node == NULL) |
0f113f3e MC |
70 | return NULL; |
71 | node->data = data; | |
72 | node->parent = parent; | |
0f113f3e MC |
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 | ||
90945fa3 | 80 | if (level->nodes == NULL) |
0f113f3e | 81 | level->nodes = policy_node_cmp_new(); |
90945fa3 | 82 | if (level->nodes == NULL) |
0f113f3e MC |
83 | goto node_error; |
84 | if (!sk_X509_POLICY_NODE_push(level->nodes, node)) | |
85 | goto node_error; | |
86 | } | |
87 | } | |
88 | ||
89 | if (tree) { | |
90945fa3 | 90 | if (tree->extra_data == NULL) |
0f113f3e | 91 | tree->extra_data = sk_X509_POLICY_DATA_new_null(); |
90945fa3 | 92 | if (tree->extra_data == NULL) |
0f113f3e MC |
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); | |
895c2f84 | 105 | return NULL; |
0f113f3e | 106 | } |
4acc3e90 DSH |
107 | |
108 | void policy_node_free(X509_POLICY_NODE *node) | |
0f113f3e MC |
109 | { |
110 | OPENSSL_free(node); | |
111 | } | |
4acc3e90 | 112 | |
0f113f3e MC |
113 | /* |
114 | * See if a policy node matches a policy OID. If mapping enabled look through | |
002e66c0 DSH |
115 | * expected policy set otherwise just valid policy. |
116 | */ | |
117 | ||
118 | int policy_node_match(const X509_POLICY_LEVEL *lvl, | |
0f113f3e MC |
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 | } |