/src/irssi/subprojects/openssl-1.1.1l/crypto/x509/x509_lu.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2019 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 <stdio.h> |
11 | | #include "internal/cryptlib.h" |
12 | | #include "internal/refcount.h" |
13 | | #include <openssl/x509.h> |
14 | | #include "crypto/x509.h" |
15 | | #include <openssl/x509v3.h> |
16 | | #include "x509_local.h" |
17 | | |
18 | | X509_LOOKUP *X509_LOOKUP_new(X509_LOOKUP_METHOD *method) |
19 | 2 | { |
20 | 2 | X509_LOOKUP *ret = OPENSSL_zalloc(sizeof(*ret)); |
21 | | |
22 | 2 | if (ret == NULL) { |
23 | 0 | X509err(X509_F_X509_LOOKUP_NEW, ERR_R_MALLOC_FAILURE); |
24 | 0 | return NULL; |
25 | 0 | } |
26 | | |
27 | 2 | ret->method = method; |
28 | 2 | if (method->new_item != NULL && method->new_item(ret) == 0) { |
29 | 0 | OPENSSL_free(ret); |
30 | 0 | return NULL; |
31 | 0 | } |
32 | 2 | return ret; |
33 | 2 | } |
34 | | |
35 | | void X509_LOOKUP_free(X509_LOOKUP *ctx) |
36 | 0 | { |
37 | 0 | if (ctx == NULL) |
38 | 0 | return; |
39 | 0 | if ((ctx->method != NULL) && (ctx->method->free != NULL)) |
40 | 0 | (*ctx->method->free) (ctx); |
41 | 0 | OPENSSL_free(ctx); |
42 | 0 | } |
43 | | |
44 | | int X509_STORE_lock(X509_STORE *s) |
45 | 0 | { |
46 | 0 | return CRYPTO_THREAD_write_lock(s->lock); |
47 | 0 | } |
48 | | |
49 | | int X509_STORE_unlock(X509_STORE *s) |
50 | 0 | { |
51 | 0 | return CRYPTO_THREAD_unlock(s->lock); |
52 | 0 | } |
53 | | |
54 | | int X509_LOOKUP_init(X509_LOOKUP *ctx) |
55 | 0 | { |
56 | 0 | if (ctx->method == NULL) |
57 | 0 | return 0; |
58 | 0 | if (ctx->method->init != NULL) |
59 | 0 | return ctx->method->init(ctx); |
60 | 0 | else |
61 | 0 | return 1; |
62 | 0 | } |
63 | | |
64 | | int X509_LOOKUP_shutdown(X509_LOOKUP *ctx) |
65 | 0 | { |
66 | 0 | if (ctx->method == NULL) |
67 | 0 | return 0; |
68 | 0 | if (ctx->method->shutdown != NULL) |
69 | 0 | return ctx->method->shutdown(ctx); |
70 | 0 | else |
71 | 0 | return 1; |
72 | 0 | } |
73 | | |
74 | | int X509_LOOKUP_ctrl(X509_LOOKUP *ctx, int cmd, const char *argc, long argl, |
75 | | char **ret) |
76 | 2 | { |
77 | 2 | if (ctx->method == NULL) |
78 | 0 | return -1; |
79 | 2 | if (ctx->method->ctrl != NULL) |
80 | 2 | return ctx->method->ctrl(ctx, cmd, argc, argl, ret); |
81 | 0 | else |
82 | 0 | return 1; |
83 | 2 | } |
84 | | |
85 | | int X509_LOOKUP_by_subject(X509_LOOKUP *ctx, X509_LOOKUP_TYPE type, |
86 | | X509_NAME *name, X509_OBJECT *ret) |
87 | 0 | { |
88 | 0 | if ((ctx->method == NULL) || (ctx->method->get_by_subject == NULL)) |
89 | 0 | return 0; |
90 | 0 | if (ctx->skip) |
91 | 0 | return 0; |
92 | 0 | return ctx->method->get_by_subject(ctx, type, name, ret); |
93 | 0 | } |
94 | | |
95 | | int X509_LOOKUP_by_issuer_serial(X509_LOOKUP *ctx, X509_LOOKUP_TYPE type, |
96 | | X509_NAME *name, ASN1_INTEGER *serial, |
97 | | X509_OBJECT *ret) |
98 | 0 | { |
99 | 0 | if ((ctx->method == NULL) || (ctx->method->get_by_issuer_serial == NULL)) |
100 | 0 | return 0; |
101 | 0 | return ctx->method->get_by_issuer_serial(ctx, type, name, serial, ret); |
102 | 0 | } |
103 | | |
104 | | int X509_LOOKUP_by_fingerprint(X509_LOOKUP *ctx, X509_LOOKUP_TYPE type, |
105 | | const unsigned char *bytes, int len, |
106 | | X509_OBJECT *ret) |
107 | 0 | { |
108 | 0 | if ((ctx->method == NULL) || (ctx->method->get_by_fingerprint == NULL)) |
109 | 0 | return 0; |
110 | 0 | return ctx->method->get_by_fingerprint(ctx, type, bytes, len, ret); |
111 | 0 | } |
112 | | |
113 | | int X509_LOOKUP_by_alias(X509_LOOKUP *ctx, X509_LOOKUP_TYPE type, |
114 | | const char *str, int len, X509_OBJECT *ret) |
115 | 0 | { |
116 | 0 | if ((ctx->method == NULL) || (ctx->method->get_by_alias == NULL)) |
117 | 0 | return 0; |
118 | 0 | return ctx->method->get_by_alias(ctx, type, str, len, ret); |
119 | 0 | } |
120 | | |
121 | | int X509_LOOKUP_set_method_data(X509_LOOKUP *ctx, void *data) |
122 | 0 | { |
123 | 0 | ctx->method_data = data; |
124 | 0 | return 1; |
125 | 0 | } |
126 | | |
127 | | void *X509_LOOKUP_get_method_data(const X509_LOOKUP *ctx) |
128 | 0 | { |
129 | 0 | return ctx->method_data; |
130 | 0 | } |
131 | | |
132 | | X509_STORE *X509_LOOKUP_get_store(const X509_LOOKUP *ctx) |
133 | 0 | { |
134 | 0 | return ctx->store_ctx; |
135 | 0 | } |
136 | | |
137 | | |
138 | | static int x509_object_cmp(const X509_OBJECT *const *a, |
139 | | const X509_OBJECT *const *b) |
140 | 0 | { |
141 | 0 | int ret; |
142 | |
|
143 | 0 | ret = ((*a)->type - (*b)->type); |
144 | 0 | if (ret) |
145 | 0 | return ret; |
146 | 0 | switch ((*a)->type) { |
147 | 0 | case X509_LU_X509: |
148 | 0 | ret = X509_subject_name_cmp((*a)->data.x509, (*b)->data.x509); |
149 | 0 | break; |
150 | 0 | case X509_LU_CRL: |
151 | 0 | ret = X509_CRL_cmp((*a)->data.crl, (*b)->data.crl); |
152 | 0 | break; |
153 | 0 | case X509_LU_NONE: |
154 | | /* abort(); */ |
155 | 0 | return 0; |
156 | 0 | } |
157 | 0 | return ret; |
158 | 0 | } |
159 | | |
160 | | X509_STORE *X509_STORE_new(void) |
161 | 1 | { |
162 | 1 | X509_STORE *ret = OPENSSL_zalloc(sizeof(*ret)); |
163 | | |
164 | 1 | if (ret == NULL) { |
165 | 0 | X509err(X509_F_X509_STORE_NEW, ERR_R_MALLOC_FAILURE); |
166 | 0 | return NULL; |
167 | 0 | } |
168 | 1 | if ((ret->objs = sk_X509_OBJECT_new(x509_object_cmp)) == NULL) { |
169 | 0 | X509err(X509_F_X509_STORE_NEW, ERR_R_MALLOC_FAILURE); |
170 | 0 | goto err; |
171 | 0 | } |
172 | 1 | ret->cache = 1; |
173 | 1 | if ((ret->get_cert_methods = sk_X509_LOOKUP_new_null()) == NULL) { |
174 | 0 | X509err(X509_F_X509_STORE_NEW, ERR_R_MALLOC_FAILURE); |
175 | 0 | goto err; |
176 | 0 | } |
177 | | |
178 | 1 | if ((ret->param = X509_VERIFY_PARAM_new()) == NULL) { |
179 | 0 | X509err(X509_F_X509_STORE_NEW, ERR_R_MALLOC_FAILURE); |
180 | 0 | goto err; |
181 | 0 | } |
182 | 1 | if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509_STORE, ret, &ret->ex_data)) { |
183 | 0 | X509err(X509_F_X509_STORE_NEW, ERR_R_MALLOC_FAILURE); |
184 | 0 | goto err; |
185 | 0 | } |
186 | | |
187 | 1 | ret->lock = CRYPTO_THREAD_lock_new(); |
188 | 1 | if (ret->lock == NULL) { |
189 | 0 | X509err(X509_F_X509_STORE_NEW, ERR_R_MALLOC_FAILURE); |
190 | 0 | goto err; |
191 | 0 | } |
192 | | |
193 | 1 | ret->references = 1; |
194 | 1 | return ret; |
195 | | |
196 | 0 | err: |
197 | 0 | X509_VERIFY_PARAM_free(ret->param); |
198 | 0 | sk_X509_OBJECT_free(ret->objs); |
199 | 0 | sk_X509_LOOKUP_free(ret->get_cert_methods); |
200 | 0 | OPENSSL_free(ret); |
201 | 0 | return NULL; |
202 | 1 | } |
203 | | |
204 | | void X509_STORE_free(X509_STORE *vfy) |
205 | 0 | { |
206 | 0 | int i; |
207 | 0 | STACK_OF(X509_LOOKUP) *sk; |
208 | 0 | X509_LOOKUP *lu; |
209 | |
|
210 | 0 | if (vfy == NULL) |
211 | 0 | return; |
212 | 0 | CRYPTO_DOWN_REF(&vfy->references, &i, vfy->lock); |
213 | 0 | REF_PRINT_COUNT("X509_STORE", vfy); |
214 | 0 | if (i > 0) |
215 | 0 | return; |
216 | 0 | REF_ASSERT_ISNT(i < 0); |
217 | |
|
218 | 0 | sk = vfy->get_cert_methods; |
219 | 0 | for (i = 0; i < sk_X509_LOOKUP_num(sk); i++) { |
220 | 0 | lu = sk_X509_LOOKUP_value(sk, i); |
221 | 0 | X509_LOOKUP_shutdown(lu); |
222 | 0 | X509_LOOKUP_free(lu); |
223 | 0 | } |
224 | 0 | sk_X509_LOOKUP_free(sk); |
225 | 0 | sk_X509_OBJECT_pop_free(vfy->objs, X509_OBJECT_free); |
226 | |
|
227 | 0 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509_STORE, vfy, &vfy->ex_data); |
228 | 0 | X509_VERIFY_PARAM_free(vfy->param); |
229 | 0 | CRYPTO_THREAD_lock_free(vfy->lock); |
230 | 0 | OPENSSL_free(vfy); |
231 | 0 | } |
232 | | |
233 | | int X509_STORE_up_ref(X509_STORE *vfy) |
234 | 0 | { |
235 | 0 | int i; |
236 | |
|
237 | 0 | if (CRYPTO_UP_REF(&vfy->references, &i, vfy->lock) <= 0) |
238 | 0 | return 0; |
239 | | |
240 | 0 | REF_PRINT_COUNT("X509_STORE", a); |
241 | 0 | REF_ASSERT_ISNT(i < 2); |
242 | 0 | return ((i > 1) ? 1 : 0); |
243 | 0 | } |
244 | | |
245 | | X509_LOOKUP *X509_STORE_add_lookup(X509_STORE *v, X509_LOOKUP_METHOD *m) |
246 | 2 | { |
247 | 2 | int i; |
248 | 2 | STACK_OF(X509_LOOKUP) *sk; |
249 | 2 | X509_LOOKUP *lu; |
250 | | |
251 | 2 | sk = v->get_cert_methods; |
252 | 3 | for (i = 0; i < sk_X509_LOOKUP_num(sk); i++) { |
253 | 1 | lu = sk_X509_LOOKUP_value(sk, i); |
254 | 1 | if (m == lu->method) { |
255 | 0 | return lu; |
256 | 0 | } |
257 | 1 | } |
258 | | /* a new one */ |
259 | 2 | lu = X509_LOOKUP_new(m); |
260 | 2 | if (lu == NULL) { |
261 | 0 | X509err(X509_F_X509_STORE_ADD_LOOKUP, ERR_R_MALLOC_FAILURE); |
262 | 0 | return NULL; |
263 | 0 | } |
264 | | |
265 | 2 | lu->store_ctx = v; |
266 | 2 | if (sk_X509_LOOKUP_push(v->get_cert_methods, lu)) |
267 | 2 | return lu; |
268 | | /* malloc failed */ |
269 | 0 | X509err(X509_F_X509_STORE_ADD_LOOKUP, ERR_R_MALLOC_FAILURE); |
270 | 0 | X509_LOOKUP_free(lu); |
271 | 0 | return NULL; |
272 | 2 | } |
273 | | |
274 | | X509_OBJECT *X509_STORE_CTX_get_obj_by_subject(X509_STORE_CTX *vs, |
275 | | X509_LOOKUP_TYPE type, |
276 | | X509_NAME *name) |
277 | 0 | { |
278 | 0 | X509_OBJECT *ret = X509_OBJECT_new(); |
279 | |
|
280 | 0 | if (ret == NULL) |
281 | 0 | return NULL; |
282 | 0 | if (!X509_STORE_CTX_get_by_subject(vs, type, name, ret)) { |
283 | 0 | X509_OBJECT_free(ret); |
284 | 0 | return NULL; |
285 | 0 | } |
286 | 0 | return ret; |
287 | 0 | } |
288 | | |
289 | | int X509_STORE_CTX_get_by_subject(X509_STORE_CTX *vs, X509_LOOKUP_TYPE type, |
290 | | X509_NAME *name, X509_OBJECT *ret) |
291 | 0 | { |
292 | 0 | X509_STORE *store = vs->ctx; |
293 | 0 | X509_LOOKUP *lu; |
294 | 0 | X509_OBJECT stmp, *tmp; |
295 | 0 | int i, j; |
296 | |
|
297 | 0 | if (store == NULL) |
298 | 0 | return 0; |
299 | | |
300 | 0 | stmp.type = X509_LU_NONE; |
301 | 0 | stmp.data.ptr = NULL; |
302 | | |
303 | |
|
304 | 0 | X509_STORE_lock(store); |
305 | 0 | tmp = X509_OBJECT_retrieve_by_subject(store->objs, type, name); |
306 | 0 | X509_STORE_unlock(store); |
307 | |
|
308 | 0 | if (tmp == NULL || type == X509_LU_CRL) { |
309 | 0 | for (i = 0; i < sk_X509_LOOKUP_num(store->get_cert_methods); i++) { |
310 | 0 | lu = sk_X509_LOOKUP_value(store->get_cert_methods, i); |
311 | 0 | j = X509_LOOKUP_by_subject(lu, type, name, &stmp); |
312 | 0 | if (j) { |
313 | 0 | tmp = &stmp; |
314 | 0 | break; |
315 | 0 | } |
316 | 0 | } |
317 | 0 | if (tmp == NULL) |
318 | 0 | return 0; |
319 | 0 | } |
320 | | |
321 | 0 | if (!X509_OBJECT_up_ref_count(tmp)) |
322 | 0 | return 0; |
323 | | |
324 | 0 | ret->type = tmp->type; |
325 | 0 | ret->data.ptr = tmp->data.ptr; |
326 | |
|
327 | 0 | return 1; |
328 | 0 | } |
329 | | |
330 | 0 | static int x509_store_add(X509_STORE *store, void *x, int crl) { |
331 | 0 | X509_OBJECT *obj; |
332 | 0 | int ret = 0, added = 0; |
333 | |
|
334 | 0 | if (x == NULL) |
335 | 0 | return 0; |
336 | 0 | obj = X509_OBJECT_new(); |
337 | 0 | if (obj == NULL) |
338 | 0 | return 0; |
339 | | |
340 | 0 | if (crl) { |
341 | 0 | obj->type = X509_LU_CRL; |
342 | 0 | obj->data.crl = (X509_CRL *)x; |
343 | 0 | } else { |
344 | 0 | obj->type = X509_LU_X509; |
345 | 0 | obj->data.x509 = (X509 *)x; |
346 | 0 | } |
347 | 0 | if (!X509_OBJECT_up_ref_count(obj)) { |
348 | 0 | obj->type = X509_LU_NONE; |
349 | 0 | X509_OBJECT_free(obj); |
350 | 0 | return 0; |
351 | 0 | } |
352 | | |
353 | 0 | X509_STORE_lock(store); |
354 | 0 | if (X509_OBJECT_retrieve_match(store->objs, obj)) { |
355 | 0 | ret = 1; |
356 | 0 | } else { |
357 | 0 | added = sk_X509_OBJECT_push(store->objs, obj); |
358 | 0 | ret = added != 0; |
359 | 0 | } |
360 | 0 | X509_STORE_unlock(store); |
361 | |
|
362 | 0 | if (added == 0) /* obj not pushed */ |
363 | 0 | X509_OBJECT_free(obj); |
364 | |
|
365 | 0 | return ret; |
366 | 0 | } |
367 | | |
368 | | int X509_STORE_add_cert(X509_STORE *ctx, X509 *x) |
369 | 0 | { |
370 | 0 | if (!x509_store_add(ctx, x, 0)) { |
371 | 0 | X509err(X509_F_X509_STORE_ADD_CERT, ERR_R_MALLOC_FAILURE); |
372 | 0 | return 0; |
373 | 0 | } |
374 | 0 | return 1; |
375 | 0 | } |
376 | | |
377 | | int X509_STORE_add_crl(X509_STORE *ctx, X509_CRL *x) |
378 | 0 | { |
379 | 0 | if (!x509_store_add(ctx, x, 1)) { |
380 | 0 | X509err(X509_F_X509_STORE_ADD_CRL, ERR_R_MALLOC_FAILURE); |
381 | 0 | return 0; |
382 | 0 | } |
383 | 0 | return 1; |
384 | 0 | } |
385 | | |
386 | | int X509_OBJECT_up_ref_count(X509_OBJECT *a) |
387 | 0 | { |
388 | 0 | switch (a->type) { |
389 | 0 | case X509_LU_NONE: |
390 | 0 | break; |
391 | 0 | case X509_LU_X509: |
392 | 0 | return X509_up_ref(a->data.x509); |
393 | 0 | case X509_LU_CRL: |
394 | 0 | return X509_CRL_up_ref(a->data.crl); |
395 | 0 | } |
396 | 0 | return 1; |
397 | 0 | } |
398 | | |
399 | | X509 *X509_OBJECT_get0_X509(const X509_OBJECT *a) |
400 | 0 | { |
401 | 0 | if (a == NULL || a->type != X509_LU_X509) |
402 | 0 | return NULL; |
403 | 0 | return a->data.x509; |
404 | 0 | } |
405 | | |
406 | | X509_CRL *X509_OBJECT_get0_X509_CRL(X509_OBJECT *a) |
407 | 0 | { |
408 | 0 | if (a == NULL || a->type != X509_LU_CRL) |
409 | 0 | return NULL; |
410 | 0 | return a->data.crl; |
411 | 0 | } |
412 | | |
413 | | X509_LOOKUP_TYPE X509_OBJECT_get_type(const X509_OBJECT *a) |
414 | 0 | { |
415 | 0 | return a->type; |
416 | 0 | } |
417 | | |
418 | | X509_OBJECT *X509_OBJECT_new(void) |
419 | 0 | { |
420 | 0 | X509_OBJECT *ret = OPENSSL_zalloc(sizeof(*ret)); |
421 | |
|
422 | 0 | if (ret == NULL) { |
423 | 0 | X509err(X509_F_X509_OBJECT_NEW, ERR_R_MALLOC_FAILURE); |
424 | 0 | return NULL; |
425 | 0 | } |
426 | 0 | ret->type = X509_LU_NONE; |
427 | 0 | return ret; |
428 | 0 | } |
429 | | |
430 | | static void x509_object_free_internal(X509_OBJECT *a) |
431 | 0 | { |
432 | 0 | if (a == NULL) |
433 | 0 | return; |
434 | 0 | switch (a->type) { |
435 | 0 | case X509_LU_NONE: |
436 | 0 | break; |
437 | 0 | case X509_LU_X509: |
438 | 0 | X509_free(a->data.x509); |
439 | 0 | break; |
440 | 0 | case X509_LU_CRL: |
441 | 0 | X509_CRL_free(a->data.crl); |
442 | 0 | break; |
443 | 0 | } |
444 | 0 | } |
445 | | |
446 | | int X509_OBJECT_set1_X509(X509_OBJECT *a, X509 *obj) |
447 | 0 | { |
448 | 0 | if (a == NULL || !X509_up_ref(obj)) |
449 | 0 | return 0; |
450 | | |
451 | 0 | x509_object_free_internal(a); |
452 | 0 | a->type = X509_LU_X509; |
453 | 0 | a->data.x509 = obj; |
454 | 0 | return 1; |
455 | 0 | } |
456 | | |
457 | | int X509_OBJECT_set1_X509_CRL(X509_OBJECT *a, X509_CRL *obj) |
458 | 0 | { |
459 | 0 | if (a == NULL || !X509_CRL_up_ref(obj)) |
460 | 0 | return 0; |
461 | | |
462 | 0 | x509_object_free_internal(a); |
463 | 0 | a->type = X509_LU_CRL; |
464 | 0 | a->data.crl = obj; |
465 | 0 | return 1; |
466 | 0 | } |
467 | | |
468 | | void X509_OBJECT_free(X509_OBJECT *a) |
469 | 0 | { |
470 | 0 | x509_object_free_internal(a); |
471 | 0 | OPENSSL_free(a); |
472 | 0 | } |
473 | | |
474 | | static int x509_object_idx_cnt(STACK_OF(X509_OBJECT) *h, X509_LOOKUP_TYPE type, |
475 | | X509_NAME *name, int *pnmatch) |
476 | 0 | { |
477 | 0 | X509_OBJECT stmp; |
478 | 0 | X509 x509_s; |
479 | 0 | X509_CRL crl_s; |
480 | 0 | int idx; |
481 | |
|
482 | 0 | stmp.type = type; |
483 | 0 | switch (type) { |
484 | 0 | case X509_LU_X509: |
485 | 0 | stmp.data.x509 = &x509_s; |
486 | 0 | x509_s.cert_info.subject = name; |
487 | 0 | break; |
488 | 0 | case X509_LU_CRL: |
489 | 0 | stmp.data.crl = &crl_s; |
490 | 0 | crl_s.crl.issuer = name; |
491 | 0 | break; |
492 | 0 | case X509_LU_NONE: |
493 | | /* abort(); */ |
494 | 0 | return -1; |
495 | 0 | } |
496 | | |
497 | 0 | idx = sk_X509_OBJECT_find(h, &stmp); |
498 | 0 | if (idx >= 0 && pnmatch) { |
499 | 0 | int tidx; |
500 | 0 | const X509_OBJECT *tobj, *pstmp; |
501 | 0 | *pnmatch = 1; |
502 | 0 | pstmp = &stmp; |
503 | 0 | for (tidx = idx + 1; tidx < sk_X509_OBJECT_num(h); tidx++) { |
504 | 0 | tobj = sk_X509_OBJECT_value(h, tidx); |
505 | 0 | if (x509_object_cmp(&tobj, &pstmp)) |
506 | 0 | break; |
507 | 0 | (*pnmatch)++; |
508 | 0 | } |
509 | 0 | } |
510 | 0 | return idx; |
511 | 0 | } |
512 | | |
513 | | int X509_OBJECT_idx_by_subject(STACK_OF(X509_OBJECT) *h, X509_LOOKUP_TYPE type, |
514 | | X509_NAME *name) |
515 | 0 | { |
516 | 0 | return x509_object_idx_cnt(h, type, name, NULL); |
517 | 0 | } |
518 | | |
519 | | X509_OBJECT *X509_OBJECT_retrieve_by_subject(STACK_OF(X509_OBJECT) *h, |
520 | | X509_LOOKUP_TYPE type, |
521 | | X509_NAME *name) |
522 | 0 | { |
523 | 0 | int idx; |
524 | 0 | idx = X509_OBJECT_idx_by_subject(h, type, name); |
525 | 0 | if (idx == -1) |
526 | 0 | return NULL; |
527 | 0 | return sk_X509_OBJECT_value(h, idx); |
528 | 0 | } |
529 | | |
530 | | STACK_OF(X509_OBJECT) *X509_STORE_get0_objects(X509_STORE *v) |
531 | 0 | { |
532 | 0 | return v->objs; |
533 | 0 | } |
534 | | |
535 | | STACK_OF(X509) *X509_STORE_CTX_get1_certs(X509_STORE_CTX *ctx, X509_NAME *nm) |
536 | 0 | { |
537 | 0 | int i, idx, cnt; |
538 | 0 | STACK_OF(X509) *sk = NULL; |
539 | 0 | X509 *x; |
540 | 0 | X509_OBJECT *obj; |
541 | 0 | X509_STORE *store = ctx->ctx; |
542 | |
|
543 | 0 | if (store == NULL) |
544 | 0 | return NULL; |
545 | | |
546 | 0 | X509_STORE_lock(store); |
547 | 0 | idx = x509_object_idx_cnt(store->objs, X509_LU_X509, nm, &cnt); |
548 | 0 | if (idx < 0) { |
549 | | /* |
550 | | * Nothing found in cache: do lookup to possibly add new objects to |
551 | | * cache |
552 | | */ |
553 | 0 | X509_OBJECT *xobj = X509_OBJECT_new(); |
554 | |
|
555 | 0 | X509_STORE_unlock(store); |
556 | |
|
557 | 0 | if (xobj == NULL) |
558 | 0 | return NULL; |
559 | 0 | if (!X509_STORE_CTX_get_by_subject(ctx, X509_LU_X509, nm, xobj)) { |
560 | 0 | X509_OBJECT_free(xobj); |
561 | 0 | return NULL; |
562 | 0 | } |
563 | 0 | X509_OBJECT_free(xobj); |
564 | 0 | X509_STORE_lock(store); |
565 | 0 | idx = x509_object_idx_cnt(store->objs, X509_LU_X509, nm, &cnt); |
566 | 0 | if (idx < 0) { |
567 | 0 | X509_STORE_unlock(store); |
568 | 0 | return NULL; |
569 | 0 | } |
570 | 0 | } |
571 | | |
572 | 0 | sk = sk_X509_new_null(); |
573 | 0 | for (i = 0; i < cnt; i++, idx++) { |
574 | 0 | obj = sk_X509_OBJECT_value(store->objs, idx); |
575 | 0 | x = obj->data.x509; |
576 | 0 | if (!X509_up_ref(x)) { |
577 | 0 | X509_STORE_unlock(store); |
578 | 0 | sk_X509_pop_free(sk, X509_free); |
579 | 0 | return NULL; |
580 | 0 | } |
581 | 0 | if (!sk_X509_push(sk, x)) { |
582 | 0 | X509_STORE_unlock(store); |
583 | 0 | X509_free(x); |
584 | 0 | sk_X509_pop_free(sk, X509_free); |
585 | 0 | return NULL; |
586 | 0 | } |
587 | 0 | } |
588 | 0 | X509_STORE_unlock(store); |
589 | 0 | return sk; |
590 | 0 | } |
591 | | |
592 | | STACK_OF(X509_CRL) *X509_STORE_CTX_get1_crls(X509_STORE_CTX *ctx, X509_NAME *nm) |
593 | 0 | { |
594 | 0 | int i, idx, cnt; |
595 | 0 | STACK_OF(X509_CRL) *sk = sk_X509_CRL_new_null(); |
596 | 0 | X509_CRL *x; |
597 | 0 | X509_OBJECT *obj, *xobj = X509_OBJECT_new(); |
598 | 0 | X509_STORE *store = ctx->ctx; |
599 | | |
600 | | /* Always do lookup to possibly add new CRLs to cache */ |
601 | 0 | if (sk == NULL |
602 | 0 | || xobj == NULL |
603 | 0 | || store == NULL |
604 | 0 | || !X509_STORE_CTX_get_by_subject(ctx, X509_LU_CRL, nm, xobj)) { |
605 | 0 | X509_OBJECT_free(xobj); |
606 | 0 | sk_X509_CRL_free(sk); |
607 | 0 | return NULL; |
608 | 0 | } |
609 | 0 | X509_OBJECT_free(xobj); |
610 | 0 | X509_STORE_lock(store); |
611 | 0 | idx = x509_object_idx_cnt(store->objs, X509_LU_CRL, nm, &cnt); |
612 | 0 | if (idx < 0) { |
613 | 0 | X509_STORE_unlock(store); |
614 | 0 | sk_X509_CRL_free(sk); |
615 | 0 | return NULL; |
616 | 0 | } |
617 | | |
618 | 0 | for (i = 0; i < cnt; i++, idx++) { |
619 | 0 | obj = sk_X509_OBJECT_value(store->objs, idx); |
620 | 0 | x = obj->data.crl; |
621 | 0 | if (!X509_CRL_up_ref(x)) { |
622 | 0 | X509_STORE_unlock(store); |
623 | 0 | sk_X509_CRL_pop_free(sk, X509_CRL_free); |
624 | 0 | return NULL; |
625 | 0 | } |
626 | 0 | if (!sk_X509_CRL_push(sk, x)) { |
627 | 0 | X509_STORE_unlock(store); |
628 | 0 | X509_CRL_free(x); |
629 | 0 | sk_X509_CRL_pop_free(sk, X509_CRL_free); |
630 | 0 | return NULL; |
631 | 0 | } |
632 | 0 | } |
633 | 0 | X509_STORE_unlock(store); |
634 | 0 | return sk; |
635 | 0 | } |
636 | | |
637 | | X509_OBJECT *X509_OBJECT_retrieve_match(STACK_OF(X509_OBJECT) *h, |
638 | | X509_OBJECT *x) |
639 | 0 | { |
640 | 0 | int idx, i, num; |
641 | 0 | X509_OBJECT *obj; |
642 | |
|
643 | 0 | idx = sk_X509_OBJECT_find(h, x); |
644 | 0 | if (idx < 0) |
645 | 0 | return NULL; |
646 | 0 | if ((x->type != X509_LU_X509) && (x->type != X509_LU_CRL)) |
647 | 0 | return sk_X509_OBJECT_value(h, idx); |
648 | 0 | for (i = idx, num = sk_X509_OBJECT_num(h); i < num; i++) { |
649 | 0 | obj = sk_X509_OBJECT_value(h, i); |
650 | 0 | if (x509_object_cmp((const X509_OBJECT **)&obj, |
651 | 0 | (const X509_OBJECT **)&x)) |
652 | 0 | return NULL; |
653 | 0 | if (x->type == X509_LU_X509) { |
654 | 0 | if (!X509_cmp(obj->data.x509, x->data.x509)) |
655 | 0 | return obj; |
656 | 0 | } else if (x->type == X509_LU_CRL) { |
657 | 0 | if (!X509_CRL_match(obj->data.crl, x->data.crl)) |
658 | 0 | return obj; |
659 | 0 | } else |
660 | 0 | return obj; |
661 | 0 | } |
662 | 0 | return NULL; |
663 | 0 | } |
664 | | |
665 | | /*- |
666 | | * Try to get issuer certificate from store. Due to limitations |
667 | | * of the API this can only retrieve a single certificate matching |
668 | | * a given subject name. However it will fill the cache with all |
669 | | * matching certificates, so we can examine the cache for all |
670 | | * matches. |
671 | | * |
672 | | * Return values are: |
673 | | * 1 lookup successful. |
674 | | * 0 certificate not found. |
675 | | * -1 some other error. |
676 | | */ |
677 | | int X509_STORE_CTX_get1_issuer(X509 **issuer, X509_STORE_CTX *ctx, X509 *x) |
678 | 0 | { |
679 | 0 | X509_NAME *xn; |
680 | 0 | X509_OBJECT *obj = X509_OBJECT_new(), *pobj = NULL; |
681 | 0 | X509_STORE *store = ctx->ctx; |
682 | 0 | int i, ok, idx, ret; |
683 | |
|
684 | 0 | if (obj == NULL) |
685 | 0 | return -1; |
686 | 0 | *issuer = NULL; |
687 | 0 | xn = X509_get_issuer_name(x); |
688 | 0 | ok = X509_STORE_CTX_get_by_subject(ctx, X509_LU_X509, xn, obj); |
689 | 0 | if (ok != 1) { |
690 | 0 | X509_OBJECT_free(obj); |
691 | 0 | return 0; |
692 | 0 | } |
693 | | /* If certificate matches all OK */ |
694 | 0 | if (ctx->check_issued(ctx, x, obj->data.x509)) { |
695 | 0 | if (x509_check_cert_time(ctx, obj->data.x509, -1)) { |
696 | 0 | *issuer = obj->data.x509; |
697 | 0 | if (!X509_up_ref(*issuer)) { |
698 | 0 | *issuer = NULL; |
699 | 0 | ok = -1; |
700 | 0 | } |
701 | 0 | X509_OBJECT_free(obj); |
702 | 0 | return ok; |
703 | 0 | } |
704 | 0 | } |
705 | 0 | X509_OBJECT_free(obj); |
706 | |
|
707 | 0 | if (store == NULL) |
708 | 0 | return 0; |
709 | | |
710 | | /* Else find index of first cert accepted by 'check_issued' */ |
711 | 0 | ret = 0; |
712 | 0 | X509_STORE_lock(store); |
713 | 0 | idx = X509_OBJECT_idx_by_subject(store->objs, X509_LU_X509, xn); |
714 | 0 | if (idx != -1) { /* should be true as we've had at least one |
715 | | * match */ |
716 | | /* Look through all matching certs for suitable issuer */ |
717 | 0 | for (i = idx; i < sk_X509_OBJECT_num(store->objs); i++) { |
718 | 0 | pobj = sk_X509_OBJECT_value(store->objs, i); |
719 | | /* See if we've run past the matches */ |
720 | 0 | if (pobj->type != X509_LU_X509) |
721 | 0 | break; |
722 | 0 | if (X509_NAME_cmp(xn, X509_get_subject_name(pobj->data.x509))) |
723 | 0 | break; |
724 | 0 | if (ctx->check_issued(ctx, x, pobj->data.x509)) { |
725 | 0 | *issuer = pobj->data.x509; |
726 | 0 | ret = 1; |
727 | | /* |
728 | | * If times check, exit with match, |
729 | | * otherwise keep looking. Leave last |
730 | | * match in issuer so we return nearest |
731 | | * match if no certificate time is OK. |
732 | | */ |
733 | |
|
734 | 0 | if (x509_check_cert_time(ctx, *issuer, -1)) |
735 | 0 | break; |
736 | 0 | } |
737 | 0 | } |
738 | 0 | } |
739 | 0 | if (*issuer && !X509_up_ref(*issuer)) { |
740 | 0 | *issuer = NULL; |
741 | 0 | ret = -1; |
742 | 0 | } |
743 | 0 | X509_STORE_unlock(store); |
744 | 0 | return ret; |
745 | 0 | } |
746 | | |
747 | | int X509_STORE_set_flags(X509_STORE *ctx, unsigned long flags) |
748 | 0 | { |
749 | 0 | return X509_VERIFY_PARAM_set_flags(ctx->param, flags); |
750 | 0 | } |
751 | | |
752 | | int X509_STORE_set_depth(X509_STORE *ctx, int depth) |
753 | 0 | { |
754 | 0 | X509_VERIFY_PARAM_set_depth(ctx->param, depth); |
755 | 0 | return 1; |
756 | 0 | } |
757 | | |
758 | | int X509_STORE_set_purpose(X509_STORE *ctx, int purpose) |
759 | 0 | { |
760 | 0 | return X509_VERIFY_PARAM_set_purpose(ctx->param, purpose); |
761 | 0 | } |
762 | | |
763 | | int X509_STORE_set_trust(X509_STORE *ctx, int trust) |
764 | 0 | { |
765 | 0 | return X509_VERIFY_PARAM_set_trust(ctx->param, trust); |
766 | 0 | } |
767 | | |
768 | | int X509_STORE_set1_param(X509_STORE *ctx, X509_VERIFY_PARAM *param) |
769 | 0 | { |
770 | 0 | return X509_VERIFY_PARAM_set1(ctx->param, param); |
771 | 0 | } |
772 | | |
773 | | X509_VERIFY_PARAM *X509_STORE_get0_param(X509_STORE *ctx) |
774 | 0 | { |
775 | 0 | return ctx->param; |
776 | 0 | } |
777 | | |
778 | | void X509_STORE_set_verify(X509_STORE *ctx, X509_STORE_CTX_verify_fn verify) |
779 | 0 | { |
780 | 0 | ctx->verify = verify; |
781 | 0 | } |
782 | | |
783 | | X509_STORE_CTX_verify_fn X509_STORE_get_verify(X509_STORE *ctx) |
784 | 0 | { |
785 | 0 | return ctx->verify; |
786 | 0 | } |
787 | | |
788 | | void X509_STORE_set_verify_cb(X509_STORE *ctx, |
789 | | X509_STORE_CTX_verify_cb verify_cb) |
790 | 0 | { |
791 | 0 | ctx->verify_cb = verify_cb; |
792 | 0 | } |
793 | | |
794 | | X509_STORE_CTX_verify_cb X509_STORE_get_verify_cb(X509_STORE *ctx) |
795 | 0 | { |
796 | 0 | return ctx->verify_cb; |
797 | 0 | } |
798 | | |
799 | | void X509_STORE_set_get_issuer(X509_STORE *ctx, |
800 | | X509_STORE_CTX_get_issuer_fn get_issuer) |
801 | 0 | { |
802 | 0 | ctx->get_issuer = get_issuer; |
803 | 0 | } |
804 | | |
805 | | X509_STORE_CTX_get_issuer_fn X509_STORE_get_get_issuer(X509_STORE *ctx) |
806 | 0 | { |
807 | 0 | return ctx->get_issuer; |
808 | 0 | } |
809 | | |
810 | | void X509_STORE_set_check_issued(X509_STORE *ctx, |
811 | | X509_STORE_CTX_check_issued_fn check_issued) |
812 | 0 | { |
813 | 0 | ctx->check_issued = check_issued; |
814 | 0 | } |
815 | | |
816 | | X509_STORE_CTX_check_issued_fn X509_STORE_get_check_issued(X509_STORE *ctx) |
817 | 0 | { |
818 | 0 | return ctx->check_issued; |
819 | 0 | } |
820 | | |
821 | | void X509_STORE_set_check_revocation(X509_STORE *ctx, |
822 | | X509_STORE_CTX_check_revocation_fn check_revocation) |
823 | 0 | { |
824 | 0 | ctx->check_revocation = check_revocation; |
825 | 0 | } |
826 | | |
827 | | X509_STORE_CTX_check_revocation_fn X509_STORE_get_check_revocation(X509_STORE *ctx) |
828 | 0 | { |
829 | 0 | return ctx->check_revocation; |
830 | 0 | } |
831 | | |
832 | | void X509_STORE_set_get_crl(X509_STORE *ctx, |
833 | | X509_STORE_CTX_get_crl_fn get_crl) |
834 | 0 | { |
835 | 0 | ctx->get_crl = get_crl; |
836 | 0 | } |
837 | | |
838 | | X509_STORE_CTX_get_crl_fn X509_STORE_get_get_crl(X509_STORE *ctx) |
839 | 0 | { |
840 | 0 | return ctx->get_crl; |
841 | 0 | } |
842 | | |
843 | | void X509_STORE_set_check_crl(X509_STORE *ctx, |
844 | | X509_STORE_CTX_check_crl_fn check_crl) |
845 | 0 | { |
846 | 0 | ctx->check_crl = check_crl; |
847 | 0 | } |
848 | | |
849 | | X509_STORE_CTX_check_crl_fn X509_STORE_get_check_crl(X509_STORE *ctx) |
850 | 0 | { |
851 | 0 | return ctx->check_crl; |
852 | 0 | } |
853 | | |
854 | | void X509_STORE_set_cert_crl(X509_STORE *ctx, |
855 | | X509_STORE_CTX_cert_crl_fn cert_crl) |
856 | 0 | { |
857 | 0 | ctx->cert_crl = cert_crl; |
858 | 0 | } |
859 | | |
860 | | X509_STORE_CTX_cert_crl_fn X509_STORE_get_cert_crl(X509_STORE *ctx) |
861 | 0 | { |
862 | 0 | return ctx->cert_crl; |
863 | 0 | } |
864 | | |
865 | | void X509_STORE_set_check_policy(X509_STORE *ctx, |
866 | | X509_STORE_CTX_check_policy_fn check_policy) |
867 | 0 | { |
868 | 0 | ctx->check_policy = check_policy; |
869 | 0 | } |
870 | | |
871 | | X509_STORE_CTX_check_policy_fn X509_STORE_get_check_policy(X509_STORE *ctx) |
872 | 0 | { |
873 | 0 | return ctx->check_policy; |
874 | 0 | } |
875 | | |
876 | | void X509_STORE_set_lookup_certs(X509_STORE *ctx, |
877 | | X509_STORE_CTX_lookup_certs_fn lookup_certs) |
878 | 0 | { |
879 | 0 | ctx->lookup_certs = lookup_certs; |
880 | 0 | } |
881 | | |
882 | | X509_STORE_CTX_lookup_certs_fn X509_STORE_get_lookup_certs(X509_STORE *ctx) |
883 | 0 | { |
884 | 0 | return ctx->lookup_certs; |
885 | 0 | } |
886 | | |
887 | | void X509_STORE_set_lookup_crls(X509_STORE *ctx, |
888 | | X509_STORE_CTX_lookup_crls_fn lookup_crls) |
889 | 0 | { |
890 | 0 | ctx->lookup_crls = lookup_crls; |
891 | 0 | } |
892 | | |
893 | | X509_STORE_CTX_lookup_crls_fn X509_STORE_get_lookup_crls(X509_STORE *ctx) |
894 | 0 | { |
895 | 0 | return ctx->lookup_crls; |
896 | 0 | } |
897 | | |
898 | | void X509_STORE_set_cleanup(X509_STORE *ctx, |
899 | | X509_STORE_CTX_cleanup_fn ctx_cleanup) |
900 | 0 | { |
901 | 0 | ctx->cleanup = ctx_cleanup; |
902 | 0 | } |
903 | | |
904 | | X509_STORE_CTX_cleanup_fn X509_STORE_get_cleanup(X509_STORE *ctx) |
905 | 0 | { |
906 | 0 | return ctx->cleanup; |
907 | 0 | } |
908 | | |
909 | | int X509_STORE_set_ex_data(X509_STORE *ctx, int idx, void *data) |
910 | 0 | { |
911 | 0 | return CRYPTO_set_ex_data(&ctx->ex_data, idx, data); |
912 | 0 | } |
913 | | |
914 | | void *X509_STORE_get_ex_data(X509_STORE *ctx, int idx) |
915 | 0 | { |
916 | 0 | return CRYPTO_get_ex_data(&ctx->ex_data, idx); |
917 | 0 | } |
918 | | |
919 | | X509_STORE *X509_STORE_CTX_get0_store(X509_STORE_CTX *ctx) |
920 | 0 | { |
921 | 0 | return ctx->ctx; |
922 | 0 | } |