conf_mod.c 17 KB

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