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_STACK_OF(CONF_VALUE
)
22 DEFINE_STACK_OF(CONF_MODULE
)
23 DEFINE_STACK_OF(CONF_IMODULE
)
25 #define DSO_mod_init_name "OPENSSL_init"
26 #define DSO_mod_finish_name "OPENSSL_finish"
29 * This structure contains a data about supported modules. entries in this
30 * table correspond to either dynamic or static modules.
33 struct conf_module_st
{
34 /* DSO of this module or NULL if static */
36 /* Name of the module */
41 conf_finish_func
*finish
;
42 /* Number of successfully initialized modules */
48 * This structure contains information about modules that have been
49 * successfully initialized. There may be more than one entry for a given
53 struct conf_imodule_st
{
61 static STACK_OF(CONF_MODULE
) *supported_modules
= NULL
;
62 static STACK_OF(CONF_IMODULE
) *initialized_modules
= NULL
;
64 static CRYPTO_ONCE load_builtin_modules
= CRYPTO_ONCE_STATIC_INIT
;
66 static void module_free(CONF_MODULE
*md
);
67 static void module_finish(CONF_IMODULE
*imod
);
68 static int module_run(const CONF
*cnf
, const char *name
, const char *value
,
70 static CONF_MODULE
*module_add(DSO
*dso
, const char *name
,
71 conf_init_func
*ifunc
,
72 conf_finish_func
*ffunc
);
73 static CONF_MODULE
*module_find(const char *name
);
74 static int module_init(CONF_MODULE
*pmod
, const char *name
, const char *value
,
76 static CONF_MODULE
*module_load_dso(const CONF
*cnf
, const char *name
,
79 /* Main function: load modules from a CONF structure */
81 int CONF_modules_load(const CONF
*cnf
, const char *appname
,
84 STACK_OF(CONF_VALUE
) *values
;
86 char *vsection
= NULL
;
94 vsection
= NCONF_get_string(cnf
, NULL
, appname
);
96 if (!appname
|| (!vsection
&& (flags
& CONF_MFLAGS_DEFAULT_SECTION
)))
97 vsection
= NCONF_get_string(cnf
, NULL
, "openssl_conf");
104 OSSL_TRACE1(CONF
, "Configuration in section %s\n", vsection
);
105 values
= NCONF_get_section(cnf
, vsection
);
110 for (i
= 0; i
< sk_CONF_VALUE_num(values
); i
++) {
111 vl
= sk_CONF_VALUE_value(values
, i
);
112 ret
= module_run(cnf
, vl
->name
, vl
->value
, flags
);
113 OSSL_TRACE3(CONF
, "Running module %s (%s) returned %d\n",
114 vl
->name
, vl
->value
, ret
);
116 if (!(flags
& CONF_MFLAGS_IGNORE_ERRORS
))
124 int CONF_modules_load_file_with_libctx(OPENSSL_CTX
*libctx
,
125 const char *filename
,
126 const char *appname
, unsigned long flags
)
132 conf
= NCONF_new_with_libctx(libctx
, NULL
);
136 if (filename
== NULL
) {
137 file
= CONF_get1_default_config_file();
141 file
= (char *)filename
;
144 if (NCONF_load(conf
, file
, NULL
) <= 0) {
145 if ((flags
& CONF_MFLAGS_IGNORE_MISSING_FILE
) &&
146 (ERR_GET_REASON(ERR_peek_last_error()) == CONF_R_NO_SUCH_FILE
)) {
153 ret
= CONF_modules_load(conf
, appname
, flags
);
156 if (filename
== NULL
)
160 if (flags
& CONF_MFLAGS_IGNORE_RETURN_CODES
)
166 int CONF_modules_load_file(const char *filename
,
167 const char *appname
, unsigned long flags
)
169 return CONF_modules_load_file_with_libctx(NULL
, filename
, appname
, flags
);
172 DEFINE_RUN_ONCE_STATIC(do_load_builtin_modules
)
174 OPENSSL_load_builtin_modules();
175 #ifndef OPENSSL_NO_ENGINE
176 /* Need to load ENGINEs */
177 ENGINE_load_builtin_engines();
183 static int module_run(const CONF
*cnf
, const char *name
, const char *value
,
189 if (!RUN_ONCE(&load_builtin_modules
, do_load_builtin_modules
))
192 md
= module_find(name
);
194 /* Module not found: try to load DSO */
195 if (!md
&& !(flags
& CONF_MFLAGS_NO_DSO
))
196 md
= module_load_dso(cnf
, name
, value
);
199 if (!(flags
& CONF_MFLAGS_SILENT
)) {
200 CONFerr(CONF_F_MODULE_RUN
, CONF_R_UNKNOWN_MODULE_NAME
);
201 ERR_add_error_data(2, "module=", name
);
206 ret
= module_init(md
, name
, value
, cnf
);
209 if (!(flags
& CONF_MFLAGS_SILENT
)) {
210 char rcode
[DECIMAL_SIZE(ret
) + 1];
212 CONFerr(CONF_F_MODULE_RUN
, CONF_R_MODULE_INITIALIZATION_ERROR
);
213 BIO_snprintf(rcode
, sizeof(rcode
), "%-8d", ret
);
214 ERR_add_error_data(6, "module=", name
, ", value=", value
,
215 ", retcode=", rcode
);
222 /* Load a module from a DSO */
223 static CONF_MODULE
*module_load_dso(const CONF
*cnf
,
224 const char *name
, const char *value
)
227 conf_init_func
*ifunc
;
228 conf_finish_func
*ffunc
;
229 const char *path
= NULL
;
233 /* Look for alternative path in module section */
234 path
= NCONF_get_string(cnf
, value
, "path");
239 dso
= DSO_load(NULL
, path
, NULL
, 0);
241 errcode
= CONF_R_ERROR_LOADING_DSO
;
244 ifunc
= (conf_init_func
*)DSO_bind_func(dso
, DSO_mod_init_name
);
246 errcode
= CONF_R_MISSING_INIT_FUNCTION
;
249 ffunc
= (conf_finish_func
*)DSO_bind_func(dso
, DSO_mod_finish_name
);
250 /* All OK, add module */
251 md
= module_add(dso
, name
, ifunc
, ffunc
);
260 CONFerr(CONF_F_MODULE_LOAD_DSO
, errcode
);
261 ERR_add_error_data(4, "module=", name
, ", path=", path
);
265 /* add module to list */
266 static CONF_MODULE
*module_add(DSO
*dso
, const char *name
,
267 conf_init_func
*ifunc
, conf_finish_func
*ffunc
)
269 CONF_MODULE
*tmod
= NULL
;
270 if (supported_modules
== NULL
)
271 supported_modules
= sk_CONF_MODULE_new_null();
272 if (supported_modules
== NULL
)
274 if ((tmod
= OPENSSL_zalloc(sizeof(*tmod
))) == NULL
) {
275 CONFerr(CONF_F_MODULE_ADD
, ERR_R_MALLOC_FAILURE
);
280 tmod
->name
= OPENSSL_strdup(name
);
282 tmod
->finish
= ffunc
;
283 if (tmod
->name
== NULL
) {
288 if (!sk_CONF_MODULE_push(supported_modules
, tmod
)) {
289 OPENSSL_free(tmod
->name
);
298 * Find a module from the list. We allow module names of the form
299 * modname.XXXX to just search for modname to allow the same module to be
300 * initialized more than once.
303 static CONF_MODULE
*module_find(const char *name
)
308 p
= strrchr(name
, '.');
313 nchar
= strlen(name
);
315 for (i
= 0; i
< sk_CONF_MODULE_num(supported_modules
); i
++) {
316 tmod
= sk_CONF_MODULE_value(supported_modules
, i
);
317 if (strncmp(tmod
->name
, name
, nchar
) == 0)
325 /* initialize a module */
326 static int module_init(CONF_MODULE
*pmod
, const char *name
, const char *value
,
331 CONF_IMODULE
*imod
= NULL
;
333 /* Otherwise add initialized module to list */
334 imod
= OPENSSL_malloc(sizeof(*imod
));
339 imod
->name
= OPENSSL_strdup(name
);
340 imod
->value
= OPENSSL_strdup(value
);
341 imod
->usr_data
= NULL
;
343 if (!imod
->name
|| !imod
->value
)
346 /* Try to initialize module */
348 ret
= pmod
->init(imod
, cnf
);
350 /* Error occurred, exit */
355 if (initialized_modules
== NULL
) {
356 initialized_modules
= sk_CONF_IMODULE_new_null();
357 if (!initialized_modules
) {
358 CONFerr(CONF_F_MODULE_INIT
, ERR_R_MALLOC_FAILURE
);
363 if (!sk_CONF_IMODULE_push(initialized_modules
, imod
)) {
364 CONFerr(CONF_F_MODULE_INIT
, ERR_R_MALLOC_FAILURE
);
374 /* We've started the module so we'd better finish it */
375 if (pmod
->finish
&& init_called
)
380 OPENSSL_free(imod
->name
);
381 OPENSSL_free(imod
->value
);
390 * Unload any dynamic modules that have a link count of zero: i.e. have no
391 * active initialized modules. If 'all' is set then all modules are unloaded
392 * including static ones.
395 void CONF_modules_unload(int all
)
399 CONF_modules_finish();
400 /* unload modules in reverse order */
401 for (i
= sk_CONF_MODULE_num(supported_modules
) - 1; i
>= 0; i
--) {
402 md
= sk_CONF_MODULE_value(supported_modules
, i
);
403 /* If static or in use and 'all' not set ignore it */
404 if (((md
->links
> 0) || !md
->dso
) && !all
)
406 /* Since we're working in reverse this is OK */
407 (void)sk_CONF_MODULE_delete(supported_modules
, i
);
410 if (sk_CONF_MODULE_num(supported_modules
) == 0) {
411 sk_CONF_MODULE_free(supported_modules
);
412 supported_modules
= NULL
;
416 /* unload a single module */
417 static void module_free(CONF_MODULE
*md
)
420 OPENSSL_free(md
->name
);
424 /* finish and free up all modules instances */
426 void CONF_modules_finish(void)
429 while (sk_CONF_IMODULE_num(initialized_modules
) > 0) {
430 imod
= sk_CONF_IMODULE_pop(initialized_modules
);
433 sk_CONF_IMODULE_free(initialized_modules
);
434 initialized_modules
= NULL
;
437 /* finish a module instance */
439 static void module_finish(CONF_IMODULE
*imod
)
443 if (imod
->pmod
->finish
)
444 imod
->pmod
->finish(imod
);
446 OPENSSL_free(imod
->name
);
447 OPENSSL_free(imod
->value
);
451 /* Add a static module to OpenSSL */
453 int CONF_module_add(const char *name
, conf_init_func
*ifunc
,
454 conf_finish_func
*ffunc
)
456 if (module_add(NULL
, name
, ifunc
, ffunc
))
462 void conf_modules_free_int(void)
464 CONF_modules_finish();
465 CONF_modules_unload(1);
468 /* Utility functions */
470 const char *CONF_imodule_get_name(const CONF_IMODULE
*md
)
475 const char *CONF_imodule_get_value(const CONF_IMODULE
*md
)
480 void *CONF_imodule_get_usr_data(const CONF_IMODULE
*md
)
485 void CONF_imodule_set_usr_data(CONF_IMODULE
*md
, void *usr_data
)
487 md
->usr_data
= usr_data
;
490 CONF_MODULE
*CONF_imodule_get_module(const CONF_IMODULE
*md
)
495 unsigned long CONF_imodule_get_flags(const CONF_IMODULE
*md
)
500 void CONF_imodule_set_flags(CONF_IMODULE
*md
, unsigned long flags
)
505 void *CONF_module_get_usr_data(CONF_MODULE
*pmod
)
507 return pmod
->usr_data
;
510 void CONF_module_set_usr_data(CONF_MODULE
*pmod
, void *usr_data
)
512 pmod
->usr_data
= usr_data
;
515 /* Return default config file name */
517 char *CONF_get1_default_config_file(void)
519 char *file
, *sep
= "";
522 if ((file
= ossl_safe_getenv("OPENSSL_CONF")) != NULL
)
523 return OPENSSL_strdup(file
);
525 len
= strlen(X509_get_default_cert_area());
526 #ifndef OPENSSL_SYS_VMS
530 len
+= strlen(OPENSSL_CONF
);
532 file
= OPENSSL_malloc(len
+ 1);
536 BIO_snprintf(file
, len
+ 1, "%s%s%s", X509_get_default_cert_area(),
543 * This function takes a list separated by 'sep' and calls the callback
544 * function giving the start and length of each member optionally stripping
545 * leading and trailing whitespace. This can be used to parse comma separated
549 int CONF_parse_list(const char *list_
, int sep
, int nospc
,
550 int (*list_cb
) (const char *elem
, int len
, void *usr
),
554 const char *lstart
, *tmpend
, *p
;
557 CONFerr(CONF_F_CONF_PARSE_LIST
, CONF_R_LIST_CANNOT_BE_NULL
);
564 while (*lstart
&& isspace((unsigned char)*lstart
))
567 p
= strchr(lstart
, sep
);
568 if (p
== lstart
|| *lstart
== '\0')
569 ret
= list_cb(NULL
, 0, arg
);
574 tmpend
= lstart
+ strlen(lstart
) - 1;
576 while (isspace((unsigned char)*tmpend
))
579 ret
= list_cb(lstart
, tmpend
- lstart
+ 1, arg
);