/src/boringssl/crypto/x509/x509_lu.cc
Line | Count | Source |
1 | | // Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #include <assert.h> |
16 | | #include <limits.h> |
17 | | #include <string.h> |
18 | | |
19 | | #include <algorithm> |
20 | | #include <utility> |
21 | | |
22 | | #include <openssl/err.h> |
23 | | #include <openssl/mem.h> |
24 | | #include <openssl/x509.h> |
25 | | |
26 | | #include "../internal.h" |
27 | | #include "../mem_internal.h" |
28 | | #include "internal.h" |
29 | | |
30 | | |
31 | | using namespace bssl; |
32 | | |
33 | | static int X509_OBJECT_idx_by_subject(STACK_OF(X509_OBJECT) *h, int type, |
34 | | const X509_NAME *name); |
35 | | static X509_OBJECT *X509_OBJECT_retrieve_by_subject(STACK_OF(X509_OBJECT) *h, |
36 | | int type, |
37 | | const X509_NAME *name); |
38 | | static X509_OBJECT *X509_OBJECT_retrieve_match(STACK_OF(X509_OBJECT) *h, |
39 | | X509_OBJECT *x); |
40 | | static int X509_OBJECT_up_ref_count(X509_OBJECT *a); |
41 | | |
42 | | static X509_LOOKUP *X509_LOOKUP_new(const X509_LOOKUP_METHOD *method, |
43 | | X509_STORE *store); |
44 | | static int X509_LOOKUP_by_subject(X509_LOOKUP *ctx, int type, |
45 | | const X509_NAME *name, X509_OBJECT *ret); |
46 | | |
47 | | static X509_LOOKUP *X509_LOOKUP_new(const X509_LOOKUP_METHOD *method, |
48 | 0 | X509_STORE *store) { |
49 | 0 | X509_LOOKUP *ret = New<X509_LOOKUP>(); |
50 | 0 | if (ret == nullptr) { |
51 | 0 | return nullptr; |
52 | 0 | } |
53 | | |
54 | 0 | ret->method = method; |
55 | 0 | ret->store_ctx = store; |
56 | 0 | if (method->new_item != nullptr && !method->new_item(ret)) { |
57 | 0 | Delete(ret); |
58 | 0 | return nullptr; |
59 | 0 | } |
60 | 0 | return ret; |
61 | 0 | } |
62 | | |
63 | 0 | void X509_LOOKUP_free(X509_LOOKUP *ctx) { |
64 | 0 | if (ctx == nullptr) { |
65 | 0 | return; |
66 | 0 | } |
67 | 0 | if (ctx->method != nullptr && ctx->method->free != nullptr) { |
68 | 0 | (*ctx->method->free)(ctx); |
69 | 0 | } |
70 | 0 | Delete(ctx); |
71 | 0 | } |
72 | | |
73 | | int X509_LOOKUP_ctrl(X509_LOOKUP *ctx, int cmd, const char *argc, long argl, |
74 | 0 | char **ret) { |
75 | 0 | if (ctx->method == nullptr) { |
76 | 0 | return -1; |
77 | 0 | } |
78 | 0 | if (ctx->method->ctrl != nullptr) { |
79 | 0 | return ctx->method->ctrl(ctx, cmd, argc, argl, ret); |
80 | 0 | } else { |
81 | 0 | return 1; |
82 | 0 | } |
83 | 0 | } |
84 | | |
85 | | static int X509_LOOKUP_by_subject(X509_LOOKUP *ctx, int type, |
86 | 0 | const X509_NAME *name, X509_OBJECT *ret) { |
87 | 0 | if (ctx->method == nullptr || ctx->method->get_by_subject == nullptr) { |
88 | 0 | return 0; |
89 | 0 | } |
90 | | // Note `get_by_subject` leaves `ret` in an inconsistent state. It has |
91 | | // pointers to an `X509` or `X509_CRL`, but has not bumped the refcount yet. |
92 | | // For now, the caller is expected to fix this, but ideally we'd fix the |
93 | | // `X509_LOOKUP` convention itself. |
94 | 0 | return ctx->method->get_by_subject(ctx, type, name, ret) > 0; |
95 | 0 | } |
96 | | |
97 | | // x509_object_cmp_name compares `a` against the specified type and name. This |
98 | | // avoids needing to construct a reference certificate or CRL. |
99 | | static int x509_object_cmp_name(const X509_OBJECT *a, int type, |
100 | 0 | const X509_NAME *name) { |
101 | 0 | int ret = a->type - type; |
102 | 0 | if (ret) { |
103 | 0 | return ret; |
104 | 0 | } |
105 | 0 | switch (type) { |
106 | 0 | case X509_LU_X509: |
107 | 0 | return X509_NAME_cmp(X509_get_subject_name(a->data.x509), name); |
108 | 0 | case X509_LU_CRL: |
109 | 0 | return X509_NAME_cmp(X509_CRL_get_issuer(a->data.crl), name); |
110 | 0 | default: |
111 | | // abort(); |
112 | 0 | return 0; |
113 | 0 | } |
114 | 0 | } |
115 | | |
116 | 0 | static int x509_object_cmp(const X509_OBJECT *a, const X509_OBJECT *b) { |
117 | 0 | switch (b->type) { |
118 | 0 | case X509_LU_X509: |
119 | 0 | return x509_object_cmp_name(a, b->type, |
120 | 0 | X509_get_subject_name(b->data.x509)); |
121 | 0 | case X509_LU_CRL: |
122 | 0 | return x509_object_cmp_name(a, b->type, X509_CRL_get_issuer(b->data.crl)); |
123 | 0 | default: |
124 | | // abort(); |
125 | 0 | return 0; |
126 | 0 | } |
127 | 0 | } |
128 | | |
129 | | static int x509_object_cmp_sk(const X509_OBJECT *const *a, |
130 | 0 | const X509_OBJECT *const *b) { |
131 | 0 | return x509_object_cmp(*a, *b); |
132 | 0 | } |
133 | | |
134 | | X509Store::X509Store() |
135 | 4.98k | : RefCounted(CheckSubClass()), |
136 | 4.98k | objs(sk_X509_OBJECT_new(x509_object_cmp_sk)), |
137 | 4.98k | param(X509_VERIFY_PARAM_new()) {} |
138 | | |
139 | 4.98k | X509_STORE *X509_STORE_new() { |
140 | 4.98k | UniquePtr<X509Store> ret(New<X509Store>()); |
141 | 4.98k | if (ret == nullptr || ret->objs == nullptr || ret->param == nullptr) { |
142 | 0 | return nullptr; |
143 | 0 | } |
144 | 4.98k | return ret.release(); |
145 | 4.98k | } |
146 | | |
147 | 0 | int X509_STORE_up_ref(X509_STORE *store) { |
148 | 0 | FromOpaque(store)->UpRefInternal(); |
149 | 0 | return 1; |
150 | 0 | } |
151 | | |
152 | 0 | X509_STORE *X509_STORE_dup_ref(const X509_STORE *store) { |
153 | 0 | if (store == nullptr) { |
154 | 0 | return nullptr; |
155 | 0 | } |
156 | 0 | return FromOpaque(store)->DupRefInternal(); |
157 | 0 | } |
158 | | |
159 | 68.7k | void X509_STORE_free(X509_STORE *vfy) { |
160 | 68.7k | if (vfy == nullptr) { |
161 | 63.7k | return; |
162 | 63.7k | } |
163 | 4.97k | FromOpaque(vfy)->DecRefInternal(); |
164 | 4.97k | } |
165 | | |
166 | | X509_LOOKUP *X509_STORE_add_lookup(X509_STORE *store, |
167 | 0 | const X509_LOOKUP_METHOD *method) { |
168 | 0 | auto *impl = FromOpaque(store); |
169 | 0 | auto it = |
170 | 0 | std::find_if(impl->get_cert_methods.begin(), impl->get_cert_methods.end(), |
171 | 0 | [&](const auto &lu) { return method == lu->method; }); |
172 | 0 | if (it != impl->get_cert_methods.end()) { |
173 | 0 | return it->get(); |
174 | 0 | } |
175 | | |
176 | 0 | UniquePtr<X509_LOOKUP> lu(X509_LOOKUP_new(method, store)); |
177 | 0 | X509_LOOKUP *lu_raw = lu.get(); |
178 | 0 | if (lu == nullptr || !impl->get_cert_methods.Push(std::move(lu))) { |
179 | 0 | return nullptr; |
180 | 0 | } |
181 | | |
182 | 0 | return lu_raw; |
183 | 0 | } |
184 | | |
185 | | int X509_STORE_CTX_get_by_subject(X509_STORE_CTX *vs, int type, |
186 | 0 | const X509_NAME *name, X509_OBJECT *ret) { |
187 | 0 | X509Store *ctx = FromOpaque(vs->ctx); |
188 | 0 | X509_OBJECT stmp; |
189 | 0 | ctx->objs_lock.LockWrite(); |
190 | 0 | X509_OBJECT *tmp = |
191 | 0 | X509_OBJECT_retrieve_by_subject(ctx->objs.get(), type, name); |
192 | 0 | ctx->objs_lock.UnlockWrite(); |
193 | |
|
194 | 0 | if (tmp == nullptr || type == X509_LU_CRL) { |
195 | 0 | for (const auto &lu : ctx->get_cert_methods) { |
196 | 0 | if (X509_LOOKUP_by_subject(lu.get(), type, name, &stmp)) { |
197 | 0 | tmp = &stmp; |
198 | 0 | break; |
199 | 0 | } |
200 | 0 | } |
201 | 0 | if (tmp == nullptr) { |
202 | 0 | return 0; |
203 | 0 | } |
204 | 0 | } |
205 | | |
206 | | // TODO(crbug.com/boringssl/685): This should call |
207 | | // `X509_OBJECT_free_contents`. |
208 | 0 | ret->type = tmp->type; |
209 | 0 | ret->data = tmp->data; |
210 | 0 | X509_OBJECT_up_ref_count(ret); |
211 | 0 | return 1; |
212 | 0 | } |
213 | | |
214 | 0 | static int x509_store_add(X509Store *ctx, const void *x, int is_crl) { |
215 | 0 | if (x == nullptr) { |
216 | 0 | return 0; |
217 | 0 | } |
218 | | |
219 | 0 | X509_OBJECT *const obj = X509_OBJECT_new(); |
220 | 0 | if (obj == nullptr) { |
221 | 0 | return 0; |
222 | 0 | } |
223 | | |
224 | 0 | if (is_crl) { |
225 | 0 | obj->type = X509_LU_CRL; |
226 | 0 | obj->data.crl = X509_CRL_dup_ref(static_cast<const X509_CRL *>(x)); |
227 | 0 | } else { |
228 | 0 | obj->type = X509_LU_X509; |
229 | 0 | obj->data.x509 = X509_dup_ref(static_cast<const X509 *>(x)); |
230 | 0 | } |
231 | |
|
232 | 0 | ctx->objs_lock.LockWrite(); |
233 | |
|
234 | 0 | int ret = 1; |
235 | 0 | int added = 0; |
236 | | // Duplicates are silently ignored |
237 | 0 | if (!X509_OBJECT_retrieve_match(ctx->objs.get(), obj)) { |
238 | 0 | ret = added = (sk_X509_OBJECT_push(ctx->objs.get(), obj) != 0); |
239 | 0 | } |
240 | |
|
241 | 0 | ctx->objs_lock.UnlockWrite(); |
242 | |
|
243 | 0 | if (!added) { |
244 | 0 | X509_OBJECT_free(obj); |
245 | 0 | } |
246 | |
|
247 | 0 | return ret; |
248 | 0 | } |
249 | | |
250 | 0 | int X509_STORE_add_cert(X509_STORE *ctx, const X509 *x) { |
251 | 0 | return x509_store_add(FromOpaque(ctx), x, /*is_crl=*/0); |
252 | 0 | } |
253 | | |
254 | 0 | int X509_STORE_add_crl(X509_STORE *ctx, const X509_CRL *x) { |
255 | 0 | return x509_store_add(FromOpaque(ctx), x, /*is_crl=*/1); |
256 | 0 | } |
257 | | |
258 | 0 | X509_OBJECT *X509_OBJECT_new() { return New<X509_OBJECT>(); } |
259 | | |
260 | 0 | void X509_OBJECT_free(X509_OBJECT *obj) { |
261 | 0 | if (obj == nullptr) { |
262 | 0 | return; |
263 | 0 | } |
264 | 0 | X509_OBJECT_free_contents(obj); |
265 | 0 | Delete(obj); |
266 | 0 | } |
267 | | |
268 | 0 | static int X509_OBJECT_up_ref_count(X509_OBJECT *a) { |
269 | 0 | switch (a->type) { |
270 | 0 | case X509_LU_X509: |
271 | 0 | X509_up_ref(a->data.x509); |
272 | 0 | break; |
273 | 0 | case X509_LU_CRL: |
274 | 0 | X509_CRL_up_ref(a->data.crl); |
275 | 0 | break; |
276 | 0 | } |
277 | 0 | return 1; |
278 | 0 | } |
279 | | |
280 | 0 | void X509_OBJECT_free_contents(X509_OBJECT *a) { |
281 | 0 | switch (a->type) { |
282 | 0 | case X509_LU_X509: |
283 | 0 | X509_free(a->data.x509); |
284 | 0 | break; |
285 | 0 | case X509_LU_CRL: |
286 | 0 | X509_CRL_free(a->data.crl); |
287 | 0 | break; |
288 | 0 | } |
289 | | |
290 | 0 | OPENSSL_memset(a, 0, sizeof(X509_OBJECT)); |
291 | 0 | } |
292 | | |
293 | 0 | int X509_OBJECT_get_type(const X509_OBJECT *a) { return a->type; } |
294 | | |
295 | 0 | X509 *X509_OBJECT_get0_X509(const X509_OBJECT *a) { |
296 | 0 | if (a == nullptr || a->type != X509_LU_X509) { |
297 | 0 | return nullptr; |
298 | 0 | } |
299 | 0 | return a->data.x509; |
300 | 0 | } |
301 | | |
302 | | static int x509_object_idx_cnt(STACK_OF(X509_OBJECT) *h, int type, |
303 | 0 | const X509_NAME *name, int *out_num_match) { |
304 | 0 | sk_X509_OBJECT_sort(h); |
305 | | |
306 | | // Find the first matching object. `sk_X509_OBJECT_find` would require |
307 | | // constructing an `X509` or `X509_CRL` object, so implement our own binary |
308 | | // search. |
309 | 0 | size_t start = 0, end = sk_X509_OBJECT_num(h); |
310 | 0 | while (end - start > 1) { |
311 | | // Bias `mid` towards `start`. The range has more than one element, so `mid` |
312 | | // is not the last element. |
313 | 0 | size_t mid = start + (end - start - 1) / 2; |
314 | 0 | assert(start <= mid && mid + 1 < end); |
315 | 0 | int r = x509_object_cmp_name(sk_X509_OBJECT_value(h, mid), type, name); |
316 | 0 | if (r < 0) { |
317 | 0 | start = mid + 1; // `mid` is too low. |
318 | 0 | } else if (r > 0) { |
319 | 0 | end = mid; // `mid` is too high. |
320 | 0 | } else { |
321 | | // `mid` matches, but we need to keep searching to find the first match. |
322 | 0 | end = mid + 1; |
323 | 0 | } |
324 | 0 | } |
325 | 0 | if (start == end || |
326 | 0 | x509_object_cmp_name(sk_X509_OBJECT_value(h, start), type, name) != 0) { |
327 | 0 | return -1; |
328 | 0 | } |
329 | | |
330 | 0 | if (out_num_match != nullptr) { |
331 | 0 | *out_num_match = 1; |
332 | 0 | for (size_t tidx = start + 1; tidx < sk_X509_OBJECT_num(h); tidx++) { |
333 | 0 | const X509_OBJECT *tobj = sk_X509_OBJECT_value(h, tidx); |
334 | 0 | if (x509_object_cmp_name(tobj, type, name) != 0) { |
335 | 0 | break; |
336 | 0 | } |
337 | 0 | (*out_num_match)++; |
338 | 0 | } |
339 | 0 | } |
340 | |
|
341 | 0 | assert(start <= INT_MAX); // `STACK_OF(T)` never stores more than `INT_MAX`. |
342 | 0 | return static_cast<int>(start); |
343 | 0 | } |
344 | | |
345 | | static int X509_OBJECT_idx_by_subject(STACK_OF(X509_OBJECT) *h, int type, |
346 | 0 | const X509_NAME *name) { |
347 | 0 | return x509_object_idx_cnt(h, type, name, nullptr); |
348 | 0 | } |
349 | | |
350 | | static X509_OBJECT *X509_OBJECT_retrieve_by_subject(STACK_OF(X509_OBJECT) *h, |
351 | | int type, |
352 | 0 | const X509_NAME *name) { |
353 | 0 | int idx; |
354 | 0 | idx = X509_OBJECT_idx_by_subject(h, type, name); |
355 | 0 | if (idx == -1) { |
356 | 0 | return nullptr; |
357 | 0 | } |
358 | 0 | return sk_X509_OBJECT_value(h, idx); |
359 | 0 | } |
360 | | |
361 | 0 | static X509_OBJECT *x509_object_dup(const X509_OBJECT *obj) { |
362 | 0 | X509_OBJECT *ret = X509_OBJECT_new(); |
363 | 0 | if (ret == nullptr) { |
364 | 0 | return nullptr; |
365 | 0 | } |
366 | 0 | ret->type = obj->type; |
367 | 0 | ret->data = obj->data; |
368 | 0 | X509_OBJECT_up_ref_count(ret); |
369 | 0 | return ret; |
370 | 0 | } |
371 | | |
372 | 0 | STACK_OF(X509_OBJECT) *X509_STORE_get1_objects(X509_STORE *store) { |
373 | 0 | auto *impl = FromOpaque(store); |
374 | 0 | MutexReadLock lock(&impl->objs_lock); |
375 | 0 | return sk_X509_OBJECT_deep_copy(impl->objs.get(), x509_object_dup, |
376 | 0 | X509_OBJECT_free); |
377 | 0 | } |
378 | | |
379 | 0 | STACK_OF(X509_OBJECT) *X509_STORE_get0_objects(X509_STORE *store) { |
380 | 0 | auto *impl = FromOpaque(store); |
381 | 0 | return impl->objs.get(); |
382 | 0 | } |
383 | | |
384 | | STACK_OF(X509) *X509_STORE_CTX_get1_certs(X509_STORE_CTX *ctx, |
385 | 0 | const X509_NAME *nm) { |
386 | 0 | int cnt; |
387 | 0 | STACK_OF(X509) *sk = sk_X509_new_null(); |
388 | 0 | if (sk == nullptr) { |
389 | 0 | return nullptr; |
390 | 0 | } |
391 | 0 | X509Store *store = FromOpaque(ctx->ctx); |
392 | 0 | store->objs_lock.LockWrite(); |
393 | 0 | int idx = x509_object_idx_cnt(store->objs.get(), X509_LU_X509, nm, &cnt); |
394 | 0 | if (idx < 0) { |
395 | | // Nothing found in cache: do lookup to possibly add new objects to |
396 | | // cache |
397 | 0 | X509_OBJECT xobj; |
398 | 0 | store->objs_lock.UnlockWrite(); |
399 | 0 | if (!X509_STORE_CTX_get_by_subject(ctx, X509_LU_X509, nm, &xobj)) { |
400 | 0 | sk_X509_free(sk); |
401 | 0 | return nullptr; |
402 | 0 | } |
403 | 0 | X509_OBJECT_free_contents(&xobj); |
404 | 0 | store->objs_lock.LockWrite(); |
405 | 0 | idx = x509_object_idx_cnt(store->objs.get(), X509_LU_X509, nm, &cnt); |
406 | 0 | if (idx < 0) { |
407 | 0 | store->objs_lock.UnlockWrite(); |
408 | 0 | sk_X509_free(sk); |
409 | 0 | return nullptr; |
410 | 0 | } |
411 | 0 | } |
412 | 0 | for (int i = 0; i < cnt; i++, idx++) { |
413 | 0 | X509_OBJECT *obj = sk_X509_OBJECT_value(store->objs.get(), idx); |
414 | 0 | X509 *x = obj->data.x509; |
415 | 0 | if (!sk_X509_push(sk, x)) { |
416 | 0 | store->objs_lock.UnlockWrite(); |
417 | 0 | sk_X509_pop_free(sk, X509_free); |
418 | 0 | return nullptr; |
419 | 0 | } |
420 | 0 | X509_up_ref(x); |
421 | 0 | } |
422 | 0 | store->objs_lock.UnlockWrite(); |
423 | 0 | return sk; |
424 | 0 | } |
425 | | |
426 | | STACK_OF(X509_CRL) *X509_STORE_CTX_get1_crls(X509_STORE_CTX *ctx, |
427 | 0 | const X509_NAME *nm) { |
428 | 0 | int cnt; |
429 | 0 | X509_OBJECT xobj; |
430 | 0 | STACK_OF(X509_CRL) *sk = sk_X509_CRL_new_null(); |
431 | 0 | if (sk == nullptr) { |
432 | 0 | return nullptr; |
433 | 0 | } |
434 | | |
435 | | // Always do lookup to possibly add new CRLs to cache. |
436 | 0 | if (!X509_STORE_CTX_get_by_subject(ctx, X509_LU_CRL, nm, &xobj)) { |
437 | 0 | sk_X509_CRL_free(sk); |
438 | 0 | return nullptr; |
439 | 0 | } |
440 | 0 | X509_OBJECT_free_contents(&xobj); |
441 | 0 | X509Store *store = FromOpaque(ctx->ctx); |
442 | 0 | store->objs_lock.LockWrite(); |
443 | 0 | int idx = x509_object_idx_cnt(store->objs.get(), X509_LU_CRL, nm, &cnt); |
444 | 0 | if (idx < 0) { |
445 | 0 | store->objs_lock.UnlockWrite(); |
446 | 0 | sk_X509_CRL_free(sk); |
447 | 0 | return nullptr; |
448 | 0 | } |
449 | | |
450 | 0 | for (int i = 0; i < cnt; i++, idx++) { |
451 | 0 | X509_OBJECT *obj = sk_X509_OBJECT_value(store->objs.get(), idx); |
452 | 0 | X509_CRL *x = obj->data.crl; |
453 | 0 | X509_CRL_up_ref(x); |
454 | 0 | if (!sk_X509_CRL_push(sk, x)) { |
455 | 0 | store->objs_lock.UnlockWrite(); |
456 | 0 | X509_CRL_free(x); |
457 | 0 | sk_X509_CRL_pop_free(sk, X509_CRL_free); |
458 | 0 | return nullptr; |
459 | 0 | } |
460 | 0 | } |
461 | 0 | store->objs_lock.UnlockWrite(); |
462 | 0 | return sk; |
463 | 0 | } |
464 | | |
465 | | static X509_OBJECT *X509_OBJECT_retrieve_match(STACK_OF(X509_OBJECT) *h, |
466 | 0 | X509_OBJECT *x) { |
467 | 0 | sk_X509_OBJECT_sort(h); |
468 | 0 | size_t idx; |
469 | 0 | if (!sk_X509_OBJECT_find(h, &idx, x)) { |
470 | 0 | return nullptr; |
471 | 0 | } |
472 | 0 | if ((x->type != X509_LU_X509) && (x->type != X509_LU_CRL)) { |
473 | 0 | return sk_X509_OBJECT_value(h, idx); |
474 | 0 | } |
475 | 0 | for (size_t i = idx; i < sk_X509_OBJECT_num(h); i++) { |
476 | 0 | X509_OBJECT *obj = sk_X509_OBJECT_value(h, i); |
477 | 0 | if (x509_object_cmp(obj, x)) { |
478 | 0 | return nullptr; |
479 | 0 | } |
480 | 0 | if (x->type == X509_LU_X509) { |
481 | 0 | if (!X509_cmp(obj->data.x509, x->data.x509)) { |
482 | 0 | return obj; |
483 | 0 | } |
484 | 0 | } else if (x->type == X509_LU_CRL) { |
485 | 0 | if (!X509_CRL_match(obj->data.crl, x->data.crl)) { |
486 | 0 | return obj; |
487 | 0 | } |
488 | 0 | } else { |
489 | 0 | return obj; |
490 | 0 | } |
491 | 0 | } |
492 | 0 | return nullptr; |
493 | 0 | } |
494 | | |
495 | | int X509_STORE_CTX_get1_issuer(X509 **out_issuer, X509_STORE_CTX *ctx, |
496 | 0 | const X509 *x) { |
497 | 0 | X509_OBJECT obj; |
498 | 0 | X509_NAME *xn = X509_get_issuer_name(x); |
499 | 0 | if (!X509_STORE_CTX_get_by_subject(ctx, X509_LU_X509, xn, &obj)) { |
500 | 0 | return 0; |
501 | 0 | } |
502 | | // If certificate matches all OK |
503 | 0 | if (x509_check_issued_with_callback(ctx, x, obj.data.x509)) { |
504 | 0 | *out_issuer = obj.data.x509; |
505 | 0 | return 1; |
506 | 0 | } |
507 | 0 | X509_OBJECT_free_contents(&obj); |
508 | | |
509 | | // Else find index of first cert accepted by |
510 | | // `x509_check_issued_with_callback`. |
511 | 0 | X509Store *store = FromOpaque(ctx->ctx); |
512 | 0 | MutexWriteLock lock(&store->objs_lock); |
513 | 0 | int idx = X509_OBJECT_idx_by_subject(store->objs.get(), X509_LU_X509, xn); |
514 | 0 | if (idx != -1) { // should be true as we've had at least one match |
515 | | // Look through all matching certs for suitable issuer |
516 | 0 | for (size_t i = idx; i < sk_X509_OBJECT_num(store->objs.get()); i++) { |
517 | 0 | X509_OBJECT *pobj = sk_X509_OBJECT_value(store->objs.get(), i); |
518 | | // See if we've run past the matches. |
519 | | // |
520 | | // This works because the objects are sorted by type, then subject |
521 | | // name, using `x509_object_cmp`. |
522 | 0 | if (pobj->type != X509_LU_X509) { |
523 | 0 | return 0; |
524 | 0 | } |
525 | 0 | if (X509_NAME_cmp(xn, X509_get_subject_name(pobj->data.x509))) { |
526 | 0 | return 0; |
527 | 0 | } |
528 | 0 | if (x509_check_issued_with_callback(ctx, x, pobj->data.x509)) { |
529 | 0 | *out_issuer = pobj->data.x509; |
530 | 0 | X509_OBJECT_up_ref_count(pobj); |
531 | 0 | return 1; |
532 | 0 | } |
533 | 0 | } |
534 | 0 | } |
535 | 0 | return 0; |
536 | 0 | } |
537 | | |
538 | 0 | int X509_STORE_set_flags(X509_STORE *ctx, unsigned long flags) { |
539 | 0 | auto *impl = FromOpaque(ctx); |
540 | 0 | return X509_VERIFY_PARAM_set_flags(impl->param.get(), flags); |
541 | 0 | } |
542 | | |
543 | 0 | int X509_STORE_set_depth(X509_STORE *ctx, int depth) { |
544 | 0 | auto *impl = FromOpaque(ctx); |
545 | 0 | X509_VERIFY_PARAM_set_depth(impl->param.get(), depth); |
546 | 0 | return 1; |
547 | 0 | } |
548 | | |
549 | 0 | int X509_STORE_set_purpose(X509_STORE *ctx, int purpose) { |
550 | 0 | auto *impl = FromOpaque(ctx); |
551 | 0 | return X509_VERIFY_PARAM_set_purpose(impl->param.get(), purpose); |
552 | 0 | } |
553 | | |
554 | 0 | int X509_STORE_set_trust(X509_STORE *ctx, int trust) { |
555 | 0 | auto *impl = FromOpaque(ctx); |
556 | 0 | return X509_VERIFY_PARAM_set_trust(impl->param.get(), trust); |
557 | 0 | } |
558 | | |
559 | 0 | int X509_STORE_set1_param(X509_STORE *ctx, const X509_VERIFY_PARAM *param) { |
560 | 0 | auto *impl = FromOpaque(ctx); |
561 | 0 | return X509_VERIFY_PARAM_set1(impl->param.get(), param); |
562 | 0 | } |
563 | | |
564 | 0 | X509_VERIFY_PARAM *X509_STORE_get0_param(X509_STORE *ctx) { |
565 | 0 | return FromOpaque(ctx)->param.get(); |
566 | 0 | } |
567 | | |
568 | | void X509_STORE_set_verify_cb(X509_STORE *ctx, |
569 | 0 | X509_STORE_CTX_verify_cb verify_cb) { |
570 | 0 | auto *impl = FromOpaque(ctx); |
571 | 0 | impl->verify_cb = verify_cb; |
572 | 0 | } |
573 | | |
574 | 0 | X509_STORE *X509_STORE_CTX_get0_store(const X509_STORE_CTX *ctx) { |
575 | 0 | return ctx->ctx; |
576 | 0 | } |