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