provider_conf.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*
  2. * Copyright 2019-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. #include <string.h>
  10. #include <openssl/trace.h>
  11. #include <openssl/err.h>
  12. #include <openssl/conf.h>
  13. #include <openssl/safestack.h>
  14. #include <openssl/provider.h>
  15. #include "internal/provider.h"
  16. #include "internal/cryptlib.h"
  17. #include "provider_local.h"
  18. #include "crypto/context.h"
  19. DEFINE_STACK_OF(OSSL_PROVIDER)
  20. /* PROVIDER config module */
  21. typedef struct {
  22. CRYPTO_RWLOCK *lock;
  23. STACK_OF(OSSL_PROVIDER) *activated_providers;
  24. } PROVIDER_CONF_GLOBAL;
  25. void *ossl_prov_conf_ctx_new(OSSL_LIB_CTX *libctx)
  26. {
  27. PROVIDER_CONF_GLOBAL *pcgbl = OPENSSL_zalloc(sizeof(*pcgbl));
  28. if (pcgbl == NULL)
  29. return NULL;
  30. pcgbl->lock = CRYPTO_THREAD_lock_new();
  31. if (pcgbl->lock == NULL) {
  32. OPENSSL_free(pcgbl);
  33. return NULL;
  34. }
  35. return pcgbl;
  36. }
  37. void ossl_prov_conf_ctx_free(void *vpcgbl)
  38. {
  39. PROVIDER_CONF_GLOBAL *pcgbl = vpcgbl;
  40. sk_OSSL_PROVIDER_pop_free(pcgbl->activated_providers,
  41. ossl_provider_free);
  42. OSSL_TRACE(CONF, "Cleaned up providers\n");
  43. CRYPTO_THREAD_lock_free(pcgbl->lock);
  44. OPENSSL_free(pcgbl);
  45. }
  46. static const char *skip_dot(const char *name)
  47. {
  48. const char *p = strchr(name, '.');
  49. if (p != NULL)
  50. return p + 1;
  51. return name;
  52. }
  53. /*
  54. * Parse the provider params section
  55. * Returns:
  56. * 1 for success
  57. * 0 for non-fatal errors
  58. * < 0 for fatal errors
  59. */
  60. static int provider_conf_params_internal(OSSL_PROVIDER *prov,
  61. OSSL_PROVIDER_INFO *provinfo,
  62. const char *name, const char *value,
  63. const CONF *cnf,
  64. STACK_OF(OPENSSL_CSTRING) *visited)
  65. {
  66. STACK_OF(CONF_VALUE) *sect;
  67. int ok = 1;
  68. int rc = 0;
  69. sect = NCONF_get_section(cnf, value);
  70. if (sect != NULL) {
  71. int i;
  72. char buffer[512];
  73. size_t buffer_len = 0;
  74. OSSL_TRACE1(CONF, "Provider params: start section %s\n", value);
  75. /*
  76. * Check to see if the provided section value has already
  77. * been visited. If it has, then we have a recursive lookup
  78. * in the configuration which isn't valid. As such we should error
  79. * out
  80. */
  81. for (i = 0; i < sk_OPENSSL_CSTRING_num(visited); i++) {
  82. if (sk_OPENSSL_CSTRING_value(visited, i) == value) {
  83. ERR_raise(ERR_LIB_CONF, CONF_R_RECURSIVE_SECTION_REFERENCE);
  84. return -1;
  85. }
  86. }
  87. /*
  88. * We've not visited this node yet, so record it on the stack
  89. */
  90. if (!sk_OPENSSL_CSTRING_push(visited, value))
  91. return -1;
  92. if (name != NULL) {
  93. OPENSSL_strlcpy(buffer, name, sizeof(buffer));
  94. OPENSSL_strlcat(buffer, ".", sizeof(buffer));
  95. buffer_len = strlen(buffer);
  96. }
  97. for (i = 0; i < sk_CONF_VALUE_num(sect); i++) {
  98. CONF_VALUE *sectconf = sk_CONF_VALUE_value(sect, i);
  99. if (buffer_len + strlen(sectconf->name) >= sizeof(buffer)) {
  100. sk_OPENSSL_CSTRING_pop(visited);
  101. return -1;
  102. }
  103. buffer[buffer_len] = '\0';
  104. OPENSSL_strlcat(buffer, sectconf->name, sizeof(buffer));
  105. rc = provider_conf_params_internal(prov, provinfo, buffer,
  106. sectconf->value, cnf, visited);
  107. if (rc < 0) {
  108. sk_OPENSSL_CSTRING_pop(visited);
  109. return rc;
  110. }
  111. }
  112. sk_OPENSSL_CSTRING_pop(visited);
  113. OSSL_TRACE1(CONF, "Provider params: finish section %s\n", value);
  114. } else {
  115. OSSL_TRACE2(CONF, "Provider params: %s = %s\n", name, value);
  116. if (prov != NULL)
  117. ok = ossl_provider_add_parameter(prov, name, value);
  118. else
  119. ok = ossl_provider_info_add_parameter(provinfo, name, value);
  120. }
  121. return ok;
  122. }
  123. /*
  124. * recursively parse the provider configuration section
  125. * of the config file.
  126. * Returns
  127. * 1 on success
  128. * 0 on non-fatal error
  129. * < 0 on fatal errors
  130. */
  131. static int provider_conf_params(OSSL_PROVIDER *prov,
  132. OSSL_PROVIDER_INFO *provinfo,
  133. const char *name, const char *value,
  134. const CONF *cnf)
  135. {
  136. int rc;
  137. STACK_OF(OPENSSL_CSTRING) *visited = sk_OPENSSL_CSTRING_new_null();
  138. if (visited == NULL)
  139. return -1;
  140. rc = provider_conf_params_internal(prov, provinfo, name,
  141. value, cnf, visited);
  142. sk_OPENSSL_CSTRING_free(visited);
  143. return rc;
  144. }
  145. static int prov_already_activated(const char *name,
  146. STACK_OF(OSSL_PROVIDER) *activated)
  147. {
  148. int i, max;
  149. if (activated == NULL)
  150. return 0;
  151. max = sk_OSSL_PROVIDER_num(activated);
  152. for (i = 0; i < max; i++) {
  153. OSSL_PROVIDER *tstprov = sk_OSSL_PROVIDER_value(activated, i);
  154. if (strcmp(OSSL_PROVIDER_get0_name(tstprov), name) == 0) {
  155. return 1;
  156. }
  157. }
  158. return 0;
  159. }
  160. /*
  161. * Attempt to activate a provider
  162. * Returns:
  163. * 1 on successful activation
  164. * 0 on failed activation for non-fatal error
  165. * < 0 on failed activation for fatal errors
  166. */
  167. static int provider_conf_activate(OSSL_LIB_CTX *libctx, const char *name,
  168. const char *value, const char *path,
  169. int soft, const CONF *cnf)
  170. {
  171. PROVIDER_CONF_GLOBAL *pcgbl
  172. = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_PROVIDER_CONF_INDEX);
  173. OSSL_PROVIDER *prov = NULL, *actual = NULL;
  174. int ok = 0;
  175. if (pcgbl == NULL || !CRYPTO_THREAD_write_lock(pcgbl->lock)) {
  176. ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
  177. return -1;
  178. }
  179. if (!prov_already_activated(name, pcgbl->activated_providers)) {
  180. /*
  181. * There is an attempt to activate a provider, so we should disable
  182. * loading of fallbacks. Otherwise a misconfiguration could mean the
  183. * intended provider does not get loaded. Subsequent fetches could
  184. * then fallback to the default provider - which may be the wrong
  185. * thing.
  186. */
  187. if (!ossl_provider_disable_fallback_loading(libctx)) {
  188. CRYPTO_THREAD_unlock(pcgbl->lock);
  189. ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
  190. return -1;
  191. }
  192. prov = ossl_provider_find(libctx, name, 1);
  193. if (prov == NULL)
  194. prov = ossl_provider_new(libctx, name, NULL, NULL, 1);
  195. if (prov == NULL) {
  196. CRYPTO_THREAD_unlock(pcgbl->lock);
  197. if (soft)
  198. ERR_clear_error();
  199. return (soft == 0) ? -1 : 0;
  200. }
  201. if (path != NULL)
  202. ossl_provider_set_module_path(prov, path);
  203. ok = provider_conf_params(prov, NULL, NULL, value, cnf);
  204. if (ok == 1) {
  205. if (!ossl_provider_activate(prov, 1, 0)) {
  206. ok = 0;
  207. } else if (!ossl_provider_add_to_store(prov, &actual, 0)) {
  208. ossl_provider_deactivate(prov, 1);
  209. ok = 0;
  210. } else if (actual != prov
  211. && !ossl_provider_activate(actual, 1, 0)) {
  212. ossl_provider_free(actual);
  213. ok = 0;
  214. } else {
  215. if (pcgbl->activated_providers == NULL)
  216. pcgbl->activated_providers = sk_OSSL_PROVIDER_new_null();
  217. if (pcgbl->activated_providers == NULL
  218. || !sk_OSSL_PROVIDER_push(pcgbl->activated_providers,
  219. actual)) {
  220. ossl_provider_deactivate(actual, 1);
  221. ossl_provider_free(actual);
  222. ok = 0;
  223. } else {
  224. ok = 1;
  225. }
  226. }
  227. }
  228. if (ok <= 0)
  229. ossl_provider_free(prov);
  230. }
  231. CRYPTO_THREAD_unlock(pcgbl->lock);
  232. return ok;
  233. }
  234. static int provider_conf_parse_bool_setting(const char *confname,
  235. const char *confvalue, int *val)
  236. {
  237. if (confvalue == NULL) {
  238. ERR_raise_data(ERR_LIB_CRYPTO, CRYPTO_R_PROVIDER_SECTION_ERROR,
  239. "directive %s set to unrecognized value",
  240. confname);
  241. return 0;
  242. }
  243. if ((strcmp(confvalue, "1") == 0)
  244. || (strcmp(confvalue, "yes") == 0)
  245. || (strcmp(confvalue, "YES") == 0)
  246. || (strcmp(confvalue, "true") == 0)
  247. || (strcmp(confvalue, "TRUE") == 0)
  248. || (strcmp(confvalue, "on") == 0)
  249. || (strcmp(confvalue, "ON") == 0)) {
  250. *val = 1;
  251. } else if ((strcmp(confvalue, "0") == 0)
  252. || (strcmp(confvalue, "no") == 0)
  253. || (strcmp(confvalue, "NO") == 0)
  254. || (strcmp(confvalue, "false") == 0)
  255. || (strcmp(confvalue, "FALSE") == 0)
  256. || (strcmp(confvalue, "off") == 0)
  257. || (strcmp(confvalue, "OFF") == 0)) {
  258. *val = 0;
  259. } else {
  260. ERR_raise_data(ERR_LIB_CRYPTO, CRYPTO_R_PROVIDER_SECTION_ERROR,
  261. "directive %s set to unrecognized value",
  262. confname);
  263. return 0;
  264. }
  265. return 1;
  266. }
  267. static int provider_conf_load(OSSL_LIB_CTX *libctx, const char *name,
  268. const char *value, const CONF *cnf)
  269. {
  270. int i;
  271. STACK_OF(CONF_VALUE) *ecmds;
  272. int soft = 0;
  273. const char *path = NULL;
  274. int activate = 0;
  275. int ok = 0;
  276. int added = 0;
  277. name = skip_dot(name);
  278. OSSL_TRACE1(CONF, "Configuring provider %s\n", name);
  279. /* Value is a section containing PROVIDER commands */
  280. ecmds = NCONF_get_section(cnf, value);
  281. if (!ecmds) {
  282. ERR_raise_data(ERR_LIB_CRYPTO, CRYPTO_R_PROVIDER_SECTION_ERROR,
  283. "section=%s not found", value);
  284. return 0;
  285. }
  286. /* Find the needed data first */
  287. for (i = 0; i < sk_CONF_VALUE_num(ecmds); i++) {
  288. CONF_VALUE *ecmd = sk_CONF_VALUE_value(ecmds, i);
  289. const char *confname = skip_dot(ecmd->name);
  290. const char *confvalue = ecmd->value;
  291. OSSL_TRACE2(CONF, "Provider command: %s = %s\n",
  292. confname, confvalue);
  293. /* First handle some special pseudo confs */
  294. /* Override provider name to use */
  295. if (strcmp(confname, "identity") == 0) {
  296. name = confvalue;
  297. } else if (strcmp(confname, "soft_load") == 0) {
  298. if (!provider_conf_parse_bool_setting(confname,
  299. confvalue, &soft))
  300. return 0;
  301. /* Load a dynamic PROVIDER */
  302. } else if (strcmp(confname, "module") == 0) {
  303. path = confvalue;
  304. } else if (strcmp(confname, "activate") == 0) {
  305. if (!provider_conf_parse_bool_setting(confname,
  306. confvalue, &activate))
  307. return 0;
  308. }
  309. }
  310. if (activate) {
  311. ok = provider_conf_activate(libctx, name, value, path, soft, cnf);
  312. } else {
  313. OSSL_PROVIDER_INFO entry;
  314. memset(&entry, 0, sizeof(entry));
  315. ok = 1;
  316. if (name != NULL) {
  317. entry.name = OPENSSL_strdup(name);
  318. if (entry.name == NULL)
  319. ok = 0;
  320. }
  321. if (ok && path != NULL) {
  322. entry.path = OPENSSL_strdup(path);
  323. if (entry.path == NULL)
  324. ok = 0;
  325. }
  326. if (ok)
  327. ok = provider_conf_params(NULL, &entry, NULL, value, cnf);
  328. if (ok >= 1 && (entry.path != NULL || entry.parameters != NULL)) {
  329. ok = ossl_provider_info_add_to_store(libctx, &entry);
  330. added = 1;
  331. }
  332. if (added == 0)
  333. ossl_provider_info_clear(&entry);
  334. }
  335. /*
  336. * Provider activation returns a tristate:
  337. * 1 for successful activation
  338. * 0 for non-fatal activation failure
  339. * < 0 for fatal activation failure
  340. * We return success (1) for activation, (1) for non-fatal activation
  341. * failure, and (0) for fatal activation failure
  342. */
  343. return ok >= 0;
  344. }
  345. static int provider_conf_init(CONF_IMODULE *md, const CONF *cnf)
  346. {
  347. STACK_OF(CONF_VALUE) *elist;
  348. CONF_VALUE *cval;
  349. int i;
  350. OSSL_TRACE1(CONF, "Loading providers module: section %s\n",
  351. CONF_imodule_get_value(md));
  352. /* Value is a section containing PROVIDERs to configure */
  353. elist = NCONF_get_section(cnf, CONF_imodule_get_value(md));
  354. if (!elist) {
  355. ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_PROVIDER_SECTION_ERROR);
  356. return 0;
  357. }
  358. for (i = 0; i < sk_CONF_VALUE_num(elist); i++) {
  359. cval = sk_CONF_VALUE_value(elist, i);
  360. if (!provider_conf_load(NCONF_get0_libctx((CONF *)cnf),
  361. cval->name, cval->value, cnf))
  362. return 0;
  363. }
  364. return 1;
  365. }
  366. void ossl_provider_add_conf_module(void)
  367. {
  368. OSSL_TRACE(CONF, "Adding config module 'providers'\n");
  369. CONF_module_add("providers", provider_conf_init, NULL);
  370. }