provider_conf.c 5.2 KB

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