eng_cnf.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /* eng_cnf.c */
  2. /*
  3. * Written by Stephen Henson (steve@openssl.org) for the OpenSSL project
  4. * 2001.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 2001 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * licensing@OpenSSL.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This product includes cryptographic software written by Eric Young
  55. * (eay@cryptsoft.com). This product includes software written by Tim
  56. * Hudson (tjh@cryptsoft.com).
  57. *
  58. */
  59. #include "eng_int.h"
  60. #include <openssl/conf.h>
  61. /* #define ENGINE_CONF_DEBUG */
  62. /* ENGINE config module */
  63. static char *skip_dot(char *name)
  64. {
  65. char *p;
  66. p = strchr(name, '.');
  67. if (p)
  68. return p + 1;
  69. return name;
  70. }
  71. static STACK_OF(ENGINE) *initialized_engines = NULL;
  72. static int int_engine_init(ENGINE *e)
  73. {
  74. if (!ENGINE_init(e))
  75. return 0;
  76. if (!initialized_engines)
  77. initialized_engines = sk_ENGINE_new_null();
  78. if (!initialized_engines || !sk_ENGINE_push(initialized_engines, e)) {
  79. ENGINE_finish(e);
  80. return 0;
  81. }
  82. return 1;
  83. }
  84. static int int_engine_configure(char *name, char *value, const CONF *cnf)
  85. {
  86. int i;
  87. int ret = 0;
  88. long do_init = -1;
  89. STACK_OF(CONF_VALUE) *ecmds;
  90. CONF_VALUE *ecmd = NULL;
  91. char *ctrlname, *ctrlvalue;
  92. ENGINE *e = NULL;
  93. int soft = 0;
  94. name = skip_dot(name);
  95. #ifdef ENGINE_CONF_DEBUG
  96. fprintf(stderr, "Configuring engine %s\n", name);
  97. #endif
  98. /* Value is a section containing ENGINE commands */
  99. ecmds = NCONF_get_section(cnf, value);
  100. if (!ecmds) {
  101. ENGINEerr(ENGINE_F_INT_ENGINE_CONFIGURE,
  102. ENGINE_R_ENGINE_SECTION_ERROR);
  103. return 0;
  104. }
  105. for (i = 0; i < sk_CONF_VALUE_num(ecmds); i++) {
  106. ecmd = sk_CONF_VALUE_value(ecmds, i);
  107. ctrlname = skip_dot(ecmd->name);
  108. ctrlvalue = ecmd->value;
  109. #ifdef ENGINE_CONF_DEBUG
  110. fprintf(stderr, "ENGINE conf: doing ctrl(%s,%s)\n", ctrlname,
  111. ctrlvalue);
  112. #endif
  113. /* First handle some special pseudo ctrls */
  114. /* Override engine name to use */
  115. if (!strcmp(ctrlname, "engine_id"))
  116. name = ctrlvalue;
  117. else if (!strcmp(ctrlname, "soft_load"))
  118. soft = 1;
  119. /* Load a dynamic ENGINE */
  120. else if (!strcmp(ctrlname, "dynamic_path")) {
  121. e = ENGINE_by_id("dynamic");
  122. if (!e)
  123. goto err;
  124. if (!ENGINE_ctrl_cmd_string(e, "SO_PATH", ctrlvalue, 0))
  125. goto err;
  126. if (!ENGINE_ctrl_cmd_string(e, "LIST_ADD", "2", 0))
  127. goto err;
  128. if (!ENGINE_ctrl_cmd_string(e, "LOAD", NULL, 0))
  129. goto err;
  130. }
  131. /* ... add other pseudos here ... */
  132. else {
  133. /*
  134. * At this point we need an ENGINE structural reference if we
  135. * don't already have one.
  136. */
  137. if (!e) {
  138. e = ENGINE_by_id(name);
  139. if (!e && soft) {
  140. ERR_clear_error();
  141. return 1;
  142. }
  143. if (!e)
  144. goto err;
  145. }
  146. /*
  147. * Allow "EMPTY" to mean no value: this allows a valid "value" to
  148. * be passed to ctrls of type NO_INPUT
  149. */
  150. if (!strcmp(ctrlvalue, "EMPTY"))
  151. ctrlvalue = NULL;
  152. if (!strcmp(ctrlname, "init")) {
  153. if (!NCONF_get_number_e(cnf, value, "init", &do_init))
  154. goto err;
  155. if (do_init == 1) {
  156. if (!int_engine_init(e))
  157. goto err;
  158. } else if (do_init != 0) {
  159. ENGINEerr(ENGINE_F_INT_ENGINE_CONFIGURE,
  160. ENGINE_R_INVALID_INIT_VALUE);
  161. goto err;
  162. }
  163. } else if (!strcmp(ctrlname, "default_algorithms")) {
  164. if (!ENGINE_set_default_string(e, ctrlvalue))
  165. goto err;
  166. } else if (!ENGINE_ctrl_cmd_string(e, ctrlname, ctrlvalue, 0))
  167. goto err;
  168. }
  169. }
  170. if (e && (do_init == -1) && !int_engine_init(e)) {
  171. ecmd = NULL;
  172. goto err;
  173. }
  174. ret = 1;
  175. err:
  176. if (ret != 1) {
  177. ENGINEerr(ENGINE_F_INT_ENGINE_CONFIGURE,
  178. ENGINE_R_ENGINE_CONFIGURATION_ERROR);
  179. if (ecmd)
  180. ERR_add_error_data(6, "section=", ecmd->section,
  181. ", name=", ecmd->name,
  182. ", value=", ecmd->value);
  183. }
  184. if (e)
  185. ENGINE_free(e);
  186. return ret;
  187. }
  188. static int int_engine_module_init(CONF_IMODULE *md, const CONF *cnf)
  189. {
  190. STACK_OF(CONF_VALUE) *elist;
  191. CONF_VALUE *cval;
  192. int i;
  193. #ifdef ENGINE_CONF_DEBUG
  194. fprintf(stderr, "Called engine module: name %s, value %s\n",
  195. CONF_imodule_get_name(md), CONF_imodule_get_value(md));
  196. #endif
  197. /* Value is a section containing ENGINEs to configure */
  198. elist = NCONF_get_section(cnf, CONF_imodule_get_value(md));
  199. if (!elist) {
  200. ENGINEerr(ENGINE_F_INT_ENGINE_MODULE_INIT,
  201. ENGINE_R_ENGINES_SECTION_ERROR);
  202. return 0;
  203. }
  204. for (i = 0; i < sk_CONF_VALUE_num(elist); i++) {
  205. cval = sk_CONF_VALUE_value(elist, i);
  206. if (!int_engine_configure(cval->name, cval->value, cnf))
  207. return 0;
  208. }
  209. return 1;
  210. }
  211. static void int_engine_module_finish(CONF_IMODULE *md)
  212. {
  213. ENGINE *e;
  214. while ((e = sk_ENGINE_pop(initialized_engines)))
  215. ENGINE_finish(e);
  216. sk_ENGINE_free(initialized_engines);
  217. initialized_engines = NULL;
  218. }
  219. void ENGINE_add_conf_module(void)
  220. {
  221. CONF_module_add("engines",
  222. int_engine_module_init, int_engine_module_finish);
  223. }