conf_mod.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  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, NULL);
  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. if (tmod != NULL) {
  305. OPENSSL_free(tmod->name);
  306. OPENSSL_free(tmod);
  307. }
  308. sk_CONF_MODULE_free(new_modules);
  309. return NULL;
  310. }
  311. /*
  312. * Find a module from the list. We allow module names of the form
  313. * modname.XXXX to just search for modname to allow the same module to be
  314. * initialized more than once.
  315. */
  316. static CONF_MODULE *module_find(const char *name)
  317. {
  318. CONF_MODULE *tmod;
  319. int i, nchar;
  320. char *p;
  321. STACK_OF(CONF_MODULE) *mods;
  322. p = strrchr(name, '.');
  323. if (p)
  324. nchar = p - name;
  325. else
  326. nchar = strlen(name);
  327. if (!RUN_ONCE(&init_module_list_lock, do_init_module_list_lock))
  328. return NULL;
  329. ossl_rcu_read_lock(module_list_lock);
  330. mods = ossl_rcu_deref(&supported_modules);
  331. for (i = 0; i < sk_CONF_MODULE_num(mods); i++) {
  332. tmod = sk_CONF_MODULE_value(mods, i);
  333. if (strncmp(tmod->name, name, nchar) == 0) {
  334. ossl_rcu_read_unlock(module_list_lock);
  335. return tmod;
  336. }
  337. }
  338. ossl_rcu_read_unlock(module_list_lock);
  339. return NULL;
  340. }
  341. /* initialize a module */
  342. static int module_init(CONF_MODULE *pmod, const char *name, const char *value,
  343. const CONF *cnf)
  344. {
  345. int ret = 1;
  346. int init_called = 0;
  347. CONF_IMODULE *imod = NULL;
  348. STACK_OF(CONF_IMODULE) *old_modules;
  349. STACK_OF(CONF_IMODULE) *new_modules;
  350. /* Otherwise add initialized module to list */
  351. imod = OPENSSL_malloc(sizeof(*imod));
  352. if (imod == NULL)
  353. goto err;
  354. imod->pmod = pmod;
  355. imod->name = OPENSSL_strdup(name);
  356. imod->value = OPENSSL_strdup(value);
  357. imod->usr_data = NULL;
  358. if (!imod->name || !imod->value)
  359. goto memerr;
  360. /* Try to initialize module */
  361. if (pmod->init) {
  362. ret = pmod->init(imod, cnf);
  363. init_called = 1;
  364. /* Error occurred, exit */
  365. if (ret <= 0)
  366. goto err;
  367. }
  368. if (!RUN_ONCE(&init_module_list_lock, do_init_module_list_lock))
  369. goto err;
  370. ossl_rcu_write_lock(module_list_lock);
  371. old_modules = ossl_rcu_deref(&initialized_modules);
  372. if (old_modules == NULL)
  373. new_modules = sk_CONF_IMODULE_new_null();
  374. else
  375. new_modules = sk_CONF_IMODULE_dup(old_modules);
  376. if (new_modules == NULL) {
  377. ossl_rcu_write_unlock(module_list_lock);
  378. ERR_raise(ERR_LIB_CONF, ERR_R_CRYPTO_LIB);
  379. goto err;
  380. }
  381. if (!sk_CONF_IMODULE_push(new_modules, imod)) {
  382. ossl_rcu_write_unlock(module_list_lock);
  383. ERR_raise(ERR_LIB_CONF, ERR_R_CRYPTO_LIB);
  384. goto err;
  385. }
  386. pmod->links++;
  387. ossl_rcu_assign_ptr(&initialized_modules, &new_modules);
  388. ossl_rcu_write_unlock(module_list_lock);
  389. ossl_synchronize_rcu(module_list_lock);
  390. sk_CONF_IMODULE_free(old_modules);
  391. return ret;
  392. err:
  393. /* We've started the module so we'd better finish it */
  394. if (pmod->finish && init_called)
  395. pmod->finish(imod);
  396. memerr:
  397. if (imod) {
  398. OPENSSL_free(imod->name);
  399. OPENSSL_free(imod->value);
  400. OPENSSL_free(imod);
  401. }
  402. return -1;
  403. }
  404. /*
  405. * Unload any dynamic modules that have a link count of zero: i.e. have no
  406. * active initialized modules. If 'all' is set then all modules are unloaded
  407. * including static ones.
  408. */
  409. void CONF_modules_unload(int all)
  410. {
  411. int i;
  412. CONF_MODULE *md;
  413. STACK_OF(CONF_MODULE) *old_modules;
  414. STACK_OF(CONF_MODULE) *new_modules;
  415. STACK_OF(CONF_MODULE) *to_delete;
  416. if (!conf_modules_finish_int()) /* also inits module list lock */
  417. return;
  418. ossl_rcu_write_lock(module_list_lock);
  419. old_modules = ossl_rcu_deref(&supported_modules);
  420. new_modules = sk_CONF_MODULE_dup(old_modules);
  421. to_delete = sk_CONF_MODULE_new_null();
  422. if (new_modules == NULL) {
  423. ossl_rcu_write_unlock(module_list_lock);
  424. return;
  425. }
  426. /* unload modules in reverse order */
  427. for (i = sk_CONF_MODULE_num(new_modules) - 1; i >= 0; i--) {
  428. md = sk_CONF_MODULE_value(new_modules, i);
  429. /* If static or in use and 'all' not set ignore it */
  430. if (((md->links > 0) || !md->dso) && !all)
  431. continue;
  432. /* Since we're working in reverse this is OK */
  433. (void)sk_CONF_MODULE_delete(new_modules, i);
  434. sk_CONF_MODULE_push(to_delete, md);
  435. }
  436. if (sk_CONF_MODULE_num(new_modules) == 0) {
  437. sk_CONF_MODULE_free(new_modules);
  438. new_modules = NULL;
  439. }
  440. ossl_rcu_assign_ptr(&supported_modules, &new_modules);
  441. ossl_rcu_write_unlock(module_list_lock);
  442. ossl_synchronize_rcu(module_list_lock);
  443. sk_CONF_MODULE_free(old_modules);
  444. sk_CONF_MODULE_pop_free(to_delete, module_free);
  445. }
  446. /* unload a single module */
  447. static void module_free(CONF_MODULE *md)
  448. {
  449. DSO_free(md->dso);
  450. OPENSSL_free(md->name);
  451. OPENSSL_free(md);
  452. }
  453. /* finish and free up all modules instances */
  454. static int conf_modules_finish_int(void)
  455. {
  456. CONF_IMODULE *imod;
  457. STACK_OF(CONF_IMODULE) *old_modules;
  458. STACK_OF(CONF_IMODULE) *new_modules = NULL;
  459. if (!RUN_ONCE(&init_module_list_lock, do_init_module_list_lock))
  460. return 0;
  461. /* If module_list_lock is NULL here it means we were already unloaded */
  462. if (module_list_lock == NULL)
  463. return 0;
  464. ossl_rcu_write_lock(module_list_lock);
  465. old_modules = ossl_rcu_deref(&initialized_modules);
  466. ossl_rcu_assign_ptr(&initialized_modules, &new_modules);
  467. ossl_rcu_write_unlock(module_list_lock);
  468. ossl_synchronize_rcu(module_list_lock);
  469. while (sk_CONF_IMODULE_num(old_modules) > 0) {
  470. imod = sk_CONF_IMODULE_pop(old_modules);
  471. module_finish(imod);
  472. }
  473. sk_CONF_IMODULE_free(old_modules);
  474. return 1;
  475. }
  476. void CONF_modules_finish(void)
  477. {
  478. conf_modules_finish_int();
  479. }
  480. /* finish a module instance */
  481. static void module_finish(CONF_IMODULE *imod)
  482. {
  483. if (!imod)
  484. return;
  485. if (imod->pmod->finish)
  486. imod->pmod->finish(imod);
  487. imod->pmod->links--;
  488. OPENSSL_free(imod->name);
  489. OPENSSL_free(imod->value);
  490. OPENSSL_free(imod);
  491. }
  492. /* Add a static module to OpenSSL */
  493. int CONF_module_add(const char *name, conf_init_func *ifunc,
  494. conf_finish_func *ffunc)
  495. {
  496. if (module_add(NULL, name, ifunc, ffunc))
  497. return 1;
  498. else
  499. return 0;
  500. }
  501. void ossl_config_modules_free(void)
  502. {
  503. CONF_modules_unload(1); /* calls CONF_modules_finish */
  504. module_lists_free();
  505. }
  506. /* Utility functions */
  507. const char *CONF_imodule_get_name(const CONF_IMODULE *md)
  508. {
  509. return md->name;
  510. }
  511. const char *CONF_imodule_get_value(const CONF_IMODULE *md)
  512. {
  513. return md->value;
  514. }
  515. void *CONF_imodule_get_usr_data(const CONF_IMODULE *md)
  516. {
  517. return md->usr_data;
  518. }
  519. void CONF_imodule_set_usr_data(CONF_IMODULE *md, void *usr_data)
  520. {
  521. md->usr_data = usr_data;
  522. }
  523. CONF_MODULE *CONF_imodule_get_module(const CONF_IMODULE *md)
  524. {
  525. return md->pmod;
  526. }
  527. unsigned long CONF_imodule_get_flags(const CONF_IMODULE *md)
  528. {
  529. return md->flags;
  530. }
  531. void CONF_imodule_set_flags(CONF_IMODULE *md, unsigned long flags)
  532. {
  533. md->flags = flags;
  534. }
  535. void *CONF_module_get_usr_data(CONF_MODULE *pmod)
  536. {
  537. return pmod->usr_data;
  538. }
  539. void CONF_module_set_usr_data(CONF_MODULE *pmod, void *usr_data)
  540. {
  541. pmod->usr_data = usr_data;
  542. }
  543. /* Return default config file name */
  544. char *CONF_get1_default_config_file(void)
  545. {
  546. const char *t;
  547. char *file, *sep = "";
  548. size_t size;
  549. if ((file = ossl_safe_getenv("OPENSSL_CONF")) != NULL)
  550. return OPENSSL_strdup(file);
  551. t = X509_get_default_cert_area();
  552. #ifndef OPENSSL_SYS_VMS
  553. sep = "/";
  554. #endif
  555. size = strlen(t) + strlen(sep) + strlen(OPENSSL_CONF) + 1;
  556. file = OPENSSL_malloc(size);
  557. if (file == NULL)
  558. return NULL;
  559. BIO_snprintf(file, size, "%s%s%s", t, sep, OPENSSL_CONF);
  560. return file;
  561. }
  562. /*
  563. * This function takes a list separated by 'sep' and calls the callback
  564. * function giving the start and length of each member optionally stripping
  565. * leading and trailing whitespace. This can be used to parse comma separated
  566. * lists for example.
  567. */
  568. int CONF_parse_list(const char *list_, int sep, int nospc,
  569. int (*list_cb) (const char *elem, int len, void *usr),
  570. void *arg)
  571. {
  572. int ret;
  573. const char *lstart, *tmpend, *p;
  574. if (list_ == NULL) {
  575. ERR_raise(ERR_LIB_CONF, CONF_R_LIST_CANNOT_BE_NULL);
  576. return 0;
  577. }
  578. lstart = list_;
  579. for (;;) {
  580. if (nospc) {
  581. while (*lstart && isspace((unsigned char)*lstart))
  582. lstart++;
  583. }
  584. p = strchr(lstart, sep);
  585. if (p == lstart || *lstart == '\0')
  586. ret = list_cb(NULL, 0, arg);
  587. else {
  588. if (p)
  589. tmpend = p - 1;
  590. else
  591. tmpend = lstart + strlen(lstart) - 1;
  592. if (nospc) {
  593. while (isspace((unsigned char)*tmpend))
  594. tmpend--;
  595. }
  596. ret = list_cb(lstart, tmpend - lstart + 1, arg);
  597. }
  598. if (ret <= 0)
  599. return ret;
  600. if (p == NULL)
  601. return 1;
  602. lstart = p + 1;
  603. }
  604. }