conf_mod.c 20 KB

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