Run the withlibctx.pl script
[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 "internal/dso.h"
19 #include "internal/thread_once.h"
20 #include <openssl/x509.h>
21 #include <openssl/trace.h>
22 #include <openssl/engine.h>
23
24 DEFINE_STACK_OF(CONF_MODULE)
25 DEFINE_STACK_OF(CONF_IMODULE)
26
27 #define DSO_mod_init_name "OPENSSL_init"
28 #define DSO_mod_finish_name "OPENSSL_finish"
29
30 /*
31  * This structure contains a data about supported modules. entries in this
32  * table correspond to either dynamic or static modules.
33  */
34
35 struct 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.
53  */
54
55 struct conf_imodule_st {
56     CONF_MODULE *pmod;
57     char *name;
58     char *value;
59     unsigned long flags;
60     void *usr_data;
61 };
62
63 static STACK_OF(CONF_MODULE) *supported_modules = NULL;
64 static STACK_OF(CONF_IMODULE) *initialized_modules = NULL;
65
66 static CRYPTO_ONCE load_builtin_modules = CRYPTO_ONCE_STATIC_INIT;
67
68 static void module_free(CONF_MODULE *md);
69 static void module_finish(CONF_IMODULE *imod);
70 static int module_run(const CONF *cnf, const char *name, const char *value,
71                       unsigned long flags);
72 static CONF_MODULE *module_add(DSO *dso, const char *name,
73                                conf_init_func *ifunc,
74                                conf_finish_func *ffunc);
75 static CONF_MODULE *module_find(const char *name);
76 static int module_init(CONF_MODULE *pmod, const char *name, const char *value,
77                        const CONF *cnf);
78 static CONF_MODULE *module_load_dso(const CONF *cnf, const char *name,
79                                     const char *value);
80
81 static 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
93 /* Main function: load modules from a CONF structure */
94
95 int CONF_modules_load(const CONF *cnf, const char *appname,
96                       unsigned long flags)
97 {
98     STACK_OF(CONF_VALUE) *values;
99     CONF_VALUE *vl;
100     char *vsection = NULL;
101     int ret, i;
102
103     if (!cnf)
104         return 1;
105
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
112     if (appname)
113         vsection = NCONF_get_string(cnf, NULL, appname);
114
115     if (!appname || (!vsection && (flags & CONF_MFLAGS_DEFAULT_SECTION)))
116         vsection = NCONF_get_string(cnf, NULL, "openssl_conf");
117
118     if (!vsection) {
119         ERR_clear_error();
120         return 1;
121     }
122
123     OSSL_TRACE1(CONF, "Configuration in section %s\n", vsection);
124     values = NCONF_get_section(cnf, vsection);
125
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         }
131         return 0;
132     }
133
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);
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                 return ret;
142     }
143
144     return 1;
145
146 }
147
148 int CONF_modules_load_file_ex(OPENSSL_CTX *libctx, const char *filename,
149                               const char *appname, unsigned long flags)
150 {
151     char *file = NULL;
152     CONF *conf = NULL;
153     int ret = 0, diagnostics = 0;
154
155     conf = NCONF_new_ex(libctx, NULL);
156     if (conf == NULL)
157         goto err;
158
159     if (filename == NULL) {
160         file = CONF_get1_default_config_file();
161         if (file == NULL)
162             goto err;
163     } else {
164         file = (char *)filename;
165     }
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);
177     diagnostics = conf_diagnostics(conf);
178
179  err:
180     if (filename == NULL)
181         OPENSSL_free(file);
182     NCONF_free(conf);
183
184     if ((flags & CONF_MFLAGS_IGNORE_RETURN_CODES) != 0 && !diagnostics)
185         return 1;
186
187     return ret;
188 }
189
190 int CONF_modules_load_file(const char *filename,
191                            const char *appname, unsigned long flags)
192 {
193     return CONF_modules_load_file_ex(NULL, filename, appname, flags);
194 }
195
196 DEFINE_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
207 static int module_run(const CONF *cnf, const char *name, const char *value,
208                       unsigned long flags)
209 {
210     CONF_MODULE *md;
211     int ret;
212
213     if (!RUN_ONCE(&load_builtin_modules, do_load_builtin_modules))
214         return -1;
215
216     md = module_find(name);
217
218     /* Module not found: try to load DSO */
219     if (!md && !(flags & CONF_MFLAGS_NO_DSO))
220         md = module_load_dso(cnf, name, value);
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];
235
236             CONFerr(CONF_F_MODULE_RUN, CONF_R_MODULE_INITIALIZATION_ERROR);
237             BIO_snprintf(rcode, sizeof(rcode), "%-8d", ret);
238             ERR_add_error_data(6, "module=", name, ", value=", value,
239                                ", retcode=", rcode);
240         }
241     }
242
243     return ret;
244 }
245
246 /* Load a module from a DSO */
247 static CONF_MODULE *module_load_dso(const CONF *cnf,
248                                     const char *name, const char *value)
249 {
250     DSO *dso = NULL;
251     conf_init_func *ifunc;
252     conf_finish_func *ffunc;
253     const char *path = NULL;
254     int errcode = 0;
255     CONF_MODULE *md;
256
257     /* Look for alternative path in module section */
258     path = NCONF_get_string(cnf, value, "path");
259     if (path == NULL) {
260         ERR_clear_error();
261         path = name;
262     }
263     dso = DSO_load(NULL, path, NULL, 0);
264     if (dso == NULL) {
265         errcode = CONF_R_ERROR_LOADING_DSO;
266         goto err;
267     }
268     ifunc = (conf_init_func *)DSO_bind_func(dso, DSO_mod_init_name);
269     if (ifunc == NULL) {
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
277     if (md == NULL)
278         goto err;
279
280     return md;
281
282  err:
283     DSO_free(dso);
284     CONFerr(CONF_F_MODULE_LOAD_DSO, errcode);
285     ERR_add_error_data(4, "module=", name, ", path=", path);
286     return NULL;
287 }
288
289 /* add module to list */
290 static CONF_MODULE *module_add(DSO *dso, const char *name,
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;
298     if ((tmod = OPENSSL_zalloc(sizeof(*tmod))) == NULL) {
299         CONFerr(CONF_F_MODULE_ADD, ERR_R_MALLOC_FAILURE);
300         return NULL;
301     }
302
303     tmod->dso = dso;
304     tmod->name = OPENSSL_strdup(name);
305     tmod->init = ifunc;
306     tmod->finish = ffunc;
307     if (tmod->name == NULL) {
308         OPENSSL_free(tmod);
309         return NULL;
310     }
311
312     if (!sk_CONF_MODULE_push(supported_modules, tmod)) {
313         OPENSSL_free(tmod->name);
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.
325  */
326
327 static CONF_MODULE *module_find(const char *name)
328 {
329     CONF_MODULE *tmod;
330     int i, nchar;
331     char *p;
332     p = strrchr(name, '.');
333
334     if (p)
335         nchar = p - name;
336     else
337         nchar = strlen(name);
338
339     for (i = 0; i < sk_CONF_MODULE_num(supported_modules); i++) {
340         tmod = sk_CONF_MODULE_value(supported_modules, i);
341         if (strncmp(tmod->name, name, nchar) == 0)
342             return tmod;
343     }
344
345     return NULL;
346
347 }
348
349 /* initialize a module */
350 static int module_init(CONF_MODULE *pmod, const char *name, const char *value,
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 */
358     imod = OPENSSL_malloc(sizeof(*imod));
359     if (imod == NULL)
360         goto err;
361
362     imod->pmod = pmod;
363     imod->name = OPENSSL_strdup(name);
364     imod->value = OPENSSL_strdup(value);
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) {
404         OPENSSL_free(imod->name);
405         OPENSSL_free(imod->value);
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.
417  */
418
419 void CONF_modules_unload(int all)
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 }
439
440 /* unload a single module */
441 static void module_free(CONF_MODULE *md)
442 {
443     DSO_free(md->dso);
444     OPENSSL_free(md->name);
445     OPENSSL_free(md);
446 }
447
448 /* finish and free up all modules instances */
449
450 void CONF_modules_finish(void)
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 }
460
461 /* finish a module instance */
462
463 static void module_finish(CONF_IMODULE *imod)
464 {
465     if (!imod)
466         return;
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 }
474
475 /* Add a static module to OpenSSL */
476
477 int 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 }
485
486 void conf_modules_free_int(void)
487 {
488     CONF_modules_finish();
489     CONF_modules_unload(1);
490 }
491
492 /* Utility functions */
493
494 const char *CONF_imodule_get_name(const CONF_IMODULE *md)
495 {
496     return md->name;
497 }
498
499 const char *CONF_imodule_get_value(const CONF_IMODULE *md)
500 {
501     return md->value;
502 }
503
504 void *CONF_imodule_get_usr_data(const CONF_IMODULE *md)
505 {
506     return md->usr_data;
507 }
508
509 void CONF_imodule_set_usr_data(CONF_IMODULE *md, void *usr_data)
510 {
511     md->usr_data = usr_data;
512 }
513
514 CONF_MODULE *CONF_imodule_get_module(const CONF_IMODULE *md)
515 {
516     return md->pmod;
517 }
518
519 unsigned long CONF_imodule_get_flags(const CONF_IMODULE *md)
520 {
521     return md->flags;
522 }
523
524 void CONF_imodule_set_flags(CONF_IMODULE *md, unsigned long flags)
525 {
526     md->flags = flags;
527 }
528
529 void *CONF_module_get_usr_data(CONF_MODULE *pmod)
530 {
531     return pmod->usr_data;
532 }
533
534 void CONF_module_set_usr_data(CONF_MODULE *pmod, void *usr_data)
535 {
536     pmod->usr_data = usr_data;
537 }
538
539 /* Return default config file name */
540
541 char *CONF_get1_default_config_file(void)
542 {
543     const char *t;
544     char *file, *sep = "";
545     size_t size;
546
547     if ((file = ossl_safe_getenv("OPENSSL_CONF")) != NULL)
548         return OPENSSL_strdup(file);
549
550     t = X509_get_default_cert_area();
551 #ifndef OPENSSL_SYS_VMS
552     sep = "/";
553 #endif
554     size = strlen(t) + strlen(sep) + strlen(OPENSSL_CONF) + 1;
555     file = OPENSSL_malloc(size);
556
557     if (file == NULL)
558         return NULL;
559     BIO_snprintf(file, size, "%s%s%s", t, sep, OPENSSL_CONF);
560
561     return file;
562 }
563
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.
569  */
570
571 int CONF_parse_list(const char *list_, int sep, int nospc,
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);
590         if (p == lstart || *lstart == '\0')
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 }