provider_conf.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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_load(OSSL_LIB_CTX *libctx, const char *name,
  235. const char *value, const CONF *cnf)
  236. {
  237. int i;
  238. STACK_OF(CONF_VALUE) *ecmds;
  239. int soft = 0;
  240. const char *path = NULL;
  241. long activate = 0;
  242. int ok = 0;
  243. int added = 0;
  244. name = skip_dot(name);
  245. OSSL_TRACE1(CONF, "Configuring provider %s\n", name);
  246. /* Value is a section containing PROVIDER commands */
  247. ecmds = NCONF_get_section(cnf, value);
  248. if (!ecmds) {
  249. ERR_raise_data(ERR_LIB_CRYPTO, CRYPTO_R_PROVIDER_SECTION_ERROR,
  250. "section=%s not found", value);
  251. return 0;
  252. }
  253. /* Find the needed data first */
  254. for (i = 0; i < sk_CONF_VALUE_num(ecmds); i++) {
  255. CONF_VALUE *ecmd = sk_CONF_VALUE_value(ecmds, i);
  256. const char *confname = skip_dot(ecmd->name);
  257. const char *confvalue = ecmd->value;
  258. OSSL_TRACE2(CONF, "Provider command: %s = %s\n",
  259. confname, confvalue);
  260. /* First handle some special pseudo confs */
  261. /* Override provider name to use */
  262. if (strcmp(confname, "identity") == 0)
  263. name = confvalue;
  264. else if (strcmp(confname, "soft_load") == 0)
  265. soft = 1;
  266. /* Load a dynamic PROVIDER */
  267. else if (strcmp(confname, "module") == 0)
  268. path = confvalue;
  269. else if (strcmp(confname, "activate") == 0)
  270. activate = 1;
  271. }
  272. if (activate) {
  273. ok = provider_conf_activate(libctx, name, value, path, soft, cnf);
  274. } else {
  275. OSSL_PROVIDER_INFO entry;
  276. memset(&entry, 0, sizeof(entry));
  277. ok = 1;
  278. if (name != NULL) {
  279. entry.name = OPENSSL_strdup(name);
  280. if (entry.name == NULL)
  281. ok = 0;
  282. }
  283. if (ok && path != NULL) {
  284. entry.path = OPENSSL_strdup(path);
  285. if (entry.path == NULL)
  286. ok = 0;
  287. }
  288. if (ok)
  289. ok = provider_conf_params(NULL, &entry, NULL, value, cnf);
  290. if (ok >= 1 && (entry.path != NULL || entry.parameters != NULL)) {
  291. ok = ossl_provider_info_add_to_store(libctx, &entry);
  292. added = 1;
  293. }
  294. if (added == 0)
  295. ossl_provider_info_clear(&entry);
  296. }
  297. /*
  298. * Provider activation returns a tristate:
  299. * 1 for successful activation
  300. * 0 for non-fatal activation failure
  301. * < 0 for fatal activation failure
  302. * We return success (1) for activation, (1) for non-fatal activation
  303. * failure, and (0) for fatal activation failure
  304. */
  305. return ok >= 0;
  306. }
  307. static int provider_conf_init(CONF_IMODULE *md, const CONF *cnf)
  308. {
  309. STACK_OF(CONF_VALUE) *elist;
  310. CONF_VALUE *cval;
  311. int i;
  312. OSSL_TRACE1(CONF, "Loading providers module: section %s\n",
  313. CONF_imodule_get_value(md));
  314. /* Value is a section containing PROVIDERs to configure */
  315. elist = NCONF_get_section(cnf, CONF_imodule_get_value(md));
  316. if (!elist) {
  317. ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_PROVIDER_SECTION_ERROR);
  318. return 0;
  319. }
  320. for (i = 0; i < sk_CONF_VALUE_num(elist); i++) {
  321. cval = sk_CONF_VALUE_value(elist, i);
  322. if (!provider_conf_load(NCONF_get0_libctx((CONF *)cnf),
  323. cval->name, cval->value, cnf))
  324. return 0;
  325. }
  326. return 1;
  327. }
  328. void ossl_provider_add_conf_module(void)
  329. {
  330. OSSL_TRACE(CONF, "Adding config module 'providers'\n");
  331. CONF_module_add("providers", provider_conf_init, NULL);
  332. }