2 * Copyright 2002-2023 The OpenSSL Project Authors. All Rights Reserved.
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
10 /* We need to use some engine deprecated APIs */
11 #define OPENSSL_SUPPRESS_DEPRECATED
13 #include "internal/cryptlib.h"
14 #include "internal/rcu.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"
27 DEFINE_STACK_OF(CONF_MODULE)
28 DEFINE_STACK_OF(CONF_IMODULE)
30 #define DSO_mod_init_name "OPENSSL_init"
31 #define DSO_mod_finish_name "OPENSSL_finish"
34 * This structure contains a data about supported modules. entries in this
35 * table correspond to either dynamic or static modules.
38 struct conf_module_st {
39 /* DSO of this module or NULL if static */
41 /* Name of the module */
46 conf_finish_func *finish;
47 /* Number of successfully initialized modules */
53 * This structure contains information about modules that have been
54 * successfully initialized. There may be more than one entry for a given
58 struct conf_imodule_st {
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 */
71 static CRYPTO_ONCE load_builtin_modules = CRYPTO_ONCE_STATIC_INIT;
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,
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,
83 static CONF_MODULE *module_load_dso(const CONF *cnf, const char *name,
86 static int conf_modules_finish_int(void);
88 static void module_lists_free(void)
90 ossl_rcu_lock_free(module_list_lock);
91 module_list_lock = NULL;
93 sk_CONF_MODULE_free(supported_modules);
94 supported_modules = NULL;
96 sk_CONF_IMODULE_free(initialized_modules);
97 initialized_modules = NULL;
100 DEFINE_RUN_ONCE_STATIC(do_init_module_list_lock)
102 module_list_lock = ossl_rcu_lock_new(1);
103 if (module_list_lock == NULL) {
104 ERR_raise(ERR_LIB_CONF, ERR_R_CRYPTO_LIB);
111 static int conf_diagnostics(const CONF *cnf)
113 return _CONF_get_number(cnf, NULL, "config_diagnostics") != 0;
116 /* Main function: load modules from a CONF structure */
118 int CONF_modules_load(const CONF *cnf, const char *appname,
121 STACK_OF(CONF_VALUE) *values;
123 char *vsection = NULL;
129 if (conf_diagnostics(cnf))
130 flags &= ~(CONF_MFLAGS_IGNORE_ERRORS
131 | CONF_MFLAGS_IGNORE_RETURN_CODES
133 | CONF_MFLAGS_IGNORE_MISSING_FILE);
137 vsection = NCONF_get_string(cnf, NULL, appname);
139 if (!appname || (!vsection && (flags & CONF_MFLAGS_DEFAULT_SECTION)))
140 vsection = NCONF_get_string(cnf, NULL, "openssl_conf");
147 OSSL_TRACE1(CONF, "Configuration in section %s\n", vsection);
148 values = NCONF_get_section(cnf, vsection);
150 if (values == NULL) {
151 if (!(flags & CONF_MFLAGS_SILENT)) {
152 ERR_clear_last_mark();
153 ERR_raise_data(ERR_LIB_CONF,
154 CONF_R_OPENSSL_CONF_REFERENCES_MISSING_SECTION,
155 "openssl_conf=%s", vsection);
163 for (i = 0; i < sk_CONF_VALUE_num(values); i++) {
164 vl = sk_CONF_VALUE_value(values, i);
166 ret = module_run(cnf, vl->name, vl->value, flags);
167 OSSL_TRACE3(CONF, "Running module %s (%s) returned %d\n",
168 vl->name, vl->value, ret);
170 if (!(flags & CONF_MFLAGS_IGNORE_ERRORS)) {
171 ERR_clear_last_mark();
181 int CONF_modules_load_file_ex(OSSL_LIB_CTX *libctx, const char *filename,
182 const char *appname, unsigned long flags)
186 int ret = 0, diagnostics = 0;
190 if (filename == NULL) {
191 file = CONF_get1_default_config_file();
195 /* Do not try to load an empty file name but do not error out */
200 file = (char *)filename;
203 conf = NCONF_new_ex(libctx, NULL);
207 if (NCONF_load(conf, file, NULL) <= 0) {
208 if ((flags & CONF_MFLAGS_IGNORE_MISSING_FILE) &&
209 (ERR_GET_REASON(ERR_peek_last_error()) == CONF_R_NO_SUCH_FILE)) {
215 ret = CONF_modules_load(conf, appname, flags);
216 diagnostics = conf_diagnostics(conf);
219 if (filename == NULL)
223 if ((flags & CONF_MFLAGS_IGNORE_RETURN_CODES) != 0 && !diagnostics)
229 ERR_clear_last_mark();
234 int CONF_modules_load_file(const char *filename,
235 const char *appname, unsigned long flags)
237 return CONF_modules_load_file_ex(NULL, filename, appname, flags);
240 DEFINE_RUN_ONCE_STATIC(do_load_builtin_modules)
242 OPENSSL_load_builtin_modules();
243 #ifndef OPENSSL_NO_ENGINE
244 /* Need to load ENGINEs */
245 ENGINE_load_builtin_engines();
250 static int module_run(const CONF *cnf, const char *name, const char *value,
256 if (!RUN_ONCE(&load_builtin_modules, do_load_builtin_modules))
259 md = module_find(name);
261 /* Module not found: try to load DSO */
262 if (!md && !(flags & CONF_MFLAGS_NO_DSO))
263 md = module_load_dso(cnf, name, value);
266 if (!(flags & CONF_MFLAGS_SILENT)) {
267 ERR_raise_data(ERR_LIB_CONF, CONF_R_UNKNOWN_MODULE_NAME,
273 ret = module_init(md, name, value, cnf);
276 if (!(flags & CONF_MFLAGS_SILENT))
277 ERR_raise_data(ERR_LIB_CONF, CONF_R_MODULE_INITIALIZATION_ERROR,
278 "module=%s, value=%s retcode=%-8d",
285 /* Load a module from a DSO */
286 static CONF_MODULE *module_load_dso(const CONF *cnf,
287 const char *name, const char *value)
290 conf_init_func *ifunc;
291 conf_finish_func *ffunc;
292 const char *path = NULL;
296 /* Look for alternative path in module section */
297 path = _CONF_get_string(cnf, value, "path");
301 dso = DSO_load(NULL, path, NULL, 0);
303 errcode = CONF_R_ERROR_LOADING_DSO;
306 ifunc = (conf_init_func *)DSO_bind_func(dso, DSO_mod_init_name);
308 errcode = CONF_R_MISSING_INIT_FUNCTION;
311 ffunc = (conf_finish_func *)DSO_bind_func(dso, DSO_mod_finish_name);
312 /* All OK, add module */
313 md = module_add(dso, name, ifunc, ffunc);
322 ERR_raise_data(ERR_LIB_CONF, errcode, "module=%s, path=%s", name, path);
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)
330 CONF_MODULE *tmod = NULL;
331 STACK_OF(CONF_MODULE) *old_modules;
332 STACK_OF(CONF_MODULE) *new_modules;
334 if (!RUN_ONCE(&init_module_list_lock, do_init_module_list_lock))
337 ossl_rcu_write_lock(module_list_lock);
339 old_modules = ossl_rcu_deref(&supported_modules);
341 if (old_modules == NULL)
342 new_modules = sk_CONF_MODULE_new_null();
344 new_modules = sk_CONF_MODULE_dup(old_modules);
346 if (new_modules == NULL)
349 if ((tmod = OPENSSL_zalloc(sizeof(*tmod))) == NULL)
353 tmod->name = OPENSSL_strdup(name);
355 tmod->finish = ffunc;
356 if (tmod->name == NULL)
359 if (!sk_CONF_MODULE_push(new_modules, tmod))
362 ossl_rcu_assign_ptr(&supported_modules, &new_modules);
363 ossl_rcu_write_unlock(module_list_lock);
364 ossl_synchronize_rcu(module_list_lock);
366 sk_CONF_MODULE_free(old_modules);
370 ossl_rcu_write_unlock(module_list_lock);
372 OPENSSL_free(tmod->name);
375 sk_CONF_MODULE_free(new_modules);
380 * Find a module from the list. We allow module names of the form
381 * modname.XXXX to just search for modname to allow the same module to be
382 * initialized more than once.
385 static CONF_MODULE *module_find(const char *name)
390 STACK_OF(CONF_MODULE) *mods;
392 p = strrchr(name, '.');
397 nchar = strlen(name);
399 if (!RUN_ONCE(&init_module_list_lock, do_init_module_list_lock))
402 ossl_rcu_read_lock(module_list_lock);
403 mods = ossl_rcu_deref(&supported_modules);
405 for (i = 0; i < sk_CONF_MODULE_num(mods); i++) {
406 tmod = sk_CONF_MODULE_value(mods, i);
407 if (strncmp(tmod->name, name, nchar) == 0) {
408 ossl_rcu_read_unlock(module_list_lock);
413 ossl_rcu_read_unlock(module_list_lock);
417 /* initialize a module */
418 static int module_init(CONF_MODULE *pmod, const char *name, const char *value,
423 CONF_IMODULE *imod = NULL;
424 STACK_OF(CONF_IMODULE) *old_modules;
425 STACK_OF(CONF_IMODULE) *new_modules;
427 /* Otherwise add initialized module to list */
428 imod = OPENSSL_malloc(sizeof(*imod));
433 imod->name = OPENSSL_strdup(name);
434 imod->value = OPENSSL_strdup(value);
435 imod->usr_data = NULL;
437 if (!imod->name || !imod->value)
440 /* Try to initialize module */
442 ret = pmod->init(imod, cnf);
444 /* Error occurred, exit */
449 if (!RUN_ONCE(&init_module_list_lock, do_init_module_list_lock))
452 ossl_rcu_write_lock(module_list_lock);
454 old_modules = ossl_rcu_deref(&initialized_modules);
456 if (old_modules == NULL)
457 new_modules = sk_CONF_IMODULE_new_null();
459 new_modules = sk_CONF_IMODULE_dup(old_modules);
461 if (new_modules == NULL) {
462 ossl_rcu_write_unlock(module_list_lock);
463 ERR_raise(ERR_LIB_CONF, ERR_R_CRYPTO_LIB);
467 if (!sk_CONF_IMODULE_push(new_modules, imod)) {
468 ossl_rcu_write_unlock(module_list_lock);
469 ERR_raise(ERR_LIB_CONF, ERR_R_CRYPTO_LIB);
475 ossl_rcu_assign_ptr(&initialized_modules, &new_modules);
476 ossl_rcu_write_unlock(module_list_lock);
477 ossl_synchronize_rcu(module_list_lock);
478 sk_CONF_IMODULE_free(old_modules);
483 /* We've started the module so we'd better finish it */
484 if (pmod->finish && init_called)
489 OPENSSL_free(imod->name);
490 OPENSSL_free(imod->value);
499 * Unload any dynamic modules that have a link count of zero: i.e. have no
500 * active initialized modules. If 'all' is set then all modules are unloaded
501 * including static ones.
504 void CONF_modules_unload(int all)
508 STACK_OF(CONF_MODULE) *old_modules;
509 STACK_OF(CONF_MODULE) *new_modules;
510 STACK_OF(CONF_MODULE) *to_delete;
512 if (!conf_modules_finish_int()) /* also inits module list lock */
515 ossl_rcu_write_lock(module_list_lock);
517 old_modules = ossl_rcu_deref(&supported_modules);
518 new_modules = sk_CONF_MODULE_dup(old_modules);
519 to_delete = sk_CONF_MODULE_new_null();
521 if (new_modules == NULL) {
522 ossl_rcu_write_unlock(module_list_lock);
526 /* unload modules in reverse order */
527 for (i = sk_CONF_MODULE_num(new_modules) - 1; i >= 0; i--) {
528 md = sk_CONF_MODULE_value(new_modules, i);
529 /* If static or in use and 'all' not set ignore it */
530 if (((md->links > 0) || !md->dso) && !all)
532 /* Since we're working in reverse this is OK */
533 (void)sk_CONF_MODULE_delete(new_modules, i);
534 sk_CONF_MODULE_push(to_delete, md);
537 if (sk_CONF_MODULE_num(new_modules) == 0) {
538 sk_CONF_MODULE_free(new_modules);
542 ossl_rcu_assign_ptr(&supported_modules, &new_modules);
543 ossl_rcu_write_unlock(module_list_lock);
544 ossl_synchronize_rcu(module_list_lock);
545 sk_CONF_MODULE_free(old_modules);
546 sk_CONF_MODULE_pop_free(to_delete, module_free);
550 /* unload a single module */
551 static void module_free(CONF_MODULE *md)
554 OPENSSL_free(md->name);
558 /* finish and free up all modules instances */
560 static int conf_modules_finish_int(void)
563 STACK_OF(CONF_IMODULE) *old_modules;
564 STACK_OF(CONF_IMODULE) *new_modules = NULL;
566 if (!RUN_ONCE(&init_module_list_lock, do_init_module_list_lock))
569 /* If module_list_lock is NULL here it means we were already unloaded */
570 if (module_list_lock == NULL)
573 ossl_rcu_write_lock(module_list_lock);
574 old_modules = ossl_rcu_deref(&initialized_modules);
575 ossl_rcu_assign_ptr(&initialized_modules, &new_modules);
576 ossl_rcu_write_unlock(module_list_lock);
577 ossl_synchronize_rcu(module_list_lock);
579 while (sk_CONF_IMODULE_num(old_modules) > 0) {
580 imod = sk_CONF_IMODULE_pop(old_modules);
583 sk_CONF_IMODULE_free(old_modules);
588 void CONF_modules_finish(void)
590 conf_modules_finish_int();
593 /* finish a module instance */
595 static void module_finish(CONF_IMODULE *imod)
599 if (imod->pmod->finish)
600 imod->pmod->finish(imod);
602 OPENSSL_free(imod->name);
603 OPENSSL_free(imod->value);
607 /* Add a static module to OpenSSL */
609 int CONF_module_add(const char *name, conf_init_func *ifunc,
610 conf_finish_func *ffunc)
612 if (module_add(NULL, name, ifunc, ffunc))
618 void ossl_config_modules_free(void)
620 CONF_modules_unload(1); /* calls CONF_modules_finish */
624 /* Utility functions */
626 const char *CONF_imodule_get_name(const CONF_IMODULE *md)
631 const char *CONF_imodule_get_value(const CONF_IMODULE *md)
636 void *CONF_imodule_get_usr_data(const CONF_IMODULE *md)
641 void CONF_imodule_set_usr_data(CONF_IMODULE *md, void *usr_data)
643 md->usr_data = usr_data;
646 CONF_MODULE *CONF_imodule_get_module(const CONF_IMODULE *md)
651 unsigned long CONF_imodule_get_flags(const CONF_IMODULE *md)
656 void CONF_imodule_set_flags(CONF_IMODULE *md, unsigned long flags)
661 void *CONF_module_get_usr_data(CONF_MODULE *pmod)
663 return pmod->usr_data;
666 void CONF_module_set_usr_data(CONF_MODULE *pmod, void *usr_data)
668 pmod->usr_data = usr_data;
671 /* Return default config file name */
672 char *CONF_get1_default_config_file(void)
675 char *file, *sep = "";
678 if ((file = ossl_safe_getenv("OPENSSL_CONF")) != NULL)
679 return OPENSSL_strdup(file);
681 t = X509_get_default_cert_area();
682 #ifndef OPENSSL_SYS_VMS
685 size = strlen(t) + strlen(sep) + strlen(OPENSSL_CONF) + 1;
686 file = OPENSSL_malloc(size);
690 BIO_snprintf(file, size, "%s%s%s", t, sep, OPENSSL_CONF);
696 * This function takes a list separated by 'sep' and calls the callback
697 * function giving the start and length of each member optionally stripping
698 * leading and trailing whitespace. This can be used to parse comma separated
702 int CONF_parse_list(const char *list_, int sep, int nospc,
703 int (*list_cb) (const char *elem, int len, void *usr),
707 const char *lstart, *tmpend, *p;
710 ERR_raise(ERR_LIB_CONF, CONF_R_LIST_CANNOT_BE_NULL);
717 while (*lstart && isspace((unsigned char)*lstart))
720 p = strchr(lstart, sep);
721 if (p == lstart || *lstart == '\0')
722 ret = list_cb(NULL, 0, arg);
727 tmpend = lstart + strlen(lstart) - 1;
729 while (isspace((unsigned char)*tmpend))
732 ret = list_cb(lstart, tmpend - lstart + 1, arg);