1*66bae5e7Schristos /*
2*66bae5e7Schristos * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
3*66bae5e7Schristos *
4*66bae5e7Schristos * Licensed under the Apache License 2.0 (the "License"). You may not use
5*66bae5e7Schristos * this file except in compliance with the License. You can obtain a copy
6*66bae5e7Schristos * in the file LICENSE in the source distribution or at
7*66bae5e7Schristos * https://www.openssl.org/source/license.html
8*66bae5e7Schristos */
9*66bae5e7Schristos
10*66bae5e7Schristos #include "apps.h"
11*66bae5e7Schristos #include <string.h>
12*66bae5e7Schristos #include <openssl/err.h>
13*66bae5e7Schristos #include <openssl/provider.h>
14*66bae5e7Schristos #include <openssl/safestack.h>
15*66bae5e7Schristos
16*66bae5e7Schristos /* Non-zero if any of the provider options have been seen */
17*66bae5e7Schristos static int provider_option_given = 0;
18*66bae5e7Schristos
19*66bae5e7Schristos DEFINE_STACK_OF(OSSL_PROVIDER)
20*66bae5e7Schristos
21*66bae5e7Schristos /*
22*66bae5e7Schristos * See comments in opt_verify for explanation of this.
23*66bae5e7Schristos */
24*66bae5e7Schristos enum prov_range { OPT_PROV_ENUM };
25*66bae5e7Schristos
26*66bae5e7Schristos static STACK_OF(OSSL_PROVIDER) *app_providers = NULL;
27*66bae5e7Schristos
provider_free(OSSL_PROVIDER * prov)28*66bae5e7Schristos static void provider_free(OSSL_PROVIDER *prov)
29*66bae5e7Schristos {
30*66bae5e7Schristos OSSL_PROVIDER_unload(prov);
31*66bae5e7Schristos }
32*66bae5e7Schristos
app_provider_load(OSSL_LIB_CTX * libctx,const char * provider_name)33*66bae5e7Schristos int app_provider_load(OSSL_LIB_CTX *libctx, const char *provider_name)
34*66bae5e7Schristos {
35*66bae5e7Schristos OSSL_PROVIDER *prov;
36*66bae5e7Schristos
37*66bae5e7Schristos prov = OSSL_PROVIDER_load(libctx, provider_name);
38*66bae5e7Schristos if (prov == NULL) {
39*66bae5e7Schristos opt_printf_stderr("%s: unable to load provider %s\n"
40*66bae5e7Schristos "Hint: use -provider-path option or OPENSSL_MODULES environment variable.\n",
41*66bae5e7Schristos opt_getprog(), provider_name);
42*66bae5e7Schristos ERR_print_errors(bio_err);
43*66bae5e7Schristos return 0;
44*66bae5e7Schristos }
45*66bae5e7Schristos if (app_providers == NULL)
46*66bae5e7Schristos app_providers = sk_OSSL_PROVIDER_new_null();
47*66bae5e7Schristos if (app_providers == NULL
48*66bae5e7Schristos || !sk_OSSL_PROVIDER_push(app_providers, prov)) {
49*66bae5e7Schristos app_providers_cleanup();
50*66bae5e7Schristos return 0;
51*66bae5e7Schristos }
52*66bae5e7Schristos return 1;
53*66bae5e7Schristos }
54*66bae5e7Schristos
app_providers_cleanup(void)55*66bae5e7Schristos void app_providers_cleanup(void)
56*66bae5e7Schristos {
57*66bae5e7Schristos sk_OSSL_PROVIDER_pop_free(app_providers, provider_free);
58*66bae5e7Schristos app_providers = NULL;
59*66bae5e7Schristos }
60*66bae5e7Schristos
opt_provider_path(const char * path)61*66bae5e7Schristos static int opt_provider_path(const char *path)
62*66bae5e7Schristos {
63*66bae5e7Schristos if (path != NULL && *path == '\0')
64*66bae5e7Schristos path = NULL;
65*66bae5e7Schristos return OSSL_PROVIDER_set_default_search_path(app_get0_libctx(), path);
66*66bae5e7Schristos }
67*66bae5e7Schristos
opt_provider(int opt)68*66bae5e7Schristos int opt_provider(int opt)
69*66bae5e7Schristos {
70*66bae5e7Schristos const int given = provider_option_given;
71*66bae5e7Schristos
72*66bae5e7Schristos provider_option_given = 1;
73*66bae5e7Schristos switch ((enum prov_range)opt) {
74*66bae5e7Schristos case OPT_PROV__FIRST:
75*66bae5e7Schristos case OPT_PROV__LAST:
76*66bae5e7Schristos return 1;
77*66bae5e7Schristos case OPT_PROV_PROVIDER:
78*66bae5e7Schristos return app_provider_load(app_get0_libctx(), opt_arg());
79*66bae5e7Schristos case OPT_PROV_PROVIDER_PATH:
80*66bae5e7Schristos return opt_provider_path(opt_arg());
81*66bae5e7Schristos case OPT_PROV_PROPQUERY:
82*66bae5e7Schristos return app_set_propq(opt_arg());
83*66bae5e7Schristos }
84*66bae5e7Schristos /* Should never get here but if we do, undo what we did earlier */
85*66bae5e7Schristos provider_option_given = given;
86*66bae5e7Schristos return 0;
87*66bae5e7Schristos }
88*66bae5e7Schristos
opt_provider_option_given(void)89*66bae5e7Schristos int opt_provider_option_given(void)
90*66bae5e7Schristos {
91*66bae5e7Schristos return provider_option_given;
92*66bae5e7Schristos }
93