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