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