2 * Copyright 2002-2020 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"
16 #include <openssl/crypto.h>
17 #include "internal/conf.h"
18 #include "internal/dso.h"
19 #include "internal/thread_once.h"
20 #include <openssl/x509.h>
21 #include <openssl/trace.h>
22 #include <openssl/engine.h>
24 DEFINE_STACK_OF(CONF_MODULE)
25 DEFINE_STACK_OF(CONF_IMODULE)
27 #define DSO_mod_init_name "OPENSSL_init"
28 #define DSO_mod_finish_name "OPENSSL_finish"
31 * This structure contains a data about supported modules. entries in this
32 * table correspond to either dynamic or static modules.
35 struct conf_module_st {
36 /* DSO of this module or NULL if static */
38 /* Name of the module */
43 conf_finish_func *finish;
44 /* Number of successfully initialized modules */
50 * This structure contains information about modules that have been
51 * successfully initialized. There may be more than one entry for a given
55 struct conf_imodule_st {
63 static STACK_OF(CONF_MODULE) *supported_modules = NULL;
64 static STACK_OF(CONF_IMODULE) *initialized_modules = NULL;
66 static CRYPTO_ONCE load_builtin_modules = CRYPTO_ONCE_STATIC_INIT;
68 static void module_free(CONF_MODULE *md);
69 static void module_finish(CONF_IMODULE *imod);
70 static int module_run(const CONF *cnf, const char *name, const char *value,
72 static CONF_MODULE *module_add(DSO *dso, const char *name,
73 conf_init_func *ifunc,
74 conf_finish_func *ffunc);
75 static CONF_MODULE *module_find(const char *name);
76 static int module_init(CONF_MODULE *pmod, const char *name, const char *value,
78 static CONF_MODULE *module_load_dso(const CONF *cnf, const char *name,
81 static int conf_diagnostics(const CONF *cnf)
87 res = NCONF_get_number(cnf, NULL, "config_diagnostics", &lflag)
93 /* Main function: load modules from a CONF structure */
95 int CONF_modules_load(const CONF *cnf, const char *appname,
98 STACK_OF(CONF_VALUE) *values;
100 char *vsection = NULL;
106 if (conf_diagnostics(cnf))
107 flags &= ~(CONF_MFLAGS_IGNORE_ERRORS
108 | CONF_MFLAGS_IGNORE_RETURN_CODES
110 | CONF_MFLAGS_IGNORE_MISSING_FILE);
113 vsection = NCONF_get_string(cnf, NULL, appname);
115 if (!appname || (!vsection && (flags & CONF_MFLAGS_DEFAULT_SECTION)))
116 vsection = NCONF_get_string(cnf, NULL, "openssl_conf");
123 OSSL_TRACE1(CONF, "Configuration in section %s\n", vsection);
124 values = NCONF_get_section(cnf, vsection);
126 if (values == NULL) {
127 if (!(flags & CONF_MFLAGS_SILENT)) {
128 CONFerr(0, CONF_R_OPENSSL_CONF_REFERENCES_MISSING_SECTION);
129 ERR_add_error_data(2, "openssl_conf=", vsection);
134 for (i = 0; i < sk_CONF_VALUE_num(values); i++) {
135 vl = sk_CONF_VALUE_value(values, i);
136 ret = module_run(cnf, vl->name, vl->value, flags);
137 OSSL_TRACE3(CONF, "Running module %s (%s) returned %d\n",
138 vl->name, vl->value, ret);
140 if (!(flags & CONF_MFLAGS_IGNORE_ERRORS))
148 int CONF_modules_load_file_ex(OPENSSL_CTX *libctx, const char *filename,
149 const char *appname, unsigned long flags)
153 int ret = 0, diagnostics = 0;
155 conf = NCONF_new_ex(libctx, NULL);
159 if (filename == NULL) {
160 file = CONF_get1_default_config_file();
164 file = (char *)filename;
167 if (NCONF_load(conf, file, NULL) <= 0) {
168 if ((flags & CONF_MFLAGS_IGNORE_MISSING_FILE) &&
169 (ERR_GET_REASON(ERR_peek_last_error()) == CONF_R_NO_SUCH_FILE)) {
176 ret = CONF_modules_load(conf, appname, flags);
177 diagnostics = conf_diagnostics(conf);
180 if (filename == NULL)
184 if ((flags & CONF_MFLAGS_IGNORE_RETURN_CODES) != 0 && !diagnostics)
190 int CONF_modules_load_file(const char *filename,
191 const char *appname, unsigned long flags)
193 return CONF_modules_load_file_ex(NULL, filename, appname, flags);
196 DEFINE_RUN_ONCE_STATIC(do_load_builtin_modules)
198 OPENSSL_load_builtin_modules();
199 #ifndef OPENSSL_NO_ENGINE
200 /* Need to load ENGINEs */
201 ENGINE_load_builtin_engines();
207 static int module_run(const CONF *cnf, const char *name, const char *value,
213 if (!RUN_ONCE(&load_builtin_modules, do_load_builtin_modules))
216 md = module_find(name);
218 /* Module not found: try to load DSO */
219 if (!md && !(flags & CONF_MFLAGS_NO_DSO))
220 md = module_load_dso(cnf, name, value);
223 if (!(flags & CONF_MFLAGS_SILENT)) {
224 CONFerr(CONF_F_MODULE_RUN, CONF_R_UNKNOWN_MODULE_NAME);
225 ERR_add_error_data(2, "module=", name);
230 ret = module_init(md, name, value, cnf);
233 if (!(flags & CONF_MFLAGS_SILENT)) {
234 char rcode[DECIMAL_SIZE(ret) + 1];
236 CONFerr(CONF_F_MODULE_RUN, CONF_R_MODULE_INITIALIZATION_ERROR);
237 BIO_snprintf(rcode, sizeof(rcode), "%-8d", ret);
238 ERR_add_error_data(6, "module=", name, ", value=", value,
239 ", retcode=", rcode);
246 /* Load a module from a DSO */
247 static CONF_MODULE *module_load_dso(const CONF *cnf,
248 const char *name, const char *value)
251 conf_init_func *ifunc;
252 conf_finish_func *ffunc;
253 const char *path = NULL;
257 /* Look for alternative path in module section */
258 path = NCONF_get_string(cnf, value, "path");
263 dso = DSO_load(NULL, path, NULL, 0);
265 errcode = CONF_R_ERROR_LOADING_DSO;
268 ifunc = (conf_init_func *)DSO_bind_func(dso, DSO_mod_init_name);
270 errcode = CONF_R_MISSING_INIT_FUNCTION;
273 ffunc = (conf_finish_func *)DSO_bind_func(dso, DSO_mod_finish_name);
274 /* All OK, add module */
275 md = module_add(dso, name, ifunc, ffunc);
284 CONFerr(CONF_F_MODULE_LOAD_DSO, errcode);
285 ERR_add_error_data(4, "module=", name, ", path=", path);
289 /* add module to list */
290 static CONF_MODULE *module_add(DSO *dso, const char *name,
291 conf_init_func *ifunc, conf_finish_func *ffunc)
293 CONF_MODULE *tmod = NULL;
294 if (supported_modules == NULL)
295 supported_modules = sk_CONF_MODULE_new_null();
296 if (supported_modules == NULL)
298 if ((tmod = OPENSSL_zalloc(sizeof(*tmod))) == NULL) {
299 CONFerr(CONF_F_MODULE_ADD, ERR_R_MALLOC_FAILURE);
304 tmod->name = OPENSSL_strdup(name);
306 tmod->finish = ffunc;
307 if (tmod->name == NULL) {
312 if (!sk_CONF_MODULE_push(supported_modules, tmod)) {
313 OPENSSL_free(tmod->name);
322 * Find a module from the list. We allow module names of the form
323 * modname.XXXX to just search for modname to allow the same module to be
324 * initialized more than once.
327 static CONF_MODULE *module_find(const char *name)
332 p = strrchr(name, '.');
337 nchar = strlen(name);
339 for (i = 0; i < sk_CONF_MODULE_num(supported_modules); i++) {
340 tmod = sk_CONF_MODULE_value(supported_modules, i);
341 if (strncmp(tmod->name, name, nchar) == 0)
349 /* initialize a module */
350 static int module_init(CONF_MODULE *pmod, const char *name, const char *value,
355 CONF_IMODULE *imod = NULL;
357 /* Otherwise add initialized module to list */
358 imod = OPENSSL_malloc(sizeof(*imod));
363 imod->name = OPENSSL_strdup(name);
364 imod->value = OPENSSL_strdup(value);
365 imod->usr_data = NULL;
367 if (!imod->name || !imod->value)
370 /* Try to initialize module */
372 ret = pmod->init(imod, cnf);
374 /* Error occurred, exit */
379 if (initialized_modules == NULL) {
380 initialized_modules = sk_CONF_IMODULE_new_null();
381 if (!initialized_modules) {
382 CONFerr(CONF_F_MODULE_INIT, ERR_R_MALLOC_FAILURE);
387 if (!sk_CONF_IMODULE_push(initialized_modules, imod)) {
388 CONFerr(CONF_F_MODULE_INIT, ERR_R_MALLOC_FAILURE);
398 /* We've started the module so we'd better finish it */
399 if (pmod->finish && init_called)
404 OPENSSL_free(imod->name);
405 OPENSSL_free(imod->value);
414 * Unload any dynamic modules that have a link count of zero: i.e. have no
415 * active initialized modules. If 'all' is set then all modules are unloaded
416 * including static ones.
419 void CONF_modules_unload(int all)
423 CONF_modules_finish();
424 /* unload modules in reverse order */
425 for (i = sk_CONF_MODULE_num(supported_modules) - 1; i >= 0; i--) {
426 md = sk_CONF_MODULE_value(supported_modules, i);
427 /* If static or in use and 'all' not set ignore it */
428 if (((md->links > 0) || !md->dso) && !all)
430 /* Since we're working in reverse this is OK */
431 (void)sk_CONF_MODULE_delete(supported_modules, i);
434 if (sk_CONF_MODULE_num(supported_modules) == 0) {
435 sk_CONF_MODULE_free(supported_modules);
436 supported_modules = NULL;
440 /* unload a single module */
441 static void module_free(CONF_MODULE *md)
444 OPENSSL_free(md->name);
448 /* finish and free up all modules instances */
450 void CONF_modules_finish(void)
453 while (sk_CONF_IMODULE_num(initialized_modules) > 0) {
454 imod = sk_CONF_IMODULE_pop(initialized_modules);
457 sk_CONF_IMODULE_free(initialized_modules);
458 initialized_modules = NULL;
461 /* finish a module instance */
463 static void module_finish(CONF_IMODULE *imod)
467 if (imod->pmod->finish)
468 imod->pmod->finish(imod);
470 OPENSSL_free(imod->name);
471 OPENSSL_free(imod->value);
475 /* Add a static module to OpenSSL */
477 int CONF_module_add(const char *name, conf_init_func *ifunc,
478 conf_finish_func *ffunc)
480 if (module_add(NULL, name, ifunc, ffunc))
486 void conf_modules_free_int(void)
488 CONF_modules_finish();
489 CONF_modules_unload(1);
492 /* Utility functions */
494 const char *CONF_imodule_get_name(const CONF_IMODULE *md)
499 const char *CONF_imodule_get_value(const CONF_IMODULE *md)
504 void *CONF_imodule_get_usr_data(const CONF_IMODULE *md)
509 void CONF_imodule_set_usr_data(CONF_IMODULE *md, void *usr_data)
511 md->usr_data = usr_data;
514 CONF_MODULE *CONF_imodule_get_module(const CONF_IMODULE *md)
519 unsigned long CONF_imodule_get_flags(const CONF_IMODULE *md)
524 void CONF_imodule_set_flags(CONF_IMODULE *md, unsigned long flags)
529 void *CONF_module_get_usr_data(CONF_MODULE *pmod)
531 return pmod->usr_data;
534 void CONF_module_set_usr_data(CONF_MODULE *pmod, void *usr_data)
536 pmod->usr_data = usr_data;
539 /* Return default config file name */
541 char *CONF_get1_default_config_file(void)
544 char *file, *sep = "";
547 if ((file = ossl_safe_getenv("OPENSSL_CONF")) != NULL)
548 return OPENSSL_strdup(file);
550 t = X509_get_default_cert_area();
551 #ifndef OPENSSL_SYS_VMS
554 size = strlen(t) + strlen(sep) + strlen(OPENSSL_CONF) + 1;
555 file = OPENSSL_malloc(size);
559 BIO_snprintf(file, size, "%s%s%s", t, sep, OPENSSL_CONF);
565 * This function takes a list separated by 'sep' and calls the callback
566 * function giving the start and length of each member optionally stripping
567 * leading and trailing whitespace. This can be used to parse comma separated
571 int CONF_parse_list(const char *list_, int sep, int nospc,
572 int (*list_cb) (const char *elem, int len, void *usr),
576 const char *lstart, *tmpend, *p;
579 CONFerr(CONF_F_CONF_PARSE_LIST, CONF_R_LIST_CANNOT_BE_NULL);
586 while (*lstart && isspace((unsigned char)*lstart))
589 p = strchr(lstart, sep);
590 if (p == lstart || *lstart == '\0')
591 ret = list_cb(NULL, 0, arg);
596 tmpend = lstart + strlen(lstart) - 1;
598 while (isspace((unsigned char)*tmpend))
601 ret = list_cb(lstart, tmpend - lstart + 1, arg);