provider_conf.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * Copyright 2019-2022 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. static int provider_conf_params(OSSL_PROVIDER *prov,
  54. OSSL_PROVIDER_INFO *provinfo,
  55. const char *name, const char *value,
  56. const CONF *cnf)
  57. {
  58. STACK_OF(CONF_VALUE) *sect;
  59. int ok = 1;
  60. sect = NCONF_get_section(cnf, value);
  61. if (sect != NULL) {
  62. int i;
  63. char buffer[512];
  64. size_t buffer_len = 0;
  65. OSSL_TRACE1(CONF, "Provider params: start section %s\n", value);
  66. if (name != NULL) {
  67. OPENSSL_strlcpy(buffer, name, sizeof(buffer));
  68. OPENSSL_strlcat(buffer, ".", sizeof(buffer));
  69. buffer_len = strlen(buffer);
  70. }
  71. for (i = 0; i < sk_CONF_VALUE_num(sect); i++) {
  72. CONF_VALUE *sectconf = sk_CONF_VALUE_value(sect, i);
  73. if (buffer_len + strlen(sectconf->name) >= sizeof(buffer))
  74. return 0;
  75. buffer[buffer_len] = '\0';
  76. OPENSSL_strlcat(buffer, sectconf->name, sizeof(buffer));
  77. if (!provider_conf_params(prov, provinfo, buffer, sectconf->value,
  78. cnf))
  79. return 0;
  80. }
  81. OSSL_TRACE1(CONF, "Provider params: finish section %s\n", value);
  82. } else {
  83. OSSL_TRACE2(CONF, "Provider params: %s = %s\n", name, value);
  84. if (prov != NULL)
  85. ok = ossl_provider_add_parameter(prov, name, value);
  86. else
  87. ok = ossl_provider_info_add_parameter(provinfo, name, value);
  88. }
  89. return ok;
  90. }
  91. static int prov_already_activated(const char *name,
  92. STACK_OF(OSSL_PROVIDER) *activated)
  93. {
  94. int i, max;
  95. if (activated == NULL)
  96. return 0;
  97. max = sk_OSSL_PROVIDER_num(activated);
  98. for (i = 0; i < max; i++) {
  99. OSSL_PROVIDER *tstprov = sk_OSSL_PROVIDER_value(activated, i);
  100. if (strcmp(OSSL_PROVIDER_get0_name(tstprov), name) == 0) {
  101. return 1;
  102. }
  103. }
  104. return 0;
  105. }
  106. static int provider_conf_activate(OSSL_LIB_CTX *libctx, const char *name,
  107. const char *value, const char *path,
  108. int soft, const CONF *cnf)
  109. {
  110. PROVIDER_CONF_GLOBAL *pcgbl
  111. = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_PROVIDER_CONF_INDEX);
  112. OSSL_PROVIDER *prov = NULL, *actual = NULL;
  113. int ok = 0;
  114. if (pcgbl == NULL || !CRYPTO_THREAD_write_lock(pcgbl->lock)) {
  115. ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
  116. return 0;
  117. }
  118. if (!prov_already_activated(name, pcgbl->activated_providers)) {
  119. /*
  120. * There is an attempt to activate a provider, so we should disable
  121. * loading of fallbacks. Otherwise a misconfiguration could mean the
  122. * intended provider does not get loaded. Subsequent fetches could
  123. * then fallback to the default provider - which may be the wrong
  124. * thing.
  125. */
  126. if (!ossl_provider_disable_fallback_loading(libctx)) {
  127. CRYPTO_THREAD_unlock(pcgbl->lock);
  128. ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
  129. return 0;
  130. }
  131. prov = ossl_provider_find(libctx, name, 1);
  132. if (prov == NULL)
  133. prov = ossl_provider_new(libctx, name, NULL, 1);
  134. if (prov == NULL) {
  135. CRYPTO_THREAD_unlock(pcgbl->lock);
  136. if (soft)
  137. ERR_clear_error();
  138. return 0;
  139. }
  140. if (path != NULL)
  141. ossl_provider_set_module_path(prov, path);
  142. ok = provider_conf_params(prov, NULL, NULL, value, cnf);
  143. if (ok) {
  144. if (!ossl_provider_activate(prov, 1, 0)) {
  145. ok = 0;
  146. } else if (!ossl_provider_add_to_store(prov, &actual, 0)) {
  147. ossl_provider_deactivate(prov, 1);
  148. ok = 0;
  149. } else if (actual != prov
  150. && !ossl_provider_activate(actual, 1, 0)) {
  151. ossl_provider_free(actual);
  152. ok = 0;
  153. } else {
  154. if (pcgbl->activated_providers == NULL)
  155. pcgbl->activated_providers = sk_OSSL_PROVIDER_new_null();
  156. if (pcgbl->activated_providers == NULL
  157. || !sk_OSSL_PROVIDER_push(pcgbl->activated_providers,
  158. actual)) {
  159. ossl_provider_deactivate(actual, 1);
  160. ossl_provider_free(actual);
  161. ok = 0;
  162. } else {
  163. ok = 1;
  164. }
  165. }
  166. }
  167. if (!ok)
  168. ossl_provider_free(prov);
  169. }
  170. CRYPTO_THREAD_unlock(pcgbl->lock);
  171. return ok;
  172. }
  173. static int provider_conf_load(OSSL_LIB_CTX *libctx, const char *name,
  174. const char *value, const CONF *cnf)
  175. {
  176. int i;
  177. STACK_OF(CONF_VALUE) *ecmds;
  178. int soft = 0;
  179. const char *path = NULL;
  180. long activate = 0;
  181. int ok = 0;
  182. name = skip_dot(name);
  183. OSSL_TRACE1(CONF, "Configuring provider %s\n", name);
  184. /* Value is a section containing PROVIDER commands */
  185. ecmds = NCONF_get_section(cnf, value);
  186. if (!ecmds) {
  187. ERR_raise_data(ERR_LIB_CRYPTO, CRYPTO_R_PROVIDER_SECTION_ERROR,
  188. "section=%s not found", value);
  189. return 0;
  190. }
  191. /* Find the needed data first */
  192. for (i = 0; i < sk_CONF_VALUE_num(ecmds); i++) {
  193. CONF_VALUE *ecmd = sk_CONF_VALUE_value(ecmds, i);
  194. const char *confname = skip_dot(ecmd->name);
  195. const char *confvalue = ecmd->value;
  196. OSSL_TRACE2(CONF, "Provider command: %s = %s\n",
  197. confname, confvalue);
  198. /* First handle some special pseudo confs */
  199. /* Override provider name to use */
  200. if (strcmp(confname, "identity") == 0)
  201. name = confvalue;
  202. else if (strcmp(confname, "soft_load") == 0)
  203. soft = 1;
  204. /* Load a dynamic PROVIDER */
  205. else if (strcmp(confname, "module") == 0)
  206. path = confvalue;
  207. else if (strcmp(confname, "activate") == 0)
  208. activate = 1;
  209. }
  210. if (activate) {
  211. ok = provider_conf_activate(libctx, name, value, path, soft, cnf);
  212. } else {
  213. OSSL_PROVIDER_INFO entry;
  214. memset(&entry, 0, sizeof(entry));
  215. ok = 1;
  216. if (name != NULL) {
  217. entry.name = OPENSSL_strdup(name);
  218. if (entry.name == NULL)
  219. ok = 0;
  220. }
  221. if (ok && path != NULL) {
  222. entry.path = OPENSSL_strdup(path);
  223. if (entry.path == NULL)
  224. ok = 0;
  225. }
  226. if (ok)
  227. ok = provider_conf_params(NULL, &entry, NULL, value, cnf);
  228. if (ok && (entry.path != NULL || entry.parameters != NULL))
  229. ok = ossl_provider_info_add_to_store(libctx, &entry);
  230. if (!ok || (entry.path == NULL && entry.parameters == NULL)) {
  231. ossl_provider_info_clear(&entry);
  232. }
  233. }
  234. /*
  235. * Even if ok is 0, we still return success. Failure to load a provider is
  236. * not fatal. We want to continue to load the rest of the config file.
  237. */
  238. return 1;
  239. }
  240. static int provider_conf_init(CONF_IMODULE *md, const CONF *cnf)
  241. {
  242. STACK_OF(CONF_VALUE) *elist;
  243. CONF_VALUE *cval;
  244. int i;
  245. OSSL_TRACE1(CONF, "Loading providers module: section %s\n",
  246. CONF_imodule_get_value(md));
  247. /* Value is a section containing PROVIDERs to configure */
  248. elist = NCONF_get_section(cnf, CONF_imodule_get_value(md));
  249. if (!elist) {
  250. ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_PROVIDER_SECTION_ERROR);
  251. return 0;
  252. }
  253. for (i = 0; i < sk_CONF_VALUE_num(elist); i++) {
  254. cval = sk_CONF_VALUE_value(elist, i);
  255. if (!provider_conf_load(NCONF_get0_libctx((CONF *)cnf),
  256. cval->name, cval->value, cnf))
  257. return 0;
  258. }
  259. return 1;
  260. }
  261. void ossl_provider_add_conf_module(void)
  262. {
  263. OSSL_TRACE(CONF, "Adding config module 'providers'\n");
  264. CONF_module_add("providers", provider_conf_init, NULL);
  265. }