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_with_libctx(OPENSSL_CTX *libctx,
149 const char *filename,
150 const char *appname, unsigned long flags)
154 int ret = 0, diagnostics = 0;
156 conf = NCONF_new_with_libctx(libctx, NULL);
160 if (filename == NULL) {
161 file = CONF_get1_default_config_file();
165 file = (char *)filename;
168 if (NCONF_load(conf, file, NULL) <= 0) {
169 if ((flags & CONF_MFLAGS_IGNORE_MISSING_FILE) &&
170 (ERR_GET_REASON(ERR_peek_last_error()) == CONF_R_NO_SUCH_FILE)) {
177 ret = CONF_modules_load(conf, appname, flags);
178 diagnostics = conf_diagnostics(conf);
181 if (filename == NULL)
185 if ((flags & CONF_MFLAGS_IGNORE_RETURN_CODES) != 0 && !diagnostics)
191 int CONF_modules_load_file(const char *filename,
192 const char *appname, unsigned long flags)
194 return CONF_modules_load_file_with_libctx(NULL, filename, appname, flags);
197 DEFINE_RUN_ONCE_STATIC(do_load_builtin_modules)
199 OPENSSL_load_builtin_modules();
200 #ifndef OPENSSL_NO_ENGINE
201 /* Need to load ENGINEs */
202 ENGINE_load_builtin_engines();
208 static int module_run(const CONF *cnf, const char *name, const char *value,
214 if (!RUN_ONCE(&load_builtin_modules, do_load_builtin_modules))
217 md = module_find(name);
219 /* Module not found: try to load DSO */
220 if (!md && !(flags & CONF_MFLAGS_NO_DSO))
221 md = module_load_dso(cnf, name, value);
224 if (!(flags & CONF_MFLAGS_SILENT)) {
225 CONFerr(CONF_F_MODULE_RUN, CONF_R_UNKNOWN_MODULE_NAME);
226 ERR_add_error_data(2, "module=", name);
231 ret = module_init(md, name, value, cnf);
234 if (!(flags & CONF_MFLAGS_SILENT)) {
235 char rcode[DECIMAL_SIZE(ret) + 1];
237 CONFerr(CONF_F_MODULE_RUN, CONF_R_MODULE_INITIALIZATION_ERROR);
238 BIO_snprintf(rcode, sizeof(rcode), "%-8d", ret);
239 ERR_add_error_data(6, "module=", name, ", value=", value,
240 ", retcode=", rcode);
247 /* Load a module from a DSO */
248 static CONF_MODULE *module_load_dso(const CONF *cnf,
249 const char *name, const char *value)
252 conf_init_func *ifunc;
253 conf_finish_func *ffunc;
254 const char *path = NULL;
258 /* Look for alternative path in module section */
259 path = NCONF_get_string(cnf, value, "path");
264 dso = DSO_load(NULL, path, NULL, 0);
266 errcode = CONF_R_ERROR_LOADING_DSO;
269 ifunc = (conf_init_func *)DSO_bind_func(dso, DSO_mod_init_name);
271 errcode = CONF_R_MISSING_INIT_FUNCTION;
274 ffunc = (conf_finish_func *)DSO_bind_func(dso, DSO_mod_finish_name);
275 /* All OK, add module */
276 md = module_add(dso, name, ifunc, ffunc);
285 CONFerr(CONF_F_MODULE_LOAD_DSO, errcode);
286 ERR_add_error_data(4, "module=", name, ", path=", path);
290 /* add module to list */
291 static CONF_MODULE *module_add(DSO *dso, const char *name,
292 conf_init_func *ifunc, conf_finish_func *ffunc)
294 CONF_MODULE *tmod = NULL;
295 if (supported_modules == NULL)
296 supported_modules = sk_CONF_MODULE_new_null();
297 if (supported_modules == NULL)
299 if ((tmod = OPENSSL_zalloc(sizeof(*tmod))) == NULL) {
300 CONFerr(CONF_F_MODULE_ADD, ERR_R_MALLOC_FAILURE);
305 tmod->name = OPENSSL_strdup(name);
307 tmod->finish = ffunc;
308 if (tmod->name == NULL) {
313 if (!sk_CONF_MODULE_push(supported_modules, tmod)) {
314 OPENSSL_free(tmod->name);
323 * Find a module from the list. We allow module names of the form
324 * modname.XXXX to just search for modname to allow the same module to be
325 * initialized more than once.
328 static CONF_MODULE *module_find(const char *name)
333 p = strrchr(name, '.');
338 nchar = strlen(name);
340 for (i = 0; i < sk_CONF_MODULE_num(supported_modules); i++) {
341 tmod = sk_CONF_MODULE_value(supported_modules, i);
342 if (strncmp(tmod->name, name, nchar) == 0)
350 /* initialize a module */
351 static int module_init(CONF_MODULE *pmod, const char *name, const char *value,
356 CONF_IMODULE *imod = NULL;
358 /* Otherwise add initialized module to list */
359 imod = OPENSSL_malloc(sizeof(*imod));
364 imod->name = OPENSSL_strdup(name);
365 imod->value = OPENSSL_strdup(value);
366 imod->usr_data = NULL;
368 if (!imod->name || !imod->value)
371 /* Try to initialize module */
373 ret = pmod->init(imod, cnf);
375 /* Error occurred, exit */
380 if (initialized_modules == NULL) {
381 initialized_modules = sk_CONF_IMODULE_new_null();
382 if (!initialized_modules) {
383 CONFerr(CONF_F_MODULE_INIT, ERR_R_MALLOC_FAILURE);
388 if (!sk_CONF_IMODULE_push(initialized_modules, imod)) {
389 CONFerr(CONF_F_MODULE_INIT, ERR_R_MALLOC_FAILURE);
399 /* We've started the module so we'd better finish it */
400 if (pmod->finish && init_called)
405 OPENSSL_free(imod->name);
406 OPENSSL_free(imod->value);
415 * Unload any dynamic modules that have a link count of zero: i.e. have no
416 * active initialized modules. If 'all' is set then all modules are unloaded
417 * including static ones.
420 void CONF_modules_unload(int all)
424 CONF_modules_finish();
425 /* unload modules in reverse order */
426 for (i = sk_CONF_MODULE_num(supported_modules) - 1; i >= 0; i--) {
427 md = sk_CONF_MODULE_value(supported_modules, i);
428 /* If static or in use and 'all' not set ignore it */
429 if (((md->links > 0) || !md->dso) && !all)
431 /* Since we're working in reverse this is OK */
432 (void)sk_CONF_MODULE_delete(supported_modules, i);
435 if (sk_CONF_MODULE_num(supported_modules) == 0) {
436 sk_CONF_MODULE_free(supported_modules);
437 supported_modules = NULL;
441 /* unload a single module */
442 static void module_free(CONF_MODULE *md)
445 OPENSSL_free(md->name);
449 /* finish and free up all modules instances */
451 void CONF_modules_finish(void)
454 while (sk_CONF_IMODULE_num(initialized_modules) > 0) {
455 imod = sk_CONF_IMODULE_pop(initialized_modules);
458 sk_CONF_IMODULE_free(initialized_modules);
459 initialized_modules = NULL;
462 /* finish a module instance */
464 static void module_finish(CONF_IMODULE *imod)
468 if (imod->pmod->finish)
469 imod->pmod->finish(imod);
471 OPENSSL_free(imod->name);
472 OPENSSL_free(imod->value);
476 /* Add a static module to OpenSSL */
478 int CONF_module_add(const char *name, conf_init_func *ifunc,
479 conf_finish_func *ffunc)
481 if (module_add(NULL, name, ifunc, ffunc))
487 void conf_modules_free_int(void)
489 CONF_modules_finish();
490 CONF_modules_unload(1);
493 /* Utility functions */
495 const char *CONF_imodule_get_name(const CONF_IMODULE *md)
500 const char *CONF_imodule_get_value(const CONF_IMODULE *md)
505 void *CONF_imodule_get_usr_data(const CONF_IMODULE *md)
510 void CONF_imodule_set_usr_data(CONF_IMODULE *md, void *usr_data)
512 md->usr_data = usr_data;
515 CONF_MODULE *CONF_imodule_get_module(const CONF_IMODULE *md)
520 unsigned long CONF_imodule_get_flags(const CONF_IMODULE *md)
525 void CONF_imodule_set_flags(CONF_IMODULE *md, unsigned long flags)
530 void *CONF_module_get_usr_data(CONF_MODULE *pmod)
532 return pmod->usr_data;
535 void CONF_module_set_usr_data(CONF_MODULE *pmod, void *usr_data)
537 pmod->usr_data = usr_data;
540 /* Return default config file name */
542 char *CONF_get1_default_config_file(void)
545 char *file, *sep = "";
548 if ((file = ossl_safe_getenv("OPENSSL_CONF")) != NULL)
549 return OPENSSL_strdup(file);
551 t = X509_get_default_cert_area();
552 #ifndef OPENSSL_SYS_VMS
555 size = strlen(t) + strlen(sep) + strlen(OPENSSL_CONF) + 1;
556 file = OPENSSL_malloc(size);
560 BIO_snprintf(file, size, "%s%s%s", t, sep, OPENSSL_CONF);
566 * This function takes a list separated by 'sep' and calls the callback
567 * function giving the start and length of each member optionally stripping
568 * leading and trailing whitespace. This can be used to parse comma separated
572 int CONF_parse_list(const char *list_, int sep, int nospc,
573 int (*list_cb) (const char *elem, int len, void *usr),
577 const char *lstart, *tmpend, *p;
580 CONFerr(CONF_F_CONF_PARSE_LIST, CONF_R_LIST_CANNOT_BE_NULL);
587 while (*lstart && isspace((unsigned char)*lstart))
590 p = strchr(lstart, sep);
591 if (p == lstart || *lstart == '\0')
592 ret = list_cb(NULL, 0, arg);
597 tmpend = lstart + strlen(lstart) - 1;
599 while (isspace((unsigned char)*tmpend))
602 ret = list_cb(lstart, tmpend - lstart + 1, arg);