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