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