provider_conf.c 5.3 KB

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