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