provider_conf.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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_new(libctx, name, NULL);
  96. if (prov == NULL) {
  97. if (soft)
  98. ERR_clear_error();
  99. return 0;
  100. }
  101. if (path != NULL)
  102. ossl_provider_set_module_path(prov, path);
  103. ok = provider_conf_params(prov, NULL, value, cnf);
  104. if (ok && activate) {
  105. if (!ossl_provider_activate(prov)) {
  106. ok = 0;
  107. } else {
  108. if (activated_providers == NULL)
  109. activated_providers = sk_OSSL_PROVIDER_new_null();
  110. sk_OSSL_PROVIDER_push(activated_providers, prov);
  111. ok = 1;
  112. }
  113. }
  114. if (!(activate && ok))
  115. ossl_provider_free(prov);
  116. return ok;
  117. }
  118. static int provider_conf_init(CONF_IMODULE *md, const CONF *cnf)
  119. {
  120. STACK_OF(CONF_VALUE) *elist;
  121. CONF_VALUE *cval;
  122. int i;
  123. OSSL_TRACE1(CONF, "Loading providers module: section %s\n",
  124. CONF_imodule_get_value(md));
  125. /* Value is a section containing PROVIDERs to configure */
  126. elist = NCONF_get_section(cnf, CONF_imodule_get_value(md));
  127. if (!elist) {
  128. CRYPTOerr(CRYPTO_F_PROVIDER_CONF_INIT,
  129. CRYPTO_R_PROVIDER_SECTION_ERROR);
  130. return 0;
  131. }
  132. for (i = 0; i < sk_CONF_VALUE_num(elist); i++) {
  133. cval = sk_CONF_VALUE_value(elist, i);
  134. if (!provider_conf_load(NULL, cval->name, cval->value, cnf))
  135. return 0;
  136. }
  137. return 1;
  138. }
  139. static void provider_conf_deinit(CONF_IMODULE *md)
  140. {
  141. sk_OSSL_PROVIDER_pop_free(activated_providers, ossl_provider_free);
  142. activated_providers = NULL;
  143. OSSL_TRACE(CONF, "Cleaned up providers\n");
  144. }
  145. void ossl_provider_add_conf_module(void)
  146. {
  147. OSSL_TRACE(CONF, "Adding config module 'providers'\n");
  148. CONF_module_add("providers", provider_conf_init, provider_conf_deinit);
  149. }