xref: /onnv-gate/usr/src/common/openssl/crypto/engine/eng_cnf.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /* eng_cnf.c */
2*0Sstevel@tonic-gate /* Written by Stephen Henson (shenson@bigfoot.com) for the OpenSSL
3*0Sstevel@tonic-gate  * project 2001.
4*0Sstevel@tonic-gate  */
5*0Sstevel@tonic-gate /* ====================================================================
6*0Sstevel@tonic-gate  * Copyright (c) 2001 The OpenSSL Project.  All rights reserved.
7*0Sstevel@tonic-gate  *
8*0Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
9*0Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
10*0Sstevel@tonic-gate  * are met:
11*0Sstevel@tonic-gate  *
12*0Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
13*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
14*0Sstevel@tonic-gate  *
15*0Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
16*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in
17*0Sstevel@tonic-gate  *    the documentation and/or other materials provided with the
18*0Sstevel@tonic-gate  *    distribution.
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this
21*0Sstevel@tonic-gate  *    software must display the following acknowledgment:
22*0Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
23*0Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24*0Sstevel@tonic-gate  *
25*0Sstevel@tonic-gate  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26*0Sstevel@tonic-gate  *    endorse or promote products derived from this software without
27*0Sstevel@tonic-gate  *    prior written permission. For written permission, please contact
28*0Sstevel@tonic-gate  *    licensing@OpenSSL.org.
29*0Sstevel@tonic-gate  *
30*0Sstevel@tonic-gate  * 5. Products derived from this software may not be called "OpenSSL"
31*0Sstevel@tonic-gate  *    nor may "OpenSSL" appear in their names without prior written
32*0Sstevel@tonic-gate  *    permission of the OpenSSL Project.
33*0Sstevel@tonic-gate  *
34*0Sstevel@tonic-gate  * 6. Redistributions of any form whatsoever must retain the following
35*0Sstevel@tonic-gate  *    acknowledgment:
36*0Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
37*0Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38*0Sstevel@tonic-gate  *
39*0Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40*0Sstevel@tonic-gate  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41*0Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42*0Sstevel@tonic-gate  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43*0Sstevel@tonic-gate  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44*0Sstevel@tonic-gate  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45*0Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46*0Sstevel@tonic-gate  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47*0Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48*0Sstevel@tonic-gate  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49*0Sstevel@tonic-gate  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50*0Sstevel@tonic-gate  * OF THE POSSIBILITY OF SUCH DAMAGE.
51*0Sstevel@tonic-gate  * ====================================================================
52*0Sstevel@tonic-gate  *
53*0Sstevel@tonic-gate  * This product includes cryptographic software written by Eric Young
54*0Sstevel@tonic-gate  * (eay@cryptsoft.com).  This product includes software written by Tim
55*0Sstevel@tonic-gate  * Hudson (tjh@cryptsoft.com).
56*0Sstevel@tonic-gate  *
57*0Sstevel@tonic-gate  */
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate #include <stdio.h>
60*0Sstevel@tonic-gate #include <openssl/crypto.h>
61*0Sstevel@tonic-gate #include "cryptlib.h"
62*0Sstevel@tonic-gate #include <openssl/conf.h>
63*0Sstevel@tonic-gate #include <openssl/engine.h>
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate /* #define ENGINE_CONF_DEBUG */
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate /* ENGINE config module */
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate static char *skip_dot(char *name)
70*0Sstevel@tonic-gate 	{
71*0Sstevel@tonic-gate 	char *p;
72*0Sstevel@tonic-gate 	p = strchr(name, '.');
73*0Sstevel@tonic-gate 	if (p)
74*0Sstevel@tonic-gate 		return p + 1;
75*0Sstevel@tonic-gate 	return name;
76*0Sstevel@tonic-gate 	}
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate static STACK_OF(ENGINE) *initialized_engines = NULL;
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate static int int_engine_init(ENGINE *e)
81*0Sstevel@tonic-gate 	{
82*0Sstevel@tonic-gate 	if (!ENGINE_init(e))
83*0Sstevel@tonic-gate 		return 0;
84*0Sstevel@tonic-gate 	if (!initialized_engines)
85*0Sstevel@tonic-gate 		initialized_engines = sk_ENGINE_new_null();
86*0Sstevel@tonic-gate 	if (!initialized_engines || !sk_ENGINE_push(initialized_engines, e))
87*0Sstevel@tonic-gate 		{
88*0Sstevel@tonic-gate 		ENGINE_finish(e);
89*0Sstevel@tonic-gate 		return 0;
90*0Sstevel@tonic-gate 		}
91*0Sstevel@tonic-gate 	return 1;
92*0Sstevel@tonic-gate 	}
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate static int int_engine_configure(char *name, char *value, const CONF *cnf)
96*0Sstevel@tonic-gate 	{
97*0Sstevel@tonic-gate 	int i;
98*0Sstevel@tonic-gate 	int ret = 0;
99*0Sstevel@tonic-gate 	long do_init = -1;
100*0Sstevel@tonic-gate 	STACK_OF(CONF_VALUE) *ecmds;
101*0Sstevel@tonic-gate 	CONF_VALUE *ecmd;
102*0Sstevel@tonic-gate 	char *ctrlname, *ctrlvalue;
103*0Sstevel@tonic-gate 	ENGINE *e = NULL;
104*0Sstevel@tonic-gate 	name = skip_dot(name);
105*0Sstevel@tonic-gate #ifdef ENGINE_CONF_DEBUG
106*0Sstevel@tonic-gate 	fprintf(stderr, "Configuring engine %s\n", name);
107*0Sstevel@tonic-gate #endif
108*0Sstevel@tonic-gate 	/* Value is a section containing ENGINE commands */
109*0Sstevel@tonic-gate 	ecmds = NCONF_get_section(cnf, value);
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate 	if (!ecmds)
112*0Sstevel@tonic-gate 		{
113*0Sstevel@tonic-gate 		ENGINEerr(ENGINE_F_INT_ENGINE_CONFIGURE, ENGINE_R_ENGINE_SECTION_ERROR);
114*0Sstevel@tonic-gate 		return 0;
115*0Sstevel@tonic-gate 		}
116*0Sstevel@tonic-gate 
117*0Sstevel@tonic-gate 	for (i = 0; i < sk_CONF_VALUE_num(ecmds); i++)
118*0Sstevel@tonic-gate 		{
119*0Sstevel@tonic-gate 		ecmd = sk_CONF_VALUE_value(ecmds, i);
120*0Sstevel@tonic-gate 		ctrlname = skip_dot(ecmd->name);
121*0Sstevel@tonic-gate 		ctrlvalue = ecmd->value;
122*0Sstevel@tonic-gate #ifdef ENGINE_CONF_DEBUG
123*0Sstevel@tonic-gate 	fprintf(stderr, "ENGINE conf: doing ctrl(%s,%s)\n", ctrlname, ctrlvalue);
124*0Sstevel@tonic-gate #endif
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate 		/* First handle some special pseudo ctrls */
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate 		/* Override engine name to use */
129*0Sstevel@tonic-gate 		if (!strcmp(ctrlname, "engine_id"))
130*0Sstevel@tonic-gate 			name = ctrlvalue;
131*0Sstevel@tonic-gate 		/* Load a dynamic ENGINE */
132*0Sstevel@tonic-gate 		else if (!strcmp(ctrlname, "dynamic_path"))
133*0Sstevel@tonic-gate 			{
134*0Sstevel@tonic-gate #ifdef SOLARIS_OPENSSL
135*0Sstevel@tonic-gate 			/*
136*0Sstevel@tonic-gate 			 * Dynamic engines must be disabled until signature
137*0Sstevel@tonic-gate 			 * verification is implemented.
138*0Sstevel@tonic-gate 			 */
139*0Sstevel@tonic-gate 			goto err;
140*0Sstevel@tonic-gate #endif /* SOLARIS_OPENSSL */
141*0Sstevel@tonic-gate 			e = ENGINE_by_id("dynamic");
142*0Sstevel@tonic-gate 			if (!e)
143*0Sstevel@tonic-gate 				goto err;
144*0Sstevel@tonic-gate 			if (!ENGINE_ctrl_cmd_string(e, "SO_PATH", ctrlvalue, 0))
145*0Sstevel@tonic-gate 				goto err;
146*0Sstevel@tonic-gate 			if (!ENGINE_ctrl_cmd_string(e, "LIST_ADD", "2", 0))
147*0Sstevel@tonic-gate 				goto err;
148*0Sstevel@tonic-gate 			if (!ENGINE_ctrl_cmd_string(e, "LOAD", NULL, 0))
149*0Sstevel@tonic-gate 				goto err;
150*0Sstevel@tonic-gate 			}
151*0Sstevel@tonic-gate 		/* ... add other pseudos here ... */
152*0Sstevel@tonic-gate 		else
153*0Sstevel@tonic-gate 			{
154*0Sstevel@tonic-gate 			/* At this point we need an ENGINE structural reference
155*0Sstevel@tonic-gate 			 * if we don't already have one.
156*0Sstevel@tonic-gate 			 */
157*0Sstevel@tonic-gate 			if (!e)
158*0Sstevel@tonic-gate 				{
159*0Sstevel@tonic-gate 				e = ENGINE_by_id(name);
160*0Sstevel@tonic-gate 				if (!e)
161*0Sstevel@tonic-gate 					return 0;
162*0Sstevel@tonic-gate 				}
163*0Sstevel@tonic-gate 			/* Allow "EMPTY" to mean no value: this allows a valid
164*0Sstevel@tonic-gate 			 * "value" to be passed to ctrls of type NO_INPUT
165*0Sstevel@tonic-gate 		 	 */
166*0Sstevel@tonic-gate 			if (!strcmp(ctrlvalue, "EMPTY"))
167*0Sstevel@tonic-gate 				ctrlvalue = NULL;
168*0Sstevel@tonic-gate 			else if (!strcmp(ctrlname, "init"))
169*0Sstevel@tonic-gate 				{
170*0Sstevel@tonic-gate 				if (!NCONF_get_number_e(cnf, value, "init", &do_init))
171*0Sstevel@tonic-gate 					goto err;
172*0Sstevel@tonic-gate 				if (do_init == 1)
173*0Sstevel@tonic-gate 					{
174*0Sstevel@tonic-gate 					if (!int_engine_init(e))
175*0Sstevel@tonic-gate 						goto err;
176*0Sstevel@tonic-gate 					}
177*0Sstevel@tonic-gate 				else if (do_init != 0)
178*0Sstevel@tonic-gate 					{
179*0Sstevel@tonic-gate 					ENGINEerr(ENGINE_F_INT_ENGINE_CONFIGURE, ENGINE_R_INVALID_INIT_VALUE);
180*0Sstevel@tonic-gate 					goto err;
181*0Sstevel@tonic-gate 					}
182*0Sstevel@tonic-gate 				}
183*0Sstevel@tonic-gate 			else if (!strcmp(ctrlname, "default_algorithms"))
184*0Sstevel@tonic-gate 				{
185*0Sstevel@tonic-gate 				if (!ENGINE_set_default_string(e, ctrlvalue))
186*0Sstevel@tonic-gate 					goto err;
187*0Sstevel@tonic-gate 				}
188*0Sstevel@tonic-gate 			else if (!ENGINE_ctrl_cmd_string(e,
189*0Sstevel@tonic-gate 					ctrlname, ctrlvalue, 0))
190*0Sstevel@tonic-gate 				return 0;
191*0Sstevel@tonic-gate 			}
192*0Sstevel@tonic-gate 
193*0Sstevel@tonic-gate 
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate 		}
196*0Sstevel@tonic-gate 	if (e && (do_init == -1) && !int_engine_init(e))
197*0Sstevel@tonic-gate 		goto err;
198*0Sstevel@tonic-gate 	ret = 1;
199*0Sstevel@tonic-gate 	err:
200*0Sstevel@tonic-gate 	if (e)
201*0Sstevel@tonic-gate 		ENGINE_free(e);
202*0Sstevel@tonic-gate 	return ret;
203*0Sstevel@tonic-gate 	}
204*0Sstevel@tonic-gate 
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate static int int_engine_module_init(CONF_IMODULE *md, const CONF *cnf)
207*0Sstevel@tonic-gate 	{
208*0Sstevel@tonic-gate 	STACK_OF(CONF_VALUE) *elist;
209*0Sstevel@tonic-gate 	CONF_VALUE *cval;
210*0Sstevel@tonic-gate 	int i;
211*0Sstevel@tonic-gate #ifdef ENGINE_CONF_DEBUG
212*0Sstevel@tonic-gate 	fprintf(stderr, "Called engine module: name %s, value %s\n",
213*0Sstevel@tonic-gate 			CONF_imodule_get_name(md), CONF_imodule_get_value(md));
214*0Sstevel@tonic-gate #endif
215*0Sstevel@tonic-gate 	/* Value is a section containing ENGINEs to configure */
216*0Sstevel@tonic-gate 	elist = NCONF_get_section(cnf, CONF_imodule_get_value(md));
217*0Sstevel@tonic-gate 
218*0Sstevel@tonic-gate 	if (!elist)
219*0Sstevel@tonic-gate 		{
220*0Sstevel@tonic-gate 		ENGINEerr(ENGINE_F_ENGINE_MODULE_INIT, ENGINE_R_ENGINES_SECTION_ERROR);
221*0Sstevel@tonic-gate 		return 0;
222*0Sstevel@tonic-gate 		}
223*0Sstevel@tonic-gate 
224*0Sstevel@tonic-gate 	for (i = 0; i < sk_CONF_VALUE_num(elist); i++)
225*0Sstevel@tonic-gate 		{
226*0Sstevel@tonic-gate 		cval = sk_CONF_VALUE_value(elist, i);
227*0Sstevel@tonic-gate 		if (!int_engine_configure(cval->name, cval->value, cnf))
228*0Sstevel@tonic-gate 			return 0;
229*0Sstevel@tonic-gate 		}
230*0Sstevel@tonic-gate 
231*0Sstevel@tonic-gate 	return 1;
232*0Sstevel@tonic-gate 	}
233*0Sstevel@tonic-gate 
234*0Sstevel@tonic-gate static void int_engine_module_finish(CONF_IMODULE *md)
235*0Sstevel@tonic-gate 	{
236*0Sstevel@tonic-gate 	ENGINE *e;
237*0Sstevel@tonic-gate 	while ((e = sk_ENGINE_pop(initialized_engines)))
238*0Sstevel@tonic-gate 		ENGINE_finish(e);
239*0Sstevel@tonic-gate 	sk_ENGINE_free(initialized_engines);
240*0Sstevel@tonic-gate 	initialized_engines = NULL;
241*0Sstevel@tonic-gate 	}
242*0Sstevel@tonic-gate 
243*0Sstevel@tonic-gate 
244*0Sstevel@tonic-gate void ENGINE_add_conf_module(void)
245*0Sstevel@tonic-gate 	{
246*0Sstevel@tonic-gate 	CONF_module_add("engines",
247*0Sstevel@tonic-gate 			int_engine_module_init,
248*0Sstevel@tonic-gate 			int_engine_module_finish);
249*0Sstevel@tonic-gate 	}
250