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