• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2  * All rights reserved.
3  *
4  * This package is an SSL implementation written
5  * by Eric Young (eay@cryptsoft.com).
6  * The implementation was written so as to conform with Netscapes SSL.
7  *
8  * This library is free for commercial and non-commercial use as long as
9  * the following conditions are aheared to.  The following conditions
10  * apply to all code found in this distribution, be it the RC4, RSA,
11  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
12  * included with this distribution is covered by the same copyright terms
13  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14  *
15  * Copyright remains Eric Young's, and as such any Copyright notices in
16  * the code are not to be removed.
17  * If this package is used in a product, Eric Young should be given attribution
18  * as the author of the parts of the library used.
19  * This can be in the form of a textual message at program startup or
20  * in documentation (online or textual) provided with the package.
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions
24  * are met:
25  * 1. Redistributions of source code must retain the copyright
26  *    notice, this list of conditions and the following disclaimer.
27  * 2. Redistributions in binary form must reproduce the above copyright
28  *    notice, this list of conditions and the following disclaimer in the
29  *    documentation and/or other materials provided with the distribution.
30  * 3. All advertising materials mentioning features or use of this software
31  *    must display the following acknowledgement:
32  *    "This product includes cryptographic software written by
33  *     Eric Young (eay@cryptsoft.com)"
34  *    The word 'cryptographic' can be left out if the rouines from the library
35  *    being used are not cryptographic related :-).
36  * 4. If you include any Windows specific code (or a derivative thereof) from
37  *    the apps directory (application code) you must include an acknowledgement:
38  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39  *
40  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  *
52  * The licence and distribution terms for any publically available version or
53  * derivative of this code cannot be changed.  i.e. this code cannot simply be
54  * copied and put under another distribution licence
55  * [including the GNU Public Licence.] */
56 
57 #include <string.h>
58 
59 #include <openssl/err.h>
60 #include <openssl/mem.h>
61 #include <openssl/thread.h>
62 #include <openssl/x509.h>
63 #include <openssl/x509v3.h>
64 
65 #include "../internal.h"
66 #include "internal.h"
67 
X509_LOOKUP_new(X509_LOOKUP_METHOD * method)68 X509_LOOKUP *X509_LOOKUP_new(X509_LOOKUP_METHOD *method) {
69   X509_LOOKUP *ret;
70 
71   ret = (X509_LOOKUP *)OPENSSL_malloc(sizeof(X509_LOOKUP));
72   if (ret == NULL) {
73     return NULL;
74   }
75 
76   ret->init = 0;
77   ret->skip = 0;
78   ret->method = method;
79   ret->method_data = NULL;
80   ret->store_ctx = NULL;
81   if ((method->new_item != NULL) && !method->new_item(ret)) {
82     OPENSSL_free(ret);
83     return NULL;
84   }
85   return ret;
86 }
87 
X509_LOOKUP_free(X509_LOOKUP * ctx)88 void X509_LOOKUP_free(X509_LOOKUP *ctx) {
89   if (ctx == NULL) {
90     return;
91   }
92   if ((ctx->method != NULL) && (ctx->method->free != NULL)) {
93     (*ctx->method->free)(ctx);
94   }
95   OPENSSL_free(ctx);
96 }
97 
X509_LOOKUP_init(X509_LOOKUP * ctx)98 int X509_LOOKUP_init(X509_LOOKUP *ctx) {
99   if (ctx->method == NULL) {
100     return 0;
101   }
102   if (ctx->method->init != NULL) {
103     return ctx->method->init(ctx);
104   } else {
105     return 1;
106   }
107 }
108 
X509_LOOKUP_shutdown(X509_LOOKUP * ctx)109 int X509_LOOKUP_shutdown(X509_LOOKUP *ctx) {
110   if (ctx->method == NULL) {
111     return 0;
112   }
113   if (ctx->method->shutdown != NULL) {
114     return ctx->method->shutdown(ctx);
115   } else {
116     return 1;
117   }
118 }
119 
X509_LOOKUP_ctrl(X509_LOOKUP * ctx,int cmd,const char * argc,long argl,char ** ret)120 int X509_LOOKUP_ctrl(X509_LOOKUP *ctx, int cmd, const char *argc, long argl,
121                      char **ret) {
122   if (ctx->method == NULL) {
123     return -1;
124   }
125   if (ctx->method->ctrl != NULL) {
126     return ctx->method->ctrl(ctx, cmd, argc, argl, ret);
127   } else {
128     return 1;
129   }
130 }
131 
X509_LOOKUP_by_subject(X509_LOOKUP * ctx,int type,X509_NAME * name,X509_OBJECT * ret)132 int X509_LOOKUP_by_subject(X509_LOOKUP *ctx, int type, X509_NAME *name,
133                            X509_OBJECT *ret) {
134   if ((ctx->method == NULL) || (ctx->method->get_by_subject == NULL)) {
135     return 0;
136   }
137   if (ctx->skip) {
138     return 0;
139   }
140   return ctx->method->get_by_subject(ctx, type, name, ret) > 0;
141 }
142 
x509_object_cmp(const X509_OBJECT * a,const X509_OBJECT * b)143 static int x509_object_cmp(const X509_OBJECT *a, const X509_OBJECT *b) {
144   int ret = a->type - b->type;
145   if (ret) {
146     return ret;
147   }
148   switch (a->type) {
149     case X509_LU_X509:
150       return X509_subject_name_cmp(a->data.x509, b->data.x509);
151     case X509_LU_CRL:
152       return X509_CRL_cmp(a->data.crl, b->data.crl);
153     default:
154       // abort();
155       return 0;
156   }
157 }
158 
x509_object_cmp_sk(const X509_OBJECT * const * a,const X509_OBJECT * const * b)159 static int x509_object_cmp_sk(const X509_OBJECT *const *a,
160                               const X509_OBJECT *const *b) {
161   return x509_object_cmp(*a, *b);
162 }
163 
X509_STORE_new(void)164 X509_STORE *X509_STORE_new(void) {
165   X509_STORE *ret;
166 
167   if ((ret = (X509_STORE *)OPENSSL_zalloc(sizeof(X509_STORE))) == NULL) {
168     return NULL;
169   }
170   CRYPTO_MUTEX_init(&ret->objs_lock);
171   ret->objs = sk_X509_OBJECT_new(x509_object_cmp_sk);
172   if (ret->objs == NULL) {
173     goto err;
174   }
175   ret->get_cert_methods = sk_X509_LOOKUP_new_null();
176   if (ret->get_cert_methods == NULL) {
177     goto err;
178   }
179   ret->param = X509_VERIFY_PARAM_new();
180   if (ret->param == NULL) {
181     goto err;
182   }
183 
184   ret->references = 1;
185   return ret;
186 err:
187   if (ret) {
188     CRYPTO_MUTEX_cleanup(&ret->objs_lock);
189     if (ret->param) {
190       X509_VERIFY_PARAM_free(ret->param);
191     }
192     if (ret->get_cert_methods) {
193       sk_X509_LOOKUP_free(ret->get_cert_methods);
194     }
195     if (ret->objs) {
196       sk_X509_OBJECT_free(ret->objs);
197     }
198     OPENSSL_free(ret);
199   }
200   return NULL;
201 }
202 
X509_STORE_up_ref(X509_STORE * store)203 int X509_STORE_up_ref(X509_STORE *store) {
204   CRYPTO_refcount_inc(&store->references);
205   return 1;
206 }
207 
cleanup(X509_OBJECT * a)208 static void cleanup(X509_OBJECT *a) {
209   if (a == NULL) {
210     return;
211   }
212   if (a->type == X509_LU_X509) {
213     X509_free(a->data.x509);
214   } else if (a->type == X509_LU_CRL) {
215     X509_CRL_free(a->data.crl);
216   } else {
217     // abort();
218   }
219 
220   OPENSSL_free(a);
221 }
222 
X509_STORE_free(X509_STORE * vfy)223 void X509_STORE_free(X509_STORE *vfy) {
224   size_t j;
225   STACK_OF(X509_LOOKUP) *sk;
226   X509_LOOKUP *lu;
227 
228   if (vfy == NULL) {
229     return;
230   }
231 
232   if (!CRYPTO_refcount_dec_and_test_zero(&vfy->references)) {
233     return;
234   }
235 
236   CRYPTO_MUTEX_cleanup(&vfy->objs_lock);
237 
238   sk = vfy->get_cert_methods;
239   for (j = 0; j < sk_X509_LOOKUP_num(sk); j++) {
240     lu = sk_X509_LOOKUP_value(sk, j);
241     X509_LOOKUP_shutdown(lu);
242     X509_LOOKUP_free(lu);
243   }
244   sk_X509_LOOKUP_free(sk);
245   sk_X509_OBJECT_pop_free(vfy->objs, cleanup);
246 
247   if (vfy->param) {
248     X509_VERIFY_PARAM_free(vfy->param);
249   }
250   OPENSSL_free(vfy);
251 }
252 
X509_STORE_add_lookup(X509_STORE * v,X509_LOOKUP_METHOD * m)253 X509_LOOKUP *X509_STORE_add_lookup(X509_STORE *v, X509_LOOKUP_METHOD *m) {
254   size_t i;
255   STACK_OF(X509_LOOKUP) *sk;
256   X509_LOOKUP *lu;
257 
258   sk = v->get_cert_methods;
259   for (i = 0; i < sk_X509_LOOKUP_num(sk); i++) {
260     lu = sk_X509_LOOKUP_value(sk, i);
261     if (m == lu->method) {
262       return lu;
263     }
264   }
265   // a new one
266   lu = X509_LOOKUP_new(m);
267   if (lu == NULL) {
268     return NULL;
269   } else {
270     lu->store_ctx = v;
271     if (sk_X509_LOOKUP_push(v->get_cert_methods, lu)) {
272       return lu;
273     } else {
274       X509_LOOKUP_free(lu);
275       return NULL;
276     }
277   }
278 }
279 
X509_STORE_get_by_subject(X509_STORE_CTX * vs,int type,X509_NAME * name,X509_OBJECT * ret)280 int X509_STORE_get_by_subject(X509_STORE_CTX *vs, int type, X509_NAME *name,
281                               X509_OBJECT *ret) {
282   X509_STORE *ctx = vs->ctx;
283   X509_LOOKUP *lu;
284   X509_OBJECT stmp, *tmp;
285   int i;
286 
287   CRYPTO_MUTEX_lock_write(&ctx->objs_lock);
288   tmp = X509_OBJECT_retrieve_by_subject(ctx->objs, type, name);
289   CRYPTO_MUTEX_unlock_write(&ctx->objs_lock);
290 
291   if (tmp == NULL || type == X509_LU_CRL) {
292     for (i = 0; i < (int)sk_X509_LOOKUP_num(ctx->get_cert_methods); i++) {
293       lu = sk_X509_LOOKUP_value(ctx->get_cert_methods, i);
294       if (X509_LOOKUP_by_subject(lu, type, name, &stmp)) {
295         tmp = &stmp;
296         break;
297       }
298     }
299     if (tmp == NULL) {
300       return 0;
301     }
302   }
303 
304   // if (ret->data.ptr != NULL) X509_OBJECT_free_contents(ret);
305 
306   ret->type = tmp->type;
307   ret->data.ptr = tmp->data.ptr;
308 
309   X509_OBJECT_up_ref_count(ret);
310 
311   return 1;
312 }
313 
x509_store_add(X509_STORE * ctx,void * x,int is_crl)314 static int x509_store_add(X509_STORE *ctx, void *x, int is_crl) {
315   if (x == NULL) {
316     return 0;
317   }
318 
319   X509_OBJECT *const obj = (X509_OBJECT *)OPENSSL_malloc(sizeof(X509_OBJECT));
320   if (obj == NULL) {
321     return 0;
322   }
323 
324   if (is_crl) {
325     obj->type = X509_LU_CRL;
326     obj->data.crl = (X509_CRL *)x;
327   } else {
328     obj->type = X509_LU_X509;
329     obj->data.x509 = (X509 *)x;
330   }
331   X509_OBJECT_up_ref_count(obj);
332 
333   CRYPTO_MUTEX_lock_write(&ctx->objs_lock);
334 
335   int ret = 1;
336   int added = 0;
337   // Duplicates are silently ignored
338   if (!X509_OBJECT_retrieve_match(ctx->objs, obj)) {
339     ret = added = (sk_X509_OBJECT_push(ctx->objs, obj) != 0);
340   }
341 
342   CRYPTO_MUTEX_unlock_write(&ctx->objs_lock);
343 
344   if (!added) {
345     X509_OBJECT_free_contents(obj);
346     OPENSSL_free(obj);
347   }
348 
349   return ret;
350 }
351 
X509_STORE_add_cert(X509_STORE * ctx,X509 * x)352 int X509_STORE_add_cert(X509_STORE *ctx, X509 *x) {
353   return x509_store_add(ctx, x, /*is_crl=*/0);
354 }
355 
X509_STORE_add_crl(X509_STORE * ctx,X509_CRL * x)356 int X509_STORE_add_crl(X509_STORE *ctx, X509_CRL *x) {
357   return x509_store_add(ctx, x, /*is_crl=*/1);
358 }
359 
X509_OBJECT_up_ref_count(X509_OBJECT * a)360 int X509_OBJECT_up_ref_count(X509_OBJECT *a) {
361   switch (a->type) {
362     case X509_LU_X509:
363       X509_up_ref(a->data.x509);
364       break;
365     case X509_LU_CRL:
366       X509_CRL_up_ref(a->data.crl);
367       break;
368   }
369   return 1;
370 }
371 
X509_OBJECT_free_contents(X509_OBJECT * a)372 void X509_OBJECT_free_contents(X509_OBJECT *a) {
373   switch (a->type) {
374     case X509_LU_X509:
375       X509_free(a->data.x509);
376       break;
377     case X509_LU_CRL:
378       X509_CRL_free(a->data.crl);
379       break;
380   }
381 }
382 
X509_OBJECT_get_type(const X509_OBJECT * a)383 int X509_OBJECT_get_type(const X509_OBJECT *a) { return a->type; }
384 
X509_OBJECT_get0_X509(const X509_OBJECT * a)385 X509 *X509_OBJECT_get0_X509(const X509_OBJECT *a) {
386   if (a == NULL || a->type != X509_LU_X509) {
387     return NULL;
388   }
389   return a->data.x509;
390 }
391 
x509_object_idx_cnt(STACK_OF (X509_OBJECT)* h,int type,X509_NAME * name,int * pnmatch)392 static int x509_object_idx_cnt(STACK_OF(X509_OBJECT) *h, int type,
393                                X509_NAME *name, int *pnmatch) {
394   X509_OBJECT stmp;
395   X509 x509_s;
396   X509_CINF cinf_s;
397   X509_CRL crl_s;
398   X509_CRL_INFO crl_info_s;
399 
400   stmp.type = type;
401   switch (type) {
402     case X509_LU_X509:
403       stmp.data.x509 = &x509_s;
404       x509_s.cert_info = &cinf_s;
405       cinf_s.subject = name;
406       break;
407     case X509_LU_CRL:
408       stmp.data.crl = &crl_s;
409       crl_s.crl = &crl_info_s;
410       crl_info_s.issuer = name;
411       break;
412     default:
413       // abort();
414       return -1;
415   }
416 
417   size_t idx;
418   sk_X509_OBJECT_sort(h);
419   if (!sk_X509_OBJECT_find(h, &idx, &stmp)) {
420     return -1;
421   }
422 
423   if (pnmatch != NULL) {
424     *pnmatch = 1;
425     for (size_t tidx = idx + 1; tidx < sk_X509_OBJECT_num(h); tidx++) {
426       const X509_OBJECT *tobj = sk_X509_OBJECT_value(h, tidx);
427       if (x509_object_cmp(tobj, &stmp)) {
428         break;
429       }
430       (*pnmatch)++;
431     }
432   }
433 
434   return (int)idx;
435 }
436 
X509_OBJECT_idx_by_subject(STACK_OF (X509_OBJECT)* h,int type,X509_NAME * name)437 int X509_OBJECT_idx_by_subject(STACK_OF(X509_OBJECT) *h, int type,
438                                X509_NAME *name) {
439   return x509_object_idx_cnt(h, type, name, NULL);
440 }
441 
X509_OBJECT_retrieve_by_subject(STACK_OF (X509_OBJECT)* h,int type,X509_NAME * name)442 X509_OBJECT *X509_OBJECT_retrieve_by_subject(STACK_OF(X509_OBJECT) *h, int type,
443                                              X509_NAME *name) {
444   int idx;
445   idx = X509_OBJECT_idx_by_subject(h, type, name);
446   if (idx == -1) {
447     return NULL;
448   }
449   return sk_X509_OBJECT_value(h, idx);
450 }
451 
STACK_OF(X509_OBJECT)452 STACK_OF(X509_OBJECT) *X509_STORE_get0_objects(X509_STORE *st) {
453   return st->objs;
454 }
455 
STACK_OF(X509)456 STACK_OF(X509) *X509_STORE_get1_certs(X509_STORE_CTX *ctx, X509_NAME *nm) {
457   int i, idx, cnt;
458   STACK_OF(X509) *sk;
459   X509 *x;
460   X509_OBJECT *obj;
461   sk = sk_X509_new_null();
462   if (sk == NULL) {
463     return NULL;
464   }
465   CRYPTO_MUTEX_lock_write(&ctx->ctx->objs_lock);
466   idx = x509_object_idx_cnt(ctx->ctx->objs, X509_LU_X509, nm, &cnt);
467   if (idx < 0) {
468     // Nothing found in cache: do lookup to possibly add new objects to
469     // cache
470     X509_OBJECT xobj;
471     CRYPTO_MUTEX_unlock_write(&ctx->ctx->objs_lock);
472     if (!X509_STORE_get_by_subject(ctx, X509_LU_X509, nm, &xobj)) {
473       sk_X509_free(sk);
474       return NULL;
475     }
476     X509_OBJECT_free_contents(&xobj);
477     CRYPTO_MUTEX_lock_write(&ctx->ctx->objs_lock);
478     idx = x509_object_idx_cnt(ctx->ctx->objs, X509_LU_X509, nm, &cnt);
479     if (idx < 0) {
480       CRYPTO_MUTEX_unlock_write(&ctx->ctx->objs_lock);
481       sk_X509_free(sk);
482       return NULL;
483     }
484   }
485   for (i = 0; i < cnt; i++, idx++) {
486     obj = sk_X509_OBJECT_value(ctx->ctx->objs, idx);
487     x = obj->data.x509;
488     if (!sk_X509_push(sk, x)) {
489       CRYPTO_MUTEX_unlock_write(&ctx->ctx->objs_lock);
490       sk_X509_pop_free(sk, X509_free);
491       return NULL;
492     }
493     X509_up_ref(x);
494   }
495   CRYPTO_MUTEX_unlock_write(&ctx->ctx->objs_lock);
496   return sk;
497 }
498 
STACK_OF(X509_CRL)499 STACK_OF(X509_CRL) *X509_STORE_get1_crls(X509_STORE_CTX *ctx, X509_NAME *nm) {
500   int i, idx, cnt;
501   STACK_OF(X509_CRL) *sk;
502   X509_CRL *x;
503   X509_OBJECT *obj, xobj;
504   sk = sk_X509_CRL_new_null();
505   if (sk == NULL) {
506     return NULL;
507   }
508 
509   // Always do lookup to possibly add new CRLs to cache.
510   if (!X509_STORE_get_by_subject(ctx, X509_LU_CRL, nm, &xobj)) {
511     sk_X509_CRL_free(sk);
512     return NULL;
513   }
514   X509_OBJECT_free_contents(&xobj);
515   CRYPTO_MUTEX_lock_write(&ctx->ctx->objs_lock);
516   idx = x509_object_idx_cnt(ctx->ctx->objs, X509_LU_CRL, nm, &cnt);
517   if (idx < 0) {
518     CRYPTO_MUTEX_unlock_write(&ctx->ctx->objs_lock);
519     sk_X509_CRL_free(sk);
520     return NULL;
521   }
522 
523   for (i = 0; i < cnt; i++, idx++) {
524     obj = sk_X509_OBJECT_value(ctx->ctx->objs, idx);
525     x = obj->data.crl;
526     X509_CRL_up_ref(x);
527     if (!sk_X509_CRL_push(sk, x)) {
528       CRYPTO_MUTEX_unlock_write(&ctx->ctx->objs_lock);
529       X509_CRL_free(x);
530       sk_X509_CRL_pop_free(sk, X509_CRL_free);
531       return NULL;
532     }
533   }
534   CRYPTO_MUTEX_unlock_write(&ctx->ctx->objs_lock);
535   return sk;
536 }
537 
X509_OBJECT_retrieve_match(STACK_OF (X509_OBJECT)* h,X509_OBJECT * x)538 X509_OBJECT *X509_OBJECT_retrieve_match(STACK_OF(X509_OBJECT) *h,
539                                         X509_OBJECT *x) {
540   sk_X509_OBJECT_sort(h);
541   size_t idx;
542   if (!sk_X509_OBJECT_find(h, &idx, x)) {
543     return NULL;
544   }
545   if ((x->type != X509_LU_X509) && (x->type != X509_LU_CRL)) {
546     return sk_X509_OBJECT_value(h, idx);
547   }
548   for (size_t i = idx; i < sk_X509_OBJECT_num(h); i++) {
549     X509_OBJECT *obj = sk_X509_OBJECT_value(h, i);
550     if (x509_object_cmp(obj, x)) {
551       return NULL;
552     }
553     if (x->type == X509_LU_X509) {
554       if (!X509_cmp(obj->data.x509, x->data.x509)) {
555         return obj;
556       }
557     } else if (x->type == X509_LU_CRL) {
558       if (!X509_CRL_match(obj->data.crl, x->data.crl)) {
559         return obj;
560       }
561     } else {
562       return obj;
563     }
564   }
565   return NULL;
566 }
567 
568 // Try to get issuer certificate from store. Due to limitations of the API
569 // this can only retrieve a single certificate matching a given subject name.
570 // However it will fill the cache with all matching certificates, so we can
571 // examine the cache for all matches. Return values are: 1 lookup
572 // successful.  0 certificate not found. -1 some other error.
X509_STORE_CTX_get1_issuer(X509 ** issuer,X509_STORE_CTX * ctx,X509 * x)573 int X509_STORE_CTX_get1_issuer(X509 **issuer, X509_STORE_CTX *ctx, X509 *x) {
574   X509_NAME *xn;
575   X509_OBJECT obj, *pobj;
576   int idx, ret;
577   size_t i;
578   xn = X509_get_issuer_name(x);
579   if (!X509_STORE_get_by_subject(ctx, X509_LU_X509, xn, &obj)) {
580     return 0;
581   }
582   // If certificate matches all OK
583   if (ctx->check_issued(ctx, x, obj.data.x509)) {
584     *issuer = obj.data.x509;
585     return 1;
586   }
587   X509_OBJECT_free_contents(&obj);
588 
589   // Else find index of first cert accepted by 'check_issued'
590   ret = 0;
591   CRYPTO_MUTEX_lock_write(&ctx->ctx->objs_lock);
592   idx = X509_OBJECT_idx_by_subject(ctx->ctx->objs, X509_LU_X509, xn);
593   if (idx != -1) {  // should be true as we've had at least one
594                     // match
595     // Look through all matching certs for suitable issuer
596     for (i = idx; i < sk_X509_OBJECT_num(ctx->ctx->objs); i++) {
597       pobj = sk_X509_OBJECT_value(ctx->ctx->objs, i);
598       // See if we've run past the matches
599       if (pobj->type != X509_LU_X509) {
600         break;
601       }
602       if (X509_NAME_cmp(xn, X509_get_subject_name(pobj->data.x509))) {
603         break;
604       }
605       if (ctx->check_issued(ctx, x, pobj->data.x509)) {
606         *issuer = pobj->data.x509;
607         X509_OBJECT_up_ref_count(pobj);
608         ret = 1;
609         break;
610       }
611     }
612   }
613   CRYPTO_MUTEX_unlock_write(&ctx->ctx->objs_lock);
614   return ret;
615 }
616 
X509_STORE_set_flags(X509_STORE * ctx,unsigned long flags)617 int X509_STORE_set_flags(X509_STORE *ctx, unsigned long flags) {
618   return X509_VERIFY_PARAM_set_flags(ctx->param, flags);
619 }
620 
X509_STORE_set_depth(X509_STORE * ctx,int depth)621 int X509_STORE_set_depth(X509_STORE *ctx, int depth) {
622   X509_VERIFY_PARAM_set_depth(ctx->param, depth);
623   return 1;
624 }
625 
X509_STORE_set_purpose(X509_STORE * ctx,int purpose)626 int X509_STORE_set_purpose(X509_STORE *ctx, int purpose) {
627   return X509_VERIFY_PARAM_set_purpose(ctx->param, purpose);
628 }
629 
X509_STORE_set_trust(X509_STORE * ctx,int trust)630 int X509_STORE_set_trust(X509_STORE *ctx, int trust) {
631   return X509_VERIFY_PARAM_set_trust(ctx->param, trust);
632 }
633 
X509_STORE_set1_param(X509_STORE * ctx,X509_VERIFY_PARAM * param)634 int X509_STORE_set1_param(X509_STORE *ctx, X509_VERIFY_PARAM *param) {
635   return X509_VERIFY_PARAM_set1(ctx->param, param);
636 }
637 
X509_STORE_get0_param(X509_STORE * ctx)638 X509_VERIFY_PARAM *X509_STORE_get0_param(X509_STORE *ctx) { return ctx->param; }
639 
X509_STORE_set_verify(X509_STORE * ctx,X509_STORE_CTX_verify_fn verify)640 void X509_STORE_set_verify(X509_STORE *ctx, X509_STORE_CTX_verify_fn verify) {
641   ctx->verify = verify;
642 }
643 
X509_STORE_get_verify(X509_STORE * ctx)644 X509_STORE_CTX_verify_fn X509_STORE_get_verify(X509_STORE *ctx) {
645   return ctx->verify;
646 }
647 
X509_STORE_set_verify_cb(X509_STORE * ctx,X509_STORE_CTX_verify_cb verify_cb)648 void X509_STORE_set_verify_cb(X509_STORE *ctx,
649                               X509_STORE_CTX_verify_cb verify_cb) {
650   ctx->verify_cb = verify_cb;
651 }
652 
X509_STORE_get_verify_cb(X509_STORE * ctx)653 X509_STORE_CTX_verify_cb X509_STORE_get_verify_cb(X509_STORE *ctx) {
654   return ctx->verify_cb;
655 }
656 
X509_STORE_set_get_issuer(X509_STORE * ctx,X509_STORE_CTX_get_issuer_fn get_issuer)657 void X509_STORE_set_get_issuer(X509_STORE *ctx,
658                                X509_STORE_CTX_get_issuer_fn get_issuer) {
659   ctx->get_issuer = get_issuer;
660 }
661 
X509_STORE_get_get_issuer(X509_STORE * ctx)662 X509_STORE_CTX_get_issuer_fn X509_STORE_get_get_issuer(X509_STORE *ctx) {
663   return ctx->get_issuer;
664 }
665 
X509_STORE_set_check_issued(X509_STORE * ctx,X509_STORE_CTX_check_issued_fn check_issued)666 void X509_STORE_set_check_issued(X509_STORE *ctx,
667                                  X509_STORE_CTX_check_issued_fn check_issued) {
668   ctx->check_issued = check_issued;
669 }
670 
X509_STORE_get_check_issued(X509_STORE * ctx)671 X509_STORE_CTX_check_issued_fn X509_STORE_get_check_issued(X509_STORE *ctx) {
672   return ctx->check_issued;
673 }
674 
X509_STORE_set_check_revocation(X509_STORE * ctx,X509_STORE_CTX_check_revocation_fn check_revocation)675 void X509_STORE_set_check_revocation(
676     X509_STORE *ctx, X509_STORE_CTX_check_revocation_fn check_revocation) {
677   ctx->check_revocation = check_revocation;
678 }
679 
X509_STORE_get_check_revocation(X509_STORE * ctx)680 X509_STORE_CTX_check_revocation_fn X509_STORE_get_check_revocation(
681     X509_STORE *ctx) {
682   return ctx->check_revocation;
683 }
684 
X509_STORE_set_get_crl(X509_STORE * ctx,X509_STORE_CTX_get_crl_fn get_crl)685 void X509_STORE_set_get_crl(X509_STORE *ctx,
686                             X509_STORE_CTX_get_crl_fn get_crl) {
687   ctx->get_crl = get_crl;
688 }
689 
X509_STORE_get_get_crl(X509_STORE * ctx)690 X509_STORE_CTX_get_crl_fn X509_STORE_get_get_crl(X509_STORE *ctx) {
691   return ctx->get_crl;
692 }
693 
X509_STORE_set_check_crl(X509_STORE * ctx,X509_STORE_CTX_check_crl_fn check_crl)694 void X509_STORE_set_check_crl(X509_STORE *ctx,
695                               X509_STORE_CTX_check_crl_fn check_crl) {
696   ctx->check_crl = check_crl;
697 }
698 
X509_STORE_get_check_crl(X509_STORE * ctx)699 X509_STORE_CTX_check_crl_fn X509_STORE_get_check_crl(X509_STORE *ctx) {
700   return ctx->check_crl;
701 }
702 
X509_STORE_set_cert_crl(X509_STORE * ctx,X509_STORE_CTX_cert_crl_fn cert_crl)703 void X509_STORE_set_cert_crl(X509_STORE *ctx,
704                              X509_STORE_CTX_cert_crl_fn cert_crl) {
705   ctx->cert_crl = cert_crl;
706 }
707 
X509_STORE_get_cert_crl(X509_STORE * ctx)708 X509_STORE_CTX_cert_crl_fn X509_STORE_get_cert_crl(X509_STORE *ctx) {
709   return ctx->cert_crl;
710 }
711 
X509_STORE_set_lookup_certs(X509_STORE * ctx,X509_STORE_CTX_lookup_certs_fn lookup_certs)712 void X509_STORE_set_lookup_certs(X509_STORE *ctx,
713                                  X509_STORE_CTX_lookup_certs_fn lookup_certs) {
714   ctx->lookup_certs = lookup_certs;
715 }
716 
X509_STORE_get_lookup_certs(X509_STORE * ctx)717 X509_STORE_CTX_lookup_certs_fn X509_STORE_get_lookup_certs(X509_STORE *ctx) {
718   return ctx->lookup_certs;
719 }
720 
X509_STORE_set_lookup_crls(X509_STORE * ctx,X509_STORE_CTX_lookup_crls_fn lookup_crls)721 void X509_STORE_set_lookup_crls(X509_STORE *ctx,
722                                 X509_STORE_CTX_lookup_crls_fn lookup_crls) {
723   ctx->lookup_crls = lookup_crls;
724 }
725 
X509_STORE_get_lookup_crls(X509_STORE * ctx)726 X509_STORE_CTX_lookup_crls_fn X509_STORE_get_lookup_crls(X509_STORE *ctx) {
727   return ctx->lookup_crls;
728 }
729 
X509_STORE_set_cleanup(X509_STORE * ctx,X509_STORE_CTX_cleanup_fn ctx_cleanup)730 void X509_STORE_set_cleanup(X509_STORE *ctx,
731                             X509_STORE_CTX_cleanup_fn ctx_cleanup) {
732   ctx->cleanup = ctx_cleanup;
733 }
734 
X509_STORE_get_cleanup(X509_STORE * ctx)735 X509_STORE_CTX_cleanup_fn X509_STORE_get_cleanup(X509_STORE *ctx) {
736   return ctx->cleanup;
737 }
738 
X509_STORE_CTX_get0_store(X509_STORE_CTX * ctx)739 X509_STORE *X509_STORE_CTX_get0_store(X509_STORE_CTX *ctx) { return ctx->ctx; }
740