conf_mod.c 20 KB

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