xref: /openbsd/lib/libcrypto/conf/conf_mod.c (revision bbdd77aa)
1 /* $OpenBSD: conf_mod.c,v 1.38 2024/04/09 13:56:30 beck Exp $ */
2 /* Written by Stephen Henson (steve@openssl.org) 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 <ctype.h>
60 #include <stdio.h>
61 #include <string.h>
62 #include <unistd.h>
63 
64 #include <openssl/conf.h>
65 #include <openssl/crypto.h>
66 #include <openssl/err.h>
67 #include <openssl/x509.h>
68 
69 /* This structure contains data about supported modules. */
70 struct conf_module_st {
71 	/* Name of the module */
72 	char *name;
73 	/* Init function */
74 	conf_init_func *init;
75 	/* Finish function */
76 	conf_finish_func *finish;
77 	/* Number of successfully initialized modules */
78 	int links;
79 	void *usr_data;
80 };
81 
82 
83 /* This structure contains information about modules that have been
84  * successfully initialized. There may be more than one entry for a
85  * given module.
86  */
87 
88 struct conf_imodule_st {
89 	CONF_MODULE *mod;
90 	char *name;
91 	char *value;
92 	unsigned long flags;
93 	void *usr_data;
94 };
95 
96 static STACK_OF(CONF_MODULE) *supported_modules = NULL;
97 static STACK_OF(CONF_IMODULE) *initialized_modules = NULL;
98 
99 static void module_free(CONF_MODULE *mod);
100 static void imodule_free(CONF_IMODULE *imod);
101 static void module_finish(CONF_IMODULE *imod);
102 static int module_run(const CONF *cnf, char *name, char *value,
103     unsigned long flags);
104 static int module_add(const char *name, conf_init_func *ifunc,
105     conf_finish_func *ffunc);
106 static CONF_MODULE *module_find(char *name);
107 static int module_init(CONF_MODULE *mod, char *name, char *value,
108     const CONF *cnf);
109 
110 /* Main function: load modules from a CONF structure */
111 
112 int
CONF_modules_load(const CONF * cnf,const char * appname,unsigned long flags)113 CONF_modules_load(const CONF *cnf, const char *appname, unsigned long flags)
114 {
115 	STACK_OF(CONF_VALUE) *values;
116 	CONF_VALUE *vl;
117 	char *vsection = NULL;
118 
119 	int ret, i;
120 
121 	if (!cnf)
122 		return 1;
123 
124 	if (appname)
125 		vsection = NCONF_get_string(cnf, NULL, appname);
126 
127 	if (!appname || (!vsection && (flags & CONF_MFLAGS_DEFAULT_SECTION)))
128 		vsection = NCONF_get_string(cnf, NULL, "openssl_conf");
129 
130 	if (!vsection) {
131 		ERR_clear_error();
132 		return 1;
133 	}
134 
135 	values = NCONF_get_section(cnf, vsection);
136 
137 	if (!values)
138 		return 0;
139 
140 	for (i = 0; i < sk_CONF_VALUE_num(values); i++) {
141 		vl = sk_CONF_VALUE_value(values, i);
142 		ret = module_run(cnf, vl->name, vl->value, flags);
143 		if (ret <= 0)
144 			if (!(flags & CONF_MFLAGS_IGNORE_ERRORS))
145 				return ret;
146 	}
147 
148 	return 1;
149 }
150 LCRYPTO_ALIAS(CONF_modules_load);
151 
152 int
CONF_modules_load_file(const char * filename,const char * appname,unsigned long flags)153 CONF_modules_load_file(const char *filename, const char *appname,
154     unsigned long flags)
155 {
156 	char *file = NULL;
157 	CONF *conf = NULL;
158 	int ret = 0;
159 	conf = NCONF_new(NULL);
160 	if (!conf)
161 		goto err;
162 
163 	if (filename == NULL) {
164 		file = CONF_get1_default_config_file();
165 		if (!file)
166 			goto err;
167 	} else
168 		file = (char *)filename;
169 
170 	if (NCONF_load(conf, file, NULL) <= 0) {
171 		if ((flags & CONF_MFLAGS_IGNORE_MISSING_FILE) &&
172 		    (ERR_GET_REASON(ERR_peek_last_error()) ==
173 		    CONF_R_NO_SUCH_FILE)) {
174 			ERR_clear_error();
175 			ret = 1;
176 		}
177 		goto err;
178 	}
179 
180 	ret = CONF_modules_load(conf, appname, flags);
181 
182 err:
183 	if (filename == NULL)
184 		free(file);
185 	NCONF_free(conf);
186 
187 	return ret;
188 }
189 LCRYPTO_ALIAS(CONF_modules_load_file);
190 
191 static int
module_run(const CONF * cnf,char * name,char * value,unsigned long flags)192 module_run(const CONF *cnf, char *name, char *value, unsigned long flags)
193 {
194 	CONF_MODULE *mod;
195 	int ret;
196 
197 	if ((mod = module_find(name)) == NULL) {
198 		if (!(flags & CONF_MFLAGS_SILENT)) {
199 			CONFerror(CONF_R_UNKNOWN_MODULE_NAME);
200 			ERR_asprintf_error_data("module=%s", name);
201 		}
202 		return -1;
203 	}
204 
205 	ret = module_init(mod, name, value, cnf);
206 
207 	if (ret <= 0) {
208 		if (!(flags & CONF_MFLAGS_SILENT)) {
209 			CONFerror(CONF_R_MODULE_INITIALIZATION_ERROR);
210 			ERR_asprintf_error_data
211 			    ("module=%s, value=%s, retcode=%-8d",
212 			    name, value, ret);
213 		}
214 	}
215 
216 	return ret;
217 }
218 
219 static int
module_add(const char * name,conf_init_func * ifunc,conf_finish_func * ffunc)220 module_add(const char *name, conf_init_func *ifunc, conf_finish_func *ffunc)
221 {
222 	CONF_MODULE *mod = NULL;
223 	int ret = 0;
224 
225 	if (name == NULL)
226 		goto err;
227 
228 	if (supported_modules == NULL)
229 		supported_modules = sk_CONF_MODULE_new_null();
230 	if (supported_modules == NULL)
231 		goto err;
232 
233 	if ((mod = calloc(1, sizeof(*mod))) == NULL)
234 		goto err;
235 	if ((mod->name = strdup(name)) == NULL)
236 		goto err;
237 	mod->init = ifunc;
238 	mod->finish = ffunc;
239 
240 	if (!sk_CONF_MODULE_push(supported_modules, mod))
241 		goto err;
242 	mod = NULL;
243 
244 	ret = 1;
245 
246  err:
247 	module_free(mod);
248 
249 	return ret;
250 }
251 
252 /* Find a module from the list. We allow module names of the
253  * form modname.XXXX to just search for modname to allow the
254  * same module to be initialized more than once.
255  */
256 
257 static CONF_MODULE *
module_find(char * name)258 module_find(char *name)
259 {
260 	CONF_MODULE *mod;
261 	int i, nchar;
262 	char *p;
263 
264 	p = strrchr(name, '.');
265 
266 	if (p)
267 		nchar = p - name;
268 	else
269 		nchar = strlen(name);
270 
271 	for (i = 0; i < sk_CONF_MODULE_num(supported_modules); i++) {
272 		mod = sk_CONF_MODULE_value(supported_modules, i);
273 		if (!strncmp(mod->name, name, nchar))
274 			return mod;
275 	}
276 
277 	return NULL;
278 }
279 
280 /* initialize a module */
281 static int
module_init(CONF_MODULE * mod,char * name,char * value,const CONF * cnf)282 module_init(CONF_MODULE *mod, char *name, char *value, const CONF *cnf)
283 {
284 	CONF_IMODULE *imod = NULL;
285 	int need_finish = 0;
286 	int ret = -1;
287 
288 	if (name == NULL || value == NULL)
289 		goto err;
290 
291 	if ((imod = calloc(1, sizeof(*imod))) == NULL)
292 		goto err;
293 
294 	imod->mod = mod;
295 
296 	if ((imod->name = strdup(name)) == NULL)
297 		goto err;
298 	if ((imod->value = strdup(value)) == NULL)
299 		goto err;
300 
301 	if (mod->init != NULL) {
302 		need_finish = 1;
303 		if (mod->init(imod, cnf) <= 0)
304 			goto err;
305 	}
306 
307 	if (initialized_modules == NULL)
308 		initialized_modules = sk_CONF_IMODULE_new_null();
309 	if (initialized_modules == NULL)
310 		goto err;
311 
312 	if (!sk_CONF_IMODULE_push(initialized_modules, imod))
313 		goto err;
314 	imod = NULL;
315 	need_finish = 0;
316 
317 	mod->links++;
318 
319 	ret = 1;
320 
321  err:
322 	if (need_finish && mod->finish != NULL)
323 		mod->finish(imod);
324 
325 	imodule_free(imod);
326 
327 	return ret;
328 }
329 
330 /* Unload any dynamic modules that have a link count of zero:
331  * i.e. have no active initialized modules. If 'all' is set
332  * then all modules are unloaded including static ones.
333  */
334 
335 void
CONF_modules_unload(int all)336 CONF_modules_unload(int all)
337 {
338 	int i;
339 	CONF_MODULE *mod;
340 
341 	CONF_modules_finish();
342 
343 	/* unload modules in reverse order */
344 	for (i = sk_CONF_MODULE_num(supported_modules) - 1; i >= 0; i--) {
345 		mod = sk_CONF_MODULE_value(supported_modules, i);
346 		if (!all)
347 			continue;
348 		/* Since we're working in reverse this is OK */
349 		(void)sk_CONF_MODULE_delete(supported_modules, i);
350 		module_free(mod);
351 	}
352 	if (sk_CONF_MODULE_num(supported_modules) == 0) {
353 		sk_CONF_MODULE_free(supported_modules);
354 		supported_modules = NULL;
355 	}
356 }
357 LCRYPTO_ALIAS(CONF_modules_unload);
358 
359 /* unload a single module */
360 static void
module_free(CONF_MODULE * mod)361 module_free(CONF_MODULE *mod)
362 {
363 	if (mod == NULL)
364 		return;
365 
366 	free(mod->name);
367 	free(mod);
368 }
369 
370 static void
imodule_free(CONF_IMODULE * imod)371 imodule_free(CONF_IMODULE *imod)
372 {
373 	if (imod == NULL)
374 		return;
375 
376 	free(imod->name);
377 	free(imod->value);
378 	free(imod);
379 }
380 
381 /* finish and free up all modules instances */
382 
383 void
CONF_modules_finish(void)384 CONF_modules_finish(void)
385 {
386 	CONF_IMODULE *imod;
387 
388 	while (sk_CONF_IMODULE_num(initialized_modules) > 0) {
389 		imod = sk_CONF_IMODULE_pop(initialized_modules);
390 		module_finish(imod);
391 	}
392 	sk_CONF_IMODULE_free(initialized_modules);
393 	initialized_modules = NULL;
394 }
395 LCRYPTO_ALIAS(CONF_modules_finish);
396 
397 /* finish a module instance */
398 
399 static void
module_finish(CONF_IMODULE * imod)400 module_finish(CONF_IMODULE *imod)
401 {
402 	if (imod->mod->finish)
403 		imod->mod->finish(imod);
404 	imod->mod->links--;
405 
406 	imodule_free(imod);
407 }
408 
409 /* Add a static module to OpenSSL */
410 
411 int
CONF_module_add(const char * name,conf_init_func * ifunc,conf_finish_func * ffunc)412 CONF_module_add(const char *name, conf_init_func *ifunc, conf_finish_func *ffunc)
413 {
414 	return module_add(name, ifunc, ffunc);
415 }
416 LCRYPTO_ALIAS(CONF_module_add);
417 
418 void
CONF_modules_free(void)419 CONF_modules_free(void)
420 {
421 	CONF_modules_finish();
422 	CONF_modules_unload(1);
423 }
424 LCRYPTO_ALIAS(CONF_modules_free);
425 
426 /* Utility functions */
427 
428 const char *
CONF_imodule_get_name(const CONF_IMODULE * imod)429 CONF_imodule_get_name(const CONF_IMODULE *imod)
430 {
431 	return imod->name;
432 }
433 LCRYPTO_ALIAS(CONF_imodule_get_name);
434 
435 const char *
CONF_imodule_get_value(const CONF_IMODULE * imod)436 CONF_imodule_get_value(const CONF_IMODULE *imod)
437 {
438 	return imod->value;
439 }
440 LCRYPTO_ALIAS(CONF_imodule_get_value);
441 
442 void *
CONF_imodule_get_usr_data(const CONF_IMODULE * imod)443 CONF_imodule_get_usr_data(const CONF_IMODULE *imod)
444 {
445 	return imod->usr_data;
446 }
447 LCRYPTO_ALIAS(CONF_imodule_get_usr_data);
448 
449 void
CONF_imodule_set_usr_data(CONF_IMODULE * imod,void * usr_data)450 CONF_imodule_set_usr_data(CONF_IMODULE *imod, void *usr_data)
451 {
452 	imod->usr_data = usr_data;
453 }
454 LCRYPTO_ALIAS(CONF_imodule_set_usr_data);
455 
456 CONF_MODULE *
CONF_imodule_get_module(const CONF_IMODULE * imod)457 CONF_imodule_get_module(const CONF_IMODULE *imod)
458 {
459 	return imod->mod;
460 }
461 LCRYPTO_ALIAS(CONF_imodule_get_module);
462 
463 unsigned long
CONF_imodule_get_flags(const CONF_IMODULE * imod)464 CONF_imodule_get_flags(const CONF_IMODULE *imod)
465 {
466 	return imod->flags;
467 }
468 LCRYPTO_ALIAS(CONF_imodule_get_flags);
469 
470 void
CONF_imodule_set_flags(CONF_IMODULE * imod,unsigned long flags)471 CONF_imodule_set_flags(CONF_IMODULE *imod, unsigned long flags)
472 {
473 	imod->flags = flags;
474 }
475 LCRYPTO_ALIAS(CONF_imodule_set_flags);
476 
477 void *
CONF_module_get_usr_data(CONF_MODULE * mod)478 CONF_module_get_usr_data(CONF_MODULE *mod)
479 {
480 	return mod->usr_data;
481 }
482 LCRYPTO_ALIAS(CONF_module_get_usr_data);
483 
484 void
CONF_module_set_usr_data(CONF_MODULE * mod,void * usr_data)485 CONF_module_set_usr_data(CONF_MODULE *mod, void *usr_data)
486 {
487 	mod->usr_data = usr_data;
488 }
489 LCRYPTO_ALIAS(CONF_module_set_usr_data);
490 
491 /* Return default config file name */
492 
493 char *
CONF_get1_default_config_file(void)494 CONF_get1_default_config_file(void)
495 {
496 	char *file = NULL;
497 
498 	if (asprintf(&file, "%s/openssl.cnf",
499 	    X509_get_default_cert_area()) == -1)
500 		return (NULL);
501 	return file;
502 }
503 LCRYPTO_ALIAS(CONF_get1_default_config_file);
504 
505 /* This function takes a list separated by 'sep' and calls the
506  * callback function giving the start and length of each member
507  * optionally stripping leading and trailing whitespace. This can
508  * be used to parse comma separated lists for example.
509  */
510 
511 int
CONF_parse_list(const char * list_,int sep,int nospc,int (* list_cb)(const char * elem,int len,void * usr),void * arg)512 CONF_parse_list(const char *list_, int sep, int nospc,
513     int (*list_cb)(const char *elem, int len, void *usr), void *arg)
514 {
515 	int ret;
516 	const char *lstart, *tmpend, *p;
517 
518 	if (list_ == NULL) {
519 		CONFerror(CONF_R_LIST_CANNOT_BE_NULL);
520 		return 0;
521 	}
522 
523 	lstart = list_;
524 	for (;;) {
525 		if (nospc) {
526 			while (*lstart && isspace((unsigned char)*lstart))
527 				lstart++;
528 		}
529 		p = strchr(lstart, sep);
530 		if (p == lstart || !*lstart)
531 			ret = list_cb(NULL, 0, arg);
532 		else {
533 			if (p)
534 				tmpend = p - 1;
535 			else
536 				tmpend = lstart + strlen(lstart) - 1;
537 			if (nospc) {
538 				while (isspace((unsigned char)*tmpend))
539 					tmpend--;
540 			}
541 			ret = list_cb(lstart, tmpend - lstart + 1, arg);
542 		}
543 		if (ret <= 0)
544 			return ret;
545 		if (p == NULL)
546 			return 1;
547 		lstart = p + 1;
548 	}
549 }
550 LCRYPTO_ALIAS(CONF_parse_list);
551