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 #include "internal/cryptlib.h"
13 #include <openssl/crypto.h>
14 #include "internal/conf.h"
15 #include "internal/dso.h"
16 #include "internal/thread_once.h"
17 #include <openssl/x509.h>
18 #include <openssl/trace.h>
19 #include <openssl/engine.h>
21 #define DSO_mod_init_name "OPENSSL_init"
22 #define DSO_mod_finish_name "OPENSSL_finish"
25 * This structure contains a data about supported modules. entries in this
26 * table correspond to either dynamic or static modules.
29 struct conf_module_st {
30 /* DSO of this module or NULL if static */
32 /* Name of the module */
37 conf_finish_func *finish;
38 /* Number of successfully initialized modules */
44 * This structure contains information about modules that have been
45 * successfully initialized. There may be more than one entry for a given
49 struct conf_imodule_st {
57 static STACK_OF(CONF_MODULE) *supported_modules = NULL;
58 static STACK_OF(CONF_IMODULE) *initialized_modules = NULL;
60 static CRYPTO_ONCE load_builtin_modules = CRYPTO_ONCE_STATIC_INIT;
62 static void module_free(CONF_MODULE *md);
63 static void module_finish(CONF_IMODULE *imod);
64 static int module_run(const CONF *cnf, const char *name, const char *value,
66 static CONF_MODULE *module_add(DSO *dso, const char *name,
67 conf_init_func *ifunc,
68 conf_finish_func *ffunc);
69 static CONF_MODULE *module_find(const char *name);
70 static int module_init(CONF_MODULE *pmod, const char *name, const char *value,
72 static CONF_MODULE *module_load_dso(const CONF *cnf, const char *name,
75 /* Main function: load modules from a CONF structure */
77 int CONF_modules_load(const CONF *cnf, const char *appname,
80 STACK_OF(CONF_VALUE) *values;
82 char *vsection = NULL;
90 vsection = NCONF_get_string(cnf, NULL, appname);
92 if (!appname || (!vsection && (flags & CONF_MFLAGS_DEFAULT_SECTION)))
93 vsection = NCONF_get_string(cnf, NULL, "openssl_conf");
100 OSSL_TRACE1(CONF, "Configuration in section %s\n", vsection);
101 values = NCONF_get_section(cnf, vsection);
106 for (i = 0; i < sk_CONF_VALUE_num(values); i++) {
107 vl = sk_CONF_VALUE_value(values, i);
108 ret = module_run(cnf, vl->name, vl->value, flags);
109 OSSL_TRACE3(CONF, "Running module %s (%s) returned %d\n",
110 vl->name, vl->value, ret);
112 if (!(flags & CONF_MFLAGS_IGNORE_ERRORS))
120 int CONF_modules_load_file_with_libctx(OPENSSL_CTX *libctx,
121 const char *filename,
122 const char *appname, unsigned long flags)
128 conf = NCONF_new_with_libctx(libctx, NULL);
132 if (filename == NULL) {
133 file = CONF_get1_default_config_file();
137 file = (char *)filename;
140 if (NCONF_load(conf, file, NULL) <= 0) {
141 if ((flags & CONF_MFLAGS_IGNORE_MISSING_FILE) &&
142 (ERR_GET_REASON(ERR_peek_last_error()) == CONF_R_NO_SUCH_FILE)) {
149 ret = CONF_modules_load(conf, appname, flags);
152 if (filename == NULL)
156 if (flags & CONF_MFLAGS_IGNORE_RETURN_CODES)
162 int CONF_modules_load_file(const char *filename,
163 const char *appname, unsigned long flags)
165 return CONF_modules_load_file_with_libctx(NULL, filename, appname, flags);
168 DEFINE_RUN_ONCE_STATIC(do_load_builtin_modules)
170 OPENSSL_load_builtin_modules();
171 #ifndef OPENSSL_NO_ENGINE
172 /* Need to load ENGINEs */
173 ENGINE_load_builtin_engines();
179 static int module_run(const CONF *cnf, const char *name, const char *value,
185 if (!RUN_ONCE(&load_builtin_modules, do_load_builtin_modules))
188 md = module_find(name);
190 /* Module not found: try to load DSO */
191 if (!md && !(flags & CONF_MFLAGS_NO_DSO))
192 md = module_load_dso(cnf, name, value);
195 if (!(flags & CONF_MFLAGS_SILENT)) {
196 CONFerr(CONF_F_MODULE_RUN, CONF_R_UNKNOWN_MODULE_NAME);
197 ERR_add_error_data(2, "module=", name);
202 ret = module_init(md, name, value, cnf);
205 if (!(flags & CONF_MFLAGS_SILENT)) {
206 char rcode[DECIMAL_SIZE(ret) + 1];
208 CONFerr(CONF_F_MODULE_RUN, CONF_R_MODULE_INITIALIZATION_ERROR);
209 BIO_snprintf(rcode, sizeof(rcode), "%-8d", ret);
210 ERR_add_error_data(6, "module=", name, ", value=", value,
211 ", retcode=", rcode);
218 /* Load a module from a DSO */
219 static CONF_MODULE *module_load_dso(const CONF *cnf,
220 const char *name, const char *value)
223 conf_init_func *ifunc;
224 conf_finish_func *ffunc;
225 const char *path = NULL;
229 /* Look for alternative path in module section */
230 path = NCONF_get_string(cnf, value, "path");
235 dso = DSO_load(NULL, path, NULL, 0);
237 errcode = CONF_R_ERROR_LOADING_DSO;
240 ifunc = (conf_init_func *)DSO_bind_func(dso, DSO_mod_init_name);
242 errcode = CONF_R_MISSING_INIT_FUNCTION;
245 ffunc = (conf_finish_func *)DSO_bind_func(dso, DSO_mod_finish_name);
246 /* All OK, add module */
247 md = module_add(dso, name, ifunc, ffunc);
256 CONFerr(CONF_F_MODULE_LOAD_DSO, errcode);
257 ERR_add_error_data(4, "module=", name, ", path=", path);
261 /* add module to list */
262 static CONF_MODULE *module_add(DSO *dso, const char *name,
263 conf_init_func *ifunc, conf_finish_func *ffunc)
265 CONF_MODULE *tmod = NULL;
266 if (supported_modules == NULL)
267 supported_modules = sk_CONF_MODULE_new_null();
268 if (supported_modules == NULL)
270 if ((tmod = OPENSSL_zalloc(sizeof(*tmod))) == NULL) {
271 CONFerr(CONF_F_MODULE_ADD, ERR_R_MALLOC_FAILURE);
276 tmod->name = OPENSSL_strdup(name);
278 tmod->finish = ffunc;
279 if (tmod->name == NULL) {
284 if (!sk_CONF_MODULE_push(supported_modules, tmod)) {
285 OPENSSL_free(tmod->name);
294 * Find a module from the list. We allow module names of the form
295 * modname.XXXX to just search for modname to allow the same module to be
296 * initialized more than once.
299 static CONF_MODULE *module_find(const char *name)
304 p = strrchr(name, '.');
309 nchar = strlen(name);
311 for (i = 0; i < sk_CONF_MODULE_num(supported_modules); i++) {
312 tmod = sk_CONF_MODULE_value(supported_modules, i);
313 if (strncmp(tmod->name, name, nchar) == 0)
321 /* initialize a module */
322 static int module_init(CONF_MODULE *pmod, const char *name, const char *value,
327 CONF_IMODULE *imod = NULL;
329 /* Otherwise add initialized module to list */
330 imod = OPENSSL_malloc(sizeof(*imod));
335 imod->name = OPENSSL_strdup(name);
336 imod->value = OPENSSL_strdup(value);
337 imod->usr_data = NULL;
339 if (!imod->name || !imod->value)
342 /* Try to initialize module */
344 ret = pmod->init(imod, cnf);
346 /* Error occurred, exit */
351 if (initialized_modules == NULL) {
352 initialized_modules = sk_CONF_IMODULE_new_null();
353 if (!initialized_modules) {
354 CONFerr(CONF_F_MODULE_INIT, ERR_R_MALLOC_FAILURE);
359 if (!sk_CONF_IMODULE_push(initialized_modules, imod)) {
360 CONFerr(CONF_F_MODULE_INIT, ERR_R_MALLOC_FAILURE);
370 /* We've started the module so we'd better finish it */
371 if (pmod->finish && init_called)
376 OPENSSL_free(imod->name);
377 OPENSSL_free(imod->value);
386 * Unload any dynamic modules that have a link count of zero: i.e. have no
387 * active initialized modules. If 'all' is set then all modules are unloaded
388 * including static ones.
391 void CONF_modules_unload(int all)
395 CONF_modules_finish();
396 /* unload modules in reverse order */
397 for (i = sk_CONF_MODULE_num(supported_modules) - 1; i >= 0; i--) {
398 md = sk_CONF_MODULE_value(supported_modules, i);
399 /* If static or in use and 'all' not set ignore it */
400 if (((md->links > 0) || !md->dso) && !all)
402 /* Since we're working in reverse this is OK */
403 (void)sk_CONF_MODULE_delete(supported_modules, i);
406 if (sk_CONF_MODULE_num(supported_modules) == 0) {
407 sk_CONF_MODULE_free(supported_modules);
408 supported_modules = NULL;
412 /* unload a single module */
413 static void module_free(CONF_MODULE *md)
416 OPENSSL_free(md->name);
420 /* finish and free up all modules instances */
422 void CONF_modules_finish(void)
425 while (sk_CONF_IMODULE_num(initialized_modules) > 0) {
426 imod = sk_CONF_IMODULE_pop(initialized_modules);
429 sk_CONF_IMODULE_free(initialized_modules);
430 initialized_modules = NULL;
433 /* finish a module instance */
435 static void module_finish(CONF_IMODULE *imod)
439 if (imod->pmod->finish)
440 imod->pmod->finish(imod);
442 OPENSSL_free(imod->name);
443 OPENSSL_free(imod->value);
447 /* Add a static module to OpenSSL */
449 int CONF_module_add(const char *name, conf_init_func *ifunc,
450 conf_finish_func *ffunc)
452 if (module_add(NULL, name, ifunc, ffunc))
458 void conf_modules_free_int(void)
460 CONF_modules_finish();
461 CONF_modules_unload(1);
464 /* Utility functions */
466 const char *CONF_imodule_get_name(const CONF_IMODULE *md)
471 const char *CONF_imodule_get_value(const CONF_IMODULE *md)
476 void *CONF_imodule_get_usr_data(const CONF_IMODULE *md)
481 void CONF_imodule_set_usr_data(CONF_IMODULE *md, void *usr_data)
483 md->usr_data = usr_data;
486 CONF_MODULE *CONF_imodule_get_module(const CONF_IMODULE *md)
491 unsigned long CONF_imodule_get_flags(const CONF_IMODULE *md)
496 void CONF_imodule_set_flags(CONF_IMODULE *md, unsigned long flags)
501 void *CONF_module_get_usr_data(CONF_MODULE *pmod)
503 return pmod->usr_data;
506 void CONF_module_set_usr_data(CONF_MODULE *pmod, void *usr_data)
508 pmod->usr_data = usr_data;
511 /* Return default config file name */
513 char *CONF_get1_default_config_file(void)
515 char *file, *sep = "";
518 if ((file = ossl_safe_getenv("OPENSSL_CONF")) != NULL)
519 return OPENSSL_strdup(file);
521 len = strlen(X509_get_default_cert_area());
522 #ifndef OPENSSL_SYS_VMS
526 len += strlen(OPENSSL_CONF);
528 file = OPENSSL_malloc(len + 1);
532 BIO_snprintf(file, len + 1, "%s%s%s", X509_get_default_cert_area(),
539 * This function takes a list separated by 'sep' and calls the callback
540 * function giving the start and length of each member optionally stripping
541 * leading and trailing whitespace. This can be used to parse comma separated
545 int CONF_parse_list(const char *list_, int sep, int nospc,
546 int (*list_cb) (const char *elem, int len, void *usr),
550 const char *lstart, *tmpend, *p;
553 CONFerr(CONF_F_CONF_PARSE_LIST, CONF_R_LIST_CANNOT_BE_NULL);
560 while (*lstart && isspace((unsigned char)*lstart))
563 p = strchr(lstart, sep);
564 if (p == lstart || *lstart == '\0')
565 ret = list_cb(NULL, 0, arg);
570 tmpend = lstart + strlen(lstart) - 1;
572 while (isspace((unsigned char)*tmpend))
575 ret = list_cb(lstart, tmpend - lstart + 1, arg);