engine.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /*
  2. * Written by Richard Levitte <richard@levitte.org> for the OpenSSL project
  3. * 2000.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 2000 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * licensing@OpenSSL.org.
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. *
  53. * This product includes cryptographic software written by Eric Young
  54. * (eay@cryptsoft.com). This product includes software written by Tim
  55. * Hudson (tjh@cryptsoft.com).
  56. *
  57. */
  58. #include <stdio.h>
  59. #include <stdlib.h>
  60. #include <string.h>
  61. #include "apps.h"
  62. #include <openssl/err.h>
  63. #ifndef OPENSSL_NO_ENGINE
  64. # include <openssl/engine.h>
  65. # include <openssl/ssl.h>
  66. typedef enum OPTION_choice {
  67. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  68. OPT_C, OPT_T, OPT_TT, OPT_PRE, OPT_POST,
  69. OPT_V = 100, OPT_VV, OPT_VVV, OPT_VVVV
  70. } OPTION_CHOICE;
  71. OPTIONS engine_options[] = {
  72. {"help", OPT_HELP, '-', "Display this summary"},
  73. {"vvvv", OPT_VVVV, '-', "Also show internal input flags"},
  74. {"vvv", OPT_VVV, '-', "Also add the input flags for each command"},
  75. {"vv", OPT_VV, '-', "Also display each command's description"},
  76. {"v", OPT_V, '-', "For each engine, list its 'control commands'"},
  77. {"c", OPT_C, '-', "List the capabilities of each engine"},
  78. {"t", OPT_T, '-', "Check that each engine is available"},
  79. {"tt", OPT_TT, '-', "Display error trace for unavailable engines"},
  80. {"pre", OPT_PRE, 's', "Run command against the ENGINE before loading it"},
  81. {"post", OPT_POST, 's', "Run command against the ENGINE after loading it"},
  82. {OPT_MORE_STR, OPT_EOF, 1,
  83. "Commands are like \"SO_PATH:/lib/libdriver.so\""},
  84. {NULL}
  85. };
  86. static void identity(char *ptr)
  87. {
  88. return;
  89. }
  90. static int append_buf(char **buf, const char *s, int *size, int step)
  91. {
  92. if (*buf == NULL) {
  93. *size = step;
  94. *buf = app_malloc(*size, "engine buffer");
  95. **buf = '\0';
  96. }
  97. if (strlen(*buf) + strlen(s) >= (unsigned int)*size) {
  98. *size += step;
  99. *buf = OPENSSL_realloc(*buf, *size);
  100. }
  101. if (*buf == NULL)
  102. return 0;
  103. if (**buf != '\0')
  104. OPENSSL_strlcat(*buf, ", ", *size);
  105. OPENSSL_strlcat(*buf, s, *size);
  106. return 1;
  107. }
  108. static int util_flags(BIO *out, unsigned int flags, const char *indent)
  109. {
  110. int started = 0, err = 0;
  111. /* Indent before displaying input flags */
  112. BIO_printf(out, "%s%s(input flags): ", indent, indent);
  113. if (flags == 0) {
  114. BIO_printf(out, "<no flags>\n");
  115. return 1;
  116. }
  117. /*
  118. * If the object is internal, mark it in a way that shows instead of
  119. * having it part of all the other flags, even if it really is.
  120. */
  121. if (flags & ENGINE_CMD_FLAG_INTERNAL) {
  122. BIO_printf(out, "[Internal] ");
  123. }
  124. if (flags & ENGINE_CMD_FLAG_NUMERIC) {
  125. BIO_printf(out, "NUMERIC");
  126. started = 1;
  127. }
  128. /*
  129. * Now we check that no combinations of the mutually exclusive NUMERIC,
  130. * STRING, and NO_INPUT flags have been used. Future flags that can be
  131. * OR'd together with these would need to added after these to preserve
  132. * the testing logic.
  133. */
  134. if (flags & ENGINE_CMD_FLAG_STRING) {
  135. if (started) {
  136. BIO_printf(out, "|");
  137. err = 1;
  138. }
  139. BIO_printf(out, "STRING");
  140. started = 1;
  141. }
  142. if (flags & ENGINE_CMD_FLAG_NO_INPUT) {
  143. if (started) {
  144. BIO_printf(out, "|");
  145. err = 1;
  146. }
  147. BIO_printf(out, "NO_INPUT");
  148. started = 1;
  149. }
  150. /* Check for unknown flags */
  151. flags = flags & ~ENGINE_CMD_FLAG_NUMERIC &
  152. ~ENGINE_CMD_FLAG_STRING &
  153. ~ENGINE_CMD_FLAG_NO_INPUT & ~ENGINE_CMD_FLAG_INTERNAL;
  154. if (flags) {
  155. if (started)
  156. BIO_printf(out, "|");
  157. BIO_printf(out, "<0x%04X>", flags);
  158. }
  159. if (err)
  160. BIO_printf(out, " <illegal flags!>");
  161. BIO_printf(out, "\n");
  162. return 1;
  163. }
  164. static int util_verbose(ENGINE *e, int verbose, BIO *out, const char *indent)
  165. {
  166. static const int line_wrap = 78;
  167. int num;
  168. int ret = 0;
  169. char *name = NULL;
  170. char *desc = NULL;
  171. int flags;
  172. int xpos = 0;
  173. STACK_OF(OPENSSL_STRING) *cmds = NULL;
  174. if (!ENGINE_ctrl(e, ENGINE_CTRL_HAS_CTRL_FUNCTION, 0, NULL, NULL) ||
  175. ((num = ENGINE_ctrl(e, ENGINE_CTRL_GET_FIRST_CMD_TYPE,
  176. 0, NULL, NULL)) <= 0)) {
  177. return 1;
  178. }
  179. cmds = sk_OPENSSL_STRING_new_null();
  180. if (!cmds)
  181. goto err;
  182. do {
  183. int len;
  184. /* Get the command input flags */
  185. if ((flags = ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FLAGS, num,
  186. NULL, NULL)) < 0)
  187. goto err;
  188. if (!(flags & ENGINE_CMD_FLAG_INTERNAL) || verbose >= 4) {
  189. /* Get the command name */
  190. if ((len = ENGINE_ctrl(e, ENGINE_CTRL_GET_NAME_LEN_FROM_CMD, num,
  191. NULL, NULL)) <= 0)
  192. goto err;
  193. name = app_malloc(len + 1, "name buffer");
  194. if (ENGINE_ctrl(e, ENGINE_CTRL_GET_NAME_FROM_CMD, num, name,
  195. NULL) <= 0)
  196. goto err;
  197. /* Get the command description */
  198. if ((len = ENGINE_ctrl(e, ENGINE_CTRL_GET_DESC_LEN_FROM_CMD, num,
  199. NULL, NULL)) < 0)
  200. goto err;
  201. if (len > 0) {
  202. desc = app_malloc(len + 1, "description buffer");
  203. if (ENGINE_ctrl(e, ENGINE_CTRL_GET_DESC_FROM_CMD, num, desc,
  204. NULL) <= 0)
  205. goto err;
  206. }
  207. /* Now decide on the output */
  208. if (xpos == 0)
  209. /* Do an indent */
  210. xpos = BIO_puts(out, indent);
  211. else
  212. /* Otherwise prepend a ", " */
  213. xpos += BIO_printf(out, ", ");
  214. if (verbose == 1) {
  215. /*
  216. * We're just listing names, comma-delimited
  217. */
  218. if ((xpos > (int)strlen(indent)) &&
  219. (xpos + (int)strlen(name) > line_wrap)) {
  220. BIO_printf(out, "\n");
  221. xpos = BIO_puts(out, indent);
  222. }
  223. xpos += BIO_printf(out, "%s", name);
  224. } else {
  225. /* We're listing names plus descriptions */
  226. BIO_printf(out, "%s: %s\n", name,
  227. (desc == NULL) ? "<no description>" : desc);
  228. /* ... and sometimes input flags */
  229. if ((verbose >= 3) && !util_flags(out, flags, indent))
  230. goto err;
  231. xpos = 0;
  232. }
  233. }
  234. OPENSSL_free(name);
  235. name = NULL;
  236. OPENSSL_free(desc);
  237. desc = NULL;
  238. /* Move to the next command */
  239. num = ENGINE_ctrl(e, ENGINE_CTRL_GET_NEXT_CMD_TYPE, num, NULL, NULL);
  240. } while (num > 0);
  241. if (xpos > 0)
  242. BIO_printf(out, "\n");
  243. ret = 1;
  244. err:
  245. sk_OPENSSL_STRING_pop_free(cmds, identity);
  246. OPENSSL_free(name);
  247. OPENSSL_free(desc);
  248. return ret;
  249. }
  250. static void util_do_cmds(ENGINE *e, STACK_OF(OPENSSL_STRING) *cmds,
  251. BIO *out, const char *indent)
  252. {
  253. int loop, res, num = sk_OPENSSL_STRING_num(cmds);
  254. if (num < 0) {
  255. BIO_printf(out, "[Error]: internal stack error\n");
  256. return;
  257. }
  258. for (loop = 0; loop < num; loop++) {
  259. char buf[256];
  260. const char *cmd, *arg;
  261. cmd = sk_OPENSSL_STRING_value(cmds, loop);
  262. res = 1; /* assume success */
  263. /* Check if this command has no ":arg" */
  264. if ((arg = strstr(cmd, ":")) == NULL) {
  265. if (!ENGINE_ctrl_cmd_string(e, cmd, NULL, 0))
  266. res = 0;
  267. } else {
  268. if ((int)(arg - cmd) > 254) {
  269. BIO_printf(out, "[Error]: command name too long\n");
  270. return;
  271. }
  272. memcpy(buf, cmd, (int)(arg - cmd));
  273. buf[arg - cmd] = '\0';
  274. arg++; /* Move past the ":" */
  275. /* Call the command with the argument */
  276. if (!ENGINE_ctrl_cmd_string(e, buf, arg, 0))
  277. res = 0;
  278. }
  279. if (res)
  280. BIO_printf(out, "[Success]: %s\n", cmd);
  281. else {
  282. BIO_printf(out, "[Failure]: %s\n", cmd);
  283. ERR_print_errors(out);
  284. }
  285. }
  286. }
  287. int engine_main(int argc, char **argv)
  288. {
  289. int ret = 1, i;
  290. int verbose = 0, list_cap = 0, test_avail = 0, test_avail_noise = 0;
  291. ENGINE *e;
  292. STACK_OF(OPENSSL_STRING) *engines = sk_OPENSSL_STRING_new_null();
  293. STACK_OF(OPENSSL_STRING) *pre_cmds = sk_OPENSSL_STRING_new_null();
  294. STACK_OF(OPENSSL_STRING) *post_cmds = sk_OPENSSL_STRING_new_null();
  295. BIO *out;
  296. const char *indent = " ";
  297. OPTION_CHOICE o;
  298. char *prog;
  299. out = dup_bio_out(FORMAT_TEXT);
  300. prog = opt_init(argc, argv, engine_options);
  301. if (!engines || !pre_cmds || !post_cmds)
  302. goto end;
  303. while ((o = opt_next()) != OPT_EOF) {
  304. switch (o) {
  305. case OPT_EOF:
  306. case OPT_ERR:
  307. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  308. goto end;
  309. case OPT_HELP:
  310. opt_help(engine_options);
  311. ret = 0;
  312. goto end;
  313. case OPT_VVVV:
  314. case OPT_VVV:
  315. case OPT_VV:
  316. case OPT_V:
  317. /* Convert to an integer from one to four. */
  318. i = (int)(o - OPT_V) + 1;
  319. if (verbose < i)
  320. verbose = i;
  321. break;
  322. case OPT_C:
  323. list_cap = 1;
  324. break;
  325. case OPT_TT:
  326. test_avail_noise++;
  327. case OPT_T:
  328. test_avail++;
  329. break;
  330. case OPT_PRE:
  331. sk_OPENSSL_STRING_push(pre_cmds, opt_arg());
  332. break;
  333. case OPT_POST:
  334. sk_OPENSSL_STRING_push(post_cmds, opt_arg());
  335. break;
  336. }
  337. }
  338. argc = opt_num_rest();
  339. argv = opt_rest();
  340. for ( ; *argv; argv++)
  341. sk_OPENSSL_STRING_push(engines, *argv);
  342. if (sk_OPENSSL_STRING_num(engines) == 0) {
  343. for (e = ENGINE_get_first(); e != NULL; e = ENGINE_get_next(e)) {
  344. sk_OPENSSL_STRING_push(engines, (char *)ENGINE_get_id(e));
  345. }
  346. }
  347. for (i = 0; i < sk_OPENSSL_STRING_num(engines); i++) {
  348. const char *id = sk_OPENSSL_STRING_value(engines, i);
  349. if ((e = ENGINE_by_id(id)) != NULL) {
  350. const char *name = ENGINE_get_name(e);
  351. /*
  352. * Do "id" first, then "name". Easier to auto-parse.
  353. */
  354. BIO_printf(out, "(%s) %s\n", id, name);
  355. util_do_cmds(e, pre_cmds, out, indent);
  356. if (strcmp(ENGINE_get_id(e), id) != 0) {
  357. BIO_printf(out, "Loaded: (%s) %s\n",
  358. ENGINE_get_id(e), ENGINE_get_name(e));
  359. }
  360. if (list_cap) {
  361. int cap_size = 256;
  362. char *cap_buf = NULL;
  363. int k, n;
  364. const int *nids;
  365. ENGINE_CIPHERS_PTR fn_c;
  366. ENGINE_DIGESTS_PTR fn_d;
  367. ENGINE_PKEY_METHS_PTR fn_pk;
  368. if (ENGINE_get_RSA(e) != NULL
  369. && !append_buf(&cap_buf, "RSA", &cap_size, 256))
  370. goto end;
  371. if (ENGINE_get_DSA(e) != NULL
  372. && !append_buf(&cap_buf, "DSA", &cap_size, 256))
  373. goto end;
  374. if (ENGINE_get_DH(e) != NULL
  375. && !append_buf(&cap_buf, "DH", &cap_size, 256))
  376. goto end;
  377. if (ENGINE_get_RAND(e) != NULL
  378. && !append_buf(&cap_buf, "RAND", &cap_size, 256))
  379. goto end;
  380. fn_c = ENGINE_get_ciphers(e);
  381. if (!fn_c)
  382. goto skip_ciphers;
  383. n = fn_c(e, NULL, &nids, 0);
  384. for (k = 0; k < n; ++k)
  385. if (!append_buf(&cap_buf,
  386. OBJ_nid2sn(nids[k]), &cap_size, 256))
  387. goto end;
  388. skip_ciphers:
  389. fn_d = ENGINE_get_digests(e);
  390. if (!fn_d)
  391. goto skip_digests;
  392. n = fn_d(e, NULL, &nids, 0);
  393. for (k = 0; k < n; ++k)
  394. if (!append_buf(&cap_buf,
  395. OBJ_nid2sn(nids[k]), &cap_size, 256))
  396. goto end;
  397. skip_digests:
  398. fn_pk = ENGINE_get_pkey_meths(e);
  399. if (!fn_pk)
  400. goto skip_pmeths;
  401. n = fn_pk(e, NULL, &nids, 0);
  402. for (k = 0; k < n; ++k)
  403. if (!append_buf(&cap_buf,
  404. OBJ_nid2sn(nids[k]), &cap_size, 256))
  405. goto end;
  406. skip_pmeths:
  407. if (cap_buf && (*cap_buf != '\0'))
  408. BIO_printf(out, " [%s]\n", cap_buf);
  409. OPENSSL_free(cap_buf);
  410. }
  411. if (test_avail) {
  412. BIO_printf(out, "%s", indent);
  413. if (ENGINE_init(e)) {
  414. BIO_printf(out, "[ available ]\n");
  415. util_do_cmds(e, post_cmds, out, indent);
  416. ENGINE_finish(e);
  417. } else {
  418. BIO_printf(out, "[ unavailable ]\n");
  419. if (test_avail_noise)
  420. ERR_print_errors_fp(stdout);
  421. ERR_clear_error();
  422. }
  423. }
  424. if ((verbose > 0) && !util_verbose(e, verbose, out, indent))
  425. goto end;
  426. ENGINE_free(e);
  427. } else
  428. ERR_print_errors(bio_err);
  429. }
  430. ret = 0;
  431. end:
  432. ERR_print_errors(bio_err);
  433. sk_OPENSSL_STRING_pop_free(engines, identity);
  434. sk_OPENSSL_STRING_pop_free(pre_cmds, identity);
  435. sk_OPENSSL_STRING_pop_free(post_cmds, identity);
  436. BIO_free_all(out);
  437. return (ret);
  438. }
  439. #else
  440. # if PEDANTIC
  441. static void *dummy = &dummy;
  442. # endif
  443. #endif