conf_mod.c 15 KB

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