1 /* $OpenBSD: pcy_cache.c,v 1.2 2021/11/01 20:53:08 tb Exp $ */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2004.
4 */
5 /* ====================================================================
6 * Copyright (c) 2004 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59 #include <openssl/x509.h>
60 #include <openssl/x509v3.h>
61
62 #include "pcy_int.h"
63 #include "x509_lcl.h"
64
65 static int policy_data_cmp(const X509_POLICY_DATA * const *a,
66 const X509_POLICY_DATA * const *b);
67 static int policy_cache_set_int(long *out, ASN1_INTEGER *value);
68
69 /* Set cache entry according to CertificatePolicies extension.
70 * Note: this destroys the passed CERTIFICATEPOLICIES structure.
71 */
72
73 static int
policy_cache_create(X509 * x,CERTIFICATEPOLICIES * policies,int crit)74 policy_cache_create(X509 *x, CERTIFICATEPOLICIES *policies, int crit)
75 {
76 int i;
77 int ret = 0;
78 X509_POLICY_CACHE *cache = x->policy_cache;
79 X509_POLICY_DATA *data = NULL;
80 POLICYINFO *policy;
81
82 if (sk_POLICYINFO_num(policies) == 0)
83 goto bad_policy;
84 cache->data = sk_X509_POLICY_DATA_new(policy_data_cmp);
85 if (!cache->data)
86 goto bad_policy;
87 for (i = 0; i < sk_POLICYINFO_num(policies); i++) {
88 policy = sk_POLICYINFO_value(policies, i);
89 data = policy_data_new(policy, NULL, crit);
90 if (!data)
91 goto bad_policy;
92 /* Duplicate policy OIDs are illegal: reject if matches
93 * found.
94 */
95 if (OBJ_obj2nid(data->valid_policy) == NID_any_policy) {
96 if (cache->anyPolicy) {
97 ret = -1;
98 goto bad_policy;
99 }
100 cache->anyPolicy = data;
101 } else if (sk_X509_POLICY_DATA_find(cache->data, data) != -1) {
102 ret = -1;
103 goto bad_policy;
104 } else if (!sk_X509_POLICY_DATA_push(cache->data, data))
105 goto bad_policy;
106 data = NULL;
107 }
108 ret = 1;
109
110 bad_policy:
111 if (ret == -1)
112 x->ex_flags |= EXFLAG_INVALID_POLICY;
113 if (data)
114 policy_data_free(data);
115 sk_POLICYINFO_pop_free(policies, POLICYINFO_free);
116 if (ret <= 0) {
117 sk_X509_POLICY_DATA_pop_free(cache->data, policy_data_free);
118 cache->data = NULL;
119 }
120 return ret;
121 }
122
123 static int
policy_cache_new(X509 * x)124 policy_cache_new(X509 *x)
125 {
126 X509_POLICY_CACHE *cache;
127 ASN1_INTEGER *ext_any = NULL;
128 POLICY_CONSTRAINTS *ext_pcons = NULL;
129 CERTIFICATEPOLICIES *ext_cpols = NULL;
130 POLICY_MAPPINGS *ext_pmaps = NULL;
131 int i;
132
133 cache = malloc(sizeof(X509_POLICY_CACHE));
134 if (!cache)
135 return 0;
136 cache->anyPolicy = NULL;
137 cache->data = NULL;
138 cache->any_skip = -1;
139 cache->explicit_skip = -1;
140 cache->map_skip = -1;
141
142 x->policy_cache = cache;
143
144 /* Handle requireExplicitPolicy *first*. Need to process this
145 * even if we don't have any policies.
146 */
147 ext_pcons = X509_get_ext_d2i(x, NID_policy_constraints, &i, NULL);
148
149 if (!ext_pcons) {
150 if (i != -1)
151 goto bad_cache;
152 } else {
153 if (!ext_pcons->requireExplicitPolicy &&
154 !ext_pcons->inhibitPolicyMapping)
155 goto bad_cache;
156 if (!policy_cache_set_int(&cache->explicit_skip,
157 ext_pcons->requireExplicitPolicy))
158 goto bad_cache;
159 if (!policy_cache_set_int(&cache->map_skip,
160 ext_pcons->inhibitPolicyMapping))
161 goto bad_cache;
162 }
163
164 /* Process CertificatePolicies */
165
166 ext_cpols = X509_get_ext_d2i(x, NID_certificate_policies, &i, NULL);
167 /* If no CertificatePolicies extension or problem decoding then
168 * there is no point continuing because the valid policies will be
169 * NULL.
170 */
171 if (!ext_cpols) {
172 /* If not absent some problem with extension */
173 if (i != -1)
174 goto bad_cache;
175 return 1;
176 }
177
178 i = policy_cache_create(x, ext_cpols, i);
179
180 /* NB: ext_cpols freed by policy_cache_set_policies */
181
182 if (i <= 0)
183 return i;
184
185 ext_pmaps = X509_get_ext_d2i(x, NID_policy_mappings, &i, NULL);
186
187 if (!ext_pmaps) {
188 /* If not absent some problem with extension */
189 if (i != -1)
190 goto bad_cache;
191 } else {
192 i = policy_cache_set_mapping(x, ext_pmaps);
193 if (i <= 0)
194 goto bad_cache;
195 }
196
197 ext_any = X509_get_ext_d2i(x, NID_inhibit_any_policy, &i, NULL);
198
199 if (!ext_any) {
200 if (i != -1)
201 goto bad_cache;
202 } else if (!policy_cache_set_int(&cache->any_skip, ext_any))
203 goto bad_cache;
204
205 if (0) {
206 bad_cache:
207 x->ex_flags |= EXFLAG_INVALID_POLICY;
208 }
209
210 if (ext_pcons)
211 POLICY_CONSTRAINTS_free(ext_pcons);
212
213 if (ext_any)
214 ASN1_INTEGER_free(ext_any);
215
216 return 1;
217 }
218
219 void
policy_cache_free(X509_POLICY_CACHE * cache)220 policy_cache_free(X509_POLICY_CACHE *cache)
221 {
222 if (!cache)
223 return;
224 if (cache->anyPolicy)
225 policy_data_free(cache->anyPolicy);
226 if (cache->data)
227 sk_X509_POLICY_DATA_pop_free(cache->data, policy_data_free);
228 free(cache);
229 }
230
231 const X509_POLICY_CACHE *
policy_cache_set(X509 * x)232 policy_cache_set(X509 *x)
233 {
234 if (x->policy_cache == NULL) {
235 CRYPTO_w_lock(CRYPTO_LOCK_X509);
236 policy_cache_new(x);
237 CRYPTO_w_unlock(CRYPTO_LOCK_X509);
238 }
239
240 return x->policy_cache;
241 }
242
243 X509_POLICY_DATA *
policy_cache_find_data(const X509_POLICY_CACHE * cache,const ASN1_OBJECT * id)244 policy_cache_find_data(const X509_POLICY_CACHE *cache, const ASN1_OBJECT *id)
245 {
246 int idx;
247 X509_POLICY_DATA tmp;
248
249 tmp.valid_policy = (ASN1_OBJECT *)id;
250 idx = sk_X509_POLICY_DATA_find(cache->data, &tmp);
251 if (idx == -1)
252 return NULL;
253 return sk_X509_POLICY_DATA_value(cache->data, idx);
254 }
255
256 static int
policy_data_cmp(const X509_POLICY_DATA * const * a,const X509_POLICY_DATA * const * b)257 policy_data_cmp(const X509_POLICY_DATA * const *a,
258 const X509_POLICY_DATA * const *b)
259 {
260 return OBJ_cmp((*a)->valid_policy, (*b)->valid_policy);
261 }
262
263 static int
policy_cache_set_int(long * out,ASN1_INTEGER * value)264 policy_cache_set_int(long *out, ASN1_INTEGER *value)
265 {
266 if (value == NULL)
267 return 1;
268 if (value->type == V_ASN1_NEG_INTEGER)
269 return 0;
270 *out = ASN1_INTEGER_get(value);
271 return 1;
272 }
273