eng_cnf.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Copyright 2002-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. /* We need to use some engine deprecated APIs */
  10. #define OPENSSL_SUPPRESS_DEPRECATED
  11. #include "eng_local.h"
  12. #include <openssl/conf.h>
  13. #include <openssl/trace.h>
  14. DEFINE_STACK_OF(CONF_VALUE)
  15. /* ENGINE config module */
  16. static const char *skip_dot(const char *name)
  17. {
  18. const char *p = strchr(name, '.');
  19. if (p != NULL)
  20. return p + 1;
  21. return name;
  22. }
  23. static STACK_OF(ENGINE) *initialized_engines = NULL;
  24. static int int_engine_init(ENGINE *e)
  25. {
  26. if (!ENGINE_init(e))
  27. return 0;
  28. if (!initialized_engines)
  29. initialized_engines = sk_ENGINE_new_null();
  30. if (!initialized_engines || !sk_ENGINE_push(initialized_engines, e)) {
  31. ENGINE_finish(e);
  32. return 0;
  33. }
  34. return 1;
  35. }
  36. static int int_engine_configure(const char *name, const char *value, const CONF *cnf)
  37. {
  38. int i;
  39. int ret = 0;
  40. long do_init = -1;
  41. STACK_OF(CONF_VALUE) *ecmds;
  42. CONF_VALUE *ecmd = NULL;
  43. const char *ctrlname, *ctrlvalue;
  44. ENGINE *e = NULL;
  45. int soft = 0;
  46. name = skip_dot(name);
  47. OSSL_TRACE1(CONF, "Configuring engine %s\n", name);
  48. /* Value is a section containing ENGINE commands */
  49. ecmds = NCONF_get_section(cnf, value);
  50. if (!ecmds) {
  51. ENGINEerr(ENGINE_F_INT_ENGINE_CONFIGURE,
  52. ENGINE_R_ENGINE_SECTION_ERROR);
  53. return 0;
  54. }
  55. for (i = 0; i < sk_CONF_VALUE_num(ecmds); i++) {
  56. ecmd = sk_CONF_VALUE_value(ecmds, i);
  57. ctrlname = skip_dot(ecmd->name);
  58. ctrlvalue = ecmd->value;
  59. OSSL_TRACE2(CONF, "ENGINE: doing ctrl(%s,%s)\n",
  60. ctrlname, ctrlvalue);
  61. /* First handle some special pseudo ctrls */
  62. /* Override engine name to use */
  63. if (strcmp(ctrlname, "engine_id") == 0)
  64. name = ctrlvalue;
  65. else if (strcmp(ctrlname, "soft_load") == 0)
  66. soft = 1;
  67. /* Load a dynamic ENGINE */
  68. else if (strcmp(ctrlname, "dynamic_path") == 0) {
  69. e = ENGINE_by_id("dynamic");
  70. if (!e)
  71. goto err;
  72. if (!ENGINE_ctrl_cmd_string(e, "SO_PATH", ctrlvalue, 0))
  73. goto err;
  74. if (!ENGINE_ctrl_cmd_string(e, "LIST_ADD", "2", 0))
  75. goto err;
  76. if (!ENGINE_ctrl_cmd_string(e, "LOAD", NULL, 0))
  77. goto err;
  78. }
  79. /* ... add other pseudos here ... */
  80. else {
  81. /*
  82. * At this point we need an ENGINE structural reference if we
  83. * don't already have one.
  84. */
  85. if (!e) {
  86. e = ENGINE_by_id(name);
  87. if (!e && soft) {
  88. ERR_clear_error();
  89. return 1;
  90. }
  91. if (!e)
  92. goto err;
  93. }
  94. /*
  95. * Allow "EMPTY" to mean no value: this allows a valid "value" to
  96. * be passed to ctrls of type NO_INPUT
  97. */
  98. if (strcmp(ctrlvalue, "EMPTY") == 0)
  99. ctrlvalue = NULL;
  100. if (strcmp(ctrlname, "init") == 0) {
  101. if (!NCONF_get_number_e(cnf, value, "init", &do_init))
  102. goto err;
  103. if (do_init == 1) {
  104. if (!int_engine_init(e))
  105. goto err;
  106. } else if (do_init != 0) {
  107. ENGINEerr(ENGINE_F_INT_ENGINE_CONFIGURE,
  108. ENGINE_R_INVALID_INIT_VALUE);
  109. goto err;
  110. }
  111. } else if (strcmp(ctrlname, "default_algorithms") == 0) {
  112. if (!ENGINE_set_default_string(e, ctrlvalue))
  113. goto err;
  114. } else if (!ENGINE_ctrl_cmd_string(e, ctrlname, ctrlvalue, 0))
  115. goto err;
  116. }
  117. }
  118. if (e && (do_init == -1) && !int_engine_init(e)) {
  119. ecmd = NULL;
  120. goto err;
  121. }
  122. ret = 1;
  123. err:
  124. if (ret != 1) {
  125. ENGINEerr(ENGINE_F_INT_ENGINE_CONFIGURE,
  126. ENGINE_R_ENGINE_CONFIGURATION_ERROR);
  127. if (ecmd)
  128. ERR_add_error_data(6, "section=", ecmd->section,
  129. ", name=", ecmd->name,
  130. ", value=", ecmd->value);
  131. }
  132. ENGINE_free(e);
  133. return ret;
  134. }
  135. static int int_engine_module_init(CONF_IMODULE *md, const CONF *cnf)
  136. {
  137. STACK_OF(CONF_VALUE) *elist;
  138. CONF_VALUE *cval;
  139. int i;
  140. OSSL_TRACE2(CONF, "Called engine module: name %s, value %s\n",
  141. CONF_imodule_get_name(md), CONF_imodule_get_value(md));
  142. /* Value is a section containing ENGINEs to configure */
  143. elist = NCONF_get_section(cnf, CONF_imodule_get_value(md));
  144. if (!elist) {
  145. ENGINEerr(ENGINE_F_INT_ENGINE_MODULE_INIT,
  146. ENGINE_R_ENGINES_SECTION_ERROR);
  147. return 0;
  148. }
  149. for (i = 0; i < sk_CONF_VALUE_num(elist); i++) {
  150. cval = sk_CONF_VALUE_value(elist, i);
  151. if (!int_engine_configure(cval->name, cval->value, cnf))
  152. return 0;
  153. }
  154. return 1;
  155. }
  156. static void int_engine_module_finish(CONF_IMODULE *md)
  157. {
  158. ENGINE *e;
  159. while ((e = sk_ENGINE_pop(initialized_engines)))
  160. ENGINE_finish(e);
  161. sk_ENGINE_free(initialized_engines);
  162. initialized_engines = NULL;
  163. }
  164. void ENGINE_add_conf_module(void)
  165. {
  166. CONF_module_add("engines",
  167. int_engine_module_init, int_engine_module_finish);
  168. }