/src/openssl33/crypto/store/store_lib.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2016-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 <stdlib.h> |
11 | | #include <string.h> |
12 | | #include <assert.h> |
13 | | |
14 | | /* We need to use some STORE deprecated APIs */ |
15 | | #define OPENSSL_SUPPRESS_DEPRECATED |
16 | | |
17 | | #include "internal/e_os.h" |
18 | | |
19 | | #include <openssl/crypto.h> |
20 | | #include <openssl/err.h> |
21 | | #include <openssl/trace.h> |
22 | | #include <openssl/core_names.h> |
23 | | #include <openssl/provider.h> |
24 | | #include <openssl/param_build.h> |
25 | | #include <openssl/store.h> |
26 | | #include "internal/thread_once.h" |
27 | | #include "internal/cryptlib.h" |
28 | | #include "internal/provider.h" |
29 | | #include "internal/bio.h" |
30 | | #include "crypto/store.h" |
31 | | #include "store_local.h" |
32 | | |
33 | | static int ossl_store_close_it(OSSL_STORE_CTX *ctx); |
34 | | |
35 | | static int loader_set_params(OSSL_STORE_LOADER *loader, |
36 | | OSSL_STORE_LOADER_CTX *loader_ctx, |
37 | | const OSSL_PARAM params[], const char *propq) |
38 | 0 | { |
39 | 0 | if (params != NULL) { |
40 | 0 | if (!loader->p_set_ctx_params(loader_ctx, params)) |
41 | 0 | return 0; |
42 | 0 | } |
43 | | |
44 | 0 | if (propq != NULL) { |
45 | 0 | OSSL_PARAM propp[2]; |
46 | |
|
47 | 0 | if (OSSL_PARAM_locate_const(params, |
48 | 0 | OSSL_STORE_PARAM_PROPERTIES) != NULL) |
49 | | /* use the propq from params */ |
50 | 0 | return 1; |
51 | | |
52 | 0 | propp[0] = OSSL_PARAM_construct_utf8_string(OSSL_STORE_PARAM_PROPERTIES, |
53 | 0 | (char *)propq, 0); |
54 | 0 | propp[1] = OSSL_PARAM_construct_end(); |
55 | |
|
56 | 0 | if (!loader->p_set_ctx_params(loader_ctx, propp)) |
57 | 0 | return 0; |
58 | 0 | } |
59 | 0 | return 1; |
60 | 0 | } |
61 | | |
62 | | OSSL_STORE_CTX * |
63 | | OSSL_STORE_open_ex(const char *uri, OSSL_LIB_CTX *libctx, const char *propq, |
64 | | const UI_METHOD *ui_method, void *ui_data, |
65 | | const OSSL_PARAM params[], |
66 | | OSSL_STORE_post_process_info_fn post_process, |
67 | | void *post_process_data) |
68 | 0 | { |
69 | 0 | struct ossl_passphrase_data_st pwdata = { 0 }; |
70 | 0 | const OSSL_STORE_LOADER *loader = NULL; |
71 | 0 | OSSL_STORE_LOADER *fetched_loader = NULL; |
72 | 0 | OSSL_STORE_LOADER_CTX *loader_ctx = NULL; |
73 | 0 | OSSL_STORE_CTX *ctx = NULL; |
74 | 0 | char *propq_copy = NULL; |
75 | 0 | int no_loader_found = 1; |
76 | 0 | char scheme_copy[256], *p, *schemes[2], *scheme = NULL; |
77 | 0 | size_t schemes_n = 0; |
78 | 0 | size_t i; |
79 | |
|
80 | 0 | if (uri == NULL) { |
81 | 0 | ERR_raise(ERR_LIB_OSSL_STORE, CRYPTO_R_INVALID_NULL_ARGUMENT); |
82 | 0 | return 0; |
83 | 0 | } |
84 | | |
85 | | /* |
86 | | * Put the file scheme first. If the uri does represent an existing file, |
87 | | * possible device name and all, then it should be loaded. Only a failed |
88 | | * attempt at loading a local file should have us try something else. |
89 | | */ |
90 | 0 | schemes[schemes_n++] = "file"; |
91 | | |
92 | | /* |
93 | | * Now, check if we have something that looks like a scheme, and add it |
94 | | * as a second scheme. However, also check if there's an authority start |
95 | | * (://), because that will invalidate the previous file scheme. Also, |
96 | | * check that this isn't actually the file scheme, as there's no point |
97 | | * going through that one twice! |
98 | | */ |
99 | 0 | OPENSSL_strlcpy(scheme_copy, uri, sizeof(scheme_copy)); |
100 | 0 | if ((p = strchr(scheme_copy, ':')) != NULL) { |
101 | 0 | *p++ = '\0'; |
102 | 0 | if (OPENSSL_strcasecmp(scheme_copy, "file") != 0) { |
103 | 0 | if (HAS_PREFIX(p, "//")) |
104 | 0 | schemes_n--; /* Invalidate the file scheme */ |
105 | 0 | schemes[schemes_n++] = scheme_copy; |
106 | 0 | } |
107 | 0 | } |
108 | |
|
109 | 0 | ERR_set_mark(); |
110 | |
|
111 | 0 | if (ui_method != NULL |
112 | 0 | && (!ossl_pw_set_ui_method(&pwdata, ui_method, ui_data) |
113 | 0 | || !ossl_pw_enable_passphrase_caching(&pwdata))) { |
114 | 0 | ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_CRYPTO_LIB); |
115 | 0 | goto err; |
116 | 0 | } |
117 | | |
118 | | /* |
119 | | * Try each scheme until we find one that could open the URI. |
120 | | * |
121 | | * For each scheme, we look for the engine implementation first, and |
122 | | * failing that, we then try to fetch a provided implementation. |
123 | | * This is consistent with how we handle legacy / engine implementations |
124 | | * elsewhere. |
125 | | */ |
126 | 0 | for (i = 0; loader_ctx == NULL && i < schemes_n; i++) { |
127 | 0 | scheme = schemes[i]; |
128 | 0 | OSSL_TRACE1(STORE, "Looking up scheme %s\n", scheme); |
129 | 0 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
130 | 0 | ERR_set_mark(); |
131 | 0 | if ((loader = ossl_store_get0_loader_int(scheme)) != NULL) { |
132 | 0 | ERR_clear_last_mark(); |
133 | 0 | no_loader_found = 0; |
134 | 0 | if (loader->open_ex != NULL) |
135 | 0 | loader_ctx = loader->open_ex(loader, uri, libctx, propq, |
136 | 0 | ui_method, ui_data); |
137 | 0 | else |
138 | 0 | loader_ctx = loader->open(loader, uri, ui_method, ui_data); |
139 | 0 | } else { |
140 | 0 | ERR_pop_to_mark(); |
141 | 0 | } |
142 | 0 | #endif |
143 | 0 | if (loader == NULL |
144 | 0 | && (fetched_loader = |
145 | 0 | OSSL_STORE_LOADER_fetch(libctx, scheme, propq)) != NULL) { |
146 | 0 | const OSSL_PROVIDER *provider = |
147 | 0 | OSSL_STORE_LOADER_get0_provider(fetched_loader); |
148 | 0 | void *provctx = OSSL_PROVIDER_get0_provider_ctx(provider); |
149 | |
|
150 | 0 | no_loader_found = 0; |
151 | 0 | if (fetched_loader->p_open_ex != NULL) { |
152 | 0 | loader_ctx = |
153 | 0 | fetched_loader->p_open_ex(provctx, uri, params, |
154 | 0 | ossl_pw_passphrase_callback_dec, |
155 | 0 | &pwdata); |
156 | 0 | } else { |
157 | 0 | if (fetched_loader->p_open != NULL && |
158 | 0 | (loader_ctx = fetched_loader->p_open(provctx, uri)) != NULL && |
159 | 0 | !loader_set_params(fetched_loader, loader_ctx, |
160 | 0 | params, propq)) { |
161 | 0 | (void)fetched_loader->p_close(loader_ctx); |
162 | 0 | loader_ctx = NULL; |
163 | 0 | } |
164 | 0 | } |
165 | 0 | if (loader_ctx == NULL) { |
166 | 0 | OSSL_STORE_LOADER_free(fetched_loader); |
167 | 0 | fetched_loader = NULL; |
168 | 0 | } |
169 | 0 | loader = fetched_loader; |
170 | | |
171 | | /* Clear any internally cached passphrase */ |
172 | 0 | (void)ossl_pw_clear_passphrase_cache(&pwdata); |
173 | 0 | } |
174 | 0 | } |
175 | |
|
176 | 0 | if (no_loader_found) |
177 | | /* |
178 | | * It's assumed that ossl_store_get0_loader_int() and |
179 | | * OSSL_STORE_LOADER_fetch() report their own errors |
180 | | */ |
181 | 0 | goto err; |
182 | | |
183 | 0 | OSSL_TRACE1(STORE, "Found loader for scheme %s\n", scheme); |
184 | |
|
185 | 0 | if (loader_ctx == NULL) |
186 | | /* |
187 | | * It's assumed that the loader's open() method reports its own |
188 | | * errors |
189 | | */ |
190 | 0 | goto err; |
191 | | |
192 | 0 | OSSL_TRACE2(STORE, "Opened %s => %p\n", uri, (void *)loader_ctx); |
193 | |
|
194 | 0 | if ((propq != NULL && (propq_copy = OPENSSL_strdup(propq)) == NULL) |
195 | 0 | || (ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL) |
196 | 0 | goto err; |
197 | | |
198 | 0 | ctx->properties = propq_copy; |
199 | 0 | ctx->fetched_loader = fetched_loader; |
200 | 0 | ctx->loader = loader; |
201 | 0 | ctx->loader_ctx = loader_ctx; |
202 | 0 | ctx->post_process = post_process; |
203 | 0 | ctx->post_process_data = post_process_data; |
204 | 0 | ctx->pwdata = pwdata; |
205 | | |
206 | | /* |
207 | | * If the attempt to open with the 'file' scheme loader failed and the |
208 | | * other scheme loader succeeded, the failure to open with the 'file' |
209 | | * scheme loader leaves an error on the error stack. Let's remove it. |
210 | | */ |
211 | 0 | ERR_pop_to_mark(); |
212 | |
|
213 | 0 | return ctx; |
214 | | |
215 | 0 | err: |
216 | 0 | ERR_clear_last_mark(); |
217 | 0 | if (loader_ctx != NULL) { |
218 | | /* |
219 | | * Temporary structure so OSSL_STORE_close() can work even when |
220 | | * |ctx| couldn't be allocated properly |
221 | | */ |
222 | 0 | OSSL_STORE_CTX tmpctx = { NULL, }; |
223 | |
|
224 | 0 | tmpctx.fetched_loader = fetched_loader; |
225 | 0 | tmpctx.loader = loader; |
226 | 0 | tmpctx.loader_ctx = loader_ctx; |
227 | | |
228 | | /* |
229 | | * We ignore a returned error because we will return NULL anyway in |
230 | | * this case, so if something goes wrong when closing, that'll simply |
231 | | * just add another entry on the error stack. |
232 | | */ |
233 | 0 | (void)ossl_store_close_it(&tmpctx); |
234 | 0 | } |
235 | | /* Coverity false positive, the reference counting is confusing it */ |
236 | | /* coverity[pass_freed_arg] */ |
237 | 0 | OSSL_STORE_LOADER_free(fetched_loader); |
238 | 0 | OPENSSL_free(propq_copy); |
239 | 0 | OPENSSL_free(ctx); |
240 | 0 | return NULL; |
241 | 0 | } |
242 | | |
243 | | OSSL_STORE_CTX *OSSL_STORE_open(const char *uri, |
244 | | const UI_METHOD *ui_method, void *ui_data, |
245 | | OSSL_STORE_post_process_info_fn post_process, |
246 | | void *post_process_data) |
247 | 0 | { |
248 | 0 | return OSSL_STORE_open_ex(uri, NULL, NULL, ui_method, ui_data, NULL, |
249 | 0 | post_process, post_process_data); |
250 | 0 | } |
251 | | |
252 | | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
253 | | int OSSL_STORE_ctrl(OSSL_STORE_CTX *ctx, int cmd, ...) |
254 | 0 | { |
255 | 0 | va_list args; |
256 | 0 | int ret; |
257 | |
|
258 | 0 | va_start(args, cmd); |
259 | 0 | ret = OSSL_STORE_vctrl(ctx, cmd, args); |
260 | 0 | va_end(args); |
261 | |
|
262 | 0 | return ret; |
263 | 0 | } |
264 | | |
265 | | int OSSL_STORE_vctrl(OSSL_STORE_CTX *ctx, int cmd, va_list args) |
266 | 0 | { |
267 | 0 | if (ctx->fetched_loader != NULL) { |
268 | 0 | if (ctx->fetched_loader->p_set_ctx_params != NULL) { |
269 | 0 | OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; |
270 | |
|
271 | 0 | switch (cmd) { |
272 | 0 | case OSSL_STORE_C_USE_SECMEM: |
273 | 0 | { |
274 | 0 | int on = *(va_arg(args, int *)); |
275 | |
|
276 | 0 | params[0] = OSSL_PARAM_construct_int("use_secmem", &on); |
277 | 0 | } |
278 | 0 | break; |
279 | 0 | default: |
280 | 0 | break; |
281 | 0 | } |
282 | | |
283 | 0 | return ctx->fetched_loader->p_set_ctx_params(ctx->loader_ctx, |
284 | 0 | params); |
285 | 0 | } |
286 | 0 | } else if (ctx->loader->ctrl != NULL) { |
287 | 0 | return ctx->loader->ctrl(ctx->loader_ctx, cmd, args); |
288 | 0 | } |
289 | | |
290 | | /* |
291 | | * If the fetched loader doesn't have a set_ctx_params or a ctrl, it's as |
292 | | * if there was one that ignored our params, which usually returns 1. |
293 | | */ |
294 | 0 | return 1; |
295 | 0 | } |
296 | | #endif |
297 | | |
298 | | int OSSL_STORE_expect(OSSL_STORE_CTX *ctx, int expected_type) |
299 | 0 | { |
300 | 0 | int ret = 1; |
301 | |
|
302 | 0 | if (ctx == NULL |
303 | 0 | || expected_type < 0 || expected_type > OSSL_STORE_INFO_CRL) { |
304 | 0 | ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_PASSED_INVALID_ARGUMENT); |
305 | 0 | return 0; |
306 | 0 | } |
307 | 0 | if (ctx->loading) { |
308 | 0 | ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_LOADING_STARTED); |
309 | 0 | return 0; |
310 | 0 | } |
311 | | |
312 | 0 | ctx->expected_type = expected_type; |
313 | 0 | if (ctx->fetched_loader != NULL |
314 | 0 | && ctx->fetched_loader->p_set_ctx_params != NULL) { |
315 | 0 | OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; |
316 | |
|
317 | 0 | params[0] = |
318 | 0 | OSSL_PARAM_construct_int(OSSL_STORE_PARAM_EXPECT, &expected_type); |
319 | 0 | ret = ctx->fetched_loader->p_set_ctx_params(ctx->loader_ctx, params); |
320 | 0 | } |
321 | 0 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
322 | 0 | if (ctx->fetched_loader == NULL |
323 | 0 | && ctx->loader->expect != NULL) { |
324 | 0 | ret = ctx->loader->expect(ctx->loader_ctx, expected_type); |
325 | 0 | } |
326 | 0 | #endif |
327 | 0 | return ret; |
328 | 0 | } |
329 | | |
330 | | int OSSL_STORE_find(OSSL_STORE_CTX *ctx, const OSSL_STORE_SEARCH *search) |
331 | 0 | { |
332 | 0 | int ret = 1; |
333 | |
|
334 | 0 | if (ctx->loading) { |
335 | 0 | ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_LOADING_STARTED); |
336 | 0 | return 0; |
337 | 0 | } |
338 | 0 | if (search == NULL) { |
339 | 0 | ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_PASSED_NULL_PARAMETER); |
340 | 0 | return 0; |
341 | 0 | } |
342 | | |
343 | 0 | if (ctx->fetched_loader != NULL) { |
344 | 0 | OSSL_PARAM_BLD *bld; |
345 | 0 | OSSL_PARAM *params; |
346 | | /* OSSL_STORE_SEARCH_BY_NAME, OSSL_STORE_SEARCH_BY_ISSUER_SERIAL*/ |
347 | 0 | void *name_der = NULL; |
348 | 0 | int name_der_sz; |
349 | | /* OSSL_STORE_SEARCH_BY_ISSUER_SERIAL */ |
350 | 0 | BIGNUM *number = NULL; |
351 | |
|
352 | 0 | if (ctx->fetched_loader->p_set_ctx_params == NULL) { |
353 | 0 | ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_UNSUPPORTED_OPERATION); |
354 | 0 | return 0; |
355 | 0 | } |
356 | | |
357 | 0 | if ((bld = OSSL_PARAM_BLD_new()) == NULL) { |
358 | 0 | ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_CRYPTO_LIB); |
359 | 0 | return 0; |
360 | 0 | } |
361 | | |
362 | 0 | ret = 0; /* Assume the worst */ |
363 | |
|
364 | 0 | switch (search->search_type) { |
365 | 0 | case OSSL_STORE_SEARCH_BY_NAME: |
366 | 0 | if ((name_der_sz = i2d_X509_NAME(search->name, |
367 | 0 | (unsigned char **)&name_der)) > 0 |
368 | 0 | && OSSL_PARAM_BLD_push_octet_string(bld, |
369 | 0 | OSSL_STORE_PARAM_SUBJECT, |
370 | 0 | name_der, name_der_sz)) |
371 | 0 | ret = 1; |
372 | 0 | break; |
373 | 0 | case OSSL_STORE_SEARCH_BY_ISSUER_SERIAL: |
374 | 0 | if ((name_der_sz = i2d_X509_NAME(search->name, |
375 | 0 | (unsigned char **)&name_der)) > 0 |
376 | 0 | && (number = ASN1_INTEGER_to_BN(search->serial, NULL)) != NULL |
377 | 0 | && OSSL_PARAM_BLD_push_octet_string(bld, |
378 | 0 | OSSL_STORE_PARAM_ISSUER, |
379 | 0 | name_der, name_der_sz) |
380 | 0 | && OSSL_PARAM_BLD_push_BN(bld, OSSL_STORE_PARAM_SERIAL, |
381 | 0 | number)) |
382 | 0 | ret = 1; |
383 | 0 | break; |
384 | 0 | case OSSL_STORE_SEARCH_BY_KEY_FINGERPRINT: |
385 | 0 | if (OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_STORE_PARAM_DIGEST, |
386 | 0 | EVP_MD_get0_name(search->digest), |
387 | 0 | 0) |
388 | 0 | && OSSL_PARAM_BLD_push_octet_string(bld, |
389 | 0 | OSSL_STORE_PARAM_FINGERPRINT, |
390 | 0 | search->string, |
391 | 0 | search->stringlength)) |
392 | 0 | ret = 1; |
393 | 0 | break; |
394 | 0 | case OSSL_STORE_SEARCH_BY_ALIAS: |
395 | 0 | if (OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_STORE_PARAM_ALIAS, |
396 | 0 | (char *)search->string, |
397 | 0 | search->stringlength)) |
398 | 0 | ret = 1; |
399 | 0 | break; |
400 | 0 | } |
401 | 0 | if (ret) { |
402 | 0 | params = OSSL_PARAM_BLD_to_param(bld); |
403 | 0 | ret = ctx->fetched_loader->p_set_ctx_params(ctx->loader_ctx, |
404 | 0 | params); |
405 | 0 | OSSL_PARAM_free(params); |
406 | 0 | } |
407 | 0 | OSSL_PARAM_BLD_free(bld); |
408 | 0 | OPENSSL_free(name_der); |
409 | 0 | BN_free(number); |
410 | 0 | } else { |
411 | 0 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
412 | | /* legacy loader section */ |
413 | 0 | if (ctx->loader->find == NULL) { |
414 | 0 | ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_UNSUPPORTED_OPERATION); |
415 | 0 | return 0; |
416 | 0 | } |
417 | 0 | ret = ctx->loader->find(ctx->loader_ctx, search); |
418 | 0 | #endif |
419 | 0 | } |
420 | | |
421 | 0 | return ret; |
422 | 0 | } |
423 | | |
424 | | OSSL_STORE_INFO *OSSL_STORE_load(OSSL_STORE_CTX *ctx) |
425 | 0 | { |
426 | 0 | OSSL_STORE_INFO *v = NULL; |
427 | |
|
428 | 0 | ctx->loading = 1; |
429 | 0 | again: |
430 | 0 | if (OSSL_STORE_eof(ctx)) |
431 | 0 | return NULL; |
432 | | |
433 | 0 | if (ctx->loader != NULL) |
434 | 0 | OSSL_TRACE(STORE, "Loading next object\n"); |
435 | |
|
436 | 0 | if (ctx->cached_info != NULL) { |
437 | 0 | v = sk_OSSL_STORE_INFO_shift(ctx->cached_info); |
438 | 0 | } else { |
439 | 0 | if (ctx->fetched_loader != NULL) { |
440 | 0 | struct ossl_load_result_data_st load_data; |
441 | |
|
442 | 0 | load_data.v = NULL; |
443 | 0 | load_data.ctx = ctx; |
444 | 0 | ctx->error_flag = 0; |
445 | |
|
446 | 0 | if (!ctx->fetched_loader->p_load(ctx->loader_ctx, |
447 | 0 | ossl_store_handle_load_result, |
448 | 0 | &load_data, |
449 | 0 | ossl_pw_passphrase_callback_dec, |
450 | 0 | &ctx->pwdata)) { |
451 | 0 | ctx->error_flag = 1; |
452 | 0 | return NULL; |
453 | 0 | } |
454 | 0 | v = load_data.v; |
455 | 0 | } |
456 | 0 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
457 | 0 | if (ctx->fetched_loader == NULL) |
458 | 0 | v = ctx->loader->load(ctx->loader_ctx, |
459 | 0 | ctx->pwdata._.ui_method.ui_method, |
460 | 0 | ctx->pwdata._.ui_method.ui_method_data); |
461 | 0 | #endif |
462 | 0 | } |
463 | | |
464 | 0 | if (ctx->post_process != NULL && v != NULL) { |
465 | 0 | v = ctx->post_process(v, ctx->post_process_data); |
466 | | |
467 | | /* |
468 | | * By returning NULL, the callback decides that this object should |
469 | | * be ignored. |
470 | | */ |
471 | 0 | if (v == NULL) |
472 | 0 | goto again; |
473 | 0 | } |
474 | | |
475 | | /* Clear any internally cached passphrase */ |
476 | 0 | (void)ossl_pw_clear_passphrase_cache(&ctx->pwdata); |
477 | |
|
478 | 0 | if (v != NULL && ctx->expected_type != 0) { |
479 | 0 | int returned_type = OSSL_STORE_INFO_get_type(v); |
480 | |
|
481 | 0 | if (returned_type != OSSL_STORE_INFO_NAME && returned_type != 0) { |
482 | 0 | if (ctx->expected_type != returned_type) { |
483 | 0 | OSSL_STORE_INFO_free(v); |
484 | 0 | goto again; |
485 | 0 | } |
486 | 0 | } |
487 | 0 | } |
488 | | |
489 | 0 | if (v != NULL) |
490 | 0 | OSSL_TRACE1(STORE, "Got a %s\n", |
491 | 0 | OSSL_STORE_INFO_type_string(OSSL_STORE_INFO_get_type(v))); |
492 | |
|
493 | 0 | return v; |
494 | 0 | } |
495 | | |
496 | | int OSSL_STORE_delete(const char *uri, OSSL_LIB_CTX *libctx, const char *propq, |
497 | | const UI_METHOD *ui_method, void *ui_data, |
498 | | const OSSL_PARAM params[]) |
499 | 0 | { |
500 | 0 | OSSL_STORE_LOADER *fetched_loader = NULL; |
501 | 0 | char scheme[256], *p; |
502 | 0 | int res = 0; |
503 | 0 | struct ossl_passphrase_data_st pwdata = {0}; |
504 | |
|
505 | 0 | OPENSSL_strlcpy(scheme, uri, sizeof(scheme)); |
506 | 0 | if ((p = strchr(scheme, ':')) != NULL) |
507 | 0 | *p++ = '\0'; |
508 | 0 | else /* We don't work without explicit scheme */ |
509 | 0 | return 0; |
510 | | |
511 | 0 | if (ui_method != NULL |
512 | 0 | && (!ossl_pw_set_ui_method(&pwdata, ui_method, ui_data) |
513 | 0 | || !ossl_pw_enable_passphrase_caching(&pwdata))) { |
514 | 0 | ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_CRYPTO_LIB); |
515 | 0 | return 0; |
516 | 0 | } |
517 | | |
518 | 0 | OSSL_TRACE1(STORE, "Looking up scheme %s\n", scheme); |
519 | 0 | fetched_loader = OSSL_STORE_LOADER_fetch(libctx, scheme, propq); |
520 | |
|
521 | 0 | if (fetched_loader != NULL && fetched_loader->p_delete != NULL) { |
522 | 0 | const OSSL_PROVIDER *provider = |
523 | 0 | OSSL_STORE_LOADER_get0_provider(fetched_loader); |
524 | 0 | void *provctx = OSSL_PROVIDER_get0_provider_ctx(provider); |
525 | | |
526 | | /* |
527 | | * It's assumed that the loader's delete() method reports its own |
528 | | * errors |
529 | | */ |
530 | 0 | OSSL_TRACE1(STORE, "Performing URI delete %s\n", uri); |
531 | 0 | res = fetched_loader->p_delete(provctx, uri, params, |
532 | 0 | ossl_pw_passphrase_callback_dec, |
533 | 0 | &pwdata); |
534 | 0 | } |
535 | | /* Clear any internally cached passphrase */ |
536 | 0 | (void)ossl_pw_clear_passphrase_cache(&pwdata); |
537 | |
|
538 | 0 | OSSL_STORE_LOADER_free(fetched_loader); |
539 | |
|
540 | 0 | return res; |
541 | 0 | } |
542 | | |
543 | | int OSSL_STORE_error(OSSL_STORE_CTX *ctx) |
544 | 0 | { |
545 | 0 | int ret = 1; |
546 | |
|
547 | 0 | if (ctx->fetched_loader != NULL) |
548 | 0 | ret = ctx->error_flag; |
549 | 0 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
550 | 0 | if (ctx->fetched_loader == NULL) |
551 | 0 | ret = ctx->loader->error(ctx->loader_ctx); |
552 | 0 | #endif |
553 | 0 | return ret; |
554 | 0 | } |
555 | | |
556 | | int OSSL_STORE_eof(OSSL_STORE_CTX *ctx) |
557 | 0 | { |
558 | 0 | int ret = 0; |
559 | |
|
560 | 0 | if (ctx->cached_info != NULL |
561 | 0 | && sk_OSSL_STORE_INFO_num(ctx->cached_info) == 0) { |
562 | 0 | sk_OSSL_STORE_INFO_free(ctx->cached_info); |
563 | 0 | ctx->cached_info = NULL; |
564 | 0 | } |
565 | |
|
566 | 0 | if (ctx->cached_info == NULL) { |
567 | 0 | ret = 1; |
568 | 0 | if (ctx->fetched_loader != NULL) |
569 | 0 | ret = ctx->loader->p_eof(ctx->loader_ctx); |
570 | 0 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
571 | 0 | if (ctx->fetched_loader == NULL) |
572 | 0 | ret = ctx->loader->eof(ctx->loader_ctx); |
573 | 0 | #endif |
574 | 0 | } |
575 | 0 | return ret != 0; |
576 | 0 | } |
577 | | |
578 | | static int ossl_store_close_it(OSSL_STORE_CTX *ctx) |
579 | 0 | { |
580 | 0 | int ret = 0; |
581 | |
|
582 | 0 | if (ctx == NULL) |
583 | 0 | return 1; |
584 | 0 | OSSL_TRACE1(STORE, "Closing %p\n", (void *)ctx->loader_ctx); |
585 | |
|
586 | 0 | if (ctx->fetched_loader != NULL) |
587 | 0 | ret = ctx->loader->p_close(ctx->loader_ctx); |
588 | 0 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
589 | 0 | if (ctx->fetched_loader == NULL) |
590 | 0 | ret = ctx->loader->closefn(ctx->loader_ctx); |
591 | 0 | #endif |
592 | |
|
593 | 0 | sk_OSSL_STORE_INFO_pop_free(ctx->cached_info, OSSL_STORE_INFO_free); |
594 | 0 | OSSL_STORE_LOADER_free(ctx->fetched_loader); |
595 | 0 | OPENSSL_free(ctx->properties); |
596 | 0 | ossl_pw_clear_passphrase_data(&ctx->pwdata); |
597 | 0 | return ret; |
598 | 0 | } |
599 | | |
600 | | int OSSL_STORE_close(OSSL_STORE_CTX *ctx) |
601 | 0 | { |
602 | 0 | int ret = ossl_store_close_it(ctx); |
603 | |
|
604 | 0 | OPENSSL_free(ctx); |
605 | 0 | return ret; |
606 | 0 | } |
607 | | |
608 | | /* |
609 | | * Functions to generate OSSL_STORE_INFOs, one function for each type we |
610 | | * support having in them as well as a generic constructor. |
611 | | * |
612 | | * In all cases, ownership of the object is transferred to the OSSL_STORE_INFO |
613 | | * and will therefore be freed when the OSSL_STORE_INFO is freed. |
614 | | */ |
615 | | OSSL_STORE_INFO *OSSL_STORE_INFO_new(int type, void *data) |
616 | 0 | { |
617 | 0 | OSSL_STORE_INFO *info = OPENSSL_zalloc(sizeof(*info)); |
618 | |
|
619 | 0 | if (info == NULL) |
620 | 0 | return NULL; |
621 | | |
622 | 0 | info->type = type; |
623 | 0 | info->_.data = data; |
624 | 0 | return info; |
625 | 0 | } |
626 | | |
627 | | OSSL_STORE_INFO *OSSL_STORE_INFO_new_NAME(char *name) |
628 | 0 | { |
629 | 0 | OSSL_STORE_INFO *info = OSSL_STORE_INFO_new(OSSL_STORE_INFO_NAME, NULL); |
630 | |
|
631 | 0 | if (info == NULL) { |
632 | 0 | ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_OSSL_STORE_LIB); |
633 | 0 | return NULL; |
634 | 0 | } |
635 | | |
636 | 0 | info->_.name.name = name; |
637 | 0 | info->_.name.desc = NULL; |
638 | |
|
639 | 0 | return info; |
640 | 0 | } |
641 | | |
642 | | int OSSL_STORE_INFO_set0_NAME_description(OSSL_STORE_INFO *info, char *desc) |
643 | 0 | { |
644 | 0 | if (info->type != OSSL_STORE_INFO_NAME) { |
645 | 0 | ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_PASSED_INVALID_ARGUMENT); |
646 | 0 | return 0; |
647 | 0 | } |
648 | | |
649 | 0 | info->_.name.desc = desc; |
650 | |
|
651 | 0 | return 1; |
652 | 0 | } |
653 | | OSSL_STORE_INFO *OSSL_STORE_INFO_new_PARAMS(EVP_PKEY *params) |
654 | 0 | { |
655 | 0 | OSSL_STORE_INFO *info = OSSL_STORE_INFO_new(OSSL_STORE_INFO_PARAMS, params); |
656 | |
|
657 | 0 | if (info == NULL) |
658 | 0 | ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_OSSL_STORE_LIB); |
659 | 0 | return info; |
660 | 0 | } |
661 | | |
662 | | OSSL_STORE_INFO *OSSL_STORE_INFO_new_PUBKEY(EVP_PKEY *pkey) |
663 | 0 | { |
664 | 0 | OSSL_STORE_INFO *info = OSSL_STORE_INFO_new(OSSL_STORE_INFO_PUBKEY, pkey); |
665 | |
|
666 | 0 | if (info == NULL) |
667 | 0 | ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_OSSL_STORE_LIB); |
668 | 0 | return info; |
669 | 0 | } |
670 | | |
671 | | OSSL_STORE_INFO *OSSL_STORE_INFO_new_PKEY(EVP_PKEY *pkey) |
672 | 0 | { |
673 | 0 | OSSL_STORE_INFO *info = OSSL_STORE_INFO_new(OSSL_STORE_INFO_PKEY, pkey); |
674 | |
|
675 | 0 | if (info == NULL) |
676 | 0 | ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_OSSL_STORE_LIB); |
677 | 0 | return info; |
678 | 0 | } |
679 | | |
680 | | OSSL_STORE_INFO *OSSL_STORE_INFO_new_CERT(X509 *x509) |
681 | 0 | { |
682 | 0 | OSSL_STORE_INFO *info = OSSL_STORE_INFO_new(OSSL_STORE_INFO_CERT, x509); |
683 | |
|
684 | 0 | if (info == NULL) |
685 | 0 | ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_OSSL_STORE_LIB); |
686 | 0 | return info; |
687 | 0 | } |
688 | | |
689 | | OSSL_STORE_INFO *OSSL_STORE_INFO_new_CRL(X509_CRL *crl) |
690 | 0 | { |
691 | 0 | OSSL_STORE_INFO *info = OSSL_STORE_INFO_new(OSSL_STORE_INFO_CRL, crl); |
692 | |
|
693 | 0 | if (info == NULL) |
694 | 0 | ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_OSSL_STORE_LIB); |
695 | 0 | return info; |
696 | 0 | } |
697 | | |
698 | | /* |
699 | | * Functions to try to extract data from an OSSL_STORE_INFO. |
700 | | */ |
701 | | int OSSL_STORE_INFO_get_type(const OSSL_STORE_INFO *info) |
702 | 0 | { |
703 | 0 | return info->type; |
704 | 0 | } |
705 | | |
706 | | void *OSSL_STORE_INFO_get0_data(int type, const OSSL_STORE_INFO *info) |
707 | 0 | { |
708 | 0 | if (info->type == type) |
709 | 0 | return info->_.data; |
710 | 0 | return NULL; |
711 | 0 | } |
712 | | |
713 | | const char *OSSL_STORE_INFO_get0_NAME(const OSSL_STORE_INFO *info) |
714 | 0 | { |
715 | 0 | if (info->type == OSSL_STORE_INFO_NAME) |
716 | 0 | return info->_.name.name; |
717 | 0 | return NULL; |
718 | 0 | } |
719 | | |
720 | | char *OSSL_STORE_INFO_get1_NAME(const OSSL_STORE_INFO *info) |
721 | 0 | { |
722 | 0 | if (info->type == OSSL_STORE_INFO_NAME) |
723 | 0 | return OPENSSL_strdup(info->_.name.name); |
724 | 0 | ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_NOT_A_NAME); |
725 | 0 | return NULL; |
726 | 0 | } |
727 | | |
728 | | const char *OSSL_STORE_INFO_get0_NAME_description(const OSSL_STORE_INFO *info) |
729 | 0 | { |
730 | 0 | if (info->type == OSSL_STORE_INFO_NAME) |
731 | 0 | return info->_.name.desc; |
732 | 0 | return NULL; |
733 | 0 | } |
734 | | |
735 | | char *OSSL_STORE_INFO_get1_NAME_description(const OSSL_STORE_INFO *info) |
736 | 0 | { |
737 | 0 | if (info->type == OSSL_STORE_INFO_NAME) |
738 | 0 | return OPENSSL_strdup(info->_.name.desc ? info->_.name.desc : ""); |
739 | 0 | ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_NOT_A_NAME); |
740 | 0 | return NULL; |
741 | 0 | } |
742 | | |
743 | | EVP_PKEY *OSSL_STORE_INFO_get0_PARAMS(const OSSL_STORE_INFO *info) |
744 | 0 | { |
745 | 0 | if (info->type == OSSL_STORE_INFO_PARAMS) |
746 | 0 | return info->_.params; |
747 | 0 | return NULL; |
748 | 0 | } |
749 | | |
750 | | EVP_PKEY *OSSL_STORE_INFO_get1_PARAMS(const OSSL_STORE_INFO *info) |
751 | 0 | { |
752 | 0 | if (info->type == OSSL_STORE_INFO_PARAMS) { |
753 | 0 | EVP_PKEY_up_ref(info->_.params); |
754 | 0 | return info->_.params; |
755 | 0 | } |
756 | 0 | ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_NOT_PARAMETERS); |
757 | 0 | return NULL; |
758 | 0 | } |
759 | | |
760 | | EVP_PKEY *OSSL_STORE_INFO_get0_PUBKEY(const OSSL_STORE_INFO *info) |
761 | 0 | { |
762 | 0 | if (info->type == OSSL_STORE_INFO_PUBKEY) |
763 | 0 | return info->_.pubkey; |
764 | 0 | return NULL; |
765 | 0 | } |
766 | | |
767 | | EVP_PKEY *OSSL_STORE_INFO_get1_PUBKEY(const OSSL_STORE_INFO *info) |
768 | 0 | { |
769 | 0 | if (info->type == OSSL_STORE_INFO_PUBKEY) { |
770 | 0 | EVP_PKEY_up_ref(info->_.pubkey); |
771 | 0 | return info->_.pubkey; |
772 | 0 | } |
773 | 0 | ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_NOT_A_PUBLIC_KEY); |
774 | 0 | return NULL; |
775 | 0 | } |
776 | | |
777 | | EVP_PKEY *OSSL_STORE_INFO_get0_PKEY(const OSSL_STORE_INFO *info) |
778 | 0 | { |
779 | 0 | if (info->type == OSSL_STORE_INFO_PKEY) |
780 | 0 | return info->_.pkey; |
781 | 0 | return NULL; |
782 | 0 | } |
783 | | |
784 | | EVP_PKEY *OSSL_STORE_INFO_get1_PKEY(const OSSL_STORE_INFO *info) |
785 | 0 | { |
786 | 0 | if (info->type == OSSL_STORE_INFO_PKEY) { |
787 | 0 | EVP_PKEY_up_ref(info->_.pkey); |
788 | 0 | return info->_.pkey; |
789 | 0 | } |
790 | 0 | ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_NOT_A_PRIVATE_KEY); |
791 | 0 | return NULL; |
792 | 0 | } |
793 | | |
794 | | X509 *OSSL_STORE_INFO_get0_CERT(const OSSL_STORE_INFO *info) |
795 | 0 | { |
796 | 0 | if (info->type == OSSL_STORE_INFO_CERT) |
797 | 0 | return info->_.x509; |
798 | 0 | return NULL; |
799 | 0 | } |
800 | | |
801 | | X509 *OSSL_STORE_INFO_get1_CERT(const OSSL_STORE_INFO *info) |
802 | 0 | { |
803 | 0 | if (info->type == OSSL_STORE_INFO_CERT) { |
804 | 0 | X509_up_ref(info->_.x509); |
805 | 0 | return info->_.x509; |
806 | 0 | } |
807 | 0 | ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_NOT_A_CERTIFICATE); |
808 | 0 | return NULL; |
809 | 0 | } |
810 | | |
811 | | X509_CRL *OSSL_STORE_INFO_get0_CRL(const OSSL_STORE_INFO *info) |
812 | 0 | { |
813 | 0 | if (info->type == OSSL_STORE_INFO_CRL) |
814 | 0 | return info->_.crl; |
815 | 0 | return NULL; |
816 | 0 | } |
817 | | |
818 | | X509_CRL *OSSL_STORE_INFO_get1_CRL(const OSSL_STORE_INFO *info) |
819 | 0 | { |
820 | 0 | if (info->type == OSSL_STORE_INFO_CRL) { |
821 | 0 | X509_CRL_up_ref(info->_.crl); |
822 | 0 | return info->_.crl; |
823 | 0 | } |
824 | 0 | ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_NOT_A_CRL); |
825 | 0 | return NULL; |
826 | 0 | } |
827 | | |
828 | | /* |
829 | | * Free the OSSL_STORE_INFO |
830 | | */ |
831 | | void OSSL_STORE_INFO_free(OSSL_STORE_INFO *info) |
832 | 0 | { |
833 | 0 | if (info != NULL) { |
834 | 0 | switch (info->type) { |
835 | 0 | case OSSL_STORE_INFO_NAME: |
836 | 0 | OPENSSL_free(info->_.name.name); |
837 | 0 | OPENSSL_free(info->_.name.desc); |
838 | 0 | break; |
839 | 0 | case OSSL_STORE_INFO_PARAMS: |
840 | 0 | EVP_PKEY_free(info->_.params); |
841 | 0 | break; |
842 | 0 | case OSSL_STORE_INFO_PUBKEY: |
843 | 0 | EVP_PKEY_free(info->_.pubkey); |
844 | 0 | break; |
845 | 0 | case OSSL_STORE_INFO_PKEY: |
846 | 0 | EVP_PKEY_free(info->_.pkey); |
847 | 0 | break; |
848 | 0 | case OSSL_STORE_INFO_CERT: |
849 | 0 | X509_free(info->_.x509); |
850 | 0 | break; |
851 | 0 | case OSSL_STORE_INFO_CRL: |
852 | 0 | X509_CRL_free(info->_.crl); |
853 | 0 | break; |
854 | 0 | } |
855 | 0 | OPENSSL_free(info); |
856 | 0 | } |
857 | 0 | } |
858 | | |
859 | | int OSSL_STORE_supports_search(OSSL_STORE_CTX *ctx, int search_type) |
860 | 0 | { |
861 | 0 | int ret = 0; |
862 | |
|
863 | 0 | if (ctx->fetched_loader != NULL) { |
864 | 0 | void *provctx = |
865 | 0 | ossl_provider_ctx(OSSL_STORE_LOADER_get0_provider(ctx->fetched_loader)); |
866 | 0 | const OSSL_PARAM *params; |
867 | 0 | const OSSL_PARAM *p_subject = NULL; |
868 | 0 | const OSSL_PARAM *p_issuer = NULL; |
869 | 0 | const OSSL_PARAM *p_serial = NULL; |
870 | 0 | const OSSL_PARAM *p_fingerprint = NULL; |
871 | 0 | const OSSL_PARAM *p_alias = NULL; |
872 | |
|
873 | 0 | if (ctx->fetched_loader->p_settable_ctx_params == NULL) |
874 | 0 | return 0; |
875 | | |
876 | 0 | params = ctx->fetched_loader->p_settable_ctx_params(provctx); |
877 | 0 | p_subject = OSSL_PARAM_locate_const(params, OSSL_STORE_PARAM_SUBJECT); |
878 | 0 | p_issuer = OSSL_PARAM_locate_const(params, OSSL_STORE_PARAM_ISSUER); |
879 | 0 | p_serial = OSSL_PARAM_locate_const(params, OSSL_STORE_PARAM_SERIAL); |
880 | 0 | p_fingerprint = |
881 | 0 | OSSL_PARAM_locate_const(params, OSSL_STORE_PARAM_FINGERPRINT); |
882 | 0 | p_alias = OSSL_PARAM_locate_const(params, OSSL_STORE_PARAM_ALIAS); |
883 | |
|
884 | 0 | switch (search_type) { |
885 | 0 | case OSSL_STORE_SEARCH_BY_NAME: |
886 | 0 | ret = (p_subject != NULL); |
887 | 0 | break; |
888 | 0 | case OSSL_STORE_SEARCH_BY_ISSUER_SERIAL: |
889 | 0 | ret = (p_issuer != NULL && p_serial != NULL); |
890 | 0 | break; |
891 | 0 | case OSSL_STORE_SEARCH_BY_KEY_FINGERPRINT: |
892 | 0 | ret = (p_fingerprint != NULL); |
893 | 0 | break; |
894 | 0 | case OSSL_STORE_SEARCH_BY_ALIAS: |
895 | 0 | ret = (p_alias != NULL); |
896 | 0 | break; |
897 | 0 | } |
898 | 0 | } |
899 | 0 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
900 | 0 | if (ctx->fetched_loader == NULL) { |
901 | 0 | OSSL_STORE_SEARCH tmp_search; |
902 | |
|
903 | 0 | if (ctx->loader->find == NULL) |
904 | 0 | return 0; |
905 | 0 | tmp_search.search_type = search_type; |
906 | 0 | ret = ctx->loader->find(NULL, &tmp_search); |
907 | 0 | } |
908 | 0 | #endif |
909 | 0 | return ret; |
910 | 0 | } |
911 | | |
912 | | /* Search term constructors */ |
913 | | OSSL_STORE_SEARCH *OSSL_STORE_SEARCH_by_name(X509_NAME *name) |
914 | 0 | { |
915 | 0 | OSSL_STORE_SEARCH *search = OPENSSL_zalloc(sizeof(*search)); |
916 | |
|
917 | 0 | if (search == NULL) |
918 | 0 | return NULL; |
919 | | |
920 | 0 | search->search_type = OSSL_STORE_SEARCH_BY_NAME; |
921 | 0 | search->name = name; |
922 | 0 | return search; |
923 | 0 | } |
924 | | |
925 | | OSSL_STORE_SEARCH *OSSL_STORE_SEARCH_by_issuer_serial(X509_NAME *name, |
926 | | const ASN1_INTEGER *serial) |
927 | 0 | { |
928 | 0 | OSSL_STORE_SEARCH *search = OPENSSL_zalloc(sizeof(*search)); |
929 | |
|
930 | 0 | if (search == NULL) |
931 | 0 | return NULL; |
932 | | |
933 | 0 | search->search_type = OSSL_STORE_SEARCH_BY_ISSUER_SERIAL; |
934 | 0 | search->name = name; |
935 | 0 | search->serial = serial; |
936 | 0 | return search; |
937 | 0 | } |
938 | | |
939 | | OSSL_STORE_SEARCH *OSSL_STORE_SEARCH_by_key_fingerprint(const EVP_MD *digest, |
940 | | const unsigned char |
941 | | *bytes, size_t len) |
942 | 0 | { |
943 | 0 | OSSL_STORE_SEARCH *search = OPENSSL_zalloc(sizeof(*search)); |
944 | |
|
945 | 0 | if (search == NULL) |
946 | 0 | return NULL; |
947 | | |
948 | 0 | if (digest != NULL && len != (size_t)EVP_MD_get_size(digest)) { |
949 | 0 | ERR_raise_data(ERR_LIB_OSSL_STORE, |
950 | 0 | OSSL_STORE_R_FINGERPRINT_SIZE_DOES_NOT_MATCH_DIGEST, |
951 | 0 | "%s size is %d, fingerprint size is %zu", |
952 | 0 | EVP_MD_get0_name(digest), EVP_MD_get_size(digest), len); |
953 | 0 | OPENSSL_free(search); |
954 | 0 | return NULL; |
955 | 0 | } |
956 | | |
957 | 0 | search->search_type = OSSL_STORE_SEARCH_BY_KEY_FINGERPRINT; |
958 | 0 | search->digest = digest; |
959 | 0 | search->string = bytes; |
960 | 0 | search->stringlength = len; |
961 | 0 | return search; |
962 | 0 | } |
963 | | |
964 | | OSSL_STORE_SEARCH *OSSL_STORE_SEARCH_by_alias(const char *alias) |
965 | 0 | { |
966 | 0 | OSSL_STORE_SEARCH *search = OPENSSL_zalloc(sizeof(*search)); |
967 | |
|
968 | 0 | if (search == NULL) |
969 | 0 | return NULL; |
970 | | |
971 | 0 | search->search_type = OSSL_STORE_SEARCH_BY_ALIAS; |
972 | 0 | search->string = (const unsigned char *)alias; |
973 | 0 | search->stringlength = strlen(alias); |
974 | 0 | return search; |
975 | 0 | } |
976 | | |
977 | | /* Search term destructor */ |
978 | | void OSSL_STORE_SEARCH_free(OSSL_STORE_SEARCH *search) |
979 | 0 | { |
980 | 0 | OPENSSL_free(search); |
981 | 0 | } |
982 | | |
983 | | /* Search term accessors */ |
984 | | int OSSL_STORE_SEARCH_get_type(const OSSL_STORE_SEARCH *criterion) |
985 | 0 | { |
986 | 0 | return criterion->search_type; |
987 | 0 | } |
988 | | |
989 | | X509_NAME *OSSL_STORE_SEARCH_get0_name(const OSSL_STORE_SEARCH *criterion) |
990 | 0 | { |
991 | 0 | return criterion->name; |
992 | 0 | } |
993 | | |
994 | | const ASN1_INTEGER *OSSL_STORE_SEARCH_get0_serial(const OSSL_STORE_SEARCH |
995 | | *criterion) |
996 | 0 | { |
997 | 0 | return criterion->serial; |
998 | 0 | } |
999 | | |
1000 | | const unsigned char *OSSL_STORE_SEARCH_get0_bytes(const OSSL_STORE_SEARCH |
1001 | | *criterion, size_t *length) |
1002 | 0 | { |
1003 | 0 | *length = criterion->stringlength; |
1004 | 0 | return criterion->string; |
1005 | 0 | } |
1006 | | |
1007 | | const char *OSSL_STORE_SEARCH_get0_string(const OSSL_STORE_SEARCH *criterion) |
1008 | 0 | { |
1009 | 0 | return (const char *)criterion->string; |
1010 | 0 | } |
1011 | | |
1012 | | const EVP_MD *OSSL_STORE_SEARCH_get0_digest(const OSSL_STORE_SEARCH *criterion) |
1013 | 0 | { |
1014 | 0 | return criterion->digest; |
1015 | 0 | } |
1016 | | |
1017 | | OSSL_STORE_CTX *OSSL_STORE_attach(BIO *bp, const char *scheme, |
1018 | | OSSL_LIB_CTX *libctx, const char *propq, |
1019 | | const UI_METHOD *ui_method, void *ui_data, |
1020 | | const OSSL_PARAM params[], |
1021 | | OSSL_STORE_post_process_info_fn post_process, |
1022 | | void *post_process_data) |
1023 | 0 | { |
1024 | 0 | const OSSL_STORE_LOADER *loader = NULL; |
1025 | 0 | OSSL_STORE_LOADER *fetched_loader = NULL; |
1026 | 0 | OSSL_STORE_LOADER_CTX *loader_ctx = NULL; |
1027 | 0 | OSSL_STORE_CTX *ctx = NULL; |
1028 | |
|
1029 | 0 | if (scheme == NULL) |
1030 | 0 | scheme = "file"; |
1031 | |
|
1032 | 0 | OSSL_TRACE1(STORE, "Looking up scheme %s\n", scheme); |
1033 | 0 | ERR_set_mark(); |
1034 | 0 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
1035 | 0 | if ((loader = ossl_store_get0_loader_int(scheme)) != NULL) |
1036 | 0 | loader_ctx = loader->attach(loader, bp, libctx, propq, |
1037 | 0 | ui_method, ui_data); |
1038 | 0 | #endif |
1039 | 0 | if (loader == NULL |
1040 | 0 | && (fetched_loader = |
1041 | 0 | OSSL_STORE_LOADER_fetch(libctx, scheme, propq)) != NULL) { |
1042 | 0 | const OSSL_PROVIDER *provider = |
1043 | 0 | OSSL_STORE_LOADER_get0_provider(fetched_loader); |
1044 | 0 | void *provctx = OSSL_PROVIDER_get0_provider_ctx(provider); |
1045 | 0 | OSSL_CORE_BIO *cbio = ossl_core_bio_new_from_bio(bp); |
1046 | |
|
1047 | 0 | if (cbio == NULL |
1048 | 0 | || fetched_loader->p_attach == NULL |
1049 | 0 | || (loader_ctx = fetched_loader->p_attach(provctx, cbio)) == NULL) { |
1050 | 0 | OSSL_STORE_LOADER_free(fetched_loader); |
1051 | 0 | fetched_loader = NULL; |
1052 | 0 | } else if (!loader_set_params(fetched_loader, loader_ctx, |
1053 | 0 | params, propq)) { |
1054 | 0 | (void)fetched_loader->p_close(loader_ctx); |
1055 | 0 | OSSL_STORE_LOADER_free(fetched_loader); |
1056 | 0 | fetched_loader = NULL; |
1057 | 0 | } |
1058 | 0 | loader = fetched_loader; |
1059 | 0 | ossl_core_bio_free(cbio); |
1060 | 0 | } |
1061 | |
|
1062 | 0 | if (loader_ctx == NULL) { |
1063 | 0 | ERR_clear_last_mark(); |
1064 | 0 | return NULL; |
1065 | 0 | } |
1066 | | |
1067 | 0 | if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL) { |
1068 | 0 | ERR_clear_last_mark(); |
1069 | 0 | return NULL; |
1070 | 0 | } |
1071 | | |
1072 | 0 | if (ui_method != NULL |
1073 | 0 | && !ossl_pw_set_ui_method(&ctx->pwdata, ui_method, ui_data)) { |
1074 | 0 | ERR_clear_last_mark(); |
1075 | 0 | OPENSSL_free(ctx); |
1076 | 0 | return NULL; |
1077 | 0 | } |
1078 | | |
1079 | 0 | ctx->fetched_loader = fetched_loader; |
1080 | 0 | ctx->loader = loader; |
1081 | 0 | ctx->loader_ctx = loader_ctx; |
1082 | 0 | ctx->post_process = post_process; |
1083 | 0 | ctx->post_process_data = post_process_data; |
1084 | | |
1085 | | /* |
1086 | | * ossl_store_get0_loader_int will raise an error if the loader for |
1087 | | * the scheme cannot be retrieved. But if a loader was successfully |
1088 | | * fetched then we remove this error from the error stack. |
1089 | | */ |
1090 | 0 | ERR_pop_to_mark(); |
1091 | |
|
1092 | 0 | return ctx; |
1093 | 0 | } |