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