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