eng_ctrl.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * Copyright 2001-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /* We need to use some engine deprecated APIs */
  10. #define OPENSSL_SUPPRESS_DEPRECATED
  11. #include "eng_local.h"
  12. /*
  13. * When querying a ENGINE-specific control command's 'description', this
  14. * string is used if the ENGINE_CMD_DEFN has cmd_desc set to NULL.
  15. */
  16. static const char *int_no_description = "";
  17. /*
  18. * These internal functions handle 'CMD'-related control commands when the
  19. * ENGINE in question has asked us to take care of it (ie. the ENGINE did not
  20. * set the ENGINE_FLAGS_MANUAL_CMD_CTRL flag.
  21. */
  22. static int int_ctrl_cmd_is_null(const ENGINE_CMD_DEFN *defn)
  23. {
  24. if ((defn->cmd_num == 0) || (defn->cmd_name == NULL))
  25. return 1;
  26. return 0;
  27. }
  28. static int int_ctrl_cmd_by_name(const ENGINE_CMD_DEFN *defn, const char *s)
  29. {
  30. int idx = 0;
  31. while (!int_ctrl_cmd_is_null(defn) && (strcmp(defn->cmd_name, s) != 0)) {
  32. idx++;
  33. defn++;
  34. }
  35. if (int_ctrl_cmd_is_null(defn))
  36. /* The given name wasn't found */
  37. return -1;
  38. return idx;
  39. }
  40. static int int_ctrl_cmd_by_num(const ENGINE_CMD_DEFN *defn, unsigned int num)
  41. {
  42. int idx = 0;
  43. /*
  44. * NB: It is stipulated that 'cmd_defn' lists are ordered by cmd_num. So
  45. * our searches don't need to take any longer than necessary.
  46. */
  47. while (!int_ctrl_cmd_is_null(defn) && (defn->cmd_num < num)) {
  48. idx++;
  49. defn++;
  50. }
  51. if (defn->cmd_num == num)
  52. return idx;
  53. /* The given cmd_num wasn't found */
  54. return -1;
  55. }
  56. static int int_ctrl_helper(ENGINE *e, int cmd, long i, void *p,
  57. void (*f) (void))
  58. {
  59. int idx;
  60. char *s = (char *)p;
  61. const ENGINE_CMD_DEFN *cdp;
  62. /* Take care of the easy one first (eg. it requires no searches) */
  63. if (cmd == ENGINE_CTRL_GET_FIRST_CMD_TYPE) {
  64. if ((e->cmd_defns == NULL) || int_ctrl_cmd_is_null(e->cmd_defns))
  65. return 0;
  66. return e->cmd_defns->cmd_num;
  67. }
  68. /* One or two commands require that "p" be a valid string buffer */
  69. if ((cmd == ENGINE_CTRL_GET_CMD_FROM_NAME) ||
  70. (cmd == ENGINE_CTRL_GET_NAME_FROM_CMD) ||
  71. (cmd == ENGINE_CTRL_GET_DESC_FROM_CMD)) {
  72. if (s == NULL) {
  73. ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
  74. return -1;
  75. }
  76. }
  77. /* Now handle cmd_name -> cmd_num conversion */
  78. if (cmd == ENGINE_CTRL_GET_CMD_FROM_NAME) {
  79. if ((e->cmd_defns == NULL)
  80. || ((idx = int_ctrl_cmd_by_name(e->cmd_defns, s)) < 0)) {
  81. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INVALID_CMD_NAME);
  82. return -1;
  83. }
  84. return e->cmd_defns[idx].cmd_num;
  85. }
  86. /*
  87. * For the rest of the commands, the 'long' argument must specify a valid
  88. * command number - so we need to conduct a search.
  89. */
  90. if ((e->cmd_defns == NULL)
  91. || ((idx = int_ctrl_cmd_by_num(e->cmd_defns, (unsigned int)i)) < 0)) {
  92. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INVALID_CMD_NUMBER);
  93. return -1;
  94. }
  95. /* Now the logic splits depending on command type */
  96. cdp = &e->cmd_defns[idx];
  97. switch (cmd) {
  98. case ENGINE_CTRL_GET_NEXT_CMD_TYPE:
  99. cdp++;
  100. return int_ctrl_cmd_is_null(cdp) ? 0 : cdp->cmd_num;
  101. case ENGINE_CTRL_GET_NAME_LEN_FROM_CMD:
  102. return strlen(cdp->cmd_name);
  103. case ENGINE_CTRL_GET_NAME_FROM_CMD:
  104. return strlen(strcpy(s, cdp->cmd_name));
  105. case ENGINE_CTRL_GET_DESC_LEN_FROM_CMD:
  106. return strlen(cdp->cmd_desc == NULL ? int_no_description
  107. : cdp->cmd_desc);
  108. case ENGINE_CTRL_GET_DESC_FROM_CMD:
  109. return strlen(strcpy(s, cdp->cmd_desc == NULL ? int_no_description
  110. : cdp->cmd_desc));
  111. case ENGINE_CTRL_GET_CMD_FLAGS:
  112. return cdp->cmd_flags;
  113. }
  114. /* Shouldn't really be here ... */
  115. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INTERNAL_LIST_ERROR);
  116. return -1;
  117. }
  118. int ENGINE_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
  119. {
  120. int ctrl_exists;
  121. if (e == NULL) {
  122. ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
  123. return 0;
  124. }
  125. ctrl_exists = ((e->ctrl == NULL) ? 0 : 1);
  126. /*
  127. * Intercept any "root-level" commands before trying to hand them on to
  128. * ctrl() handlers.
  129. */
  130. switch (cmd) {
  131. case ENGINE_CTRL_HAS_CTRL_FUNCTION:
  132. return ctrl_exists;
  133. case ENGINE_CTRL_GET_FIRST_CMD_TYPE:
  134. case ENGINE_CTRL_GET_NEXT_CMD_TYPE:
  135. case ENGINE_CTRL_GET_CMD_FROM_NAME:
  136. case ENGINE_CTRL_GET_NAME_LEN_FROM_CMD:
  137. case ENGINE_CTRL_GET_NAME_FROM_CMD:
  138. case ENGINE_CTRL_GET_DESC_LEN_FROM_CMD:
  139. case ENGINE_CTRL_GET_DESC_FROM_CMD:
  140. case ENGINE_CTRL_GET_CMD_FLAGS:
  141. if (ctrl_exists && !(e->flags & ENGINE_FLAGS_MANUAL_CMD_CTRL))
  142. return int_ctrl_helper(e, cmd, i, p, f);
  143. if (!ctrl_exists) {
  144. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_NO_CONTROL_FUNCTION);
  145. /*
  146. * For these cmd-related functions, failure is indicated by a -1
  147. * return value (because 0 is used as a valid return in some
  148. * places).
  149. */
  150. return -1;
  151. }
  152. default:
  153. break;
  154. }
  155. /* Anything else requires a ctrl() handler to exist. */
  156. if (!ctrl_exists) {
  157. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_NO_CONTROL_FUNCTION);
  158. return 0;
  159. }
  160. return e->ctrl(e, cmd, i, p, f);
  161. }
  162. int ENGINE_cmd_is_executable(ENGINE *e, int cmd)
  163. {
  164. int flags;
  165. if ((flags =
  166. ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FLAGS, cmd, NULL, NULL)) < 0) {
  167. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INVALID_CMD_NUMBER);
  168. return 0;
  169. }
  170. if (!(flags & ENGINE_CMD_FLAG_NO_INPUT) &&
  171. !(flags & ENGINE_CMD_FLAG_NUMERIC) &&
  172. !(flags & ENGINE_CMD_FLAG_STRING))
  173. return 0;
  174. return 1;
  175. }
  176. int ENGINE_ctrl_cmd(ENGINE *e, const char *cmd_name,
  177. long i, void *p, void (*f) (void), int cmd_optional)
  178. {
  179. int num;
  180. if (e == NULL || cmd_name == NULL) {
  181. ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
  182. return 0;
  183. }
  184. if (e->ctrl == NULL
  185. || (num = ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FROM_NAME,
  186. 0, (void *)cmd_name, NULL)) <= 0) {
  187. /*
  188. * If the command didn't *have* to be supported, we fake success.
  189. * This allows certain settings to be specified for multiple ENGINEs
  190. * and only require a change of ENGINE id (without having to
  191. * selectively apply settings). Eg. changing from a hardware device
  192. * back to the regular software ENGINE without editing the config
  193. * file, etc.
  194. */
  195. if (cmd_optional) {
  196. ERR_clear_error();
  197. return 1;
  198. }
  199. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INVALID_CMD_NAME);
  200. return 0;
  201. }
  202. /*
  203. * Force the result of the control command to 0 or 1, for the reasons
  204. * mentioned before.
  205. */
  206. if (ENGINE_ctrl(e, num, i, p, f) > 0)
  207. return 1;
  208. return 0;
  209. }
  210. int ENGINE_ctrl_cmd_string(ENGINE *e, const char *cmd_name, const char *arg,
  211. int cmd_optional)
  212. {
  213. int num, flags;
  214. long l;
  215. char *ptr;
  216. if (e == NULL || cmd_name == NULL) {
  217. ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
  218. return 0;
  219. }
  220. if (e->ctrl == NULL
  221. || (num = ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FROM_NAME,
  222. 0, (void *)cmd_name, NULL)) <= 0) {
  223. /*
  224. * If the command didn't *have* to be supported, we fake success.
  225. * This allows certain settings to be specified for multiple ENGINEs
  226. * and only require a change of ENGINE id (without having to
  227. * selectively apply settings). Eg. changing from a hardware device
  228. * back to the regular software ENGINE without editing the config
  229. * file, etc.
  230. */
  231. if (cmd_optional) {
  232. ERR_clear_error();
  233. return 1;
  234. }
  235. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INVALID_CMD_NAME);
  236. return 0;
  237. }
  238. if (!ENGINE_cmd_is_executable(e, num)) {
  239. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_CMD_NOT_EXECUTABLE);
  240. return 0;
  241. }
  242. flags = ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FLAGS, num, NULL, NULL);
  243. if (flags < 0) {
  244. /*
  245. * Shouldn't happen, given that ENGINE_cmd_is_executable() returned
  246. * success.
  247. */
  248. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INTERNAL_LIST_ERROR);
  249. return 0;
  250. }
  251. /*
  252. * If the command takes no input, there must be no input. And vice versa.
  253. */
  254. if (flags & ENGINE_CMD_FLAG_NO_INPUT) {
  255. if (arg != NULL) {
  256. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_COMMAND_TAKES_NO_INPUT);
  257. return 0;
  258. }
  259. /*
  260. * We deliberately force the result of ENGINE_ctrl() to 0 or 1 rather
  261. * than returning it as "return data". This is to ensure usage of
  262. * these commands is consistent across applications and that certain
  263. * applications don't understand it one way, and others another.
  264. */
  265. if (ENGINE_ctrl(e, num, 0, (void *)arg, NULL) > 0)
  266. return 1;
  267. return 0;
  268. }
  269. /* So, we require input */
  270. if (arg == NULL) {
  271. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_COMMAND_TAKES_INPUT);
  272. return 0;
  273. }
  274. /* If it takes string input, that's easy */
  275. if (flags & ENGINE_CMD_FLAG_STRING) {
  276. /* Same explanation as above */
  277. if (ENGINE_ctrl(e, num, 0, (void *)arg, NULL) > 0)
  278. return 1;
  279. return 0;
  280. }
  281. /*
  282. * If it doesn't take numeric either, then it is unsupported for use in a
  283. * config-setting situation, which is what this function is for. This
  284. * should never happen though, because ENGINE_cmd_is_executable() was
  285. * used.
  286. */
  287. if (!(flags & ENGINE_CMD_FLAG_NUMERIC)) {
  288. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INTERNAL_LIST_ERROR);
  289. return 0;
  290. }
  291. l = strtol(arg, &ptr, 10);
  292. if ((arg == ptr) || (*ptr != '\0')) {
  293. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_ARGUMENT_IS_NOT_A_NUMBER);
  294. return 0;
  295. }
  296. /*
  297. * Force the result of the control command to 0 or 1, for the reasons
  298. * mentioned before.
  299. */
  300. if (ENGINE_ctrl(e, num, l, NULL, NULL) > 0)
  301. return 1;
  302. return 0;
  303. }