]> git.ipfire.org Git - thirdparty/openssl.git/blob - apps/lib/app_provider.c
c3100b2fa889dfd312088620cb9b5f82f342d4c4
[thirdparty/openssl.git] / apps / lib / app_provider.c
1 /*
2 * Copyright 2020-2021 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 "apps.h"
11 #include <string.h>
12 #include <openssl/err.h>
13 #include <openssl/provider.h>
14 #include <openssl/safestack.h>
15
16 DEFINE_STACK_OF(OSSL_PROVIDER)
17
18 /*
19 * See comments in opt_verify for explanation of this.
20 */
21 enum prov_range { OPT_PROV_ENUM };
22
23 static STACK_OF(OSSL_PROVIDER) *app_providers = NULL;
24
25 static void provider_free(OSSL_PROVIDER *prov)
26 {
27 OSSL_PROVIDER_unload(prov);
28 }
29
30 int app_provider_load(OSSL_LIB_CTX *libctx, const char *provider_name)
31 {
32 OSSL_PROVIDER *prov;
33
34 prov = OSSL_PROVIDER_load(libctx, provider_name);
35 if (prov == NULL) {
36 opt_printf_stderr("%s: unable to load provider %s\n"
37 "Hint: use -provider-path option or OPENSSL_MODULES environment variable.\n",
38 opt_getprog(), provider_name);
39 ERR_print_errors(bio_err);
40 return 0;
41 }
42 if (app_providers == NULL)
43 app_providers = sk_OSSL_PROVIDER_new_null();
44 if (app_providers == NULL
45 || !sk_OSSL_PROVIDER_push(app_providers, prov)) {
46 app_providers_cleanup();
47 return 0;
48 }
49 return 1;
50 }
51
52 void app_providers_cleanup(void)
53 {
54 sk_OSSL_PROVIDER_pop_free(app_providers, provider_free);
55 app_providers = NULL;
56 }
57
58 static int opt_provider_path(const char *path)
59 {
60 if (path != NULL && *path == '\0')
61 path = NULL;
62 return OSSL_PROVIDER_set_default_search_path(app_get0_libctx(), path);
63 }
64
65 int opt_provider(int opt)
66 {
67 switch ((enum prov_range)opt) {
68 case OPT_PROV__FIRST:
69 case OPT_PROV__LAST:
70 return 1;
71 case OPT_PROV_PROVIDER:
72 return app_provider_load(app_get0_libctx(), opt_arg());
73 case OPT_PROV_PROVIDER_PATH:
74 return opt_provider_path(opt_arg());
75 case OPT_PROV_PROPQUERY:
76 return app_set_propq(opt_arg());
77 }
78 return 0;
79 }