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