1 /* conf_mod.c */
2 /* Written by Stephen Henson (shenson@bigfoot.com) for the OpenSSL
3  * project 2001.
4  */
5 /* ====================================================================
6  * Copyright (c) 2001 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
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
18  *    distribution.
19  *
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/)"
24  *
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.
29  *
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.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
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  * ====================================================================
52  *
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).
56  *
57  */
58 
59 #include <stdio.h>
60 #include <ctype.h>
61 #include <openssl/crypto.h>
62 #include "cryptlib.h"
63 #include <openssl/conf.h>
64 #include <openssl/dso.h>
65 #include <openssl/x509.h>
66 
67 __RCSID("$MirOS: src/lib/libssl/src/crypto/conf/conf_mod.c,v 1.2 2014/06/05 12:47:58 tg Exp $");
68 
69 #define DSO_mod_init_name "OPENSSL_init"
70 #define DSO_mod_finish_name "OPENSSL_finish"
71 
72 
73 /* This structure contains a data about supported modules.
74  * entries in this table correspond to either dynamic or
75  * static modules.
76  */
77 
78 struct conf_module_st
79 	{
80 	/* DSO of this module or NULL if static */
81 	DSO *dso;
82 	/* Name of the module */
83 	char *name;
84 	/* Init function */
85 	conf_init_func *init;
86 	/* Finish function */
87 	conf_finish_func *finish;
88 	/* Number of successfully initialized modules */
89 	int links;
90 	void *usr_data;
91 	};
92 
93 
94 /* This structure contains information about modules that have been
95  * successfully initialized. There may be more than one entry for a
96  * given module.
97  */
98 
99 struct conf_imodule_st
100 	{
101 	CONF_MODULE *pmod;
102 	char *name;
103 	char *value;
104 	unsigned long flags;
105 	void *usr_data;
106 	};
107 
108 static STACK_OF(CONF_MODULE) *supported_modules = NULL;
109 static STACK_OF(CONF_IMODULE) *initialized_modules = NULL;
110 
111 static void module_free(CONF_MODULE *md);
112 static void module_finish(CONF_IMODULE *imod);
113 static int module_run(const CONF *cnf, char *name, char *value,
114 					  unsigned long flags);
115 static CONF_MODULE *module_add(DSO *dso, const char *name,
116 			conf_init_func *ifunc, conf_finish_func *ffunc);
117 static CONF_MODULE *module_find(char *name);
118 static int module_init(CONF_MODULE *pmod, char *name, char *value,
119 					   const CONF *cnf);
120 static CONF_MODULE *module_load_dso(const CONF *cnf, char *name, char *value,
121 									unsigned long flags);
122 
123 /* Main function: load modules from a CONF structure */
124 
CONF_modules_load(const CONF * cnf,const char * appname,unsigned long flags)125 int CONF_modules_load(const CONF *cnf, const char *appname,
126 		      unsigned long flags)
127 	{
128 	STACK_OF(CONF_VALUE) *values;
129 	CONF_VALUE *vl;
130 	char *vsection = NULL;
131 
132 	int ret, i;
133 
134 	if (!cnf)
135 		return 1;
136 
137 	if (appname)
138 		vsection = NCONF_get_string(cnf, NULL, appname);
139 
140 	if (!appname || (!vsection && (flags & CONF_MFLAGS_DEFAULT_SECTION)))
141 		vsection = NCONF_get_string(cnf, NULL, "openssl_conf");
142 
143 	if (!vsection)
144 		{
145 		ERR_clear_error();
146 		return 1;
147 		}
148 
149 	values = NCONF_get_section(cnf, vsection);
150 
151 	if (!values)
152 		return 0;
153 
154 	for (i = 0; i < sk_CONF_VALUE_num(values); i++)
155 		{
156 		vl = sk_CONF_VALUE_value(values, i);
157 		ret = module_run(cnf, vl->name, vl->value, flags);
158 		if (ret <= 0)
159 			if(!(flags & CONF_MFLAGS_IGNORE_ERRORS))
160 				return ret;
161 		}
162 
163 	return 1;
164 
165 	}
166 
CONF_modules_load_file(const char * filename,const char * appname,unsigned long flags)167 int CONF_modules_load_file(const char *filename, const char *appname,
168 			   unsigned long flags)
169 	{
170 	char *file = NULL;
171 	CONF *conf = NULL;
172 	int ret = 0;
173 	conf = NCONF_new(NULL);
174 	if (!conf)
175 		goto err;
176 
177 	if (filename == NULL)
178 		{
179 		file = CONF_get1_default_config_file();
180 		if (!file)
181 			goto err;
182 		}
183 	else
184 		file = (char *)filename;
185 
186 	if (NCONF_load(conf, file, NULL) <= 0)
187 		{
188 		if ((flags & CONF_MFLAGS_IGNORE_MISSING_FILE) &&
189 		  (ERR_GET_REASON(ERR_peek_last_error()) == CONF_R_NO_SUCH_FILE))
190 			{
191 			ERR_clear_error();
192 			ret = 1;
193 			}
194 		goto err;
195 		}
196 
197 	ret = CONF_modules_load(conf, appname, flags);
198 
199 	err:
200 	if (filename == NULL)
201 		OPENSSL_free(file);
202 	NCONF_free(conf);
203 
204 	return ret;
205 	}
206 
module_run(const CONF * cnf,char * name,char * value,unsigned long flags)207 static int module_run(const CONF *cnf, char *name, char *value,
208 		      unsigned long flags)
209 	{
210 	CONF_MODULE *md;
211 	int ret;
212 
213 	md = module_find(name);
214 
215 	/* Module not found: try to load DSO */
216 	if (!md && !(flags & CONF_MFLAGS_NO_DSO))
217 		md = module_load_dso(cnf, name, value, flags);
218 
219 	if (!md)
220 		{
221 		if (!(flags & CONF_MFLAGS_SILENT))
222 			{
223 			CONFerr(CONF_F_MODULE_RUN, CONF_R_UNKNOWN_MODULE_NAME);
224 			ERR_add_error_data(2, "module=", name);
225 			}
226 		return -1;
227 		}
228 
229 	ret = module_init(md, name, value, cnf);
230 
231 	if (ret <= 0)
232 		{
233 		if (!(flags & CONF_MFLAGS_SILENT))
234 			{
235 			char rcode[DECIMAL_SIZE(ret)+1];
236 			CONFerr(CONF_F_CONF_MODULES_LOAD, CONF_R_MODULE_INITIALIZATION_ERROR);
237 			BIO_snprintf(rcode, sizeof rcode, "%-8d", ret);
238 			ERR_add_error_data(6, "module=", name, ", value=", value, ", retcode=", rcode);
239 			}
240 		}
241 
242 	return ret;
243 	}
244 
245 /* Load a module from a DSO */
module_load_dso(const CONF * cnf,char * name,char * value,unsigned long flags)246 static CONF_MODULE *module_load_dso(const CONF *cnf, char *name, char *value,
247 				    unsigned long flags)
248 	{
249 	DSO *dso = NULL;
250 	conf_init_func *ifunc;
251 	conf_finish_func *ffunc;
252 	char *path = NULL;
253 	int errcode = 0;
254 	CONF_MODULE *md;
255 	/* Look for alternative path in module section */
256 	path = NCONF_get_string(cnf, value, "path");
257 	if (!path)
258 		{
259 		ERR_get_error();
260 		path = name;
261 		}
262 	dso = DSO_load(NULL, path, NULL, 0);
263 	if (!dso)
264 		{
265 		errcode = CONF_R_ERROR_LOADING_DSO;
266 		goto err;
267 		}
268         ifunc = (conf_init_func *)DSO_bind_func(dso, DSO_mod_init_name);
269 	if (!ifunc)
270 		{
271 		errcode = CONF_R_MISSING_INIT_FUNCTION;
272 		goto err;
273 		}
274         ffunc = (conf_finish_func *)DSO_bind_func(dso, DSO_mod_finish_name);
275 	/* All OK, add module */
276 	md = module_add(dso, name, ifunc, ffunc);
277 
278 	if (!md)
279 		goto err;
280 
281 	return md;
282 
283 	err:
284 	if (dso)
285 		DSO_free(dso);
286 	CONFerr(CONF_F_MODULE_LOAD_DSO, errcode);
287 	ERR_add_error_data(4, "module=", name, ", path=", path);
288 	return NULL;
289 	}
290 
291 /* add module to list */
module_add(DSO * dso,const char * name,conf_init_func * ifunc,conf_finish_func * ffunc)292 static CONF_MODULE *module_add(DSO *dso, const char *name,
293 			       conf_init_func *ifunc, conf_finish_func *ffunc)
294 	{
295 	CONF_MODULE *tmod = NULL;
296 	if (supported_modules == NULL)
297 		supported_modules = sk_CONF_MODULE_new_null();
298 	if (supported_modules == NULL)
299 		return NULL;
300 	tmod = OPENSSL_malloc(sizeof(CONF_MODULE));
301 	if (tmod == NULL)
302 		return NULL;
303 
304 	tmod->dso = dso;
305 	tmod->name = BUF_strdup(name);
306 	tmod->init = ifunc;
307 	tmod->finish = ffunc;
308 	tmod->links = 0;
309 
310 	if (!sk_CONF_MODULE_push(supported_modules, tmod))
311 		{
312 		OPENSSL_free(tmod);
313 		return NULL;
314 		}
315 
316 	return tmod;
317 	}
318 
319 /* Find a module from the list. We allow module names of the
320  * form modname.XXXX to just search for modname to allow the
321  * same module to be initialized more than once.
322  */
323 
module_find(char * name)324 static CONF_MODULE *module_find(char *name)
325 	{
326 	CONF_MODULE *tmod;
327 	int i, nchar;
328 	char *p;
329 	p = strrchr(name, '.');
330 
331 	if (p)
332 		nchar = p - name;
333 	else
334 		nchar = strlen(name);
335 
336 	for (i = 0; i < sk_CONF_MODULE_num(supported_modules); i++)
337 		{
338 		tmod = sk_CONF_MODULE_value(supported_modules, i);
339 		if (!strncmp(tmod->name, name, nchar))
340 			return tmod;
341 		}
342 
343 	return NULL;
344 
345 	}
346 
347 /* initialize a module */
module_init(CONF_MODULE * pmod,char * name,char * value,const CONF * cnf)348 static int module_init(CONF_MODULE *pmod, char *name, char *value,
349 		       const CONF *cnf)
350 	{
351 	int ret = 1;
352 	int init_called = 0;
353 	CONF_IMODULE *imod = NULL;
354 
355 	/* Otherwise add initialized module to list */
356 	imod = OPENSSL_malloc(sizeof(CONF_IMODULE));
357 	if (!imod)
358 		goto err;
359 
360 	imod->pmod = pmod;
361 	imod->name = BUF_strdup(name);
362 	imod->value = BUF_strdup(value);
363 	imod->usr_data = NULL;
364 
365 	if (!imod->name || !imod->value)
366 		goto memerr;
367 
368 	/* Try to initialize module */
369 	if(pmod->init)
370 		{
371 		ret = pmod->init(imod, cnf);
372 		init_called = 1;
373 		/* Error occurred, exit */
374 		if (ret <= 0)
375 			goto err;
376 		}
377 
378 	if (initialized_modules == NULL)
379 		{
380 		initialized_modules = sk_CONF_IMODULE_new_null();
381 		if (!initialized_modules)
382 			{
383 			CONFerr(CONF_F_MODULE_INIT, ERR_R_MALLOC_FAILURE);
384 			goto err;
385 			}
386 		}
387 
388 	if (!sk_CONF_IMODULE_push(initialized_modules, imod))
389 		{
390 		CONFerr(CONF_F_MODULE_INIT, ERR_R_MALLOC_FAILURE);
391 		goto err;
392 		}
393 
394 	pmod->links++;
395 
396 	return ret;
397 
398 	err:
399 
400 	/* We've started the module so we'd better finish it */
401 	if (pmod->finish && init_called)
402 		pmod->finish(imod);
403 
404 	memerr:
405 	if (imod)
406 		{
407 		if (imod->name)
408 			OPENSSL_free(imod->name);
409 		if (imod->value)
410 			OPENSSL_free(imod->value);
411 		OPENSSL_free(imod);
412 		}
413 
414 	return -1;
415 
416 	}
417 
418 /* Unload any dynamic modules that have a link count of zero:
419  * i.e. have no active initialized modules. If 'all' is set
420  * then all modules are unloaded including static ones.
421  */
422 
CONF_modules_unload(int all)423 void CONF_modules_unload(int all)
424 	{
425 	int i;
426 	CONF_MODULE *md;
427 	CONF_modules_finish();
428 	/* unload modules in reverse order */
429 	for (i = sk_CONF_MODULE_num(supported_modules) - 1; i >= 0; i--)
430 		{
431 		md = sk_CONF_MODULE_value(supported_modules, i);
432 		/* If static or in use and 'all' not set ignore it */
433 		if (((md->links > 0) || !md->dso) && !all)
434 			continue;
435 		/* Since we're working in reverse this is OK */
436 		sk_CONF_MODULE_delete(supported_modules, i);
437 		module_free(md);
438 		}
439 	if (sk_CONF_MODULE_num(supported_modules) == 0)
440 		{
441 		sk_CONF_MODULE_free(supported_modules);
442 		supported_modules = NULL;
443 		}
444 	}
445 
446 /* unload a single module */
module_free(CONF_MODULE * md)447 static void module_free(CONF_MODULE *md)
448 	{
449 	if (md->dso)
450 		DSO_free(md->dso);
451 	OPENSSL_free(md->name);
452 	OPENSSL_free(md);
453 	}
454 
455 /* finish and free up all modules instances */
456 
CONF_modules_finish(void)457 void CONF_modules_finish(void)
458 	{
459 	CONF_IMODULE *imod;
460 	while (sk_CONF_IMODULE_num(initialized_modules) > 0)
461 		{
462 		imod = sk_CONF_IMODULE_pop(initialized_modules);
463 		module_finish(imod);
464 		}
465 	sk_CONF_IMODULE_free(initialized_modules);
466 	initialized_modules = NULL;
467 	}
468 
469 /* finish a module instance */
470 
module_finish(CONF_IMODULE * imod)471 static void module_finish(CONF_IMODULE *imod)
472 	{
473 	if (imod->pmod->finish)
474 		imod->pmod->finish(imod);
475 	imod->pmod->links--;
476 	OPENSSL_free(imod->name);
477 	OPENSSL_free(imod->value);
478 	OPENSSL_free(imod);
479 	}
480 
481 /* Add a static module to OpenSSL */
482 
CONF_module_add(const char * name,conf_init_func * ifunc,conf_finish_func * ffunc)483 int CONF_module_add(const char *name, conf_init_func *ifunc,
484 		    conf_finish_func *ffunc)
485 	{
486 	if (module_add(NULL, name, ifunc, ffunc))
487 		return 1;
488 	else
489 		return 0;
490 	}
491 
CONF_modules_free(void)492 void CONF_modules_free(void)
493 	{
494 	CONF_modules_finish();
495 	CONF_modules_unload(1);
496 	}
497 
498 /* Utility functions */
499 
CONF_imodule_get_name(const CONF_IMODULE * md)500 const char *CONF_imodule_get_name(const CONF_IMODULE *md)
501 	{
502 	return md->name;
503 	}
504 
CONF_imodule_get_value(const CONF_IMODULE * md)505 const char *CONF_imodule_get_value(const CONF_IMODULE *md)
506 	{
507 	return md->value;
508 	}
509 
CONF_imodule_get_usr_data(const CONF_IMODULE * md)510 void *CONF_imodule_get_usr_data(const CONF_IMODULE *md)
511 	{
512 	return md->usr_data;
513 	}
514 
CONF_imodule_set_usr_data(CONF_IMODULE * md,void * usr_data)515 void CONF_imodule_set_usr_data(CONF_IMODULE *md, void *usr_data)
516 	{
517 	md->usr_data = usr_data;
518 	}
519 
CONF_imodule_get_module(const CONF_IMODULE * md)520 CONF_MODULE *CONF_imodule_get_module(const CONF_IMODULE *md)
521 	{
522 	return md->pmod;
523 	}
524 
CONF_imodule_get_flags(const CONF_IMODULE * md)525 unsigned long CONF_imodule_get_flags(const CONF_IMODULE *md)
526 	{
527 	return md->flags;
528 	}
529 
CONF_imodule_set_flags(CONF_IMODULE * md,unsigned long flags)530 void CONF_imodule_set_flags(CONF_IMODULE *md, unsigned long flags)
531 	{
532 	md->flags = flags;
533 	}
534 
CONF_module_get_usr_data(CONF_MODULE * pmod)535 void *CONF_module_get_usr_data(CONF_MODULE *pmod)
536 	{
537 	return pmod->usr_data;
538 	}
539 
CONF_module_set_usr_data(CONF_MODULE * pmod,void * usr_data)540 void CONF_module_set_usr_data(CONF_MODULE *pmod, void *usr_data)
541 	{
542 	pmod->usr_data = usr_data;
543 	}
544 
545 /* Return default config file name */
546 
CONF_get1_default_config_file(void)547 char *CONF_get1_default_config_file(void)
548 	{
549 	char *file;
550 	int len;
551 
552 	file = getenv("OPENSSL_CONF");
553 	if (file)
554 		return BUF_strdup(file);
555 
556 	len = strlen(X509_get_default_cert_area());
557 #ifndef OPENSSL_SYS_VMS
558 	len++;
559 #endif
560 	len += strlen(OPENSSL_CONF);
561 
562 	file = OPENSSL_malloc(len + 1);
563 
564 	if (!file)
565 		return NULL;
566 	BUF_strlcpy(file,X509_get_default_cert_area(),len + 1);
567 #ifndef OPENSSL_SYS_VMS
568 	BUF_strlcat(file,"/",len + 1);
569 #endif
570 	BUF_strlcat(file,OPENSSL_CONF,len + 1);
571 
572 	return file;
573 	}
574 
575 /* This function takes a list separated by 'sep' and calls the
576  * callback function giving the start and length of each member
577  * optionally stripping leading and trailing whitespace. This can
578  * be used to parse comma separated lists for example.
579  */
580 
CONF_parse_list(const char * list_,int sep,int nospc,int (* list_cb)(const char * elem,int len,void * usr),void * arg)581 int CONF_parse_list(const char *list_, int sep, int nospc,
582 	int (*list_cb)(const char *elem, int len, void *usr), void *arg)
583 	{
584 	int ret;
585 	const char *lstart, *tmpend, *p;
586 	lstart = list_;
587 
588 	for(;;)
589 		{
590 		if (nospc)
591 			{
592 			while(isspace((unsigned char)*lstart))
593 				lstart++;
594 			}
595 		p = strchr(lstart, sep);
596 		if (p == lstart || !*lstart)
597 			ret = list_cb(NULL, 0, arg);
598 		else
599 			{
600 			if (p)
601 				tmpend = p - 1;
602 			else
603 				tmpend = lstart + strlen(lstart) - 1;
604 			if (nospc)
605 				{
606 				while(isspace((unsigned char)*tmpend))
607 					tmpend--;
608 				}
609 			ret = list_cb(lstart, tmpend - lstart + 1, arg);
610 			}
611 		if (ret <= 0)
612 			return ret;
613 		if (p == NULL)
614 			return 1;
615 		lstart = p + 1;
616 		}
617 	}
618 
619