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