/src/boringssl/crypto/x509/x509_lu.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #include <string.h> |
16 | | |
17 | | #include <openssl/err.h> |
18 | | #include <openssl/mem.h> |
19 | | #include <openssl/x509.h> |
20 | | |
21 | | #include "../internal.h" |
22 | | #include "internal.h" |
23 | | |
24 | | |
25 | | static int X509_OBJECT_idx_by_subject(STACK_OF(X509_OBJECT) *h, int type, |
26 | | X509_NAME *name); |
27 | | static X509_OBJECT *X509_OBJECT_retrieve_by_subject(STACK_OF(X509_OBJECT) *h, |
28 | | int type, X509_NAME *name); |
29 | | static X509_OBJECT *X509_OBJECT_retrieve_match(STACK_OF(X509_OBJECT) *h, |
30 | | X509_OBJECT *x); |
31 | | static int X509_OBJECT_up_ref_count(X509_OBJECT *a); |
32 | | |
33 | | static X509_LOOKUP *X509_LOOKUP_new(const X509_LOOKUP_METHOD *method, |
34 | | X509_STORE *store); |
35 | | static int X509_LOOKUP_by_subject(X509_LOOKUP *ctx, int type, X509_NAME *name, |
36 | | X509_OBJECT *ret); |
37 | | |
38 | | static X509_LOOKUP *X509_LOOKUP_new(const X509_LOOKUP_METHOD *method, |
39 | 0 | X509_STORE *store) { |
40 | 0 | X509_LOOKUP *ret = |
41 | 0 | reinterpret_cast<X509_LOOKUP *>(OPENSSL_zalloc(sizeof(X509_LOOKUP))); |
42 | 0 | if (ret == NULL) { |
43 | 0 | return NULL; |
44 | 0 | } |
45 | | |
46 | 0 | ret->method = method; |
47 | 0 | ret->store_ctx = store; |
48 | 0 | if (method->new_item != NULL && !method->new_item(ret)) { |
49 | 0 | OPENSSL_free(ret); |
50 | 0 | return NULL; |
51 | 0 | } |
52 | 0 | return ret; |
53 | 0 | } |
54 | | |
55 | 0 | void X509_LOOKUP_free(X509_LOOKUP *ctx) { |
56 | 0 | if (ctx == NULL) { |
57 | 0 | return; |
58 | 0 | } |
59 | 0 | if (ctx->method != NULL && ctx->method->free != NULL) { |
60 | 0 | (*ctx->method->free)(ctx); |
61 | 0 | } |
62 | 0 | OPENSSL_free(ctx); |
63 | 0 | } |
64 | | |
65 | | int X509_LOOKUP_ctrl(X509_LOOKUP *ctx, int cmd, const char *argc, long argl, |
66 | 0 | char **ret) { |
67 | 0 | if (ctx->method == NULL) { |
68 | 0 | return -1; |
69 | 0 | } |
70 | 0 | if (ctx->method->ctrl != NULL) { |
71 | 0 | return ctx->method->ctrl(ctx, cmd, argc, argl, ret); |
72 | 0 | } else { |
73 | 0 | return 1; |
74 | 0 | } |
75 | 0 | } |
76 | | |
77 | | static int X509_LOOKUP_by_subject(X509_LOOKUP *ctx, int type, X509_NAME *name, |
78 | 0 | X509_OBJECT *ret) { |
79 | 0 | if (ctx->method == NULL || ctx->method->get_by_subject == NULL) { |
80 | 0 | return 0; |
81 | 0 | } |
82 | | // Note |get_by_subject| leaves |ret| in an inconsistent state. It has |
83 | | // pointers to an |X509| or |X509_CRL|, but has not bumped the refcount yet. |
84 | | // For now, the caller is expected to fix this, but ideally we'd fix the |
85 | | // |X509_LOOKUP| convention itself. |
86 | 0 | return ctx->method->get_by_subject(ctx, type, name, ret) > 0; |
87 | 0 | } |
88 | | |
89 | 0 | static int x509_object_cmp(const X509_OBJECT *a, const X509_OBJECT *b) { |
90 | 0 | int ret = a->type - b->type; |
91 | 0 | if (ret) { |
92 | 0 | return ret; |
93 | 0 | } |
94 | 0 | switch (a->type) { |
95 | 0 | case X509_LU_X509: |
96 | 0 | return X509_subject_name_cmp(a->data.x509, b->data.x509); |
97 | 0 | case X509_LU_CRL: |
98 | 0 | return X509_CRL_cmp(a->data.crl, b->data.crl); |
99 | 0 | default: |
100 | | // abort(); |
101 | 0 | return 0; |
102 | 0 | } |
103 | 0 | } |
104 | | |
105 | | static int x509_object_cmp_sk(const X509_OBJECT *const *a, |
106 | 0 | const X509_OBJECT *const *b) { |
107 | 0 | return x509_object_cmp(*a, *b); |
108 | 0 | } |
109 | | |
110 | 4.90k | X509_STORE *X509_STORE_new(void) { |
111 | 4.90k | X509_STORE *ret = |
112 | 4.90k | reinterpret_cast<X509_STORE *>(OPENSSL_zalloc(sizeof(X509_STORE))); |
113 | 4.90k | if (ret == NULL) { |
114 | 0 | return NULL; |
115 | 0 | } |
116 | | |
117 | 4.90k | ret->references = 1; |
118 | 4.90k | CRYPTO_MUTEX_init(&ret->objs_lock); |
119 | 4.90k | ret->objs = sk_X509_OBJECT_new(x509_object_cmp_sk); |
120 | 4.90k | ret->get_cert_methods = sk_X509_LOOKUP_new_null(); |
121 | 4.90k | ret->param = X509_VERIFY_PARAM_new(); |
122 | 4.90k | if (ret->objs == NULL || ret->get_cert_methods == NULL || |
123 | 4.90k | ret->param == NULL) { |
124 | 0 | X509_STORE_free(ret); |
125 | 0 | return NULL; |
126 | 0 | } |
127 | | |
128 | 4.90k | return ret; |
129 | 4.90k | } |
130 | | |
131 | 0 | int X509_STORE_up_ref(X509_STORE *store) { |
132 | 0 | CRYPTO_refcount_inc(&store->references); |
133 | 0 | return 1; |
134 | 0 | } |
135 | | |
136 | 130k | void X509_STORE_free(X509_STORE *vfy) { |
137 | 130k | if (vfy == nullptr || !CRYPTO_refcount_dec_and_test_zero(&vfy->references)) { |
138 | 125k | return; |
139 | 125k | } |
140 | | |
141 | 4.88k | CRYPTO_MUTEX_cleanup(&vfy->objs_lock); |
142 | 4.88k | sk_X509_LOOKUP_pop_free(vfy->get_cert_methods, X509_LOOKUP_free); |
143 | 4.88k | sk_X509_OBJECT_pop_free(vfy->objs, X509_OBJECT_free); |
144 | 4.88k | X509_VERIFY_PARAM_free(vfy->param); |
145 | 4.88k | OPENSSL_free(vfy); |
146 | 4.88k | } |
147 | | |
148 | 0 | X509_LOOKUP *X509_STORE_add_lookup(X509_STORE *v, const X509_LOOKUP_METHOD *m) { |
149 | 0 | STACK_OF(X509_LOOKUP) *sk = v->get_cert_methods; |
150 | 0 | for (size_t i = 0; i < sk_X509_LOOKUP_num(sk); i++) { |
151 | 0 | X509_LOOKUP *lu = sk_X509_LOOKUP_value(sk, i); |
152 | 0 | if (m == lu->method) { |
153 | 0 | return lu; |
154 | 0 | } |
155 | 0 | } |
156 | | |
157 | 0 | X509_LOOKUP *lu = X509_LOOKUP_new(m, v); |
158 | 0 | if (lu == NULL || !sk_X509_LOOKUP_push(v->get_cert_methods, lu)) { |
159 | 0 | X509_LOOKUP_free(lu); |
160 | 0 | return NULL; |
161 | 0 | } |
162 | | |
163 | 0 | return lu; |
164 | 0 | } |
165 | | |
166 | | int X509_STORE_CTX_get_by_subject(X509_STORE_CTX *vs, int type, X509_NAME *name, |
167 | 0 | X509_OBJECT *ret) { |
168 | 0 | X509_STORE *ctx = vs->ctx; |
169 | 0 | X509_OBJECT stmp; |
170 | 0 | CRYPTO_MUTEX_lock_write(&ctx->objs_lock); |
171 | 0 | X509_OBJECT *tmp = X509_OBJECT_retrieve_by_subject(ctx->objs, type, name); |
172 | 0 | CRYPTO_MUTEX_unlock_write(&ctx->objs_lock); |
173 | |
|
174 | 0 | if (tmp == NULL || type == X509_LU_CRL) { |
175 | 0 | for (size_t i = 0; i < sk_X509_LOOKUP_num(ctx->get_cert_methods); i++) { |
176 | 0 | X509_LOOKUP *lu = sk_X509_LOOKUP_value(ctx->get_cert_methods, i); |
177 | 0 | if (X509_LOOKUP_by_subject(lu, type, name, &stmp)) { |
178 | 0 | tmp = &stmp; |
179 | 0 | break; |
180 | 0 | } |
181 | 0 | } |
182 | 0 | if (tmp == NULL) { |
183 | 0 | return 0; |
184 | 0 | } |
185 | 0 | } |
186 | | |
187 | | // TODO(crbug.com/boringssl/685): This should call |
188 | | // |X509_OBJECT_free_contents|. |
189 | 0 | ret->type = tmp->type; |
190 | 0 | ret->data = tmp->data; |
191 | 0 | X509_OBJECT_up_ref_count(ret); |
192 | 0 | return 1; |
193 | 0 | } |
194 | | |
195 | 0 | static int x509_store_add(X509_STORE *ctx, void *x, int is_crl) { |
196 | 0 | if (x == NULL) { |
197 | 0 | return 0; |
198 | 0 | } |
199 | | |
200 | 0 | X509_OBJECT *const obj = X509_OBJECT_new(); |
201 | 0 | if (obj == NULL) { |
202 | 0 | return 0; |
203 | 0 | } |
204 | | |
205 | 0 | if (is_crl) { |
206 | 0 | obj->type = X509_LU_CRL; |
207 | 0 | obj->data.crl = (X509_CRL *)x; |
208 | 0 | } else { |
209 | 0 | obj->type = X509_LU_X509; |
210 | 0 | obj->data.x509 = (X509 *)x; |
211 | 0 | } |
212 | 0 | X509_OBJECT_up_ref_count(obj); |
213 | |
|
214 | 0 | CRYPTO_MUTEX_lock_write(&ctx->objs_lock); |
215 | |
|
216 | 0 | int ret = 1; |
217 | 0 | int added = 0; |
218 | | // Duplicates are silently ignored |
219 | 0 | if (!X509_OBJECT_retrieve_match(ctx->objs, obj)) { |
220 | 0 | ret = added = (sk_X509_OBJECT_push(ctx->objs, obj) != 0); |
221 | 0 | } |
222 | |
|
223 | 0 | CRYPTO_MUTEX_unlock_write(&ctx->objs_lock); |
224 | |
|
225 | 0 | if (!added) { |
226 | 0 | X509_OBJECT_free(obj); |
227 | 0 | } |
228 | |
|
229 | 0 | return ret; |
230 | 0 | } |
231 | | |
232 | 0 | int X509_STORE_add_cert(X509_STORE *ctx, X509 *x) { |
233 | 0 | return x509_store_add(ctx, x, /*is_crl=*/0); |
234 | 0 | } |
235 | | |
236 | 0 | int X509_STORE_add_crl(X509_STORE *ctx, X509_CRL *x) { |
237 | 0 | return x509_store_add(ctx, x, /*is_crl=*/1); |
238 | 0 | } |
239 | | |
240 | 0 | X509_OBJECT *X509_OBJECT_new(void) { |
241 | 0 | return reinterpret_cast<X509_OBJECT *>(OPENSSL_zalloc(sizeof(X509_OBJECT))); |
242 | 0 | } |
243 | | |
244 | 0 | void X509_OBJECT_free(X509_OBJECT *obj) { |
245 | 0 | if (obj == NULL) { |
246 | 0 | return; |
247 | 0 | } |
248 | 0 | X509_OBJECT_free_contents(obj); |
249 | 0 | OPENSSL_free(obj); |
250 | 0 | } |
251 | | |
252 | 0 | static int X509_OBJECT_up_ref_count(X509_OBJECT *a) { |
253 | 0 | switch (a->type) { |
254 | 0 | case X509_LU_X509: |
255 | 0 | X509_up_ref(a->data.x509); |
256 | 0 | break; |
257 | 0 | case X509_LU_CRL: |
258 | 0 | X509_CRL_up_ref(a->data.crl); |
259 | 0 | break; |
260 | 0 | } |
261 | 0 | return 1; |
262 | 0 | } |
263 | | |
264 | 0 | void X509_OBJECT_free_contents(X509_OBJECT *a) { |
265 | 0 | switch (a->type) { |
266 | 0 | case X509_LU_X509: |
267 | 0 | X509_free(a->data.x509); |
268 | 0 | break; |
269 | 0 | case X509_LU_CRL: |
270 | 0 | X509_CRL_free(a->data.crl); |
271 | 0 | break; |
272 | 0 | } |
273 | | |
274 | 0 | OPENSSL_memset(a, 0, sizeof(X509_OBJECT)); |
275 | 0 | } |
276 | | |
277 | 0 | int X509_OBJECT_get_type(const X509_OBJECT *a) { return a->type; } |
278 | | |
279 | 0 | X509 *X509_OBJECT_get0_X509(const X509_OBJECT *a) { |
280 | 0 | if (a == NULL || a->type != X509_LU_X509) { |
281 | 0 | return NULL; |
282 | 0 | } |
283 | 0 | return a->data.x509; |
284 | 0 | } |
285 | | |
286 | | static int x509_object_idx_cnt(STACK_OF(X509_OBJECT) *h, int type, |
287 | 0 | X509_NAME *name, int *pnmatch) { |
288 | 0 | X509_OBJECT stmp; |
289 | 0 | X509 x509_s; |
290 | 0 | X509_CINF cinf_s; |
291 | 0 | X509_CRL crl_s; |
292 | 0 | X509_CRL_INFO crl_info_s; |
293 | |
|
294 | 0 | stmp.type = type; |
295 | 0 | switch (type) { |
296 | 0 | case X509_LU_X509: |
297 | 0 | stmp.data.x509 = &x509_s; |
298 | 0 | x509_s.cert_info = &cinf_s; |
299 | 0 | cinf_s.subject = name; |
300 | 0 | break; |
301 | 0 | case X509_LU_CRL: |
302 | 0 | stmp.data.crl = &crl_s; |
303 | 0 | crl_s.crl = &crl_info_s; |
304 | 0 | crl_info_s.issuer = name; |
305 | 0 | break; |
306 | 0 | default: |
307 | | // abort(); |
308 | 0 | return -1; |
309 | 0 | } |
310 | | |
311 | 0 | size_t idx; |
312 | 0 | sk_X509_OBJECT_sort(h); |
313 | 0 | if (!sk_X509_OBJECT_find(h, &idx, &stmp)) { |
314 | 0 | return -1; |
315 | 0 | } |
316 | | |
317 | 0 | if (pnmatch != NULL) { |
318 | 0 | *pnmatch = 1; |
319 | 0 | for (size_t tidx = idx + 1; tidx < sk_X509_OBJECT_num(h); tidx++) { |
320 | 0 | const X509_OBJECT *tobj = sk_X509_OBJECT_value(h, tidx); |
321 | 0 | if (x509_object_cmp(tobj, &stmp)) { |
322 | 0 | break; |
323 | 0 | } |
324 | 0 | (*pnmatch)++; |
325 | 0 | } |
326 | 0 | } |
327 | |
|
328 | 0 | return (int)idx; |
329 | 0 | } |
330 | | |
331 | | static int X509_OBJECT_idx_by_subject(STACK_OF(X509_OBJECT) *h, int type, |
332 | 0 | X509_NAME *name) { |
333 | 0 | return x509_object_idx_cnt(h, type, name, NULL); |
334 | 0 | } |
335 | | |
336 | | static X509_OBJECT *X509_OBJECT_retrieve_by_subject(STACK_OF(X509_OBJECT) *h, |
337 | 0 | int type, X509_NAME *name) { |
338 | 0 | int idx; |
339 | 0 | idx = X509_OBJECT_idx_by_subject(h, type, name); |
340 | 0 | if (idx == -1) { |
341 | 0 | return NULL; |
342 | 0 | } |
343 | 0 | return sk_X509_OBJECT_value(h, idx); |
344 | 0 | } |
345 | | |
346 | 0 | static X509_OBJECT *x509_object_dup(const X509_OBJECT *obj) { |
347 | 0 | X509_OBJECT *ret = X509_OBJECT_new(); |
348 | 0 | if (ret == NULL) { |
349 | 0 | return NULL; |
350 | 0 | } |
351 | 0 | ret->type = obj->type; |
352 | 0 | ret->data = obj->data; |
353 | 0 | X509_OBJECT_up_ref_count(ret); |
354 | 0 | return ret; |
355 | 0 | } |
356 | | |
357 | 0 | STACK_OF(X509_OBJECT) *X509_STORE_get1_objects(X509_STORE *store) { |
358 | 0 | CRYPTO_MUTEX_lock_read(&store->objs_lock); |
359 | 0 | STACK_OF(X509_OBJECT) *ret = |
360 | 0 | sk_X509_OBJECT_deep_copy(store->objs, x509_object_dup, X509_OBJECT_free); |
361 | 0 | CRYPTO_MUTEX_unlock_read(&store->objs_lock); |
362 | 0 | return ret; |
363 | 0 | } |
364 | | |
365 | 0 | STACK_OF(X509_OBJECT) *X509_STORE_get0_objects(X509_STORE *store) { |
366 | 0 | return store->objs; |
367 | 0 | } |
368 | | |
369 | 0 | STACK_OF(X509) *X509_STORE_CTX_get1_certs(X509_STORE_CTX *ctx, X509_NAME *nm) { |
370 | 0 | int cnt; |
371 | 0 | STACK_OF(X509) *sk = sk_X509_new_null(); |
372 | 0 | if (sk == NULL) { |
373 | 0 | return NULL; |
374 | 0 | } |
375 | 0 | CRYPTO_MUTEX_lock_write(&ctx->ctx->objs_lock); |
376 | 0 | int idx = x509_object_idx_cnt(ctx->ctx->objs, X509_LU_X509, nm, &cnt); |
377 | 0 | if (idx < 0) { |
378 | | // Nothing found in cache: do lookup to possibly add new objects to |
379 | | // cache |
380 | 0 | X509_OBJECT xobj; |
381 | 0 | CRYPTO_MUTEX_unlock_write(&ctx->ctx->objs_lock); |
382 | 0 | if (!X509_STORE_CTX_get_by_subject(ctx, X509_LU_X509, nm, &xobj)) { |
383 | 0 | sk_X509_free(sk); |
384 | 0 | return NULL; |
385 | 0 | } |
386 | 0 | X509_OBJECT_free_contents(&xobj); |
387 | 0 | CRYPTO_MUTEX_lock_write(&ctx->ctx->objs_lock); |
388 | 0 | idx = x509_object_idx_cnt(ctx->ctx->objs, X509_LU_X509, nm, &cnt); |
389 | 0 | if (idx < 0) { |
390 | 0 | CRYPTO_MUTEX_unlock_write(&ctx->ctx->objs_lock); |
391 | 0 | sk_X509_free(sk); |
392 | 0 | return NULL; |
393 | 0 | } |
394 | 0 | } |
395 | 0 | for (int i = 0; i < cnt; i++, idx++) { |
396 | 0 | X509_OBJECT *obj = sk_X509_OBJECT_value(ctx->ctx->objs, idx); |
397 | 0 | X509 *x = obj->data.x509; |
398 | 0 | if (!sk_X509_push(sk, x)) { |
399 | 0 | CRYPTO_MUTEX_unlock_write(&ctx->ctx->objs_lock); |
400 | 0 | sk_X509_pop_free(sk, X509_free); |
401 | 0 | return NULL; |
402 | 0 | } |
403 | 0 | X509_up_ref(x); |
404 | 0 | } |
405 | 0 | CRYPTO_MUTEX_unlock_write(&ctx->ctx->objs_lock); |
406 | 0 | return sk; |
407 | 0 | } |
408 | | |
409 | | STACK_OF(X509_CRL) *X509_STORE_CTX_get1_crls(X509_STORE_CTX *ctx, |
410 | 0 | X509_NAME *nm) { |
411 | 0 | int cnt; |
412 | 0 | X509_OBJECT xobj; |
413 | 0 | STACK_OF(X509_CRL) *sk = sk_X509_CRL_new_null(); |
414 | 0 | if (sk == NULL) { |
415 | 0 | return NULL; |
416 | 0 | } |
417 | | |
418 | | // Always do lookup to possibly add new CRLs to cache. |
419 | 0 | if (!X509_STORE_CTX_get_by_subject(ctx, X509_LU_CRL, nm, &xobj)) { |
420 | 0 | sk_X509_CRL_free(sk); |
421 | 0 | return NULL; |
422 | 0 | } |
423 | 0 | X509_OBJECT_free_contents(&xobj); |
424 | 0 | CRYPTO_MUTEX_lock_write(&ctx->ctx->objs_lock); |
425 | 0 | int idx = x509_object_idx_cnt(ctx->ctx->objs, X509_LU_CRL, nm, &cnt); |
426 | 0 | if (idx < 0) { |
427 | 0 | CRYPTO_MUTEX_unlock_write(&ctx->ctx->objs_lock); |
428 | 0 | sk_X509_CRL_free(sk); |
429 | 0 | return NULL; |
430 | 0 | } |
431 | | |
432 | 0 | for (int i = 0; i < cnt; i++, idx++) { |
433 | 0 | X509_OBJECT *obj = sk_X509_OBJECT_value(ctx->ctx->objs, idx); |
434 | 0 | X509_CRL *x = obj->data.crl; |
435 | 0 | X509_CRL_up_ref(x); |
436 | 0 | if (!sk_X509_CRL_push(sk, x)) { |
437 | 0 | CRYPTO_MUTEX_unlock_write(&ctx->ctx->objs_lock); |
438 | 0 | X509_CRL_free(x); |
439 | 0 | sk_X509_CRL_pop_free(sk, X509_CRL_free); |
440 | 0 | return NULL; |
441 | 0 | } |
442 | 0 | } |
443 | 0 | CRYPTO_MUTEX_unlock_write(&ctx->ctx->objs_lock); |
444 | 0 | return sk; |
445 | 0 | } |
446 | | |
447 | | static X509_OBJECT *X509_OBJECT_retrieve_match(STACK_OF(X509_OBJECT) *h, |
448 | 0 | X509_OBJECT *x) { |
449 | 0 | sk_X509_OBJECT_sort(h); |
450 | 0 | size_t idx; |
451 | 0 | if (!sk_X509_OBJECT_find(h, &idx, x)) { |
452 | 0 | return NULL; |
453 | 0 | } |
454 | 0 | if ((x->type != X509_LU_X509) && (x->type != X509_LU_CRL)) { |
455 | 0 | return sk_X509_OBJECT_value(h, idx); |
456 | 0 | } |
457 | 0 | for (size_t i = idx; i < sk_X509_OBJECT_num(h); i++) { |
458 | 0 | X509_OBJECT *obj = sk_X509_OBJECT_value(h, i); |
459 | 0 | if (x509_object_cmp(obj, x)) { |
460 | 0 | return NULL; |
461 | 0 | } |
462 | 0 | if (x->type == X509_LU_X509) { |
463 | 0 | if (!X509_cmp(obj->data.x509, x->data.x509)) { |
464 | 0 | return obj; |
465 | 0 | } |
466 | 0 | } else if (x->type == X509_LU_CRL) { |
467 | 0 | if (!X509_CRL_match(obj->data.crl, x->data.crl)) { |
468 | 0 | return obj; |
469 | 0 | } |
470 | 0 | } else { |
471 | 0 | return obj; |
472 | 0 | } |
473 | 0 | } |
474 | 0 | return NULL; |
475 | 0 | } |
476 | | |
477 | | int X509_STORE_CTX_get1_issuer(X509 **out_issuer, X509_STORE_CTX *ctx, |
478 | 0 | X509 *x) { |
479 | 0 | X509_NAME *xn; |
480 | 0 | X509_OBJECT obj, *pobj; |
481 | 0 | int idx, ret; |
482 | 0 | size_t i; |
483 | 0 | xn = X509_get_issuer_name(x); |
484 | 0 | if (!X509_STORE_CTX_get_by_subject(ctx, X509_LU_X509, xn, &obj)) { |
485 | 0 | return 0; |
486 | 0 | } |
487 | | // If certificate matches all OK |
488 | 0 | if (x509_check_issued_with_callback(ctx, x, obj.data.x509)) { |
489 | 0 | *out_issuer = obj.data.x509; |
490 | 0 | return 1; |
491 | 0 | } |
492 | 0 | X509_OBJECT_free_contents(&obj); |
493 | | |
494 | | // Else find index of first cert accepted by |
495 | | // |x509_check_issued_with_callback|. |
496 | 0 | ret = 0; |
497 | 0 | CRYPTO_MUTEX_lock_write(&ctx->ctx->objs_lock); |
498 | 0 | idx = X509_OBJECT_idx_by_subject(ctx->ctx->objs, X509_LU_X509, xn); |
499 | 0 | if (idx != -1) { // should be true as we've had at least one |
500 | | // match |
501 | | // Look through all matching certs for suitable issuer |
502 | 0 | for (i = idx; i < sk_X509_OBJECT_num(ctx->ctx->objs); i++) { |
503 | 0 | pobj = sk_X509_OBJECT_value(ctx->ctx->objs, i); |
504 | | // See if we've run past the matches |
505 | 0 | if (pobj->type != X509_LU_X509) { |
506 | 0 | break; |
507 | 0 | } |
508 | 0 | if (X509_NAME_cmp(xn, X509_get_subject_name(pobj->data.x509))) { |
509 | 0 | break; |
510 | 0 | } |
511 | 0 | if (x509_check_issued_with_callback(ctx, x, pobj->data.x509)) { |
512 | 0 | *out_issuer = pobj->data.x509; |
513 | 0 | X509_OBJECT_up_ref_count(pobj); |
514 | 0 | ret = 1; |
515 | 0 | break; |
516 | 0 | } |
517 | 0 | } |
518 | 0 | } |
519 | 0 | CRYPTO_MUTEX_unlock_write(&ctx->ctx->objs_lock); |
520 | 0 | return ret; |
521 | 0 | } |
522 | | |
523 | 0 | int X509_STORE_set_flags(X509_STORE *ctx, unsigned long flags) { |
524 | 0 | return X509_VERIFY_PARAM_set_flags(ctx->param, flags); |
525 | 0 | } |
526 | | |
527 | 0 | int X509_STORE_set_depth(X509_STORE *ctx, int depth) { |
528 | 0 | X509_VERIFY_PARAM_set_depth(ctx->param, depth); |
529 | 0 | return 1; |
530 | 0 | } |
531 | | |
532 | 0 | int X509_STORE_set_purpose(X509_STORE *ctx, int purpose) { |
533 | 0 | return X509_VERIFY_PARAM_set_purpose(ctx->param, purpose); |
534 | 0 | } |
535 | | |
536 | 0 | int X509_STORE_set_trust(X509_STORE *ctx, int trust) { |
537 | 0 | return X509_VERIFY_PARAM_set_trust(ctx->param, trust); |
538 | 0 | } |
539 | | |
540 | 0 | int X509_STORE_set1_param(X509_STORE *ctx, const X509_VERIFY_PARAM *param) { |
541 | 0 | return X509_VERIFY_PARAM_set1(ctx->param, param); |
542 | 0 | } |
543 | | |
544 | 0 | X509_VERIFY_PARAM *X509_STORE_get0_param(X509_STORE *ctx) { return ctx->param; } |
545 | | |
546 | | void X509_STORE_set_verify_cb(X509_STORE *ctx, |
547 | 0 | X509_STORE_CTX_verify_cb verify_cb) { |
548 | 0 | ctx->verify_cb = verify_cb; |
549 | 0 | } |
550 | | |
551 | 0 | X509_STORE *X509_STORE_CTX_get0_store(const X509_STORE_CTX *ctx) { |
552 | 0 | return ctx->ctx; |
553 | 0 | } |