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