2 * Copyright 2002-2018 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 <openssl/x509.h>
18 #define DSO_mod_init_name "OPENSSL_init"
19 #define DSO_mod_finish_name "OPENSSL_finish"
22 * This structure contains a data about supported modules. entries in this
23 * table correspond to either dynamic or static modules.
26 struct conf_module_st {
27 /* DSO of this module or NULL if static */
29 /* Name of the module */
34 conf_finish_func *finish;
35 /* Number of successfully initialized modules */
41 * This structure contains information about modules that have been
42 * successfully initialized. There may be more than one entry for a given
46 struct conf_imodule_st {
54 static STACK_OF(CONF_MODULE) *supported_modules = NULL;
55 static STACK_OF(CONF_IMODULE) *initialized_modules = NULL;
57 static void module_free(CONF_MODULE *md);
58 static void module_finish(CONF_IMODULE *imod);
59 static int module_run(const CONF *cnf, const char *name, const char *value,
61 static CONF_MODULE *module_add(DSO *dso, const char *name,
62 conf_init_func *ifunc,
63 conf_finish_func *ffunc);
64 static CONF_MODULE *module_find(const char *name);
65 static int module_init(CONF_MODULE *pmod, const char *name, const char *value,
67 static CONF_MODULE *module_load_dso(const CONF *cnf, const char *name,
70 /* Main function: load modules from a CONF structure */
72 int CONF_modules_load(const CONF *cnf, const char *appname,
75 STACK_OF(CONF_VALUE) *values;
77 char *vsection = NULL;
85 vsection = NCONF_get_string(cnf, NULL, appname);
87 if (!appname || (!vsection && (flags & CONF_MFLAGS_DEFAULT_SECTION)))
88 vsection = NCONF_get_string(cnf, NULL, "openssl_conf");
95 values = NCONF_get_section(cnf, vsection);
100 for (i = 0; i < sk_CONF_VALUE_num(values); i++) {
101 vl = sk_CONF_VALUE_value(values, i);
102 ret = module_run(cnf, vl->name, vl->value, flags);
104 if (!(flags & CONF_MFLAGS_IGNORE_ERRORS))
112 int CONF_modules_load_file(const char *filename, const char *appname,
118 conf = NCONF_new(NULL);
122 if (filename == NULL) {
123 file = CONF_get1_default_config_file();
127 file = (char *)filename;
129 if (NCONF_load(conf, file, NULL) <= 0) {
130 if ((flags & CONF_MFLAGS_IGNORE_MISSING_FILE) &&
131 (ERR_GET_REASON(ERR_peek_last_error()) == CONF_R_NO_SUCH_FILE)) {
138 ret = CONF_modules_load(conf, appname, flags);
141 if (filename == NULL)
145 if (flags & CONF_MFLAGS_IGNORE_RETURN_CODES)
151 static int module_run(const CONF *cnf, const char *name, const char *value,
157 md = module_find(name);
159 /* Module not found: try to load DSO */
160 if (!md && !(flags & CONF_MFLAGS_NO_DSO))
161 md = module_load_dso(cnf, name, value);
164 if (!(flags & CONF_MFLAGS_SILENT)) {
165 CONFerr(CONF_F_MODULE_RUN, CONF_R_UNKNOWN_MODULE_NAME);
166 ERR_add_error_data(2, "module=", name);
171 ret = module_init(md, name, value, cnf);
174 if (!(flags & CONF_MFLAGS_SILENT)) {
175 char rcode[DECIMAL_SIZE(ret) + 1];
177 CONFerr(CONF_F_MODULE_RUN, CONF_R_MODULE_INITIALIZATION_ERROR);
178 BIO_snprintf(rcode, sizeof(rcode), "%-8d", ret);
179 ERR_add_error_data(6, "module=", name, ", value=", value,
180 ", retcode=", rcode);
187 /* Load a module from a DSO */
188 static CONF_MODULE *module_load_dso(const CONF *cnf,
189 const char *name, const char *value)
192 conf_init_func *ifunc;
193 conf_finish_func *ffunc;
194 const char *path = NULL;
197 /* Look for alternative path in module section */
198 path = NCONF_get_string(cnf, value, "path");
203 dso = DSO_load(NULL, path, NULL, 0);
205 errcode = CONF_R_ERROR_LOADING_DSO;
208 ifunc = (conf_init_func *)DSO_bind_func(dso, DSO_mod_init_name);
210 errcode = CONF_R_MISSING_INIT_FUNCTION;
213 ffunc = (conf_finish_func *)DSO_bind_func(dso, DSO_mod_finish_name);
214 /* All OK, add module */
215 md = module_add(dso, name, ifunc, ffunc);
224 CONFerr(CONF_F_MODULE_LOAD_DSO, errcode);
225 ERR_add_error_data(4, "module=", name, ", path=", path);
229 /* add module to list */
230 static CONF_MODULE *module_add(DSO *dso, const char *name,
231 conf_init_func *ifunc, conf_finish_func *ffunc)
233 CONF_MODULE *tmod = NULL;
234 if (supported_modules == NULL)
235 supported_modules = sk_CONF_MODULE_new_null();
236 if (supported_modules == NULL)
238 if ((tmod = OPENSSL_zalloc(sizeof(*tmod))) == NULL) {
239 CONFerr(CONF_F_MODULE_ADD, ERR_R_MALLOC_FAILURE);
244 tmod->name = OPENSSL_strdup(name);
246 tmod->finish = ffunc;
247 if (tmod->name == NULL) {
252 if (!sk_CONF_MODULE_push(supported_modules, tmod)) {
253 OPENSSL_free(tmod->name);
262 * Find a module from the list. We allow module names of the form
263 * modname.XXXX to just search for modname to allow the same module to be
264 * initialized more than once.
267 static CONF_MODULE *module_find(const char *name)
272 p = strrchr(name, '.');
277 nchar = strlen(name);
279 for (i = 0; i < sk_CONF_MODULE_num(supported_modules); i++) {
280 tmod = sk_CONF_MODULE_value(supported_modules, i);
281 if (strncmp(tmod->name, name, nchar) == 0)
289 /* initialize a module */
290 static int module_init(CONF_MODULE *pmod, const char *name, const char *value,
295 CONF_IMODULE *imod = NULL;
297 /* Otherwise add initialized module to list */
298 imod = OPENSSL_malloc(sizeof(*imod));
303 imod->name = OPENSSL_strdup(name);
304 imod->value = OPENSSL_strdup(value);
305 imod->usr_data = NULL;
307 if (!imod->name || !imod->value)
310 /* Try to initialize module */
312 ret = pmod->init(imod, cnf);
314 /* Error occurred, exit */
319 if (initialized_modules == NULL) {
320 initialized_modules = sk_CONF_IMODULE_new_null();
321 if (!initialized_modules) {
322 CONFerr(CONF_F_MODULE_INIT, ERR_R_MALLOC_FAILURE);
327 if (!sk_CONF_IMODULE_push(initialized_modules, imod)) {
328 CONFerr(CONF_F_MODULE_INIT, ERR_R_MALLOC_FAILURE);
338 /* We've started the module so we'd better finish it */
339 if (pmod->finish && init_called)
344 OPENSSL_free(imod->name);
345 OPENSSL_free(imod->value);
354 * Unload any dynamic modules that have a link count of zero: i.e. have no
355 * active initialized modules. If 'all' is set then all modules are unloaded
356 * including static ones.
359 void CONF_modules_unload(int all)
363 CONF_modules_finish();
364 /* unload modules in reverse order */
365 for (i = sk_CONF_MODULE_num(supported_modules) - 1; i >= 0; i--) {
366 md = sk_CONF_MODULE_value(supported_modules, i);
367 /* If static or in use and 'all' not set ignore it */
368 if (((md->links > 0) || !md->dso) && !all)
370 /* Since we're working in reverse this is OK */
371 (void)sk_CONF_MODULE_delete(supported_modules, i);
374 if (sk_CONF_MODULE_num(supported_modules) == 0) {
375 sk_CONF_MODULE_free(supported_modules);
376 supported_modules = NULL;
380 /* unload a single module */
381 static void module_free(CONF_MODULE *md)
384 OPENSSL_free(md->name);
388 /* finish and free up all modules instances */
390 void CONF_modules_finish(void)
393 while (sk_CONF_IMODULE_num(initialized_modules) > 0) {
394 imod = sk_CONF_IMODULE_pop(initialized_modules);
397 sk_CONF_IMODULE_free(initialized_modules);
398 initialized_modules = NULL;
401 /* finish a module instance */
403 static void module_finish(CONF_IMODULE *imod)
407 if (imod->pmod->finish)
408 imod->pmod->finish(imod);
410 OPENSSL_free(imod->name);
411 OPENSSL_free(imod->value);
415 /* Add a static module to OpenSSL */
417 int CONF_module_add(const char *name, conf_init_func *ifunc,
418 conf_finish_func *ffunc)
420 if (module_add(NULL, name, ifunc, ffunc))
426 void conf_modules_free_int(void)
428 CONF_modules_finish();
429 CONF_modules_unload(1);
432 /* Utility functions */
434 const char *CONF_imodule_get_name(const CONF_IMODULE *md)
439 const char *CONF_imodule_get_value(const CONF_IMODULE *md)
444 void *CONF_imodule_get_usr_data(const CONF_IMODULE *md)
449 void CONF_imodule_set_usr_data(CONF_IMODULE *md, void *usr_data)
451 md->usr_data = usr_data;
454 CONF_MODULE *CONF_imodule_get_module(const CONF_IMODULE *md)
459 unsigned long CONF_imodule_get_flags(const CONF_IMODULE *md)
464 void CONF_imodule_set_flags(CONF_IMODULE *md, unsigned long flags)
469 void *CONF_module_get_usr_data(CONF_MODULE *pmod)
471 return pmod->usr_data;
474 void CONF_module_set_usr_data(CONF_MODULE *pmod, void *usr_data)
476 pmod->usr_data = usr_data;
479 /* Return default config file name */
481 char *CONF_get1_default_config_file(void)
483 char *file, *sep = "";
486 if ((file = ossl_safe_getenv("OPENSSL_CONF")) != NULL)
487 return OPENSSL_strdup(file);
489 len = strlen(X509_get_default_cert_area());
490 #ifndef OPENSSL_SYS_VMS
494 len += strlen(OPENSSL_CONF);
496 file = OPENSSL_malloc(len + 1);
500 BIO_snprintf(file, len + 1, "%s%s%s", X509_get_default_cert_area(),
507 * This function takes a list separated by 'sep' and calls the callback
508 * function giving the start and length of each member optionally stripping
509 * leading and trailing whitespace. This can be used to parse comma separated
513 int CONF_parse_list(const char *list_, int sep, int nospc,
514 int (*list_cb) (const char *elem, int len, void *usr),
518 const char *lstart, *tmpend, *p;
521 CONFerr(CONF_F_CONF_PARSE_LIST, CONF_R_LIST_CANNOT_BE_NULL);
528 while (*lstart && isspace((unsigned char)*lstart))
531 p = strchr(lstart, sep);
532 if (p == lstart || !*lstart)
533 ret = list_cb(NULL, 0, arg);
538 tmpend = lstart + strlen(lstart) - 1;
540 while (isspace((unsigned char)*tmpend))
543 ret = list_cb(lstart, tmpend - lstart + 1, arg);