eng_cnf.c 5.6 KB

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