eng_ctrl.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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) || (cmd == ENGINE_CTRL_GET_NAME_FROM_CMD) || (cmd == ENGINE_CTRL_GET_DESC_FROM_CMD)) {
  70. if (s == NULL) {
  71. ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
  72. return -1;
  73. }
  74. }
  75. /* Now handle cmd_name -> cmd_num conversion */
  76. if (cmd == ENGINE_CTRL_GET_CMD_FROM_NAME) {
  77. if ((e->cmd_defns == NULL)
  78. || ((idx = int_ctrl_cmd_by_name(e->cmd_defns, s)) < 0)) {
  79. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INVALID_CMD_NAME);
  80. return -1;
  81. }
  82. return e->cmd_defns[idx].cmd_num;
  83. }
  84. /*
  85. * For the rest of the commands, the 'long' argument must specify a valid
  86. * command number - so we need to conduct a search.
  87. */
  88. if ((e->cmd_defns == NULL)
  89. || ((idx = int_ctrl_cmd_by_num(e->cmd_defns, (unsigned int)i)) < 0)) {
  90. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INVALID_CMD_NUMBER);
  91. return -1;
  92. }
  93. /* Now the logic splits depending on command type */
  94. cdp = &e->cmd_defns[idx];
  95. switch (cmd) {
  96. case ENGINE_CTRL_GET_NEXT_CMD_TYPE:
  97. cdp++;
  98. return int_ctrl_cmd_is_null(cdp) ? 0 : cdp->cmd_num;
  99. case ENGINE_CTRL_GET_NAME_LEN_FROM_CMD:
  100. return strlen(cdp->cmd_name);
  101. case ENGINE_CTRL_GET_NAME_FROM_CMD:
  102. return strlen(strcpy(s, cdp->cmd_name));
  103. case ENGINE_CTRL_GET_DESC_LEN_FROM_CMD:
  104. return strlen(cdp->cmd_desc == NULL ? int_no_description
  105. : cdp->cmd_desc);
  106. case ENGINE_CTRL_GET_DESC_FROM_CMD:
  107. return strlen(strcpy(s, cdp->cmd_desc == NULL ? int_no_description : cdp->cmd_desc));
  108. case ENGINE_CTRL_GET_CMD_FLAGS:
  109. return cdp->cmd_flags;
  110. }
  111. /* Shouldn't really be here ... */
  112. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INTERNAL_LIST_ERROR);
  113. return -1;
  114. }
  115. int ENGINE_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void))
  116. {
  117. int ctrl_exists;
  118. if (e == NULL) {
  119. ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
  120. return 0;
  121. }
  122. ctrl_exists = ((e->ctrl == NULL) ? 0 : 1);
  123. /*
  124. * Intercept any "root-level" commands before trying to hand them on to
  125. * ctrl() handlers.
  126. */
  127. switch (cmd) {
  128. case ENGINE_CTRL_HAS_CTRL_FUNCTION:
  129. return ctrl_exists;
  130. case ENGINE_CTRL_GET_FIRST_CMD_TYPE:
  131. case ENGINE_CTRL_GET_NEXT_CMD_TYPE:
  132. case ENGINE_CTRL_GET_CMD_FROM_NAME:
  133. case ENGINE_CTRL_GET_NAME_LEN_FROM_CMD:
  134. case ENGINE_CTRL_GET_NAME_FROM_CMD:
  135. case ENGINE_CTRL_GET_DESC_LEN_FROM_CMD:
  136. case ENGINE_CTRL_GET_DESC_FROM_CMD:
  137. case ENGINE_CTRL_GET_CMD_FLAGS:
  138. if (ctrl_exists && !(e->flags & ENGINE_FLAGS_MANUAL_CMD_CTRL))
  139. return int_ctrl_helper(e, cmd, i, p, f);
  140. if (!ctrl_exists) {
  141. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_NO_CONTROL_FUNCTION);
  142. /*
  143. * For these cmd-related functions, failure is indicated by a -1
  144. * return value (because 0 is used as a valid return in some
  145. * places).
  146. */
  147. return -1;
  148. }
  149. default:
  150. break;
  151. }
  152. /* Anything else requires a ctrl() handler to exist. */
  153. if (!ctrl_exists) {
  154. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_NO_CONTROL_FUNCTION);
  155. return 0;
  156. }
  157. return e->ctrl(e, cmd, i, p, f);
  158. }
  159. int ENGINE_cmd_is_executable(ENGINE *e, int cmd)
  160. {
  161. int flags;
  162. if ((flags = ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FLAGS, cmd, NULL, NULL)) < 0) {
  163. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INVALID_CMD_NUMBER);
  164. return 0;
  165. }
  166. if (!(flags & ENGINE_CMD_FLAG_NO_INPUT) && !(flags & ENGINE_CMD_FLAG_NUMERIC) && !(flags & ENGINE_CMD_FLAG_STRING))
  167. return 0;
  168. return 1;
  169. }
  170. int ENGINE_ctrl_cmd(ENGINE *e, const char *cmd_name,
  171. long i, void *p, void (*f)(void), int cmd_optional)
  172. {
  173. int num;
  174. if (e == NULL || cmd_name == NULL) {
  175. ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
  176. return 0;
  177. }
  178. if (e->ctrl == NULL
  179. || (num = ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FROM_NAME,
  180. 0, (void *)cmd_name, NULL))
  181. <= 0) {
  182. /*
  183. * If the command didn't *have* to be supported, we fake success.
  184. * This allows certain settings to be specified for multiple ENGINEs
  185. * and only require a change of ENGINE id (without having to
  186. * selectively apply settings). Eg. changing from a hardware device
  187. * back to the regular software ENGINE without editing the config
  188. * file, etc.
  189. */
  190. if (cmd_optional) {
  191. ERR_clear_error();
  192. return 1;
  193. }
  194. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INVALID_CMD_NAME);
  195. return 0;
  196. }
  197. /*
  198. * Force the result of the control command to 0 or 1, for the reasons
  199. * mentioned before.
  200. */
  201. if (ENGINE_ctrl(e, num, i, p, f) > 0)
  202. return 1;
  203. return 0;
  204. }
  205. int ENGINE_ctrl_cmd_string(ENGINE *e, const char *cmd_name, const char *arg,
  206. int cmd_optional)
  207. {
  208. int num, flags;
  209. long l;
  210. char *ptr;
  211. if (e == NULL || cmd_name == NULL) {
  212. ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
  213. return 0;
  214. }
  215. if (e->ctrl == NULL
  216. || (num = ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FROM_NAME,
  217. 0, (void *)cmd_name, NULL))
  218. <= 0) {
  219. /*
  220. * If the command didn't *have* to be supported, we fake success.
  221. * This allows certain settings to be specified for multiple ENGINEs
  222. * and only require a change of ENGINE id (without having to
  223. * selectively apply settings). Eg. changing from a hardware device
  224. * back to the regular software ENGINE without editing the config
  225. * file, etc.
  226. */
  227. if (cmd_optional) {
  228. ERR_clear_error();
  229. return 1;
  230. }
  231. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INVALID_CMD_NAME);
  232. return 0;
  233. }
  234. if (!ENGINE_cmd_is_executable(e, num)) {
  235. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_CMD_NOT_EXECUTABLE);
  236. return 0;
  237. }
  238. flags = ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FLAGS, num, NULL, NULL);
  239. if (flags < 0) {
  240. /*
  241. * Shouldn't happen, given that ENGINE_cmd_is_executable() returned
  242. * success.
  243. */
  244. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INTERNAL_LIST_ERROR);
  245. return 0;
  246. }
  247. /*
  248. * If the command takes no input, there must be no input. And vice versa.
  249. */
  250. if (flags & ENGINE_CMD_FLAG_NO_INPUT) {
  251. if (arg != NULL) {
  252. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_COMMAND_TAKES_NO_INPUT);
  253. return 0;
  254. }
  255. /*
  256. * We deliberately force the result of ENGINE_ctrl() to 0 or 1 rather
  257. * than returning it as "return data". This is to ensure usage of
  258. * these commands is consistent across applications and that certain
  259. * applications don't understand it one way, and others another.
  260. */
  261. if (ENGINE_ctrl(e, num, 0, (void *)arg, NULL) > 0)
  262. return 1;
  263. return 0;
  264. }
  265. /* So, we require input */
  266. if (arg == NULL) {
  267. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_COMMAND_TAKES_INPUT);
  268. return 0;
  269. }
  270. /* If it takes string input, that's easy */
  271. if (flags & ENGINE_CMD_FLAG_STRING) {
  272. /* Same explanation as above */
  273. if (ENGINE_ctrl(e, num, 0, (void *)arg, NULL) > 0)
  274. return 1;
  275. return 0;
  276. }
  277. /*
  278. * If it doesn't take numeric either, then it is unsupported for use in a
  279. * config-setting situation, which is what this function is for. This
  280. * should never happen though, because ENGINE_cmd_is_executable() was
  281. * used.
  282. */
  283. if (!(flags & ENGINE_CMD_FLAG_NUMERIC)) {
  284. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INTERNAL_LIST_ERROR);
  285. return 0;
  286. }
  287. l = strtol(arg, &ptr, 10);
  288. if ((arg == ptr) || (*ptr != '\0')) {
  289. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_ARGUMENT_IS_NOT_A_NUMBER);
  290. return 0;
  291. }
  292. /*
  293. * Force the result of the control command to 0 or 1, for the reasons
  294. * mentioned before.
  295. */
  296. if (ENGINE_ctrl(e, num, l, NULL, NULL) > 0)
  297. return 1;
  298. return 0;
  299. }