2
0

provider_conf.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. DEFINE_STACK_OF(OSSL_PROVIDER)
  16. /* PROVIDER config module */
  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(OSSL_LIB_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. ERR_raise_data(ERR_LIB_CRYPTO, CRYPTO_R_PROVIDER_SECTION_ERROR,
  74. "section=%s not found", value);
  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, 0)) {
  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. ERR_raise(ERR_LIB_CRYPTO, 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(cnf->libctx, 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. }