]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/x509/x509_lu.c
test/ct_test.c: Add the missing check after calling sk_SCT_new_null
[thirdparty/openssl.git] / crypto / x509 / x509_lu.c
CommitLineData
b1322259 1/*
4333b89f 2 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
3e4b43b9 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
b1322259
RS
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
d02b48c6
RE
8 */
9
10#include <stdio.h>
b39fc560 11#include "internal/cryptlib.h"
cd420b0b 12#include "internal/refcount.h"
ec577822 13#include <openssl/x509.h>
25f2138b 14#include "crypto/x509.h"
926a56bf 15#include <openssl/x509v3.h>
706457b7 16#include "x509_local.h"
d02b48c6 17
6b691a5c 18X509_LOOKUP *X509_LOOKUP_new(X509_LOOKUP_METHOD *method)
0f113f3e 19{
7fcdbd83 20 X509_LOOKUP *ret = OPENSSL_zalloc(sizeof(*ret));
0f113f3e 21
7fcdbd83 22 if (ret == NULL) {
9311d0c4 23 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
0f113f3e 24 return NULL;
7fcdbd83 25 }
0f113f3e 26
0f113f3e 27 ret->method = method;
7fcdbd83 28 if (method->new_item != NULL && method->new_item(ret) == 0) {
0f113f3e
MC
29 OPENSSL_free(ret);
30 return NULL;
31 }
32 return ret;
33}
d02b48c6 34
6b691a5c 35void X509_LOOKUP_free(X509_LOOKUP *ctx)
0f113f3e
MC
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}
d02b48c6 43
3067095e
RL
44int X509_STORE_lock(X509_STORE *s)
45{
46 return CRYPTO_THREAD_write_lock(s->lock);
47}
48
49int X509_STORE_unlock(X509_STORE *s)
50{
51 return CRYPTO_THREAD_unlock(s->lock);
52}
53
6b691a5c 54int X509_LOOKUP_init(X509_LOOKUP *ctx)
0f113f3e
MC
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}
d02b48c6 63
6b691a5c 64int X509_LOOKUP_shutdown(X509_LOOKUP *ctx)
0f113f3e
MC
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}
d02b48c6 73
d8652be0 74int X509_LOOKUP_ctrl_ex(X509_LOOKUP *ctx, int cmd, const char *argc, long argl,
b4250010 75 char **ret, OSSL_LIB_CTX *libctx, const char *propq)
0f113f3e
MC
76{
77 if (ctx->method == NULL)
78 return -1;
d8652be0
MC
79 if (ctx->method->ctrl_ex != NULL)
80 return ctx->method->ctrl_ex(ctx, cmd, argc, argl, ret, libctx, propq);
0f113f3e
MC
81 if (ctx->method->ctrl != NULL)
82 return ctx->method->ctrl(ctx, cmd, argc, argl, ret);
6725682d
SL
83 return 1;
84}
85
86int X509_LOOKUP_ctrl(X509_LOOKUP *ctx, int cmd, const char *argc, long argl,
87 char **ret)
88{
d8652be0 89 return X509_LOOKUP_ctrl_ex(ctx, cmd, argc, argl, ret, NULL, NULL);
6725682d
SL
90}
91
d8652be0
MC
92int X509_LOOKUP_by_subject_ex(X509_LOOKUP *ctx, X509_LOOKUP_TYPE type,
93 const X509_NAME *name, X509_OBJECT *ret,
b4250010 94 OSSL_LIB_CTX *libctx, const char *propq)
6725682d
SL
95{
96 if (ctx->skip
97 || ctx->method == NULL
98 || (ctx->method->get_by_subject == NULL
d8652be0 99 && ctx->method->get_by_subject_ex == NULL))
6725682d 100 return 0;
d8652be0
MC
101 if (ctx->method->get_by_subject_ex != NULL)
102 return ctx->method->get_by_subject_ex(ctx, type, name, ret, libctx,
103 propq);
0f113f3e 104 else
6725682d 105 return ctx->method->get_by_subject(ctx, type, name, ret);
0f113f3e 106}
d02b48c6 107
0946a198 108int X509_LOOKUP_by_subject(X509_LOOKUP *ctx, X509_LOOKUP_TYPE type,
8cc86b81 109 const X509_NAME *name, X509_OBJECT *ret)
0f113f3e 110{
d8652be0 111 return X509_LOOKUP_by_subject_ex(ctx, type, name, ret, NULL, NULL);
0f113f3e 112}
d02b48c6 113
0946a198 114int X509_LOOKUP_by_issuer_serial(X509_LOOKUP *ctx, X509_LOOKUP_TYPE type,
8cc86b81
DDO
115 const X509_NAME *name,
116 const ASN1_INTEGER *serial,
0946a198 117 X509_OBJECT *ret)
0f113f3e
MC
118{
119 if ((ctx->method == NULL) || (ctx->method->get_by_issuer_serial == NULL))
ba1a1c37 120 return 0;
0f113f3e
MC
121 return ctx->method->get_by_issuer_serial(ctx, type, name, serial, ret);
122}
d02b48c6 123
0946a198 124int X509_LOOKUP_by_fingerprint(X509_LOOKUP *ctx, X509_LOOKUP_TYPE type,
cfc5e0aa 125 const unsigned char *bytes, int len,
0f113f3e
MC
126 X509_OBJECT *ret)
127{
128 if ((ctx->method == NULL) || (ctx->method->get_by_fingerprint == NULL))
ba1a1c37 129 return 0;
0f113f3e
MC
130 return ctx->method->get_by_fingerprint(ctx, type, bytes, len, ret);
131}
d02b48c6 132
0946a198 133int X509_LOOKUP_by_alias(X509_LOOKUP *ctx, X509_LOOKUP_TYPE type,
cfc5e0aa 134 const char *str, int len, X509_OBJECT *ret)
0f113f3e
MC
135{
136 if ((ctx->method == NULL) || (ctx->method->get_by_alias == NULL))
ba1a1c37 137 return 0;
0f113f3e
MC
138 return ctx->method->get_by_alias(ctx, type, str, len, ret);
139}
140
0124f32a
MY
141int X509_LOOKUP_set_method_data(X509_LOOKUP *ctx, void *data)
142{
143 ctx->method_data = data;
144 return 1;
145}
146
147void *X509_LOOKUP_get_method_data(const X509_LOOKUP *ctx)
148{
149 return ctx->method_data;
150}
151
152X509_STORE *X509_LOOKUP_get_store(const X509_LOOKUP *ctx)
153{
154 return ctx->store_ctx;
155}
156
157
0f113f3e
MC
158static 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;
f3b3d7f0 173 case X509_LU_NONE:
0f113f3e
MC
174 /* abort(); */
175 return 0;
176 }
177 return ret;
178}
d02b48c6 179
6b691a5c 180X509_STORE *X509_STORE_new(void)
0f113f3e 181{
7fcdbd83 182 X509_STORE *ret = OPENSSL_zalloc(sizeof(*ret));
0f113f3e 183
7fcdbd83 184 if (ret == NULL) {
9311d0c4 185 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
0f113f3e 186 return NULL;
7fcdbd83
F
187 }
188 if ((ret->objs = sk_X509_OBJECT_new(x509_object_cmp)) == NULL) {
9311d0c4 189 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
90945fa3 190 goto err;
7fcdbd83 191 }
0f113f3e 192 ret->cache = 1;
7fcdbd83 193 if ((ret->get_cert_methods = sk_X509_LOOKUP_new_null()) == NULL) {
9311d0c4 194 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
90945fa3 195 goto err;
7fcdbd83 196 }
0f113f3e 197
7fcdbd83 198 if ((ret->param = X509_VERIFY_PARAM_new()) == NULL) {
9311d0c4 199 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
90945fa3 200 goto err;
7fcdbd83
F
201 }
202 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509_STORE, ret, &ret->ex_data)) {
9311d0c4 203 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
90945fa3 204 goto err;
7fcdbd83 205 }
0f113f3e 206
c001ce33 207 ret->lock = CRYPTO_THREAD_lock_new();
7fcdbd83 208 if (ret->lock == NULL) {
9311d0c4 209 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
c001ce33 210 goto err;
7fcdbd83 211 }
0f113f3e
MC
212 ret->references = 1;
213 return ret;
c001ce33 214
90945fa3
MC
215err:
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;
0f113f3e 221}
d02b48c6 222
6b691a5c 223void X509_STORE_free(X509_STORE *vfy)
0f113f3e
MC
224{
225 int i;
226 STACK_OF(X509_LOOKUP) *sk;
227 X509_LOOKUP *lu;
d02b48c6 228
e6e9170d
RS
229 if (vfy == NULL)
230 return;
2f545ae4 231 CRYPTO_DOWN_REF(&vfy->references, &i, vfy->lock);
f3f1cf84 232 REF_PRINT_COUNT("X509_STORE", vfy);
0f113f3e
MC
233 if (i > 0)
234 return;
f3f1cf84 235 REF_ASSERT_ISNT(i < 0);
bff9ce4d 236
0f113f3e
MC
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);
b6ef12c4 244 sk_X509_OBJECT_pop_free(vfy->objs, X509_OBJECT_free);
0f113f3e
MC
245
246 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509_STORE, vfy, &vfy->ex_data);
222561fe 247 X509_VERIFY_PARAM_free(vfy->param);
c001ce33 248 CRYPTO_THREAD_lock_free(vfy->lock);
0f113f3e
MC
249 OPENSSL_free(vfy);
250}
d02b48c6 251
c001ce33
AG
252int X509_STORE_up_ref(X509_STORE *vfy)
253{
254 int i;
255
2f545ae4 256 if (CRYPTO_UP_REF(&vfy->references, &i, vfy->lock) <= 0)
c001ce33
AG
257 return 0;
258
e26f653d 259 REF_PRINT_COUNT("X509_STORE", vfy);
c001ce33
AG
260 REF_ASSERT_ISNT(i < 2);
261 return ((i > 1) ? 1 : 0);
262}
263
6b691a5c 264X509_LOOKUP *X509_STORE_add_lookup(X509_STORE *v, X509_LOOKUP_METHOD *m)
0f113f3e
MC
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);
7fcdbd83 279 if (lu == NULL) {
9311d0c4 280 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
0f113f3e 281 return NULL;
0f113f3e 282 }
7fcdbd83
F
283
284 lu->store_ctx = v;
285 if (sk_X509_LOOKUP_push(v->get_cert_methods, lu))
286 return lu;
287 /* malloc failed */
9311d0c4 288 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
7fcdbd83
F
289 X509_LOOKUP_free(lu);
290 return NULL;
0f113f3e 291}
d02b48c6 292
0946a198
DSH
293X509_OBJECT *X509_STORE_CTX_get_obj_by_subject(X509_STORE_CTX *vs,
294 X509_LOOKUP_TYPE type,
8cc86b81 295 const X509_NAME *name)
f0e0fd51 296{
6ddbb4cd 297 X509_OBJECT *ret = X509_OBJECT_new();
f0e0fd51 298
6ddbb4cd 299 if (ret == NULL)
f0e0fd51 300 return NULL;
6ddbb4cd
RS
301 if (!X509_STORE_CTX_get_by_subject(vs, type, name, ret)) {
302 X509_OBJECT_free(ret);
f0e0fd51
RS
303 return NULL;
304 }
305 return ret;
306}
307
f1923a21 308/* Also fill the cache with all matching certificates */
8cc86b81
DDO
309int X509_STORE_CTX_get_by_subject(const X509_STORE_CTX *vs,
310 X509_LOOKUP_TYPE type,
311 const X509_NAME *name, X509_OBJECT *ret)
0f113f3e 312{
faa9dcd4 313 X509_STORE *store = vs->store;
0f113f3e
MC
314 X509_LOOKUP *lu;
315 X509_OBJECT stmp, *tmp;
316 int i, j;
317
7a9abccd 318 if (store == NULL)
1c705121
MC
319 return 0;
320
b926f9de
MC
321 stmp.type = X509_LU_NONE;
322 stmp.data.ptr = NULL;
323
7a9abccd
SL
324 X509_STORE_lock(store);
325 tmp = X509_OBJECT_retrieve_by_subject(store->objs, type, name);
326 X509_STORE_unlock(store);
0f113f3e
MC
327
328 if (tmp == NULL || type == X509_LU_CRL) {
7a9abccd
SL
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);
d8652be0
MC
331 j = X509_LOOKUP_by_subject_ex(lu, type, name, &stmp, vs->libctx,
332 vs->propq);
fc9d1ef3 333 if (j) {
0f113f3e
MC
334 tmp = &stmp;
335 break;
336 }
337 }
0f113f3e
MC
338 if (tmp == NULL)
339 return 0;
340 }
341
c70e2ec3
BE
342 if (!X509_OBJECT_up_ref_count(tmp))
343 return 0;
344
0f113f3e
MC
345 ret->type = tmp->type;
346 ret->data.ptr = tmp->data.ptr;
347
0f113f3e
MC
348 return 1;
349}
d02b48c6 350
7a9abccd 351static int x509_store_add(X509_STORE *store, void *x, int crl) {
0f113f3e 352 X509_OBJECT *obj;
c0452248 353 int ret = 0, added = 0;
0f113f3e
MC
354
355 if (x == NULL)
356 return 0;
6ddbb4cd
RS
357 obj = X509_OBJECT_new();
358 if (obj == NULL)
0f113f3e 359 return 0;
c0452248
RS
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 }
c70e2ec3
BE
368 if (!X509_OBJECT_up_ref_count(obj)) {
369 obj->type = X509_LU_NONE;
370 X509_OBJECT_free(obj);
371 return 0;
372 }
0f113f3e 373
7a9abccd
SL
374 X509_STORE_lock(store);
375 if (X509_OBJECT_retrieve_match(store->objs, obj)) {
c0452248 376 ret = 1;
68efafc5 377 } else {
7a9abccd 378 added = sk_X509_OBJECT_push(store->objs, obj);
68efafc5
F
379 ret = added != 0;
380 }
7a9abccd 381 X509_STORE_unlock(store);
0f113f3e 382
c0452248 383 if (added == 0) /* obj not pushed */
68efafc5 384 X509_OBJECT_free(obj);
68efafc5 385
0f113f3e
MC
386 return ret;
387}
2f043896 388
c0452248 389int X509_STORE_add_cert(X509_STORE *ctx, X509 *x)
0f113f3e 390{
c0452248 391 if (!x509_store_add(ctx, x, 0)) {
9311d0c4 392 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
0f113f3e 393 return 0;
68efafc5 394 }
c0452248
RS
395 return 1;
396}
0f113f3e 397
c0452248
RS
398int X509_STORE_add_crl(X509_STORE *ctx, X509_CRL *x)
399{
400 if (!x509_store_add(ctx, x, 1)) {
9311d0c4 401 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
c0452248
RS
402 return 0;
403 }
404 return 1;
0f113f3e 405}
2f043896 406
c5ebfcab 407int X509_OBJECT_up_ref_count(X509_OBJECT *a)
0f113f3e
MC
408{
409 switch (a->type) {
f3b3d7f0 410 case X509_LU_NONE:
bca3f06b 411 break;
0f113f3e 412 case X509_LU_X509:
c5ebfcab 413 return X509_up_ref(a->data.x509);
0f113f3e 414 case X509_LU_CRL:
c5ebfcab 415 return X509_CRL_up_ref(a->data.crl);
0f113f3e 416 }
c5ebfcab 417 return 1;
0f113f3e 418}
d02b48c6 419
7d7da288 420X509 *X509_OBJECT_get0_X509(const X509_OBJECT *a)
f0e0fd51 421{
6ddbb4cd
RS
422 if (a == NULL || a->type != X509_LU_X509)
423 return NULL;
f0e0fd51
RS
424 return a->data.x509;
425}
426
8cc86b81 427X509_CRL *X509_OBJECT_get0_X509_CRL(const X509_OBJECT *a)
6ddbb4cd
RS
428{
429 if (a == NULL || a->type != X509_LU_CRL)
430 return NULL;
431 return a->data.crl;
432}
433
0946a198 434X509_LOOKUP_TYPE X509_OBJECT_get_type(const X509_OBJECT *a)
f0c58c32
CH
435{
436 return a->type;
437}
438
3cb7c5cf 439X509_OBJECT *X509_OBJECT_new(void)
f0e0fd51 440{
6ddbb4cd
RS
441 X509_OBJECT *ret = OPENSSL_zalloc(sizeof(*ret));
442
443 if (ret == NULL) {
9311d0c4 444 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
6ddbb4cd
RS
445 return NULL;
446 }
ba1a1c37 447 ret->type = X509_LU_NONE;
6ddbb4cd 448 return ret;
f0e0fd51
RS
449}
450
0124f32a 451static void x509_object_free_internal(X509_OBJECT *a)
0f113f3e 452{
f0e0fd51 453 if (a == NULL)
222561fe 454 return;
0f113f3e 455 switch (a->type) {
f3b3d7f0 456 case X509_LU_NONE:
bca3f06b 457 break;
0f113f3e
MC
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 }
0124f32a
MY
465}
466
467int 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
478int 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
489void X509_OBJECT_free(X509_OBJECT *a)
490{
491 x509_object_free_internal(a);
6ddbb4cd 492 OPENSSL_free(a);
0f113f3e 493}
d02b48c6 494
0946a198 495static int x509_object_idx_cnt(STACK_OF(X509_OBJECT) *h, X509_LOOKUP_TYPE type,
8cc86b81 496 const X509_NAME *name, int *pnmatch)
0f113f3e
MC
497{
498 X509_OBJECT stmp;
499 X509 x509_s;
0f113f3e 500 X509_CRL crl_s;
0f113f3e
MC
501 int idx;
502
503 stmp.type = type;
504 switch (type) {
505 case X509_LU_X509:
506 stmp.data.x509 = &x509_s;
8cc86b81 507 x509_s.cert_info.subject = (X509_NAME *)name; /* won't modify it */
0f113f3e
MC
508 break;
509 case X509_LU_CRL:
510 stmp.data.crl = &crl_s;
8cc86b81 511 crl_s.crl.issuer = (X509_NAME *)name; /* won't modify it */
0f113f3e 512 break;
f3b3d7f0 513 case X509_LU_NONE:
0f113f3e
MC
514 /* abort(); */
515 return -1;
516 }
517
5fd7eb5c 518 idx = sk_X509_OBJECT_find_all(h, &stmp, pnmatch);
0f113f3e
MC
519 return idx;
520}
4d50a2b4 521
0946a198 522int X509_OBJECT_idx_by_subject(STACK_OF(X509_OBJECT) *h, X509_LOOKUP_TYPE type,
8cc86b81 523 const X509_NAME *name)
0f113f3e
MC
524{
525 return x509_object_idx_cnt(h, type, name, NULL);
526}
527
528X509_OBJECT *X509_OBJECT_retrieve_by_subject(STACK_OF(X509_OBJECT) *h,
0946a198 529 X509_LOOKUP_TYPE type,
8cc86b81 530 const X509_NAME *name)
0f113f3e
MC
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}
11262391 538
8cc86b81 539STACK_OF(X509_OBJECT) *X509_STORE_get0_objects(const X509_STORE *v)
f0c58c32
CH
540{
541 return v->objs;
542}
543
31b28ad9
DDO
544STACK_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) {
9311d0c4 551 ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
31b28ad9
DDO
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
eeccc237
DDO
561 if (cert != NULL
562 && !X509_add_cert(sk, cert, X509_ADD_FLAG_UP_REF))
563 goto err;
31b28ad9
DDO
564 }
565 X509_STORE_unlock(store);
566 return sk;
567
568 err:
569 X509_STORE_unlock(store);
79b2a2f2 570 OSSL_STACK_OF_X509_free(sk);
31b28ad9
DDO
571 return NULL;
572}
573
8cc86b81
DDO
574STACK_OF(X509) *X509_STORE_CTX_get1_certs(X509_STORE_CTX *ctx,
575 const X509_NAME *nm)
0f113f3e
MC
576{
577 int i, idx, cnt;
6ddbb4cd 578 STACK_OF(X509) *sk = NULL;
0f113f3e
MC
579 X509 *x;
580 X509_OBJECT *obj;
faa9dcd4 581 X509_STORE *store = ctx->store;
6ddbb4cd 582
7a9abccd 583 if (store == NULL)
1c705121
MC
584 return NULL;
585
7a9abccd
SL
586 X509_STORE_lock(store);
587 idx = x509_object_idx_cnt(store->objs, X509_LU_X509, nm, &cnt);
0f113f3e
MC
588 if (idx < 0) {
589 /*
590 * Nothing found in cache: do lookup to possibly add new objects to
591 * cache
592 */
6ddbb4cd
RS
593 X509_OBJECT *xobj = X509_OBJECT_new();
594
7a9abccd
SL
595 X509_STORE_unlock(store);
596
6ddbb4cd
RS
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);
0f113f3e
MC
601 return NULL;
602 }
6ddbb4cd 603 X509_OBJECT_free(xobj);
7a9abccd
SL
604 X509_STORE_lock(store);
605 idx = x509_object_idx_cnt(store->objs, X509_LU_X509, nm, &cnt);
0f113f3e 606 if (idx < 0) {
7a9abccd 607 X509_STORE_unlock(store);
0f113f3e
MC
608 return NULL;
609 }
610 }
6ddbb4cd
RS
611
612 sk = sk_X509_new_null();
0f113f3e 613 for (i = 0; i < cnt; i++, idx++) {
7a9abccd 614 obj = sk_X509_OBJECT_value(store->objs, idx);
0f113f3e 615 x = obj->data.x509;
eeccc237 616 if (!X509_add_cert(sk, x, X509_ADD_FLAG_UP_REF)) {
7a9abccd 617 X509_STORE_unlock(store);
79b2a2f2 618 OSSL_STACK_OF_X509_free(sk);
0f113f3e
MC
619 return NULL;
620 }
621 }
7a9abccd 622 X509_STORE_unlock(store);
0f113f3e 623 return sk;
0f113f3e 624}
4d50a2b4 625
8cc86b81
DDO
626STACK_OF(X509_CRL) *X509_STORE_CTX_get1_crls(const X509_STORE_CTX *ctx,
627 const X509_NAME *nm)
0f113f3e
MC
628{
629 int i, idx, cnt;
6ddbb4cd 630 STACK_OF(X509_CRL) *sk = sk_X509_CRL_new_null();
0f113f3e 631 X509_CRL *x;
6ddbb4cd 632 X509_OBJECT *obj, *xobj = X509_OBJECT_new();
faa9dcd4 633 X509_STORE *store = ctx->store;
0f113f3e 634
6ddbb4cd 635 /* Always do lookup to possibly add new CRLs to cache */
1c705121
MC
636 if (sk == NULL
637 || xobj == NULL
7a9abccd 638 || store == NULL
1c705121 639 || !X509_STORE_CTX_get_by_subject(ctx, X509_LU_CRL, nm, xobj)) {
6ddbb4cd 640 X509_OBJECT_free(xobj);
0f113f3e
MC
641 sk_X509_CRL_free(sk);
642 return NULL;
643 }
6ddbb4cd 644 X509_OBJECT_free(xobj);
7a9abccd
SL
645 X509_STORE_lock(store);
646 idx = x509_object_idx_cnt(store->objs, X509_LU_CRL, nm, &cnt);
0f113f3e 647 if (idx < 0) {
7a9abccd 648 X509_STORE_unlock(store);
0f113f3e
MC
649 sk_X509_CRL_free(sk);
650 return NULL;
651 }
652
653 for (i = 0; i < cnt; i++, idx++) {
7a9abccd 654 obj = sk_X509_OBJECT_value(store->objs, idx);
0f113f3e 655 x = obj->data.crl;
c70e2ec3
BE
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 }
0f113f3e 661 if (!sk_X509_CRL_push(sk, x)) {
7a9abccd 662 X509_STORE_unlock(store);
0f113f3e
MC
663 X509_CRL_free(x);
664 sk_X509_CRL_pop_free(sk, X509_CRL_free);
665 return NULL;
666 }
667 }
7a9abccd 668 X509_STORE_unlock(store);
0f113f3e
MC
669 return sk;
670}
671
672X509_OBJECT *X509_OBJECT_retrieve_match(STACK_OF(X509_OBJECT) *h,
673 X509_OBJECT *x)
674{
5b37fef0 675 int idx, i, num;
0f113f3e 676 X509_OBJECT *obj;
5b37fef0 677
0f113f3e 678 idx = sk_X509_OBJECT_find(h, x);
5b37fef0 679 if (idx < 0)
0f113f3e
MC
680 return NULL;
681 if ((x->type != X509_LU_X509) && (x->type != X509_LU_CRL))
682 return sk_X509_OBJECT_value(h, idx);
5b37fef0 683 for (i = idx, num = sk_X509_OBJECT_num(h); i < num; i++) {
0f113f3e 684 obj = sk_X509_OBJECT_value(h, i);
5b37fef0
AP
685 if (x509_object_cmp((const X509_OBJECT **)&obj,
686 (const X509_OBJECT **)&x))
0f113f3e
MC
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) {
f2a04587 692 if (X509_CRL_match(obj->data.crl, x->data.crl) == 0)
0f113f3e
MC
693 return obj;
694 } else
695 return obj;
696 }
697 return NULL;
698}
d02b48c6 699
1d97c843 700/*-
f1923a21
DDO
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.
2f043896
DSH
703 *
704 * Return values are:
705 * 1 lookup successful.
706 * 0 certificate not found.
707 * -1 some other error.
708 */
2f043896 709int X509_STORE_CTX_get1_issuer(X509 **issuer, X509_STORE_CTX *ctx, X509 *x)
0f113f3e 710{
8cc86b81 711 const X509_NAME *xn;
6ddbb4cd 712 X509_OBJECT *obj = X509_OBJECT_new(), *pobj = NULL;
faa9dcd4 713 X509_STORE *store = ctx->store;
5fd7eb5c 714 int i, ok, idx, ret, nmatch = 0;
f0e0fd51 715
6ddbb4cd
RS
716 if (obj == NULL)
717 return -1;
0f113f3e
MC
718 *issuer = NULL;
719 xn = X509_get_issuer_name(x);
6ddbb4cd 720 ok = X509_STORE_CTX_get_by_subject(ctx, X509_LU_X509, xn, obj);
ba1a1c37 721 if (ok != 1) {
6ddbb4cd 722 X509_OBJECT_free(obj);
0f113f3e
MC
723 return 0;
724 }
f1923a21 725 /* If certificate matches and is currently valid all OK */
6ddbb4cd 726 if (ctx->check_issued(ctx, x, obj->data.x509)) {
4669015d 727 if (ossl_x509_check_cert_time(ctx, obj->data.x509, -1)) {
6ddbb4cd 728 *issuer = obj->data.x509;
d6bf19a4
DDO
729 /* |*issuer| has taken over the cert reference from |obj| */
730 obj->type = X509_LU_NONE;
6ddbb4cd 731 X509_OBJECT_free(obj);
d6bf19a4 732 return 1;
0f113f3e
MC
733 }
734 }
6ddbb4cd 735 X509_OBJECT_free(obj);
0f113f3e 736
f1923a21
DDO
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 */
7a9abccd 742 if (store == NULL)
1c705121
MC
743 return 0;
744
f1923a21 745 /* Find index of first currently valid cert accepted by 'check_issued' */
0f113f3e 746 ret = 0;
7a9abccd 747 X509_STORE_lock(store);
5fd7eb5c 748 idx = x509_object_idx_cnt(store->objs, X509_LU_X509, xn, &nmatch);
f1923a21 749 if (idx != -1) { /* should be true as we've had at least one match */
0f113f3e 750 /* Look through all matching certs for suitable issuer */
5fd7eb5c 751 for (i = idx; i < idx + nmatch; i++) {
7a9abccd 752 pobj = sk_X509_OBJECT_value(store->objs, i);
0f113f3e
MC
753 /* See if we've run past the matches */
754 if (pobj->type != X509_LU_X509)
755 break;
0f113f3e 756 if (ctx->check_issued(ctx, x, pobj->data.x509)) {
0f113f3e 757 ret = 1;
f1923a21 758 /* If times check fine, exit with match, else keep looking. */
4669015d 759 if (ossl_x509_check_cert_time(ctx, pobj->data.x509, -1)) {
f1923a21
DDO
760 *issuer = pobj->data.x509;
761 break;
762 }
0f113f3e 763 /*
f1923a21
DDO
764 * Leave the so far most recently expired match in *issuer
765 * so we return nearest match if no certificate time is OK.
0f113f3e 766 */
f1923a21
DDO
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;
0f113f3e
MC
771 }
772 }
773 }
f1923a21 774 if (*issuer != NULL && !X509_up_ref(*issuer)) {
c70e2ec3
BE
775 *issuer = NULL;
776 ret = -1;
777 }
7a9abccd 778 X509_STORE_unlock(store);
0f113f3e
MC
779 return ret;
780}
d02b48c6 781
5d7c222d 782int X509_STORE_set_flags(X509_STORE *ctx, unsigned long flags)
0f113f3e
MC
783{
784 return X509_VERIFY_PARAM_set_flags(ctx->param, flags);
785}
5d7c222d
DSH
786
787int X509_STORE_set_depth(X509_STORE *ctx, int depth)
0f113f3e
MC
788{
789 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
790 return 1;
791}
bdee69f7 792
926a56bf 793int X509_STORE_set_purpose(X509_STORE *ctx, int purpose)
0f113f3e
MC
794{
795 return X509_VERIFY_PARAM_set_purpose(ctx->param, purpose);
796}
926a56bf
DSH
797
798int X509_STORE_set_trust(X509_STORE *ctx, int trust)
0f113f3e
MC
799{
800 return X509_VERIFY_PARAM_set_trust(ctx->param, trust);
801}
5d7c222d 802
8cc86b81 803int X509_STORE_set1_param(X509_STORE *ctx, const X509_VERIFY_PARAM *param)
0f113f3e
MC
804{
805 return X509_VERIFY_PARAM_set1(ctx->param, param);
806}
926a56bf 807
8cc86b81 808X509_VERIFY_PARAM *X509_STORE_get0_param(const X509_STORE *ctx)
f0c58c32
CH
809{
810 return ctx->param;
811}
812
1060a50b
RL
813void X509_STORE_set_verify(X509_STORE *ctx, X509_STORE_CTX_verify_fn verify)
814{
815 ctx->verify = verify;
816}
817
8cc86b81 818X509_STORE_CTX_verify_fn X509_STORE_get_verify(const X509_STORE *ctx)
1060a50b
RL
819{
820 return ctx->verify;
821}
822
a5b37fca 823void X509_STORE_set_verify_cb(X509_STORE *ctx,
1060a50b 824 X509_STORE_CTX_verify_cb verify_cb)
0f113f3e
MC
825{
826 ctx->verify_cb = verify_cb;
827}
a5b37fca 828
8cc86b81 829X509_STORE_CTX_verify_cb X509_STORE_get_verify_cb(const X509_STORE *ctx)
7cafbb4b 830{
1060a50b
RL
831 return ctx->verify_cb;
832}
833
834void 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
8cc86b81 840X509_STORE_CTX_get_issuer_fn X509_STORE_get_get_issuer(const X509_STORE *ctx)
1060a50b
RL
841{
842 return ctx->get_issuer;
843}
844
845void 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
8cc86b81 851X509_STORE_CTX_check_issued_fn X509_STORE_get_check_issued(const X509_STORE *ctx)
1060a50b
RL
852{
853 return ctx->check_issued;
854}
855
856void 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
8cc86b81 862X509_STORE_CTX_check_revocation_fn X509_STORE_get_check_revocation(const X509_STORE *ctx)
1060a50b
RL
863{
864 return ctx->check_revocation;
865}
866
867void 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
8cc86b81 873X509_STORE_CTX_get_crl_fn X509_STORE_get_get_crl(const X509_STORE *ctx)
1060a50b
RL
874{
875 return ctx->get_crl;
876}
877
878void 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
8cc86b81 884X509_STORE_CTX_check_crl_fn X509_STORE_get_check_crl(const X509_STORE *ctx)
1060a50b
RL
885{
886 return ctx->check_crl;
887}
888
889void 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
8cc86b81 895X509_STORE_CTX_cert_crl_fn X509_STORE_get_cert_crl(const X509_STORE *ctx)
1060a50b
RL
896{
897 return ctx->cert_crl;
898}
899
0a5fe2eb
RL
900void 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
8cc86b81 906X509_STORE_CTX_check_policy_fn X509_STORE_get_check_policy(const X509_STORE *ctx)
0a5fe2eb
RL
907{
908 return ctx->check_policy;
909}
910
1060a50b
RL
911void 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
8cc86b81 917X509_STORE_CTX_lookup_certs_fn X509_STORE_get_lookup_certs(const X509_STORE *ctx)
1060a50b
RL
918{
919 return ctx->lookup_certs;
920}
921
922void 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
8cc86b81 928X509_STORE_CTX_lookup_crls_fn X509_STORE_get_lookup_crls(const X509_STORE *ctx)
1060a50b
RL
929{
930 return ctx->lookup_crls;
931}
932
933void X509_STORE_set_cleanup(X509_STORE *ctx,
934 X509_STORE_CTX_cleanup_fn ctx_cleanup)
935{
936 ctx->cleanup = ctx_cleanup;
7cafbb4b
MC
937}
938
8cc86b81 939X509_STORE_CTX_cleanup_fn X509_STORE_get_cleanup(const X509_STORE *ctx)
0f113f3e 940{
1060a50b 941 return ctx->cleanup;
0f113f3e 942}
2c340864 943
3aec886e
KY
944int 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
8cc86b81 949void *X509_STORE_get_ex_data(const X509_STORE *ctx, int idx)
3aec886e
KY
950{
951 return CRYPTO_get_ex_data(&ctx->ex_data, idx);
952}
953
8cc86b81 954X509_STORE *X509_STORE_CTX_get0_store(const X509_STORE_CTX *ctx)
0f113f3e 955{
faa9dcd4 956 return ctx->store;
0f113f3e 957}