]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/conf/conf_mod.c
Convert all {NAME}err() in crypto/ to their corresponding ERR_raise() call
[thirdparty/openssl.git] / crypto / conf / conf_mod.c
1 /*
2 * Copyright 2002-2020 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(ERR_LIB_CONF, CONF_R_OPENSSL_CONF_REFERENCES_MISSING_SECTION);
125 ERR_add_error_data(2, "openssl_conf=", vsection);
126 } else {
127 ERR_pop_to_mark();
128 }
129 return 0;
130 }
131 ERR_pop_to_mark();
132
133 for (i = 0; i < sk_CONF_VALUE_num(values); i++) {
134 vl = sk_CONF_VALUE_value(values, i);
135 ERR_set_mark();
136 ret = module_run(cnf, vl->name, vl->value, flags);
137 OSSL_TRACE3(CONF, "Running module %s (%s) returned %d\n",
138 vl->name, vl->value, ret);
139 if (ret <= 0)
140 if (!(flags & CONF_MFLAGS_IGNORE_ERRORS)) {
141 ERR_clear_last_mark();
142 return ret;
143 }
144 ERR_pop_to_mark();
145 }
146
147 return 1;
148
149 }
150
151 int CONF_modules_load_file_ex(OSSL_LIB_CTX *libctx, const char *filename,
152 const char *appname, unsigned long flags)
153 {
154 char *file = NULL;
155 CONF *conf = NULL;
156 int ret = 0, diagnostics = 0;
157
158 ERR_set_mark();
159 conf = NCONF_new_ex(libctx, NULL);
160 if (conf == NULL)
161 goto err;
162
163 if (filename == NULL) {
164 file = CONF_get1_default_config_file();
165 if (file == NULL)
166 goto err;
167 } else {
168 file = (char *)filename;
169 }
170
171 if (NCONF_load(conf, file, NULL) <= 0) {
172 if ((flags & CONF_MFLAGS_IGNORE_MISSING_FILE) &&
173 (ERR_GET_REASON(ERR_peek_last_error()) == CONF_R_NO_SUCH_FILE)) {
174 ret = 1;
175 }
176 goto err;
177 }
178
179 ret = CONF_modules_load(conf, appname, flags);
180 diagnostics = conf_diagnostics(conf);
181
182 err:
183 if (filename == NULL)
184 OPENSSL_free(file);
185 NCONF_free(conf);
186
187 if ((flags & CONF_MFLAGS_IGNORE_RETURN_CODES) != 0 && !diagnostics)
188 ret = 1;
189
190 if (ret > 0)
191 ERR_pop_to_mark();
192 else
193 ERR_clear_last_mark();
194
195 return ret;
196 }
197
198 int CONF_modules_load_file(const char *filename,
199 const char *appname, unsigned long flags)
200 {
201 return CONF_modules_load_file_ex(NULL, filename, appname, flags);
202 }
203
204 DEFINE_RUN_ONCE_STATIC(do_load_builtin_modules)
205 {
206 OPENSSL_load_builtin_modules();
207 #ifndef OPENSSL_NO_ENGINE
208 /* Need to load ENGINEs */
209 ENGINE_load_builtin_engines();
210 #endif
211 return 1;
212 }
213
214 static int module_run(const CONF *cnf, const char *name, const char *value,
215 unsigned long flags)
216 {
217 CONF_MODULE *md;
218 int ret;
219
220 if (!RUN_ONCE(&load_builtin_modules, do_load_builtin_modules))
221 return -1;
222
223 md = module_find(name);
224
225 /* Module not found: try to load DSO */
226 if (!md && !(flags & CONF_MFLAGS_NO_DSO))
227 md = module_load_dso(cnf, name, value);
228
229 if (!md) {
230 if (!(flags & CONF_MFLAGS_SILENT)) {
231 ERR_raise(ERR_LIB_CONF, CONF_R_UNKNOWN_MODULE_NAME);
232 ERR_add_error_data(2, "module=", name);
233 }
234 return -1;
235 }
236
237 ret = module_init(md, name, value, cnf);
238
239 if (ret <= 0) {
240 if (!(flags & CONF_MFLAGS_SILENT)) {
241 char rcode[DECIMAL_SIZE(ret) + 1];
242
243 ERR_raise(ERR_LIB_CONF, CONF_R_MODULE_INITIALIZATION_ERROR);
244 BIO_snprintf(rcode, sizeof(rcode), "%-8d", ret);
245 ERR_add_error_data(6, "module=", name, ", value=", value,
246 ", retcode=", rcode);
247 }
248 }
249
250 return ret;
251 }
252
253 /* Load a module from a DSO */
254 static CONF_MODULE *module_load_dso(const CONF *cnf,
255 const char *name, const char *value)
256 {
257 DSO *dso = NULL;
258 conf_init_func *ifunc;
259 conf_finish_func *ffunc;
260 const char *path = NULL;
261 int errcode = 0;
262 CONF_MODULE *md;
263
264 /* Look for alternative path in module section */
265 path = _CONF_get_string(cnf, value, "path");
266 if (path == NULL) {
267 path = name;
268 }
269 dso = DSO_load(NULL, path, NULL, 0);
270 if (dso == NULL) {
271 errcode = CONF_R_ERROR_LOADING_DSO;
272 goto err;
273 }
274 ifunc = (conf_init_func *)DSO_bind_func(dso, DSO_mod_init_name);
275 if (ifunc == NULL) {
276 errcode = CONF_R_MISSING_INIT_FUNCTION;
277 goto err;
278 }
279 ffunc = (conf_finish_func *)DSO_bind_func(dso, DSO_mod_finish_name);
280 /* All OK, add module */
281 md = module_add(dso, name, ifunc, ffunc);
282
283 if (md == NULL)
284 goto err;
285
286 return md;
287
288 err:
289 DSO_free(dso);
290 ERR_raise(ERR_LIB_CONF, errcode);
291 ERR_add_error_data(4, "module=", name, ", path=", path);
292 return NULL;
293 }
294
295 /* add module to list */
296 static CONF_MODULE *module_add(DSO *dso, const char *name,
297 conf_init_func *ifunc, conf_finish_func *ffunc)
298 {
299 CONF_MODULE *tmod = NULL;
300 if (supported_modules == NULL)
301 supported_modules = sk_CONF_MODULE_new_null();
302 if (supported_modules == NULL)
303 return NULL;
304 if ((tmod = OPENSSL_zalloc(sizeof(*tmod))) == NULL) {
305 ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
306 return NULL;
307 }
308
309 tmod->dso = dso;
310 tmod->name = OPENSSL_strdup(name);
311 tmod->init = ifunc;
312 tmod->finish = ffunc;
313 if (tmod->name == NULL) {
314 OPENSSL_free(tmod);
315 return NULL;
316 }
317
318 if (!sk_CONF_MODULE_push(supported_modules, tmod)) {
319 OPENSSL_free(tmod->name);
320 OPENSSL_free(tmod);
321 return NULL;
322 }
323
324 return tmod;
325 }
326
327 /*
328 * Find a module from the list. We allow module names of the form
329 * modname.XXXX to just search for modname to allow the same module to be
330 * initialized more than once.
331 */
332
333 static CONF_MODULE *module_find(const char *name)
334 {
335 CONF_MODULE *tmod;
336 int i, nchar;
337 char *p;
338 p = strrchr(name, '.');
339
340 if (p)
341 nchar = p - name;
342 else
343 nchar = strlen(name);
344
345 for (i = 0; i < sk_CONF_MODULE_num(supported_modules); i++) {
346 tmod = sk_CONF_MODULE_value(supported_modules, i);
347 if (strncmp(tmod->name, name, nchar) == 0)
348 return tmod;
349 }
350
351 return NULL;
352
353 }
354
355 /* initialize a module */
356 static int module_init(CONF_MODULE *pmod, const char *name, const char *value,
357 const CONF *cnf)
358 {
359 int ret = 1;
360 int init_called = 0;
361 CONF_IMODULE *imod = NULL;
362
363 /* Otherwise add initialized module to list */
364 imod = OPENSSL_malloc(sizeof(*imod));
365 if (imod == NULL)
366 goto err;
367
368 imod->pmod = pmod;
369 imod->name = OPENSSL_strdup(name);
370 imod->value = OPENSSL_strdup(value);
371 imod->usr_data = NULL;
372
373 if (!imod->name || !imod->value)
374 goto memerr;
375
376 /* Try to initialize module */
377 if (pmod->init) {
378 ret = pmod->init(imod, cnf);
379 init_called = 1;
380 /* Error occurred, exit */
381 if (ret <= 0)
382 goto err;
383 }
384
385 if (initialized_modules == NULL) {
386 initialized_modules = sk_CONF_IMODULE_new_null();
387 if (!initialized_modules) {
388 ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
389 goto err;
390 }
391 }
392
393 if (!sk_CONF_IMODULE_push(initialized_modules, imod)) {
394 ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
395 goto err;
396 }
397
398 pmod->links++;
399
400 return ret;
401
402 err:
403
404 /* We've started the module so we'd better finish it */
405 if (pmod->finish && init_called)
406 pmod->finish(imod);
407
408 memerr:
409 if (imod) {
410 OPENSSL_free(imod->name);
411 OPENSSL_free(imod->value);
412 OPENSSL_free(imod);
413 }
414
415 return -1;
416
417 }
418
419 /*
420 * Unload any dynamic modules that have a link count of zero: i.e. have no
421 * active initialized modules. If 'all' is set then all modules are unloaded
422 * including static ones.
423 */
424
425 void CONF_modules_unload(int all)
426 {
427 int i;
428 CONF_MODULE *md;
429 CONF_modules_finish();
430 /* unload modules in reverse order */
431 for (i = sk_CONF_MODULE_num(supported_modules) - 1; i >= 0; i--) {
432 md = sk_CONF_MODULE_value(supported_modules, i);
433 /* If static or in use and 'all' not set ignore it */
434 if (((md->links > 0) || !md->dso) && !all)
435 continue;
436 /* Since we're working in reverse this is OK */
437 (void)sk_CONF_MODULE_delete(supported_modules, i);
438 module_free(md);
439 }
440 if (sk_CONF_MODULE_num(supported_modules) == 0) {
441 sk_CONF_MODULE_free(supported_modules);
442 supported_modules = NULL;
443 }
444 }
445
446 /* unload a single module */
447 static void module_free(CONF_MODULE *md)
448 {
449 DSO_free(md->dso);
450 OPENSSL_free(md->name);
451 OPENSSL_free(md);
452 }
453
454 /* finish and free up all modules instances */
455
456 void CONF_modules_finish(void)
457 {
458 CONF_IMODULE *imod;
459 while (sk_CONF_IMODULE_num(initialized_modules) > 0) {
460 imod = sk_CONF_IMODULE_pop(initialized_modules);
461 module_finish(imod);
462 }
463 sk_CONF_IMODULE_free(initialized_modules);
464 initialized_modules = NULL;
465 }
466
467 /* finish a module instance */
468
469 static void module_finish(CONF_IMODULE *imod)
470 {
471 if (!imod)
472 return;
473 if (imod->pmod->finish)
474 imod->pmod->finish(imod);
475 imod->pmod->links--;
476 OPENSSL_free(imod->name);
477 OPENSSL_free(imod->value);
478 OPENSSL_free(imod);
479 }
480
481 /* Add a static module to OpenSSL */
482
483 int CONF_module_add(const char *name, conf_init_func *ifunc,
484 conf_finish_func *ffunc)
485 {
486 if (module_add(NULL, name, ifunc, ffunc))
487 return 1;
488 else
489 return 0;
490 }
491
492 void conf_modules_free_int(void)
493 {
494 CONF_modules_finish();
495 CONF_modules_unload(1);
496 }
497
498 /* Utility functions */
499
500 const char *CONF_imodule_get_name(const CONF_IMODULE *md)
501 {
502 return md->name;
503 }
504
505 const char *CONF_imodule_get_value(const CONF_IMODULE *md)
506 {
507 return md->value;
508 }
509
510 void *CONF_imodule_get_usr_data(const CONF_IMODULE *md)
511 {
512 return md->usr_data;
513 }
514
515 void CONF_imodule_set_usr_data(CONF_IMODULE *md, void *usr_data)
516 {
517 md->usr_data = usr_data;
518 }
519
520 CONF_MODULE *CONF_imodule_get_module(const CONF_IMODULE *md)
521 {
522 return md->pmod;
523 }
524
525 unsigned long CONF_imodule_get_flags(const CONF_IMODULE *md)
526 {
527 return md->flags;
528 }
529
530 void CONF_imodule_set_flags(CONF_IMODULE *md, unsigned long flags)
531 {
532 md->flags = flags;
533 }
534
535 void *CONF_module_get_usr_data(CONF_MODULE *pmod)
536 {
537 return pmod->usr_data;
538 }
539
540 void CONF_module_set_usr_data(CONF_MODULE *pmod, void *usr_data)
541 {
542 pmod->usr_data = usr_data;
543 }
544
545 /* Return default config file name */
546
547 char *CONF_get1_default_config_file(void)
548 {
549 const char *t;
550 char *file, *sep = "";
551 size_t size;
552
553 if ((file = ossl_safe_getenv("OPENSSL_CONF")) != NULL)
554 return OPENSSL_strdup(file);
555
556 t = X509_get_default_cert_area();
557 #ifndef OPENSSL_SYS_VMS
558 sep = "/";
559 #endif
560 size = strlen(t) + strlen(sep) + strlen(OPENSSL_CONF) + 1;
561 file = OPENSSL_malloc(size);
562
563 if (file == NULL)
564 return NULL;
565 BIO_snprintf(file, size, "%s%s%s", t, sep, OPENSSL_CONF);
566
567 return file;
568 }
569
570 /*
571 * This function takes a list separated by 'sep' and calls the callback
572 * function giving the start and length of each member optionally stripping
573 * leading and trailing whitespace. This can be used to parse comma separated
574 * lists for example.
575 */
576
577 int CONF_parse_list(const char *list_, int sep, int nospc,
578 int (*list_cb) (const char *elem, int len, void *usr),
579 void *arg)
580 {
581 int ret;
582 const char *lstart, *tmpend, *p;
583
584 if (list_ == NULL) {
585 ERR_raise(ERR_LIB_CONF, CONF_R_LIST_CANNOT_BE_NULL);
586 return 0;
587 }
588
589 lstart = list_;
590 for (;;) {
591 if (nospc) {
592 while (*lstart && isspace((unsigned char)*lstart))
593 lstart++;
594 }
595 p = strchr(lstart, sep);
596 if (p == lstart || *lstart == '\0')
597 ret = list_cb(NULL, 0, arg);
598 else {
599 if (p)
600 tmpend = p - 1;
601 else
602 tmpend = lstart + strlen(lstart) - 1;
603 if (nospc) {
604 while (isspace((unsigned char)*tmpend))
605 tmpend--;
606 }
607 ret = list_cb(lstart, tmpend - lstart + 1, arg);
608 }
609 if (ret <= 0)
610 return ret;
611 if (p == NULL)
612 return 1;
613 lstart = p + 1;
614 }
615 }