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 "openssl/conf_api.h"
19 #include "internal/dso.h"
20 #include "internal/thread_once.h"
21 #include <openssl/x509.h>
22 #include <openssl/trace.h>
23 #include <openssl/engine.h>
25 DEFINE_STACK_OF(CONF_MODULE
)
26 DEFINE_STACK_OF(CONF_IMODULE
)
28 #define DSO_mod_init_name "OPENSSL_init"
29 #define DSO_mod_finish_name "OPENSSL_finish"
32 * This structure contains a data about supported modules. entries in this
33 * table correspond to either dynamic or static modules.
36 struct conf_module_st
{
37 /* DSO of this module or NULL if static */
39 /* Name of the module */
44 conf_finish_func
*finish
;
45 /* Number of successfully initialized modules */
51 * This structure contains information about modules that have been
52 * successfully initialized. There may be more than one entry for a given
56 struct conf_imodule_st
{
64 static STACK_OF(CONF_MODULE
) *supported_modules
= NULL
;
65 static STACK_OF(CONF_IMODULE
) *initialized_modules
= NULL
;
67 static CRYPTO_ONCE load_builtin_modules
= CRYPTO_ONCE_STATIC_INIT
;
69 static void module_free(CONF_MODULE
*md
);
70 static void module_finish(CONF_IMODULE
*imod
);
71 static int module_run(const CONF
*cnf
, const char *name
, const char *value
,
73 static CONF_MODULE
*module_add(DSO
*dso
, const char *name
,
74 conf_init_func
*ifunc
,
75 conf_finish_func
*ffunc
);
76 static CONF_MODULE
*module_find(const char *name
);
77 static int module_init(CONF_MODULE
*pmod
, const char *name
, const char *value
,
79 static CONF_MODULE
*module_load_dso(const CONF
*cnf
, const char *name
,
82 static int conf_diagnostics(const CONF
*cnf
)
84 return _CONF_get_number(cnf
, NULL
, "config_diagnostics") != 0;
87 /* Main function: load modules from a CONF structure */
89 int CONF_modules_load(const CONF
*cnf
, const char *appname
,
92 STACK_OF(CONF_VALUE
) *values
;
94 char *vsection
= NULL
;
100 if (conf_diagnostics(cnf
))
101 flags
&= ~(CONF_MFLAGS_IGNORE_ERRORS
102 | CONF_MFLAGS_IGNORE_RETURN_CODES
104 | CONF_MFLAGS_IGNORE_MISSING_FILE
);
108 vsection
= NCONF_get_string(cnf
, NULL
, appname
);
110 if (!appname
|| (!vsection
&& (flags
& CONF_MFLAGS_DEFAULT_SECTION
)))
111 vsection
= NCONF_get_string(cnf
, NULL
, "openssl_conf");
118 OSSL_TRACE1(CONF
, "Configuration in section %s\n", vsection
);
119 values
= NCONF_get_section(cnf
, vsection
);
121 if (values
== NULL
) {
122 if (!(flags
& CONF_MFLAGS_SILENT
)) {
123 ERR_clear_last_mark();
124 ERR_raise(ERR_LIB_CONF
, CONF_R_OPENSSL_CONF_REFERENCES_MISSING_SECTION
);
125 ERR_add_error_data(2, "openssl_conf=", vsection
);
133 for (i
= 0; i
< sk_CONF_VALUE_num(values
); i
++) {
134 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
)) {
141 ERR_clear_last_mark();
151 int CONF_modules_load_file_ex(OSSL_LIB_CTX
*libctx
, const char *filename
,
152 const char *appname
, unsigned long flags
)
156 int ret
= 0, diagnostics
= 0;
159 conf
= NCONF_new_ex(libctx
, NULL
);
163 if (filename
== NULL
) {
164 file
= CONF_get1_default_config_file();
168 file
= (char *)filename
;
171 if (NCONF_load(conf
, file
, NULL
) <= 0) {
172 if ((flags
& CONF_MFLAGS_IGNORE_MISSING_FILE
) &&
173 (ERR_GET_REASON(ERR_peek_last_error()) == CONF_R_NO_SUCH_FILE
)) {
179 ret
= CONF_modules_load(conf
, appname
, flags
);
180 diagnostics
= conf_diagnostics(conf
);
183 if (filename
== NULL
)
187 if ((flags
& CONF_MFLAGS_IGNORE_RETURN_CODES
) != 0 && !diagnostics
)
193 ERR_clear_last_mark();
198 int CONF_modules_load_file(const char *filename
,
199 const char *appname
, unsigned long flags
)
201 return CONF_modules_load_file_ex(NULL
, filename
, appname
, flags
);
204 DEFINE_RUN_ONCE_STATIC(do_load_builtin_modules
)
206 OPENSSL_load_builtin_modules();
207 #ifndef OPENSSL_NO_ENGINE
208 /* Need to load ENGINEs */
209 ENGINE_load_builtin_engines();
214 static int module_run(const CONF
*cnf
, const char *name
, const char *value
,
220 if (!RUN_ONCE(&load_builtin_modules
, do_load_builtin_modules
))
223 md
= module_find(name
);
225 /* Module not found: try to load DSO */
226 if (!md
&& !(flags
& CONF_MFLAGS_NO_DSO
))
227 md
= module_load_dso(cnf
, name
, value
);
230 if (!(flags
& CONF_MFLAGS_SILENT
)) {
231 ERR_raise(ERR_LIB_CONF
, CONF_R_UNKNOWN_MODULE_NAME
);
232 ERR_add_error_data(2, "module=", name
);
237 ret
= module_init(md
, name
, value
, cnf
);
240 if (!(flags
& CONF_MFLAGS_SILENT
)) {
241 char rcode
[DECIMAL_SIZE(ret
) + 1];
243 ERR_raise(ERR_LIB_CONF
, CONF_R_MODULE_INITIALIZATION_ERROR
);
244 BIO_snprintf(rcode
, sizeof(rcode
), "%-8d", ret
);
245 ERR_add_error_data(6, "module=", name
, ", value=", value
,
246 ", retcode=", rcode
);
253 /* Load a module from a DSO */
254 static CONF_MODULE
*module_load_dso(const CONF
*cnf
,
255 const char *name
, const char *value
)
258 conf_init_func
*ifunc
;
259 conf_finish_func
*ffunc
;
260 const char *path
= NULL
;
264 /* Look for alternative path in module section */
265 path
= _CONF_get_string(cnf
, value
, "path");
269 dso
= DSO_load(NULL
, path
, NULL
, 0);
271 errcode
= CONF_R_ERROR_LOADING_DSO
;
274 ifunc
= (conf_init_func
*)DSO_bind_func(dso
, DSO_mod_init_name
);
276 errcode
= CONF_R_MISSING_INIT_FUNCTION
;
279 ffunc
= (conf_finish_func
*)DSO_bind_func(dso
, DSO_mod_finish_name
);
280 /* All OK, add module */
281 md
= module_add(dso
, name
, ifunc
, ffunc
);
290 ERR_raise(ERR_LIB_CONF
, errcode
);
291 ERR_add_error_data(4, "module=", name
, ", path=", path
);
295 /* add module to list */
296 static CONF_MODULE
*module_add(DSO
*dso
, const char *name
,
297 conf_init_func
*ifunc
, conf_finish_func
*ffunc
)
299 CONF_MODULE
*tmod
= NULL
;
300 if (supported_modules
== NULL
)
301 supported_modules
= sk_CONF_MODULE_new_null();
302 if (supported_modules
== NULL
)
304 if ((tmod
= OPENSSL_zalloc(sizeof(*tmod
))) == NULL
) {
305 ERR_raise(ERR_LIB_CONF
, ERR_R_MALLOC_FAILURE
);
310 tmod
->name
= OPENSSL_strdup(name
);
312 tmod
->finish
= ffunc
;
313 if (tmod
->name
== NULL
) {
318 if (!sk_CONF_MODULE_push(supported_modules
, tmod
)) {
319 OPENSSL_free(tmod
->name
);
328 * Find a module from the list. We allow module names of the form
329 * modname.XXXX to just search for modname to allow the same module to be
330 * initialized more than once.
333 static CONF_MODULE
*module_find(const char *name
)
338 p
= strrchr(name
, '.');
343 nchar
= strlen(name
);
345 for (i
= 0; i
< sk_CONF_MODULE_num(supported_modules
); i
++) {
346 tmod
= sk_CONF_MODULE_value(supported_modules
, i
);
347 if (strncmp(tmod
->name
, name
, nchar
) == 0)
355 /* initialize a module */
356 static int module_init(CONF_MODULE
*pmod
, const char *name
, const char *value
,
361 CONF_IMODULE
*imod
= NULL
;
363 /* Otherwise add initialized module to list */
364 imod
= OPENSSL_malloc(sizeof(*imod
));
369 imod
->name
= OPENSSL_strdup(name
);
370 imod
->value
= OPENSSL_strdup(value
);
371 imod
->usr_data
= NULL
;
373 if (!imod
->name
|| !imod
->value
)
376 /* Try to initialize module */
378 ret
= pmod
->init(imod
, cnf
);
380 /* Error occurred, exit */
385 if (initialized_modules
== NULL
) {
386 initialized_modules
= sk_CONF_IMODULE_new_null();
387 if (!initialized_modules
) {
388 ERR_raise(ERR_LIB_CONF
, ERR_R_MALLOC_FAILURE
);
393 if (!sk_CONF_IMODULE_push(initialized_modules
, imod
)) {
394 ERR_raise(ERR_LIB_CONF
, ERR_R_MALLOC_FAILURE
);
404 /* We've started the module so we'd better finish it */
405 if (pmod
->finish
&& init_called
)
410 OPENSSL_free(imod
->name
);
411 OPENSSL_free(imod
->value
);
420 * Unload any dynamic modules that have a link count of zero: i.e. have no
421 * active initialized modules. If 'all' is set then all modules are unloaded
422 * including static ones.
425 void CONF_modules_unload(int all
)
429 CONF_modules_finish();
430 /* unload modules in reverse order */
431 for (i
= sk_CONF_MODULE_num(supported_modules
) - 1; i
>= 0; i
--) {
432 md
= sk_CONF_MODULE_value(supported_modules
, i
);
433 /* If static or in use and 'all' not set ignore it */
434 if (((md
->links
> 0) || !md
->dso
) && !all
)
436 /* Since we're working in reverse this is OK */
437 (void)sk_CONF_MODULE_delete(supported_modules
, i
);
440 if (sk_CONF_MODULE_num(supported_modules
) == 0) {
441 sk_CONF_MODULE_free(supported_modules
);
442 supported_modules
= NULL
;
446 /* unload a single module */
447 static void module_free(CONF_MODULE
*md
)
450 OPENSSL_free(md
->name
);
454 /* finish and free up all modules instances */
456 void CONF_modules_finish(void)
459 while (sk_CONF_IMODULE_num(initialized_modules
) > 0) {
460 imod
= sk_CONF_IMODULE_pop(initialized_modules
);
463 sk_CONF_IMODULE_free(initialized_modules
);
464 initialized_modules
= NULL
;
467 /* finish a module instance */
469 static void module_finish(CONF_IMODULE
*imod
)
473 if (imod
->pmod
->finish
)
474 imod
->pmod
->finish(imod
);
476 OPENSSL_free(imod
->name
);
477 OPENSSL_free(imod
->value
);
481 /* Add a static module to OpenSSL */
483 int CONF_module_add(const char *name
, conf_init_func
*ifunc
,
484 conf_finish_func
*ffunc
)
486 if (module_add(NULL
, name
, ifunc
, ffunc
))
492 void conf_modules_free_int(void)
494 CONF_modules_finish();
495 CONF_modules_unload(1);
498 /* Utility functions */
500 const char *CONF_imodule_get_name(const CONF_IMODULE
*md
)
505 const char *CONF_imodule_get_value(const CONF_IMODULE
*md
)
510 void *CONF_imodule_get_usr_data(const CONF_IMODULE
*md
)
515 void CONF_imodule_set_usr_data(CONF_IMODULE
*md
, void *usr_data
)
517 md
->usr_data
= usr_data
;
520 CONF_MODULE
*CONF_imodule_get_module(const CONF_IMODULE
*md
)
525 unsigned long CONF_imodule_get_flags(const CONF_IMODULE
*md
)
530 void CONF_imodule_set_flags(CONF_IMODULE
*md
, unsigned long flags
)
535 void *CONF_module_get_usr_data(CONF_MODULE
*pmod
)
537 return pmod
->usr_data
;
540 void CONF_module_set_usr_data(CONF_MODULE
*pmod
, void *usr_data
)
542 pmod
->usr_data
= usr_data
;
545 /* Return default config file name */
547 char *CONF_get1_default_config_file(void)
550 char *file
, *sep
= "";
553 if ((file
= ossl_safe_getenv("OPENSSL_CONF")) != NULL
)
554 return OPENSSL_strdup(file
);
556 t
= X509_get_default_cert_area();
557 #ifndef OPENSSL_SYS_VMS
560 size
= strlen(t
) + strlen(sep
) + strlen(OPENSSL_CONF
) + 1;
561 file
= OPENSSL_malloc(size
);
565 BIO_snprintf(file
, size
, "%s%s%s", t
, sep
, OPENSSL_CONF
);
571 * This function takes a list separated by 'sep' and calls the callback
572 * function giving the start and length of each member optionally stripping
573 * leading and trailing whitespace. This can be used to parse comma separated
577 int CONF_parse_list(const char *list_
, int sep
, int nospc
,
578 int (*list_cb
) (const char *elem
, int len
, void *usr
),
582 const char *lstart
, *tmpend
, *p
;
585 ERR_raise(ERR_LIB_CONF
, CONF_R_LIST_CANNOT_BE_NULL
);
592 while (*lstart
&& isspace((unsigned char)*lstart
))
595 p
= strchr(lstart
, sep
);
596 if (p
== lstart
|| *lstart
== '\0')
597 ret
= list_cb(NULL
, 0, arg
);
602 tmpend
= lstart
+ strlen(lstart
) - 1;
604 while (isspace((unsigned char)*tmpend
))
607 ret
= list_cb(lstart
, tmpend
- lstart
+ 1, arg
);