1 /*
2 * Copyright 1995-2024 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 return NULL;
24
25 ret->method = method;
26 if (method->new_item != NULL && method->new_item(ret) == 0) {
27 OPENSSL_free(ret);
28 return NULL;
29 }
30 return ret;
31 }
32
X509_LOOKUP_free(X509_LOOKUP * ctx)33 void X509_LOOKUP_free(X509_LOOKUP *ctx)
34 {
35 if (ctx == NULL)
36 return;
37 if ((ctx->method != NULL) && (ctx->method->free != NULL))
38 (*ctx->method->free) (ctx);
39 OPENSSL_free(ctx);
40 }
41
X509_STORE_lock(X509_STORE * xs)42 int X509_STORE_lock(X509_STORE *xs)
43 {
44 return CRYPTO_THREAD_write_lock(xs->lock);
45 }
46
x509_store_read_lock(X509_STORE * xs)47 static int x509_store_read_lock(X509_STORE *xs)
48 {
49 return CRYPTO_THREAD_read_lock(xs->lock);
50 }
51
X509_STORE_unlock(X509_STORE * xs)52 int X509_STORE_unlock(X509_STORE *xs)
53 {
54 return CRYPTO_THREAD_unlock(xs->lock);
55 }
56
X509_LOOKUP_init(X509_LOOKUP * ctx)57 int X509_LOOKUP_init(X509_LOOKUP *ctx)
58 {
59 if (ctx->method == NULL)
60 return 0;
61 if (ctx->method->init != NULL)
62 return ctx->method->init(ctx);
63 else
64 return 1;
65 }
66
X509_LOOKUP_shutdown(X509_LOOKUP * ctx)67 int X509_LOOKUP_shutdown(X509_LOOKUP *ctx)
68 {
69 if (ctx->method == NULL)
70 return 0;
71 if (ctx->method->shutdown != NULL)
72 return ctx->method->shutdown(ctx);
73 else
74 return 1;
75 }
76
X509_LOOKUP_ctrl_ex(X509_LOOKUP * ctx,int cmd,const char * argc,long argl,char ** ret,OSSL_LIB_CTX * libctx,const char * propq)77 int X509_LOOKUP_ctrl_ex(X509_LOOKUP *ctx, int cmd, const char *argc, long argl,
78 char **ret, OSSL_LIB_CTX *libctx, const char *propq)
79 {
80 if (ctx->method == NULL)
81 return -1;
82 if (ctx->method->ctrl_ex != NULL)
83 return ctx->method->ctrl_ex(ctx, cmd, argc, argl, ret, libctx, propq);
84 if (ctx->method->ctrl != NULL)
85 return ctx->method->ctrl(ctx, cmd, argc, argl, ret);
86 return 1;
87 }
88
X509_LOOKUP_ctrl(X509_LOOKUP * ctx,int cmd,const char * argc,long argl,char ** ret)89 int X509_LOOKUP_ctrl(X509_LOOKUP *ctx, int cmd, const char *argc, long argl,
90 char **ret)
91 {
92 return X509_LOOKUP_ctrl_ex(ctx, cmd, argc, argl, ret, NULL, NULL);
93 }
94
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)95 int X509_LOOKUP_by_subject_ex(X509_LOOKUP *ctx, X509_LOOKUP_TYPE type,
96 const X509_NAME *name, X509_OBJECT *ret,
97 OSSL_LIB_CTX *libctx, const char *propq)
98 {
99 if (ctx->skip
100 || ctx->method == NULL
101 || (ctx->method->get_by_subject == NULL
102 && ctx->method->get_by_subject_ex == NULL))
103 return 0;
104 if (ctx->method->get_by_subject_ex != NULL)
105 return ctx->method->get_by_subject_ex(ctx, type, name, ret, libctx,
106 propq);
107 else
108 return ctx->method->get_by_subject(ctx, type, name, ret);
109 }
110
X509_LOOKUP_by_subject(X509_LOOKUP * ctx,X509_LOOKUP_TYPE type,const X509_NAME * name,X509_OBJECT * ret)111 int X509_LOOKUP_by_subject(X509_LOOKUP *ctx, X509_LOOKUP_TYPE type,
112 const X509_NAME *name, X509_OBJECT *ret)
113 {
114 return X509_LOOKUP_by_subject_ex(ctx, type, name, ret, NULL, NULL);
115 }
116
X509_LOOKUP_by_issuer_serial(X509_LOOKUP * ctx,X509_LOOKUP_TYPE type,const X509_NAME * name,const ASN1_INTEGER * serial,X509_OBJECT * ret)117 int X509_LOOKUP_by_issuer_serial(X509_LOOKUP *ctx, X509_LOOKUP_TYPE type,
118 const X509_NAME *name,
119 const ASN1_INTEGER *serial,
120 X509_OBJECT *ret)
121 {
122 if ((ctx->method == NULL) || (ctx->method->get_by_issuer_serial == NULL))
123 return 0;
124 return ctx->method->get_by_issuer_serial(ctx, type, name, serial, ret);
125 }
126
X509_LOOKUP_by_fingerprint(X509_LOOKUP * ctx,X509_LOOKUP_TYPE type,const unsigned char * bytes,int len,X509_OBJECT * ret)127 int X509_LOOKUP_by_fingerprint(X509_LOOKUP *ctx, X509_LOOKUP_TYPE type,
128 const unsigned char *bytes, int len,
129 X509_OBJECT *ret)
130 {
131 if ((ctx->method == NULL) || (ctx->method->get_by_fingerprint == NULL))
132 return 0;
133 return ctx->method->get_by_fingerprint(ctx, type, bytes, len, ret);
134 }
135
X509_LOOKUP_by_alias(X509_LOOKUP * ctx,X509_LOOKUP_TYPE type,const char * str,int len,X509_OBJECT * ret)136 int X509_LOOKUP_by_alias(X509_LOOKUP *ctx, X509_LOOKUP_TYPE type,
137 const char *str, int len, X509_OBJECT *ret)
138 {
139 if ((ctx->method == NULL) || (ctx->method->get_by_alias == NULL))
140 return 0;
141 return ctx->method->get_by_alias(ctx, type, str, len, ret);
142 }
143
X509_LOOKUP_set_method_data(X509_LOOKUP * ctx,void * data)144 int X509_LOOKUP_set_method_data(X509_LOOKUP *ctx, void *data)
145 {
146 ctx->method_data = data;
147 return 1;
148 }
149
X509_LOOKUP_get_method_data(const X509_LOOKUP * ctx)150 void *X509_LOOKUP_get_method_data(const X509_LOOKUP *ctx)
151 {
152 return ctx->method_data;
153 }
154
X509_LOOKUP_get_store(const X509_LOOKUP * ctx)155 X509_STORE *X509_LOOKUP_get_store(const X509_LOOKUP *ctx)
156 {
157 return ctx->store_ctx;
158 }
159
x509_object_cmp(const X509_OBJECT * const * a,const X509_OBJECT * const * b)160 static int x509_object_cmp(const X509_OBJECT *const *a,
161 const X509_OBJECT *const *b)
162 {
163 int ret;
164
165 ret = ((*a)->type - (*b)->type);
166 if (ret)
167 return ret;
168 switch ((*a)->type) {
169 case X509_LU_X509:
170 ret = X509_subject_name_cmp((*a)->data.x509, (*b)->data.x509);
171 break;
172 case X509_LU_CRL:
173 ret = X509_CRL_cmp((*a)->data.crl, (*b)->data.crl);
174 break;
175 case X509_LU_NONE:
176 /* abort(); */
177 return 0;
178 }
179 return ret;
180 }
181
X509_STORE_new(void)182 X509_STORE *X509_STORE_new(void)
183 {
184 X509_STORE *ret = OPENSSL_zalloc(sizeof(*ret));
185
186 if (ret == NULL)
187 return NULL;
188 if ((ret->objs = sk_X509_OBJECT_new(x509_object_cmp)) == NULL) {
189 ERR_raise(ERR_LIB_X509, ERR_R_CRYPTO_LIB);
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_CRYPTO_LIB);
195 goto err;
196 }
197
198 if ((ret->param = X509_VERIFY_PARAM_new()) == NULL) {
199 ERR_raise(ERR_LIB_X509, ERR_R_X509_LIB);
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_CRYPTO_LIB);
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_CRYPTO_LIB);
210 goto err;
211 }
212
213 if (!CRYPTO_NEW_REF(&ret->references, 1))
214 goto err;
215 return ret;
216
217 err:
218 X509_VERIFY_PARAM_free(ret->param);
219 sk_X509_OBJECT_free(ret->objs);
220 sk_X509_LOOKUP_free(ret->get_cert_methods);
221 CRYPTO_THREAD_lock_free(ret->lock);
222 OPENSSL_free(ret);
223 return NULL;
224 }
225
X509_STORE_free(X509_STORE * xs)226 void X509_STORE_free(X509_STORE *xs)
227 {
228 int i;
229 STACK_OF(X509_LOOKUP) *sk;
230 X509_LOOKUP *lu;
231
232 if (xs == NULL)
233 return;
234 CRYPTO_DOWN_REF(&xs->references, &i);
235 REF_PRINT_COUNT("X509_STORE", xs);
236 if (i > 0)
237 return;
238 REF_ASSERT_ISNT(i < 0);
239
240 sk = xs->get_cert_methods;
241 for (i = 0; i < sk_X509_LOOKUP_num(sk); i++) {
242 lu = sk_X509_LOOKUP_value(sk, i);
243 X509_LOOKUP_shutdown(lu);
244 X509_LOOKUP_free(lu);
245 }
246 sk_X509_LOOKUP_free(sk);
247 sk_X509_OBJECT_pop_free(xs->objs, X509_OBJECT_free);
248
249 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509_STORE, xs, &xs->ex_data);
250 X509_VERIFY_PARAM_free(xs->param);
251 CRYPTO_THREAD_lock_free(xs->lock);
252 CRYPTO_FREE_REF(&xs->references);
253 OPENSSL_free(xs);
254 }
255
X509_STORE_up_ref(X509_STORE * xs)256 int X509_STORE_up_ref(X509_STORE *xs)
257 {
258 int i;
259
260 if (CRYPTO_UP_REF(&xs->references, &i) <= 0)
261 return 0;
262
263 REF_PRINT_COUNT("X509_STORE", xs);
264 REF_ASSERT_ISNT(i < 2);
265 return i > 1 ? 1 : 0;
266 }
267
X509_STORE_add_lookup(X509_STORE * xs,X509_LOOKUP_METHOD * m)268 X509_LOOKUP *X509_STORE_add_lookup(X509_STORE *xs, X509_LOOKUP_METHOD *m)
269 {
270 int i;
271 STACK_OF(X509_LOOKUP) *sk;
272 X509_LOOKUP *lu;
273
274 sk = xs->get_cert_methods;
275 for (i = 0; i < sk_X509_LOOKUP_num(sk); i++) {
276 lu = sk_X509_LOOKUP_value(sk, i);
277 if (m == lu->method) {
278 return lu;
279 }
280 }
281 /* a new one */
282 lu = X509_LOOKUP_new(m);
283 if (lu == NULL) {
284 ERR_raise(ERR_LIB_X509, ERR_R_X509_LIB);
285 return NULL;
286 }
287
288 lu->store_ctx = xs;
289 if (sk_X509_LOOKUP_push(xs->get_cert_methods, lu))
290 return lu;
291 /* sk_X509_LOOKUP_push() failed */
292 ERR_raise(ERR_LIB_X509, ERR_R_CRYPTO_LIB);
293 X509_LOOKUP_free(lu);
294 return NULL;
295 }
296
297 /* Also fill the cache (ctx->store->objs) with all matching certificates. */
X509_STORE_CTX_get_obj_by_subject(X509_STORE_CTX * ctx,X509_LOOKUP_TYPE type,const X509_NAME * name)298 X509_OBJECT *X509_STORE_CTX_get_obj_by_subject(X509_STORE_CTX *ctx,
299 X509_LOOKUP_TYPE type,
300 const X509_NAME *name)
301 {
302 X509_OBJECT *ret = X509_OBJECT_new();
303
304 if (ret == NULL)
305 return NULL;
306 if (!X509_STORE_CTX_get_by_subject(ctx, type, name, ret)) {
307 X509_OBJECT_free(ret);
308 return NULL;
309 }
310 return ret;
311 }
312
313 /*
314 * May be called with |ret| == NULL just for the side effect of
315 * caching all certs matching the given subject DN in |ctx->store->objs|.
316 * Returns 1 if successful,
317 * 0 if not found or X509_LOOKUP_by_subject_ex() returns an error,
318 * -1 on failure
319 */
ossl_x509_store_ctx_get_by_subject(const X509_STORE_CTX * ctx,X509_LOOKUP_TYPE type,const X509_NAME * name,X509_OBJECT * ret)320 static int ossl_x509_store_ctx_get_by_subject(const X509_STORE_CTX *ctx,
321 X509_LOOKUP_TYPE type,
322 const X509_NAME *name,
323 X509_OBJECT *ret)
324 {
325 X509_STORE *store = ctx->store;
326 X509_LOOKUP *lu;
327 X509_OBJECT stmp, *tmp;
328 int i, j;
329
330 if (store == NULL)
331 return 0;
332
333 stmp.type = X509_LU_NONE;
334 stmp.data.x509 = NULL;
335
336 if (!x509_store_read_lock(store))
337 return 0;
338 /* Should already be sorted...but just in case */
339 if (!sk_X509_OBJECT_is_sorted(store->objs)) {
340 X509_STORE_unlock(store);
341 /* Take a write lock instead of a read lock */
342 if (!X509_STORE_lock(store))
343 return 0;
344 /*
345 * Another thread might have sorted it in the meantime. But if so,
346 * sk_X509_OBJECT_sort() exits early.
347 */
348 sk_X509_OBJECT_sort(store->objs);
349 }
350 tmp = X509_OBJECT_retrieve_by_subject(store->objs, type, name);
351 X509_STORE_unlock(store);
352
353 if (tmp == NULL || type == X509_LU_CRL) {
354 for (i = 0; i < sk_X509_LOOKUP_num(store->get_cert_methods); i++) {
355 lu = sk_X509_LOOKUP_value(store->get_cert_methods, i);
356 if (lu->skip)
357 continue;
358 if (lu->method == NULL)
359 return -1;
360 j = X509_LOOKUP_by_subject_ex(lu, type, name, &stmp,
361 ctx->libctx, ctx->propq);
362 if (j != 0) { /* non-zero value is considered success here */
363 tmp = &stmp;
364 break;
365 }
366 }
367 if (tmp == NULL)
368 return 0;
369 }
370
371 if (ret != NULL) {
372 if (!X509_OBJECT_up_ref_count(tmp))
373 return -1;
374 ret->type = tmp->type;
375 ret->data = tmp->data;
376 }
377 return 1;
378 }
379
380 /* Also fill the cache |ctx->store->objs| with all matching certificates. */
X509_STORE_CTX_get_by_subject(const X509_STORE_CTX * ctx,X509_LOOKUP_TYPE type,const X509_NAME * name,X509_OBJECT * ret)381 int X509_STORE_CTX_get_by_subject(const X509_STORE_CTX *ctx,
382 X509_LOOKUP_TYPE type,
383 const X509_NAME *name, X509_OBJECT *ret)
384 {
385 return ossl_x509_store_ctx_get_by_subject(ctx, type, name, ret) > 0;
386 }
387
x509_store_add(X509_STORE * store,void * x,int crl)388 static int x509_store_add(X509_STORE *store, void *x, int crl)
389 {
390 X509_OBJECT *obj;
391 int ret = 0, added = 0;
392
393 if (x == NULL)
394 return 0;
395 obj = X509_OBJECT_new();
396 if (obj == NULL)
397 return 0;
398
399 if (crl) {
400 obj->type = X509_LU_CRL;
401 obj->data.crl = (X509_CRL *)x;
402 } else {
403 obj->type = X509_LU_X509;
404 obj->data.x509 = (X509 *)x;
405 }
406 if (!X509_OBJECT_up_ref_count(obj)) {
407 obj->type = X509_LU_NONE;
408 X509_OBJECT_free(obj);
409 return 0;
410 }
411
412 if (!X509_STORE_lock(store)) {
413 obj->type = X509_LU_NONE;
414 X509_OBJECT_free(obj);
415 return 0;
416 }
417
418 if (X509_OBJECT_retrieve_match(store->objs, obj)) {
419 ret = 1;
420 } else {
421 added = sk_X509_OBJECT_push(store->objs, obj);
422 ret = added != 0;
423 }
424 X509_STORE_unlock(store);
425
426 if (added == 0) /* obj not pushed */
427 X509_OBJECT_free(obj);
428
429 return ret;
430 }
431
X509_STORE_add_cert(X509_STORE * xs,X509 * x)432 int X509_STORE_add_cert(X509_STORE *xs, X509 *x)
433 {
434 if (!x509_store_add(xs, x, 0)) {
435 ERR_raise(ERR_LIB_X509, ERR_R_X509_LIB);
436 return 0;
437 }
438 return 1;
439 }
440
X509_STORE_add_crl(X509_STORE * xs,X509_CRL * x)441 int X509_STORE_add_crl(X509_STORE *xs, X509_CRL *x)
442 {
443 if (!x509_store_add(xs, x, 1)) {
444 ERR_raise(ERR_LIB_X509, ERR_R_X509_LIB);
445 return 0;
446 }
447 return 1;
448 }
449
X509_OBJECT_up_ref_count(X509_OBJECT * a)450 int X509_OBJECT_up_ref_count(X509_OBJECT *a)
451 {
452 switch (a->type) {
453 case X509_LU_NONE:
454 break;
455 case X509_LU_X509:
456 return X509_up_ref(a->data.x509);
457 case X509_LU_CRL:
458 return X509_CRL_up_ref(a->data.crl);
459 }
460 return 1;
461 }
462
X509_OBJECT_get0_X509(const X509_OBJECT * a)463 X509 *X509_OBJECT_get0_X509(const X509_OBJECT *a)
464 {
465 if (a == NULL || a->type != X509_LU_X509)
466 return NULL;
467 return a->data.x509;
468 }
469
X509_OBJECT_get0_X509_CRL(const X509_OBJECT * a)470 X509_CRL *X509_OBJECT_get0_X509_CRL(const X509_OBJECT *a)
471 {
472 if (a == NULL || a->type != X509_LU_CRL)
473 return NULL;
474 return a->data.crl;
475 }
476
X509_OBJECT_get_type(const X509_OBJECT * a)477 X509_LOOKUP_TYPE X509_OBJECT_get_type(const X509_OBJECT *a)
478 {
479 return a->type;
480 }
481
X509_OBJECT_new(void)482 X509_OBJECT *X509_OBJECT_new(void)
483 {
484 X509_OBJECT *ret = OPENSSL_zalloc(sizeof(*ret));
485
486 if (ret == NULL)
487 return NULL;
488 ret->type = X509_LU_NONE;
489 return ret;
490 }
491
x509_object_free_internal(X509_OBJECT * a)492 static void x509_object_free_internal(X509_OBJECT *a)
493 {
494 if (a == NULL)
495 return;
496 switch (a->type) {
497 case X509_LU_NONE:
498 break;
499 case X509_LU_X509:
500 X509_free(a->data.x509);
501 break;
502 case X509_LU_CRL:
503 X509_CRL_free(a->data.crl);
504 break;
505 }
506 }
507
X509_OBJECT_set1_X509(X509_OBJECT * a,X509 * obj)508 int X509_OBJECT_set1_X509(X509_OBJECT *a, X509 *obj)
509 {
510 if (a == NULL || !X509_up_ref(obj))
511 return 0;
512
513 x509_object_free_internal(a);
514 a->type = X509_LU_X509;
515 a->data.x509 = obj;
516 return 1;
517 }
518
X509_OBJECT_set1_X509_CRL(X509_OBJECT * a,X509_CRL * obj)519 int X509_OBJECT_set1_X509_CRL(X509_OBJECT *a, X509_CRL *obj)
520 {
521 if (a == NULL || !X509_CRL_up_ref(obj))
522 return 0;
523
524 x509_object_free_internal(a);
525 a->type = X509_LU_CRL;
526 a->data.crl = obj;
527 return 1;
528 }
529
X509_OBJECT_free(X509_OBJECT * a)530 void X509_OBJECT_free(X509_OBJECT *a)
531 {
532 x509_object_free_internal(a);
533 OPENSSL_free(a);
534 }
535
536 /* Returns -1 if not found, but also on error */
x509_object_idx_cnt(STACK_OF (X509_OBJECT)* h,X509_LOOKUP_TYPE type,const X509_NAME * name,int * pnmatch)537 static int x509_object_idx_cnt(STACK_OF(X509_OBJECT) *h, X509_LOOKUP_TYPE type,
538 const X509_NAME *name, int *pnmatch)
539 {
540 X509_OBJECT stmp;
541 X509 x509_s;
542 X509_CRL crl_s;
543
544 stmp.type = type;
545 switch (type) {
546 case X509_LU_X509:
547 stmp.data.x509 = &x509_s;
548 x509_s.cert_info.subject = (X509_NAME *)name; /* won't modify it */
549 break;
550 case X509_LU_CRL:
551 stmp.data.crl = &crl_s;
552 crl_s.crl.issuer = (X509_NAME *)name; /* won't modify it */
553 break;
554 case X509_LU_NONE:
555 default:
556 /* abort(); */
557 return -1;
558 }
559
560 /* Assumes h is locked for read if applicable */
561 return sk_X509_OBJECT_find_all(h, &stmp, pnmatch);
562 }
563
564 /* Assumes h is locked for read if applicable */
X509_OBJECT_idx_by_subject(STACK_OF (X509_OBJECT)* h,X509_LOOKUP_TYPE type,const X509_NAME * name)565 int X509_OBJECT_idx_by_subject(STACK_OF(X509_OBJECT) *h, X509_LOOKUP_TYPE type,
566 const X509_NAME *name)
567 {
568 return x509_object_idx_cnt(h, type, name, NULL);
569 }
570
571 /* Assumes h is locked for read if applicable */
X509_OBJECT_retrieve_by_subject(STACK_OF (X509_OBJECT)* h,X509_LOOKUP_TYPE type,const X509_NAME * name)572 X509_OBJECT *X509_OBJECT_retrieve_by_subject(STACK_OF(X509_OBJECT) *h,
573 X509_LOOKUP_TYPE type,
574 const X509_NAME *name)
575 {
576 int idx = X509_OBJECT_idx_by_subject(h, type, name);
577
578 if (idx == -1)
579 return NULL;
580 return sk_X509_OBJECT_value(h, idx);
581 }
582
STACK_OF(X509_OBJECT)583 STACK_OF(X509_OBJECT) *X509_STORE_get0_objects(const X509_STORE *xs)
584 {
585 return xs->objs;
586 }
587
x509_object_dup(const X509_OBJECT * obj)588 static X509_OBJECT *x509_object_dup(const X509_OBJECT *obj)
589 {
590 X509_OBJECT *ret = X509_OBJECT_new();
591 if (ret == NULL)
592 return NULL;
593
594 ret->type = obj->type;
595 ret->data = obj->data;
596 X509_OBJECT_up_ref_count(ret);
597 return ret;
598 }
599
STACK_OF(X509_OBJECT)600 STACK_OF(X509_OBJECT) *X509_STORE_get1_objects(X509_STORE *store)
601 {
602 STACK_OF(X509_OBJECT) *objs;
603
604 if (store == NULL) {
605 ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
606 return NULL;
607 }
608
609 if (!x509_store_read_lock(store))
610 return NULL;
611
612 objs = sk_X509_OBJECT_deep_copy(store->objs, x509_object_dup,
613 X509_OBJECT_free);
614 X509_STORE_unlock(store);
615 return objs;
616 }
617
STACK_OF(X509)618 STACK_OF(X509) *X509_STORE_get1_all_certs(X509_STORE *store)
619 {
620 STACK_OF(X509) *sk;
621 STACK_OF(X509_OBJECT) *objs;
622 int i;
623
624 if (store == NULL) {
625 ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
626 return NULL;
627 }
628 if ((sk = sk_X509_new_null()) == NULL)
629 return NULL;
630 if (!X509_STORE_lock(store))
631 goto out_free;
632
633 sk_X509_OBJECT_sort(store->objs);
634 objs = X509_STORE_get0_objects(store);
635 for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
636 X509 *cert = X509_OBJECT_get0_X509(sk_X509_OBJECT_value(objs, i));
637
638 if (cert != NULL
639 && !X509_add_cert(sk, cert, X509_ADD_FLAG_UP_REF))
640 goto err;
641 }
642 X509_STORE_unlock(store);
643 return sk;
644
645 err:
646 X509_STORE_unlock(store);
647 out_free:
648 OSSL_STACK_OF_X509_free(sk);
649 return NULL;
650 }
651
652 /*-
653 * Collect from |ctx->store| all certs with subject matching |nm|.
654 * Returns NULL on internal/fatal error, empty stack if not found.
655 */
STACK_OF(X509)656 STACK_OF(X509) *X509_STORE_CTX_get1_certs(X509_STORE_CTX *ctx,
657 const X509_NAME *nm)
658 {
659 int i, idx, cnt;
660 STACK_OF(X509) *sk = NULL;
661 X509 *x;
662 X509_OBJECT *obj;
663 X509_STORE *store = ctx->store;
664
665 if (store == NULL)
666 return sk_X509_new_null();
667
668 if (!X509_STORE_lock(store))
669 return NULL;
670
671 sk_X509_OBJECT_sort(store->objs);
672 idx = x509_object_idx_cnt(store->objs, X509_LU_X509, nm, &cnt);
673 if (idx < 0) {
674 /*
675 * Nothing found in cache: do lookup to possibly add new objects to
676 * cache
677 */
678 X509_STORE_unlock(store);
679 i = ossl_x509_store_ctx_get_by_subject(ctx, X509_LU_X509, nm, NULL);
680 if (i <= 0)
681 return i < 0 ? NULL : sk_X509_new_null();
682 if (!X509_STORE_lock(store))
683 return NULL;
684 sk_X509_OBJECT_sort(store->objs);
685 idx = x509_object_idx_cnt(store->objs, X509_LU_X509, nm, &cnt);
686 }
687
688 sk = sk_X509_new_null();
689 if (idx < 0 || sk == NULL)
690 goto end;
691 for (i = 0; i < cnt; i++, idx++) {
692 obj = sk_X509_OBJECT_value(store->objs, idx);
693 x = obj->data.x509;
694 if (!X509_add_cert(sk, x, X509_ADD_FLAG_UP_REF)) {
695 X509_STORE_unlock(store);
696 OSSL_STACK_OF_X509_free(sk);
697 return NULL;
698 }
699 }
700 end:
701 X509_STORE_unlock(store);
702 return sk;
703 }
704
705 /* Returns NULL on internal/fatal error, empty stack if not found */
STACK_OF(X509_CRL)706 STACK_OF(X509_CRL) *X509_STORE_CTX_get1_crls(const X509_STORE_CTX *ctx,
707 const X509_NAME *nm)
708 {
709 int i = 1, idx, cnt;
710 STACK_OF(X509_CRL) *sk;
711 X509_CRL *x;
712 X509_OBJECT *obj;
713 X509_STORE *store = ctx->store;
714
715 /* Always do lookup to possibly add new CRLs to cache */
716 i = ossl_x509_store_ctx_get_by_subject(ctx, X509_LU_CRL, nm, NULL);
717 if (i < 0)
718 return NULL;
719 sk = sk_X509_CRL_new_null();
720 if (i == 0)
721 return sk;
722 if (!X509_STORE_lock(store)) {
723 sk_X509_CRL_free(sk);
724 return NULL;
725 }
726 sk_X509_OBJECT_sort(store->objs);
727 idx = x509_object_idx_cnt(store->objs, X509_LU_CRL, nm, &cnt);
728 if (idx < 0) {
729 X509_STORE_unlock(store);
730 return sk;
731 }
732
733 for (i = 0; i < cnt; i++, idx++) {
734 obj = sk_X509_OBJECT_value(store->objs, idx);
735 x = obj->data.crl;
736 if (!X509_CRL_up_ref(x)) {
737 X509_STORE_unlock(store);
738 sk_X509_CRL_pop_free(sk, X509_CRL_free);
739 return NULL;
740 }
741 if (!sk_X509_CRL_push(sk, x)) {
742 X509_STORE_unlock(store);
743 X509_CRL_free(x);
744 sk_X509_CRL_pop_free(sk, X509_CRL_free);
745 return NULL;
746 }
747 }
748 X509_STORE_unlock(store);
749 return sk;
750 }
751
X509_OBJECT_retrieve_match(STACK_OF (X509_OBJECT)* h,X509_OBJECT * x)752 X509_OBJECT *X509_OBJECT_retrieve_match(STACK_OF(X509_OBJECT) *h,
753 X509_OBJECT *x)
754 {
755 int idx, i, num;
756 X509_OBJECT *obj;
757
758 idx = sk_X509_OBJECT_find(h, x);
759 if (idx < 0)
760 return NULL;
761 if ((x->type != X509_LU_X509) && (x->type != X509_LU_CRL))
762 return sk_X509_OBJECT_value(h, idx);
763 for (i = idx, num = sk_X509_OBJECT_num(h); i < num; i++) {
764 obj = sk_X509_OBJECT_value(h, i);
765 if (x509_object_cmp((const X509_OBJECT **)&obj,
766 (const X509_OBJECT **)&x))
767 return NULL;
768 if (x->type == X509_LU_X509) {
769 if (!X509_cmp(obj->data.x509, x->data.x509))
770 return obj;
771 } else if (x->type == X509_LU_CRL) {
772 if (X509_CRL_match(obj->data.crl, x->data.crl) == 0)
773 return obj;
774 } else {
775 return obj;
776 }
777 }
778 return NULL;
779 }
780
X509_STORE_set_flags(X509_STORE * xs,unsigned long flags)781 int X509_STORE_set_flags(X509_STORE *xs, unsigned long flags)
782 {
783 return X509_VERIFY_PARAM_set_flags(xs->param, flags);
784 }
785
X509_STORE_set_depth(X509_STORE * xs,int depth)786 int X509_STORE_set_depth(X509_STORE *xs, int depth)
787 {
788 X509_VERIFY_PARAM_set_depth(xs->param, depth);
789 return 1;
790 }
791
X509_STORE_set_purpose(X509_STORE * xs,int purpose)792 int X509_STORE_set_purpose(X509_STORE *xs, int purpose)
793 {
794 return X509_VERIFY_PARAM_set_purpose(xs->param, purpose);
795 }
796
X509_STORE_set_trust(X509_STORE * xs,int trust)797 int X509_STORE_set_trust(X509_STORE *xs, int trust)
798 {
799 return X509_VERIFY_PARAM_set_trust(xs->param, trust);
800 }
801
X509_STORE_set1_param(X509_STORE * xs,const X509_VERIFY_PARAM * param)802 int X509_STORE_set1_param(X509_STORE *xs, const X509_VERIFY_PARAM *param)
803 {
804 return X509_VERIFY_PARAM_set1(xs->param, param);
805 }
806
X509_STORE_get0_param(const X509_STORE * xs)807 X509_VERIFY_PARAM *X509_STORE_get0_param(const X509_STORE *xs)
808 {
809 return xs->param;
810 }
811
X509_STORE_set_verify(X509_STORE * xs,X509_STORE_CTX_verify_fn verify)812 void X509_STORE_set_verify(X509_STORE *xs, X509_STORE_CTX_verify_fn verify)
813 {
814 xs->verify = verify;
815 }
816
X509_STORE_get_verify(const X509_STORE * xs)817 X509_STORE_CTX_verify_fn X509_STORE_get_verify(const X509_STORE *xs)
818 {
819 return xs->verify;
820 }
821
X509_STORE_set_verify_cb(X509_STORE * xs,X509_STORE_CTX_verify_cb verify_cb)822 void X509_STORE_set_verify_cb(X509_STORE *xs,
823 X509_STORE_CTX_verify_cb verify_cb)
824 {
825 xs->verify_cb = verify_cb;
826 }
827
X509_STORE_get_verify_cb(const X509_STORE * xs)828 X509_STORE_CTX_verify_cb X509_STORE_get_verify_cb(const X509_STORE *xs)
829 {
830 return xs->verify_cb;
831 }
832
X509_STORE_set_get_issuer(X509_STORE * xs,X509_STORE_CTX_get_issuer_fn get_issuer)833 void X509_STORE_set_get_issuer(X509_STORE *xs,
834 X509_STORE_CTX_get_issuer_fn get_issuer)
835 {
836 xs->get_issuer = get_issuer;
837 }
838
X509_STORE_get_get_issuer(const X509_STORE * xs)839 X509_STORE_CTX_get_issuer_fn X509_STORE_get_get_issuer(const X509_STORE *xs)
840 {
841 return xs->get_issuer;
842 }
843
X509_STORE_set_check_issued(X509_STORE * xs,X509_STORE_CTX_check_issued_fn check_issued)844 void X509_STORE_set_check_issued(X509_STORE *xs,
845 X509_STORE_CTX_check_issued_fn check_issued)
846 {
847 xs->check_issued = check_issued;
848 }
849
X509_STORE_get_check_issued(const X509_STORE * xs)850 X509_STORE_CTX_check_issued_fn X509_STORE_get_check_issued(const X509_STORE *xs)
851 {
852 return xs->check_issued;
853 }
854
X509_STORE_set_check_revocation(X509_STORE * xs,X509_STORE_CTX_check_revocation_fn cb)855 void X509_STORE_set_check_revocation(X509_STORE *xs,
856 X509_STORE_CTX_check_revocation_fn cb)
857 {
858 xs->check_revocation = cb;
859 }
860
X509_STORE_get_check_revocation(const X509_STORE * xs)861 X509_STORE_CTX_check_revocation_fn X509_STORE_get_check_revocation(const X509_STORE *xs)
862 {
863 return xs->check_revocation;
864 }
865
X509_STORE_set_get_crl(X509_STORE * xs,X509_STORE_CTX_get_crl_fn get_crl)866 void X509_STORE_set_get_crl(X509_STORE *xs,
867 X509_STORE_CTX_get_crl_fn get_crl)
868 {
869 xs->get_crl = get_crl;
870 }
871
X509_STORE_get_get_crl(const X509_STORE * xs)872 X509_STORE_CTX_get_crl_fn X509_STORE_get_get_crl(const X509_STORE *xs)
873 {
874 return xs->get_crl;
875 }
876
X509_STORE_set_check_crl(X509_STORE * xs,X509_STORE_CTX_check_crl_fn check_crl)877 void X509_STORE_set_check_crl(X509_STORE *xs,
878 X509_STORE_CTX_check_crl_fn check_crl)
879 {
880 xs->check_crl = check_crl;
881 }
882
X509_STORE_get_check_crl(const X509_STORE * xs)883 X509_STORE_CTX_check_crl_fn X509_STORE_get_check_crl(const X509_STORE *xs)
884 {
885 return xs->check_crl;
886 }
887
X509_STORE_set_cert_crl(X509_STORE * xs,X509_STORE_CTX_cert_crl_fn cert_crl)888 void X509_STORE_set_cert_crl(X509_STORE *xs,
889 X509_STORE_CTX_cert_crl_fn cert_crl)
890 {
891 xs->cert_crl = cert_crl;
892 }
893
X509_STORE_get_cert_crl(const X509_STORE * xs)894 X509_STORE_CTX_cert_crl_fn X509_STORE_get_cert_crl(const X509_STORE *xs)
895 {
896 return xs->cert_crl;
897 }
898
X509_STORE_set_check_policy(X509_STORE * xs,X509_STORE_CTX_check_policy_fn check_policy)899 void X509_STORE_set_check_policy(X509_STORE *xs,
900 X509_STORE_CTX_check_policy_fn check_policy)
901 {
902 xs->check_policy = check_policy;
903 }
904
X509_STORE_get_check_policy(const X509_STORE * xs)905 X509_STORE_CTX_check_policy_fn X509_STORE_get_check_policy(const X509_STORE *xs)
906 {
907 return xs->check_policy;
908 }
909
X509_STORE_set_lookup_certs(X509_STORE * xs,X509_STORE_CTX_lookup_certs_fn lookup_certs)910 void X509_STORE_set_lookup_certs(X509_STORE *xs,
911 X509_STORE_CTX_lookup_certs_fn lookup_certs)
912 {
913 xs->lookup_certs = lookup_certs;
914 }
915
X509_STORE_get_lookup_certs(const X509_STORE * xs)916 X509_STORE_CTX_lookup_certs_fn X509_STORE_get_lookup_certs(const X509_STORE *xs)
917 {
918 return xs->lookup_certs;
919 }
920
X509_STORE_set_lookup_crls(X509_STORE * xs,X509_STORE_CTX_lookup_crls_fn lookup_crls)921 void X509_STORE_set_lookup_crls(X509_STORE *xs,
922 X509_STORE_CTX_lookup_crls_fn lookup_crls)
923 {
924 xs->lookup_crls = lookup_crls;
925 }
926
X509_STORE_get_lookup_crls(const X509_STORE * xs)927 X509_STORE_CTX_lookup_crls_fn X509_STORE_get_lookup_crls(const X509_STORE *xs)
928 {
929 return xs->lookup_crls;
930 }
931
X509_STORE_set_cleanup(X509_STORE * xs,X509_STORE_CTX_cleanup_fn cleanup)932 void X509_STORE_set_cleanup(X509_STORE *xs,
933 X509_STORE_CTX_cleanup_fn cleanup)
934 {
935 xs->cleanup = cleanup;
936 }
937
X509_STORE_get_cleanup(const X509_STORE * xs)938 X509_STORE_CTX_cleanup_fn X509_STORE_get_cleanup(const X509_STORE *xs)
939 {
940 return xs->cleanup;
941 }
942
X509_STORE_set_ex_data(X509_STORE * xs,int idx,void * data)943 int X509_STORE_set_ex_data(X509_STORE *xs, int idx, void *data)
944 {
945 return CRYPTO_set_ex_data(&xs->ex_data, idx, data);
946 }
947
X509_STORE_get_ex_data(const X509_STORE * xs,int idx)948 void *X509_STORE_get_ex_data(const X509_STORE *xs, int idx)
949 {
950 return CRYPTO_get_ex_data(&xs->ex_data, idx);
951 }
952
X509_STORE_CTX_get0_store(const X509_STORE_CTX * ctx)953 X509_STORE *X509_STORE_CTX_get0_store(const X509_STORE_CTX *ctx)
954 {
955 return ctx->store;
956 }
957