conf_mod.c 19 KB

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