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