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