eng_ctrl.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * Copyright 2001-2021 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, ref_exists;
  121. if (e == NULL) {
  122. ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
  123. return 0;
  124. }
  125. if (!CRYPTO_THREAD_write_lock(global_engine_lock))
  126. return 0;
  127. ref_exists = ((e->struct_ref > 0) ? 1 : 0);
  128. CRYPTO_THREAD_unlock(global_engine_lock);
  129. ctrl_exists = ((e->ctrl == NULL) ? 0 : 1);
  130. if (!ref_exists) {
  131. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_NO_REFERENCE);
  132. return 0;
  133. }
  134. /*
  135. * Intercept any "root-level" commands before trying to hand them on to
  136. * ctrl() handlers.
  137. */
  138. switch (cmd) {
  139. case ENGINE_CTRL_HAS_CTRL_FUNCTION:
  140. return ctrl_exists;
  141. case ENGINE_CTRL_GET_FIRST_CMD_TYPE:
  142. case ENGINE_CTRL_GET_NEXT_CMD_TYPE:
  143. case ENGINE_CTRL_GET_CMD_FROM_NAME:
  144. case ENGINE_CTRL_GET_NAME_LEN_FROM_CMD:
  145. case ENGINE_CTRL_GET_NAME_FROM_CMD:
  146. case ENGINE_CTRL_GET_DESC_LEN_FROM_CMD:
  147. case ENGINE_CTRL_GET_DESC_FROM_CMD:
  148. case ENGINE_CTRL_GET_CMD_FLAGS:
  149. if (ctrl_exists && !(e->flags & ENGINE_FLAGS_MANUAL_CMD_CTRL))
  150. return int_ctrl_helper(e, cmd, i, p, f);
  151. if (!ctrl_exists) {
  152. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_NO_CONTROL_FUNCTION);
  153. /*
  154. * For these cmd-related functions, failure is indicated by a -1
  155. * return value (because 0 is used as a valid return in some
  156. * places).
  157. */
  158. return -1;
  159. }
  160. default:
  161. break;
  162. }
  163. /* Anything else requires a ctrl() handler to exist. */
  164. if (!ctrl_exists) {
  165. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_NO_CONTROL_FUNCTION);
  166. return 0;
  167. }
  168. return e->ctrl(e, cmd, i, p, f);
  169. }
  170. int ENGINE_cmd_is_executable(ENGINE *e, int cmd)
  171. {
  172. int flags;
  173. if ((flags =
  174. ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FLAGS, cmd, NULL, NULL)) < 0) {
  175. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INVALID_CMD_NUMBER);
  176. return 0;
  177. }
  178. if (!(flags & ENGINE_CMD_FLAG_NO_INPUT) &&
  179. !(flags & ENGINE_CMD_FLAG_NUMERIC) &&
  180. !(flags & ENGINE_CMD_FLAG_STRING))
  181. return 0;
  182. return 1;
  183. }
  184. int ENGINE_ctrl_cmd(ENGINE *e, const char *cmd_name,
  185. long i, void *p, void (*f) (void), int cmd_optional)
  186. {
  187. int num;
  188. if (e == NULL || cmd_name == NULL) {
  189. ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
  190. return 0;
  191. }
  192. if (e->ctrl == NULL
  193. || (num = ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FROM_NAME,
  194. 0, (void *)cmd_name, NULL)) <= 0) {
  195. /*
  196. * If the command didn't *have* to be supported, we fake success.
  197. * This allows certain settings to be specified for multiple ENGINEs
  198. * and only require a change of ENGINE id (without having to
  199. * selectively apply settings). Eg. changing from a hardware device
  200. * back to the regular software ENGINE without editing the config
  201. * file, etc.
  202. */
  203. if (cmd_optional) {
  204. ERR_clear_error();
  205. return 1;
  206. }
  207. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INVALID_CMD_NAME);
  208. return 0;
  209. }
  210. /*
  211. * Force the result of the control command to 0 or 1, for the reasons
  212. * mentioned before.
  213. */
  214. if (ENGINE_ctrl(e, num, i, p, f) > 0)
  215. return 1;
  216. return 0;
  217. }
  218. int ENGINE_ctrl_cmd_string(ENGINE *e, const char *cmd_name, const char *arg,
  219. int cmd_optional)
  220. {
  221. int num, flags;
  222. long l;
  223. char *ptr;
  224. if (e == NULL || cmd_name == NULL) {
  225. ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
  226. return 0;
  227. }
  228. if (e->ctrl == NULL
  229. || (num = ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FROM_NAME,
  230. 0, (void *)cmd_name, NULL)) <= 0) {
  231. /*
  232. * If the command didn't *have* to be supported, we fake success.
  233. * This allows certain settings to be specified for multiple ENGINEs
  234. * and only require a change of ENGINE id (without having to
  235. * selectively apply settings). Eg. changing from a hardware device
  236. * back to the regular software ENGINE without editing the config
  237. * file, etc.
  238. */
  239. if (cmd_optional) {
  240. ERR_clear_error();
  241. return 1;
  242. }
  243. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INVALID_CMD_NAME);
  244. return 0;
  245. }
  246. if (!ENGINE_cmd_is_executable(e, num)) {
  247. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_CMD_NOT_EXECUTABLE);
  248. return 0;
  249. }
  250. flags = ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FLAGS, num, NULL, NULL);
  251. if (flags < 0) {
  252. /*
  253. * Shouldn't happen, given that ENGINE_cmd_is_executable() returned
  254. * success.
  255. */
  256. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INTERNAL_LIST_ERROR);
  257. return 0;
  258. }
  259. /*
  260. * If the command takes no input, there must be no input. And vice versa.
  261. */
  262. if (flags & ENGINE_CMD_FLAG_NO_INPUT) {
  263. if (arg != NULL) {
  264. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_COMMAND_TAKES_NO_INPUT);
  265. return 0;
  266. }
  267. /*
  268. * We deliberately force the result of ENGINE_ctrl() to 0 or 1 rather
  269. * than returning it as "return data". This is to ensure usage of
  270. * these commands is consistent across applications and that certain
  271. * applications don't understand it one way, and others another.
  272. */
  273. if (ENGINE_ctrl(e, num, 0, (void *)arg, NULL) > 0)
  274. return 1;
  275. return 0;
  276. }
  277. /* So, we require input */
  278. if (arg == NULL) {
  279. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_COMMAND_TAKES_INPUT);
  280. return 0;
  281. }
  282. /* If it takes string input, that's easy */
  283. if (flags & ENGINE_CMD_FLAG_STRING) {
  284. /* Same explanation as above */
  285. if (ENGINE_ctrl(e, num, 0, (void *)arg, NULL) > 0)
  286. return 1;
  287. return 0;
  288. }
  289. /*
  290. * If it doesn't take numeric either, then it is unsupported for use in a
  291. * config-setting situation, which is what this function is for. This
  292. * should never happen though, because ENGINE_cmd_is_executable() was
  293. * used.
  294. */
  295. if (!(flags & ENGINE_CMD_FLAG_NUMERIC)) {
  296. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INTERNAL_LIST_ERROR);
  297. return 0;
  298. }
  299. l = strtol(arg, &ptr, 10);
  300. if ((arg == ptr) || (*ptr != '\0')) {
  301. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_ARGUMENT_IS_NOT_A_NUMBER);
  302. return 0;
  303. }
  304. /*
  305. * Force the result of the control command to 0 or 1, for the reasons
  306. * mentioned before.
  307. */
  308. if (ENGINE_ctrl(e, num, l, NULL, NULL) > 0)
  309. return 1;
  310. return 0;
  311. }