Add X509 related libctx changes.
[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             if (!X509_up_ref(cert))
583                 goto err;
584             if (!sk_X509_push(sk, cert)) {
585                 X509_free(cert);
586                 goto err;
587             }
588         }
589     }
590     X509_STORE_unlock(store);
591     return sk;
592
593  err:
594     X509_STORE_unlock(store);
595     sk_X509_pop_free(sk, X509_free);
596     return NULL;
597 }
598
599 STACK_OF(X509) *X509_STORE_CTX_get1_certs(X509_STORE_CTX *ctx,
600                                           const X509_NAME *nm)
601 {
602     int i, idx, cnt;
603     STACK_OF(X509) *sk = NULL;
604     X509 *x;
605     X509_OBJECT *obj;
606     X509_STORE *store = ctx->store;
607
608     if (store == NULL)
609         return NULL;
610
611     X509_STORE_lock(store);
612     idx = x509_object_idx_cnt(store->objs, X509_LU_X509, nm, &cnt);
613     if (idx < 0) {
614         /*
615          * Nothing found in cache: do lookup to possibly add new objects to
616          * cache
617          */
618         X509_OBJECT *xobj = X509_OBJECT_new();
619
620         X509_STORE_unlock(store);
621
622         if (xobj == NULL)
623             return NULL;
624         if (!X509_STORE_CTX_get_by_subject(ctx, X509_LU_X509, nm, xobj)) {
625             X509_OBJECT_free(xobj);
626             return NULL;
627         }
628         X509_OBJECT_free(xobj);
629         X509_STORE_lock(store);
630         idx = x509_object_idx_cnt(store->objs, X509_LU_X509, nm, &cnt);
631         if (idx < 0) {
632             X509_STORE_unlock(store);
633             return NULL;
634         }
635     }
636
637     sk = sk_X509_new_null();
638     for (i = 0; i < cnt; i++, idx++) {
639         obj = sk_X509_OBJECT_value(store->objs, idx);
640         x = obj->data.x509;
641         if (!X509_up_ref(x)) {
642             X509_STORE_unlock(store);
643             sk_X509_pop_free(sk, X509_free);
644             return NULL;
645         }
646         if (!sk_X509_push(sk, x)) {
647             X509_STORE_unlock(store);
648             X509_free(x);
649             sk_X509_pop_free(sk, X509_free);
650             return NULL;
651         }
652     }
653     X509_STORE_unlock(store);
654     return sk;
655 }
656
657 STACK_OF(X509_CRL) *X509_STORE_CTX_get1_crls(const X509_STORE_CTX *ctx,
658                                              const X509_NAME *nm)
659 {
660     int i, idx, cnt;
661     STACK_OF(X509_CRL) *sk = sk_X509_CRL_new_null();
662     X509_CRL *x;
663     X509_OBJECT *obj, *xobj = X509_OBJECT_new();
664     X509_STORE *store = ctx->store;
665
666     /* Always do lookup to possibly add new CRLs to cache */
667     if (sk == NULL
668             || xobj == NULL
669             || store == NULL
670             || !X509_STORE_CTX_get_by_subject(ctx, X509_LU_CRL, nm, xobj)) {
671         X509_OBJECT_free(xobj);
672         sk_X509_CRL_free(sk);
673         return NULL;
674     }
675     X509_OBJECT_free(xobj);
676     X509_STORE_lock(store);
677     idx = x509_object_idx_cnt(store->objs, X509_LU_CRL, nm, &cnt);
678     if (idx < 0) {
679         X509_STORE_unlock(store);
680         sk_X509_CRL_free(sk);
681         return NULL;
682     }
683
684     for (i = 0; i < cnt; i++, idx++) {
685         obj = sk_X509_OBJECT_value(store->objs, idx);
686         x = obj->data.crl;
687         if (!X509_CRL_up_ref(x)) {
688             X509_STORE_unlock(store);
689             sk_X509_CRL_pop_free(sk, X509_CRL_free);
690             return NULL;
691         }
692         if (!sk_X509_CRL_push(sk, x)) {
693             X509_STORE_unlock(store);
694             X509_CRL_free(x);
695             sk_X509_CRL_pop_free(sk, X509_CRL_free);
696             return NULL;
697         }
698     }
699     X509_STORE_unlock(store);
700     return sk;
701 }
702
703 X509_OBJECT *X509_OBJECT_retrieve_match(STACK_OF(X509_OBJECT) *h,
704                                         X509_OBJECT *x)
705 {
706     int idx, i, num;
707     X509_OBJECT *obj;
708
709     idx = sk_X509_OBJECT_find(h, x);
710     if (idx < 0)
711         return NULL;
712     if ((x->type != X509_LU_X509) && (x->type != X509_LU_CRL))
713         return sk_X509_OBJECT_value(h, idx);
714     for (i = idx, num = sk_X509_OBJECT_num(h); i < num; i++) {
715         obj = sk_X509_OBJECT_value(h, i);
716         if (x509_object_cmp((const X509_OBJECT **)&obj,
717                             (const X509_OBJECT **)&x))
718             return NULL;
719         if (x->type == X509_LU_X509) {
720             if (!X509_cmp(obj->data.x509, x->data.x509))
721                 return obj;
722         } else if (x->type == X509_LU_CRL) {
723             if (!X509_CRL_match(obj->data.crl, x->data.crl))
724                 return obj;
725         } else
726             return obj;
727     }
728     return NULL;
729 }
730
731 /*-
732  * Try to get issuer certificate from store. Due to limitations
733  * of the API this can only retrieve a single certificate matching
734  * a given subject name. However it will fill the cache with all
735  * matching certificates, so we can examine the cache for all
736  * matches.
737  *
738  * Return values are:
739  *  1 lookup successful.
740  *  0 certificate not found.
741  * -1 some other error.
742  */
743 int X509_STORE_CTX_get1_issuer(X509 **issuer, X509_STORE_CTX *ctx, X509 *x)
744 {
745     const X509_NAME *xn;
746     X509_OBJECT *obj = X509_OBJECT_new(), *pobj = NULL;
747     X509_STORE *store = ctx->store;
748     int i, ok, idx, ret;
749
750     if (obj == NULL)
751         return -1;
752     *issuer = NULL;
753     xn = X509_get_issuer_name(x);
754     ok = X509_STORE_CTX_get_by_subject(ctx, X509_LU_X509, xn, obj);
755     if (ok != 1) {
756         X509_OBJECT_free(obj);
757         return 0;
758     }
759     /* If certificate matches all OK */
760     if (ctx->check_issued(ctx, x, obj->data.x509)) {
761         if (x509_check_cert_time(ctx, obj->data.x509, -1)) {
762             *issuer = obj->data.x509;
763             if (!X509_up_ref(*issuer)) {
764                 *issuer = NULL;
765                 ok = -1;
766             }
767             X509_OBJECT_free(obj);
768             return ok;
769         }
770     }
771     X509_OBJECT_free(obj);
772
773     if (store == NULL)
774         return 0;
775
776     /* Else find index of first cert accepted by 'check_issued' */
777     ret = 0;
778     X509_STORE_lock(store);
779     idx = X509_OBJECT_idx_by_subject(store->objs, X509_LU_X509, xn);
780     if (idx != -1) {            /* should be true as we've had at least one
781                                  * match */
782         /* Look through all matching certs for suitable issuer */
783         for (i = idx; i < sk_X509_OBJECT_num(store->objs); i++) {
784             pobj = sk_X509_OBJECT_value(store->objs, i);
785             /* See if we've run past the matches */
786             if (pobj->type != X509_LU_X509)
787                 break;
788             if (X509_NAME_cmp(xn, X509_get_subject_name(pobj->data.x509)))
789                 break;
790             if (ctx->check_issued(ctx, x, pobj->data.x509)) {
791                 *issuer = pobj->data.x509;
792                 ret = 1;
793                 /*
794                  * If times check, exit with match,
795                  * otherwise keep looking. Leave last
796                  * match in issuer so we return nearest
797                  * match if no certificate time is OK.
798                  */
799
800                 if (x509_check_cert_time(ctx, *issuer, -1))
801                     break;
802             }
803         }
804     }
805     if (*issuer && !X509_up_ref(*issuer)) {
806         *issuer = NULL;
807         ret = -1;
808     }
809     X509_STORE_unlock(store);
810     return ret;
811 }
812
813 int X509_STORE_set_flags(X509_STORE *ctx, unsigned long flags)
814 {
815     return X509_VERIFY_PARAM_set_flags(ctx->param, flags);
816 }
817
818 int X509_STORE_set_depth(X509_STORE *ctx, int depth)
819 {
820     X509_VERIFY_PARAM_set_depth(ctx->param, depth);
821     return 1;
822 }
823
824 int X509_STORE_set_purpose(X509_STORE *ctx, int purpose)
825 {
826     return X509_VERIFY_PARAM_set_purpose(ctx->param, purpose);
827 }
828
829 int X509_STORE_set_trust(X509_STORE *ctx, int trust)
830 {
831     return X509_VERIFY_PARAM_set_trust(ctx->param, trust);
832 }
833
834 int X509_STORE_set1_param(X509_STORE *ctx, const X509_VERIFY_PARAM *param)
835 {
836     return X509_VERIFY_PARAM_set1(ctx->param, param);
837 }
838
839 X509_VERIFY_PARAM *X509_STORE_get0_param(const X509_STORE *ctx)
840 {
841     return ctx->param;
842 }
843
844 void X509_STORE_set_verify(X509_STORE *ctx, X509_STORE_CTX_verify_fn verify)
845 {
846     ctx->verify = verify;
847 }
848
849 X509_STORE_CTX_verify_fn X509_STORE_get_verify(const X509_STORE *ctx)
850 {
851     return ctx->verify;
852 }
853
854 void X509_STORE_set_verify_cb(X509_STORE *ctx,
855                               X509_STORE_CTX_verify_cb verify_cb)
856 {
857     ctx->verify_cb = verify_cb;
858 }
859
860 X509_STORE_CTX_verify_cb X509_STORE_get_verify_cb(const X509_STORE *ctx)
861 {
862     return ctx->verify_cb;
863 }
864
865 void X509_STORE_set_get_issuer(X509_STORE *ctx,
866                                X509_STORE_CTX_get_issuer_fn get_issuer)
867 {
868     ctx->get_issuer = get_issuer;
869 }
870
871 X509_STORE_CTX_get_issuer_fn X509_STORE_get_get_issuer(const X509_STORE *ctx)
872 {
873     return ctx->get_issuer;
874 }
875
876 void X509_STORE_set_check_issued(X509_STORE *ctx,
877                                  X509_STORE_CTX_check_issued_fn check_issued)
878 {
879     ctx->check_issued = check_issued;
880 }
881
882 X509_STORE_CTX_check_issued_fn X509_STORE_get_check_issued(const X509_STORE *ctx)
883 {
884     return ctx->check_issued;
885 }
886
887 void X509_STORE_set_check_revocation(X509_STORE *ctx,
888                                      X509_STORE_CTX_check_revocation_fn check_revocation)
889 {
890     ctx->check_revocation = check_revocation;
891 }
892
893 X509_STORE_CTX_check_revocation_fn X509_STORE_get_check_revocation(const X509_STORE *ctx)
894 {
895     return ctx->check_revocation;
896 }
897
898 void X509_STORE_set_get_crl(X509_STORE *ctx,
899                             X509_STORE_CTX_get_crl_fn get_crl)
900 {
901     ctx->get_crl = get_crl;
902 }
903
904 X509_STORE_CTX_get_crl_fn X509_STORE_get_get_crl(const X509_STORE *ctx)
905 {
906     return ctx->get_crl;
907 }
908
909 void X509_STORE_set_check_crl(X509_STORE *ctx,
910                               X509_STORE_CTX_check_crl_fn check_crl)
911 {
912     ctx->check_crl = check_crl;
913 }
914
915 X509_STORE_CTX_check_crl_fn X509_STORE_get_check_crl(const X509_STORE *ctx)
916 {
917     return ctx->check_crl;
918 }
919
920 void X509_STORE_set_cert_crl(X509_STORE *ctx,
921                              X509_STORE_CTX_cert_crl_fn cert_crl)
922 {
923     ctx->cert_crl = cert_crl;
924 }
925
926 X509_STORE_CTX_cert_crl_fn X509_STORE_get_cert_crl(const X509_STORE *ctx)
927 {
928     return ctx->cert_crl;
929 }
930
931 void X509_STORE_set_check_policy(X509_STORE *ctx,
932                                  X509_STORE_CTX_check_policy_fn check_policy)
933 {
934     ctx->check_policy = check_policy;
935 }
936
937 X509_STORE_CTX_check_policy_fn X509_STORE_get_check_policy(const X509_STORE *ctx)
938 {
939     return ctx->check_policy;
940 }
941
942 void X509_STORE_set_lookup_certs(X509_STORE *ctx,
943                                  X509_STORE_CTX_lookup_certs_fn lookup_certs)
944 {
945     ctx->lookup_certs = lookup_certs;
946 }
947
948 X509_STORE_CTX_lookup_certs_fn X509_STORE_get_lookup_certs(const X509_STORE *ctx)
949 {
950     return ctx->lookup_certs;
951 }
952
953 void X509_STORE_set_lookup_crls(X509_STORE *ctx,
954                                 X509_STORE_CTX_lookup_crls_fn lookup_crls)
955 {
956     ctx->lookup_crls = lookup_crls;
957 }
958
959 X509_STORE_CTX_lookup_crls_fn X509_STORE_get_lookup_crls(const X509_STORE *ctx)
960 {
961     return ctx->lookup_crls;
962 }
963
964 void X509_STORE_set_cleanup(X509_STORE *ctx,
965                             X509_STORE_CTX_cleanup_fn ctx_cleanup)
966 {
967     ctx->cleanup = ctx_cleanup;
968 }
969
970 X509_STORE_CTX_cleanup_fn X509_STORE_get_cleanup(const X509_STORE *ctx)
971 {
972     return ctx->cleanup;
973 }
974
975 int X509_STORE_set_ex_data(X509_STORE *ctx, int idx, void *data)
976 {
977     return CRYPTO_set_ex_data(&ctx->ex_data, idx, data);
978 }
979
980 void *X509_STORE_get_ex_data(const X509_STORE *ctx, int idx)
981 {
982     return CRYPTO_get_ex_data(&ctx->ex_data, idx);
983 }
984
985 X509_STORE *X509_STORE_CTX_get0_store(const X509_STORE_CTX *ctx)
986 {
987     return ctx->store;
988 }