eng_ctrl.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * Copyright 2001-2016 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. #include "eng_local.h"
  10. /*
  11. * When querying a ENGINE-specific control command's 'description', this
  12. * string is used if the ENGINE_CMD_DEFN has cmd_desc set to NULL.
  13. */
  14. static const char *int_no_description = "";
  15. /*
  16. * These internal functions handle 'CMD'-related control commands when the
  17. * ENGINE in question has asked us to take care of it (ie. the ENGINE did not
  18. * set the ENGINE_FLAGS_MANUAL_CMD_CTRL flag.
  19. */
  20. static int int_ctrl_cmd_is_null(const ENGINE_CMD_DEFN *defn)
  21. {
  22. if ((defn->cmd_num == 0) || (defn->cmd_name == NULL))
  23. return 1;
  24. return 0;
  25. }
  26. static int int_ctrl_cmd_by_name(const ENGINE_CMD_DEFN *defn, const char *s)
  27. {
  28. int idx = 0;
  29. while (!int_ctrl_cmd_is_null(defn) && (strcmp(defn->cmd_name, s) != 0)) {
  30. idx++;
  31. defn++;
  32. }
  33. if (int_ctrl_cmd_is_null(defn))
  34. /* The given name wasn't found */
  35. return -1;
  36. return idx;
  37. }
  38. static int int_ctrl_cmd_by_num(const ENGINE_CMD_DEFN *defn, unsigned int num)
  39. {
  40. int idx = 0;
  41. /*
  42. * NB: It is stipulated that 'cmd_defn' lists are ordered by cmd_num. So
  43. * our searches don't need to take any longer than necessary.
  44. */
  45. while (!int_ctrl_cmd_is_null(defn) && (defn->cmd_num < num)) {
  46. idx++;
  47. defn++;
  48. }
  49. if (defn->cmd_num == num)
  50. return idx;
  51. /* The given cmd_num wasn't found */
  52. return -1;
  53. }
  54. static int int_ctrl_helper(ENGINE *e, int cmd, long i, void *p,
  55. void (*f) (void))
  56. {
  57. int idx;
  58. char *s = (char *)p;
  59. const ENGINE_CMD_DEFN *cdp;
  60. /* Take care of the easy one first (eg. it requires no searches) */
  61. if (cmd == ENGINE_CTRL_GET_FIRST_CMD_TYPE) {
  62. if ((e->cmd_defns == NULL) || int_ctrl_cmd_is_null(e->cmd_defns))
  63. return 0;
  64. return e->cmd_defns->cmd_num;
  65. }
  66. /* One or two commands require that "p" be a valid string buffer */
  67. if ((cmd == ENGINE_CTRL_GET_CMD_FROM_NAME) ||
  68. (cmd == ENGINE_CTRL_GET_NAME_FROM_CMD) ||
  69. (cmd == ENGINE_CTRL_GET_DESC_FROM_CMD)) {
  70. if (s == NULL) {
  71. ENGINEerr(ENGINE_F_INT_CTRL_HELPER, 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. ENGINEerr(ENGINE_F_INT_CTRL_HELPER, 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. ENGINEerr(ENGINE_F_INT_CTRL_HELPER, 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
  108. : cdp->cmd_desc));
  109. case ENGINE_CTRL_GET_CMD_FLAGS:
  110. return cdp->cmd_flags;
  111. }
  112. /* Shouldn't really be here ... */
  113. ENGINEerr(ENGINE_F_INT_CTRL_HELPER, ENGINE_R_INTERNAL_LIST_ERROR);
  114. return -1;
  115. }
  116. int ENGINE_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
  117. {
  118. int ctrl_exists, ref_exists;
  119. if (e == NULL) {
  120. ENGINEerr(ENGINE_F_ENGINE_CTRL, ERR_R_PASSED_NULL_PARAMETER);
  121. return 0;
  122. }
  123. CRYPTO_THREAD_write_lock(global_engine_lock);
  124. ref_exists = ((e->struct_ref > 0) ? 1 : 0);
  125. CRYPTO_THREAD_unlock(global_engine_lock);
  126. ctrl_exists = ((e->ctrl == NULL) ? 0 : 1);
  127. if (!ref_exists) {
  128. ENGINEerr(ENGINE_F_ENGINE_CTRL, ENGINE_R_NO_REFERENCE);
  129. return 0;
  130. }
  131. /*
  132. * Intercept any "root-level" commands before trying to hand them on to
  133. * ctrl() handlers.
  134. */
  135. switch (cmd) {
  136. case ENGINE_CTRL_HAS_CTRL_FUNCTION:
  137. return ctrl_exists;
  138. case ENGINE_CTRL_GET_FIRST_CMD_TYPE:
  139. case ENGINE_CTRL_GET_NEXT_CMD_TYPE:
  140. case ENGINE_CTRL_GET_CMD_FROM_NAME:
  141. case ENGINE_CTRL_GET_NAME_LEN_FROM_CMD:
  142. case ENGINE_CTRL_GET_NAME_FROM_CMD:
  143. case ENGINE_CTRL_GET_DESC_LEN_FROM_CMD:
  144. case ENGINE_CTRL_GET_DESC_FROM_CMD:
  145. case ENGINE_CTRL_GET_CMD_FLAGS:
  146. if (ctrl_exists && !(e->flags & ENGINE_FLAGS_MANUAL_CMD_CTRL))
  147. return int_ctrl_helper(e, cmd, i, p, f);
  148. if (!ctrl_exists) {
  149. ENGINEerr(ENGINE_F_ENGINE_CTRL, ENGINE_R_NO_CONTROL_FUNCTION);
  150. /*
  151. * For these cmd-related functions, failure is indicated by a -1
  152. * return value (because 0 is used as a valid return in some
  153. * places).
  154. */
  155. return -1;
  156. }
  157. default:
  158. break;
  159. }
  160. /* Anything else requires a ctrl() handler to exist. */
  161. if (!ctrl_exists) {
  162. ENGINEerr(ENGINE_F_ENGINE_CTRL, ENGINE_R_NO_CONTROL_FUNCTION);
  163. return 0;
  164. }
  165. return e->ctrl(e, cmd, i, p, f);
  166. }
  167. int ENGINE_cmd_is_executable(ENGINE *e, int cmd)
  168. {
  169. int flags;
  170. if ((flags =
  171. ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FLAGS, cmd, NULL, NULL)) < 0) {
  172. ENGINEerr(ENGINE_F_ENGINE_CMD_IS_EXECUTABLE,
  173. ENGINE_R_INVALID_CMD_NUMBER);
  174. return 0;
  175. }
  176. if (!(flags & ENGINE_CMD_FLAG_NO_INPUT) &&
  177. !(flags & ENGINE_CMD_FLAG_NUMERIC) &&
  178. !(flags & ENGINE_CMD_FLAG_STRING))
  179. return 0;
  180. return 1;
  181. }
  182. int ENGINE_ctrl_cmd(ENGINE *e, const char *cmd_name,
  183. long i, void *p, void (*f) (void), int cmd_optional)
  184. {
  185. int num;
  186. if (e == NULL || cmd_name == NULL) {
  187. ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD, ERR_R_PASSED_NULL_PARAMETER);
  188. return 0;
  189. }
  190. if (e->ctrl == NULL
  191. || (num = ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FROM_NAME,
  192. 0, (void *)cmd_name, NULL)) <= 0) {
  193. /*
  194. * If the command didn't *have* to be supported, we fake success.
  195. * This allows certain settings to be specified for multiple ENGINEs
  196. * and only require a change of ENGINE id (without having to
  197. * selectively apply settings). Eg. changing from a hardware device
  198. * back to the regular software ENGINE without editing the config
  199. * file, etc.
  200. */
  201. if (cmd_optional) {
  202. ERR_clear_error();
  203. return 1;
  204. }
  205. ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD, ENGINE_R_INVALID_CMD_NAME);
  206. return 0;
  207. }
  208. /*
  209. * Force the result of the control command to 0 or 1, for the reasons
  210. * mentioned before.
  211. */
  212. if (ENGINE_ctrl(e, num, i, p, f) > 0)
  213. return 1;
  214. return 0;
  215. }
  216. int ENGINE_ctrl_cmd_string(ENGINE *e, const char *cmd_name, const char *arg,
  217. int cmd_optional)
  218. {
  219. int num, flags;
  220. long l;
  221. char *ptr;
  222. if (e == NULL || cmd_name == NULL) {
  223. ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING, ERR_R_PASSED_NULL_PARAMETER);
  224. return 0;
  225. }
  226. if (e->ctrl == NULL
  227. || (num = ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FROM_NAME,
  228. 0, (void *)cmd_name, NULL)) <= 0) {
  229. /*
  230. * If the command didn't *have* to be supported, we fake success.
  231. * This allows certain settings to be specified for multiple ENGINEs
  232. * and only require a change of ENGINE id (without having to
  233. * selectively apply settings). Eg. changing from a hardware device
  234. * back to the regular software ENGINE without editing the config
  235. * file, etc.
  236. */
  237. if (cmd_optional) {
  238. ERR_clear_error();
  239. return 1;
  240. }
  241. ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING, ENGINE_R_INVALID_CMD_NAME);
  242. return 0;
  243. }
  244. if (!ENGINE_cmd_is_executable(e, num)) {
  245. ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
  246. ENGINE_R_CMD_NOT_EXECUTABLE);
  247. return 0;
  248. }
  249. flags = ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FLAGS, num, NULL, NULL);
  250. if (flags < 0) {
  251. /*
  252. * Shouldn't happen, given that ENGINE_cmd_is_executable() returned
  253. * success.
  254. */
  255. ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
  256. 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. ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
  265. ENGINE_R_COMMAND_TAKES_NO_INPUT);
  266. return 0;
  267. }
  268. /*
  269. * We deliberately force the result of ENGINE_ctrl() to 0 or 1 rather
  270. * than returning it as "return data". This is to ensure usage of
  271. * these commands is consistent across applications and that certain
  272. * applications don't understand it one way, and others another.
  273. */
  274. if (ENGINE_ctrl(e, num, 0, (void *)arg, NULL) > 0)
  275. return 1;
  276. return 0;
  277. }
  278. /* So, we require input */
  279. if (arg == NULL) {
  280. ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
  281. ENGINE_R_COMMAND_TAKES_INPUT);
  282. return 0;
  283. }
  284. /* If it takes string input, that's easy */
  285. if (flags & ENGINE_CMD_FLAG_STRING) {
  286. /* Same explanation as above */
  287. if (ENGINE_ctrl(e, num, 0, (void *)arg, NULL) > 0)
  288. return 1;
  289. return 0;
  290. }
  291. /*
  292. * If it doesn't take numeric either, then it is unsupported for use in a
  293. * config-setting situation, which is what this function is for. This
  294. * should never happen though, because ENGINE_cmd_is_executable() was
  295. * used.
  296. */
  297. if (!(flags & ENGINE_CMD_FLAG_NUMERIC)) {
  298. ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
  299. ENGINE_R_INTERNAL_LIST_ERROR);
  300. return 0;
  301. }
  302. l = strtol(arg, &ptr, 10);
  303. if ((arg == ptr) || (*ptr != '\0')) {
  304. ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
  305. ENGINE_R_ARGUMENT_IS_NOT_A_NUMBER);
  306. return 0;
  307. }
  308. /*
  309. * Force the result of the control command to 0 or 1, for the reasons
  310. * mentioned before.
  311. */
  312. if (ENGINE_ctrl(e, num, l, NULL, NULL) > 0)
  313. return 1;
  314. return 0;
  315. }