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