eng_ctrl.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /* crypto/engine/eng_ctrl.c */
  2. /* ====================================================================
  3. * Copyright (c) 1999-2001 The OpenSSL Project. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in
  14. * the documentation and/or other materials provided with the
  15. * distribution.
  16. *
  17. * 3. All advertising materials mentioning features or use of this
  18. * software must display the following acknowledgment:
  19. * "This product includes software developed by the OpenSSL Project
  20. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  21. *
  22. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  23. * endorse or promote products derived from this software without
  24. * prior written permission. For written permission, please contact
  25. * licensing@OpenSSL.org.
  26. *
  27. * 5. Products derived from this software may not be called "OpenSSL"
  28. * nor may "OpenSSL" appear in their names without prior written
  29. * permission of the OpenSSL Project.
  30. *
  31. * 6. Redistributions of any form whatsoever must retain the following
  32. * acknowledgment:
  33. * "This product includes software developed by the OpenSSL Project
  34. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  35. *
  36. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  37. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  38. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  39. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  42. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  43. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  44. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  45. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  46. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  47. * OF THE POSSIBILITY OF SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This product includes cryptographic software written by Eric Young
  51. * (eay@cryptsoft.com). This product includes software written by Tim
  52. * Hudson (tjh@cryptsoft.com).
  53. *
  54. */
  55. #include "eng_int.h"
  56. /* When querying a ENGINE-specific control command's 'description', this string
  57. * is used if the ENGINE_CMD_DEFN has cmd_desc set to NULL. */
  58. static const char *int_no_description = "";
  59. /* These internal functions handle 'CMD'-related control commands when the
  60. * ENGINE in question has asked us to take care of it (ie. the ENGINE did not
  61. * set the ENGINE_FLAGS_MANUAL_CMD_CTRL flag. */
  62. static int int_ctrl_cmd_is_null(const ENGINE_CMD_DEFN *defn)
  63. {
  64. if((defn->cmd_num == 0) || (defn->cmd_name == NULL))
  65. return 1;
  66. return 0;
  67. }
  68. static int int_ctrl_cmd_by_name(const ENGINE_CMD_DEFN *defn, const char *s)
  69. {
  70. int idx = 0;
  71. while(!int_ctrl_cmd_is_null(defn) && (strcmp(defn->cmd_name, s) != 0))
  72. {
  73. idx++;
  74. defn++;
  75. }
  76. if(int_ctrl_cmd_is_null(defn))
  77. /* The given name wasn't found */
  78. return -1;
  79. return idx;
  80. }
  81. static int int_ctrl_cmd_by_num(const ENGINE_CMD_DEFN *defn, unsigned int num)
  82. {
  83. int idx = 0;
  84. /* NB: It is stipulated that 'cmd_defn' lists are ordered by cmd_num. So
  85. * our searches don't need to take any longer than necessary. */
  86. while(!int_ctrl_cmd_is_null(defn) && (defn->cmd_num < num))
  87. {
  88. idx++;
  89. defn++;
  90. }
  91. if(defn->cmd_num == num)
  92. return idx;
  93. /* The given cmd_num wasn't found */
  94. return -1;
  95. }
  96. static int int_ctrl_helper(ENGINE *e, int cmd, long i, void *p,
  97. void (*f)(void))
  98. {
  99. int idx;
  100. char *s = (char *)p;
  101. /* Take care of the easy one first (eg. it requires no searches) */
  102. if(cmd == ENGINE_CTRL_GET_FIRST_CMD_TYPE)
  103. {
  104. if((e->cmd_defns == NULL) || int_ctrl_cmd_is_null(e->cmd_defns))
  105. return 0;
  106. return e->cmd_defns->cmd_num;
  107. }
  108. /* One or two commands require that "p" be a valid string buffer */
  109. if((cmd == ENGINE_CTRL_GET_CMD_FROM_NAME) ||
  110. (cmd == ENGINE_CTRL_GET_NAME_FROM_CMD) ||
  111. (cmd == ENGINE_CTRL_GET_DESC_FROM_CMD))
  112. {
  113. if(s == NULL)
  114. {
  115. ENGINEerr(ENGINE_F_INT_CTRL_HELPER,
  116. ERR_R_PASSED_NULL_PARAMETER);
  117. return -1;
  118. }
  119. }
  120. /* Now handle cmd_name -> cmd_num conversion */
  121. if(cmd == ENGINE_CTRL_GET_CMD_FROM_NAME)
  122. {
  123. if((e->cmd_defns == NULL) || ((idx = int_ctrl_cmd_by_name(
  124. e->cmd_defns, s)) < 0))
  125. {
  126. ENGINEerr(ENGINE_F_INT_CTRL_HELPER,
  127. ENGINE_R_INVALID_CMD_NAME);
  128. return -1;
  129. }
  130. return e->cmd_defns[idx].cmd_num;
  131. }
  132. /* For the rest of the commands, the 'long' argument must specify a
  133. * valie command number - so we need to conduct a search. */
  134. if((e->cmd_defns == NULL) || ((idx = int_ctrl_cmd_by_num(e->cmd_defns,
  135. (unsigned int)i)) < 0))
  136. {
  137. ENGINEerr(ENGINE_F_INT_CTRL_HELPER,
  138. ENGINE_R_INVALID_CMD_NUMBER);
  139. return -1;
  140. }
  141. /* Now the logic splits depending on command type */
  142. switch(cmd)
  143. {
  144. case ENGINE_CTRL_GET_NEXT_CMD_TYPE:
  145. idx++;
  146. if(int_ctrl_cmd_is_null(e->cmd_defns + idx))
  147. /* end-of-list */
  148. return 0;
  149. else
  150. return e->cmd_defns[idx].cmd_num;
  151. case ENGINE_CTRL_GET_NAME_LEN_FROM_CMD:
  152. return strlen(e->cmd_defns[idx].cmd_name);
  153. case ENGINE_CTRL_GET_NAME_FROM_CMD:
  154. return BIO_snprintf(s,strlen(e->cmd_defns[idx].cmd_name) + 1,
  155. "%s", e->cmd_defns[idx].cmd_name);
  156. case ENGINE_CTRL_GET_DESC_LEN_FROM_CMD:
  157. if(e->cmd_defns[idx].cmd_desc)
  158. return strlen(e->cmd_defns[idx].cmd_desc);
  159. return strlen(int_no_description);
  160. case ENGINE_CTRL_GET_DESC_FROM_CMD:
  161. if(e->cmd_defns[idx].cmd_desc)
  162. return BIO_snprintf(s,
  163. strlen(e->cmd_defns[idx].cmd_desc) + 1,
  164. "%s", e->cmd_defns[idx].cmd_desc);
  165. return BIO_snprintf(s, strlen(int_no_description) + 1,"%s",
  166. int_no_description);
  167. case ENGINE_CTRL_GET_CMD_FLAGS:
  168. return e->cmd_defns[idx].cmd_flags;
  169. }
  170. /* Shouldn't really be here ... */
  171. ENGINEerr(ENGINE_F_INT_CTRL_HELPER,ENGINE_R_INTERNAL_LIST_ERROR);
  172. return -1;
  173. }
  174. int ENGINE_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void))
  175. {
  176. int ctrl_exists, ref_exists;
  177. if(e == NULL)
  178. {
  179. ENGINEerr(ENGINE_F_ENGINE_CTRL,ERR_R_PASSED_NULL_PARAMETER);
  180. return 0;
  181. }
  182. CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
  183. ref_exists = ((e->struct_ref > 0) ? 1 : 0);
  184. CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
  185. ctrl_exists = ((e->ctrl == NULL) ? 0 : 1);
  186. if(!ref_exists)
  187. {
  188. ENGINEerr(ENGINE_F_ENGINE_CTRL,ENGINE_R_NO_REFERENCE);
  189. return 0;
  190. }
  191. /* Intercept any "root-level" commands before trying to hand them on to
  192. * ctrl() handlers. */
  193. switch(cmd)
  194. {
  195. case ENGINE_CTRL_HAS_CTRL_FUNCTION:
  196. return ctrl_exists;
  197. case ENGINE_CTRL_GET_FIRST_CMD_TYPE:
  198. case ENGINE_CTRL_GET_NEXT_CMD_TYPE:
  199. case ENGINE_CTRL_GET_CMD_FROM_NAME:
  200. case ENGINE_CTRL_GET_NAME_LEN_FROM_CMD:
  201. case ENGINE_CTRL_GET_NAME_FROM_CMD:
  202. case ENGINE_CTRL_GET_DESC_LEN_FROM_CMD:
  203. case ENGINE_CTRL_GET_DESC_FROM_CMD:
  204. case ENGINE_CTRL_GET_CMD_FLAGS:
  205. if(ctrl_exists && !(e->flags & ENGINE_FLAGS_MANUAL_CMD_CTRL))
  206. return int_ctrl_helper(e,cmd,i,p,f);
  207. if(!ctrl_exists)
  208. {
  209. ENGINEerr(ENGINE_F_ENGINE_CTRL,ENGINE_R_NO_CONTROL_FUNCTION);
  210. /* For these cmd-related functions, failure is indicated
  211. * by a -1 return value (because 0 is used as a valid
  212. * return in some places). */
  213. return -1;
  214. }
  215. default:
  216. break;
  217. }
  218. /* Anything else requires a ctrl() handler to exist. */
  219. if(!ctrl_exists)
  220. {
  221. ENGINEerr(ENGINE_F_ENGINE_CTRL,ENGINE_R_NO_CONTROL_FUNCTION);
  222. return 0;
  223. }
  224. return e->ctrl(e, cmd, i, p, f);
  225. }
  226. int ENGINE_cmd_is_executable(ENGINE *e, int cmd)
  227. {
  228. int flags;
  229. if((flags = ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FLAGS, cmd, NULL, NULL)) < 0)
  230. {
  231. ENGINEerr(ENGINE_F_ENGINE_CMD_IS_EXECUTABLE,
  232. ENGINE_R_INVALID_CMD_NUMBER);
  233. return 0;
  234. }
  235. if(!(flags & ENGINE_CMD_FLAG_NO_INPUT) &&
  236. !(flags & ENGINE_CMD_FLAG_NUMERIC) &&
  237. !(flags & ENGINE_CMD_FLAG_STRING))
  238. return 0;
  239. return 1;
  240. }
  241. int ENGINE_ctrl_cmd(ENGINE *e, const char *cmd_name,
  242. long i, void *p, void (*f)(void), int cmd_optional)
  243. {
  244. int num;
  245. if((e == NULL) || (cmd_name == NULL))
  246. {
  247. ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD,
  248. ERR_R_PASSED_NULL_PARAMETER);
  249. return 0;
  250. }
  251. if((e->ctrl == NULL) || ((num = ENGINE_ctrl(e,
  252. ENGINE_CTRL_GET_CMD_FROM_NAME,
  253. 0, (void *)cmd_name, NULL)) <= 0))
  254. {
  255. /* If the command didn't *have* to be supported, we fake
  256. * success. This allows certain settings to be specified for
  257. * multiple ENGINEs and only require a change of ENGINE id
  258. * (without having to selectively apply settings). Eg. changing
  259. * from a hardware device back to the regular software ENGINE
  260. * without editing the config file, etc. */
  261. if(cmd_optional)
  262. {
  263. ERR_clear_error();
  264. return 1;
  265. }
  266. ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD,
  267. ENGINE_R_INVALID_CMD_NAME);
  268. return 0;
  269. }
  270. /* Force the result of the control command to 0 or 1, for the reasons
  271. * mentioned before. */
  272. if (ENGINE_ctrl(e, num, i, p, f) > 0)
  273. return 1;
  274. return 0;
  275. }
  276. int ENGINE_ctrl_cmd_string(ENGINE *e, const char *cmd_name, const char *arg,
  277. int cmd_optional)
  278. {
  279. int num, flags;
  280. long l;
  281. char *ptr;
  282. if((e == NULL) || (cmd_name == NULL))
  283. {
  284. ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
  285. ERR_R_PASSED_NULL_PARAMETER);
  286. return 0;
  287. }
  288. if((e->ctrl == NULL) || ((num = ENGINE_ctrl(e,
  289. ENGINE_CTRL_GET_CMD_FROM_NAME,
  290. 0, (void *)cmd_name, NULL)) <= 0))
  291. {
  292. /* If the command didn't *have* to be supported, we fake
  293. * success. This allows certain settings to be specified for
  294. * multiple ENGINEs and only require a change of ENGINE id
  295. * (without having to selectively apply settings). Eg. changing
  296. * from a hardware device back to the regular software ENGINE
  297. * without editing the config file, etc. */
  298. if(cmd_optional)
  299. {
  300. ERR_clear_error();
  301. return 1;
  302. }
  303. ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
  304. ENGINE_R_INVALID_CMD_NAME);
  305. return 0;
  306. }
  307. if(!ENGINE_cmd_is_executable(e, num))
  308. {
  309. ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
  310. ENGINE_R_CMD_NOT_EXECUTABLE);
  311. return 0;
  312. }
  313. if((flags = ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FLAGS, num, NULL, NULL)) < 0)
  314. {
  315. /* Shouldn't happen, given that ENGINE_cmd_is_executable()
  316. * returned success. */
  317. ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
  318. ENGINE_R_INTERNAL_LIST_ERROR);
  319. return 0;
  320. }
  321. /* If the command takes no input, there must be no input. And vice
  322. * versa. */
  323. if(flags & ENGINE_CMD_FLAG_NO_INPUT)
  324. {
  325. if(arg != NULL)
  326. {
  327. ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
  328. ENGINE_R_COMMAND_TAKES_NO_INPUT);
  329. return 0;
  330. }
  331. /* We deliberately force the result of ENGINE_ctrl() to 0 or 1
  332. * rather than returning it as "return data". This is to ensure
  333. * usage of these commands is consistent across applications and
  334. * that certain applications don't understand it one way, and
  335. * others another. */
  336. if(ENGINE_ctrl(e, num, 0, (void *)arg, NULL) > 0)
  337. return 1;
  338. return 0;
  339. }
  340. /* So, we require input */
  341. if(arg == NULL)
  342. {
  343. ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
  344. ENGINE_R_COMMAND_TAKES_INPUT);
  345. return 0;
  346. }
  347. /* If it takes string input, that's easy */
  348. if(flags & ENGINE_CMD_FLAG_STRING)
  349. {
  350. /* Same explanation as above */
  351. if(ENGINE_ctrl(e, num, 0, (void *)arg, NULL) > 0)
  352. return 1;
  353. return 0;
  354. }
  355. /* If it doesn't take numeric either, then it is unsupported for use in
  356. * a config-setting situation, which is what this function is for. This
  357. * should never happen though, because ENGINE_cmd_is_executable() was
  358. * used. */
  359. if(!(flags & ENGINE_CMD_FLAG_NUMERIC))
  360. {
  361. ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
  362. ENGINE_R_INTERNAL_LIST_ERROR);
  363. return 0;
  364. }
  365. l = strtol(arg, &ptr, 10);
  366. if((arg == ptr) || (*ptr != '\0'))
  367. {
  368. ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
  369. ENGINE_R_ARGUMENT_IS_NOT_A_NUMBER);
  370. return 0;
  371. }
  372. /* Force the result of the control command to 0 or 1, for the reasons
  373. * mentioned before. */
  374. if(ENGINE_ctrl(e, num, l, NULL, NULL) > 0)
  375. return 1;
  376. return 0;
  377. }