1 /* $OpenBSD: conf_mod.c,v 1.27 2017/01/29 17:49:22 beck Exp $ */
2 /* Written by Stephen Henson (steve@openssl.org) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 2001 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
64 #include <openssl/conf.h>
65 #include <openssl/crypto.h>
66 #include <openssl/dso.h>
67 #include <openssl/err.h>
68 #include <openssl/x509.h>
70 #define DSO_mod_init_name "OPENSSL_init"
71 #define DSO_mod_finish_name "OPENSSL_finish"
73 /* This structure contains a data about supported modules.
74 * entries in this table correspond to either dynamic or
78 struct conf_module_st
{
79 /* DSO of this module or NULL if static */
81 /* Name of the module */
86 conf_finish_func
*finish
;
87 /* Number of successfully initialized modules */
93 /* This structure contains information about modules that have been
94 * successfully initialized. There may be more than one entry for a
98 struct conf_imodule_st
{
106 static STACK_OF(CONF_MODULE
) *supported_modules
= NULL
;
107 static STACK_OF(CONF_IMODULE
) *initialized_modules
= NULL
;
109 static void module_free(CONF_MODULE
*md
);
110 static void module_finish(CONF_IMODULE
*imod
);
111 static int module_run(const CONF
*cnf
, char *name
, char *value
,
112 unsigned long flags
);
113 static CONF_MODULE
*module_add(DSO
*dso
, const char *name
,
114 conf_init_func
*ifunc
, conf_finish_func
*ffunc
);
115 static CONF_MODULE
*module_find(char *name
);
116 static int module_init(CONF_MODULE
*pmod
, char *name
, char *value
,
118 static CONF_MODULE
*module_load_dso(const CONF
*cnf
, char *name
, char *value
,
119 unsigned long flags
);
121 /* Main function: load modules from a CONF structure */
124 CONF_modules_load(const CONF
*cnf
, const char *appname
, unsigned long flags
)
126 STACK_OF(CONF_VALUE
) *values
;
128 char *vsection
= NULL
;
136 vsection
= NCONF_get_string(cnf
, NULL
, appname
);
138 if (!appname
|| (!vsection
&& (flags
& CONF_MFLAGS_DEFAULT_SECTION
)))
139 vsection
= NCONF_get_string(cnf
, NULL
, "openssl_conf");
146 values
= NCONF_get_section(cnf
, vsection
);
151 for (i
= 0; i
< sk_CONF_VALUE_num(values
); i
++) {
152 vl
= sk_CONF_VALUE_value(values
, i
);
153 ret
= module_run(cnf
, vl
->name
, vl
->value
, flags
);
155 if (!(flags
& CONF_MFLAGS_IGNORE_ERRORS
))
163 CONF_modules_load_file(const char *filename
, const char *appname
,
169 conf
= NCONF_new(NULL
);
173 if (filename
== NULL
) {
174 file
= CONF_get1_default_config_file();
178 file
= (char *)filename
;
180 if (NCONF_load(conf
, file
, NULL
) <= 0) {
181 if ((flags
& CONF_MFLAGS_IGNORE_MISSING_FILE
) &&
182 (ERR_GET_REASON(ERR_peek_last_error()) ==
183 CONF_R_NO_SUCH_FILE
)) {
190 ret
= CONF_modules_load(conf
, appname
, flags
);
193 if (filename
== NULL
)
201 module_run(const CONF
*cnf
, char *name
, char *value
, unsigned long flags
)
206 md
= module_find(name
);
208 /* Module not found: try to load DSO */
209 if (!md
&& !(flags
& CONF_MFLAGS_NO_DSO
))
210 md
= module_load_dso(cnf
, name
, value
, flags
);
213 if (!(flags
& CONF_MFLAGS_SILENT
)) {
214 CONFerror(CONF_R_UNKNOWN_MODULE_NAME
);
215 ERR_asprintf_error_data("module=%s", name
);
220 ret
= module_init(md
, name
, value
, cnf
);
223 if (!(flags
& CONF_MFLAGS_SILENT
)) {
224 CONFerror(CONF_R_MODULE_INITIALIZATION_ERROR
);
225 ERR_asprintf_error_data
226 ("module=%s, value=%s, retcode=%-8d",
234 /* Load a module from a DSO */
236 module_load_dso(const CONF
*cnf
, char *name
, char *value
, unsigned long flags
)
239 conf_init_func
*ifunc
;
240 conf_finish_func
*ffunc
;
245 /* Look for alternative path in module section */
246 path
= NCONF_get_string(cnf
, value
, "path");
251 dso
= DSO_load(NULL
, path
, NULL
, 0);
253 errcode
= CONF_R_ERROR_LOADING_DSO
;
256 ifunc
= (conf_init_func
*)DSO_bind_func(dso
, DSO_mod_init_name
);
258 errcode
= CONF_R_MISSING_INIT_FUNCTION
;
261 ffunc
= (conf_finish_func
*)DSO_bind_func(dso
, DSO_mod_finish_name
);
262 /* All OK, add module */
263 md
= module_add(dso
, name
, ifunc
, ffunc
);
274 ERR_asprintf_error_data("module=%s, path=%s", name
, path
);
278 /* add module to list */
280 module_add(DSO
*dso
, const char *name
, conf_init_func
*ifunc
,
281 conf_finish_func
*ffunc
)
283 CONF_MODULE
*tmod
= NULL
;
287 if (supported_modules
== NULL
)
288 supported_modules
= sk_CONF_MODULE_new_null();
289 if (supported_modules
== NULL
)
291 tmod
= malloc(sizeof(CONF_MODULE
));
296 tmod
->name
= strdup(name
);
298 tmod
->finish
= ffunc
;
301 if (!sk_CONF_MODULE_push(supported_modules
, tmod
)) {
309 /* Find a module from the list. We allow module names of the
310 * form modname.XXXX to just search for modname to allow the
311 * same module to be initialized more than once.
315 module_find(char *name
)
321 p
= strrchr(name
, '.');
326 nchar
= strlen(name
);
328 for (i
= 0; i
< sk_CONF_MODULE_num(supported_modules
); i
++) {
329 tmod
= sk_CONF_MODULE_value(supported_modules
, i
);
330 if (!strncmp(tmod
->name
, name
, nchar
))
337 /* initialize a module */
339 module_init(CONF_MODULE
*pmod
, char *name
, char *value
, const CONF
*cnf
)
343 CONF_IMODULE
*imod
= NULL
;
345 /* Otherwise add initialized module to list */
346 imod
= malloc(sizeof(CONF_IMODULE
));
351 imod
->name
= name
? strdup(name
) : NULL
;
352 imod
->value
= value
? strdup(value
) : NULL
;
353 imod
->usr_data
= NULL
;
355 if (!imod
->name
|| !imod
->value
)
358 /* Try to initialize module */
360 ret
= pmod
->init(imod
, cnf
);
362 /* Error occurred, exit */
367 if (initialized_modules
== NULL
) {
368 initialized_modules
= sk_CONF_IMODULE_new_null();
369 if (!initialized_modules
) {
370 CONFerror(ERR_R_MALLOC_FAILURE
);
375 if (!sk_CONF_IMODULE_push(initialized_modules
, imod
)) {
376 CONFerror(ERR_R_MALLOC_FAILURE
);
385 /* We've started the module so we'd better finish it */
386 if (pmod
->finish
&& init_called
)
399 /* Unload any dynamic modules that have a link count of zero:
400 * i.e. have no active initialized modules. If 'all' is set
401 * then all modules are unloaded including static ones.
405 CONF_modules_unload(int all
)
410 CONF_modules_finish();
412 /* unload modules in reverse order */
413 for (i
= sk_CONF_MODULE_num(supported_modules
) - 1; i
>= 0; i
--) {
414 md
= sk_CONF_MODULE_value(supported_modules
, i
);
415 /* If static or in use and 'all' not set ignore it */
416 if (((md
->links
> 0) || !md
->dso
) && !all
)
418 /* Since we're working in reverse this is OK */
419 (void)sk_CONF_MODULE_delete(supported_modules
, i
);
422 if (sk_CONF_MODULE_num(supported_modules
) == 0) {
423 sk_CONF_MODULE_free(supported_modules
);
424 supported_modules
= NULL
;
428 /* unload a single module */
430 module_free(CONF_MODULE
*md
)
438 /* finish and free up all modules instances */
441 CONF_modules_finish(void)
445 while (sk_CONF_IMODULE_num(initialized_modules
) > 0) {
446 imod
= sk_CONF_IMODULE_pop(initialized_modules
);
449 sk_CONF_IMODULE_free(initialized_modules
);
450 initialized_modules
= NULL
;
453 /* finish a module instance */
456 module_finish(CONF_IMODULE
*imod
)
458 if (imod
->pmod
->finish
)
459 imod
->pmod
->finish(imod
);
466 /* Add a static module to OpenSSL */
469 CONF_module_add(const char *name
, conf_init_func
*ifunc
,
470 conf_finish_func
*ffunc
)
472 if (module_add(NULL
, name
, ifunc
, ffunc
))
479 CONF_modules_free(void)
481 CONF_modules_finish();
482 CONF_modules_unload(1);
485 /* Utility functions */
488 CONF_imodule_get_name(const CONF_IMODULE
*md
)
494 CONF_imodule_get_value(const CONF_IMODULE
*md
)
500 CONF_imodule_get_usr_data(const CONF_IMODULE
*md
)
506 CONF_imodule_set_usr_data(CONF_IMODULE
*md
, void *usr_data
)
508 md
->usr_data
= usr_data
;
512 CONF_imodule_get_module(const CONF_IMODULE
*md
)
518 CONF_imodule_get_flags(const CONF_IMODULE
*md
)
524 CONF_imodule_set_flags(CONF_IMODULE
*md
, unsigned long flags
)
530 CONF_module_get_usr_data(CONF_MODULE
*pmod
)
532 return pmod
->usr_data
;
536 CONF_module_set_usr_data(CONF_MODULE
*pmod
, void *usr_data
)
538 pmod
->usr_data
= usr_data
;
541 /* Return default config file name */
544 CONF_get1_default_config_file(void)
548 if (asprintf(&file
, "%s/openssl.cnf",
549 X509_get_default_cert_area()) == -1)
554 /* This function takes a list separated by 'sep' and calls the
555 * callback function giving the start and length of each member
556 * optionally stripping leading and trailing whitespace. This can
557 * be used to parse comma separated lists for example.
561 CONF_parse_list(const char *list_
, int sep
, int nospc
,
562 int (*list_cb
)(const char *elem
, int len
, void *usr
), void *arg
)
565 const char *lstart
, *tmpend
, *p
;
568 CONFerror(CONF_R_LIST_CANNOT_BE_NULL
);
575 while (*lstart
&& isspace((unsigned char)*lstart
))
578 p
= strchr(lstart
, sep
);
579 if (p
== lstart
|| !*lstart
)
580 ret
= list_cb(NULL
, 0, arg
);
585 tmpend
= lstart
+ strlen(lstart
) - 1;
587 while (isspace((unsigned char)*tmpend
))
590 ret
= list_cb(lstart
, tmpend
- lstart
+ 1, arg
);