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