fa7f6b9d9fa9adc61a1fd561e0e47c544b806003
[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_with_libctx(OPENSSL_CTX *libctx,
149                                        const char *filename,
150                                        const char *appname, unsigned long flags)
151 {
152     char *file = NULL;
153     CONF *conf = NULL;
154     int ret = 0, diagnostics = 0;
155
156     conf = NCONF_new_with_libctx(libctx, NULL);
157     if (conf == NULL)
158         goto err;
159
160     if (filename == NULL) {
161         file = CONF_get1_default_config_file();
162         if (file == NULL)
163             goto err;
164     } else {
165         file = (char *)filename;
166     }
167
168     if (NCONF_load(conf, file, NULL) <= 0) {
169         if ((flags & CONF_MFLAGS_IGNORE_MISSING_FILE) &&
170             (ERR_GET_REASON(ERR_peek_last_error()) == CONF_R_NO_SUCH_FILE)) {
171             ERR_clear_error();
172             ret = 1;
173         }
174         goto err;
175     }
176
177     ret = CONF_modules_load(conf, appname, flags);
178     diagnostics = conf_diagnostics(conf);
179
180  err:
181     if (filename == NULL)
182         OPENSSL_free(file);
183     NCONF_free(conf);
184
185     if ((flags & CONF_MFLAGS_IGNORE_RETURN_CODES) != 0 && !diagnostics)
186         return 1;
187
188     return ret;
189 }
190
191 int CONF_modules_load_file(const char *filename,
192                            const char *appname, unsigned long flags)
193 {
194     return CONF_modules_load_file_with_libctx(NULL, filename, appname, flags);
195 }
196
197 DEFINE_RUN_ONCE_STATIC(do_load_builtin_modules)
198 {
199     OPENSSL_load_builtin_modules();
200 #ifndef OPENSSL_NO_ENGINE
201     /* Need to load ENGINEs */
202     ENGINE_load_builtin_engines();
203 #endif
204     ERR_clear_error();
205     return 1;
206 }
207
208 static int module_run(const CONF *cnf, const char *name, const char *value,
209                       unsigned long flags)
210 {
211     CONF_MODULE *md;
212     int ret;
213
214     if (!RUN_ONCE(&load_builtin_modules, do_load_builtin_modules))
215         return -1;
216
217     md = module_find(name);
218
219     /* Module not found: try to load DSO */
220     if (!md && !(flags & CONF_MFLAGS_NO_DSO))
221         md = module_load_dso(cnf, name, value);
222
223     if (!md) {
224         if (!(flags & CONF_MFLAGS_SILENT)) {
225             CONFerr(CONF_F_MODULE_RUN, CONF_R_UNKNOWN_MODULE_NAME);
226             ERR_add_error_data(2, "module=", name);
227         }
228         return -1;
229     }
230
231     ret = module_init(md, name, value, cnf);
232
233     if (ret <= 0) {
234         if (!(flags & CONF_MFLAGS_SILENT)) {
235             char rcode[DECIMAL_SIZE(ret) + 1];
236
237             CONFerr(CONF_F_MODULE_RUN, CONF_R_MODULE_INITIALIZATION_ERROR);
238             BIO_snprintf(rcode, sizeof(rcode), "%-8d", ret);
239             ERR_add_error_data(6, "module=", name, ", value=", value,
240                                ", retcode=", rcode);
241         }
242     }
243
244     return ret;
245 }
246
247 /* Load a module from a DSO */
248 static CONF_MODULE *module_load_dso(const CONF *cnf,
249                                     const char *name, const char *value)
250 {
251     DSO *dso = NULL;
252     conf_init_func *ifunc;
253     conf_finish_func *ffunc;
254     const char *path = NULL;
255     int errcode = 0;
256     CONF_MODULE *md;
257
258     /* Look for alternative path in module section */
259     path = NCONF_get_string(cnf, value, "path");
260     if (path == NULL) {
261         ERR_clear_error();
262         path = name;
263     }
264     dso = DSO_load(NULL, path, NULL, 0);
265     if (dso == NULL) {
266         errcode = CONF_R_ERROR_LOADING_DSO;
267         goto err;
268     }
269     ifunc = (conf_init_func *)DSO_bind_func(dso, DSO_mod_init_name);
270     if (ifunc == NULL) {
271         errcode = CONF_R_MISSING_INIT_FUNCTION;
272         goto err;
273     }
274     ffunc = (conf_finish_func *)DSO_bind_func(dso, DSO_mod_finish_name);
275     /* All OK, add module */
276     md = module_add(dso, name, ifunc, ffunc);
277
278     if (md == NULL)
279         goto err;
280
281     return md;
282
283  err:
284     DSO_free(dso);
285     CONFerr(CONF_F_MODULE_LOAD_DSO, errcode);
286     ERR_add_error_data(4, "module=", name, ", path=", path);
287     return NULL;
288 }
289
290 /* add module to list */
291 static CONF_MODULE *module_add(DSO *dso, const char *name,
292                                conf_init_func *ifunc, conf_finish_func *ffunc)
293 {
294     CONF_MODULE *tmod = NULL;
295     if (supported_modules == NULL)
296         supported_modules = sk_CONF_MODULE_new_null();
297     if (supported_modules == NULL)
298         return NULL;
299     if ((tmod = OPENSSL_zalloc(sizeof(*tmod))) == NULL) {
300         CONFerr(CONF_F_MODULE_ADD, ERR_R_MALLOC_FAILURE);
301         return NULL;
302     }
303
304     tmod->dso = dso;
305     tmod->name = OPENSSL_strdup(name);
306     tmod->init = ifunc;
307     tmod->finish = ffunc;
308     if (tmod->name == NULL) {
309         OPENSSL_free(tmod);
310         return NULL;
311     }
312
313     if (!sk_CONF_MODULE_push(supported_modules, tmod)) {
314         OPENSSL_free(tmod->name);
315         OPENSSL_free(tmod);
316         return NULL;
317     }
318
319     return tmod;
320 }
321
322 /*
323  * Find a module from the list. We allow module names of the form
324  * modname.XXXX to just search for modname to allow the same module to be
325  * initialized more than once.
326  */
327
328 static CONF_MODULE *module_find(const char *name)
329 {
330     CONF_MODULE *tmod;
331     int i, nchar;
332     char *p;
333     p = strrchr(name, '.');
334
335     if (p)
336         nchar = p - name;
337     else
338         nchar = strlen(name);
339
340     for (i = 0; i < sk_CONF_MODULE_num(supported_modules); i++) {
341         tmod = sk_CONF_MODULE_value(supported_modules, i);
342         if (strncmp(tmod->name, name, nchar) == 0)
343             return tmod;
344     }
345
346     return NULL;
347
348 }
349
350 /* initialize a module */
351 static int module_init(CONF_MODULE *pmod, const char *name, const char *value,
352                        const CONF *cnf)
353 {
354     int ret = 1;
355     int init_called = 0;
356     CONF_IMODULE *imod = NULL;
357
358     /* Otherwise add initialized module to list */
359     imod = OPENSSL_malloc(sizeof(*imod));
360     if (imod == NULL)
361         goto err;
362
363     imod->pmod = pmod;
364     imod->name = OPENSSL_strdup(name);
365     imod->value = OPENSSL_strdup(value);
366     imod->usr_data = NULL;
367
368     if (!imod->name || !imod->value)
369         goto memerr;
370
371     /* Try to initialize module */
372     if (pmod->init) {
373         ret = pmod->init(imod, cnf);
374         init_called = 1;
375         /* Error occurred, exit */
376         if (ret <= 0)
377             goto err;
378     }
379
380     if (initialized_modules == NULL) {
381         initialized_modules = sk_CONF_IMODULE_new_null();
382         if (!initialized_modules) {
383             CONFerr(CONF_F_MODULE_INIT, ERR_R_MALLOC_FAILURE);
384             goto err;
385         }
386     }
387
388     if (!sk_CONF_IMODULE_push(initialized_modules, imod)) {
389         CONFerr(CONF_F_MODULE_INIT, ERR_R_MALLOC_FAILURE);
390         goto err;
391     }
392
393     pmod->links++;
394
395     return ret;
396
397  err:
398
399     /* We've started the module so we'd better finish it */
400     if (pmod->finish && init_called)
401         pmod->finish(imod);
402
403  memerr:
404     if (imod) {
405         OPENSSL_free(imod->name);
406         OPENSSL_free(imod->value);
407         OPENSSL_free(imod);
408     }
409
410     return -1;
411
412 }
413
414 /*
415  * Unload any dynamic modules that have a link count of zero: i.e. have no
416  * active initialized modules. If 'all' is set then all modules are unloaded
417  * including static ones.
418  */
419
420 void CONF_modules_unload(int all)
421 {
422     int i;
423     CONF_MODULE *md;
424     CONF_modules_finish();
425     /* unload modules in reverse order */
426     for (i = sk_CONF_MODULE_num(supported_modules) - 1; i >= 0; i--) {
427         md = sk_CONF_MODULE_value(supported_modules, i);
428         /* If static or in use and 'all' not set ignore it */
429         if (((md->links > 0) || !md->dso) && !all)
430             continue;
431         /* Since we're working in reverse this is OK */
432         (void)sk_CONF_MODULE_delete(supported_modules, i);
433         module_free(md);
434     }
435     if (sk_CONF_MODULE_num(supported_modules) == 0) {
436         sk_CONF_MODULE_free(supported_modules);
437         supported_modules = NULL;
438     }
439 }
440
441 /* unload a single module */
442 static void module_free(CONF_MODULE *md)
443 {
444     DSO_free(md->dso);
445     OPENSSL_free(md->name);
446     OPENSSL_free(md);
447 }
448
449 /* finish and free up all modules instances */
450
451 void CONF_modules_finish(void)
452 {
453     CONF_IMODULE *imod;
454     while (sk_CONF_IMODULE_num(initialized_modules) > 0) {
455         imod = sk_CONF_IMODULE_pop(initialized_modules);
456         module_finish(imod);
457     }
458     sk_CONF_IMODULE_free(initialized_modules);
459     initialized_modules = NULL;
460 }
461
462 /* finish a module instance */
463
464 static void module_finish(CONF_IMODULE *imod)
465 {
466     if (!imod)
467         return;
468     if (imod->pmod->finish)
469         imod->pmod->finish(imod);
470     imod->pmod->links--;
471     OPENSSL_free(imod->name);
472     OPENSSL_free(imod->value);
473     OPENSSL_free(imod);
474 }
475
476 /* Add a static module to OpenSSL */
477
478 int CONF_module_add(const char *name, conf_init_func *ifunc,
479                     conf_finish_func *ffunc)
480 {
481     if (module_add(NULL, name, ifunc, ffunc))
482         return 1;
483     else
484         return 0;
485 }
486
487 void conf_modules_free_int(void)
488 {
489     CONF_modules_finish();
490     CONF_modules_unload(1);
491 }
492
493 /* Utility functions */
494
495 const char *CONF_imodule_get_name(const CONF_IMODULE *md)
496 {
497     return md->name;
498 }
499
500 const char *CONF_imodule_get_value(const CONF_IMODULE *md)
501 {
502     return md->value;
503 }
504
505 void *CONF_imodule_get_usr_data(const CONF_IMODULE *md)
506 {
507     return md->usr_data;
508 }
509
510 void CONF_imodule_set_usr_data(CONF_IMODULE *md, void *usr_data)
511 {
512     md->usr_data = usr_data;
513 }
514
515 CONF_MODULE *CONF_imodule_get_module(const CONF_IMODULE *md)
516 {
517     return md->pmod;
518 }
519
520 unsigned long CONF_imodule_get_flags(const CONF_IMODULE *md)
521 {
522     return md->flags;
523 }
524
525 void CONF_imodule_set_flags(CONF_IMODULE *md, unsigned long flags)
526 {
527     md->flags = flags;
528 }
529
530 void *CONF_module_get_usr_data(CONF_MODULE *pmod)
531 {
532     return pmod->usr_data;
533 }
534
535 void CONF_module_set_usr_data(CONF_MODULE *pmod, void *usr_data)
536 {
537     pmod->usr_data = usr_data;
538 }
539
540 /* Return default config file name */
541
542 char *CONF_get1_default_config_file(void)
543 {
544     const char *t;
545     char *file, *sep = "";
546     size_t size;
547
548     if ((file = ossl_safe_getenv("OPENSSL_CONF")) != NULL)
549         return OPENSSL_strdup(file);
550
551     t = X509_get_default_cert_area();
552 #ifndef OPENSSL_SYS_VMS
553     sep = "/";
554 #endif
555     size = strlen(t) + strlen(sep) + strlen(OPENSSL_CONF) + 1;
556     file = OPENSSL_malloc(size);
557
558     if (file == NULL)
559         return NULL;
560     BIO_snprintf(file, size, "%s%s%s", t, sep, OPENSSL_CONF);
561
562     return file;
563 }
564
565 /*
566  * This function takes a list separated by 'sep' and calls the callback
567  * function giving the start and length of each member optionally stripping
568  * leading and trailing whitespace. This can be used to parse comma separated
569  * lists for example.
570  */
571
572 int CONF_parse_list(const char *list_, int sep, int nospc,
573                     int (*list_cb) (const char *elem, int len, void *usr),
574                     void *arg)
575 {
576     int ret;
577     const char *lstart, *tmpend, *p;
578
579     if (list_ == NULL) {
580         CONFerr(CONF_F_CONF_PARSE_LIST, CONF_R_LIST_CANNOT_BE_NULL);
581         return 0;
582     }
583
584     lstart = list_;
585     for (;;) {
586         if (nospc) {
587             while (*lstart && isspace((unsigned char)*lstart))
588                 lstart++;
589         }
590         p = strchr(lstart, sep);
591         if (p == lstart || *lstart == '\0')
592             ret = list_cb(NULL, 0, arg);
593         else {
594             if (p)
595                 tmpend = p - 1;
596             else
597                 tmpend = lstart + strlen(lstart) - 1;
598             if (nospc) {
599                 while (isspace((unsigned char)*tmpend))
600                     tmpend--;
601             }
602             ret = list_cb(lstart, tmpend - lstart + 1, arg);
603         }
604         if (ret <= 0)
605             return ret;
606         if (p == NULL)
607             return 1;
608         lstart = p + 1;
609     }
610 }