conf_mod.c 17 KB

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