/src/openssl/crypto/provider_conf.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2019-2022 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/trace.h> |
12 | | #include <openssl/err.h> |
13 | | #include <openssl/conf.h> |
14 | | #include <openssl/safestack.h> |
15 | | #include <openssl/provider.h> |
16 | | #include "internal/provider.h" |
17 | | #include "internal/cryptlib.h" |
18 | | #include "provider_local.h" |
19 | | #include "crypto/context.h" |
20 | | |
21 | | DEFINE_STACK_OF(OSSL_PROVIDER) |
22 | | |
23 | | /* PROVIDER config module */ |
24 | | |
25 | | typedef struct { |
26 | | CRYPTO_RWLOCK *lock; |
27 | | STACK_OF(OSSL_PROVIDER) *activated_providers; |
28 | | } PROVIDER_CONF_GLOBAL; |
29 | | |
30 | | void *ossl_prov_conf_ctx_new(OSSL_LIB_CTX *libctx) |
31 | 2 | { |
32 | 2 | PROVIDER_CONF_GLOBAL *pcgbl = OPENSSL_zalloc(sizeof(*pcgbl)); |
33 | | |
34 | 2 | if (pcgbl == NULL) |
35 | 0 | return NULL; |
36 | | |
37 | 2 | pcgbl->lock = CRYPTO_THREAD_lock_new(); |
38 | 2 | if (pcgbl->lock == NULL) { |
39 | 0 | OPENSSL_free(pcgbl); |
40 | 0 | return NULL; |
41 | 0 | } |
42 | | |
43 | 2 | return pcgbl; |
44 | 2 | } |
45 | | |
46 | | void ossl_prov_conf_ctx_free(void *vpcgbl) |
47 | 2 | { |
48 | 2 | PROVIDER_CONF_GLOBAL *pcgbl = vpcgbl; |
49 | | |
50 | 2 | sk_OSSL_PROVIDER_pop_free(pcgbl->activated_providers, |
51 | 2 | ossl_provider_free); |
52 | | |
53 | 2 | OSSL_TRACE(CONF, "Cleaned up providers\n"); |
54 | 2 | CRYPTO_THREAD_lock_free(pcgbl->lock); |
55 | 2 | OPENSSL_free(pcgbl); |
56 | 2 | } |
57 | | |
58 | | static const char *skip_dot(const char *name) |
59 | 0 | { |
60 | 0 | const char *p = strchr(name, '.'); |
61 | |
|
62 | 0 | if (p != NULL) |
63 | 0 | return p + 1; |
64 | 0 | return name; |
65 | 0 | } |
66 | | |
67 | | static int provider_conf_params(OSSL_PROVIDER *prov, |
68 | | OSSL_PROVIDER_INFO *provinfo, |
69 | | const char *name, const char *value, |
70 | | const CONF *cnf) |
71 | 0 | { |
72 | 0 | STACK_OF(CONF_VALUE) *sect; |
73 | 0 | int ok = 1; |
74 | |
|
75 | 0 | sect = NCONF_get_section(cnf, value); |
76 | 0 | if (sect != NULL) { |
77 | 0 | int i; |
78 | 0 | char buffer[512]; |
79 | 0 | size_t buffer_len = 0; |
80 | |
|
81 | 0 | OSSL_TRACE1(CONF, "Provider params: start section %s\n", value); |
82 | |
|
83 | 0 | if (name != NULL) { |
84 | 0 | OPENSSL_strlcpy(buffer, name, sizeof(buffer)); |
85 | 0 | OPENSSL_strlcat(buffer, ".", sizeof(buffer)); |
86 | 0 | buffer_len = strlen(buffer); |
87 | 0 | } |
88 | |
|
89 | 0 | for (i = 0; i < sk_CONF_VALUE_num(sect); i++) { |
90 | 0 | CONF_VALUE *sectconf = sk_CONF_VALUE_value(sect, i); |
91 | |
|
92 | 0 | if (buffer_len + strlen(sectconf->name) >= sizeof(buffer)) |
93 | 0 | return 0; |
94 | 0 | buffer[buffer_len] = '\0'; |
95 | 0 | OPENSSL_strlcat(buffer, sectconf->name, sizeof(buffer)); |
96 | 0 | if (!provider_conf_params(prov, provinfo, buffer, sectconf->value, |
97 | 0 | cnf)) |
98 | 0 | return 0; |
99 | 0 | } |
100 | | |
101 | 0 | OSSL_TRACE1(CONF, "Provider params: finish section %s\n", value); |
102 | 0 | } else { |
103 | 0 | OSSL_TRACE2(CONF, "Provider params: %s = %s\n", name, value); |
104 | 0 | if (prov != NULL) |
105 | 0 | ok = ossl_provider_add_parameter(prov, name, value); |
106 | 0 | else |
107 | 0 | ok = ossl_provider_info_add_parameter(provinfo, name, value); |
108 | 0 | } |
109 | | |
110 | 0 | return ok; |
111 | 0 | } |
112 | | |
113 | | static int prov_already_activated(const char *name, |
114 | | STACK_OF(OSSL_PROVIDER) *activated) |
115 | 0 | { |
116 | 0 | int i, max; |
117 | |
|
118 | 0 | if (activated == NULL) |
119 | 0 | return 0; |
120 | | |
121 | 0 | max = sk_OSSL_PROVIDER_num(activated); |
122 | 0 | for (i = 0; i < max; i++) { |
123 | 0 | OSSL_PROVIDER *tstprov = sk_OSSL_PROVIDER_value(activated, i); |
124 | |
|
125 | 0 | if (strcmp(OSSL_PROVIDER_get0_name(tstprov), name) == 0) { |
126 | 0 | return 1; |
127 | 0 | } |
128 | 0 | } |
129 | | |
130 | 0 | return 0; |
131 | 0 | } |
132 | | |
133 | | static int provider_conf_activate(OSSL_LIB_CTX *libctx, const char *name, |
134 | | const char *value, const char *path, |
135 | | int soft, const CONF *cnf) |
136 | 0 | { |
137 | 0 | PROVIDER_CONF_GLOBAL *pcgbl |
138 | 0 | = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_PROVIDER_CONF_INDEX); |
139 | 0 | OSSL_PROVIDER *prov = NULL, *actual = NULL; |
140 | 0 | int ok = 0; |
141 | |
|
142 | 0 | if (pcgbl == NULL || !CRYPTO_THREAD_write_lock(pcgbl->lock)) { |
143 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
144 | 0 | return 0; |
145 | 0 | } |
146 | 0 | if (!prov_already_activated(name, pcgbl->activated_providers)) { |
147 | | /* |
148 | | * There is an attempt to activate a provider, so we should disable |
149 | | * loading of fallbacks. Otherwise a misconfiguration could mean the |
150 | | * intended provider does not get loaded. Subsequent fetches could |
151 | | * then fallback to the default provider - which may be the wrong |
152 | | * thing. |
153 | | */ |
154 | 0 | if (!ossl_provider_disable_fallback_loading(libctx)) { |
155 | 0 | CRYPTO_THREAD_unlock(pcgbl->lock); |
156 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
157 | 0 | return 0; |
158 | 0 | } |
159 | 0 | prov = ossl_provider_find(libctx, name, 1); |
160 | 0 | if (prov == NULL) |
161 | 0 | prov = ossl_provider_new(libctx, name, NULL, 1); |
162 | 0 | if (prov == NULL) { |
163 | 0 | CRYPTO_THREAD_unlock(pcgbl->lock); |
164 | 0 | if (soft) |
165 | 0 | ERR_clear_error(); |
166 | 0 | return 0; |
167 | 0 | } |
168 | | |
169 | 0 | if (path != NULL) |
170 | 0 | ossl_provider_set_module_path(prov, path); |
171 | |
|
172 | 0 | ok = provider_conf_params(prov, NULL, NULL, value, cnf); |
173 | |
|
174 | 0 | if (ok) { |
175 | 0 | if (!ossl_provider_activate(prov, 1, 0)) { |
176 | 0 | ok = 0; |
177 | 0 | } else if (!ossl_provider_add_to_store(prov, &actual, 0)) { |
178 | 0 | ossl_provider_deactivate(prov, 1); |
179 | 0 | ok = 0; |
180 | 0 | } else if (actual != prov |
181 | 0 | && !ossl_provider_activate(actual, 1, 0)) { |
182 | 0 | ossl_provider_free(actual); |
183 | 0 | ok = 0; |
184 | 0 | } else { |
185 | 0 | if (pcgbl->activated_providers == NULL) |
186 | 0 | pcgbl->activated_providers = sk_OSSL_PROVIDER_new_null(); |
187 | 0 | if (pcgbl->activated_providers == NULL |
188 | 0 | || !sk_OSSL_PROVIDER_push(pcgbl->activated_providers, |
189 | 0 | actual)) { |
190 | 0 | ossl_provider_deactivate(actual, 1); |
191 | 0 | ossl_provider_free(actual); |
192 | 0 | ok = 0; |
193 | 0 | } else { |
194 | 0 | ok = 1; |
195 | 0 | } |
196 | 0 | } |
197 | 0 | } |
198 | 0 | if (!ok) |
199 | 0 | ossl_provider_free(prov); |
200 | 0 | } |
201 | 0 | CRYPTO_THREAD_unlock(pcgbl->lock); |
202 | |
|
203 | 0 | return ok; |
204 | 0 | } |
205 | | |
206 | | static int provider_conf_load(OSSL_LIB_CTX *libctx, const char *name, |
207 | | const char *value, const CONF *cnf) |
208 | 0 | { |
209 | 0 | int i; |
210 | 0 | STACK_OF(CONF_VALUE) *ecmds; |
211 | 0 | int soft = 0; |
212 | 0 | const char *path = NULL; |
213 | 0 | long activate = 0; |
214 | 0 | int ok = 0; |
215 | |
|
216 | 0 | name = skip_dot(name); |
217 | 0 | OSSL_TRACE1(CONF, "Configuring provider %s\n", name); |
218 | | /* Value is a section containing PROVIDER commands */ |
219 | 0 | ecmds = NCONF_get_section(cnf, value); |
220 | |
|
221 | 0 | if (!ecmds) { |
222 | 0 | ERR_raise_data(ERR_LIB_CRYPTO, CRYPTO_R_PROVIDER_SECTION_ERROR, |
223 | 0 | "section=%s not found", value); |
224 | 0 | return 0; |
225 | 0 | } |
226 | | |
227 | | /* Find the needed data first */ |
228 | 0 | for (i = 0; i < sk_CONF_VALUE_num(ecmds); i++) { |
229 | 0 | CONF_VALUE *ecmd = sk_CONF_VALUE_value(ecmds, i); |
230 | 0 | const char *confname = skip_dot(ecmd->name); |
231 | 0 | const char *confvalue = ecmd->value; |
232 | |
|
233 | 0 | OSSL_TRACE2(CONF, "Provider command: %s = %s\n", |
234 | 0 | confname, confvalue); |
235 | | |
236 | | /* First handle some special pseudo confs */ |
237 | | |
238 | | /* Override provider name to use */ |
239 | 0 | if (strcmp(confname, "identity") == 0) |
240 | 0 | name = confvalue; |
241 | 0 | else if (strcmp(confname, "soft_load") == 0) |
242 | 0 | soft = 1; |
243 | | /* Load a dynamic PROVIDER */ |
244 | 0 | else if (strcmp(confname, "module") == 0) |
245 | 0 | path = confvalue; |
246 | 0 | else if (strcmp(confname, "activate") == 0) |
247 | 0 | activate = 1; |
248 | 0 | } |
249 | |
|
250 | 0 | if (activate) { |
251 | 0 | ok = provider_conf_activate(libctx, name, value, path, soft, cnf); |
252 | 0 | } else { |
253 | 0 | OSSL_PROVIDER_INFO entry; |
254 | |
|
255 | 0 | memset(&entry, 0, sizeof(entry)); |
256 | 0 | ok = 1; |
257 | 0 | if (name != NULL) { |
258 | 0 | entry.name = OPENSSL_strdup(name); |
259 | 0 | if (entry.name == NULL) |
260 | 0 | ok = 0; |
261 | 0 | } |
262 | 0 | if (ok && path != NULL) { |
263 | 0 | entry.path = OPENSSL_strdup(path); |
264 | 0 | if (entry.path == NULL) |
265 | 0 | ok = 0; |
266 | 0 | } |
267 | 0 | if (ok) |
268 | 0 | ok = provider_conf_params(NULL, &entry, NULL, value, cnf); |
269 | 0 | if (ok && (entry.path != NULL || entry.parameters != NULL)) |
270 | 0 | ok = ossl_provider_info_add_to_store(libctx, &entry); |
271 | 0 | if (!ok || (entry.path == NULL && entry.parameters == NULL)) { |
272 | 0 | ossl_provider_info_clear(&entry); |
273 | 0 | } |
274 | |
|
275 | 0 | } |
276 | | |
277 | | /* |
278 | | * Even if ok is 0, we still return success. Failure to load a provider is |
279 | | * not fatal. We want to continue to load the rest of the config file. |
280 | | */ |
281 | 0 | return 1; |
282 | 0 | } |
283 | | |
284 | | static int provider_conf_init(CONF_IMODULE *md, const CONF *cnf) |
285 | 0 | { |
286 | 0 | STACK_OF(CONF_VALUE) *elist; |
287 | 0 | CONF_VALUE *cval; |
288 | 0 | int i; |
289 | |
|
290 | 0 | OSSL_TRACE1(CONF, "Loading providers module: section %s\n", |
291 | 0 | CONF_imodule_get_value(md)); |
292 | | |
293 | | /* Value is a section containing PROVIDERs to configure */ |
294 | 0 | elist = NCONF_get_section(cnf, CONF_imodule_get_value(md)); |
295 | |
|
296 | 0 | if (!elist) { |
297 | 0 | ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_PROVIDER_SECTION_ERROR); |
298 | 0 | return 0; |
299 | 0 | } |
300 | | |
301 | 0 | for (i = 0; i < sk_CONF_VALUE_num(elist); i++) { |
302 | 0 | cval = sk_CONF_VALUE_value(elist, i); |
303 | 0 | if (!provider_conf_load(NCONF_get0_libctx((CONF *)cnf), |
304 | 0 | cval->name, cval->value, cnf)) |
305 | 0 | return 0; |
306 | 0 | } |
307 | | |
308 | 0 | return 1; |
309 | 0 | } |
310 | | |
311 | | void ossl_provider_add_conf_module(void) |
312 | 0 | { |
313 | 0 | OSSL_TRACE(CONF, "Adding config module 'providers'\n"); |
314 | 0 | CONF_module_add("providers", provider_conf_init, NULL); |
315 | 0 | } |