eng_ctrl.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. * Copyright 2001-2020 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. ENGINEerr(ENGINE_F_INT_CTRL_HELPER, 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. ENGINEerr(ENGINE_F_INT_CTRL_HELPER, 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. ENGINEerr(ENGINE_F_INT_CTRL_HELPER, 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. ENGINEerr(ENGINE_F_INT_CTRL_HELPER, 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. ENGINEerr(ENGINE_F_ENGINE_CTRL, ERR_R_PASSED_NULL_PARAMETER);
  123. return 0;
  124. }
  125. CRYPTO_THREAD_write_lock(global_engine_lock);
  126. ref_exists = ((e->struct_ref > 0) ? 1 : 0);
  127. CRYPTO_THREAD_unlock(global_engine_lock);
  128. ctrl_exists = ((e->ctrl == NULL) ? 0 : 1);
  129. if (!ref_exists) {
  130. ENGINEerr(ENGINE_F_ENGINE_CTRL, ENGINE_R_NO_REFERENCE);
  131. return 0;
  132. }
  133. /*
  134. * Intercept any "root-level" commands before trying to hand them on to
  135. * ctrl() handlers.
  136. */
  137. switch (cmd) {
  138. case ENGINE_CTRL_HAS_CTRL_FUNCTION:
  139. return ctrl_exists;
  140. case ENGINE_CTRL_GET_FIRST_CMD_TYPE:
  141. case ENGINE_CTRL_GET_NEXT_CMD_TYPE:
  142. case ENGINE_CTRL_GET_CMD_FROM_NAME:
  143. case ENGINE_CTRL_GET_NAME_LEN_FROM_CMD:
  144. case ENGINE_CTRL_GET_NAME_FROM_CMD:
  145. case ENGINE_CTRL_GET_DESC_LEN_FROM_CMD:
  146. case ENGINE_CTRL_GET_DESC_FROM_CMD:
  147. case ENGINE_CTRL_GET_CMD_FLAGS:
  148. if (ctrl_exists && !(e->flags & ENGINE_FLAGS_MANUAL_CMD_CTRL))
  149. return int_ctrl_helper(e, cmd, i, p, f);
  150. if (!ctrl_exists) {
  151. ENGINEerr(ENGINE_F_ENGINE_CTRL, ENGINE_R_NO_CONTROL_FUNCTION);
  152. /*
  153. * For these cmd-related functions, failure is indicated by a -1
  154. * return value (because 0 is used as a valid return in some
  155. * places).
  156. */
  157. return -1;
  158. }
  159. default:
  160. break;
  161. }
  162. /* Anything else requires a ctrl() handler to exist. */
  163. if (!ctrl_exists) {
  164. ENGINEerr(ENGINE_F_ENGINE_CTRL, ENGINE_R_NO_CONTROL_FUNCTION);
  165. return 0;
  166. }
  167. return e->ctrl(e, cmd, i, p, f);
  168. }
  169. int ENGINE_cmd_is_executable(ENGINE *e, int cmd)
  170. {
  171. int flags;
  172. if ((flags =
  173. ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FLAGS, cmd, NULL, NULL)) < 0) {
  174. ENGINEerr(ENGINE_F_ENGINE_CMD_IS_EXECUTABLE,
  175. 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. ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD, 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. ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD, 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. ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING, 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. ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING, ENGINE_R_INVALID_CMD_NAME);
  244. return 0;
  245. }
  246. if (!ENGINE_cmd_is_executable(e, num)) {
  247. ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
  248. ENGINE_R_CMD_NOT_EXECUTABLE);
  249. return 0;
  250. }
  251. flags = ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FLAGS, num, NULL, NULL);
  252. if (flags < 0) {
  253. /*
  254. * Shouldn't happen, given that ENGINE_cmd_is_executable() returned
  255. * success.
  256. */
  257. ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
  258. ENGINE_R_INTERNAL_LIST_ERROR);
  259. return 0;
  260. }
  261. /*
  262. * If the command takes no input, there must be no input. And vice versa.
  263. */
  264. if (flags & ENGINE_CMD_FLAG_NO_INPUT) {
  265. if (arg != NULL) {
  266. ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
  267. ENGINE_R_COMMAND_TAKES_NO_INPUT);
  268. return 0;
  269. }
  270. /*
  271. * We deliberately force the result of ENGINE_ctrl() to 0 or 1 rather
  272. * than returning it as "return data". This is to ensure usage of
  273. * these commands is consistent across applications and that certain
  274. * applications don't understand it one way, and others another.
  275. */
  276. if (ENGINE_ctrl(e, num, 0, (void *)arg, NULL) > 0)
  277. return 1;
  278. return 0;
  279. }
  280. /* So, we require input */
  281. if (arg == NULL) {
  282. ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
  283. ENGINE_R_COMMAND_TAKES_INPUT);
  284. return 0;
  285. }
  286. /* If it takes string input, that's easy */
  287. if (flags & ENGINE_CMD_FLAG_STRING) {
  288. /* Same explanation as above */
  289. if (ENGINE_ctrl(e, num, 0, (void *)arg, NULL) > 0)
  290. return 1;
  291. return 0;
  292. }
  293. /*
  294. * If it doesn't take numeric either, then it is unsupported for use in a
  295. * config-setting situation, which is what this function is for. This
  296. * should never happen though, because ENGINE_cmd_is_executable() was
  297. * used.
  298. */
  299. if (!(flags & ENGINE_CMD_FLAG_NUMERIC)) {
  300. ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
  301. ENGINE_R_INTERNAL_LIST_ERROR);
  302. return 0;
  303. }
  304. l = strtol(arg, &ptr, 10);
  305. if ((arg == ptr) || (*ptr != '\0')) {
  306. ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
  307. ENGINE_R_ARGUMENT_IS_NOT_A_NUMBER);
  308. return 0;
  309. }
  310. /*
  311. * Force the result of the control command to 0 or 1, for the reasons
  312. * mentioned before.
  313. */
  314. if (ENGINE_ctrl(e, num, l, NULL, NULL) > 0)
  315. return 1;
  316. return 0;
  317. }