/src/openssl/crypto/conf/conf_mod.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * Copyright 2002-2024 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 "internal/rcu.h"  | 
15  |  | #include <stdio.h>  | 
16  |  | #include <ctype.h>  | 
17  |  | #include <openssl/crypto.h>  | 
18  |  | #include "internal/conf.h"  | 
19  |  | #include <openssl/conf_api.h>  | 
20  |  | #include "internal/dso.h"  | 
21  |  | #include "internal/thread_once.h"  | 
22  |  | #include <openssl/x509.h>  | 
23  |  | #include <openssl/trace.h>  | 
24  |  | #include <openssl/engine.h>  | 
25  |  | #include "conf_local.h"  | 
26  |  |  | 
27  |  | DEFINE_STACK_OF(CONF_MODULE)  | 
28  |  | DEFINE_STACK_OF(CONF_IMODULE)  | 
29  |  |  | 
30  | 0  | #define DSO_mod_init_name "OPENSSL_init"  | 
31  | 0  | #define DSO_mod_finish_name "OPENSSL_finish"  | 
32  |  |  | 
33  |  | /*  | 
34  |  |  * This structure contains a data about supported modules. entries in this  | 
35  |  |  * table correspond to either dynamic or static modules.  | 
36  |  |  */  | 
37  |  |  | 
38  |  | struct conf_module_st { | 
39  |  |     /* DSO of this module or NULL if static */  | 
40  |  |     DSO *dso;  | 
41  |  |     /* Name of the module */  | 
42  |  |     char *name;  | 
43  |  |     /* Init function */  | 
44  |  |     conf_init_func *init;  | 
45  |  |     /* Finish function */  | 
46  |  |     conf_finish_func *finish;  | 
47  |  |     /* Number of successfully initialized modules */  | 
48  |  |     int links;  | 
49  |  |     void *usr_data;  | 
50  |  | };  | 
51  |  |  | 
52  |  | /*  | 
53  |  |  * This structure contains information about modules that have been  | 
54  |  |  * successfully initialized. There may be more than one entry for a given  | 
55  |  |  * module.  | 
56  |  |  */  | 
57  |  |  | 
58  |  | struct conf_imodule_st { | 
59  |  |     CONF_MODULE *pmod;  | 
60  |  |     char *name;  | 
61  |  |     char *value;  | 
62  |  |     unsigned long flags;  | 
63  |  |     void *usr_data;  | 
64  |  | };  | 
65  |  |  | 
66  |  | static CRYPTO_ONCE init_module_list_lock = CRYPTO_ONCE_STATIC_INIT;  | 
67  |  | static CRYPTO_RCU_LOCK *module_list_lock = NULL;  | 
68  |  | static STACK_OF(CONF_MODULE) *supported_modules = NULL; /* protected by lock */  | 
69  |  | static STACK_OF(CONF_IMODULE) *initialized_modules = NULL; /* protected by lock */  | 
70  |  |  | 
71  |  | static CRYPTO_ONCE load_builtin_modules = CRYPTO_ONCE_STATIC_INIT;  | 
72  |  |  | 
73  |  | static void module_free(CONF_MODULE *md);  | 
74  |  | static void module_finish(CONF_IMODULE *imod);  | 
75  |  | static int module_run(const CONF *cnf, const char *name, const char *value,  | 
76  |  |                       unsigned long flags);  | 
77  |  | static CONF_MODULE *module_add(DSO *dso, const char *name,  | 
78  |  |                                conf_init_func *ifunc,  | 
79  |  |                                conf_finish_func *ffunc);  | 
80  |  | static CONF_MODULE *module_find(const char *name);  | 
81  |  | static int module_init(CONF_MODULE *pmod, const char *name, const char *value,  | 
82  |  |                        const CONF *cnf);  | 
83  |  | static CONF_MODULE *module_load_dso(const CONF *cnf, const char *name,  | 
84  |  |                                     const char *value);  | 
85  |  |  | 
86  |  | static int conf_modules_finish_int(void);  | 
87  |  |  | 
88  |  | static void module_lists_free(void)  | 
89  | 2  | { | 
90  | 2  |     ossl_rcu_lock_free(module_list_lock);  | 
91  | 2  |     module_list_lock = NULL;  | 
92  |  |  | 
93  | 2  |     sk_CONF_MODULE_free(supported_modules);  | 
94  | 2  |     supported_modules = NULL;  | 
95  |  |  | 
96  | 2  |     sk_CONF_IMODULE_free(initialized_modules);  | 
97  | 2  |     initialized_modules = NULL;  | 
98  | 2  | }  | 
99  |  |  | 
100  |  | DEFINE_RUN_ONCE_STATIC(do_init_module_list_lock)  | 
101  | 2  | { | 
102  | 2  |     module_list_lock = ossl_rcu_lock_new(1, NULL);  | 
103  | 2  |     if (module_list_lock == NULL) { | 
104  | 0  |         ERR_raise(ERR_LIB_CONF, ERR_R_CRYPTO_LIB);  | 
105  | 0  |         return 0;  | 
106  | 0  |     }  | 
107  |  |  | 
108  | 2  |     return 1;  | 
109  | 2  | }  | 
110  |  |  | 
111  |  | static int conf_diagnostics(const CONF *cnf)  | 
112  | 0  | { | 
113  | 0  |     int status;  | 
114  | 0  |     long result = 0;  | 
115  |  | 
  | 
116  | 0  |     ERR_set_mark();  | 
117  | 0  |     status = NCONF_get_number_e(cnf, NULL, "config_diagnostics", &result);  | 
118  | 0  |     ERR_pop_to_mark();  | 
119  | 0  |     if (status > 0) { | 
120  | 0  |         OSSL_LIB_CTX_set_conf_diagnostics(cnf->libctx, result > 0);  | 
121  | 0  |         return result > 0;  | 
122  | 0  |     }  | 
123  | 0  |     return OSSL_LIB_CTX_get_conf_diagnostics(cnf->libctx);  | 
124  | 0  | }  | 
125  |  |  | 
126  |  | /* Main function: load modules from a CONF structure */  | 
127  |  |  | 
128  |  | int CONF_modules_load(const CONF *cnf, const char *appname,  | 
129  |  |                       unsigned long flags)  | 
130  | 0  | { | 
131  | 0  |     STACK_OF(CONF_VALUE) *values;  | 
132  | 0  |     CONF_VALUE *vl;  | 
133  | 0  |     char *vsection = NULL;  | 
134  | 0  |     int ret, i;  | 
135  |  | 
  | 
136  | 0  |     if (!cnf)  | 
137  | 0  |         return 1;  | 
138  |  |  | 
139  | 0  |     if (conf_diagnostics(cnf))  | 
140  | 0  |         flags &= ~(CONF_MFLAGS_IGNORE_ERRORS  | 
141  | 0  |                    | CONF_MFLAGS_IGNORE_RETURN_CODES  | 
142  | 0  |                    | CONF_MFLAGS_SILENT  | 
143  | 0  |                    | CONF_MFLAGS_IGNORE_MISSING_FILE);  | 
144  |  | 
  | 
145  | 0  |     ERR_set_mark();  | 
146  | 0  |     if (appname)  | 
147  | 0  |         vsection = NCONF_get_string(cnf, NULL, appname);  | 
148  |  | 
  | 
149  | 0  |     if (!appname || (!vsection && (flags & CONF_MFLAGS_DEFAULT_SECTION)))  | 
150  | 0  |         vsection = NCONF_get_string(cnf, NULL, "openssl_conf");  | 
151  |  | 
  | 
152  | 0  |     if (!vsection) { | 
153  | 0  |         ERR_pop_to_mark();  | 
154  | 0  |         return 1;  | 
155  | 0  |     }  | 
156  |  |  | 
157  | 0  |     OSSL_TRACE1(CONF, "Configuration in section %s\n", vsection);  | 
158  | 0  |     values = NCONF_get_section(cnf, vsection);  | 
159  |  | 
  | 
160  | 0  |     if (values == NULL) { | 
161  | 0  |         if (!(flags & CONF_MFLAGS_SILENT)) { | 
162  | 0  |             ERR_clear_last_mark();  | 
163  | 0  |             ERR_raise_data(ERR_LIB_CONF,  | 
164  | 0  |                            CONF_R_OPENSSL_CONF_REFERENCES_MISSING_SECTION,  | 
165  | 0  |                            "openssl_conf=%s", vsection);  | 
166  | 0  |         } else { | 
167  | 0  |             ERR_pop_to_mark();  | 
168  | 0  |         }  | 
169  | 0  |         return 0;  | 
170  | 0  |     }  | 
171  | 0  |     ERR_pop_to_mark();  | 
172  |  | 
  | 
173  | 0  |     for (i = 0; i < sk_CONF_VALUE_num(values); i++) { | 
174  | 0  |         vl = sk_CONF_VALUE_value(values, i);  | 
175  | 0  |         ERR_set_mark();  | 
176  | 0  |         ret = module_run(cnf, vl->name, vl->value, flags);  | 
177  | 0  |         OSSL_TRACE3(CONF, "Running module %s (%s) returned %d\n",  | 
178  | 0  |                     vl->name, vl->value, ret);  | 
179  | 0  |         if (ret <= 0)  | 
180  | 0  |             if (!(flags & CONF_MFLAGS_IGNORE_ERRORS)) { | 
181  | 0  |                 ERR_clear_last_mark();  | 
182  | 0  |                 return ret;  | 
183  | 0  |             }  | 
184  | 0  |         ERR_pop_to_mark();  | 
185  | 0  |     }  | 
186  |  |  | 
187  | 0  |     return 1;  | 
188  |  | 
  | 
189  | 0  | }  | 
190  |  |  | 
191  |  | int CONF_modules_load_file_ex(OSSL_LIB_CTX *libctx, const char *filename,  | 
192  |  |                               const char *appname, unsigned long flags)  | 
193  | 0  | { | 
194  | 0  |     char *file = NULL;  | 
195  | 0  |     CONF *conf = NULL;  | 
196  | 0  |     int ret = 0, diagnostics = OSSL_LIB_CTX_get_conf_diagnostics(libctx);  | 
197  |  | 
  | 
198  | 0  |     ERR_set_mark();  | 
199  |  | 
  | 
200  | 0  |     if (filename == NULL) { | 
201  | 0  |         file = CONF_get1_default_config_file();  | 
202  | 0  |         if (file == NULL)  | 
203  | 0  |             goto err;  | 
204  | 0  |         if (*file == '\0') { | 
205  |  |             /* Do not try to load an empty file name but do not error out */  | 
206  | 0  |             ret = 1;  | 
207  | 0  |             goto err;  | 
208  | 0  |         }  | 
209  | 0  |     } else { | 
210  | 0  |         file = (char *)filename;  | 
211  | 0  |     }  | 
212  |  |  | 
213  | 0  |     conf = NCONF_new_ex(libctx, NULL);  | 
214  | 0  |     if (conf == NULL)  | 
215  | 0  |         goto err;  | 
216  |  |  | 
217  | 0  |     if (NCONF_load(conf, file, NULL) <= 0) { | 
218  | 0  |         if ((flags & CONF_MFLAGS_IGNORE_MISSING_FILE) &&  | 
219  | 0  |             (ERR_GET_REASON(ERR_peek_last_error()) == CONF_R_NO_SUCH_FILE)) { | 
220  | 0  |             ret = 1;  | 
221  | 0  |         }  | 
222  | 0  |         goto err;  | 
223  | 0  |     }  | 
224  |  |  | 
225  | 0  |     ret = CONF_modules_load(conf, appname, flags);  | 
226  |  |     /* CONF_modules_load() might change the diagnostics setting, reread it. */  | 
227  | 0  |     diagnostics = OSSL_LIB_CTX_get_conf_diagnostics(libctx);  | 
228  |  | 
  | 
229  | 0  |  err:  | 
230  | 0  |     if (filename == NULL)  | 
231  | 0  |         OPENSSL_free(file);  | 
232  | 0  |     NCONF_free(conf);  | 
233  |  | 
  | 
234  | 0  |     if ((flags & CONF_MFLAGS_IGNORE_RETURN_CODES) != 0 && !diagnostics)  | 
235  | 0  |         ret = 1;  | 
236  |  | 
  | 
237  | 0  |     if (ret > 0)  | 
238  | 0  |         ERR_pop_to_mark();  | 
239  | 0  |     else  | 
240  | 0  |         ERR_clear_last_mark();  | 
241  |  | 
  | 
242  | 0  |     return ret;  | 
243  | 0  | }  | 
244  |  |  | 
245  |  | int CONF_modules_load_file(const char *filename,  | 
246  |  |                            const char *appname, unsigned long flags)  | 
247  | 0  | { | 
248  | 0  |     return CONF_modules_load_file_ex(NULL, filename, appname, flags);  | 
249  | 0  | }  | 
250  |  |  | 
251  |  | DEFINE_RUN_ONCE_STATIC(do_load_builtin_modules)  | 
252  | 0  | { | 
253  | 0  |     OPENSSL_load_builtin_modules();  | 
254  | 0  | #ifndef OPENSSL_NO_ENGINE  | 
255  |  |     /* Need to load ENGINEs */  | 
256  | 0  |     ENGINE_load_builtin_engines();  | 
257  | 0  | #endif  | 
258  | 0  |     return 1;  | 
259  | 0  | }  | 
260  |  |  | 
261  |  | static int module_run(const CONF *cnf, const char *name, const char *value,  | 
262  |  |                       unsigned long flags)  | 
263  | 0  | { | 
264  | 0  |     CONF_MODULE *md;  | 
265  | 0  |     int ret;  | 
266  |  | 
  | 
267  | 0  |     if (!RUN_ONCE(&load_builtin_modules, do_load_builtin_modules))  | 
268  | 0  |         return -1;  | 
269  |  |  | 
270  | 0  |     md = module_find(name);  | 
271  |  |  | 
272  |  |     /* Module not found: try to load DSO */  | 
273  | 0  |     if (!md && !(flags & CONF_MFLAGS_NO_DSO))  | 
274  | 0  |         md = module_load_dso(cnf, name, value);  | 
275  |  | 
  | 
276  | 0  |     if (!md) { | 
277  | 0  |         if (!(flags & CONF_MFLAGS_SILENT)) { | 
278  | 0  |             ERR_raise_data(ERR_LIB_CONF, CONF_R_UNKNOWN_MODULE_NAME,  | 
279  | 0  |                            "module=%s", name);  | 
280  | 0  |         }  | 
281  | 0  |         return -1;  | 
282  | 0  |     }  | 
283  |  |  | 
284  | 0  |     ret = module_init(md, name, value, cnf);  | 
285  |  | 
  | 
286  | 0  |     if (ret <= 0) { | 
287  | 0  |         if (!(flags & CONF_MFLAGS_SILENT))  | 
288  | 0  |             ERR_raise_data(ERR_LIB_CONF, CONF_R_MODULE_INITIALIZATION_ERROR,  | 
289  | 0  |                            "module=%s, value=%s retcode=%-8d",  | 
290  | 0  |                            name, value, ret);  | 
291  | 0  |     }  | 
292  |  | 
  | 
293  | 0  |     return ret;  | 
294  | 0  | }  | 
295  |  |  | 
296  |  | /* Load a module from a DSO */  | 
297  |  | static CONF_MODULE *module_load_dso(const CONF *cnf,  | 
298  |  |                                     const char *name, const char *value)  | 
299  | 0  | { | 
300  | 0  |     DSO *dso = NULL;  | 
301  | 0  |     conf_init_func *ifunc;  | 
302  | 0  |     conf_finish_func *ffunc;  | 
303  | 0  |     const char *path = NULL;  | 
304  | 0  |     int errcode = 0;  | 
305  | 0  |     CONF_MODULE *md;  | 
306  |  |  | 
307  |  |     /* Look for alternative path in module section */  | 
308  | 0  |     path = _CONF_get_string(cnf, value, "path");  | 
309  | 0  |     if (path == NULL) { | 
310  | 0  |         path = name;  | 
311  | 0  |     }  | 
312  | 0  |     dso = DSO_load(NULL, path, NULL, 0);  | 
313  | 0  |     if (dso == NULL) { | 
314  | 0  |         errcode = CONF_R_ERROR_LOADING_DSO;  | 
315  | 0  |         goto err;  | 
316  | 0  |     }  | 
317  | 0  |     ifunc = (conf_init_func *)DSO_bind_func(dso, DSO_mod_init_name);  | 
318  | 0  |     if (ifunc == NULL) { | 
319  | 0  |         errcode = CONF_R_MISSING_INIT_FUNCTION;  | 
320  | 0  |         goto err;  | 
321  | 0  |     }  | 
322  | 0  |     ffunc = (conf_finish_func *)DSO_bind_func(dso, DSO_mod_finish_name);  | 
323  |  |     /* All OK, add module */  | 
324  | 0  |     md = module_add(dso, name, ifunc, ffunc);  | 
325  |  | 
  | 
326  | 0  |     if (md == NULL)  | 
327  | 0  |         goto err;  | 
328  |  |  | 
329  | 0  |     return md;  | 
330  |  |  | 
331  | 0  |  err:  | 
332  | 0  |     DSO_free(dso);  | 
333  | 0  |     ERR_raise_data(ERR_LIB_CONF, errcode, "module=%s, path=%s", name, path);  | 
334  | 0  |     return NULL;  | 
335  | 0  | }  | 
336  |  |  | 
337  |  | /* add module to list */  | 
338  |  | static CONF_MODULE *module_add(DSO *dso, const char *name,  | 
339  |  |                                conf_init_func *ifunc, conf_finish_func *ffunc)  | 
340  | 0  | { | 
341  | 0  |     CONF_MODULE *tmod = NULL;  | 
342  | 0  |     STACK_OF(CONF_MODULE) *old_modules;  | 
343  | 0  |     STACK_OF(CONF_MODULE) *new_modules;  | 
344  |  | 
  | 
345  | 0  |     if (!RUN_ONCE(&init_module_list_lock, do_init_module_list_lock))  | 
346  | 0  |         return NULL;  | 
347  |  |  | 
348  | 0  |     ossl_rcu_write_lock(module_list_lock);  | 
349  |  | 
  | 
350  | 0  |     old_modules = ossl_rcu_deref(&supported_modules);  | 
351  |  | 
  | 
352  | 0  |     if (old_modules == NULL)  | 
353  | 0  |         new_modules = sk_CONF_MODULE_new_null();  | 
354  | 0  |     else  | 
355  | 0  |         new_modules = sk_CONF_MODULE_dup(old_modules);  | 
356  |  | 
  | 
357  | 0  |     if (new_modules == NULL)  | 
358  | 0  |         goto err;  | 
359  |  |  | 
360  | 0  |     if ((tmod = OPENSSL_zalloc(sizeof(*tmod))) == NULL)  | 
361  | 0  |         goto err;  | 
362  |  |  | 
363  | 0  |     tmod->dso = dso;  | 
364  | 0  |     tmod->name = OPENSSL_strdup(name);  | 
365  | 0  |     tmod->init = ifunc;  | 
366  | 0  |     tmod->finish = ffunc;  | 
367  | 0  |     if (tmod->name == NULL)  | 
368  | 0  |         goto err;  | 
369  |  |  | 
370  | 0  |     if (!sk_CONF_MODULE_push(new_modules, tmod))  | 
371  | 0  |         goto err;  | 
372  |  |  | 
373  | 0  |     ossl_rcu_assign_ptr(&supported_modules, &new_modules);  | 
374  | 0  |     ossl_rcu_write_unlock(module_list_lock);  | 
375  | 0  |     ossl_synchronize_rcu(module_list_lock);  | 
376  |  | 
  | 
377  | 0  |     sk_CONF_MODULE_free(old_modules);  | 
378  | 0  |     return tmod;  | 
379  |  |  | 
380  | 0  |  err:  | 
381  | 0  |     ossl_rcu_write_unlock(module_list_lock);  | 
382  | 0  |     if (tmod != NULL) { | 
383  | 0  |         OPENSSL_free(tmod->name);  | 
384  | 0  |         OPENSSL_free(tmod);  | 
385  | 0  |     }  | 
386  | 0  |     sk_CONF_MODULE_free(new_modules);  | 
387  | 0  |     return NULL;  | 
388  | 0  | }  | 
389  |  |  | 
390  |  | /*  | 
391  |  |  * Find a module from the list. We allow module names of the form  | 
392  |  |  * modname.XXXX to just search for modname to allow the same module to be  | 
393  |  |  * initialized more than once.  | 
394  |  |  */  | 
395  |  |  | 
396  |  | static CONF_MODULE *module_find(const char *name)  | 
397  | 0  | { | 
398  | 0  |     CONF_MODULE *tmod;  | 
399  | 0  |     int i, nchar;  | 
400  | 0  |     char *p;  | 
401  | 0  |     STACK_OF(CONF_MODULE) *mods;  | 
402  |  | 
  | 
403  | 0  |     p = strrchr(name, '.');  | 
404  |  | 
  | 
405  | 0  |     if (p)  | 
406  | 0  |         nchar = p - name;  | 
407  | 0  |     else  | 
408  | 0  |         nchar = strlen(name);  | 
409  |  | 
  | 
410  | 0  |     if (!RUN_ONCE(&init_module_list_lock, do_init_module_list_lock))  | 
411  | 0  |         return NULL;  | 
412  |  |  | 
413  | 0  |     ossl_rcu_read_lock(module_list_lock);  | 
414  | 0  |     mods = ossl_rcu_deref(&supported_modules);  | 
415  |  | 
  | 
416  | 0  |     for (i = 0; i < sk_CONF_MODULE_num(mods); i++) { | 
417  | 0  |         tmod = sk_CONF_MODULE_value(mods, i);  | 
418  | 0  |         if (strncmp(tmod->name, name, nchar) == 0) { | 
419  | 0  |             ossl_rcu_read_unlock(module_list_lock);  | 
420  | 0  |             return tmod;  | 
421  | 0  |         }  | 
422  | 0  |     }  | 
423  |  |  | 
424  | 0  |     ossl_rcu_read_unlock(module_list_lock);  | 
425  | 0  |     return NULL;  | 
426  | 0  | }  | 
427  |  |  | 
428  |  | /* initialize a module */  | 
429  |  | static int module_init(CONF_MODULE *pmod, const char *name, const char *value,  | 
430  |  |                        const CONF *cnf)  | 
431  | 0  | { | 
432  | 0  |     int ret = 1;  | 
433  | 0  |     int init_called = 0;  | 
434  | 0  |     CONF_IMODULE *imod = NULL;  | 
435  | 0  |     STACK_OF(CONF_IMODULE) *old_modules;  | 
436  | 0  |     STACK_OF(CONF_IMODULE) *new_modules;  | 
437  |  |  | 
438  |  |     /* Otherwise add initialized module to list */  | 
439  | 0  |     imod = OPENSSL_malloc(sizeof(*imod));  | 
440  | 0  |     if (imod == NULL)  | 
441  | 0  |         goto err;  | 
442  |  |  | 
443  | 0  |     imod->pmod = pmod;  | 
444  | 0  |     imod->name = OPENSSL_strdup(name);  | 
445  | 0  |     imod->value = OPENSSL_strdup(value);  | 
446  | 0  |     imod->usr_data = NULL;  | 
447  |  | 
  | 
448  | 0  |     if (!imod->name || !imod->value)  | 
449  | 0  |         goto memerr;  | 
450  |  |  | 
451  |  |     /* Try to initialize module */  | 
452  | 0  |     if (pmod->init) { | 
453  | 0  |         ret = pmod->init(imod, cnf);  | 
454  | 0  |         init_called = 1;  | 
455  |  |         /* Error occurred, exit */  | 
456  | 0  |         if (ret <= 0)  | 
457  | 0  |             goto err;  | 
458  | 0  |     }  | 
459  |  |  | 
460  | 0  |     if (!RUN_ONCE(&init_module_list_lock, do_init_module_list_lock))  | 
461  | 0  |         goto err;  | 
462  |  |  | 
463  | 0  |     ossl_rcu_write_lock(module_list_lock);  | 
464  |  | 
  | 
465  | 0  |     old_modules = ossl_rcu_deref(&initialized_modules);  | 
466  |  | 
  | 
467  | 0  |     if (old_modules == NULL)  | 
468  | 0  |         new_modules = sk_CONF_IMODULE_new_null();  | 
469  | 0  |     else  | 
470  | 0  |         new_modules = sk_CONF_IMODULE_dup(old_modules);  | 
471  |  | 
  | 
472  | 0  |     if (new_modules == NULL) { | 
473  | 0  |         ossl_rcu_write_unlock(module_list_lock);  | 
474  | 0  |         ERR_raise(ERR_LIB_CONF, ERR_R_CRYPTO_LIB);  | 
475  | 0  |         goto err;  | 
476  | 0  |     }  | 
477  |  |  | 
478  | 0  |     if (!sk_CONF_IMODULE_push(new_modules, imod)) { | 
479  | 0  |         ossl_rcu_write_unlock(module_list_lock);  | 
480  | 0  |         sk_CONF_IMODULE_free(new_modules);  | 
481  | 0  |         ERR_raise(ERR_LIB_CONF, ERR_R_CRYPTO_LIB);  | 
482  | 0  |         goto err;  | 
483  | 0  |     }  | 
484  |  |  | 
485  | 0  |     pmod->links++;  | 
486  |  | 
  | 
487  | 0  |     ossl_rcu_assign_ptr(&initialized_modules, &new_modules);  | 
488  | 0  |     ossl_rcu_write_unlock(module_list_lock);  | 
489  | 0  |     ossl_synchronize_rcu(module_list_lock);  | 
490  | 0  |     sk_CONF_IMODULE_free(old_modules);  | 
491  | 0  |     return ret;  | 
492  |  |  | 
493  | 0  |  err:  | 
494  |  |  | 
495  |  |     /* We've started the module so we'd better finish it */  | 
496  | 0  |     if (pmod->finish && init_called)  | 
497  | 0  |         pmod->finish(imod);  | 
498  |  | 
  | 
499  | 0  |  memerr:  | 
500  | 0  |     if (imod) { | 
501  | 0  |         OPENSSL_free(imod->name);  | 
502  | 0  |         OPENSSL_free(imod->value);  | 
503  | 0  |         OPENSSL_free(imod);  | 
504  | 0  |     }  | 
505  |  | 
  | 
506  | 0  |     return -1;  | 
507  |  | 
  | 
508  | 0  | }  | 
509  |  |  | 
510  |  | /*  | 
511  |  |  * Unload any dynamic modules that have a link count of zero: i.e. have no  | 
512  |  |  * active initialized modules. If 'all' is set then all modules are unloaded  | 
513  |  |  * including static ones.  | 
514  |  |  */  | 
515  |  |  | 
516  |  | void CONF_modules_unload(int all)  | 
517  | 2  | { | 
518  | 2  |     int i;  | 
519  | 2  |     CONF_MODULE *md;  | 
520  | 2  |     STACK_OF(CONF_MODULE) *old_modules;  | 
521  | 2  |     STACK_OF(CONF_MODULE) *new_modules;  | 
522  | 2  |     STACK_OF(CONF_MODULE) *to_delete;  | 
523  |  |  | 
524  | 2  |     if (!conf_modules_finish_int()) /* also inits module list lock */  | 
525  | 0  |         return;  | 
526  |  |  | 
527  | 2  |     ossl_rcu_write_lock(module_list_lock);  | 
528  |  |  | 
529  | 2  |     old_modules = ossl_rcu_deref(&supported_modules);  | 
530  | 2  |     new_modules = sk_CONF_MODULE_dup(old_modules);  | 
531  |  |  | 
532  | 2  |     if (new_modules == NULL) { | 
533  | 0  |         ossl_rcu_write_unlock(module_list_lock);  | 
534  | 0  |         return;  | 
535  | 0  |     }  | 
536  |  |  | 
537  | 2  |     to_delete = sk_CONF_MODULE_new_null();  | 
538  |  |  | 
539  |  |     /* unload modules in reverse order */  | 
540  | 2  |     for (i = sk_CONF_MODULE_num(new_modules) - 1; i >= 0; i--) { | 
541  | 0  |         md = sk_CONF_MODULE_value(new_modules, i);  | 
542  |  |         /* If static or in use and 'all' not set ignore it */  | 
543  | 0  |         if (((md->links > 0) || !md->dso) && !all)  | 
544  | 0  |             continue;  | 
545  |  |         /* Since we're working in reverse this is OK */  | 
546  | 0  |         (void)sk_CONF_MODULE_delete(new_modules, i);  | 
547  | 0  |         sk_CONF_MODULE_push(to_delete, md);  | 
548  | 0  |     }  | 
549  |  |  | 
550  | 2  |     if (sk_CONF_MODULE_num(new_modules) == 0) { | 
551  | 2  |         sk_CONF_MODULE_free(new_modules);  | 
552  | 2  |         new_modules = NULL;  | 
553  | 2  |     }  | 
554  |  |  | 
555  | 2  |     ossl_rcu_assign_ptr(&supported_modules, &new_modules);  | 
556  | 2  |     ossl_rcu_write_unlock(module_list_lock);  | 
557  | 2  |     ossl_synchronize_rcu(module_list_lock);  | 
558  | 2  |     sk_CONF_MODULE_free(old_modules);  | 
559  | 2  |     sk_CONF_MODULE_pop_free(to_delete, module_free);  | 
560  |  |  | 
561  | 2  | }  | 
562  |  |  | 
563  |  | /* unload a single module */  | 
564  |  | static void module_free(CONF_MODULE *md)  | 
565  | 0  | { | 
566  | 0  |     DSO_free(md->dso);  | 
567  | 0  |     OPENSSL_free(md->name);  | 
568  | 0  |     OPENSSL_free(md);  | 
569  | 0  | }  | 
570  |  |  | 
571  |  | /* finish and free up all modules instances */  | 
572  |  |  | 
573  |  | static int conf_modules_finish_int(void)  | 
574  | 2  | { | 
575  | 2  |     CONF_IMODULE *imod;  | 
576  | 2  |     STACK_OF(CONF_IMODULE) *old_modules;  | 
577  | 2  |     STACK_OF(CONF_IMODULE) *new_modules = NULL;  | 
578  |  |  | 
579  | 2  |     if (!RUN_ONCE(&init_module_list_lock, do_init_module_list_lock))  | 
580  | 0  |         return 0;  | 
581  |  |  | 
582  |  |     /* If module_list_lock is NULL here it means we were already unloaded */  | 
583  | 2  |     if (module_list_lock == NULL)  | 
584  | 0  |         return 0;  | 
585  |  |  | 
586  | 2  |     ossl_rcu_write_lock(module_list_lock);  | 
587  | 2  |     old_modules = ossl_rcu_deref(&initialized_modules);  | 
588  | 2  |     ossl_rcu_assign_ptr(&initialized_modules, &new_modules);  | 
589  | 2  |     ossl_rcu_write_unlock(module_list_lock);  | 
590  | 2  |     ossl_synchronize_rcu(module_list_lock);  | 
591  |  |  | 
592  | 2  |     while (sk_CONF_IMODULE_num(old_modules) > 0) { | 
593  | 0  |         imod = sk_CONF_IMODULE_pop(old_modules);  | 
594  | 0  |         module_finish(imod);  | 
595  | 0  |     }  | 
596  | 2  |     sk_CONF_IMODULE_free(old_modules);  | 
597  |  |  | 
598  | 2  |     return 1;  | 
599  | 2  | }  | 
600  |  |  | 
601  |  | void CONF_modules_finish(void)  | 
602  | 0  | { | 
603  | 0  |     conf_modules_finish_int();  | 
604  | 0  | }  | 
605  |  |  | 
606  |  | /* finish a module instance */  | 
607  |  |  | 
608  |  | static void module_finish(CONF_IMODULE *imod)  | 
609  | 0  | { | 
610  | 0  |     if (!imod)  | 
611  | 0  |         return;  | 
612  | 0  |     if (imod->pmod->finish)  | 
613  | 0  |         imod->pmod->finish(imod);  | 
614  | 0  |     imod->pmod->links--;  | 
615  | 0  |     OPENSSL_free(imod->name);  | 
616  | 0  |     OPENSSL_free(imod->value);  | 
617  | 0  |     OPENSSL_free(imod);  | 
618  | 0  | }  | 
619  |  |  | 
620  |  | /* Add a static module to OpenSSL */  | 
621  |  |  | 
622  |  | int CONF_module_add(const char *name, conf_init_func *ifunc,  | 
623  |  |                     conf_finish_func *ffunc)  | 
624  | 0  | { | 
625  | 0  |     if (module_add(NULL, name, ifunc, ffunc))  | 
626  | 0  |         return 1;  | 
627  | 0  |     else  | 
628  | 0  |         return 0;  | 
629  | 0  | }  | 
630  |  |  | 
631  |  | void ossl_config_modules_free(void)  | 
632  | 2  | { | 
633  | 2  |     CONF_modules_unload(1); /* calls CONF_modules_finish */  | 
634  | 2  |     module_lists_free();  | 
635  | 2  | }  | 
636  |  |  | 
637  |  | /* Utility functions */  | 
638  |  |  | 
639  |  | const char *CONF_imodule_get_name(const CONF_IMODULE *md)  | 
640  | 0  | { | 
641  | 0  |     return md->name;  | 
642  | 0  | }  | 
643  |  |  | 
644  |  | const char *CONF_imodule_get_value(const CONF_IMODULE *md)  | 
645  | 0  | { | 
646  | 0  |     return md->value;  | 
647  | 0  | }  | 
648  |  |  | 
649  |  | void *CONF_imodule_get_usr_data(const CONF_IMODULE *md)  | 
650  | 0  | { | 
651  | 0  |     return md->usr_data;  | 
652  | 0  | }  | 
653  |  |  | 
654  |  | void CONF_imodule_set_usr_data(CONF_IMODULE *md, void *usr_data)  | 
655  | 0  | { | 
656  | 0  |     md->usr_data = usr_data;  | 
657  | 0  | }  | 
658  |  |  | 
659  |  | CONF_MODULE *CONF_imodule_get_module(const CONF_IMODULE *md)  | 
660  | 0  | { | 
661  | 0  |     return md->pmod;  | 
662  | 0  | }  | 
663  |  |  | 
664  |  | unsigned long CONF_imodule_get_flags(const CONF_IMODULE *md)  | 
665  | 0  | { | 
666  | 0  |     return md->flags;  | 
667  | 0  | }  | 
668  |  |  | 
669  |  | void CONF_imodule_set_flags(CONF_IMODULE *md, unsigned long flags)  | 
670  | 0  | { | 
671  | 0  |     md->flags = flags;  | 
672  | 0  | }  | 
673  |  |  | 
674  |  | void *CONF_module_get_usr_data(CONF_MODULE *pmod)  | 
675  | 0  | { | 
676  | 0  |     return pmod->usr_data;  | 
677  | 0  | }  | 
678  |  |  | 
679  |  | void CONF_module_set_usr_data(CONF_MODULE *pmod, void *usr_data)  | 
680  | 0  | { | 
681  | 0  |     pmod->usr_data = usr_data;  | 
682  | 0  | }  | 
683  |  |  | 
684  |  | /* Return default config file name */  | 
685  |  | char *CONF_get1_default_config_file(void)  | 
686  | 0  | { | 
687  | 0  |     const char *t;  | 
688  | 0  |     char *file, *sep = "";  | 
689  | 0  |     size_t size;  | 
690  |  | 
  | 
691  | 0  |     if ((file = ossl_safe_getenv("OPENSSL_CONF")) != NULL) | 
692  | 0  |         return OPENSSL_strdup(file);  | 
693  |  |  | 
694  | 0  |     t = X509_get_default_cert_area();  | 
695  |  |     /*  | 
696  |  |      * On windows systems with -DOSSL_WINCTX set, if the needed registry  | 
697  |  |      * keys are not yet set, openssl applets will return, due to an inability  | 
698  |  |      * to locate various directories, like the default cert area.  In that  | 
699  |  |      * event, clone an empty string here, so that commands like openssl version  | 
700  |  |      * continue to operate properly without needing to set OPENSSL_CONF.  | 
701  |  |      * Applets like cms will fail gracefully later when they try to parse an  | 
702  |  |      * empty config file  | 
703  |  |      */  | 
704  | 0  |     if (t == NULL)  | 
705  | 0  |         return OPENSSL_strdup(""); | 
706  |  |  | 
707  | 0  | #ifndef OPENSSL_SYS_VMS  | 
708  | 0  |     sep = "/";  | 
709  | 0  | #endif  | 
710  | 0  |     size = strlen(t) + strlen(sep) + strlen(OPENSSL_CONF) + 1;  | 
711  | 0  |     file = OPENSSL_malloc(size);  | 
712  |  | 
  | 
713  | 0  |     if (file == NULL)  | 
714  | 0  |         return NULL;  | 
715  | 0  |     BIO_snprintf(file, size, "%s%s%s", t, sep, OPENSSL_CONF);  | 
716  |  | 
  | 
717  | 0  |     return file;  | 
718  | 0  | }  | 
719  |  |  | 
720  |  | /*  | 
721  |  |  * This function takes a list separated by 'sep' and calls the callback  | 
722  |  |  * function giving the start and length of each member optionally stripping  | 
723  |  |  * leading and trailing whitespace. This can be used to parse comma separated  | 
724  |  |  * lists for example.  | 
725  |  |  */  | 
726  |  |  | 
727  |  | int CONF_parse_list(const char *list_, int sep, int nospc,  | 
728  |  |                     int (*list_cb) (const char *elem, int len, void *usr),  | 
729  |  |                     void *arg)  | 
730  | 0  | { | 
731  | 0  |     int ret;  | 
732  | 0  |     const char *lstart, *tmpend, *p;  | 
733  |  | 
  | 
734  | 0  |     if (list_ == NULL) { | 
735  | 0  |         ERR_raise(ERR_LIB_CONF, CONF_R_LIST_CANNOT_BE_NULL);  | 
736  | 0  |         return 0;  | 
737  | 0  |     }  | 
738  |  |  | 
739  | 0  |     lstart = list_;  | 
740  | 0  |     for (;;) { | 
741  | 0  |         if (nospc) { | 
742  | 0  |             while (*lstart && isspace((unsigned char)*lstart))  | 
743  | 0  |                 lstart++;  | 
744  | 0  |         }  | 
745  | 0  |         p = strchr(lstart, sep);  | 
746  | 0  |         if (p == lstart || *lstart == '\0')  | 
747  | 0  |             ret = list_cb(NULL, 0, arg);  | 
748  | 0  |         else { | 
749  | 0  |             if (p)  | 
750  | 0  |                 tmpend = p - 1;  | 
751  | 0  |             else  | 
752  | 0  |                 tmpend = lstart + strlen(lstart) - 1;  | 
753  | 0  |             if (nospc) { | 
754  | 0  |                 while (isspace((unsigned char)*tmpend))  | 
755  | 0  |                     tmpend--;  | 
756  | 0  |             }  | 
757  | 0  |             ret = list_cb(lstart, tmpend - lstart + 1, arg);  | 
758  | 0  |         }  | 
759  | 0  |         if (ret <= 0)  | 
760  | 0  |             return ret;  | 
761  | 0  |         if (p == NULL)  | 
762  | 0  |             return 1;  | 
763  | 0  |         lstart = p + 1;  | 
764  | 0  |     }  | 
765  | 0  | }  |