xref: /onnv-gate/usr/src/common/openssl/crypto/engine/eng_cnf.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* eng_cnf.c */
20Sstevel@tonic-gate /* Written by Stephen Henson (shenson@bigfoot.com) for the OpenSSL
30Sstevel@tonic-gate  * project 2001.
40Sstevel@tonic-gate  */
50Sstevel@tonic-gate /* ====================================================================
60Sstevel@tonic-gate  * Copyright (c) 2001 The OpenSSL Project.  All rights reserved.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
90Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
100Sstevel@tonic-gate  * are met:
110Sstevel@tonic-gate  *
120Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
130Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
140Sstevel@tonic-gate  *
150Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
160Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in
170Sstevel@tonic-gate  *    the documentation and/or other materials provided with the
180Sstevel@tonic-gate  *    distribution.
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this
210Sstevel@tonic-gate  *    software must display the following acknowledgment:
220Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
230Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
240Sstevel@tonic-gate  *
250Sstevel@tonic-gate  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
260Sstevel@tonic-gate  *    endorse or promote products derived from this software without
270Sstevel@tonic-gate  *    prior written permission. For written permission, please contact
280Sstevel@tonic-gate  *    licensing@OpenSSL.org.
290Sstevel@tonic-gate  *
300Sstevel@tonic-gate  * 5. Products derived from this software may not be called "OpenSSL"
310Sstevel@tonic-gate  *    nor may "OpenSSL" appear in their names without prior written
320Sstevel@tonic-gate  *    permission of the OpenSSL Project.
330Sstevel@tonic-gate  *
340Sstevel@tonic-gate  * 6. Redistributions of any form whatsoever must retain the following
350Sstevel@tonic-gate  *    acknowledgment:
360Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
370Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
380Sstevel@tonic-gate  *
390Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
400Sstevel@tonic-gate  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
410Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
420Sstevel@tonic-gate  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
430Sstevel@tonic-gate  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
440Sstevel@tonic-gate  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
450Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
460Sstevel@tonic-gate  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
470Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
480Sstevel@tonic-gate  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
490Sstevel@tonic-gate  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
500Sstevel@tonic-gate  * OF THE POSSIBILITY OF SUCH DAMAGE.
510Sstevel@tonic-gate  * ====================================================================
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  * This product includes cryptographic software written by Eric Young
540Sstevel@tonic-gate  * (eay@cryptsoft.com).  This product includes software written by Tim
550Sstevel@tonic-gate  * Hudson (tjh@cryptsoft.com).
560Sstevel@tonic-gate  *
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate 
59*2139Sjp161948 /*
60*2139Sjp161948  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
61*2139Sjp161948  * Use is subject to license terms.
62*2139Sjp161948  */
63*2139Sjp161948 
64*2139Sjp161948 #pragma ident	"%Z%%M%	%I%	%E% SMI"
65*2139Sjp161948 
66*2139Sjp161948 #include "eng_int.h"
670Sstevel@tonic-gate #include <openssl/conf.h>
680Sstevel@tonic-gate 
690Sstevel@tonic-gate /* #define ENGINE_CONF_DEBUG */
700Sstevel@tonic-gate 
710Sstevel@tonic-gate /* ENGINE config module */
720Sstevel@tonic-gate 
skip_dot(char * name)730Sstevel@tonic-gate static char *skip_dot(char *name)
740Sstevel@tonic-gate 	{
750Sstevel@tonic-gate 	char *p;
760Sstevel@tonic-gate 	p = strchr(name, '.');
770Sstevel@tonic-gate 	if (p)
780Sstevel@tonic-gate 		return p + 1;
790Sstevel@tonic-gate 	return name;
800Sstevel@tonic-gate 	}
810Sstevel@tonic-gate 
820Sstevel@tonic-gate static STACK_OF(ENGINE) *initialized_engines = NULL;
830Sstevel@tonic-gate 
int_engine_init(ENGINE * e)840Sstevel@tonic-gate static int int_engine_init(ENGINE *e)
850Sstevel@tonic-gate 	{
860Sstevel@tonic-gate 	if (!ENGINE_init(e))
870Sstevel@tonic-gate 		return 0;
880Sstevel@tonic-gate 	if (!initialized_engines)
890Sstevel@tonic-gate 		initialized_engines = sk_ENGINE_new_null();
900Sstevel@tonic-gate 	if (!initialized_engines || !sk_ENGINE_push(initialized_engines, e))
910Sstevel@tonic-gate 		{
920Sstevel@tonic-gate 		ENGINE_finish(e);
930Sstevel@tonic-gate 		return 0;
940Sstevel@tonic-gate 		}
950Sstevel@tonic-gate 	return 1;
960Sstevel@tonic-gate 	}
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 
int_engine_configure(char * name,char * value,const CONF * cnf)990Sstevel@tonic-gate static int int_engine_configure(char *name, char *value, const CONF *cnf)
1000Sstevel@tonic-gate 	{
1010Sstevel@tonic-gate 	int i;
1020Sstevel@tonic-gate 	int ret = 0;
1030Sstevel@tonic-gate 	long do_init = -1;
1040Sstevel@tonic-gate 	STACK_OF(CONF_VALUE) *ecmds;
1050Sstevel@tonic-gate 	CONF_VALUE *ecmd;
1060Sstevel@tonic-gate 	char *ctrlname, *ctrlvalue;
1070Sstevel@tonic-gate 	ENGINE *e = NULL;
1080Sstevel@tonic-gate 	name = skip_dot(name);
1090Sstevel@tonic-gate #ifdef ENGINE_CONF_DEBUG
1100Sstevel@tonic-gate 	fprintf(stderr, "Configuring engine %s\n", name);
1110Sstevel@tonic-gate #endif
1120Sstevel@tonic-gate 	/* Value is a section containing ENGINE commands */
1130Sstevel@tonic-gate 	ecmds = NCONF_get_section(cnf, value);
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 	if (!ecmds)
1160Sstevel@tonic-gate 		{
1170Sstevel@tonic-gate 		ENGINEerr(ENGINE_F_INT_ENGINE_CONFIGURE, ENGINE_R_ENGINE_SECTION_ERROR);
1180Sstevel@tonic-gate 		return 0;
1190Sstevel@tonic-gate 		}
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 	for (i = 0; i < sk_CONF_VALUE_num(ecmds); i++)
1220Sstevel@tonic-gate 		{
1230Sstevel@tonic-gate 		ecmd = sk_CONF_VALUE_value(ecmds, i);
1240Sstevel@tonic-gate 		ctrlname = skip_dot(ecmd->name);
1250Sstevel@tonic-gate 		ctrlvalue = ecmd->value;
1260Sstevel@tonic-gate #ifdef ENGINE_CONF_DEBUG
1270Sstevel@tonic-gate 	fprintf(stderr, "ENGINE conf: doing ctrl(%s,%s)\n", ctrlname, ctrlvalue);
1280Sstevel@tonic-gate #endif
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate 		/* First handle some special pseudo ctrls */
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 		/* Override engine name to use */
1330Sstevel@tonic-gate 		if (!strcmp(ctrlname, "engine_id"))
1340Sstevel@tonic-gate 			name = ctrlvalue;
1350Sstevel@tonic-gate 		/* Load a dynamic ENGINE */
1360Sstevel@tonic-gate 		else if (!strcmp(ctrlname, "dynamic_path"))
1370Sstevel@tonic-gate 			{
1380Sstevel@tonic-gate #ifdef SOLARIS_OPENSSL
1390Sstevel@tonic-gate 			/*
1400Sstevel@tonic-gate 			 * Dynamic engines must be disabled until signature
1410Sstevel@tonic-gate 			 * verification is implemented.
1420Sstevel@tonic-gate 			 */
1430Sstevel@tonic-gate 			goto err;
1440Sstevel@tonic-gate #endif /* SOLARIS_OPENSSL */
1450Sstevel@tonic-gate 			e = ENGINE_by_id("dynamic");
1460Sstevel@tonic-gate 			if (!e)
1470Sstevel@tonic-gate 				goto err;
1480Sstevel@tonic-gate 			if (!ENGINE_ctrl_cmd_string(e, "SO_PATH", ctrlvalue, 0))
1490Sstevel@tonic-gate 				goto err;
1500Sstevel@tonic-gate 			if (!ENGINE_ctrl_cmd_string(e, "LIST_ADD", "2", 0))
1510Sstevel@tonic-gate 				goto err;
1520Sstevel@tonic-gate 			if (!ENGINE_ctrl_cmd_string(e, "LOAD", NULL, 0))
1530Sstevel@tonic-gate 				goto err;
1540Sstevel@tonic-gate 			}
1550Sstevel@tonic-gate 		/* ... add other pseudos here ... */
1560Sstevel@tonic-gate 		else
1570Sstevel@tonic-gate 			{
1580Sstevel@tonic-gate 			/* At this point we need an ENGINE structural reference
1590Sstevel@tonic-gate 			 * if we don't already have one.
1600Sstevel@tonic-gate 			 */
1610Sstevel@tonic-gate 			if (!e)
1620Sstevel@tonic-gate 				{
1630Sstevel@tonic-gate 				e = ENGINE_by_id(name);
1640Sstevel@tonic-gate 				if (!e)
1650Sstevel@tonic-gate 					return 0;
1660Sstevel@tonic-gate 				}
1670Sstevel@tonic-gate 			/* Allow "EMPTY" to mean no value: this allows a valid
1680Sstevel@tonic-gate 			 * "value" to be passed to ctrls of type NO_INPUT
1690Sstevel@tonic-gate 		 	 */
1700Sstevel@tonic-gate 			if (!strcmp(ctrlvalue, "EMPTY"))
1710Sstevel@tonic-gate 				ctrlvalue = NULL;
172*2139Sjp161948 			if (!strcmp(ctrlname, "init"))
1730Sstevel@tonic-gate 				{
1740Sstevel@tonic-gate 				if (!NCONF_get_number_e(cnf, value, "init", &do_init))
1750Sstevel@tonic-gate 					goto err;
1760Sstevel@tonic-gate 				if (do_init == 1)
1770Sstevel@tonic-gate 					{
1780Sstevel@tonic-gate 					if (!int_engine_init(e))
1790Sstevel@tonic-gate 						goto err;
1800Sstevel@tonic-gate 					}
1810Sstevel@tonic-gate 				else if (do_init != 0)
1820Sstevel@tonic-gate 					{
1830Sstevel@tonic-gate 					ENGINEerr(ENGINE_F_INT_ENGINE_CONFIGURE, ENGINE_R_INVALID_INIT_VALUE);
1840Sstevel@tonic-gate 					goto err;
1850Sstevel@tonic-gate 					}
1860Sstevel@tonic-gate 				}
1870Sstevel@tonic-gate 			else if (!strcmp(ctrlname, "default_algorithms"))
1880Sstevel@tonic-gate 				{
1890Sstevel@tonic-gate 				if (!ENGINE_set_default_string(e, ctrlvalue))
1900Sstevel@tonic-gate 					goto err;
1910Sstevel@tonic-gate 				}
1920Sstevel@tonic-gate 			else if (!ENGINE_ctrl_cmd_string(e,
1930Sstevel@tonic-gate 					ctrlname, ctrlvalue, 0))
1940Sstevel@tonic-gate 				return 0;
1950Sstevel@tonic-gate 			}
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 		}
2000Sstevel@tonic-gate 	if (e && (do_init == -1) && !int_engine_init(e))
2010Sstevel@tonic-gate 		goto err;
2020Sstevel@tonic-gate 	ret = 1;
2030Sstevel@tonic-gate 	err:
2040Sstevel@tonic-gate 	if (e)
2050Sstevel@tonic-gate 		ENGINE_free(e);
2060Sstevel@tonic-gate 	return ret;
2070Sstevel@tonic-gate 	}
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 
int_engine_module_init(CONF_IMODULE * md,const CONF * cnf)2100Sstevel@tonic-gate static int int_engine_module_init(CONF_IMODULE *md, const CONF *cnf)
2110Sstevel@tonic-gate 	{
2120Sstevel@tonic-gate 	STACK_OF(CONF_VALUE) *elist;
2130Sstevel@tonic-gate 	CONF_VALUE *cval;
2140Sstevel@tonic-gate 	int i;
2150Sstevel@tonic-gate #ifdef ENGINE_CONF_DEBUG
2160Sstevel@tonic-gate 	fprintf(stderr, "Called engine module: name %s, value %s\n",
2170Sstevel@tonic-gate 			CONF_imodule_get_name(md), CONF_imodule_get_value(md));
2180Sstevel@tonic-gate #endif
2190Sstevel@tonic-gate 	/* Value is a section containing ENGINEs to configure */
2200Sstevel@tonic-gate 	elist = NCONF_get_section(cnf, CONF_imodule_get_value(md));
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	if (!elist)
2230Sstevel@tonic-gate 		{
224*2139Sjp161948 		ENGINEerr(ENGINE_F_INT_ENGINE_MODULE_INIT, ENGINE_R_ENGINES_SECTION_ERROR);
2250Sstevel@tonic-gate 		return 0;
2260Sstevel@tonic-gate 		}
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	for (i = 0; i < sk_CONF_VALUE_num(elist); i++)
2290Sstevel@tonic-gate 		{
2300Sstevel@tonic-gate 		cval = sk_CONF_VALUE_value(elist, i);
2310Sstevel@tonic-gate 		if (!int_engine_configure(cval->name, cval->value, cnf))
2320Sstevel@tonic-gate 			return 0;
2330Sstevel@tonic-gate 		}
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	return 1;
2360Sstevel@tonic-gate 	}
2370Sstevel@tonic-gate 
int_engine_module_finish(CONF_IMODULE * md)2380Sstevel@tonic-gate static void int_engine_module_finish(CONF_IMODULE *md)
2390Sstevel@tonic-gate 	{
2400Sstevel@tonic-gate 	ENGINE *e;
2410Sstevel@tonic-gate 	while ((e = sk_ENGINE_pop(initialized_engines)))
2420Sstevel@tonic-gate 		ENGINE_finish(e);
2430Sstevel@tonic-gate 	sk_ENGINE_free(initialized_engines);
2440Sstevel@tonic-gate 	initialized_engines = NULL;
2450Sstevel@tonic-gate 	}
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 
ENGINE_add_conf_module(void)2480Sstevel@tonic-gate void ENGINE_add_conf_module(void)
2490Sstevel@tonic-gate 	{
2500Sstevel@tonic-gate 	CONF_module_add("engines",
2510Sstevel@tonic-gate 			int_engine_module_init,
2520Sstevel@tonic-gate 			int_engine_module_finish);
2530Sstevel@tonic-gate 	}
254