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>
17 #include <openssl/trace.h>
19 #define DSO_mod_init_name "OPENSSL_init"
20 #define DSO_mod_finish_name "OPENSSL_finish"
23 * This structure contains a data about supported modules. entries in this
24 * table correspond to either dynamic or static modules.
27 struct conf_module_st
{
28 /* DSO of this module or NULL if static */
30 /* Name of the module */
35 conf_finish_func
*finish
;
36 /* Number of successfully initialized modules */
42 * This structure contains information about modules that have been
43 * successfully initialized. There may be more than one entry for a given
47 struct conf_imodule_st
{
55 static STACK_OF(CONF_MODULE
) *supported_modules
= NULL
;
56 static STACK_OF(CONF_IMODULE
) *initialized_modules
= NULL
;
58 static void module_free(CONF_MODULE
*md
);
59 static void module_finish(CONF_IMODULE
*imod
);
60 static int module_run(const CONF
*cnf
, const char *name
, const char *value
,
62 static CONF_MODULE
*module_add(DSO
*dso
, const char *name
,
63 conf_init_func
*ifunc
,
64 conf_finish_func
*ffunc
);
65 static CONF_MODULE
*module_find(const char *name
);
66 static int module_init(CONF_MODULE
*pmod
, const char *name
, const char *value
,
68 static CONF_MODULE
*module_load_dso(const CONF
*cnf
, const char *name
,
71 /* Main function: load modules from a CONF structure */
73 int CONF_modules_load(const CONF
*cnf
, const char *appname
,
76 STACK_OF(CONF_VALUE
) *values
;
78 char *vsection
= NULL
;
86 vsection
= NCONF_get_string(cnf
, NULL
, appname
);
88 if (!appname
|| (!vsection
&& (flags
& CONF_MFLAGS_DEFAULT_SECTION
)))
89 vsection
= NCONF_get_string(cnf
, NULL
, "openssl_conf");
96 OSSL_TRACE1(CONF
, "Configuration in section %s\n", vsection
);
97 values
= NCONF_get_section(cnf
, vsection
);
102 for (i
= 0; i
< sk_CONF_VALUE_num(values
); i
++) {
103 vl
= sk_CONF_VALUE_value(values
, i
);
104 ret
= module_run(cnf
, vl
->name
, vl
->value
, flags
);
105 OSSL_TRACE3(CONF
, "Running module %s (%s) returned %d\n",
106 vl
->name
, vl
->value
, ret
);
108 if (!(flags
& CONF_MFLAGS_IGNORE_ERRORS
))
116 int CONF_modules_load_file(const char *filename
, const char *appname
,
122 conf
= NCONF_new(NULL
);
126 if (filename
== NULL
) {
127 file
= CONF_get1_default_config_file();
131 file
= (char *)filename
;
133 if (NCONF_load(conf
, file
, NULL
) <= 0) {
134 if ((flags
& CONF_MFLAGS_IGNORE_MISSING_FILE
) &&
135 (ERR_GET_REASON(ERR_peek_last_error()) == CONF_R_NO_SUCH_FILE
)) {
142 ret
= CONF_modules_load(conf
, appname
, flags
);
145 if (filename
== NULL
)
149 if (flags
& CONF_MFLAGS_IGNORE_RETURN_CODES
)
155 static int module_run(const CONF
*cnf
, const char *name
, const char *value
,
161 md
= module_find(name
);
163 /* Module not found: try to load DSO */
164 if (!md
&& !(flags
& CONF_MFLAGS_NO_DSO
))
165 md
= module_load_dso(cnf
, name
, value
);
168 if (!(flags
& CONF_MFLAGS_SILENT
)) {
169 CONFerr(CONF_F_MODULE_RUN
, CONF_R_UNKNOWN_MODULE_NAME
);
170 ERR_add_error_data(2, "module=", name
);
175 ret
= module_init(md
, name
, value
, cnf
);
178 if (!(flags
& CONF_MFLAGS_SILENT
)) {
179 char rcode
[DECIMAL_SIZE(ret
) + 1];
181 CONFerr(CONF_F_MODULE_RUN
, CONF_R_MODULE_INITIALIZATION_ERROR
);
182 BIO_snprintf(rcode
, sizeof(rcode
), "%-8d", ret
);
183 ERR_add_error_data(6, "module=", name
, ", value=", value
,
184 ", retcode=", rcode
);
191 /* Load a module from a DSO */
192 static CONF_MODULE
*module_load_dso(const CONF
*cnf
,
193 const char *name
, const char *value
)
196 conf_init_func
*ifunc
;
197 conf_finish_func
*ffunc
;
198 const char *path
= NULL
;
202 /* Look for alternative path in module section */
203 path
= NCONF_get_string(cnf
, value
, "path");
208 dso
= DSO_load(NULL
, path
, NULL
, 0);
210 errcode
= CONF_R_ERROR_LOADING_DSO
;
213 ifunc
= (conf_init_func
*)DSO_bind_func(dso
, DSO_mod_init_name
);
215 errcode
= CONF_R_MISSING_INIT_FUNCTION
;
218 ffunc
= (conf_finish_func
*)DSO_bind_func(dso
, DSO_mod_finish_name
);
219 /* All OK, add module */
220 md
= module_add(dso
, name
, ifunc
, ffunc
);
229 CONFerr(CONF_F_MODULE_LOAD_DSO
, errcode
);
230 ERR_add_error_data(4, "module=", name
, ", path=", path
);
234 /* add module to list */
235 static CONF_MODULE
*module_add(DSO
*dso
, const char *name
,
236 conf_init_func
*ifunc
, conf_finish_func
*ffunc
)
238 CONF_MODULE
*tmod
= NULL
;
239 if (supported_modules
== NULL
)
240 supported_modules
= sk_CONF_MODULE_new_null();
241 if (supported_modules
== NULL
)
243 if ((tmod
= OPENSSL_zalloc(sizeof(*tmod
))) == NULL
) {
244 CONFerr(CONF_F_MODULE_ADD
, ERR_R_MALLOC_FAILURE
);
249 tmod
->name
= OPENSSL_strdup(name
);
251 tmod
->finish
= ffunc
;
252 if (tmod
->name
== NULL
) {
257 if (!sk_CONF_MODULE_push(supported_modules
, tmod
)) {
258 OPENSSL_free(tmod
->name
);
267 * Find a module from the list. We allow module names of the form
268 * modname.XXXX to just search for modname to allow the same module to be
269 * initialized more than once.
272 static CONF_MODULE
*module_find(const char *name
)
277 p
= strrchr(name
, '.');
282 nchar
= strlen(name
);
284 for (i
= 0; i
< sk_CONF_MODULE_num(supported_modules
); i
++) {
285 tmod
= sk_CONF_MODULE_value(supported_modules
, i
);
286 if (strncmp(tmod
->name
, name
, nchar
) == 0)
294 /* initialize a module */
295 static int module_init(CONF_MODULE
*pmod
, const char *name
, const char *value
,
300 CONF_IMODULE
*imod
= NULL
;
302 /* Otherwise add initialized module to list */
303 imod
= OPENSSL_malloc(sizeof(*imod
));
308 imod
->name
= OPENSSL_strdup(name
);
309 imod
->value
= OPENSSL_strdup(value
);
310 imod
->usr_data
= NULL
;
312 if (!imod
->name
|| !imod
->value
)
315 /* Try to initialize module */
317 ret
= pmod
->init(imod
, cnf
);
319 /* Error occurred, exit */
324 if (initialized_modules
== NULL
) {
325 initialized_modules
= sk_CONF_IMODULE_new_null();
326 if (!initialized_modules
) {
327 CONFerr(CONF_F_MODULE_INIT
, ERR_R_MALLOC_FAILURE
);
332 if (!sk_CONF_IMODULE_push(initialized_modules
, imod
)) {
333 CONFerr(CONF_F_MODULE_INIT
, ERR_R_MALLOC_FAILURE
);
343 /* We've started the module so we'd better finish it */
344 if (pmod
->finish
&& init_called
)
349 OPENSSL_free(imod
->name
);
350 OPENSSL_free(imod
->value
);
359 * Unload any dynamic modules that have a link count of zero: i.e. have no
360 * active initialized modules. If 'all' is set then all modules are unloaded
361 * including static ones.
364 void CONF_modules_unload(int all
)
368 CONF_modules_finish();
369 /* unload modules in reverse order */
370 for (i
= sk_CONF_MODULE_num(supported_modules
) - 1; i
>= 0; i
--) {
371 md
= sk_CONF_MODULE_value(supported_modules
, i
);
372 /* If static or in use and 'all' not set ignore it */
373 if (((md
->links
> 0) || !md
->dso
) && !all
)
375 /* Since we're working in reverse this is OK */
376 (void)sk_CONF_MODULE_delete(supported_modules
, i
);
379 if (sk_CONF_MODULE_num(supported_modules
) == 0) {
380 sk_CONF_MODULE_free(supported_modules
);
381 supported_modules
= NULL
;
385 /* unload a single module */
386 static void module_free(CONF_MODULE
*md
)
389 OPENSSL_free(md
->name
);
393 /* finish and free up all modules instances */
395 void CONF_modules_finish(void)
398 while (sk_CONF_IMODULE_num(initialized_modules
) > 0) {
399 imod
= sk_CONF_IMODULE_pop(initialized_modules
);
402 sk_CONF_IMODULE_free(initialized_modules
);
403 initialized_modules
= NULL
;
406 /* finish a module instance */
408 static void module_finish(CONF_IMODULE
*imod
)
412 if (imod
->pmod
->finish
)
413 imod
->pmod
->finish(imod
);
415 OPENSSL_free(imod
->name
);
416 OPENSSL_free(imod
->value
);
420 /* Add a static module to OpenSSL */
422 int CONF_module_add(const char *name
, conf_init_func
*ifunc
,
423 conf_finish_func
*ffunc
)
425 if (module_add(NULL
, name
, ifunc
, ffunc
))
431 void conf_modules_free_int(void)
433 CONF_modules_finish();
434 CONF_modules_unload(1);
437 /* Utility functions */
439 const char *CONF_imodule_get_name(const CONF_IMODULE
*md
)
444 const char *CONF_imodule_get_value(const CONF_IMODULE
*md
)
449 void *CONF_imodule_get_usr_data(const CONF_IMODULE
*md
)
454 void CONF_imodule_set_usr_data(CONF_IMODULE
*md
, void *usr_data
)
456 md
->usr_data
= usr_data
;
459 CONF_MODULE
*CONF_imodule_get_module(const CONF_IMODULE
*md
)
464 unsigned long CONF_imodule_get_flags(const CONF_IMODULE
*md
)
469 void CONF_imodule_set_flags(CONF_IMODULE
*md
, unsigned long flags
)
474 void *CONF_module_get_usr_data(CONF_MODULE
*pmod
)
476 return pmod
->usr_data
;
479 void CONF_module_set_usr_data(CONF_MODULE
*pmod
, void *usr_data
)
481 pmod
->usr_data
= usr_data
;
484 /* Return default config file name */
486 char *CONF_get1_default_config_file(void)
488 char *file
, *sep
= "";
491 if ((file
= ossl_safe_getenv("OPENSSL_CONF")) != NULL
)
492 return OPENSSL_strdup(file
);
494 len
= strlen(X509_get_default_cert_area());
495 #ifndef OPENSSL_SYS_VMS
499 len
+= strlen(OPENSSL_CONF
);
501 file
= OPENSSL_malloc(len
+ 1);
505 BIO_snprintf(file
, len
+ 1, "%s%s%s", X509_get_default_cert_area(),
512 * This function takes a list separated by 'sep' and calls the callback
513 * function giving the start and length of each member optionally stripping
514 * leading and trailing whitespace. This can be used to parse comma separated
518 int CONF_parse_list(const char *list_
, int sep
, int nospc
,
519 int (*list_cb
) (const char *elem
, int len
, void *usr
),
523 const char *lstart
, *tmpend
, *p
;
526 CONFerr(CONF_F_CONF_PARSE_LIST
, CONF_R_LIST_CANNOT_BE_NULL
);
533 while (*lstart
&& isspace((unsigned char)*lstart
))
536 p
= strchr(lstart
, sep
);
537 if (p
== lstart
|| *lstart
== '\0')
538 ret
= list_cb(NULL
, 0, arg
);
543 tmpend
= lstart
+ strlen(lstart
) - 1;
545 while (isspace((unsigned char)*tmpend
))
548 ret
= list_cb(lstart
, tmpend
- lstart
+ 1, arg
);