provider_conf.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright 2019-2021 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 "internal/provider.h"
  15. #include "internal/cryptlib.h"
  16. DEFINE_STACK_OF(OSSL_PROVIDER)
  17. /* PROVIDER config module */
  18. typedef struct {
  19. STACK_OF(OSSL_PROVIDER) *activated_providers;
  20. } PROVIDER_CONF_GLOBAL;
  21. static void *prov_conf_ossl_ctx_new(OSSL_LIB_CTX *libctx)
  22. {
  23. PROVIDER_CONF_GLOBAL *pcgbl = OPENSSL_zalloc(sizeof(*pcgbl));
  24. if (pcgbl == NULL)
  25. return NULL;
  26. return pcgbl;
  27. }
  28. static void prov_conf_ossl_ctx_free(void *vpcgbl)
  29. {
  30. PROVIDER_CONF_GLOBAL *pcgbl = vpcgbl;
  31. sk_OSSL_PROVIDER_pop_free(pcgbl->activated_providers,
  32. ossl_provider_free);
  33. OSSL_TRACE(CONF, "Cleaned up providers\n");
  34. OPENSSL_free(pcgbl);
  35. }
  36. static const OSSL_LIB_CTX_METHOD provider_conf_ossl_ctx_method = {
  37. /* Must be freed before the provider store is freed */
  38. OSSL_LIB_CTX_METHOD_PRIORITY_2,
  39. prov_conf_ossl_ctx_new,
  40. prov_conf_ossl_ctx_free,
  41. };
  42. static const char *skip_dot(const char *name)
  43. {
  44. const char *p = strchr(name, '.');
  45. if (p != NULL)
  46. return p + 1;
  47. return name;
  48. }
  49. static int provider_conf_params(OSSL_PROVIDER *prov,
  50. const char *name, const char *value,
  51. const CONF *cnf)
  52. {
  53. STACK_OF(CONF_VALUE) *sect;
  54. int ok = 1;
  55. sect = NCONF_get_section(cnf, value);
  56. if (sect != NULL) {
  57. int i;
  58. char buffer[512];
  59. size_t buffer_len = 0;
  60. OSSL_TRACE1(CONF, "Provider params: start section %s\n", value);
  61. if (name != NULL) {
  62. OPENSSL_strlcpy(buffer, name, sizeof(buffer));
  63. OPENSSL_strlcat(buffer, ".", sizeof(buffer));
  64. buffer_len = strlen(buffer);
  65. }
  66. for (i = 0; i < sk_CONF_VALUE_num(sect); i++) {
  67. CONF_VALUE *sectconf = sk_CONF_VALUE_value(sect, i);
  68. if (buffer_len + strlen(sectconf->name) >= sizeof(buffer))
  69. return 0;
  70. buffer[buffer_len] = '\0';
  71. OPENSSL_strlcat(buffer, sectconf->name, sizeof(buffer));
  72. if (!provider_conf_params(prov, buffer, sectconf->value, cnf))
  73. return 0;
  74. }
  75. OSSL_TRACE1(CONF, "Provider params: finish section %s\n", value);
  76. } else {
  77. OSSL_TRACE2(CONF, "Provider params: %s = %s\n", name, value);
  78. ok = ossl_provider_add_parameter(prov, name, value);
  79. }
  80. return ok;
  81. }
  82. static int provider_conf_load(OSSL_LIB_CTX *libctx, const char *name,
  83. const char *value, const CONF *cnf)
  84. {
  85. int i;
  86. STACK_OF(CONF_VALUE) *ecmds;
  87. int soft = 0;
  88. OSSL_PROVIDER *prov = NULL;
  89. const char *path = NULL;
  90. long activate = 0;
  91. int ok = 0;
  92. PROVIDER_CONF_GLOBAL *pcgbl
  93. = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_PROVIDER_CONF_INDEX,
  94. &provider_conf_ossl_ctx_method);
  95. name = skip_dot(name);
  96. OSSL_TRACE1(CONF, "Configuring provider %s\n", name);
  97. /* Value is a section containing PROVIDER commands */
  98. ecmds = NCONF_get_section(cnf, value);
  99. if (!ecmds) {
  100. ERR_raise_data(ERR_LIB_CRYPTO, CRYPTO_R_PROVIDER_SECTION_ERROR,
  101. "section=%s not found", value);
  102. return 0;
  103. }
  104. /* Find the needed data first */
  105. for (i = 0; i < sk_CONF_VALUE_num(ecmds); i++) {
  106. CONF_VALUE *ecmd = sk_CONF_VALUE_value(ecmds, i);
  107. const char *confname = skip_dot(ecmd->name);
  108. const char *confvalue = ecmd->value;
  109. OSSL_TRACE2(CONF, "Provider command: %s = %s\n",
  110. confname, confvalue);
  111. /* First handle some special pseudo confs */
  112. /* Override provider name to use */
  113. if (strcmp(confname, "identity") == 0)
  114. name = confvalue;
  115. else if (strcmp(confname, "soft_load") == 0)
  116. soft = 1;
  117. /* Load a dynamic PROVIDER */
  118. else if (strcmp(confname, "module") == 0)
  119. path = confvalue;
  120. else if (strcmp(confname, "activate") == 0)
  121. activate = 1;
  122. }
  123. prov = ossl_provider_find(libctx, name, 1);
  124. if (prov == NULL)
  125. prov = ossl_provider_new(libctx, name, NULL, 1);
  126. if (prov == NULL) {
  127. if (soft)
  128. ERR_clear_error();
  129. return 0;
  130. }
  131. if (path != NULL)
  132. ossl_provider_set_module_path(prov, path);
  133. ok = provider_conf_params(prov, NULL, value, cnf);
  134. if (ok && activate) {
  135. if (!ossl_provider_activate(prov, 0, 1)) {
  136. ok = 0;
  137. } else {
  138. if (pcgbl->activated_providers == NULL)
  139. pcgbl->activated_providers = sk_OSSL_PROVIDER_new_null();
  140. sk_OSSL_PROVIDER_push(pcgbl->activated_providers, prov);
  141. ok = 1;
  142. }
  143. }
  144. if (!(activate && ok))
  145. ossl_provider_free(prov);
  146. return ok;
  147. }
  148. static int provider_conf_init(CONF_IMODULE *md, const CONF *cnf)
  149. {
  150. STACK_OF(CONF_VALUE) *elist;
  151. CONF_VALUE *cval;
  152. int i;
  153. OSSL_TRACE1(CONF, "Loading providers module: section %s\n",
  154. CONF_imodule_get_value(md));
  155. /* Value is a section containing PROVIDERs to configure */
  156. elist = NCONF_get_section(cnf, CONF_imodule_get_value(md));
  157. if (!elist) {
  158. ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_PROVIDER_SECTION_ERROR);
  159. return 0;
  160. }
  161. for (i = 0; i < sk_CONF_VALUE_num(elist); i++) {
  162. cval = sk_CONF_VALUE_value(elist, i);
  163. if (!provider_conf_load(NCONF_get0_libctx((CONF *)cnf),
  164. cval->name, cval->value, cnf))
  165. return 0;
  166. }
  167. return 1;
  168. }
  169. void ossl_provider_add_conf_module(void)
  170. {
  171. OSSL_TRACE(CONF, "Adding config module 'providers'\n");
  172. CONF_module_add("providers", provider_conf_init, NULL);
  173. }