2 * Copyright 2002-2021 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_data(ERR_LIB_CONF
,
125 CONF_R_OPENSSL_CONF_REFERENCES_MISSING_SECTION
,
126 "openssl_conf=%s", vsection
);
134 for (i
= 0; i
< sk_CONF_VALUE_num(values
); i
++) {
135 vl
= sk_CONF_VALUE_value(values
, i
);
137 ret
= module_run(cnf
, vl
->name
, vl
->value
, flags
);
138 OSSL_TRACE3(CONF
, "Running module %s (%s) returned %d\n",
139 vl
->name
, vl
->value
, ret
);
141 if (!(flags
& CONF_MFLAGS_IGNORE_ERRORS
)) {
142 ERR_clear_last_mark();
152 int CONF_modules_load_file_ex(OSSL_LIB_CTX
*libctx
, const char *filename
,
153 const char *appname
, unsigned long flags
)
157 int ret
= 0, diagnostics
= 0;
159 if (filename
== NULL
) {
160 file
= CONF_get1_default_config_file();
164 file
= (char *)filename
;
168 conf
= NCONF_new_ex(libctx
, NULL
);
172 if (NCONF_load(conf
, file
, NULL
) <= 0) {
173 if ((flags
& CONF_MFLAGS_IGNORE_MISSING_FILE
) &&
174 (ERR_GET_REASON(ERR_peek_last_error()) == CONF_R_NO_SUCH_FILE
)) {
180 ret
= CONF_modules_load(conf
, appname
, flags
);
181 diagnostics
= conf_diagnostics(conf
);
184 if (filename
== NULL
)
188 if ((flags
& CONF_MFLAGS_IGNORE_RETURN_CODES
) != 0 && !diagnostics
)
194 ERR_clear_last_mark();
199 int CONF_modules_load_file(const char *filename
,
200 const char *appname
, unsigned long flags
)
202 return CONF_modules_load_file_ex(NULL
, filename
, appname
, flags
);
205 DEFINE_RUN_ONCE_STATIC(do_load_builtin_modules
)
207 OPENSSL_load_builtin_modules();
208 #ifndef OPENSSL_NO_ENGINE
209 /* Need to load ENGINEs */
210 ENGINE_load_builtin_engines();
215 static int module_run(const CONF
*cnf
, const char *name
, const char *value
,
221 if (!RUN_ONCE(&load_builtin_modules
, do_load_builtin_modules
))
224 md
= module_find(name
);
226 /* Module not found: try to load DSO */
227 if (!md
&& !(flags
& CONF_MFLAGS_NO_DSO
))
228 md
= module_load_dso(cnf
, name
, value
);
231 if (!(flags
& CONF_MFLAGS_SILENT
)) {
232 ERR_raise_data(ERR_LIB_CONF
, CONF_R_UNKNOWN_MODULE_NAME
,
238 ret
= module_init(md
, name
, value
, cnf
);
241 if (!(flags
& CONF_MFLAGS_SILENT
))
242 ERR_raise_data(ERR_LIB_CONF
, CONF_R_MODULE_INITIALIZATION_ERROR
,
243 "module=%s, value=%s retcode=%-8d",
250 /* Load a module from a DSO */
251 static CONF_MODULE
*module_load_dso(const CONF
*cnf
,
252 const char *name
, const char *value
)
255 conf_init_func
*ifunc
;
256 conf_finish_func
*ffunc
;
257 const char *path
= NULL
;
261 /* Look for alternative path in module section */
262 path
= _CONF_get_string(cnf
, value
, "path");
266 dso
= DSO_load(NULL
, path
, NULL
, 0);
268 errcode
= CONF_R_ERROR_LOADING_DSO
;
271 ifunc
= (conf_init_func
*)DSO_bind_func(dso
, DSO_mod_init_name
);
273 errcode
= CONF_R_MISSING_INIT_FUNCTION
;
276 ffunc
= (conf_finish_func
*)DSO_bind_func(dso
, DSO_mod_finish_name
);
277 /* All OK, add module */
278 md
= module_add(dso
, name
, ifunc
, ffunc
);
287 ERR_raise_data(ERR_LIB_CONF
, errcode
, "module=%s, path=%s", name
, path
);
291 /* add module to list */
292 static CONF_MODULE
*module_add(DSO
*dso
, const char *name
,
293 conf_init_func
*ifunc
, conf_finish_func
*ffunc
)
295 CONF_MODULE
*tmod
= NULL
;
296 if (supported_modules
== NULL
)
297 supported_modules
= sk_CONF_MODULE_new_null();
298 if (supported_modules
== NULL
)
300 if ((tmod
= OPENSSL_zalloc(sizeof(*tmod
))) == NULL
) {
301 ERR_raise(ERR_LIB_CONF
, ERR_R_MALLOC_FAILURE
);
306 tmod
->name
= OPENSSL_strdup(name
);
308 tmod
->finish
= ffunc
;
309 if (tmod
->name
== NULL
) {
314 if (!sk_CONF_MODULE_push(supported_modules
, tmod
)) {
315 OPENSSL_free(tmod
->name
);
324 * Find a module from the list. We allow module names of the form
325 * modname.XXXX to just search for modname to allow the same module to be
326 * initialized more than once.
329 static CONF_MODULE
*module_find(const char *name
)
334 p
= strrchr(name
, '.');
339 nchar
= strlen(name
);
341 for (i
= 0; i
< sk_CONF_MODULE_num(supported_modules
); i
++) {
342 tmod
= sk_CONF_MODULE_value(supported_modules
, i
);
343 if (strncmp(tmod
->name
, name
, nchar
) == 0)
351 /* initialize a module */
352 static int module_init(CONF_MODULE
*pmod
, const char *name
, const char *value
,
357 CONF_IMODULE
*imod
= NULL
;
359 /* Otherwise add initialized module to list */
360 imod
= OPENSSL_malloc(sizeof(*imod
));
365 imod
->name
= OPENSSL_strdup(name
);
366 imod
->value
= OPENSSL_strdup(value
);
367 imod
->usr_data
= NULL
;
369 if (!imod
->name
|| !imod
->value
)
372 /* Try to initialize module */
374 ret
= pmod
->init(imod
, cnf
);
376 /* Error occurred, exit */
381 if (initialized_modules
== NULL
) {
382 initialized_modules
= sk_CONF_IMODULE_new_null();
383 if (!initialized_modules
) {
384 ERR_raise(ERR_LIB_CONF
, ERR_R_MALLOC_FAILURE
);
389 if (!sk_CONF_IMODULE_push(initialized_modules
, imod
)) {
390 ERR_raise(ERR_LIB_CONF
, ERR_R_MALLOC_FAILURE
);
400 /* We've started the module so we'd better finish it */
401 if (pmod
->finish
&& init_called
)
406 OPENSSL_free(imod
->name
);
407 OPENSSL_free(imod
->value
);
416 * Unload any dynamic modules that have a link count of zero: i.e. have no
417 * active initialized modules. If 'all' is set then all modules are unloaded
418 * including static ones.
421 void CONF_modules_unload(int all
)
425 CONF_modules_finish();
426 /* unload modules in reverse order */
427 for (i
= sk_CONF_MODULE_num(supported_modules
) - 1; i
>= 0; i
--) {
428 md
= sk_CONF_MODULE_value(supported_modules
, i
);
429 /* If static or in use and 'all' not set ignore it */
430 if (((md
->links
> 0) || !md
->dso
) && !all
)
432 /* Since we're working in reverse this is OK */
433 (void)sk_CONF_MODULE_delete(supported_modules
, i
);
436 if (sk_CONF_MODULE_num(supported_modules
) == 0) {
437 sk_CONF_MODULE_free(supported_modules
);
438 supported_modules
= NULL
;
442 /* unload a single module */
443 static void module_free(CONF_MODULE
*md
)
446 OPENSSL_free(md
->name
);
450 /* finish and free up all modules instances */
452 void CONF_modules_finish(void)
455 while (sk_CONF_IMODULE_num(initialized_modules
) > 0) {
456 imod
= sk_CONF_IMODULE_pop(initialized_modules
);
459 sk_CONF_IMODULE_free(initialized_modules
);
460 initialized_modules
= NULL
;
463 /* finish a module instance */
465 static void module_finish(CONF_IMODULE
*imod
)
469 if (imod
->pmod
->finish
)
470 imod
->pmod
->finish(imod
);
472 OPENSSL_free(imod
->name
);
473 OPENSSL_free(imod
->value
);
477 /* Add a static module to OpenSSL */
479 int CONF_module_add(const char *name
, conf_init_func
*ifunc
,
480 conf_finish_func
*ffunc
)
482 if (module_add(NULL
, name
, ifunc
, ffunc
))
488 void ossl_config_modules_free(void)
490 CONF_modules_finish();
491 CONF_modules_unload(1);
494 /* Utility functions */
496 const char *CONF_imodule_get_name(const CONF_IMODULE
*md
)
501 const char *CONF_imodule_get_value(const CONF_IMODULE
*md
)
506 void *CONF_imodule_get_usr_data(const CONF_IMODULE
*md
)
511 void CONF_imodule_set_usr_data(CONF_IMODULE
*md
, void *usr_data
)
513 md
->usr_data
= usr_data
;
516 CONF_MODULE
*CONF_imodule_get_module(const CONF_IMODULE
*md
)
521 unsigned long CONF_imodule_get_flags(const CONF_IMODULE
*md
)
526 void CONF_imodule_set_flags(CONF_IMODULE
*md
, unsigned long flags
)
531 void *CONF_module_get_usr_data(CONF_MODULE
*pmod
)
533 return pmod
->usr_data
;
536 void CONF_module_set_usr_data(CONF_MODULE
*pmod
, void *usr_data
)
538 pmod
->usr_data
= usr_data
;
541 /* 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 ERR_raise(ERR_LIB_CONF
, 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
);