/src/openssl31/crypto/provider.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2019-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 <string.h> |
11 | | #include <openssl/err.h> |
12 | | #include <openssl/cryptoerr.h> |
13 | | #include <openssl/provider.h> |
14 | | #include <openssl/core_names.h> |
15 | | #include "internal/provider.h" |
16 | | #include "provider_local.h" |
17 | | |
18 | | OSSL_PROVIDER *OSSL_PROVIDER_try_load(OSSL_LIB_CTX *libctx, const char *name, |
19 | | int retain_fallbacks) |
20 | 20 | { |
21 | 20 | OSSL_PROVIDER *prov = NULL, *actual; |
22 | 20 | int isnew = 0; |
23 | | |
24 | | /* Find it or create it */ |
25 | 20 | if ((prov = ossl_provider_find(libctx, name, 0)) == NULL) { |
26 | 20 | if ((prov = ossl_provider_new(libctx, name, NULL, 0)) == NULL) |
27 | 0 | return NULL; |
28 | 20 | isnew = 1; |
29 | 20 | } |
30 | | |
31 | 20 | if (!ossl_provider_activate(prov, 1, 0)) { |
32 | 0 | ossl_provider_free(prov); |
33 | 0 | return NULL; |
34 | 0 | } |
35 | | |
36 | 20 | actual = prov; |
37 | 20 | if (isnew && !ossl_provider_add_to_store(prov, &actual, retain_fallbacks)) { |
38 | 0 | ossl_provider_deactivate(prov, 1); |
39 | 0 | ossl_provider_free(prov); |
40 | 0 | return NULL; |
41 | 0 | } |
42 | 20 | if (actual != prov) { |
43 | 0 | if (!ossl_provider_activate(actual, 1, 0)) { |
44 | 0 | ossl_provider_free(actual); |
45 | 0 | return NULL; |
46 | 0 | } |
47 | 0 | } |
48 | | |
49 | 20 | return actual; |
50 | 20 | } |
51 | | |
52 | | OSSL_PROVIDER *OSSL_PROVIDER_load(OSSL_LIB_CTX *libctx, const char *name) |
53 | 0 | { |
54 | | /* Any attempt to load a provider disables auto-loading of defaults */ |
55 | 0 | if (ossl_provider_disable_fallback_loading(libctx)) |
56 | 0 | return OSSL_PROVIDER_try_load(libctx, name, 0); |
57 | 0 | return NULL; |
58 | 0 | } |
59 | | |
60 | | int OSSL_PROVIDER_unload(OSSL_PROVIDER *prov) |
61 | 0 | { |
62 | 0 | if (!ossl_provider_deactivate(prov, 1)) |
63 | 0 | return 0; |
64 | 0 | ossl_provider_free(prov); |
65 | 0 | return 1; |
66 | 0 | } |
67 | | |
68 | | const OSSL_PARAM *OSSL_PROVIDER_gettable_params(const OSSL_PROVIDER *prov) |
69 | 0 | { |
70 | 0 | return ossl_provider_gettable_params(prov); |
71 | 0 | } |
72 | | |
73 | | int OSSL_PROVIDER_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[]) |
74 | 0 | { |
75 | 0 | return ossl_provider_get_params(prov, params); |
76 | 0 | } |
77 | | |
78 | | const OSSL_ALGORITHM *OSSL_PROVIDER_query_operation(const OSSL_PROVIDER *prov, |
79 | | int operation_id, |
80 | | int *no_cache) |
81 | 0 | { |
82 | 0 | return ossl_provider_query_operation(prov, operation_id, no_cache); |
83 | 0 | } |
84 | | |
85 | | void OSSL_PROVIDER_unquery_operation(const OSSL_PROVIDER *prov, |
86 | | int operation_id, |
87 | | const OSSL_ALGORITHM *algs) |
88 | 0 | { |
89 | 0 | ossl_provider_unquery_operation(prov, operation_id, algs); |
90 | 0 | } |
91 | | |
92 | | void *OSSL_PROVIDER_get0_provider_ctx(const OSSL_PROVIDER *prov) |
93 | 329M | { |
94 | 329M | return ossl_provider_prov_ctx(prov); |
95 | 329M | } |
96 | | |
97 | | const OSSL_DISPATCH *OSSL_PROVIDER_get0_dispatch(const OSSL_PROVIDER *prov) |
98 | 0 | { |
99 | 0 | return ossl_provider_get0_dispatch(prov); |
100 | 0 | } |
101 | | |
102 | | int OSSL_PROVIDER_self_test(const OSSL_PROVIDER *prov) |
103 | 0 | { |
104 | 0 | return ossl_provider_self_test(prov); |
105 | 0 | } |
106 | | |
107 | | int OSSL_PROVIDER_get_capabilities(const OSSL_PROVIDER *prov, |
108 | | const char *capability, |
109 | | OSSL_CALLBACK *cb, |
110 | | void *arg) |
111 | 268k | { |
112 | 268k | return ossl_provider_get_capabilities(prov, capability, cb, arg); |
113 | 268k | } |
114 | | |
115 | | int OSSL_PROVIDER_add_builtin(OSSL_LIB_CTX *libctx, const char *name, |
116 | | OSSL_provider_init_fn *init_fn) |
117 | 60 | { |
118 | 60 | OSSL_PROVIDER_INFO entry; |
119 | | |
120 | 60 | if (name == NULL || init_fn == NULL) { |
121 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER); |
122 | 0 | return 0; |
123 | 0 | } |
124 | 60 | memset(&entry, 0, sizeof(entry)); |
125 | 60 | entry.name = OPENSSL_strdup(name); |
126 | 60 | if (entry.name == NULL) { |
127 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE); |
128 | 0 | return 0; |
129 | 0 | } |
130 | 60 | entry.init = init_fn; |
131 | 60 | if (!ossl_provider_info_add_to_store(libctx, &entry)) { |
132 | 0 | ossl_provider_info_clear(&entry); |
133 | 0 | return 0; |
134 | 0 | } |
135 | 60 | return 1; |
136 | 60 | } |
137 | | |
138 | | const char *OSSL_PROVIDER_get0_name(const OSSL_PROVIDER *prov) |
139 | 0 | { |
140 | 0 | return ossl_provider_name(prov); |
141 | 0 | } |
142 | | |
143 | | int OSSL_PROVIDER_do_all(OSSL_LIB_CTX *ctx, |
144 | | int (*cb)(OSSL_PROVIDER *provider, |
145 | | void *cbdata), |
146 | | void *cbdata) |
147 | 134k | { |
148 | 134k | return ossl_provider_doall_activated(ctx, cb, cbdata); |
149 | 134k | } |