]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/x509/x509_lu.c
3b76b92f713856819cbccfcc2cdfa01fa9bf499c
[thirdparty/openssl.git] / crypto / x509 / x509_lu.c
1 /*
2 * Copyright 1995-2021 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 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
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
44 int X509_STORE_lock(X509_STORE *s)
45 {
46 return CRYPTO_THREAD_write_lock(s->lock);
47 }
48
49 int X509_STORE_unlock(X509_STORE *s)
50 {
51 return CRYPTO_THREAD_unlock(s->lock);
52 }
53
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
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
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
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
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
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
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
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
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
141 int X509_LOOKUP_set_method_data(X509_LOOKUP *ctx, void *data)
142 {
143 ctx->method_data = data;
144 return 1;
145 }
146
147 void *X509_LOOKUP_get_method_data(const X509_LOOKUP *ctx)
148 {
149 return ctx->method_data;
150 }
151
152 X509_STORE *X509_LOOKUP_get_store(const X509_LOOKUP *ctx)
153 {
154 return ctx->store_ctx;
155 }
156
157
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
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
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
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
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
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 */
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 X509_STORE_lock(store);
325 tmp = X509_OBJECT_retrieve_by_subject(store->objs, type, name);
326 X509_STORE_unlock(store);
327
328 if (tmp == NULL || type == X509_LU_CRL) {
329 for (i = 0; i < sk_X509_LOOKUP_num(store->get_cert_methods); i++) {
330 lu = sk_X509_LOOKUP_value(store->get_cert_methods, i);
331 j = X509_LOOKUP_by_subject_ex(lu, type, name, &stmp, vs->libctx,
332 vs->propq);
333 if (j) {
334 tmp = &stmp;
335 break;
336 }
337 }
338 if (tmp == NULL)
339 return 0;
340 }
341
342 if (!X509_OBJECT_up_ref_count(tmp))
343 return 0;
344
345 ret->type = tmp->type;
346 ret->data.ptr = tmp->data.ptr;
347
348 return 1;
349 }
350
351 static int x509_store_add(X509_STORE *store, void *x, int crl) {
352 X509_OBJECT *obj;
353 int ret = 0, added = 0;
354
355 if (x == NULL)
356 return 0;
357 obj = X509_OBJECT_new();
358 if (obj == NULL)
359 return 0;
360
361 if (crl) {
362 obj->type = X509_LU_CRL;
363 obj->data.crl = (X509_CRL *)x;
364 } else {
365 obj->type = X509_LU_X509;
366 obj->data.x509 = (X509 *)x;
367 }
368 if (!X509_OBJECT_up_ref_count(obj)) {
369 obj->type = X509_LU_NONE;
370 X509_OBJECT_free(obj);
371 return 0;
372 }
373
374 X509_STORE_lock(store);
375 if (X509_OBJECT_retrieve_match(store->objs, obj)) {
376 ret = 1;
377 } else {
378 added = sk_X509_OBJECT_push(store->objs, obj);
379 ret = added != 0;
380 }
381 X509_STORE_unlock(store);
382
383 if (added == 0) /* obj not pushed */
384 X509_OBJECT_free(obj);
385
386 return ret;
387 }
388
389 int X509_STORE_add_cert(X509_STORE *ctx, X509 *x)
390 {
391 if (!x509_store_add(ctx, x, 0)) {
392 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
393 return 0;
394 }
395 return 1;
396 }
397
398 int X509_STORE_add_crl(X509_STORE *ctx, X509_CRL *x)
399 {
400 if (!x509_store_add(ctx, x, 1)) {
401 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
402 return 0;
403 }
404 return 1;
405 }
406
407 int X509_OBJECT_up_ref_count(X509_OBJECT *a)
408 {
409 switch (a->type) {
410 case X509_LU_NONE:
411 break;
412 case X509_LU_X509:
413 return X509_up_ref(a->data.x509);
414 case X509_LU_CRL:
415 return X509_CRL_up_ref(a->data.crl);
416 }
417 return 1;
418 }
419
420 X509 *X509_OBJECT_get0_X509(const X509_OBJECT *a)
421 {
422 if (a == NULL || a->type != X509_LU_X509)
423 return NULL;
424 return a->data.x509;
425 }
426
427 X509_CRL *X509_OBJECT_get0_X509_CRL(const X509_OBJECT *a)
428 {
429 if (a == NULL || a->type != X509_LU_CRL)
430 return NULL;
431 return a->data.crl;
432 }
433
434 X509_LOOKUP_TYPE X509_OBJECT_get_type(const X509_OBJECT *a)
435 {
436 return a->type;
437 }
438
439 X509_OBJECT *X509_OBJECT_new(void)
440 {
441 X509_OBJECT *ret = OPENSSL_zalloc(sizeof(*ret));
442
443 if (ret == NULL) {
444 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
445 return NULL;
446 }
447 ret->type = X509_LU_NONE;
448 return ret;
449 }
450
451 static void x509_object_free_internal(X509_OBJECT *a)
452 {
453 if (a == NULL)
454 return;
455 switch (a->type) {
456 case X509_LU_NONE:
457 break;
458 case X509_LU_X509:
459 X509_free(a->data.x509);
460 break;
461 case X509_LU_CRL:
462 X509_CRL_free(a->data.crl);
463 break;
464 }
465 }
466
467 int X509_OBJECT_set1_X509(X509_OBJECT *a, X509 *obj)
468 {
469 if (a == NULL || !X509_up_ref(obj))
470 return 0;
471
472 x509_object_free_internal(a);
473 a->type = X509_LU_X509;
474 a->data.x509 = obj;
475 return 1;
476 }
477
478 int X509_OBJECT_set1_X509_CRL(X509_OBJECT *a, X509_CRL *obj)
479 {
480 if (a == NULL || !X509_CRL_up_ref(obj))
481 return 0;
482
483 x509_object_free_internal(a);
484 a->type = X509_LU_CRL;
485 a->data.crl = obj;
486 return 1;
487 }
488
489 void X509_OBJECT_free(X509_OBJECT *a)
490 {
491 x509_object_free_internal(a);
492 OPENSSL_free(a);
493 }
494
495 static int x509_object_idx_cnt(STACK_OF(X509_OBJECT) *h, X509_LOOKUP_TYPE type,
496 const X509_NAME *name, int *pnmatch)
497 {
498 X509_OBJECT stmp;
499 X509 x509_s;
500 X509_CRL crl_s;
501 int idx;
502
503 stmp.type = type;
504 switch (type) {
505 case X509_LU_X509:
506 stmp.data.x509 = &x509_s;
507 x509_s.cert_info.subject = (X509_NAME *)name; /* won't modify it */
508 break;
509 case X509_LU_CRL:
510 stmp.data.crl = &crl_s;
511 crl_s.crl.issuer = (X509_NAME *)name; /* won't modify it */
512 break;
513 case X509_LU_NONE:
514 /* abort(); */
515 return -1;
516 }
517
518 idx = sk_X509_OBJECT_find_all(h, &stmp, pnmatch);
519 return idx;
520 }
521
522 int X509_OBJECT_idx_by_subject(STACK_OF(X509_OBJECT) *h, X509_LOOKUP_TYPE type,
523 const X509_NAME *name)
524 {
525 return x509_object_idx_cnt(h, type, name, NULL);
526 }
527
528 X509_OBJECT *X509_OBJECT_retrieve_by_subject(STACK_OF(X509_OBJECT) *h,
529 X509_LOOKUP_TYPE type,
530 const X509_NAME *name)
531 {
532 int idx;
533 idx = X509_OBJECT_idx_by_subject(h, type, name);
534 if (idx == -1)
535 return NULL;
536 return sk_X509_OBJECT_value(h, idx);
537 }
538
539 STACK_OF(X509_OBJECT) *X509_STORE_get0_objects(const X509_STORE *v)
540 {
541 return v->objs;
542 }
543
544 STACK_OF(X509) *X509_STORE_get1_all_certs(X509_STORE *store)
545 {
546 STACK_OF(X509) *sk;
547 STACK_OF(X509_OBJECT) *objs;
548 int i;
549
550 if (store == NULL) {
551 ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
552 return NULL;
553 }
554 if ((sk = sk_X509_new_null()) == NULL)
555 return NULL;
556 X509_STORE_lock(store);
557 objs = X509_STORE_get0_objects(store);
558 for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
559 X509 *cert = X509_OBJECT_get0_X509(sk_X509_OBJECT_value(objs, i));
560
561 if (cert != NULL
562 && !X509_add_cert(sk, cert, X509_ADD_FLAG_UP_REF))
563 goto err;
564 }
565 X509_STORE_unlock(store);
566 return sk;
567
568 err:
569 X509_STORE_unlock(store);
570 sk_X509_pop_free(sk, X509_free);
571 return NULL;
572 }
573
574 STACK_OF(X509) *X509_STORE_CTX_get1_certs(X509_STORE_CTX *ctx,
575 const X509_NAME *nm)
576 {
577 int i, idx, cnt;
578 STACK_OF(X509) *sk = NULL;
579 X509 *x;
580 X509_OBJECT *obj;
581 X509_STORE *store = ctx->store;
582
583 if (store == NULL)
584 return NULL;
585
586 X509_STORE_lock(store);
587 idx = x509_object_idx_cnt(store->objs, X509_LU_X509, nm, &cnt);
588 if (idx < 0) {
589 /*
590 * Nothing found in cache: do lookup to possibly add new objects to
591 * cache
592 */
593 X509_OBJECT *xobj = X509_OBJECT_new();
594
595 X509_STORE_unlock(store);
596
597 if (xobj == NULL)
598 return NULL;
599 if (!X509_STORE_CTX_get_by_subject(ctx, X509_LU_X509, nm, xobj)) {
600 X509_OBJECT_free(xobj);
601 return NULL;
602 }
603 X509_OBJECT_free(xobj);
604 X509_STORE_lock(store);
605 idx = x509_object_idx_cnt(store->objs, X509_LU_X509, nm, &cnt);
606 if (idx < 0) {
607 X509_STORE_unlock(store);
608 return NULL;
609 }
610 }
611
612 sk = sk_X509_new_null();
613 for (i = 0; i < cnt; i++, idx++) {
614 obj = sk_X509_OBJECT_value(store->objs, idx);
615 x = obj->data.x509;
616 if (!X509_add_cert(sk, x, X509_ADD_FLAG_UP_REF)) {
617 X509_STORE_unlock(store);
618 sk_X509_pop_free(sk, X509_free);
619 return NULL;
620 }
621 }
622 X509_STORE_unlock(store);
623 return sk;
624 }
625
626 STACK_OF(X509_CRL) *X509_STORE_CTX_get1_crls(const X509_STORE_CTX *ctx,
627 const X509_NAME *nm)
628 {
629 int i, idx, cnt;
630 STACK_OF(X509_CRL) *sk = sk_X509_CRL_new_null();
631 X509_CRL *x;
632 X509_OBJECT *obj, *xobj = X509_OBJECT_new();
633 X509_STORE *store = ctx->store;
634
635 /* Always do lookup to possibly add new CRLs to cache */
636 if (sk == NULL
637 || xobj == NULL
638 || store == NULL
639 || !X509_STORE_CTX_get_by_subject(ctx, X509_LU_CRL, nm, xobj)) {
640 X509_OBJECT_free(xobj);
641 sk_X509_CRL_free(sk);
642 return NULL;
643 }
644 X509_OBJECT_free(xobj);
645 X509_STORE_lock(store);
646 idx = x509_object_idx_cnt(store->objs, X509_LU_CRL, nm, &cnt);
647 if (idx < 0) {
648 X509_STORE_unlock(store);
649 sk_X509_CRL_free(sk);
650 return NULL;
651 }
652
653 for (i = 0; i < cnt; i++, idx++) {
654 obj = sk_X509_OBJECT_value(store->objs, idx);
655 x = obj->data.crl;
656 if (!X509_CRL_up_ref(x)) {
657 X509_STORE_unlock(store);
658 sk_X509_CRL_pop_free(sk, X509_CRL_free);
659 return NULL;
660 }
661 if (!sk_X509_CRL_push(sk, x)) {
662 X509_STORE_unlock(store);
663 X509_CRL_free(x);
664 sk_X509_CRL_pop_free(sk, X509_CRL_free);
665 return NULL;
666 }
667 }
668 X509_STORE_unlock(store);
669 return sk;
670 }
671
672 X509_OBJECT *X509_OBJECT_retrieve_match(STACK_OF(X509_OBJECT) *h,
673 X509_OBJECT *x)
674 {
675 int idx, i, num;
676 X509_OBJECT *obj;
677
678 idx = sk_X509_OBJECT_find(h, x);
679 if (idx < 0)
680 return NULL;
681 if ((x->type != X509_LU_X509) && (x->type != X509_LU_CRL))
682 return sk_X509_OBJECT_value(h, idx);
683 for (i = idx, num = sk_X509_OBJECT_num(h); i < num; i++) {
684 obj = sk_X509_OBJECT_value(h, i);
685 if (x509_object_cmp((const X509_OBJECT **)&obj,
686 (const X509_OBJECT **)&x))
687 return NULL;
688 if (x->type == X509_LU_X509) {
689 if (!X509_cmp(obj->data.x509, x->data.x509))
690 return obj;
691 } else if (x->type == X509_LU_CRL) {
692 if (X509_CRL_match(obj->data.crl, x->data.crl) == 0)
693 return obj;
694 } else
695 return obj;
696 }
697 return NULL;
698 }
699
700 /*-
701 * Try to get issuer cert from |ctx->store| matching the subject name of |x|.
702 * Prefer the first non-expired one, else take the most recently expired one.
703 *
704 * Return values are:
705 * 1 lookup successful.
706 * 0 certificate not found.
707 * -1 some other error.
708 */
709 int X509_STORE_CTX_get1_issuer(X509 **issuer, X509_STORE_CTX *ctx, X509 *x)
710 {
711 const X509_NAME *xn;
712 X509_OBJECT *obj = X509_OBJECT_new(), *pobj = NULL;
713 X509_STORE *store = ctx->store;
714 int i, ok, idx, ret, nmatch = 0;
715
716 if (obj == NULL)
717 return -1;
718 *issuer = NULL;
719 xn = X509_get_issuer_name(x);
720 ok = X509_STORE_CTX_get_by_subject(ctx, X509_LU_X509, xn, obj);
721 if (ok != 1) {
722 X509_OBJECT_free(obj);
723 return 0;
724 }
725 /* If certificate matches and is currently valid all OK */
726 if (ctx->check_issued(ctx, x, obj->data.x509)) {
727 if (ossl_x509_check_cert_time(ctx, obj->data.x509, -1)) {
728 *issuer = obj->data.x509;
729 /* |*issuer| has taken over the cert reference from |obj| */
730 obj->type = X509_LU_NONE;
731 X509_OBJECT_free(obj);
732 return 1;
733 }
734 }
735 X509_OBJECT_free(obj);
736
737 /*
738 * Due to limitations of the API this can only retrieve a single cert.
739 * However it will fill the cache with all matching certificates,
740 * so we can examine the cache for all matches.
741 */
742 if (store == NULL)
743 return 0;
744
745 /* Find index of first currently valid cert accepted by 'check_issued' */
746 ret = 0;
747 X509_STORE_lock(store);
748 idx = x509_object_idx_cnt(store->objs, X509_LU_X509, xn, &nmatch);
749 if (idx != -1) { /* should be true as we've had at least one match */
750 /* Look through all matching certs for suitable issuer */
751 for (i = idx; i < idx + nmatch; i++) {
752 pobj = sk_X509_OBJECT_value(store->objs, i);
753 /* See if we've run past the matches */
754 if (pobj->type != X509_LU_X509)
755 break;
756 if (ctx->check_issued(ctx, x, pobj->data.x509)) {
757 ret = 1;
758 /* If times check fine, exit with match, else keep looking. */
759 if (ossl_x509_check_cert_time(ctx, pobj->data.x509, -1)) {
760 *issuer = pobj->data.x509;
761 break;
762 }
763 /*
764 * Leave the so far most recently expired match in *issuer
765 * so we return nearest match if no certificate time is OK.
766 */
767 if (*issuer == NULL
768 || ASN1_TIME_compare(X509_get0_notAfter(pobj->data.x509),
769 X509_get0_notAfter(*issuer)) > 0)
770 *issuer = pobj->data.x509;
771 }
772 }
773 }
774 if (*issuer != NULL && !X509_up_ref(*issuer)) {
775 *issuer = NULL;
776 ret = -1;
777 }
778 X509_STORE_unlock(store);
779 return ret;
780 }
781
782 int X509_STORE_set_flags(X509_STORE *ctx, unsigned long flags)
783 {
784 return X509_VERIFY_PARAM_set_flags(ctx->param, flags);
785 }
786
787 int X509_STORE_set_depth(X509_STORE *ctx, int depth)
788 {
789 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
790 return 1;
791 }
792
793 int X509_STORE_set_purpose(X509_STORE *ctx, int purpose)
794 {
795 return X509_VERIFY_PARAM_set_purpose(ctx->param, purpose);
796 }
797
798 int X509_STORE_set_trust(X509_STORE *ctx, int trust)
799 {
800 return X509_VERIFY_PARAM_set_trust(ctx->param, trust);
801 }
802
803 int X509_STORE_set1_param(X509_STORE *ctx, const X509_VERIFY_PARAM *param)
804 {
805 return X509_VERIFY_PARAM_set1(ctx->param, param);
806 }
807
808 X509_VERIFY_PARAM *X509_STORE_get0_param(const X509_STORE *ctx)
809 {
810 return ctx->param;
811 }
812
813 void X509_STORE_set_verify(X509_STORE *ctx, X509_STORE_CTX_verify_fn verify)
814 {
815 ctx->verify = verify;
816 }
817
818 X509_STORE_CTX_verify_fn X509_STORE_get_verify(const X509_STORE *ctx)
819 {
820 return ctx->verify;
821 }
822
823 void X509_STORE_set_verify_cb(X509_STORE *ctx,
824 X509_STORE_CTX_verify_cb verify_cb)
825 {
826 ctx->verify_cb = verify_cb;
827 }
828
829 X509_STORE_CTX_verify_cb X509_STORE_get_verify_cb(const X509_STORE *ctx)
830 {
831 return ctx->verify_cb;
832 }
833
834 void X509_STORE_set_get_issuer(X509_STORE *ctx,
835 X509_STORE_CTX_get_issuer_fn get_issuer)
836 {
837 ctx->get_issuer = get_issuer;
838 }
839
840 X509_STORE_CTX_get_issuer_fn X509_STORE_get_get_issuer(const X509_STORE *ctx)
841 {
842 return ctx->get_issuer;
843 }
844
845 void X509_STORE_set_check_issued(X509_STORE *ctx,
846 X509_STORE_CTX_check_issued_fn check_issued)
847 {
848 ctx->check_issued = check_issued;
849 }
850
851 X509_STORE_CTX_check_issued_fn X509_STORE_get_check_issued(const X509_STORE *ctx)
852 {
853 return ctx->check_issued;
854 }
855
856 void X509_STORE_set_check_revocation(X509_STORE *ctx,
857 X509_STORE_CTX_check_revocation_fn check_revocation)
858 {
859 ctx->check_revocation = check_revocation;
860 }
861
862 X509_STORE_CTX_check_revocation_fn X509_STORE_get_check_revocation(const X509_STORE *ctx)
863 {
864 return ctx->check_revocation;
865 }
866
867 void X509_STORE_set_get_crl(X509_STORE *ctx,
868 X509_STORE_CTX_get_crl_fn get_crl)
869 {
870 ctx->get_crl = get_crl;
871 }
872
873 X509_STORE_CTX_get_crl_fn X509_STORE_get_get_crl(const X509_STORE *ctx)
874 {
875 return ctx->get_crl;
876 }
877
878 void X509_STORE_set_check_crl(X509_STORE *ctx,
879 X509_STORE_CTX_check_crl_fn check_crl)
880 {
881 ctx->check_crl = check_crl;
882 }
883
884 X509_STORE_CTX_check_crl_fn X509_STORE_get_check_crl(const X509_STORE *ctx)
885 {
886 return ctx->check_crl;
887 }
888
889 void X509_STORE_set_cert_crl(X509_STORE *ctx,
890 X509_STORE_CTX_cert_crl_fn cert_crl)
891 {
892 ctx->cert_crl = cert_crl;
893 }
894
895 X509_STORE_CTX_cert_crl_fn X509_STORE_get_cert_crl(const X509_STORE *ctx)
896 {
897 return ctx->cert_crl;
898 }
899
900 void X509_STORE_set_check_policy(X509_STORE *ctx,
901 X509_STORE_CTX_check_policy_fn check_policy)
902 {
903 ctx->check_policy = check_policy;
904 }
905
906 X509_STORE_CTX_check_policy_fn X509_STORE_get_check_policy(const X509_STORE *ctx)
907 {
908 return ctx->check_policy;
909 }
910
911 void X509_STORE_set_lookup_certs(X509_STORE *ctx,
912 X509_STORE_CTX_lookup_certs_fn lookup_certs)
913 {
914 ctx->lookup_certs = lookup_certs;
915 }
916
917 X509_STORE_CTX_lookup_certs_fn X509_STORE_get_lookup_certs(const X509_STORE *ctx)
918 {
919 return ctx->lookup_certs;
920 }
921
922 void X509_STORE_set_lookup_crls(X509_STORE *ctx,
923 X509_STORE_CTX_lookup_crls_fn lookup_crls)
924 {
925 ctx->lookup_crls = lookup_crls;
926 }
927
928 X509_STORE_CTX_lookup_crls_fn X509_STORE_get_lookup_crls(const X509_STORE *ctx)
929 {
930 return ctx->lookup_crls;
931 }
932
933 void X509_STORE_set_cleanup(X509_STORE *ctx,
934 X509_STORE_CTX_cleanup_fn ctx_cleanup)
935 {
936 ctx->cleanup = ctx_cleanup;
937 }
938
939 X509_STORE_CTX_cleanup_fn X509_STORE_get_cleanup(const X509_STORE *ctx)
940 {
941 return ctx->cleanup;
942 }
943
944 int X509_STORE_set_ex_data(X509_STORE *ctx, int idx, void *data)
945 {
946 return CRYPTO_set_ex_data(&ctx->ex_data, idx, data);
947 }
948
949 void *X509_STORE_get_ex_data(const X509_STORE *ctx, int idx)
950 {
951 return CRYPTO_get_ex_data(&ctx->ex_data, idx);
952 }
953
954 X509_STORE *X509_STORE_CTX_get0_store(const X509_STORE_CTX *ctx)
955 {
956 return ctx->store;
957 }