conf_mod.c 19 KB

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