eng_ctrl.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. /*
  57. * When querying a ENGINE-specific control command's 'description', this
  58. * string is used if the ENGINE_CMD_DEFN has cmd_desc set to NULL.
  59. */
  60. static const char *int_no_description = "";
  61. /*
  62. * These internal functions handle 'CMD'-related control commands when the
  63. * ENGINE in question has asked us to take care of it (ie. the ENGINE did not
  64. * set the ENGINE_FLAGS_MANUAL_CMD_CTRL flag.
  65. */
  66. static int int_ctrl_cmd_is_null(const ENGINE_CMD_DEFN *defn)
  67. {
  68. if ((defn->cmd_num == 0) || (defn->cmd_name == NULL))
  69. return 1;
  70. return 0;
  71. }
  72. static int int_ctrl_cmd_by_name(const ENGINE_CMD_DEFN *defn, const char *s)
  73. {
  74. int idx = 0;
  75. while (!int_ctrl_cmd_is_null(defn) && (strcmp(defn->cmd_name, s) != 0)) {
  76. idx++;
  77. defn++;
  78. }
  79. if (int_ctrl_cmd_is_null(defn))
  80. /* The given name wasn't found */
  81. return -1;
  82. return idx;
  83. }
  84. static int int_ctrl_cmd_by_num(const ENGINE_CMD_DEFN *defn, unsigned int num)
  85. {
  86. int idx = 0;
  87. /*
  88. * NB: It is stipulated that 'cmd_defn' lists are ordered by cmd_num. So
  89. * our searches don't need to take any longer than necessary.
  90. */
  91. while (!int_ctrl_cmd_is_null(defn) && (defn->cmd_num < num)) {
  92. idx++;
  93. defn++;
  94. }
  95. if (defn->cmd_num == num)
  96. return idx;
  97. /* The given cmd_num wasn't found */
  98. return -1;
  99. }
  100. static int int_ctrl_helper(ENGINE *e, int cmd, long i, void *p,
  101. void (*f) (void))
  102. {
  103. int idx;
  104. char *s = (char *)p;
  105. /* Take care of the easy one first (eg. it requires no searches) */
  106. if (cmd == ENGINE_CTRL_GET_FIRST_CMD_TYPE) {
  107. if ((e->cmd_defns == NULL) || int_ctrl_cmd_is_null(e->cmd_defns))
  108. return 0;
  109. return e->cmd_defns->cmd_num;
  110. }
  111. /* One or two commands require that "p" be a valid string buffer */
  112. if ((cmd == ENGINE_CTRL_GET_CMD_FROM_NAME) ||
  113. (cmd == ENGINE_CTRL_GET_NAME_FROM_CMD) ||
  114. (cmd == ENGINE_CTRL_GET_DESC_FROM_CMD)) {
  115. if (s == NULL) {
  116. ENGINEerr(ENGINE_F_INT_CTRL_HELPER, 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. if ((e->cmd_defns == NULL)
  123. || ((idx = int_ctrl_cmd_by_name(e->cmd_defns, s)) < 0)) {
  124. ENGINEerr(ENGINE_F_INT_CTRL_HELPER, ENGINE_R_INVALID_CMD_NAME);
  125. return -1;
  126. }
  127. return e->cmd_defns[idx].cmd_num;
  128. }
  129. /*
  130. * For the rest of the commands, the 'long' argument must specify a valie
  131. * command number - so we need to conduct a search.
  132. */
  133. if ((e->cmd_defns == NULL) || ((idx = int_ctrl_cmd_by_num(e->cmd_defns,
  134. (unsigned int)
  135. i)) < 0)) {
  136. ENGINEerr(ENGINE_F_INT_CTRL_HELPER, ENGINE_R_INVALID_CMD_NUMBER);
  137. return -1;
  138. }
  139. /* Now the logic splits depending on command type */
  140. switch (cmd) {
  141. case ENGINE_CTRL_GET_NEXT_CMD_TYPE:
  142. idx++;
  143. if (int_ctrl_cmd_is_null(e->cmd_defns + idx))
  144. /* end-of-list */
  145. return 0;
  146. else
  147. return e->cmd_defns[idx].cmd_num;
  148. case ENGINE_CTRL_GET_NAME_LEN_FROM_CMD:
  149. return strlen(e->cmd_defns[idx].cmd_name);
  150. case ENGINE_CTRL_GET_NAME_FROM_CMD:
  151. return BIO_snprintf(s, strlen(e->cmd_defns[idx].cmd_name) + 1,
  152. "%s", e->cmd_defns[idx].cmd_name);
  153. case ENGINE_CTRL_GET_DESC_LEN_FROM_CMD:
  154. if (e->cmd_defns[idx].cmd_desc)
  155. return strlen(e->cmd_defns[idx].cmd_desc);
  156. return strlen(int_no_description);
  157. case ENGINE_CTRL_GET_DESC_FROM_CMD:
  158. if (e->cmd_defns[idx].cmd_desc)
  159. return BIO_snprintf(s,
  160. strlen(e->cmd_defns[idx].cmd_desc) + 1,
  161. "%s", e->cmd_defns[idx].cmd_desc);
  162. return BIO_snprintf(s, strlen(int_no_description) + 1, "%s",
  163. int_no_description);
  164. case ENGINE_CTRL_GET_CMD_FLAGS:
  165. return e->cmd_defns[idx].cmd_flags;
  166. }
  167. /* Shouldn't really be here ... */
  168. ENGINEerr(ENGINE_F_INT_CTRL_HELPER, ENGINE_R_INTERNAL_LIST_ERROR);
  169. return -1;
  170. }
  171. int ENGINE_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
  172. {
  173. int ctrl_exists, ref_exists;
  174. if (e == NULL) {
  175. ENGINEerr(ENGINE_F_ENGINE_CTRL, ERR_R_PASSED_NULL_PARAMETER);
  176. return 0;
  177. }
  178. CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
  179. ref_exists = ((e->struct_ref > 0) ? 1 : 0);
  180. CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
  181. ctrl_exists = ((e->ctrl == NULL) ? 0 : 1);
  182. if (!ref_exists) {
  183. ENGINEerr(ENGINE_F_ENGINE_CTRL, ENGINE_R_NO_REFERENCE);
  184. return 0;
  185. }
  186. /*
  187. * Intercept any "root-level" commands before trying to hand them on to
  188. * ctrl() handlers.
  189. */
  190. switch (cmd) {
  191. case ENGINE_CTRL_HAS_CTRL_FUNCTION:
  192. return ctrl_exists;
  193. case ENGINE_CTRL_GET_FIRST_CMD_TYPE:
  194. case ENGINE_CTRL_GET_NEXT_CMD_TYPE:
  195. case ENGINE_CTRL_GET_CMD_FROM_NAME:
  196. case ENGINE_CTRL_GET_NAME_LEN_FROM_CMD:
  197. case ENGINE_CTRL_GET_NAME_FROM_CMD:
  198. case ENGINE_CTRL_GET_DESC_LEN_FROM_CMD:
  199. case ENGINE_CTRL_GET_DESC_FROM_CMD:
  200. case ENGINE_CTRL_GET_CMD_FLAGS:
  201. if (ctrl_exists && !(e->flags & ENGINE_FLAGS_MANUAL_CMD_CTRL))
  202. return int_ctrl_helper(e, cmd, i, p, f);
  203. if (!ctrl_exists) {
  204. ENGINEerr(ENGINE_F_ENGINE_CTRL, ENGINE_R_NO_CONTROL_FUNCTION);
  205. /*
  206. * For these cmd-related functions, failure is indicated by a -1
  207. * return value (because 0 is used as a valid return in some
  208. * places).
  209. */
  210. return -1;
  211. }
  212. default:
  213. break;
  214. }
  215. /* Anything else requires a ctrl() handler to exist. */
  216. if (!ctrl_exists) {
  217. ENGINEerr(ENGINE_F_ENGINE_CTRL, ENGINE_R_NO_CONTROL_FUNCTION);
  218. return 0;
  219. }
  220. return e->ctrl(e, cmd, i, p, f);
  221. }
  222. int ENGINE_cmd_is_executable(ENGINE *e, int cmd)
  223. {
  224. int flags;
  225. if ((flags =
  226. ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FLAGS, cmd, NULL, NULL)) < 0) {
  227. ENGINEerr(ENGINE_F_ENGINE_CMD_IS_EXECUTABLE,
  228. ENGINE_R_INVALID_CMD_NUMBER);
  229. return 0;
  230. }
  231. if (!(flags & ENGINE_CMD_FLAG_NO_INPUT) &&
  232. !(flags & ENGINE_CMD_FLAG_NUMERIC) &&
  233. !(flags & ENGINE_CMD_FLAG_STRING))
  234. return 0;
  235. return 1;
  236. }
  237. int ENGINE_ctrl_cmd(ENGINE *e, const char *cmd_name,
  238. long i, void *p, void (*f) (void), int cmd_optional)
  239. {
  240. int num;
  241. if ((e == NULL) || (cmd_name == NULL)) {
  242. ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD, ERR_R_PASSED_NULL_PARAMETER);
  243. return 0;
  244. }
  245. if ((e->ctrl == NULL) || ((num = ENGINE_ctrl(e,
  246. ENGINE_CTRL_GET_CMD_FROM_NAME,
  247. 0, (void *)cmd_name,
  248. NULL)) <= 0)) {
  249. /*
  250. * If the command didn't *have* to be supported, we fake success.
  251. * This allows certain settings to be specified for multiple ENGINEs
  252. * and only require a change of ENGINE id (without having to
  253. * selectively apply settings). Eg. changing from a hardware device
  254. * back to the regular software ENGINE without editing the config
  255. * file, etc.
  256. */
  257. if (cmd_optional) {
  258. ERR_clear_error();
  259. return 1;
  260. }
  261. ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD, ENGINE_R_INVALID_CMD_NAME);
  262. return 0;
  263. }
  264. /*
  265. * Force the result of the control command to 0 or 1, for the reasons
  266. * mentioned before.
  267. */
  268. if (ENGINE_ctrl(e, num, i, p, f) > 0)
  269. return 1;
  270. return 0;
  271. }
  272. int ENGINE_ctrl_cmd_string(ENGINE *e, const char *cmd_name, const char *arg,
  273. int cmd_optional)
  274. {
  275. int num, flags;
  276. long l;
  277. char *ptr;
  278. if ((e == NULL) || (cmd_name == NULL)) {
  279. ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
  280. ERR_R_PASSED_NULL_PARAMETER);
  281. return 0;
  282. }
  283. if ((e->ctrl == NULL) || ((num = ENGINE_ctrl(e,
  284. ENGINE_CTRL_GET_CMD_FROM_NAME,
  285. 0, (void *)cmd_name,
  286. NULL)) <= 0)) {
  287. /*
  288. * If the command didn't *have* to be supported, we fake success.
  289. * This allows certain settings to be specified for multiple ENGINEs
  290. * and only require a change of ENGINE id (without having to
  291. * selectively apply settings). Eg. changing from a hardware device
  292. * back to the regular software ENGINE without editing the config
  293. * file, etc.
  294. */
  295. if (cmd_optional) {
  296. ERR_clear_error();
  297. return 1;
  298. }
  299. ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING, ENGINE_R_INVALID_CMD_NAME);
  300. return 0;
  301. }
  302. if (!ENGINE_cmd_is_executable(e, num)) {
  303. ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
  304. ENGINE_R_CMD_NOT_EXECUTABLE);
  305. return 0;
  306. }
  307. if ((flags =
  308. ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FLAGS, num, NULL, NULL)) < 0) {
  309. /*
  310. * Shouldn't happen, given that ENGINE_cmd_is_executable() returned
  311. * success.
  312. */
  313. ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
  314. ENGINE_R_INTERNAL_LIST_ERROR);
  315. return 0;
  316. }
  317. /*
  318. * If the command takes no input, there must be no input. And vice versa.
  319. */
  320. if (flags & ENGINE_CMD_FLAG_NO_INPUT) {
  321. if (arg != NULL) {
  322. ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
  323. ENGINE_R_COMMAND_TAKES_NO_INPUT);
  324. return 0;
  325. }
  326. /*
  327. * We deliberately force the result of ENGINE_ctrl() to 0 or 1 rather
  328. * than returning it as "return data". This is to ensure usage of
  329. * these commands is consistent across applications and that certain
  330. * applications don't understand it one way, and others another.
  331. */
  332. if (ENGINE_ctrl(e, num, 0, (void *)arg, NULL) > 0)
  333. return 1;
  334. return 0;
  335. }
  336. /* So, we require input */
  337. if (arg == NULL) {
  338. ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
  339. ENGINE_R_COMMAND_TAKES_INPUT);
  340. return 0;
  341. }
  342. /* If it takes string input, that's easy */
  343. if (flags & ENGINE_CMD_FLAG_STRING) {
  344. /* Same explanation as above */
  345. if (ENGINE_ctrl(e, num, 0, (void *)arg, NULL) > 0)
  346. return 1;
  347. return 0;
  348. }
  349. /*
  350. * If it doesn't take numeric either, then it is unsupported for use in a
  351. * config-setting situation, which is what this function is for. This
  352. * should never happen though, because ENGINE_cmd_is_executable() was
  353. * used.
  354. */
  355. if (!(flags & ENGINE_CMD_FLAG_NUMERIC)) {
  356. ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
  357. ENGINE_R_INTERNAL_LIST_ERROR);
  358. return 0;
  359. }
  360. l = strtol(arg, &ptr, 10);
  361. if ((arg == ptr) || (*ptr != '\0')) {
  362. ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
  363. ENGINE_R_ARGUMENT_IS_NOT_A_NUMBER);
  364. return 0;
  365. }
  366. /*
  367. * Force the result of the control command to 0 or 1, for the reasons
  368. * mentioned before.
  369. */
  370. if (ENGINE_ctrl(e, num, l, NULL, NULL) > 0)
  371. return 1;
  372. return 0;
  373. }