eng_cnf.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /* eng_cnf.c */
  2. /* Written by Stephen Henson (shenson@bigfoot.com) 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;
  91. char *ctrlname, *ctrlvalue;
  92. ENGINE *e = NULL;
  93. name = skip_dot(name);
  94. #ifdef ENGINE_CONF_DEBUG
  95. fprintf(stderr, "Configuring engine %s\n", name);
  96. #endif
  97. /* Value is a section containing ENGINE commands */
  98. ecmds = NCONF_get_section(cnf, value);
  99. if (!ecmds)
  100. {
  101. ENGINEerr(ENGINE_F_INT_ENGINE_CONFIGURE, ENGINE_R_ENGINE_SECTION_ERROR);
  102. return 0;
  103. }
  104. for (i = 0; i < sk_CONF_VALUE_num(ecmds); i++)
  105. {
  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, ctrlvalue);
  111. #endif
  112. /* First handle some special pseudo ctrls */
  113. /* Override engine name to use */
  114. if (!strcmp(ctrlname, "engine_id"))
  115. name = ctrlvalue;
  116. /* Load a dynamic ENGINE */
  117. else if (!strcmp(ctrlname, "dynamic_path"))
  118. {
  119. e = ENGINE_by_id("dynamic");
  120. if (!e)
  121. goto err;
  122. if (!ENGINE_ctrl_cmd_string(e, "SO_PATH", ctrlvalue, 0))
  123. goto err;
  124. if (!ENGINE_ctrl_cmd_string(e, "LIST_ADD", "2", 0))
  125. goto err;
  126. if (!ENGINE_ctrl_cmd_string(e, "LOAD", NULL, 0))
  127. goto err;
  128. }
  129. /* ... add other pseudos here ... */
  130. else
  131. {
  132. /* At this point we need an ENGINE structural reference
  133. * if we don't already have one.
  134. */
  135. if (!e)
  136. {
  137. e = ENGINE_by_id(name);
  138. if (!e)
  139. return 0;
  140. }
  141. /* Allow "EMPTY" to mean no value: this allows a valid
  142. * "value" to be passed to ctrls of type NO_INPUT
  143. */
  144. if (!strcmp(ctrlvalue, "EMPTY"))
  145. ctrlvalue = NULL;
  146. else if (!strcmp(ctrlname, "init"))
  147. {
  148. if (!NCONF_get_number_e(cnf, value, "init", &do_init))
  149. goto err;
  150. if (do_init == 1)
  151. {
  152. if (!int_engine_init(e))
  153. goto err;
  154. }
  155. else if (do_init != 0)
  156. {
  157. ENGINEerr(ENGINE_F_INT_ENGINE_CONFIGURE, ENGINE_R_INVALID_INIT_VALUE);
  158. goto err;
  159. }
  160. }
  161. else if (!strcmp(ctrlname, "default_algorithms"))
  162. {
  163. if (!ENGINE_set_default_string(e, ctrlvalue))
  164. goto err;
  165. }
  166. else if (!ENGINE_ctrl_cmd_string(e,
  167. ctrlname, ctrlvalue, 0))
  168. return 0;
  169. }
  170. }
  171. if (e && (do_init == -1) && !int_engine_init(e))
  172. goto err;
  173. ret = 1;
  174. err:
  175. if (e)
  176. ENGINE_free(e);
  177. return ret;
  178. }
  179. static int int_engine_module_init(CONF_IMODULE *md, const CONF *cnf)
  180. {
  181. STACK_OF(CONF_VALUE) *elist;
  182. CONF_VALUE *cval;
  183. int i;
  184. #ifdef ENGINE_CONF_DEBUG
  185. fprintf(stderr, "Called engine module: name %s, value %s\n",
  186. CONF_imodule_get_name(md), CONF_imodule_get_value(md));
  187. #endif
  188. /* Value is a section containing ENGINEs to configure */
  189. elist = NCONF_get_section(cnf, CONF_imodule_get_value(md));
  190. if (!elist)
  191. {
  192. ENGINEerr(ENGINE_F_ENGINE_MODULE_INIT, ENGINE_R_ENGINES_SECTION_ERROR);
  193. return 0;
  194. }
  195. for (i = 0; i < sk_CONF_VALUE_num(elist); i++)
  196. {
  197. cval = sk_CONF_VALUE_value(elist, i);
  198. if (!int_engine_configure(cval->name, cval->value, cnf))
  199. return 0;
  200. }
  201. return 1;
  202. }
  203. static void int_engine_module_finish(CONF_IMODULE *md)
  204. {
  205. ENGINE *e;
  206. while ((e = sk_ENGINE_pop(initialized_engines)))
  207. ENGINE_finish(e);
  208. sk_ENGINE_free(initialized_engines);
  209. initialized_engines = NULL;
  210. }
  211. void ENGINE_add_conf_module(void)
  212. {
  213. CONF_module_add("engines",
  214. int_engine_module_init,
  215. int_engine_module_finish);
  216. }