eng_cnf.c 6.9 KB

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