]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/conf/conf_mod.c
Correct and simplify use of ERR_clear_error() etc. for loading DSO libs
[thirdparty/openssl.git] / crypto / conf / conf_mod.c
CommitLineData
0f113f3e 1/*
22e27978 2 * Copyright 2002-2020 The OpenSSL Project Authors. All Rights Reserved.
bc37d996 3 *
2044d382 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
62867571
RS
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
bc37d996
DSH
8 */
9
e4468e6d
P
10/* We need to use some engine deprecated APIs */
11#define OPENSSL_SUPPRESS_DEPRECATED
12
07016a8a 13#include "internal/cryptlib.h"
bc37d996 14#include <stdio.h>
df5eaa8a 15#include <ctype.h>
bc37d996 16#include <openssl/crypto.h>
cbf6959f 17#include "internal/conf.h"
55c61473 18#include "openssl/conf_api.h"
921de151 19#include "internal/dso.h"
22e27978 20#include "internal/thread_once.h"
bc37d996 21#include <openssl/x509.h>
0196ad63 22#include <openssl/trace.h>
22e27978 23#include <openssl/engine.h>
bc37d996 24
852c2ed2
RS
25DEFINE_STACK_OF(CONF_MODULE)
26DEFINE_STACK_OF(CONF_IMODULE)
27
bc37d996
DSH
28#define DSO_mod_init_name "OPENSSL_init"
29#define DSO_mod_finish_name "OPENSSL_finish"
30
0f113f3e
MC
31/*
32 * This structure contains a data about supported modules. entries in this
33 * table correspond to either dynamic or static modules.
bc37d996
DSH
34 */
35
0f113f3e
MC
36struct 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.
bc37d996
DSH
54 */
55
0f113f3e
MC
56struct conf_imodule_st {
57 CONF_MODULE *pmod;
58 char *name;
59 char *value;
60 unsigned long flags;
61 void *usr_data;
62};
bc37d996
DSH
63
64static STACK_OF(CONF_MODULE) *supported_modules = NULL;
65static STACK_OF(CONF_IMODULE) *initialized_modules = NULL;
66
22e27978
SL
67static CRYPTO_ONCE load_builtin_modules = CRYPTO_ONCE_STATIC_INIT;
68
bc37d996
DSH
69static void module_free(CONF_MODULE *md);
70static void module_finish(CONF_IMODULE *imod);
159b9a4d 71static int module_run(const CONF *cnf, const char *name, const char *value,
0f113f3e 72 unsigned long flags);
cca28b29 73static CONF_MODULE *module_add(DSO *dso, const char *name,
0f113f3e
MC
74 conf_init_func *ifunc,
75 conf_finish_func *ffunc);
159b9a4d
F
76static CONF_MODULE *module_find(const char *name);
77static int module_init(CONF_MODULE *pmod, const char *name, const char *value,
0f113f3e 78 const CONF *cnf);
02e112a8 79static CONF_MODULE *module_load_dso(const CONF *cnf, const char *name,
159b9a4d 80 const char *value);
bc37d996 81
33855c0a
P
82static int conf_diagnostics(const CONF *cnf)
83{
55c61473 84 return _CONF_get_number(cnf, NULL, "config_diagnostics") != 0;
33855c0a
P
85}
86
bc37d996
DSH
87/* Main function: load modules from a CONF structure */
88
9dd5ae65 89int CONF_modules_load(const CONF *cnf, const char *appname,
0f113f3e
MC
90 unsigned long flags)
91{
92 STACK_OF(CONF_VALUE) *values;
93 CONF_VALUE *vl;
94 char *vsection = NULL;
0f113f3e 95 int ret, i;
bc37d996 96
0f113f3e
MC
97 if (!cnf)
98 return 1;
bc37d996 99
33855c0a
P
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
55c61473 106 ERR_set_mark();
0f113f3e
MC
107 if (appname)
108 vsection = NCONF_get_string(cnf, NULL, appname);
bc37d996 109
0f113f3e
MC
110 if (!appname || (!vsection && (flags & CONF_MFLAGS_DEFAULT_SECTION)))
111 vsection = NCONF_get_string(cnf, NULL, "openssl_conf");
bc37d996 112
0f113f3e 113 if (!vsection) {
55c61473 114 ERR_pop_to_mark();
0f113f3e
MC
115 return 1;
116 }
bc37d996 117
0196ad63 118 OSSL_TRACE1(CONF, "Configuration in section %s\n", vsection);
0f113f3e 119 values = NCONF_get_section(cnf, vsection);
bc37d996 120
33b4f731
P
121 if (values == NULL) {
122 if (!(flags & CONF_MFLAGS_SILENT)) {
55c61473 123 ERR_clear_last_mark();
33b4f731
P
124 CONFerr(0, CONF_R_OPENSSL_CONF_REFERENCES_MISSING_SECTION);
125 ERR_add_error_data(2, "openssl_conf=", vsection);
55c61473
DDO
126 } else {
127 ERR_pop_to_mark();
33b4f731 128 }
0f113f3e 129 return 0;
33b4f731 130 }
55c61473 131 ERR_pop_to_mark();
bc37d996 132
0f113f3e
MC
133 for (i = 0; i < sk_CONF_VALUE_num(values); i++) {
134 vl = sk_CONF_VALUE_value(values, i);
55c61473 135 ERR_set_mark();
0f113f3e 136 ret = module_run(cnf, vl->name, vl->value, flags);
0196ad63
RL
137 OSSL_TRACE3(CONF, "Running module %s (%s) returned %d\n",
138 vl->name, vl->value, ret);
0f113f3e 139 if (ret <= 0)
55c61473
DDO
140 if (!(flags & CONF_MFLAGS_IGNORE_ERRORS)) {
141 ERR_clear_last_mark();
0f113f3e 142 return ret;
55c61473
DDO
143 }
144 ERR_pop_to_mark();
0f113f3e 145 }
bc37d996 146
0f113f3e 147 return 1;
bc37d996 148
0f113f3e 149}
bc37d996 150
d8652be0
MC
151int CONF_modules_load_file_ex(OPENSSL_CTX *libctx, const char *filename,
152 const char *appname, unsigned long flags)
0f113f3e
MC
153{
154 char *file = NULL;
155 CONF *conf = NULL;
33855c0a 156 int ret = 0, diagnostics = 0;
22e27978 157
55c61473 158 ERR_set_mark();
d8652be0 159 conf = NCONF_new_ex(libctx, NULL);
90945fa3 160 if (conf == NULL)
0f113f3e
MC
161 goto err;
162
163 if (filename == NULL) {
164 file = CONF_get1_default_config_file();
22e27978 165 if (file == NULL)
0f113f3e 166 goto err;
22e27978 167 } else {
0f113f3e 168 file = (char *)filename;
22e27978 169 }
0f113f3e
MC
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)) {
0f113f3e
MC
174 ret = 1;
175 }
176 goto err;
177 }
178
179 ret = CONF_modules_load(conf, appname, flags);
33855c0a 180 diagnostics = conf_diagnostics(conf);
0f113f3e
MC
181
182 err:
183 if (filename == NULL)
184 OPENSSL_free(file);
185 NCONF_free(conf);
186
33855c0a 187 if ((flags & CONF_MFLAGS_IGNORE_RETURN_CODES) != 0 && !diagnostics)
55c61473 188 ret = 1;
df1f538f 189
55c61473
DDO
190 if (ret)
191 ERR_pop_to_mark();
192 else
193 ERR_clear_last_mark();
0f113f3e
MC
194 return ret;
195}
bc37d996 196
22e27978
SL
197int CONF_modules_load_file(const char *filename,
198 const char *appname, unsigned long flags)
199{
d8652be0 200 return CONF_modules_load_file_ex(NULL, filename, appname, flags);
22e27978
SL
201}
202
203DEFINE_RUN_ONCE_STATIC(do_load_builtin_modules)
204{
205 OPENSSL_load_builtin_modules();
206#ifndef OPENSSL_NO_ENGINE
207 /* Need to load ENGINEs */
208 ENGINE_load_builtin_engines();
209#endif
210 ERR_clear_error();
211 return 1;
212}
213
159b9a4d 214static int module_run(const CONF *cnf, const char *name, const char *value,
0f113f3e
MC
215 unsigned long flags)
216{
217 CONF_MODULE *md;
218 int ret;
219
22e27978
SL
220 if (!RUN_ONCE(&load_builtin_modules, do_load_builtin_modules))
221 return -1;
222
0f113f3e
MC
223 md = module_find(name);
224
225 /* Module not found: try to load DSO */
226 if (!md && !(flags & CONF_MFLAGS_NO_DSO))
a773b52a 227 md = module_load_dso(cnf, name, value);
0f113f3e
MC
228
229 if (!md) {
230 if (!(flags & CONF_MFLAGS_SILENT)) {
231 CONFerr(CONF_F_MODULE_RUN, 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];
a2371fa9 242
0f113f3e 243 CONFerr(CONF_F_MODULE_RUN, CONF_R_MODULE_INITIALIZATION_ERROR);
a2371fa9 244 BIO_snprintf(rcode, sizeof(rcode), "%-8d", ret);
0f113f3e
MC
245 ERR_add_error_data(6, "module=", name, ", value=", value,
246 ", retcode=", rcode);
247 }
248 }
249
250 return ret;
251}
bc37d996
DSH
252
253/* Load a module from a DSO */
02e112a8 254static CONF_MODULE *module_load_dso(const CONF *cnf,
159b9a4d 255 const char *name, const char *value)
0f113f3e
MC
256{
257 DSO *dso = NULL;
258 conf_init_func *ifunc;
259 conf_finish_func *ffunc;
159b9a4d 260 const char *path = NULL;
0f113f3e
MC
261 int errcode = 0;
262 CONF_MODULE *md;
12a765a5 263
0f113f3e 264 /* Look for alternative path in module section */
55c61473 265 path = _CONF_get_string(cnf, value, "path");
12a765a5 266 if (path == NULL) {
0f113f3e
MC
267 path = name;
268 }
269 dso = DSO_load(NULL, path, NULL, 0);
12a765a5 270 if (dso == NULL) {
0f113f3e
MC
271 errcode = CONF_R_ERROR_LOADING_DSO;
272 goto err;
273 }
274 ifunc = (conf_init_func *)DSO_bind_func(dso, DSO_mod_init_name);
12a765a5 275 if (ifunc == NULL) {
0f113f3e
MC
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
12a765a5 283 if (md == NULL)
0f113f3e
MC
284 goto err;
285
286 return md;
287
288 err:
efa7dd64 289 DSO_free(dso);
0f113f3e
MC
290 CONFerr(CONF_F_MODULE_LOAD_DSO, errcode);
291 ERR_add_error_data(4, "module=", name, ", path=", path);
292 return NULL;
293}
bc37d996
DSH
294
295/* add module to list */
cca28b29 296static CONF_MODULE *module_add(DSO *dso, const char *name,
0f113f3e
MC
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;
cdb10bae
RS
304 if ((tmod = OPENSSL_zalloc(sizeof(*tmod))) == NULL) {
305 CONFerr(CONF_F_MODULE_ADD, ERR_R_MALLOC_FAILURE);
0f113f3e 306 return NULL;
cdb10bae 307 }
0f113f3e
MC
308
309 tmod->dso = dso;
7644a9ae 310 tmod->name = OPENSSL_strdup(name);
0f113f3e
MC
311 tmod->init = ifunc;
312 tmod->finish = ffunc;
b7b8e948
DM
313 if (tmod->name == NULL) {
314 OPENSSL_free(tmod);
315 return NULL;
316 }
0f113f3e
MC
317
318 if (!sk_CONF_MODULE_push(supported_modules, tmod)) {
b7b8e948 319 OPENSSL_free(tmod->name);
0f113f3e
MC
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.
bc37d996
DSH
331 */
332
159b9a4d 333static CONF_MODULE *module_find(const char *name)
0f113f3e
MC
334{
335 CONF_MODULE *tmod;
336 int i, nchar;
337 char *p;
338 p = strrchr(name, '.');
bc37d996 339
0f113f3e
MC
340 if (p)
341 nchar = p - name;
342 else
343 nchar = strlen(name);
bc37d996 344
0f113f3e
MC
345 for (i = 0; i < sk_CONF_MODULE_num(supported_modules); i++) {
346 tmod = sk_CONF_MODULE_value(supported_modules, i);
86885c28 347 if (strncmp(tmod->name, name, nchar) == 0)
0f113f3e
MC
348 return tmod;
349 }
bc37d996 350
0f113f3e 351 return NULL;
bc37d996 352
0f113f3e 353}
bc37d996
DSH
354
355/* initialize a module */
159b9a4d 356static int module_init(CONF_MODULE *pmod, const char *name, const char *value,
0f113f3e
MC
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 */
b4faea50 364 imod = OPENSSL_malloc(sizeof(*imod));
90945fa3 365 if (imod == NULL)
0f113f3e
MC
366 goto err;
367
368 imod->pmod = pmod;
7644a9ae
RS
369 imod->name = OPENSSL_strdup(name);
370 imod->value = OPENSSL_strdup(value);
0f113f3e
MC
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 CONFerr(CONF_F_MODULE_INIT, ERR_R_MALLOC_FAILURE);
389 goto err;
390 }
391 }
392
393 if (!sk_CONF_IMODULE_push(initialized_modules, imod)) {
394 CONFerr(CONF_F_MODULE_INIT, 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) {
b548a1f1
RS
410 OPENSSL_free(imod->name);
411 OPENSSL_free(imod->value);
0f113f3e
MC
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.
bc37d996
DSH
423 */
424
425void CONF_modules_unload(int all)
0f113f3e
MC
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}
bc37d996
DSH
445
446/* unload a single module */
447static void module_free(CONF_MODULE *md)
0f113f3e 448{
efa7dd64 449 DSO_free(md->dso);
0f113f3e
MC
450 OPENSSL_free(md->name);
451 OPENSSL_free(md);
452}
bc37d996
DSH
453
454/* finish and free up all modules instances */
455
456void CONF_modules_finish(void)
0f113f3e
MC
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}
bc37d996
DSH
466
467/* finish a module instance */
468
469static void module_finish(CONF_IMODULE *imod)
0f113f3e 470{
efa7dd64
RS
471 if (!imod)
472 return;
0f113f3e
MC
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}
bc37d996
DSH
480
481/* Add a static module to OpenSSL */
482
0f113f3e
MC
483int 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}
bc37d996 491
b3599dbb 492void conf_modules_free_int(void)
0f113f3e
MC
493{
494 CONF_modules_finish();
495 CONF_modules_unload(1);
496}
bc37d996
DSH
497
498/* Utility functions */
499
9dd5ae65 500const char *CONF_imodule_get_name(const CONF_IMODULE *md)
0f113f3e
MC
501{
502 return md->name;
503}
bc37d996 504
9dd5ae65 505const char *CONF_imodule_get_value(const CONF_IMODULE *md)
0f113f3e
MC
506{
507 return md->value;
508}
bc37d996 509
9dd5ae65 510void *CONF_imodule_get_usr_data(const CONF_IMODULE *md)
0f113f3e
MC
511{
512 return md->usr_data;
513}
bc37d996
DSH
514
515void CONF_imodule_set_usr_data(CONF_IMODULE *md, void *usr_data)
0f113f3e
MC
516{
517 md->usr_data = usr_data;
518}
bc37d996 519
9dd5ae65 520CONF_MODULE *CONF_imodule_get_module(const CONF_IMODULE *md)
0f113f3e
MC
521{
522 return md->pmod;
523}
bc37d996 524
9dd5ae65 525unsigned long CONF_imodule_get_flags(const CONF_IMODULE *md)
0f113f3e
MC
526{
527 return md->flags;
528}
bc37d996
DSH
529
530void CONF_imodule_set_flags(CONF_IMODULE *md, unsigned long flags)
0f113f3e
MC
531{
532 md->flags = flags;
533}
bc37d996
DSH
534
535void *CONF_module_get_usr_data(CONF_MODULE *pmod)
0f113f3e
MC
536{
537 return pmod->usr_data;
538}
bc37d996
DSH
539
540void CONF_module_set_usr_data(CONF_MODULE *pmod, void *usr_data)
0f113f3e
MC
541{
542 pmod->usr_data = usr_data;
543}
bc37d996 544
c9501c22
DSH
545/* Return default config file name */
546
547char *CONF_get1_default_config_file(void)
0f113f3e 548{
e306f83c 549 const char *t;
a2371fa9 550 char *file, *sep = "";
e306f83c 551 size_t size;
c9501c22 552
5c39a55d
P
553 if ((file = ossl_safe_getenv("OPENSSL_CONF")) != NULL)
554 return OPENSSL_strdup(file);
c9501c22 555
e306f83c 556 t = X509_get_default_cert_area();
c9501c22 557#ifndef OPENSSL_SYS_VMS
a2371fa9 558 sep = "/";
c9501c22 559#endif
e306f83c
RL
560 size = strlen(t) + strlen(sep) + strlen(OPENSSL_CONF) + 1;
561 file = OPENSSL_malloc(size);
c9501c22 562
90945fa3 563 if (file == NULL)
0f113f3e 564 return NULL;
e306f83c 565 BIO_snprintf(file, size, "%s%s%s", t, sep, OPENSSL_CONF);
c9501c22 566
0f113f3e
MC
567 return file;
568}
df5eaa8a 569
0f113f3e
MC
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.
df5eaa8a
DSH
575 */
576
3822740c 577int CONF_parse_list(const char *list_, int sep, int nospc,
0f113f3e
MC
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 CONFerr(CONF_F_CONF_PARSE_LIST, 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);
12a765a5 596 if (p == lstart || *lstart == '\0')
0f113f3e
MC
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}