conf_mod.c 15 KB

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