/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 | | 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 | 0 | ret->references = 1; |
213 | 0 | return ret; |
214 | | |
215 | 0 | err: |
216 | 0 | X509_VERIFY_PARAM_free(ret->param); |
217 | 0 | sk_X509_OBJECT_free(ret->objs); |
218 | 0 | sk_X509_LOOKUP_free(ret->get_cert_methods); |
219 | 0 | OPENSSL_free(ret); |
220 | 0 | return NULL; |
221 | 0 | } |
222 | | |
223 | | void X509_STORE_free(X509_STORE *xs) |
224 | 0 | { |
225 | 0 | int i; |
226 | 0 | STACK_OF(X509_LOOKUP) *sk; |
227 | 0 | X509_LOOKUP *lu; |
228 | |
|
229 | 0 | if (xs == NULL) |
230 | 0 | return; |
231 | 0 | CRYPTO_DOWN_REF(&xs->references, &i, xs->lock); |
232 | 0 | REF_PRINT_COUNT("X509_STORE", xs); |
233 | 0 | if (i > 0) |
234 | 0 | return; |
235 | 0 | REF_ASSERT_ISNT(i < 0); |
236 | |
|
237 | 0 | sk = xs->get_cert_methods; |
238 | 0 | for (i = 0; i < sk_X509_LOOKUP_num(sk); i++) { |
239 | 0 | lu = sk_X509_LOOKUP_value(sk, i); |
240 | 0 | X509_LOOKUP_shutdown(lu); |
241 | 0 | X509_LOOKUP_free(lu); |
242 | 0 | } |
243 | 0 | sk_X509_LOOKUP_free(sk); |
244 | 0 | sk_X509_OBJECT_pop_free(xs->objs, X509_OBJECT_free); |
245 | |
|
246 | 0 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509_STORE, xs, &xs->ex_data); |
247 | 0 | X509_VERIFY_PARAM_free(xs->param); |
248 | 0 | CRYPTO_THREAD_lock_free(xs->lock); |
249 | 0 | OPENSSL_free(xs); |
250 | 0 | } |
251 | | |
252 | | int X509_STORE_up_ref(X509_STORE *xs) |
253 | 0 | { |
254 | 0 | int i; |
255 | |
|
256 | 0 | if (CRYPTO_UP_REF(&xs->references, &i, xs->lock) <= 0) |
257 | 0 | return 0; |
258 | | |
259 | 0 | REF_PRINT_COUNT("X509_STORE", xs); |
260 | 0 | REF_ASSERT_ISNT(i < 2); |
261 | 0 | return i > 1 ? 1 : 0; |
262 | 0 | } |
263 | | |
264 | | X509_LOOKUP *X509_STORE_add_lookup(X509_STORE *xs, X509_LOOKUP_METHOD *m) |
265 | 0 | { |
266 | 0 | int i; |
267 | 0 | STACK_OF(X509_LOOKUP) *sk; |
268 | 0 | X509_LOOKUP *lu; |
269 | |
|
270 | 0 | sk = xs->get_cert_methods; |
271 | 0 | for (i = 0; i < sk_X509_LOOKUP_num(sk); i++) { |
272 | 0 | lu = sk_X509_LOOKUP_value(sk, i); |
273 | 0 | if (m == lu->method) { |
274 | 0 | return lu; |
275 | 0 | } |
276 | 0 | } |
277 | | /* a new one */ |
278 | 0 | lu = X509_LOOKUP_new(m); |
279 | 0 | if (lu == NULL) { |
280 | 0 | ERR_raise(ERR_LIB_X509, ERR_R_X509_LIB); |
281 | 0 | return NULL; |
282 | 0 | } |
283 | | |
284 | 0 | lu->store_ctx = xs; |
285 | 0 | if (sk_X509_LOOKUP_push(xs->get_cert_methods, lu)) |
286 | 0 | return lu; |
287 | | /* sk_X509_LOOKUP_push() failed */ |
288 | 0 | ERR_raise(ERR_LIB_X509, ERR_R_CRYPTO_LIB); |
289 | 0 | X509_LOOKUP_free(lu); |
290 | 0 | return NULL; |
291 | 0 | } |
292 | | |
293 | | /* Also fill the cache (ctx->store->objs) with all matching certificates. */ |
294 | | X509_OBJECT *X509_STORE_CTX_get_obj_by_subject(X509_STORE_CTX *ctx, |
295 | | X509_LOOKUP_TYPE type, |
296 | | const X509_NAME *name) |
297 | 0 | { |
298 | 0 | X509_OBJECT *ret = X509_OBJECT_new(); |
299 | |
|
300 | 0 | if (ret == NULL) |
301 | 0 | return NULL; |
302 | 0 | if (!X509_STORE_CTX_get_by_subject(ctx, type, name, ret)) { |
303 | 0 | X509_OBJECT_free(ret); |
304 | 0 | return NULL; |
305 | 0 | } |
306 | 0 | return ret; |
307 | 0 | } |
308 | | |
309 | | /* |
310 | | * May be called with |ret| == NULL just for the side effect of |
311 | | * caching all certs matching the given subject DN in |ctx->store->objs|. |
312 | | * Returns 1 if successful, |
313 | | * 0 if not found or X509_LOOKUP_by_subject_ex() returns an error, |
314 | | * -1 on failure |
315 | | */ |
316 | | static int ossl_x509_store_ctx_get_by_subject(const X509_STORE_CTX *ctx, |
317 | | X509_LOOKUP_TYPE type, |
318 | | const X509_NAME *name, |
319 | | X509_OBJECT *ret) |
320 | 0 | { |
321 | 0 | X509_STORE *store = ctx->store; |
322 | 0 | X509_LOOKUP *lu; |
323 | 0 | X509_OBJECT stmp, *tmp; |
324 | 0 | int i, j; |
325 | |
|
326 | 0 | if (store == NULL) |
327 | 0 | return 0; |
328 | | |
329 | 0 | stmp.type = X509_LU_NONE; |
330 | 0 | stmp.data.ptr = NULL; |
331 | |
|
332 | 0 | if (!x509_store_read_lock(store)) |
333 | 0 | return 0; |
334 | | /* Should already be sorted...but just in case */ |
335 | 0 | if (!sk_X509_OBJECT_is_sorted(store->objs)) { |
336 | 0 | X509_STORE_unlock(store); |
337 | | /* Take a write lock instead of a read lock */ |
338 | 0 | X509_STORE_lock(store); |
339 | | /* |
340 | | * Another thread might have sorted it in the meantime. But if so, |
341 | | * sk_X509_OBJECT_sort() exits early. |
342 | | */ |
343 | 0 | sk_X509_OBJECT_sort(store->objs); |
344 | 0 | } |
345 | 0 | tmp = X509_OBJECT_retrieve_by_subject(store->objs, type, name); |
346 | 0 | X509_STORE_unlock(store); |
347 | |
|
348 | 0 | if (tmp == NULL || type == X509_LU_CRL) { |
349 | 0 | for (i = 0; i < sk_X509_LOOKUP_num(store->get_cert_methods); i++) { |
350 | 0 | lu = sk_X509_LOOKUP_value(store->get_cert_methods, i); |
351 | 0 | if (lu->skip) |
352 | 0 | continue; |
353 | 0 | if (lu->method == NULL) |
354 | 0 | return -1; |
355 | 0 | j = X509_LOOKUP_by_subject_ex(lu, type, name, &stmp, |
356 | 0 | ctx->libctx, ctx->propq); |
357 | 0 | if (j != 0) { /* non-zero value is considered success here */ |
358 | 0 | tmp = &stmp; |
359 | 0 | break; |
360 | 0 | } |
361 | 0 | } |
362 | 0 | if (tmp == NULL) |
363 | 0 | return 0; |
364 | 0 | } |
365 | 0 | if (!X509_OBJECT_up_ref_count(tmp)) |
366 | 0 | return -1; |
367 | | |
368 | 0 | ret->type = tmp->type; |
369 | 0 | ret->data.ptr = tmp->data.ptr; |
370 | 0 | return 1; |
371 | 0 | } |
372 | | |
373 | | /* Also fill the cache |ctx->store->objs| with all matching certificates. */ |
374 | | int X509_STORE_CTX_get_by_subject(const X509_STORE_CTX *ctx, |
375 | | X509_LOOKUP_TYPE type, |
376 | | const X509_NAME *name, X509_OBJECT *ret) |
377 | 0 | { |
378 | 0 | return ossl_x509_store_ctx_get_by_subject(ctx, type, name, ret) > 0; |
379 | 0 | } |
380 | | |
381 | | static int x509_store_add(X509_STORE *store, void *x, int crl) |
382 | 0 | { |
383 | 0 | X509_OBJECT *obj; |
384 | 0 | int ret = 0, added = 0; |
385 | |
|
386 | 0 | if (x == NULL) |
387 | 0 | return 0; |
388 | 0 | obj = X509_OBJECT_new(); |
389 | 0 | if (obj == NULL) |
390 | 0 | return 0; |
391 | | |
392 | 0 | if (crl) { |
393 | 0 | obj->type = X509_LU_CRL; |
394 | 0 | obj->data.crl = (X509_CRL *)x; |
395 | 0 | } else { |
396 | 0 | obj->type = X509_LU_X509; |
397 | 0 | obj->data.x509 = (X509 *)x; |
398 | 0 | } |
399 | 0 | if (!X509_OBJECT_up_ref_count(obj)) { |
400 | 0 | obj->type = X509_LU_NONE; |
401 | 0 | X509_OBJECT_free(obj); |
402 | 0 | return 0; |
403 | 0 | } |
404 | | |
405 | 0 | if (!X509_STORE_lock(store)) { |
406 | 0 | obj->type = X509_LU_NONE; |
407 | 0 | X509_OBJECT_free(obj); |
408 | 0 | return 0; |
409 | 0 | } |
410 | | |
411 | 0 | if (X509_OBJECT_retrieve_match(store->objs, obj)) { |
412 | 0 | ret = 1; |
413 | 0 | } else { |
414 | 0 | added = sk_X509_OBJECT_push(store->objs, obj); |
415 | 0 | ret = added != 0; |
416 | 0 | } |
417 | 0 | X509_STORE_unlock(store); |
418 | |
|
419 | 0 | if (added == 0) /* obj not pushed */ |
420 | 0 | X509_OBJECT_free(obj); |
421 | |
|
422 | 0 | return ret; |
423 | 0 | } |
424 | | |
425 | | int X509_STORE_add_cert(X509_STORE *xs, X509 *x) |
426 | 0 | { |
427 | 0 | if (!x509_store_add(xs, x, 0)) { |
428 | 0 | ERR_raise(ERR_LIB_X509, ERR_R_X509_LIB); |
429 | 0 | return 0; |
430 | 0 | } |
431 | 0 | return 1; |
432 | 0 | } |
433 | | |
434 | | int X509_STORE_add_crl(X509_STORE *xs, X509_CRL *x) |
435 | 0 | { |
436 | 0 | if (!x509_store_add(xs, x, 1)) { |
437 | 0 | ERR_raise(ERR_LIB_X509, ERR_R_X509_LIB); |
438 | 0 | return 0; |
439 | 0 | } |
440 | 0 | return 1; |
441 | 0 | } |
442 | | |
443 | | int X509_OBJECT_up_ref_count(X509_OBJECT *a) |
444 | 0 | { |
445 | 0 | switch (a->type) { |
446 | 0 | case X509_LU_NONE: |
447 | 0 | break; |
448 | 0 | case X509_LU_X509: |
449 | 0 | return X509_up_ref(a->data.x509); |
450 | 0 | case X509_LU_CRL: |
451 | 0 | return X509_CRL_up_ref(a->data.crl); |
452 | 0 | } |
453 | 0 | return 1; |
454 | 0 | } |
455 | | |
456 | | X509 *X509_OBJECT_get0_X509(const X509_OBJECT *a) |
457 | 0 | { |
458 | 0 | if (a == NULL || a->type != X509_LU_X509) |
459 | 0 | return NULL; |
460 | 0 | return a->data.x509; |
461 | 0 | } |
462 | | |
463 | | X509_CRL *X509_OBJECT_get0_X509_CRL(const X509_OBJECT *a) |
464 | 0 | { |
465 | 0 | if (a == NULL || a->type != X509_LU_CRL) |
466 | 0 | return NULL; |
467 | 0 | return a->data.crl; |
468 | 0 | } |
469 | | |
470 | | X509_LOOKUP_TYPE X509_OBJECT_get_type(const X509_OBJECT *a) |
471 | 0 | { |
472 | 0 | return a->type; |
473 | 0 | } |
474 | | |
475 | | X509_OBJECT *X509_OBJECT_new(void) |
476 | 0 | { |
477 | 0 | X509_OBJECT *ret = OPENSSL_zalloc(sizeof(*ret)); |
478 | |
|
479 | 0 | if (ret == NULL) |
480 | 0 | return NULL; |
481 | 0 | ret->type = X509_LU_NONE; |
482 | 0 | return ret; |
483 | 0 | } |
484 | | |
485 | | static void x509_object_free_internal(X509_OBJECT *a) |
486 | 0 | { |
487 | 0 | if (a == NULL) |
488 | 0 | return; |
489 | 0 | switch (a->type) { |
490 | 0 | case X509_LU_NONE: |
491 | 0 | break; |
492 | 0 | case X509_LU_X509: |
493 | 0 | X509_free(a->data.x509); |
494 | 0 | break; |
495 | 0 | case X509_LU_CRL: |
496 | 0 | X509_CRL_free(a->data.crl); |
497 | 0 | break; |
498 | 0 | } |
499 | 0 | } |
500 | | |
501 | | int X509_OBJECT_set1_X509(X509_OBJECT *a, X509 *obj) |
502 | 0 | { |
503 | 0 | if (a == NULL || !X509_up_ref(obj)) |
504 | 0 | return 0; |
505 | | |
506 | 0 | x509_object_free_internal(a); |
507 | 0 | a->type = X509_LU_X509; |
508 | 0 | a->data.x509 = obj; |
509 | 0 | return 1; |
510 | 0 | } |
511 | | |
512 | | int X509_OBJECT_set1_X509_CRL(X509_OBJECT *a, X509_CRL *obj) |
513 | 0 | { |
514 | 0 | if (a == NULL || !X509_CRL_up_ref(obj)) |
515 | 0 | return 0; |
516 | | |
517 | 0 | x509_object_free_internal(a); |
518 | 0 | a->type = X509_LU_CRL; |
519 | 0 | a->data.crl = obj; |
520 | 0 | return 1; |
521 | 0 | } |
522 | | |
523 | | void X509_OBJECT_free(X509_OBJECT *a) |
524 | 0 | { |
525 | 0 | x509_object_free_internal(a); |
526 | 0 | OPENSSL_free(a); |
527 | 0 | } |
528 | | |
529 | | /* Returns -1 if not found, but also on error */ |
530 | | static int x509_object_idx_cnt(STACK_OF(X509_OBJECT) *h, X509_LOOKUP_TYPE type, |
531 | | const X509_NAME *name, int *pnmatch) |
532 | 0 | { |
533 | 0 | X509_OBJECT stmp; |
534 | 0 | X509 x509_s; |
535 | 0 | X509_CRL crl_s; |
536 | |
|
537 | 0 | stmp.type = type; |
538 | 0 | switch (type) { |
539 | 0 | case X509_LU_X509: |
540 | 0 | stmp.data.x509 = &x509_s; |
541 | 0 | x509_s.cert_info.subject = (X509_NAME *)name; /* won't modify it */ |
542 | 0 | break; |
543 | 0 | case X509_LU_CRL: |
544 | 0 | stmp.data.crl = &crl_s; |
545 | 0 | crl_s.crl.issuer = (X509_NAME *)name; /* won't modify it */ |
546 | 0 | break; |
547 | 0 | case X509_LU_NONE: |
548 | 0 | default: |
549 | | /* abort(); */ |
550 | 0 | return -1; |
551 | 0 | } |
552 | | |
553 | | /* Assumes h is locked for read if applicable */ |
554 | 0 | return sk_X509_OBJECT_find_all(h, &stmp, pnmatch); |
555 | 0 | } |
556 | | |
557 | | /* Assumes h is locked for read if applicable */ |
558 | | int X509_OBJECT_idx_by_subject(STACK_OF(X509_OBJECT) *h, X509_LOOKUP_TYPE type, |
559 | | const X509_NAME *name) |
560 | 0 | { |
561 | 0 | return x509_object_idx_cnt(h, type, name, NULL); |
562 | 0 | } |
563 | | |
564 | | /* Assumes h is locked for read if applicable */ |
565 | | X509_OBJECT *X509_OBJECT_retrieve_by_subject(STACK_OF(X509_OBJECT) *h, |
566 | | X509_LOOKUP_TYPE type, |
567 | | const X509_NAME *name) |
568 | 0 | { |
569 | 0 | int idx = X509_OBJECT_idx_by_subject(h, type, name); |
570 | |
|
571 | 0 | if (idx == -1) |
572 | 0 | return NULL; |
573 | 0 | return sk_X509_OBJECT_value(h, idx); |
574 | 0 | } |
575 | | |
576 | | STACK_OF(X509_OBJECT) *X509_STORE_get0_objects(const X509_STORE *xs) |
577 | 0 | { |
578 | 0 | return xs->objs; |
579 | 0 | } |
580 | | |
581 | | STACK_OF(X509) *X509_STORE_get1_all_certs(X509_STORE *store) |
582 | 0 | { |
583 | 0 | STACK_OF(X509) *sk; |
584 | 0 | STACK_OF(X509_OBJECT) *objs; |
585 | 0 | int i; |
586 | |
|
587 | 0 | if (store == NULL) { |
588 | 0 | ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER); |
589 | 0 | return NULL; |
590 | 0 | } |
591 | 0 | if ((sk = sk_X509_new_null()) == NULL) |
592 | 0 | return NULL; |
593 | 0 | if (!X509_STORE_lock(store)) |
594 | 0 | goto out_free; |
595 | | |
596 | 0 | sk_X509_OBJECT_sort(store->objs); |
597 | 0 | objs = X509_STORE_get0_objects(store); |
598 | 0 | for (i = 0; i < sk_X509_OBJECT_num(objs); i++) { |
599 | 0 | X509 *cert = X509_OBJECT_get0_X509(sk_X509_OBJECT_value(objs, i)); |
600 | |
|
601 | 0 | if (cert != NULL |
602 | 0 | && !X509_add_cert(sk, cert, X509_ADD_FLAG_UP_REF)) |
603 | 0 | goto err; |
604 | 0 | } |
605 | 0 | X509_STORE_unlock(store); |
606 | 0 | return sk; |
607 | | |
608 | 0 | err: |
609 | 0 | X509_STORE_unlock(store); |
610 | 0 | out_free: |
611 | 0 | OSSL_STACK_OF_X509_free(sk); |
612 | 0 | return NULL; |
613 | 0 | } |
614 | | |
615 | | /* Returns NULL on internal/fatal error, empty stack if not found */ |
616 | | STACK_OF(X509) *X509_STORE_CTX_get1_certs(X509_STORE_CTX *ctx, |
617 | | const X509_NAME *nm) |
618 | 0 | { |
619 | 0 | int i, idx, cnt; |
620 | 0 | STACK_OF(X509) *sk = NULL; |
621 | 0 | X509 *x; |
622 | 0 | X509_OBJECT *obj; |
623 | 0 | X509_STORE *store = ctx->store; |
624 | |
|
625 | 0 | if (store == NULL) |
626 | 0 | return sk_X509_new_null(); |
627 | | |
628 | 0 | if (!X509_STORE_lock(store)) |
629 | 0 | return NULL; |
630 | | |
631 | 0 | sk_X509_OBJECT_sort(store->objs); |
632 | 0 | idx = x509_object_idx_cnt(store->objs, X509_LU_X509, nm, &cnt); |
633 | 0 | if (idx < 0) { |
634 | | /* |
635 | | * Nothing found in cache: do lookup to possibly add new objects to |
636 | | * cache |
637 | | */ |
638 | 0 | X509_OBJECT *xobj = X509_OBJECT_new(); |
639 | |
|
640 | 0 | X509_STORE_unlock(store); |
641 | 0 | if (xobj == NULL) |
642 | 0 | return NULL; |
643 | 0 | i = ossl_x509_store_ctx_get_by_subject(ctx, X509_LU_X509, nm, xobj); |
644 | 0 | if (i <= 0) { |
645 | 0 | X509_OBJECT_free(xobj); |
646 | 0 | return i < 0 ? NULL : sk_X509_new_null(); |
647 | 0 | } |
648 | 0 | X509_OBJECT_free(xobj); |
649 | 0 | if (!X509_STORE_lock(store)) |
650 | 0 | return NULL; |
651 | 0 | sk_X509_OBJECT_sort(store->objs); |
652 | 0 | idx = x509_object_idx_cnt(store->objs, X509_LU_X509, nm, &cnt); |
653 | 0 | if (idx < 0) { |
654 | 0 | sk = sk_X509_new_null(); |
655 | 0 | goto end; |
656 | 0 | } |
657 | 0 | } |
658 | | |
659 | 0 | sk = sk_X509_new_null(); |
660 | 0 | if (sk == NULL) |
661 | 0 | goto end; |
662 | 0 | for (i = 0; i < cnt; i++, idx++) { |
663 | 0 | obj = sk_X509_OBJECT_value(store->objs, idx); |
664 | 0 | x = obj->data.x509; |
665 | 0 | if (!X509_add_cert(sk, x, X509_ADD_FLAG_UP_REF)) { |
666 | 0 | X509_STORE_unlock(store); |
667 | 0 | OSSL_STACK_OF_X509_free(sk); |
668 | 0 | return NULL; |
669 | 0 | } |
670 | 0 | } |
671 | 0 | end: |
672 | 0 | X509_STORE_unlock(store); |
673 | 0 | return sk; |
674 | 0 | } |
675 | | |
676 | | /* Returns NULL on internal/fatal error, empty stack if not found */ |
677 | | STACK_OF(X509_CRL) *X509_STORE_CTX_get1_crls(const X509_STORE_CTX *ctx, |
678 | | const X509_NAME *nm) |
679 | 0 | { |
680 | 0 | int i = 1, idx, cnt; |
681 | 0 | STACK_OF(X509_CRL) *sk = sk_X509_CRL_new_null(); |
682 | 0 | X509_CRL *x; |
683 | 0 | X509_OBJECT *obj, *xobj = X509_OBJECT_new(); |
684 | 0 | X509_STORE *store = ctx->store; |
685 | | |
686 | | /* Always do lookup to possibly add new CRLs to cache */ |
687 | 0 | if (sk == NULL |
688 | 0 | || xobj == NULL |
689 | 0 | || (i = ossl_x509_store_ctx_get_by_subject(ctx, X509_LU_CRL, |
690 | 0 | nm, xobj)) < 0) { |
691 | 0 | X509_OBJECT_free(xobj); |
692 | 0 | sk_X509_CRL_free(sk); |
693 | 0 | return NULL; |
694 | 0 | } |
695 | 0 | X509_OBJECT_free(xobj); |
696 | 0 | if (i == 0) |
697 | 0 | return sk; |
698 | 0 | if (!X509_STORE_lock(store)) { |
699 | 0 | sk_X509_CRL_free(sk); |
700 | 0 | return NULL; |
701 | 0 | } |
702 | 0 | sk_X509_OBJECT_sort(store->objs); |
703 | 0 | idx = x509_object_idx_cnt(store->objs, X509_LU_CRL, nm, &cnt); |
704 | 0 | if (idx < 0) { |
705 | 0 | X509_STORE_unlock(store); |
706 | 0 | return sk; |
707 | 0 | } |
708 | | |
709 | 0 | for (i = 0; i < cnt; i++, idx++) { |
710 | 0 | obj = sk_X509_OBJECT_value(store->objs, idx); |
711 | 0 | x = obj->data.crl; |
712 | 0 | if (!X509_CRL_up_ref(x)) { |
713 | 0 | X509_STORE_unlock(store); |
714 | 0 | sk_X509_CRL_pop_free(sk, X509_CRL_free); |
715 | 0 | return NULL; |
716 | 0 | } |
717 | 0 | if (!sk_X509_CRL_push(sk, x)) { |
718 | 0 | X509_STORE_unlock(store); |
719 | 0 | X509_CRL_free(x); |
720 | 0 | sk_X509_CRL_pop_free(sk, X509_CRL_free); |
721 | 0 | return NULL; |
722 | 0 | } |
723 | 0 | } |
724 | 0 | X509_STORE_unlock(store); |
725 | 0 | return sk; |
726 | 0 | } |
727 | | |
728 | | X509_OBJECT *X509_OBJECT_retrieve_match(STACK_OF(X509_OBJECT) *h, |
729 | | X509_OBJECT *x) |
730 | 0 | { |
731 | 0 | int idx, i, num; |
732 | 0 | X509_OBJECT *obj; |
733 | |
|
734 | 0 | idx = sk_X509_OBJECT_find(h, x); |
735 | 0 | if (idx < 0) |
736 | 0 | return NULL; |
737 | 0 | if ((x->type != X509_LU_X509) && (x->type != X509_LU_CRL)) |
738 | 0 | return sk_X509_OBJECT_value(h, idx); |
739 | 0 | for (i = idx, num = sk_X509_OBJECT_num(h); i < num; i++) { |
740 | 0 | obj = sk_X509_OBJECT_value(h, i); |
741 | 0 | if (x509_object_cmp((const X509_OBJECT **)&obj, |
742 | 0 | (const X509_OBJECT **)&x)) |
743 | 0 | return NULL; |
744 | 0 | if (x->type == X509_LU_X509) { |
745 | 0 | if (!X509_cmp(obj->data.x509, x->data.x509)) |
746 | 0 | return obj; |
747 | 0 | } else if (x->type == X509_LU_CRL) { |
748 | 0 | if (X509_CRL_match(obj->data.crl, x->data.crl) == 0) |
749 | 0 | return obj; |
750 | 0 | } else { |
751 | 0 | return obj; |
752 | 0 | } |
753 | 0 | } |
754 | 0 | return NULL; |
755 | 0 | } |
756 | | |
757 | | /*- |
758 | | * Try to get issuer cert from |ctx->store| matching the subject name of |x|. |
759 | | * Prefer the first non-expired one, else take the most recently expired one. |
760 | | * |
761 | | * Return values are: |
762 | | * 1 lookup successful. |
763 | | * 0 certificate not found. |
764 | | * -1 some other error. |
765 | | */ |
766 | | int X509_STORE_CTX_get1_issuer(X509 **issuer, X509_STORE_CTX *ctx, X509 *x) |
767 | 0 | { |
768 | 0 | const X509_NAME *xn; |
769 | 0 | X509_OBJECT *obj = X509_OBJECT_new(), *pobj = NULL; |
770 | 0 | X509_STORE *store = ctx->store; |
771 | 0 | int i, ok, idx, ret, nmatch = 0; |
772 | |
|
773 | 0 | if (obj == NULL) |
774 | 0 | return -1; |
775 | 0 | *issuer = NULL; |
776 | 0 | xn = X509_get_issuer_name(x); |
777 | 0 | ok = ossl_x509_store_ctx_get_by_subject(ctx, X509_LU_X509, xn, obj); |
778 | 0 | if (ok != 1) { |
779 | 0 | X509_OBJECT_free(obj); |
780 | 0 | return ok; |
781 | 0 | } |
782 | | /* If certificate matches and is currently valid all OK */ |
783 | 0 | if (ctx->check_issued(ctx, x, obj->data.x509)) { |
784 | 0 | if (ossl_x509_check_cert_time(ctx, obj->data.x509, -1)) { |
785 | 0 | *issuer = obj->data.x509; |
786 | | /* |*issuer| has taken over the cert reference from |obj| */ |
787 | 0 | obj->type = X509_LU_NONE; |
788 | 0 | X509_OBJECT_free(obj); |
789 | 0 | return 1; |
790 | 0 | } |
791 | 0 | } |
792 | 0 | X509_OBJECT_free(obj); |
793 | | |
794 | | /* |
795 | | * Due to limitations of the API this can only retrieve a single cert. |
796 | | * However it will fill the cache with all matching certificates, |
797 | | * so we can examine the cache for all matches. |
798 | | */ |
799 | 0 | if (store == NULL) |
800 | 0 | return 0; |
801 | | |
802 | | /* Find index of first currently valid cert accepted by 'check_issued' */ |
803 | 0 | ret = 0; |
804 | 0 | if (!X509_STORE_lock(store)) |
805 | 0 | return 0; |
806 | | |
807 | 0 | sk_X509_OBJECT_sort(store->objs); |
808 | 0 | idx = x509_object_idx_cnt(store->objs, X509_LU_X509, xn, &nmatch); |
809 | 0 | if (idx != -1) { /* should be true as we've had at least one match */ |
810 | | /* Look through all matching certs for suitable issuer */ |
811 | 0 | for (i = idx; i < idx + nmatch; i++) { |
812 | 0 | pobj = sk_X509_OBJECT_value(store->objs, i); |
813 | | /* See if we've run past the matches */ |
814 | 0 | if (pobj->type != X509_LU_X509) |
815 | 0 | break; |
816 | 0 | if (ctx->check_issued(ctx, x, pobj->data.x509)) { |
817 | 0 | ret = 1; |
818 | | /* If times check fine, exit with match, else keep looking. */ |
819 | 0 | if (ossl_x509_check_cert_time(ctx, pobj->data.x509, -1)) { |
820 | 0 | *issuer = pobj->data.x509; |
821 | 0 | break; |
822 | 0 | } |
823 | | /* |
824 | | * Leave the so far most recently expired match in *issuer |
825 | | * so we return nearest match if no certificate time is OK. |
826 | | */ |
827 | 0 | if (*issuer == NULL |
828 | 0 | || ASN1_TIME_compare(X509_get0_notAfter(pobj->data.x509), |
829 | 0 | X509_get0_notAfter(*issuer)) > 0) |
830 | 0 | *issuer = pobj->data.x509; |
831 | 0 | } |
832 | 0 | } |
833 | 0 | } |
834 | 0 | if (*issuer != NULL && !X509_up_ref(*issuer)) { |
835 | 0 | *issuer = NULL; |
836 | 0 | ret = -1; |
837 | 0 | } |
838 | 0 | X509_STORE_unlock(store); |
839 | 0 | return ret; |
840 | 0 | } |
841 | | |
842 | | int X509_STORE_set_flags(X509_STORE *xs, unsigned long flags) |
843 | 0 | { |
844 | 0 | return X509_VERIFY_PARAM_set_flags(xs->param, flags); |
845 | 0 | } |
846 | | |
847 | | int X509_STORE_set_depth(X509_STORE *xs, int depth) |
848 | 0 | { |
849 | 0 | X509_VERIFY_PARAM_set_depth(xs->param, depth); |
850 | 0 | return 1; |
851 | 0 | } |
852 | | |
853 | | int X509_STORE_set_purpose(X509_STORE *xs, int purpose) |
854 | 0 | { |
855 | 0 | return X509_VERIFY_PARAM_set_purpose(xs->param, purpose); |
856 | 0 | } |
857 | | |
858 | | int X509_STORE_set_trust(X509_STORE *xs, int trust) |
859 | 0 | { |
860 | 0 | return X509_VERIFY_PARAM_set_trust(xs->param, trust); |
861 | 0 | } |
862 | | |
863 | | int X509_STORE_set1_param(X509_STORE *xs, const X509_VERIFY_PARAM *param) |
864 | 0 | { |
865 | 0 | return X509_VERIFY_PARAM_set1(xs->param, param); |
866 | 0 | } |
867 | | |
868 | | X509_VERIFY_PARAM *X509_STORE_get0_param(const X509_STORE *xs) |
869 | 0 | { |
870 | 0 | return xs->param; |
871 | 0 | } |
872 | | |
873 | | void X509_STORE_set_verify(X509_STORE *xs, X509_STORE_CTX_verify_fn verify) |
874 | 0 | { |
875 | 0 | xs->verify = verify; |
876 | 0 | } |
877 | | |
878 | | X509_STORE_CTX_verify_fn X509_STORE_get_verify(const X509_STORE *xs) |
879 | 0 | { |
880 | 0 | return xs->verify; |
881 | 0 | } |
882 | | |
883 | | void X509_STORE_set_verify_cb(X509_STORE *xs, |
884 | | X509_STORE_CTX_verify_cb verify_cb) |
885 | 0 | { |
886 | 0 | xs->verify_cb = verify_cb; |
887 | 0 | } |
888 | | |
889 | | X509_STORE_CTX_verify_cb X509_STORE_get_verify_cb(const X509_STORE *xs) |
890 | 0 | { |
891 | 0 | return xs->verify_cb; |
892 | 0 | } |
893 | | |
894 | | void X509_STORE_set_get_issuer(X509_STORE *xs, |
895 | | X509_STORE_CTX_get_issuer_fn get_issuer) |
896 | 0 | { |
897 | 0 | xs->get_issuer = get_issuer; |
898 | 0 | } |
899 | | |
900 | | X509_STORE_CTX_get_issuer_fn X509_STORE_get_get_issuer(const X509_STORE *xs) |
901 | 0 | { |
902 | 0 | return xs->get_issuer; |
903 | 0 | } |
904 | | |
905 | | void X509_STORE_set_check_issued(X509_STORE *xs, |
906 | | X509_STORE_CTX_check_issued_fn check_issued) |
907 | 0 | { |
908 | 0 | xs->check_issued = check_issued; |
909 | 0 | } |
910 | | |
911 | | X509_STORE_CTX_check_issued_fn X509_STORE_get_check_issued(const X509_STORE *xs) |
912 | 0 | { |
913 | 0 | return xs->check_issued; |
914 | 0 | } |
915 | | |
916 | | void X509_STORE_set_check_revocation(X509_STORE *xs, |
917 | | X509_STORE_CTX_check_revocation_fn cb) |
918 | 0 | { |
919 | 0 | xs->check_revocation = cb; |
920 | 0 | } |
921 | | |
922 | | X509_STORE_CTX_check_revocation_fn X509_STORE_get_check_revocation(const X509_STORE *xs) |
923 | 0 | { |
924 | 0 | return xs->check_revocation; |
925 | 0 | } |
926 | | |
927 | | void X509_STORE_set_get_crl(X509_STORE *xs, |
928 | | X509_STORE_CTX_get_crl_fn get_crl) |
929 | 0 | { |
930 | 0 | xs->get_crl = get_crl; |
931 | 0 | } |
932 | | |
933 | | X509_STORE_CTX_get_crl_fn X509_STORE_get_get_crl(const X509_STORE *xs) |
934 | 0 | { |
935 | 0 | return xs->get_crl; |
936 | 0 | } |
937 | | |
938 | | void X509_STORE_set_check_crl(X509_STORE *xs, |
939 | | X509_STORE_CTX_check_crl_fn check_crl) |
940 | 0 | { |
941 | 0 | xs->check_crl = check_crl; |
942 | 0 | } |
943 | | |
944 | | X509_STORE_CTX_check_crl_fn X509_STORE_get_check_crl(const X509_STORE *xs) |
945 | 0 | { |
946 | 0 | return xs->check_crl; |
947 | 0 | } |
948 | | |
949 | | void X509_STORE_set_cert_crl(X509_STORE *xs, |
950 | | X509_STORE_CTX_cert_crl_fn cert_crl) |
951 | 0 | { |
952 | 0 | xs->cert_crl = cert_crl; |
953 | 0 | } |
954 | | |
955 | | X509_STORE_CTX_cert_crl_fn X509_STORE_get_cert_crl(const X509_STORE *xs) |
956 | 0 | { |
957 | 0 | return xs->cert_crl; |
958 | 0 | } |
959 | | |
960 | | void X509_STORE_set_check_policy(X509_STORE *xs, |
961 | | X509_STORE_CTX_check_policy_fn check_policy) |
962 | 0 | { |
963 | 0 | xs->check_policy = check_policy; |
964 | 0 | } |
965 | | |
966 | | X509_STORE_CTX_check_policy_fn X509_STORE_get_check_policy(const X509_STORE *xs) |
967 | 0 | { |
968 | 0 | return xs->check_policy; |
969 | 0 | } |
970 | | |
971 | | void X509_STORE_set_lookup_certs(X509_STORE *xs, |
972 | | X509_STORE_CTX_lookup_certs_fn lookup_certs) |
973 | 0 | { |
974 | 0 | xs->lookup_certs = lookup_certs; |
975 | 0 | } |
976 | | |
977 | | X509_STORE_CTX_lookup_certs_fn X509_STORE_get_lookup_certs(const X509_STORE *xs) |
978 | 0 | { |
979 | 0 | return xs->lookup_certs; |
980 | 0 | } |
981 | | |
982 | | void X509_STORE_set_lookup_crls(X509_STORE *xs, |
983 | | X509_STORE_CTX_lookup_crls_fn lookup_crls) |
984 | 0 | { |
985 | 0 | xs->lookup_crls = lookup_crls; |
986 | 0 | } |
987 | | |
988 | | X509_STORE_CTX_lookup_crls_fn X509_STORE_get_lookup_crls(const X509_STORE *xs) |
989 | 0 | { |
990 | 0 | return xs->lookup_crls; |
991 | 0 | } |
992 | | |
993 | | void X509_STORE_set_cleanup(X509_STORE *xs, |
994 | | X509_STORE_CTX_cleanup_fn cleanup) |
995 | 0 | { |
996 | 0 | xs->cleanup = cleanup; |
997 | 0 | } |
998 | | |
999 | | X509_STORE_CTX_cleanup_fn X509_STORE_get_cleanup(const X509_STORE *xs) |
1000 | 0 | { |
1001 | 0 | return xs->cleanup; |
1002 | 0 | } |
1003 | | |
1004 | | int X509_STORE_set_ex_data(X509_STORE *xs, int idx, void *data) |
1005 | 0 | { |
1006 | 0 | return CRYPTO_set_ex_data(&xs->ex_data, idx, data); |
1007 | 0 | } |
1008 | | |
1009 | | void *X509_STORE_get_ex_data(const X509_STORE *xs, int idx) |
1010 | 0 | { |
1011 | 0 | return CRYPTO_get_ex_data(&xs->ex_data, idx); |
1012 | 0 | } |
1013 | | |
1014 | | X509_STORE *X509_STORE_CTX_get0_store(const X509_STORE_CTX *ctx) |
1015 | 0 | { |
1016 | 0 | return ctx->store; |
1017 | 0 | } |