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