/src/openssl/crypto/x509/by_store.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2018-2025 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | #include <openssl/safestack.h> |
11 | | #include <openssl/store.h> |
12 | | #include "internal/cryptlib.h" |
13 | | #include "crypto/x509.h" |
14 | | #include "x509_local.h" |
15 | | |
16 | | typedef struct cached_store_st { |
17 | | char *uri; |
18 | | OSSL_LIB_CTX *libctx; |
19 | | char *propq; |
20 | | OSSL_STORE_CTX *ctx; |
21 | | } CACHED_STORE; |
22 | | |
23 | | DEFINE_STACK_OF(CACHED_STORE) |
24 | | |
25 | | /* Generic object loader, given expected type and criterion */ |
26 | | static int cache_objects(X509_LOOKUP *lctx, CACHED_STORE *store, |
27 | | const OSSL_STORE_SEARCH *criterion, int depth) |
28 | 0 | { |
29 | 0 | int ok = 0; |
30 | 0 | OSSL_STORE_CTX *ctx = store->ctx; |
31 | 0 | X509_STORE *xstore = X509_LOOKUP_get_store(lctx); |
32 | |
|
33 | 0 | if (ctx == NULL |
34 | 0 | && (ctx = OSSL_STORE_open_ex(store->uri, store->libctx, store->propq, |
35 | 0 | NULL, NULL, NULL, NULL, NULL)) == NULL) |
36 | 0 | return 0; |
37 | 0 | store->ctx = ctx; |
38 | | |
39 | | /* |
40 | | * We try to set the criterion, but don't care if it was valid or not. |
41 | | * For an OSSL_STORE, it merely serves as an optimization, the expectation |
42 | | * being that if the criterion couldn't be used, we will get *everything* |
43 | | * from the container that the URI represents rather than the subset that |
44 | | * the criterion indicates, so the biggest harm is that we cache more |
45 | | * objects certs and CRLs than we may expect, but that's ok. |
46 | | * |
47 | | * Specifically for OpenSSL's own file: scheme, the only workable |
48 | | * criterion is the BY_NAME one, which it can only apply on directories, |
49 | | * but it's possible that the URI is a single file rather than a directory, |
50 | | * and in that case, the BY_NAME criterion is pointless. |
51 | | * |
52 | | * We could very simply not apply any criterion at all here, and just let |
53 | | * the code that selects certs and CRLs from the cached objects do its job, |
54 | | * but it's a nice optimization when it can be applied (such as on an |
55 | | * actual directory with a thousand CA certs). |
56 | | */ |
57 | 0 | if (criterion != NULL) { |
58 | 0 | ERR_set_mark(); |
59 | 0 | OSSL_STORE_find(ctx, criterion); |
60 | 0 | ERR_pop_to_mark(); |
61 | 0 | } |
62 | |
|
63 | 0 | for (;;) { |
64 | 0 | OSSL_STORE_INFO *info = OSSL_STORE_load(ctx); |
65 | 0 | int infotype; |
66 | | |
67 | | /* NULL means error or "end of file". Either way, we break. */ |
68 | 0 | if (info == NULL) |
69 | 0 | break; |
70 | | |
71 | 0 | infotype = OSSL_STORE_INFO_get_type(info); |
72 | 0 | ok = 0; |
73 | |
|
74 | 0 | if (infotype == OSSL_STORE_INFO_NAME) { |
75 | | /* |
76 | | * This is an entry in the "directory" represented by the current |
77 | | * uri. if |depth| allows, dive into it. |
78 | | */ |
79 | 0 | if (depth > 0) { |
80 | 0 | CACHED_STORE substore; |
81 | |
|
82 | 0 | substore.uri = (char *)OSSL_STORE_INFO_get0_NAME(info); |
83 | 0 | substore.libctx = store->libctx; |
84 | 0 | substore.propq = store->propq; |
85 | 0 | substore.ctx = NULL; |
86 | 0 | ok = cache_objects(lctx, &substore, criterion, depth - 1); |
87 | 0 | } |
88 | 0 | } else { |
89 | | /* |
90 | | * We know that X509_STORE_add_{cert|crl} increments the object's |
91 | | * refcount, so we can safely use OSSL_STORE_INFO_get0_{cert,crl} |
92 | | * to get them. |
93 | | */ |
94 | 0 | switch (infotype) { |
95 | 0 | case OSSL_STORE_INFO_CERT: |
96 | 0 | ok = X509_STORE_add_cert(xstore, |
97 | 0 | OSSL_STORE_INFO_get0_CERT(info)); |
98 | 0 | break; |
99 | 0 | case OSSL_STORE_INFO_CRL: |
100 | 0 | ok = X509_STORE_add_crl(xstore, |
101 | 0 | OSSL_STORE_INFO_get0_CRL(info)); |
102 | 0 | break; |
103 | 0 | } |
104 | 0 | } |
105 | | |
106 | 0 | OSSL_STORE_INFO_free(info); |
107 | 0 | if (!ok) |
108 | 0 | break; |
109 | 0 | } |
110 | 0 | OSSL_STORE_close(ctx); |
111 | 0 | store->ctx = NULL; |
112 | |
|
113 | 0 | return ok; |
114 | 0 | } |
115 | | |
116 | | |
117 | | static void free_store(CACHED_STORE *store) |
118 | 0 | { |
119 | 0 | if (store != NULL) { |
120 | 0 | OSSL_STORE_close(store->ctx); |
121 | 0 | OPENSSL_free(store->uri); |
122 | 0 | OPENSSL_free(store->propq); |
123 | 0 | OPENSSL_free(store); |
124 | 0 | } |
125 | 0 | } |
126 | | |
127 | | static void by_store_free(X509_LOOKUP *ctx) |
128 | 0 | { |
129 | 0 | STACK_OF(CACHED_STORE) *stores = X509_LOOKUP_get_method_data(ctx); |
130 | 0 | sk_CACHED_STORE_pop_free(stores, free_store); |
131 | 0 | } |
132 | | |
133 | | static int by_store_ctrl_ex(X509_LOOKUP *ctx, int cmd, const char *argp, |
134 | | long argl, char **retp, OSSL_LIB_CTX *libctx, |
135 | | const char *propq) |
136 | 0 | { |
137 | 0 | switch (cmd) { |
138 | 0 | case X509_L_ADD_STORE: |
139 | 0 | if (argp != NULL) { |
140 | 0 | STACK_OF(CACHED_STORE) *stores = X509_LOOKUP_get_method_data(ctx); |
141 | 0 | CACHED_STORE *store = OPENSSL_zalloc(sizeof(*store)); |
142 | |
|
143 | 0 | if (store == NULL) { |
144 | 0 | return 0; |
145 | 0 | } |
146 | | |
147 | 0 | store->uri = OPENSSL_strdup(argp); |
148 | 0 | store->libctx = libctx; |
149 | 0 | if (propq != NULL) |
150 | 0 | store->propq = OPENSSL_strdup(propq); |
151 | 0 | store->ctx = OSSL_STORE_open_ex(argp, libctx, propq, NULL, NULL, |
152 | 0 | NULL, NULL, NULL); |
153 | 0 | if (store->ctx == NULL |
154 | 0 | || (propq != NULL && store->propq == NULL) |
155 | 0 | || store->uri == NULL) { |
156 | 0 | free_store(store); |
157 | 0 | return 0; |
158 | 0 | } |
159 | | |
160 | 0 | if (stores == NULL) { |
161 | 0 | stores = sk_CACHED_STORE_new_null(); |
162 | 0 | if (stores != NULL) |
163 | 0 | X509_LOOKUP_set_method_data(ctx, stores); |
164 | 0 | } |
165 | 0 | if (stores == NULL || sk_CACHED_STORE_push(stores, store) <= 0) { |
166 | 0 | free_store(store); |
167 | 0 | return 0; |
168 | 0 | } |
169 | 0 | return 1; |
170 | 0 | } |
171 | | /* NOP if no URI is given. */ |
172 | 0 | return 1; |
173 | 0 | case X509_L_LOAD_STORE: { |
174 | | /* This is a shortcut for quick loading of specific containers */ |
175 | 0 | CACHED_STORE store; |
176 | |
|
177 | 0 | store.uri = (char *)argp; |
178 | 0 | store.libctx = libctx; |
179 | 0 | store.propq = (char *)propq; |
180 | 0 | store.ctx = NULL; |
181 | 0 | return cache_objects(ctx, &store, NULL, 0); |
182 | 0 | } |
183 | 0 | default: |
184 | | /* Unsupported command */ |
185 | 0 | return 0; |
186 | 0 | } |
187 | 0 | } |
188 | | |
189 | | static int by_store_ctrl(X509_LOOKUP *ctx, int cmd, |
190 | | const char *argp, long argl, char **retp) |
191 | 0 | { |
192 | 0 | return by_store_ctrl_ex(ctx, cmd, argp, argl, retp, NULL, NULL); |
193 | 0 | } |
194 | | |
195 | | static int by_store(X509_LOOKUP *ctx, X509_LOOKUP_TYPE type, |
196 | | const OSSL_STORE_SEARCH *criterion, X509_OBJECT *ret) |
197 | 0 | { |
198 | 0 | STACK_OF(CACHED_STORE) *stores = X509_LOOKUP_get_method_data(ctx); |
199 | 0 | int i; |
200 | 0 | int ok = 0; |
201 | |
|
202 | 0 | for (i = 0; i < sk_CACHED_STORE_num(stores); i++) { |
203 | 0 | ok = cache_objects(ctx, sk_CACHED_STORE_value(stores, i), criterion, |
204 | 0 | 1 /* depth */); |
205 | |
|
206 | 0 | if (ok) |
207 | 0 | break; |
208 | 0 | } |
209 | 0 | return ok; |
210 | 0 | } |
211 | | |
212 | | static int by_store_subject(X509_LOOKUP *ctx, X509_LOOKUP_TYPE type, |
213 | | const X509_NAME *name, X509_OBJECT *ret) |
214 | 0 | { |
215 | 0 | OSSL_STORE_SEARCH *criterion = |
216 | 0 | OSSL_STORE_SEARCH_by_name((X509_NAME *)name); /* won't modify it */ |
217 | 0 | int ok = by_store(ctx, type, criterion, ret); |
218 | 0 | STACK_OF(X509_OBJECT) *store_objects = |
219 | 0 | X509_STORE_get0_objects(X509_LOOKUP_get_store(ctx)); |
220 | 0 | X509_OBJECT *tmp = NULL; |
221 | |
|
222 | 0 | OSSL_STORE_SEARCH_free(criterion); |
223 | |
|
224 | 0 | if (ok) |
225 | 0 | tmp = X509_OBJECT_retrieve_by_subject(store_objects, type, name); |
226 | |
|
227 | 0 | ok = 0; |
228 | 0 | if (tmp != NULL) { |
229 | | /* |
230 | | * This could also be done like this: |
231 | | * |
232 | | * if (tmp != NULL) { |
233 | | * *ret = *tmp; |
234 | | * ok = 1; |
235 | | * } |
236 | | * |
237 | | * However, we want to exercise the documented API to the max, so |
238 | | * we do it the hard way. |
239 | | * |
240 | | * To be noted is that X509_OBJECT_set1_* increment the refcount, |
241 | | * but so does X509_STORE_CTX_get_by_subject upon return of this |
242 | | * function, so we must ensure the refcount is decremented |
243 | | * before we return, or we will get a refcount leak. We cannot do |
244 | | * this with X509_OBJECT_free(), though, as that will free a bit |
245 | | * too much. |
246 | | */ |
247 | 0 | switch (type) { |
248 | 0 | case X509_LU_X509: |
249 | 0 | ok = X509_OBJECT_set1_X509(ret, tmp->data.x509); |
250 | 0 | if (ok) |
251 | 0 | X509_free(tmp->data.x509); |
252 | 0 | break; |
253 | 0 | case X509_LU_CRL: |
254 | 0 | ok = X509_OBJECT_set1_X509_CRL(ret, tmp->data.crl); |
255 | 0 | if (ok) |
256 | 0 | X509_CRL_free(tmp->data.crl); |
257 | 0 | break; |
258 | 0 | case X509_LU_NONE: |
259 | 0 | break; |
260 | 0 | } |
261 | 0 | } |
262 | 0 | return ok; |
263 | 0 | } |
264 | | |
265 | | /* |
266 | | * We lack the implementations for get_by_issuer_serial, get_by_fingerprint |
267 | | * and get_by_alias. There's simply not enough support in the X509_LOOKUP |
268 | | * or X509_STORE APIs. |
269 | | */ |
270 | | |
271 | | static X509_LOOKUP_METHOD x509_store_lookup = { |
272 | | "Load certs from STORE URIs", |
273 | | NULL, /* new_item */ |
274 | | by_store_free, /* free */ |
275 | | NULL, /* init */ |
276 | | NULL, /* shutdown */ |
277 | | by_store_ctrl, /* ctrl */ |
278 | | by_store_subject, /* get_by_subject */ |
279 | | NULL, /* get_by_issuer_serial */ |
280 | | NULL, /* get_by_fingerprint */ |
281 | | NULL, /* get_by_alias */ |
282 | | NULL, /* get_by_subject_ex */ |
283 | | by_store_ctrl_ex |
284 | | }; |
285 | | |
286 | | X509_LOOKUP_METHOD *X509_LOOKUP_store(void) |
287 | 0 | { |
288 | 0 | return &x509_store_lookup; |
289 | 0 | } |