/src/openssl/crypto/conf/conf_mod.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2002-2025 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 "internal/cryptlib.h" |
11 | | #include "internal/rcu.h" |
12 | | #include <stdio.h> |
13 | | #include <ctype.h> |
14 | | #include <openssl/crypto.h> |
15 | | #include <openssl/conf.h> |
16 | | #include "internal/conf.h" |
17 | | #include <openssl/conf_api.h> |
18 | | #include "internal/dso.h" |
19 | | #include "internal/thread_once.h" |
20 | | #include <openssl/x509.h> |
21 | | #include <openssl/trace.h> |
22 | | #include "conf_local.h" |
23 | | |
24 | | DEFINE_STACK_OF(CONF_MODULE) |
25 | | DEFINE_STACK_OF(CONF_IMODULE) |
26 | | |
27 | 0 | #define DSO_mod_init_name "OPENSSL_init" |
28 | 0 | #define DSO_mod_finish_name "OPENSSL_finish" |
29 | | |
30 | | static CRYPTO_ONCE init_module_list_lock = CRYPTO_ONCE_STATIC_INIT; |
31 | | static CRYPTO_RCU_LOCK *module_list_lock = NULL; |
32 | | static STACK_OF(CONF_MODULE) *supported_modules = NULL; /* protected by lock */ |
33 | | static STACK_OF(CONF_IMODULE) *initialized_modules = NULL; /* protected by lock */ |
34 | | |
35 | | static CRYPTO_ONCE load_builtin_modules = CRYPTO_ONCE_STATIC_INIT; |
36 | | |
37 | | static void module_free(CONF_MODULE *md); |
38 | | static void module_finish(CONF_IMODULE *imod); |
39 | | static int module_run(const CONF *cnf, const char *name, const char *value, |
40 | | unsigned long flags); |
41 | | static CONF_MODULE *module_add(DSO *dso, const char *name, |
42 | | conf_init_func *ifunc, |
43 | | conf_finish_func *ffunc); |
44 | | static CONF_MODULE *module_find(const char *name); |
45 | | static int module_init(CONF_MODULE *pmod, const char *name, const char *value, |
46 | | const CONF *cnf); |
47 | | static CONF_MODULE *module_load_dso(const CONF *cnf, const char *name, |
48 | | const char *value); |
49 | | |
50 | | static int conf_modules_finish_int(void); |
51 | | |
52 | | static void module_lists_free(void) |
53 | 16 | { |
54 | 16 | ossl_rcu_lock_free(module_list_lock); |
55 | 16 | module_list_lock = NULL; |
56 | | |
57 | 16 | sk_CONF_MODULE_free(supported_modules); |
58 | 16 | supported_modules = NULL; |
59 | | |
60 | 16 | sk_CONF_IMODULE_free(initialized_modules); |
61 | 16 | initialized_modules = NULL; |
62 | 16 | } |
63 | | |
64 | | DEFINE_RUN_ONCE_STATIC(do_init_module_list_lock) |
65 | 16 | { |
66 | 16 | module_list_lock = ossl_rcu_lock_new(1, NULL); |
67 | 16 | if (module_list_lock == NULL) { |
68 | 0 | ERR_raise(ERR_LIB_CONF, ERR_R_CRYPTO_LIB); |
69 | 0 | return 0; |
70 | 0 | } |
71 | | |
72 | 16 | return 1; |
73 | 16 | } |
74 | | |
75 | | static int conf_diagnostics(const CONF *cnf) |
76 | 0 | { |
77 | 0 | int status; |
78 | 0 | long result = 0; |
79 | |
|
80 | 0 | ERR_set_mark(); |
81 | 0 | status = NCONF_get_number_e(cnf, NULL, "config_diagnostics", &result); |
82 | 0 | ERR_pop_to_mark(); |
83 | 0 | if (status > 0) { |
84 | 0 | OSSL_LIB_CTX_set_conf_diagnostics(cnf->libctx, result > 0); |
85 | 0 | return result > 0; |
86 | 0 | } |
87 | 0 | return OSSL_LIB_CTX_get_conf_diagnostics(cnf->libctx); |
88 | 0 | } |
89 | | |
90 | | /* Main function: load modules from a CONF structure */ |
91 | | |
92 | | int CONF_modules_load(const CONF *cnf, const char *appname, |
93 | | unsigned long flags) |
94 | 0 | { |
95 | 0 | STACK_OF(CONF_VALUE) *values; |
96 | 0 | CONF_VALUE *vl; |
97 | 0 | char *vsection = NULL; |
98 | 0 | int ret, i; |
99 | |
|
100 | 0 | if (!cnf) |
101 | 0 | return 1; |
102 | | |
103 | 0 | if (conf_diagnostics(cnf)) |
104 | 0 | flags &= ~(CONF_MFLAGS_IGNORE_ERRORS |
105 | 0 | | CONF_MFLAGS_IGNORE_RETURN_CODES |
106 | 0 | | CONF_MFLAGS_SILENT |
107 | 0 | | CONF_MFLAGS_IGNORE_MISSING_FILE); |
108 | |
|
109 | 0 | ERR_set_mark(); |
110 | 0 | if (appname) |
111 | 0 | vsection = NCONF_get_string(cnf, NULL, appname); |
112 | |
|
113 | 0 | if (!appname || (!vsection && (flags & CONF_MFLAGS_DEFAULT_SECTION))) |
114 | 0 | vsection = NCONF_get_string(cnf, NULL, "openssl_conf"); |
115 | |
|
116 | 0 | if (!vsection) { |
117 | 0 | ERR_pop_to_mark(); |
118 | 0 | return 1; |
119 | 0 | } |
120 | | |
121 | 0 | OSSL_TRACE1(CONF, "Configuration in section %s\n", vsection); |
122 | 0 | values = NCONF_get_section(cnf, vsection); |
123 | |
|
124 | 0 | if (values == NULL) { |
125 | 0 | if (!(flags & CONF_MFLAGS_SILENT)) { |
126 | 0 | ERR_clear_last_mark(); |
127 | 0 | ERR_raise_data(ERR_LIB_CONF, |
128 | 0 | CONF_R_OPENSSL_CONF_REFERENCES_MISSING_SECTION, |
129 | 0 | "openssl_conf=%s", vsection); |
130 | 0 | } else { |
131 | 0 | ERR_pop_to_mark(); |
132 | 0 | } |
133 | 0 | return 0; |
134 | 0 | } |
135 | 0 | ERR_pop_to_mark(); |
136 | |
|
137 | 0 | for (i = 0; i < sk_CONF_VALUE_num(values); i++) { |
138 | 0 | vl = sk_CONF_VALUE_value(values, i); |
139 | 0 | ERR_set_mark(); |
140 | 0 | ret = module_run(cnf, vl->name, vl->value, flags); |
141 | 0 | OSSL_TRACE3(CONF, "Running module %s (%s) returned %d\n", |
142 | 0 | vl->name, vl->value, ret); |
143 | 0 | if (ret <= 0) |
144 | 0 | if (!(flags & CONF_MFLAGS_IGNORE_ERRORS)) { |
145 | 0 | ERR_clear_last_mark(); |
146 | 0 | return ret; |
147 | 0 | } |
148 | 0 | ERR_pop_to_mark(); |
149 | 0 | } |
150 | | |
151 | 0 | return 1; |
152 | |
|
153 | 0 | } |
154 | | |
155 | | int CONF_modules_load_file_ex(OSSL_LIB_CTX *libctx, const char *filename, |
156 | | const char *appname, unsigned long flags) |
157 | 16 | { |
158 | 16 | char *file = NULL; |
159 | 16 | CONF *conf = NULL; |
160 | 16 | int ret = 0, diagnostics = OSSL_LIB_CTX_get_conf_diagnostics(libctx); |
161 | | |
162 | 16 | ERR_set_mark(); |
163 | | |
164 | 16 | if (filename == NULL) { |
165 | 16 | file = CONF_get1_default_config_file(); |
166 | 16 | if (file == NULL) |
167 | 0 | goto err; |
168 | 16 | if (*file == '\0') { |
169 | | /* Do not try to load an empty file name but do not error out */ |
170 | 0 | ret = 1; |
171 | 0 | goto err; |
172 | 0 | } |
173 | 16 | } else { |
174 | 0 | file = (char *)filename; |
175 | 0 | } |
176 | | |
177 | 16 | conf = NCONF_new_ex(libctx, NULL); |
178 | 16 | if (conf == NULL) |
179 | 0 | goto err; |
180 | | |
181 | 16 | if (NCONF_load(conf, file, NULL) <= 0) { |
182 | 16 | if ((flags & CONF_MFLAGS_IGNORE_MISSING_FILE) && |
183 | 16 | (ERR_GET_REASON(ERR_peek_last_error()) == CONF_R_NO_SUCH_FILE)) { |
184 | 16 | ret = 1; |
185 | 16 | } |
186 | 16 | goto err; |
187 | 16 | } |
188 | | |
189 | 0 | ret = CONF_modules_load(conf, appname, flags); |
190 | | /* CONF_modules_load() might change the diagnostics setting, reread it. */ |
191 | 0 | diagnostics = OSSL_LIB_CTX_get_conf_diagnostics(libctx); |
192 | |
|
193 | 16 | err: |
194 | 16 | if (filename == NULL) |
195 | 16 | OPENSSL_free(file); |
196 | 16 | NCONF_free(conf); |
197 | | |
198 | 16 | if ((flags & CONF_MFLAGS_IGNORE_RETURN_CODES) != 0 && !diagnostics) |
199 | 16 | ret = 1; |
200 | | |
201 | 16 | if (ret > 0) |
202 | 16 | ERR_pop_to_mark(); |
203 | 0 | else |
204 | 0 | ERR_clear_last_mark(); |
205 | | |
206 | 16 | return ret; |
207 | 0 | } |
208 | | |
209 | | int CONF_modules_load_file(const char *filename, |
210 | | const char *appname, unsigned long flags) |
211 | 0 | { |
212 | 0 | return CONF_modules_load_file_ex(NULL, filename, appname, flags); |
213 | 0 | } |
214 | | |
215 | | DEFINE_RUN_ONCE_STATIC(do_load_builtin_modules) |
216 | 0 | { |
217 | 0 | OPENSSL_load_builtin_modules(); |
218 | 0 | return 1; |
219 | 0 | } |
220 | | |
221 | | static int module_run(const CONF *cnf, const char *name, const char *value, |
222 | | unsigned long flags) |
223 | 0 | { |
224 | 0 | CONF_MODULE *md; |
225 | 0 | int ret; |
226 | |
|
227 | 0 | if (!RUN_ONCE(&load_builtin_modules, do_load_builtin_modules)) |
228 | 0 | return -1; |
229 | | |
230 | 0 | md = module_find(name); |
231 | | |
232 | | /* Module not found: try to load DSO */ |
233 | 0 | if (!md && !(flags & CONF_MFLAGS_NO_DSO)) |
234 | 0 | md = module_load_dso(cnf, name, value); |
235 | |
|
236 | 0 | if (!md) { |
237 | 0 | if (!(flags & CONF_MFLAGS_SILENT)) { |
238 | 0 | ERR_raise_data(ERR_LIB_CONF, CONF_R_UNKNOWN_MODULE_NAME, |
239 | 0 | "module=%s", name); |
240 | 0 | } |
241 | 0 | return -1; |
242 | 0 | } |
243 | | |
244 | 0 | ret = module_init(md, name, value, cnf); |
245 | |
|
246 | 0 | if (ret <= 0) { |
247 | 0 | if (!(flags & CONF_MFLAGS_SILENT)) |
248 | 0 | ERR_raise_data(ERR_LIB_CONF, CONF_R_MODULE_INITIALIZATION_ERROR, |
249 | 0 | "module=%s, value=%s retcode=%-8d", |
250 | 0 | name, value, ret); |
251 | 0 | } |
252 | |
|
253 | 0 | return ret; |
254 | 0 | } |
255 | | |
256 | | /* Load a module from a DSO */ |
257 | | static CONF_MODULE *module_load_dso(const CONF *cnf, |
258 | | const char *name, const char *value) |
259 | 0 | { |
260 | 0 | DSO *dso = NULL; |
261 | 0 | conf_init_func *ifunc; |
262 | 0 | conf_finish_func *ffunc; |
263 | 0 | const char *path = NULL; |
264 | 0 | int errcode = 0; |
265 | 0 | CONF_MODULE *md; |
266 | | |
267 | | /* Look for alternative path in module section */ |
268 | 0 | path = _CONF_get_string(cnf, value, "path"); |
269 | 0 | if (path == NULL) { |
270 | 0 | path = name; |
271 | 0 | } |
272 | 0 | dso = DSO_load(NULL, path, NULL, 0); |
273 | 0 | if (dso == NULL) { |
274 | 0 | errcode = CONF_R_ERROR_LOADING_DSO; |
275 | 0 | goto err; |
276 | 0 | } |
277 | 0 | ifunc = (conf_init_func *)DSO_bind_func(dso, DSO_mod_init_name); |
278 | 0 | if (ifunc == NULL) { |
279 | 0 | errcode = CONF_R_MISSING_INIT_FUNCTION; |
280 | 0 | goto err; |
281 | 0 | } |
282 | 0 | ffunc = (conf_finish_func *)DSO_bind_func(dso, DSO_mod_finish_name); |
283 | | /* All OK, add module */ |
284 | 0 | md = module_add(dso, name, ifunc, ffunc); |
285 | |
|
286 | 0 | if (md == NULL) |
287 | 0 | goto err; |
288 | | |
289 | 0 | return md; |
290 | | |
291 | 0 | err: |
292 | 0 | DSO_free(dso); |
293 | 0 | ERR_raise_data(ERR_LIB_CONF, errcode, "module=%s, path=%s", name, path); |
294 | 0 | return NULL; |
295 | 0 | } |
296 | | |
297 | | /* add module to list */ |
298 | | static CONF_MODULE *module_add(DSO *dso, const char *name, |
299 | | conf_init_func *ifunc, conf_finish_func *ffunc) |
300 | 0 | { |
301 | 0 | CONF_MODULE *tmod = NULL; |
302 | 0 | STACK_OF(CONF_MODULE) *old_modules; |
303 | 0 | STACK_OF(CONF_MODULE) *new_modules; |
304 | |
|
305 | 0 | if (!RUN_ONCE(&init_module_list_lock, do_init_module_list_lock)) |
306 | 0 | return NULL; |
307 | | |
308 | 0 | ossl_rcu_write_lock(module_list_lock); |
309 | |
|
310 | 0 | old_modules = ossl_rcu_deref(&supported_modules); |
311 | |
|
312 | 0 | if (old_modules == NULL) |
313 | 0 | new_modules = sk_CONF_MODULE_new_null(); |
314 | 0 | else |
315 | 0 | new_modules = sk_CONF_MODULE_dup(old_modules); |
316 | |
|
317 | 0 | if (new_modules == NULL) |
318 | 0 | goto err; |
319 | | |
320 | 0 | if ((tmod = OPENSSL_zalloc(sizeof(*tmod))) == NULL) |
321 | 0 | goto err; |
322 | | |
323 | 0 | tmod->dso = dso; |
324 | 0 | tmod->name = OPENSSL_strdup(name); |
325 | 0 | tmod->init = ifunc; |
326 | 0 | tmod->finish = ffunc; |
327 | 0 | if (tmod->name == NULL) |
328 | 0 | goto err; |
329 | | |
330 | 0 | if (!sk_CONF_MODULE_push(new_modules, tmod)) |
331 | 0 | goto err; |
332 | | |
333 | 0 | ossl_rcu_assign_ptr(&supported_modules, &new_modules); |
334 | 0 | ossl_rcu_write_unlock(module_list_lock); |
335 | 0 | ossl_synchronize_rcu(module_list_lock); |
336 | |
|
337 | 0 | sk_CONF_MODULE_free(old_modules); |
338 | 0 | return tmod; |
339 | | |
340 | 0 | err: |
341 | 0 | ossl_rcu_write_unlock(module_list_lock); |
342 | 0 | if (tmod != NULL) { |
343 | 0 | OPENSSL_free(tmod->name); |
344 | 0 | OPENSSL_free(tmod); |
345 | 0 | } |
346 | 0 | sk_CONF_MODULE_free(new_modules); |
347 | 0 | return NULL; |
348 | 0 | } |
349 | | |
350 | | /* |
351 | | * Find a module from the list. We allow module names of the form |
352 | | * modname.XXXX to just search for modname to allow the same module to be |
353 | | * initialized more than once. |
354 | | */ |
355 | | |
356 | | static CONF_MODULE *module_find(const char *name) |
357 | 0 | { |
358 | 0 | CONF_MODULE *tmod; |
359 | 0 | int i; |
360 | 0 | size_t nchar; |
361 | 0 | char *p; |
362 | 0 | STACK_OF(CONF_MODULE) *mods; |
363 | |
|
364 | 0 | p = strrchr(name, '.'); |
365 | |
|
366 | 0 | if (p) |
367 | 0 | nchar = p - name; |
368 | 0 | else |
369 | 0 | nchar = strlen(name); |
370 | |
|
371 | 0 | if (!RUN_ONCE(&init_module_list_lock, do_init_module_list_lock)) |
372 | 0 | return NULL; |
373 | | |
374 | 0 | if (!ossl_rcu_read_lock(module_list_lock)) |
375 | 0 | return NULL; |
376 | | |
377 | 0 | mods = ossl_rcu_deref(&supported_modules); |
378 | |
|
379 | 0 | for (i = 0; i < sk_CONF_MODULE_num(mods); i++) { |
380 | 0 | tmod = sk_CONF_MODULE_value(mods, i); |
381 | 0 | if (strncmp(tmod->name, name, nchar) == 0) { |
382 | 0 | ossl_rcu_read_unlock(module_list_lock); |
383 | 0 | return tmod; |
384 | 0 | } |
385 | 0 | } |
386 | | |
387 | 0 | ossl_rcu_read_unlock(module_list_lock); |
388 | 0 | return NULL; |
389 | 0 | } |
390 | | |
391 | | /* initialize a module */ |
392 | | static int module_init(CONF_MODULE *pmod, const char *name, const char *value, |
393 | | const CONF *cnf) |
394 | 0 | { |
395 | 0 | int ret = 1; |
396 | 0 | int init_called = 0; |
397 | 0 | CONF_IMODULE *imod = NULL; |
398 | 0 | STACK_OF(CONF_IMODULE) *old_modules; |
399 | 0 | STACK_OF(CONF_IMODULE) *new_modules; |
400 | | |
401 | | /* Otherwise add initialized module to list */ |
402 | 0 | imod = OPENSSL_malloc(sizeof(*imod)); |
403 | 0 | if (imod == NULL) |
404 | 0 | goto err; |
405 | | |
406 | 0 | imod->pmod = pmod; |
407 | 0 | imod->name = OPENSSL_strdup(name); |
408 | 0 | imod->value = OPENSSL_strdup(value); |
409 | 0 | imod->usr_data = NULL; |
410 | 0 | imod->libctx = NULL; |
411 | |
|
412 | 0 | if (!imod->name || !imod->value) |
413 | 0 | goto memerr; |
414 | | |
415 | | /* Try to initialize module */ |
416 | 0 | if (pmod->init) { |
417 | 0 | ret = pmod->init(imod, cnf); |
418 | 0 | init_called = 1; |
419 | | /* Error occurred, exit */ |
420 | 0 | if (ret <= 0) |
421 | 0 | goto err; |
422 | 0 | } |
423 | | |
424 | 0 | if (!RUN_ONCE(&init_module_list_lock, do_init_module_list_lock)) |
425 | 0 | goto err; |
426 | | |
427 | 0 | ossl_rcu_write_lock(module_list_lock); |
428 | |
|
429 | 0 | old_modules = ossl_rcu_deref(&initialized_modules); |
430 | |
|
431 | 0 | if (old_modules == NULL) |
432 | 0 | new_modules = sk_CONF_IMODULE_new_null(); |
433 | 0 | else |
434 | 0 | new_modules = sk_CONF_IMODULE_dup(old_modules); |
435 | |
|
436 | 0 | if (new_modules == NULL) { |
437 | 0 | ossl_rcu_write_unlock(module_list_lock); |
438 | 0 | ERR_raise(ERR_LIB_CONF, ERR_R_CRYPTO_LIB); |
439 | 0 | goto err; |
440 | 0 | } |
441 | | |
442 | 0 | if (!sk_CONF_IMODULE_push(new_modules, imod)) { |
443 | 0 | ossl_rcu_write_unlock(module_list_lock); |
444 | 0 | sk_CONF_IMODULE_free(new_modules); |
445 | 0 | ERR_raise(ERR_LIB_CONF, ERR_R_CRYPTO_LIB); |
446 | 0 | goto err; |
447 | 0 | } |
448 | | |
449 | 0 | pmod->links++; |
450 | |
|
451 | 0 | ossl_rcu_assign_ptr(&initialized_modules, &new_modules); |
452 | 0 | ossl_rcu_write_unlock(module_list_lock); |
453 | 0 | ossl_synchronize_rcu(module_list_lock); |
454 | 0 | sk_CONF_IMODULE_free(old_modules); |
455 | 0 | return ret; |
456 | | |
457 | 0 | err: |
458 | | |
459 | | /* We've started the module so we'd better finish it */ |
460 | 0 | if (pmod->finish && init_called) |
461 | 0 | pmod->finish(imod); |
462 | |
|
463 | 0 | memerr: |
464 | 0 | if (imod) { |
465 | 0 | OPENSSL_free(imod->name); |
466 | 0 | OPENSSL_free(imod->value); |
467 | 0 | OPENSSL_free(imod); |
468 | 0 | } |
469 | |
|
470 | 0 | return -1; |
471 | |
|
472 | 0 | } |
473 | | |
474 | | /* |
475 | | * Unload any dynamic modules that have a link count of zero: i.e. have no |
476 | | * active initialized modules. If 'all' is set then all modules are unloaded |
477 | | * including static ones. |
478 | | */ |
479 | | |
480 | | void CONF_modules_unload(int all) |
481 | 16 | { |
482 | 16 | int i; |
483 | 16 | CONF_MODULE *md; |
484 | 16 | STACK_OF(CONF_MODULE) *old_modules; |
485 | 16 | STACK_OF(CONF_MODULE) *new_modules; |
486 | 16 | STACK_OF(CONF_MODULE) *to_delete; |
487 | | |
488 | 16 | if (!conf_modules_finish_int()) /* also inits module list lock */ |
489 | 0 | return; |
490 | | |
491 | 16 | ossl_rcu_write_lock(module_list_lock); |
492 | | |
493 | 16 | old_modules = ossl_rcu_deref(&supported_modules); |
494 | 16 | new_modules = sk_CONF_MODULE_dup(old_modules); |
495 | | |
496 | 16 | if (new_modules == NULL) { |
497 | 0 | ossl_rcu_write_unlock(module_list_lock); |
498 | 0 | return; |
499 | 0 | } |
500 | | |
501 | 16 | to_delete = sk_CONF_MODULE_new_null(); |
502 | | |
503 | | /* unload modules in reverse order */ |
504 | 16 | for (i = sk_CONF_MODULE_num(new_modules) - 1; i >= 0; i--) { |
505 | 0 | md = sk_CONF_MODULE_value(new_modules, i); |
506 | | /* If static or in use and 'all' not set ignore it */ |
507 | 0 | if (((md->links > 0) || !md->dso) && !all) |
508 | 0 | continue; |
509 | | /* Since we're working in reverse this is OK */ |
510 | 0 | (void)sk_CONF_MODULE_delete(new_modules, i); |
511 | 0 | sk_CONF_MODULE_push(to_delete, md); |
512 | 0 | } |
513 | | |
514 | 16 | if (sk_CONF_MODULE_num(new_modules) == 0) { |
515 | 16 | sk_CONF_MODULE_free(new_modules); |
516 | 16 | new_modules = NULL; |
517 | 16 | } |
518 | | |
519 | 16 | ossl_rcu_assign_ptr(&supported_modules, &new_modules); |
520 | 16 | ossl_rcu_write_unlock(module_list_lock); |
521 | 16 | ossl_synchronize_rcu(module_list_lock); |
522 | 16 | sk_CONF_MODULE_free(old_modules); |
523 | 16 | sk_CONF_MODULE_pop_free(to_delete, module_free); |
524 | | |
525 | 16 | } |
526 | | |
527 | | /* unload a single module */ |
528 | | static void module_free(CONF_MODULE *md) |
529 | 0 | { |
530 | 0 | DSO_free(md->dso); |
531 | 0 | OPENSSL_free(md->name); |
532 | 0 | OPENSSL_free(md); |
533 | 0 | } |
534 | | |
535 | | /* finish and free up all modules instances */ |
536 | | |
537 | | static int conf_modules_finish_int(void) |
538 | 16 | { |
539 | 16 | CONF_IMODULE *imod; |
540 | 16 | STACK_OF(CONF_IMODULE) *old_modules; |
541 | 16 | STACK_OF(CONF_IMODULE) *new_modules = NULL; |
542 | | |
543 | 16 | if (!RUN_ONCE(&init_module_list_lock, do_init_module_list_lock)) |
544 | 0 | return 0; |
545 | | |
546 | | /* If module_list_lock is NULL here it means we were already unloaded */ |
547 | 16 | if (module_list_lock == NULL) |
548 | 0 | return 0; |
549 | | |
550 | 16 | ossl_rcu_write_lock(module_list_lock); |
551 | 16 | old_modules = ossl_rcu_deref(&initialized_modules); |
552 | 16 | ossl_rcu_assign_ptr(&initialized_modules, &new_modules); |
553 | 16 | ossl_rcu_write_unlock(module_list_lock); |
554 | 16 | ossl_synchronize_rcu(module_list_lock); |
555 | | |
556 | 16 | while (sk_CONF_IMODULE_num(old_modules) > 0) { |
557 | 0 | imod = sk_CONF_IMODULE_pop(old_modules); |
558 | 0 | module_finish(imod); |
559 | 0 | } |
560 | 16 | sk_CONF_IMODULE_free(old_modules); |
561 | | |
562 | 16 | return 1; |
563 | 16 | } |
564 | | |
565 | | void CONF_modules_finish(void) |
566 | 0 | { |
567 | 0 | conf_modules_finish_int(); |
568 | 0 | } |
569 | | |
570 | | /* finish a module instance */ |
571 | | |
572 | | static void module_finish(CONF_IMODULE *imod) |
573 | 0 | { |
574 | 0 | if (!imod) |
575 | 0 | return; |
576 | 0 | if (imod->pmod->finish) |
577 | 0 | imod->pmod->finish(imod); |
578 | 0 | imod->pmod->links--; |
579 | 0 | OPENSSL_free(imod->name); |
580 | 0 | OPENSSL_free(imod->value); |
581 | 0 | OPENSSL_free(imod); |
582 | 0 | } |
583 | | |
584 | | /* Add a static module to OpenSSL */ |
585 | | |
586 | | int CONF_module_add(const char *name, conf_init_func *ifunc, |
587 | | conf_finish_func *ffunc) |
588 | 0 | { |
589 | 0 | if (module_add(NULL, name, ifunc, ffunc)) |
590 | 0 | return 1; |
591 | 0 | else |
592 | 0 | return 0; |
593 | 0 | } |
594 | | |
595 | | void ossl_config_modules_free(void) |
596 | 16 | { |
597 | 16 | CONF_modules_unload(1); /* calls CONF_modules_finish */ |
598 | 16 | module_lists_free(); |
599 | 16 | } |
600 | | |
601 | | /* Utility functions */ |
602 | | |
603 | | const char *CONF_imodule_get_name(const CONF_IMODULE *md) |
604 | 0 | { |
605 | 0 | return md->name; |
606 | 0 | } |
607 | | |
608 | | const char *CONF_imodule_get_value(const CONF_IMODULE *md) |
609 | 0 | { |
610 | 0 | return md->value; |
611 | 0 | } |
612 | | |
613 | | void *CONF_imodule_get_usr_data(const CONF_IMODULE *md) |
614 | 0 | { |
615 | 0 | return md->usr_data; |
616 | 0 | } |
617 | | |
618 | | void CONF_imodule_set_usr_data(CONF_IMODULE *md, void *usr_data) |
619 | 0 | { |
620 | 0 | md->usr_data = usr_data; |
621 | 0 | } |
622 | | |
623 | | CONF_MODULE *CONF_imodule_get_module(const CONF_IMODULE *md) |
624 | 0 | { |
625 | 0 | return md->pmod; |
626 | 0 | } |
627 | | |
628 | | unsigned long CONF_imodule_get_flags(const CONF_IMODULE *md) |
629 | 0 | { |
630 | 0 | return md->flags; |
631 | 0 | } |
632 | | |
633 | | void CONF_imodule_set_flags(CONF_IMODULE *md, unsigned long flags) |
634 | 0 | { |
635 | 0 | md->flags = flags; |
636 | 0 | } |
637 | | |
638 | | void *CONF_module_get_usr_data(CONF_MODULE *pmod) |
639 | 0 | { |
640 | 0 | return pmod->usr_data; |
641 | 0 | } |
642 | | |
643 | | void CONF_module_set_usr_data(CONF_MODULE *pmod, void *usr_data) |
644 | 0 | { |
645 | 0 | pmod->usr_data = usr_data; |
646 | 0 | } |
647 | | |
648 | | /* Return default config file name */ |
649 | | char *CONF_get1_default_config_file(void) |
650 | 16 | { |
651 | 16 | const char *t; |
652 | 16 | char *file, *sep = ""; |
653 | 16 | size_t size; |
654 | | |
655 | 16 | if ((file = ossl_safe_getenv("OPENSSL_CONF")) != NULL) |
656 | 0 | return OPENSSL_strdup(file); |
657 | | |
658 | 16 | t = X509_get_default_cert_area(); |
659 | | /* |
660 | | * On windows systems with -DOSSL_WINCTX set, if the needed registry |
661 | | * keys are not yet set, openssl applets will return, due to an inability |
662 | | * to locate various directories, like the default cert area. In that |
663 | | * event, clone an empty string here, so that commands like openssl version |
664 | | * continue to operate properly without needing to set OPENSSL_CONF. |
665 | | * Applets like cms will fail gracefully later when they try to parse an |
666 | | * empty config file |
667 | | */ |
668 | 16 | if (t == NULL) |
669 | 0 | return OPENSSL_strdup(""); |
670 | | |
671 | 16 | #ifndef OPENSSL_SYS_VMS |
672 | 16 | sep = "/"; |
673 | 16 | #endif |
674 | 16 | size = strlen(t) + strlen(sep) + strlen(OPENSSL_CONF) + 1; |
675 | 16 | file = OPENSSL_malloc(size); |
676 | | |
677 | 16 | if (file == NULL) |
678 | 0 | return NULL; |
679 | 16 | BIO_snprintf(file, size, "%s%s%s", t, sep, OPENSSL_CONF); |
680 | | |
681 | 16 | return file; |
682 | 16 | } |
683 | | |
684 | | /* |
685 | | * This function takes a list separated by 'sep' and calls the callback |
686 | | * function giving the start and length of each member optionally stripping |
687 | | * leading and trailing whitespace. This can be used to parse comma separated |
688 | | * lists for example. |
689 | | */ |
690 | | |
691 | | int CONF_parse_list(const char *list_, int sep, int nospc, |
692 | | int (*list_cb) (const char *elem, int len, void *usr), |
693 | | void *arg) |
694 | 0 | { |
695 | 0 | int ret; |
696 | 0 | const char *lstart, *tmpend, *p; |
697 | |
|
698 | 0 | if (list_ == NULL) { |
699 | 0 | ERR_raise(ERR_LIB_CONF, CONF_R_LIST_CANNOT_BE_NULL); |
700 | 0 | return 0; |
701 | 0 | } |
702 | | |
703 | 0 | lstart = list_; |
704 | 0 | for (;;) { |
705 | 0 | if (nospc) { |
706 | 0 | while (*lstart && isspace((unsigned char)*lstart)) |
707 | 0 | lstart++; |
708 | 0 | } |
709 | 0 | p = strchr(lstart, sep); |
710 | 0 | if (p == lstart || *lstart == '\0') |
711 | 0 | ret = list_cb(NULL, 0, arg); |
712 | 0 | else { |
713 | 0 | if (p) |
714 | 0 | tmpend = p - 1; |
715 | 0 | else |
716 | 0 | tmpend = lstart + strlen(lstart) - 1; |
717 | 0 | if (nospc) { |
718 | 0 | while (isspace((unsigned char)*tmpend)) |
719 | 0 | tmpend--; |
720 | 0 | } |
721 | 0 | ret = list_cb(lstart, (int)(tmpend - lstart + 1), arg); |
722 | 0 | } |
723 | 0 | if (ret <= 0) |
724 | 0 | return ret; |
725 | 0 | if (p == NULL) |
726 | 0 | return 1; |
727 | 0 | lstart = p + 1; |
728 | 0 | } |
729 | 0 | } |