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