2 * Copyright 2016-2022 The OpenSSL Project Authors. All Rights Reserved.
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
14 /* We need to use some STORE deprecated APIs */
15 #define OPENSSL_SUPPRESS_DEPRECATED
17 #include "internal/e_os.h"
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"
33 static int ossl_store_close_it(OSSL_STORE_CTX *ctx);
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)
40 if (!loader->p_set_ctx_params(loader_ctx, params))
47 if (OSSL_PARAM_locate_const(params,
48 OSSL_STORE_PARAM_PROPERTIES) != NULL)
49 /* use the propq from params */
52 propp[0] = OSSL_PARAM_construct_utf8_string(OSSL_STORE_PARAM_PROPERTIES,
54 propp[1] = OSSL_PARAM_construct_end();
56 if (!loader->p_set_ctx_params(loader_ctx, propp))
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)
69 const OSSL_STORE_LOADER *loader = NULL;
70 OSSL_STORE_LOADER *fetched_loader = NULL;
71 OSSL_STORE_LOADER_CTX *loader_ctx = NULL;
72 OSSL_STORE_CTX *ctx = NULL;
73 char *propq_copy = NULL;
74 int no_loader_found = 1;
75 char scheme_copy[256], *p, *schemes[2], *scheme = NULL;
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.
84 schemes[schemes_n++] = "file";
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!
93 OPENSSL_strlcpy(scheme_copy, uri, sizeof(scheme_copy));
94 if ((p = strchr(scheme_copy, ':')) != NULL) {
96 if (OPENSSL_strcasecmp(scheme_copy, "file") != 0) {
97 if (HAS_PREFIX(p, "//"))
98 schemes_n--; /* Invalidate the file scheme */
99 schemes[schemes_n++] = scheme_copy;
106 * Try each scheme until we find one that could open the URI.
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
113 for (i = 0; loader_ctx == NULL && i < schemes_n; i++) {
115 OSSL_TRACE1(STORE, "Looking up scheme %s\n", scheme);
116 #ifndef OPENSSL_NO_DEPRECATED_3_0
118 if ((loader = ossl_store_get0_loader_int(scheme)) != NULL) {
119 ERR_clear_last_mark();
121 if (loader->open_ex != NULL)
122 loader_ctx = loader->open_ex(loader, uri, libctx, propq,
125 loader_ctx = loader->open(loader, uri, ui_method, ui_data);
132 OSSL_STORE_LOADER_fetch(libctx, scheme, propq)) != NULL) {
133 const OSSL_PROVIDER *provider =
134 OSSL_STORE_LOADER_get0_provider(fetched_loader);
135 void *provctx = OSSL_PROVIDER_get0_provider_ctx(provider);
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;
142 } else if (!loader_set_params(fetched_loader, loader_ctx,
144 (void)fetched_loader->p_close(loader_ctx);
145 OSSL_STORE_LOADER_free(fetched_loader);
146 fetched_loader = NULL;
148 loader = fetched_loader;
154 * It's assumed that ossl_store_get0_loader_int() and
155 * OSSL_STORE_LOADER_fetch() report their own errors
159 OSSL_TRACE1(STORE, "Found loader for scheme %s\n", scheme);
161 if (loader_ctx == NULL)
163 * It's assumed that the loader's open() method reports its own
168 OSSL_TRACE2(STORE, "Opened %s => %p\n", uri, (void *)loader_ctx);
170 if ((propq != NULL && (propq_copy = OPENSSL_strdup(propq)) == NULL)
171 || (ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL) {
172 ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_MALLOC_FAILURE);
176 if (ui_method != NULL
177 && (!ossl_pw_set_ui_method(&ctx->pwdata, ui_method, ui_data)
178 || !ossl_pw_enable_passphrase_caching(&ctx->pwdata))) {
179 ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_CRYPTO_LIB);
182 ctx->properties = propq_copy;
183 ctx->fetched_loader = fetched_loader;
184 ctx->loader = loader;
185 ctx->loader_ctx = loader_ctx;
186 ctx->post_process = post_process;
187 ctx->post_process_data = post_process_data;
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.
199 ERR_clear_last_mark();
200 if (loader_ctx != NULL) {
202 * Temporary structure so OSSL_STORE_close() can work even when
203 * |ctx| couldn't be allocated properly
205 OSSL_STORE_CTX tmpctx = { NULL, };
207 tmpctx.fetched_loader = fetched_loader;
208 tmpctx.loader = loader;
209 tmpctx.loader_ctx = loader_ctx;
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.
216 (void)ossl_store_close_it(&tmpctx);
218 OSSL_STORE_LOADER_free(fetched_loader);
219 OPENSSL_free(propq_copy);
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)
229 return OSSL_STORE_open_ex(uri, NULL, NULL, ui_method, ui_data, NULL,
230 post_process, post_process_data);
233 #ifndef OPENSSL_NO_DEPRECATED_3_0
234 int OSSL_STORE_ctrl(OSSL_STORE_CTX *ctx, int cmd, ...)
240 ret = OSSL_STORE_vctrl(ctx, cmd, args);
246 int OSSL_STORE_vctrl(OSSL_STORE_CTX *ctx, int cmd, va_list args)
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 };
253 case OSSL_STORE_C_USE_SECMEM:
255 int on = *(va_arg(args, int *));
257 params[0] = OSSL_PARAM_construct_int("use_secmem", &on);
264 return ctx->fetched_loader->p_set_ctx_params(ctx->loader_ctx,
267 } else if (ctx->loader->ctrl != NULL) {
268 return ctx->loader->ctrl(ctx->loader_ctx, cmd, args);
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.
279 int OSSL_STORE_expect(OSSL_STORE_CTX *ctx, int expected_type)
284 || expected_type < 0 || expected_type > OSSL_STORE_INFO_CRL) {
285 ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_PASSED_INVALID_ARGUMENT);
289 ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_LOADING_STARTED);
293 ctx->expected_type = expected_type;
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 };
299 OSSL_PARAM_construct_int(OSSL_STORE_PARAM_EXPECT, &expected_type);
300 ret = ctx->fetched_loader->p_set_ctx_params(ctx->loader_ctx, params);
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);
311 int OSSL_STORE_find(OSSL_STORE_CTX *ctx, const OSSL_STORE_SEARCH *search)
316 ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_LOADING_STARTED);
319 if (search == NULL) {
320 ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_PASSED_NULL_PARAMETER);
324 if (ctx->fetched_loader != NULL) {
327 /* OSSL_STORE_SEARCH_BY_NAME, OSSL_STORE_SEARCH_BY_ISSUER_SERIAL*/
328 void *name_der = NULL;
330 /* OSSL_STORE_SEARCH_BY_ISSUER_SERIAL */
331 BIGNUM *number = NULL;
333 if (ctx->fetched_loader->p_set_ctx_params == NULL) {
334 ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_UNSUPPORTED_OPERATION);
338 if ((bld = OSSL_PARAM_BLD_new()) == NULL) {
339 ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_MALLOC_FAILURE);
343 ret = 0; /* Assume the worst */
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))
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,
365 case OSSL_STORE_SEARCH_BY_KEY_FINGERPRINT:
366 if (OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_STORE_PARAM_DIGEST,
367 EVP_MD_get0_name(search->digest),
369 && OSSL_PARAM_BLD_push_octet_string(bld,
370 OSSL_STORE_PARAM_FINGERPRINT,
372 search->stringlength))
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))
383 params = OSSL_PARAM_BLD_to_param(bld);
384 ret = ctx->fetched_loader->p_set_ctx_params(ctx->loader_ctx,
386 OSSL_PARAM_free(params);
388 OSSL_PARAM_BLD_free(bld);
389 OPENSSL_free(name_der);
392 #ifndef OPENSSL_NO_DEPRECATED_3_0
393 /* legacy loader section */
394 if (ctx->loader->find == NULL) {
395 ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_UNSUPPORTED_OPERATION);
398 ret = ctx->loader->find(ctx->loader_ctx, search);
405 OSSL_STORE_INFO *OSSL_STORE_load(OSSL_STORE_CTX *ctx)
407 OSSL_STORE_INFO *v = NULL;
411 if (OSSL_STORE_eof(ctx))
414 if (ctx->loader != NULL)
415 OSSL_TRACE(STORE, "Loading next object\n");
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;
423 if (ctx->cached_info != NULL) {
424 v = sk_OSSL_STORE_INFO_shift(ctx->cached_info);
426 if (ctx->fetched_loader != NULL) {
427 struct ossl_load_result_data_st load_data;
432 if (!ctx->fetched_loader->p_load(ctx->loader_ctx,
433 ossl_store_handle_load_result,
435 ossl_pw_passphrase_callback_dec,
437 if (!OSSL_STORE_eof(ctx))
443 #ifndef OPENSSL_NO_DEPRECATED_3_0
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);
451 if (ctx->post_process != NULL && v != NULL) {
452 v = ctx->post_process(v, ctx->post_process_data);
455 * By returning NULL, the callback decides that this object should
462 /* Clear any internally cached passphrase */
463 (void)ossl_pw_clear_passphrase_cache(&ctx->pwdata);
465 if (v != NULL && ctx->expected_type != 0) {
466 int returned_type = OSSL_STORE_INFO_get_type(v);
468 if (returned_type != OSSL_STORE_INFO_NAME && returned_type != 0) {
469 if (ctx->expected_type != returned_type) {
470 OSSL_STORE_INFO_free(v);
477 OSSL_TRACE1(STORE, "Got a %s\n",
478 OSSL_STORE_INFO_type_string(OSSL_STORE_INFO_get_type(v)));
483 int OSSL_STORE_error(OSSL_STORE_CTX *ctx)
487 if (ctx->fetched_loader != NULL)
488 ret = ctx->error_flag;
489 #ifndef OPENSSL_NO_DEPRECATED_3_0
490 if (ctx->fetched_loader == NULL)
491 ret = ctx->loader->error(ctx->loader_ctx);
496 int OSSL_STORE_eof(OSSL_STORE_CTX *ctx)
500 if (ctx->fetched_loader != NULL)
501 ret = ctx->loader->p_eof(ctx->loader_ctx);
502 #ifndef OPENSSL_NO_DEPRECATED_3_0
503 if (ctx->fetched_loader == NULL)
504 ret = ctx->loader->eof(ctx->loader_ctx);
509 static int ossl_store_close_it(OSSL_STORE_CTX *ctx)
515 OSSL_TRACE1(STORE, "Closing %p\n", (void *)ctx->loader_ctx);
517 if (ctx->fetched_loader != NULL)
518 ret = ctx->loader->p_close(ctx->loader_ctx);
519 #ifndef OPENSSL_NO_DEPRECATED_3_0
520 if (ctx->fetched_loader == NULL)
521 ret = ctx->loader->closefn(ctx->loader_ctx);
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);
527 ossl_pw_clear_passphrase_data(&ctx->pwdata);
531 int OSSL_STORE_close(OSSL_STORE_CTX *ctx)
533 int ret = ossl_store_close_it(ctx);
540 * Functions to generate OSSL_STORE_INFOs, one function for each type we
541 * support having in them as well as a generic constructor.
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.
546 OSSL_STORE_INFO *OSSL_STORE_INFO_new(int type, void *data)
548 OSSL_STORE_INFO *info = OPENSSL_zalloc(sizeof(*info));
558 OSSL_STORE_INFO *OSSL_STORE_INFO_new_NAME(char *name)
560 OSSL_STORE_INFO *info = OSSL_STORE_INFO_new(OSSL_STORE_INFO_NAME, NULL);
563 ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_MALLOC_FAILURE);
567 info->_.name.name = name;
568 info->_.name.desc = NULL;
573 int OSSL_STORE_INFO_set0_NAME_description(OSSL_STORE_INFO *info, char *desc)
575 if (info->type != OSSL_STORE_INFO_NAME) {
576 ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_PASSED_INVALID_ARGUMENT);
580 info->_.name.desc = desc;
584 OSSL_STORE_INFO *OSSL_STORE_INFO_new_PARAMS(EVP_PKEY *params)
586 OSSL_STORE_INFO *info = OSSL_STORE_INFO_new(OSSL_STORE_INFO_PARAMS, params);
589 ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_MALLOC_FAILURE);
593 OSSL_STORE_INFO *OSSL_STORE_INFO_new_PUBKEY(EVP_PKEY *pkey)
595 OSSL_STORE_INFO *info = OSSL_STORE_INFO_new(OSSL_STORE_INFO_PUBKEY, pkey);
598 ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_MALLOC_FAILURE);
602 OSSL_STORE_INFO *OSSL_STORE_INFO_new_PKEY(EVP_PKEY *pkey)
604 OSSL_STORE_INFO *info = OSSL_STORE_INFO_new(OSSL_STORE_INFO_PKEY, pkey);
607 ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_MALLOC_FAILURE);
611 OSSL_STORE_INFO *OSSL_STORE_INFO_new_CERT(X509 *x509)
613 OSSL_STORE_INFO *info = OSSL_STORE_INFO_new(OSSL_STORE_INFO_CERT, x509);
616 ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_MALLOC_FAILURE);
620 OSSL_STORE_INFO *OSSL_STORE_INFO_new_CRL(X509_CRL *crl)
622 OSSL_STORE_INFO *info = OSSL_STORE_INFO_new(OSSL_STORE_INFO_CRL, crl);
625 ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_MALLOC_FAILURE);
630 * Functions to try to extract data from a OSSL_STORE_INFO.
632 int OSSL_STORE_INFO_get_type(const OSSL_STORE_INFO *info)
637 void *OSSL_STORE_INFO_get0_data(int type, const OSSL_STORE_INFO *info)
639 if (info->type == type)
644 const char *OSSL_STORE_INFO_get0_NAME(const OSSL_STORE_INFO *info)
646 if (info->type == OSSL_STORE_INFO_NAME)
647 return info->_.name.name;
651 char *OSSL_STORE_INFO_get1_NAME(const OSSL_STORE_INFO *info)
653 if (info->type == OSSL_STORE_INFO_NAME) {
654 char *ret = OPENSSL_strdup(info->_.name.name);
657 ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_MALLOC_FAILURE);
660 ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_NOT_A_NAME);
664 const char *OSSL_STORE_INFO_get0_NAME_description(const OSSL_STORE_INFO *info)
666 if (info->type == OSSL_STORE_INFO_NAME)
667 return info->_.name.desc;
671 char *OSSL_STORE_INFO_get1_NAME_description(const OSSL_STORE_INFO *info)
673 if (info->type == OSSL_STORE_INFO_NAME) {
674 char *ret = OPENSSL_strdup(info->_.name.desc
675 ? info->_.name.desc : "");
678 ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_MALLOC_FAILURE);
681 ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_NOT_A_NAME);
685 EVP_PKEY *OSSL_STORE_INFO_get0_PARAMS(const OSSL_STORE_INFO *info)
687 if (info->type == OSSL_STORE_INFO_PARAMS)
688 return info->_.params;
692 EVP_PKEY *OSSL_STORE_INFO_get1_PARAMS(const OSSL_STORE_INFO *info)
694 if (info->type == OSSL_STORE_INFO_PARAMS) {
695 EVP_PKEY_up_ref(info->_.params);
696 return info->_.params;
698 ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_NOT_PARAMETERS);
702 EVP_PKEY *OSSL_STORE_INFO_get0_PUBKEY(const OSSL_STORE_INFO *info)
704 if (info->type == OSSL_STORE_INFO_PUBKEY)
705 return info->_.pubkey;
709 EVP_PKEY *OSSL_STORE_INFO_get1_PUBKEY(const OSSL_STORE_INFO *info)
711 if (info->type == OSSL_STORE_INFO_PUBKEY) {
712 EVP_PKEY_up_ref(info->_.pubkey);
713 return info->_.pubkey;
715 ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_NOT_A_PUBLIC_KEY);
719 EVP_PKEY *OSSL_STORE_INFO_get0_PKEY(const OSSL_STORE_INFO *info)
721 if (info->type == OSSL_STORE_INFO_PKEY)
726 EVP_PKEY *OSSL_STORE_INFO_get1_PKEY(const OSSL_STORE_INFO *info)
728 if (info->type == OSSL_STORE_INFO_PKEY) {
729 EVP_PKEY_up_ref(info->_.pkey);
732 ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_NOT_A_PRIVATE_KEY);
736 X509 *OSSL_STORE_INFO_get0_CERT(const OSSL_STORE_INFO *info)
738 if (info->type == OSSL_STORE_INFO_CERT)
743 X509 *OSSL_STORE_INFO_get1_CERT(const OSSL_STORE_INFO *info)
745 if (info->type == OSSL_STORE_INFO_CERT) {
746 X509_up_ref(info->_.x509);
749 ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_NOT_A_CERTIFICATE);
753 X509_CRL *OSSL_STORE_INFO_get0_CRL(const OSSL_STORE_INFO *info)
755 if (info->type == OSSL_STORE_INFO_CRL)
760 X509_CRL *OSSL_STORE_INFO_get1_CRL(const OSSL_STORE_INFO *info)
762 if (info->type == OSSL_STORE_INFO_CRL) {
763 X509_CRL_up_ref(info->_.crl);
766 ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_NOT_A_CRL);
771 * Free the OSSL_STORE_INFO
773 void OSSL_STORE_INFO_free(OSSL_STORE_INFO *info)
776 switch (info->type) {
777 case OSSL_STORE_INFO_NAME:
778 OPENSSL_free(info->_.name.name);
779 OPENSSL_free(info->_.name.desc);
781 case OSSL_STORE_INFO_PARAMS:
782 EVP_PKEY_free(info->_.params);
784 case OSSL_STORE_INFO_PUBKEY:
785 EVP_PKEY_free(info->_.pubkey);
787 case OSSL_STORE_INFO_PKEY:
788 EVP_PKEY_free(info->_.pkey);
790 case OSSL_STORE_INFO_CERT:
791 X509_free(info->_.x509);
793 case OSSL_STORE_INFO_CRL:
794 X509_CRL_free(info->_.crl);
801 int OSSL_STORE_supports_search(OSSL_STORE_CTX *ctx, int search_type)
805 if (ctx->fetched_loader != NULL) {
807 ossl_provider_ctx(OSSL_STORE_LOADER_get0_provider(ctx->fetched_loader));
808 const OSSL_PARAM *params;
809 const OSSL_PARAM *p_subject = NULL;
810 const OSSL_PARAM *p_issuer = NULL;
811 const OSSL_PARAM *p_serial = NULL;
812 const OSSL_PARAM *p_fingerprint = NULL;
813 const OSSL_PARAM *p_alias = NULL;
815 if (ctx->fetched_loader->p_settable_ctx_params == NULL)
818 params = ctx->fetched_loader->p_settable_ctx_params(provctx);
819 p_subject = OSSL_PARAM_locate_const(params, OSSL_STORE_PARAM_SUBJECT);
820 p_issuer = OSSL_PARAM_locate_const(params, OSSL_STORE_PARAM_ISSUER);
821 p_serial = OSSL_PARAM_locate_const(params, OSSL_STORE_PARAM_SERIAL);
823 OSSL_PARAM_locate_const(params, OSSL_STORE_PARAM_FINGERPRINT);
824 p_alias = OSSL_PARAM_locate_const(params, OSSL_STORE_PARAM_ALIAS);
826 switch (search_type) {
827 case OSSL_STORE_SEARCH_BY_NAME:
828 ret = (p_subject != NULL);
830 case OSSL_STORE_SEARCH_BY_ISSUER_SERIAL:
831 ret = (p_issuer != NULL && p_serial != NULL);
833 case OSSL_STORE_SEARCH_BY_KEY_FINGERPRINT:
834 ret = (p_fingerprint != NULL);
836 case OSSL_STORE_SEARCH_BY_ALIAS:
837 ret = (p_alias != NULL);
841 #ifndef OPENSSL_NO_DEPRECATED_3_0
842 if (ctx->fetched_loader == NULL) {
843 OSSL_STORE_SEARCH tmp_search;
845 if (ctx->loader->find == NULL)
847 tmp_search.search_type = search_type;
848 ret = ctx->loader->find(NULL, &tmp_search);
854 /* Search term constructors */
855 OSSL_STORE_SEARCH *OSSL_STORE_SEARCH_by_name(X509_NAME *name)
857 OSSL_STORE_SEARCH *search = OPENSSL_zalloc(sizeof(*search));
859 if (search == NULL) {
860 ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_MALLOC_FAILURE);
864 search->search_type = OSSL_STORE_SEARCH_BY_NAME;
869 OSSL_STORE_SEARCH *OSSL_STORE_SEARCH_by_issuer_serial(X509_NAME *name,
870 const ASN1_INTEGER *serial)
872 OSSL_STORE_SEARCH *search = OPENSSL_zalloc(sizeof(*search));
874 if (search == NULL) {
875 ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_MALLOC_FAILURE);
879 search->search_type = OSSL_STORE_SEARCH_BY_ISSUER_SERIAL;
881 search->serial = serial;
885 OSSL_STORE_SEARCH *OSSL_STORE_SEARCH_by_key_fingerprint(const EVP_MD *digest,
889 OSSL_STORE_SEARCH *search = OPENSSL_zalloc(sizeof(*search));
891 if (search == NULL) {
892 ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_MALLOC_FAILURE);
896 if (digest != NULL && len != (size_t)EVP_MD_get_size(digest)) {
897 ERR_raise_data(ERR_LIB_OSSL_STORE,
898 OSSL_STORE_R_FINGERPRINT_SIZE_DOES_NOT_MATCH_DIGEST,
899 "%s size is %d, fingerprint size is %zu",
900 EVP_MD_get0_name(digest), EVP_MD_get_size(digest), len);
901 OPENSSL_free(search);
905 search->search_type = OSSL_STORE_SEARCH_BY_KEY_FINGERPRINT;
906 search->digest = digest;
907 search->string = bytes;
908 search->stringlength = len;
912 OSSL_STORE_SEARCH *OSSL_STORE_SEARCH_by_alias(const char *alias)
914 OSSL_STORE_SEARCH *search = OPENSSL_zalloc(sizeof(*search));
916 if (search == NULL) {
917 ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_MALLOC_FAILURE);
921 search->search_type = OSSL_STORE_SEARCH_BY_ALIAS;
922 search->string = (const unsigned char *)alias;
923 search->stringlength = strlen(alias);
927 /* Search term destructor */
928 void OSSL_STORE_SEARCH_free(OSSL_STORE_SEARCH *search)
930 OPENSSL_free(search);
933 /* Search term accessors */
934 int OSSL_STORE_SEARCH_get_type(const OSSL_STORE_SEARCH *criterion)
936 return criterion->search_type;
939 X509_NAME *OSSL_STORE_SEARCH_get0_name(const OSSL_STORE_SEARCH *criterion)
941 return criterion->name;
944 const ASN1_INTEGER *OSSL_STORE_SEARCH_get0_serial(const OSSL_STORE_SEARCH
947 return criterion->serial;
950 const unsigned char *OSSL_STORE_SEARCH_get0_bytes(const OSSL_STORE_SEARCH
951 *criterion, size_t *length)
953 *length = criterion->stringlength;
954 return criterion->string;
957 const char *OSSL_STORE_SEARCH_get0_string(const OSSL_STORE_SEARCH *criterion)
959 return (const char *)criterion->string;
962 const EVP_MD *OSSL_STORE_SEARCH_get0_digest(const OSSL_STORE_SEARCH *criterion)
964 return criterion->digest;
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)
974 const OSSL_STORE_LOADER *loader = NULL;
975 OSSL_STORE_LOADER *fetched_loader = NULL;
976 OSSL_STORE_LOADER_CTX *loader_ctx = NULL;
977 OSSL_STORE_CTX *ctx = NULL;
982 OSSL_TRACE1(STORE, "Looking up scheme %s\n", scheme);
984 #ifndef OPENSSL_NO_DEPRECATED_3_0
985 if ((loader = ossl_store_get0_loader_int(scheme)) != NULL)
986 loader_ctx = loader->attach(loader, bp, libctx, propq,
991 OSSL_STORE_LOADER_fetch(libctx, scheme, propq)) != NULL) {
992 const OSSL_PROVIDER *provider =
993 OSSL_STORE_LOADER_get0_provider(fetched_loader);
994 void *provctx = OSSL_PROVIDER_get0_provider_ctx(provider);
995 OSSL_CORE_BIO *cbio = ossl_core_bio_new_from_bio(bp);
998 || (loader_ctx = fetched_loader->p_attach(provctx, cbio)) == NULL) {
999 OSSL_STORE_LOADER_free(fetched_loader);
1000 fetched_loader = NULL;
1001 } else if (!loader_set_params(fetched_loader, loader_ctx,
1003 (void)fetched_loader->p_close(loader_ctx);
1004 OSSL_STORE_LOADER_free(fetched_loader);
1005 fetched_loader = NULL;
1007 loader = fetched_loader;
1008 ossl_core_bio_free(cbio);
1011 if (loader_ctx == NULL) {
1012 ERR_clear_last_mark();
1016 if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL) {
1017 ERR_clear_last_mark();
1018 ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_MALLOC_FAILURE);
1022 if (ui_method != NULL
1023 && !ossl_pw_set_ui_method(&ctx->pwdata, ui_method, ui_data)) {
1024 ERR_clear_last_mark();
1029 ctx->fetched_loader = fetched_loader;
1030 ctx->loader = loader;
1031 ctx->loader_ctx = loader_ctx;
1032 ctx->post_process = post_process;
1033 ctx->post_process_data = post_process_data;
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.