2
0

engine.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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 <openssl/opensslconf.h>
  59. #ifdef OPENSSL_NO_ENGINE
  60. NON_EMPTY_TRANSLATION_UNIT
  61. #else
  62. # include "apps.h"
  63. # include <stdio.h>
  64. # include <stdlib.h>
  65. # include <string.h>
  66. # include <openssl/err.h>
  67. # include <openssl/engine.h>
  68. # include <openssl/ssl.h>
  69. typedef enum OPTION_choice {
  70. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  71. OPT_C, OPT_T, OPT_TT, OPT_PRE, OPT_POST,
  72. OPT_V = 100, OPT_VV, OPT_VVV, OPT_VVVV
  73. } OPTION_CHOICE;
  74. OPTIONS engine_options[] = {
  75. {OPT_HELP_STR, 1, '-', "Usage: %s [options] engine...\n"},
  76. {OPT_HELP_STR, 1, '-',
  77. " engine... Engines to load\n"},
  78. {"help", OPT_HELP, '-', "Display this summary"},
  79. {"v", OPT_V, '-', "List 'control commands' For each specified engine"},
  80. {"vv", OPT_VV, '-', "Also display each command's description"},
  81. {"vvv", OPT_VVV, '-', "Also add the input flags for each command"},
  82. {"vvvv", OPT_VVVV, '-', "Also show internal input flags"},
  83. {"c", OPT_C, '-', "List the capabilities of specified engine"},
  84. {"t", OPT_T, '-', "Check that specified engine is available"},
  85. {"tt", OPT_TT, '-', "Display error trace for unavailable engines"},
  86. {"pre", OPT_PRE, 's', "Run command against the ENGINE before loading it"},
  87. {"post", OPT_POST, 's', "Run command against the ENGINE after loading it"},
  88. {OPT_MORE_STR, OPT_EOF, 1,
  89. "Commands are like \"SO_PATH:/lib/libdriver.so\""},
  90. {NULL}
  91. };
  92. static void identity(char *ptr)
  93. {
  94. }
  95. static int append_buf(char **buf, int *size, const char *s)
  96. {
  97. if (*buf == NULL) {
  98. *size = 256;
  99. *buf = app_malloc(*size, "engine buffer");
  100. **buf = '\0';
  101. }
  102. if (strlen(*buf) + strlen(s) >= (unsigned int)*size) {
  103. *size += 256;
  104. *buf = OPENSSL_realloc(*buf, *size);
  105. }
  106. if (*buf == NULL)
  107. return 0;
  108. if (**buf != '\0')
  109. OPENSSL_strlcat(*buf, ", ", *size);
  110. OPENSSL_strlcat(*buf, s, *size);
  111. return 1;
  112. }
  113. static int util_flags(BIO *out, unsigned int flags, const char *indent)
  114. {
  115. int started = 0, err = 0;
  116. /* Indent before displaying input flags */
  117. BIO_printf(out, "%s%s(input flags): ", indent, indent);
  118. if (flags == 0) {
  119. BIO_printf(out, "<no flags>\n");
  120. return 1;
  121. }
  122. /*
  123. * If the object is internal, mark it in a way that shows instead of
  124. * having it part of all the other flags, even if it really is.
  125. */
  126. if (flags & ENGINE_CMD_FLAG_INTERNAL) {
  127. BIO_printf(out, "[Internal] ");
  128. }
  129. if (flags & ENGINE_CMD_FLAG_NUMERIC) {
  130. BIO_printf(out, "NUMERIC");
  131. started = 1;
  132. }
  133. /*
  134. * Now we check that no combinations of the mutually exclusive NUMERIC,
  135. * STRING, and NO_INPUT flags have been used. Future flags that can be
  136. * OR'd together with these would need to added after these to preserve
  137. * the testing logic.
  138. */
  139. if (flags & ENGINE_CMD_FLAG_STRING) {
  140. if (started) {
  141. BIO_printf(out, "|");
  142. err = 1;
  143. }
  144. BIO_printf(out, "STRING");
  145. started = 1;
  146. }
  147. if (flags & ENGINE_CMD_FLAG_NO_INPUT) {
  148. if (started) {
  149. BIO_printf(out, "|");
  150. err = 1;
  151. }
  152. BIO_printf(out, "NO_INPUT");
  153. started = 1;
  154. }
  155. /* Check for unknown flags */
  156. flags = flags & ~ENGINE_CMD_FLAG_NUMERIC &
  157. ~ENGINE_CMD_FLAG_STRING &
  158. ~ENGINE_CMD_FLAG_NO_INPUT & ~ENGINE_CMD_FLAG_INTERNAL;
  159. if (flags) {
  160. if (started)
  161. BIO_printf(out, "|");
  162. BIO_printf(out, "<0x%04X>", flags);
  163. }
  164. if (err)
  165. BIO_printf(out, " <illegal flags!>");
  166. BIO_printf(out, "\n");
  167. return 1;
  168. }
  169. static int util_verbose(ENGINE *e, int verbose, BIO *out, const char *indent)
  170. {
  171. static const int line_wrap = 78;
  172. int num;
  173. int ret = 0;
  174. char *name = NULL;
  175. char *desc = NULL;
  176. int flags;
  177. int xpos = 0;
  178. STACK_OF(OPENSSL_STRING) *cmds = NULL;
  179. if (!ENGINE_ctrl(e, ENGINE_CTRL_HAS_CTRL_FUNCTION, 0, NULL, NULL) ||
  180. ((num = ENGINE_ctrl(e, ENGINE_CTRL_GET_FIRST_CMD_TYPE,
  181. 0, NULL, NULL)) <= 0)) {
  182. return 1;
  183. }
  184. cmds = sk_OPENSSL_STRING_new_null();
  185. if (!cmds)
  186. goto err;
  187. do {
  188. int len;
  189. /* Get the command input flags */
  190. if ((flags = ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FLAGS, num,
  191. NULL, NULL)) < 0)
  192. goto err;
  193. if (!(flags & ENGINE_CMD_FLAG_INTERNAL) || verbose >= 4) {
  194. /* Get the command name */
  195. if ((len = ENGINE_ctrl(e, ENGINE_CTRL_GET_NAME_LEN_FROM_CMD, num,
  196. NULL, NULL)) <= 0)
  197. goto err;
  198. name = app_malloc(len + 1, "name buffer");
  199. if (ENGINE_ctrl(e, ENGINE_CTRL_GET_NAME_FROM_CMD, num, name,
  200. NULL) <= 0)
  201. goto err;
  202. /* Get the command description */
  203. if ((len = ENGINE_ctrl(e, ENGINE_CTRL_GET_DESC_LEN_FROM_CMD, num,
  204. NULL, NULL)) < 0)
  205. goto err;
  206. if (len > 0) {
  207. desc = app_malloc(len + 1, "description buffer");
  208. if (ENGINE_ctrl(e, ENGINE_CTRL_GET_DESC_FROM_CMD, num, desc,
  209. NULL) <= 0)
  210. goto err;
  211. }
  212. /* Now decide on the output */
  213. if (xpos == 0)
  214. /* Do an indent */
  215. xpos = BIO_puts(out, indent);
  216. else
  217. /* Otherwise prepend a ", " */
  218. xpos += BIO_printf(out, ", ");
  219. if (verbose == 1) {
  220. /*
  221. * We're just listing names, comma-delimited
  222. */
  223. if ((xpos > (int)strlen(indent)) &&
  224. (xpos + (int)strlen(name) > line_wrap)) {
  225. BIO_printf(out, "\n");
  226. xpos = BIO_puts(out, indent);
  227. }
  228. xpos += BIO_printf(out, "%s", name);
  229. } else {
  230. /* We're listing names plus descriptions */
  231. BIO_printf(out, "%s: %s\n", name,
  232. (desc == NULL) ? "<no description>" : desc);
  233. /* ... and sometimes input flags */
  234. if ((verbose >= 3) && !util_flags(out, flags, indent))
  235. goto err;
  236. xpos = 0;
  237. }
  238. }
  239. OPENSSL_free(name);
  240. name = NULL;
  241. OPENSSL_free(desc);
  242. desc = NULL;
  243. /* Move to the next command */
  244. num = ENGINE_ctrl(e, ENGINE_CTRL_GET_NEXT_CMD_TYPE, num, NULL, NULL);
  245. } while (num > 0);
  246. if (xpos > 0)
  247. BIO_printf(out, "\n");
  248. ret = 1;
  249. err:
  250. sk_OPENSSL_STRING_pop_free(cmds, identity);
  251. OPENSSL_free(name);
  252. OPENSSL_free(desc);
  253. return ret;
  254. }
  255. static void util_do_cmds(ENGINE *e, STACK_OF(OPENSSL_STRING) *cmds,
  256. BIO *out, const char *indent)
  257. {
  258. int loop, res, num = sk_OPENSSL_STRING_num(cmds);
  259. if (num < 0) {
  260. BIO_printf(out, "[Error]: internal stack error\n");
  261. return;
  262. }
  263. for (loop = 0; loop < num; loop++) {
  264. char buf[256];
  265. const char *cmd, *arg;
  266. cmd = sk_OPENSSL_STRING_value(cmds, loop);
  267. res = 1; /* assume success */
  268. /* Check if this command has no ":arg" */
  269. if ((arg = strstr(cmd, ":")) == NULL) {
  270. if (!ENGINE_ctrl_cmd_string(e, cmd, NULL, 0))
  271. res = 0;
  272. } else {
  273. if ((int)(arg - cmd) > 254) {
  274. BIO_printf(out, "[Error]: command name too long\n");
  275. return;
  276. }
  277. memcpy(buf, cmd, (int)(arg - cmd));
  278. buf[arg - cmd] = '\0';
  279. arg++; /* Move past the ":" */
  280. /* Call the command with the argument */
  281. if (!ENGINE_ctrl_cmd_string(e, buf, arg, 0))
  282. res = 0;
  283. }
  284. if (res)
  285. BIO_printf(out, "[Success]: %s\n", cmd);
  286. else {
  287. BIO_printf(out, "[Failure]: %s\n", cmd);
  288. ERR_print_errors(out);
  289. }
  290. }
  291. }
  292. int engine_main(int argc, char **argv)
  293. {
  294. int ret = 1, i;
  295. int verbose = 0, list_cap = 0, test_avail = 0, test_avail_noise = 0;
  296. ENGINE *e;
  297. STACK_OF(OPENSSL_STRING) *engines = sk_OPENSSL_STRING_new_null();
  298. STACK_OF(OPENSSL_STRING) *pre_cmds = sk_OPENSSL_STRING_new_null();
  299. STACK_OF(OPENSSL_STRING) *post_cmds = sk_OPENSSL_STRING_new_null();
  300. BIO *out;
  301. const char *indent = " ";
  302. OPTION_CHOICE o;
  303. char *prog;
  304. char *argv1;
  305. out = dup_bio_out(FORMAT_TEXT);
  306. if (engines == NULL || pre_cmds == NULL || post_cmds == NULL)
  307. goto end;
  308. /* Remember the original command name, parse/skip any leading engine
  309. * names, and then setup to parse the rest of the line as flags. */
  310. prog = argv[0];
  311. while ((argv1 = argv[1]) != NULL && *argv1 != '-') {
  312. sk_OPENSSL_STRING_push(engines, argv1);
  313. argc--;
  314. argv++;
  315. }
  316. argv[0] = prog;
  317. opt_init(argc, argv, engine_options);
  318. while ((o = opt_next()) != OPT_EOF) {
  319. switch (o) {
  320. case OPT_EOF:
  321. case OPT_ERR:
  322. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  323. goto end;
  324. case OPT_HELP:
  325. opt_help(engine_options);
  326. ret = 0;
  327. goto end;
  328. case OPT_VVVV:
  329. case OPT_VVV:
  330. case OPT_VV:
  331. case OPT_V:
  332. /* Convert to an integer from one to four. */
  333. i = (int)(o - OPT_V) + 1;
  334. if (verbose < i)
  335. verbose = i;
  336. break;
  337. case OPT_C:
  338. list_cap = 1;
  339. break;
  340. case OPT_TT:
  341. test_avail_noise++;
  342. case OPT_T:
  343. test_avail++;
  344. break;
  345. case OPT_PRE:
  346. sk_OPENSSL_STRING_push(pre_cmds, opt_arg());
  347. break;
  348. case OPT_POST:
  349. sk_OPENSSL_STRING_push(post_cmds, opt_arg());
  350. break;
  351. }
  352. }
  353. /* Allow any trailing parameters as engine names. */
  354. argc = opt_num_rest();
  355. argv = opt_rest();
  356. for ( ; *argv; argv++) {
  357. if (**argv == '-') {
  358. BIO_printf(bio_err, "%s: Cannot mix flags and engine names.\n",
  359. prog);
  360. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  361. goto end;
  362. }
  363. sk_OPENSSL_STRING_push(engines, *argv);
  364. }
  365. if (sk_OPENSSL_STRING_num(engines) == 0) {
  366. for (e = ENGINE_get_first(); e != NULL; e = ENGINE_get_next(e)) {
  367. sk_OPENSSL_STRING_push(engines, (char *)ENGINE_get_id(e));
  368. }
  369. }
  370. for (i = 0; i < sk_OPENSSL_STRING_num(engines); i++) {
  371. const char *id = sk_OPENSSL_STRING_value(engines, i);
  372. if ((e = ENGINE_by_id(id)) != NULL) {
  373. const char *name = ENGINE_get_name(e);
  374. /*
  375. * Do "id" first, then "name". Easier to auto-parse.
  376. */
  377. BIO_printf(out, "(%s) %s\n", id, name);
  378. util_do_cmds(e, pre_cmds, out, indent);
  379. if (strcmp(ENGINE_get_id(e), id) != 0) {
  380. BIO_printf(out, "Loaded: (%s) %s\n",
  381. ENGINE_get_id(e), ENGINE_get_name(e));
  382. }
  383. if (list_cap) {
  384. int cap_size = 256;
  385. char *cap_buf = NULL;
  386. int k, n;
  387. const int *nids;
  388. ENGINE_CIPHERS_PTR fn_c;
  389. ENGINE_DIGESTS_PTR fn_d;
  390. ENGINE_PKEY_METHS_PTR fn_pk;
  391. if (ENGINE_get_RSA(e) != NULL
  392. && !append_buf(&cap_buf, &cap_size, "RSA"))
  393. goto end;
  394. if (ENGINE_get_DSA(e) != NULL
  395. && !append_buf(&cap_buf, &cap_size, "DSA"))
  396. goto end;
  397. if (ENGINE_get_DH(e) != NULL
  398. && !append_buf(&cap_buf, &cap_size, "DH"))
  399. goto end;
  400. if (ENGINE_get_RAND(e) != NULL
  401. && !append_buf(&cap_buf, &cap_size, "RAND"))
  402. goto end;
  403. fn_c = ENGINE_get_ciphers(e);
  404. if (!fn_c)
  405. goto skip_ciphers;
  406. n = fn_c(e, NULL, &nids, 0);
  407. for (k = 0; k < n; ++k)
  408. if (!append_buf(&cap_buf, &cap_size, OBJ_nid2sn(nids[k])))
  409. goto end;
  410. skip_ciphers:
  411. fn_d = ENGINE_get_digests(e);
  412. if (!fn_d)
  413. goto skip_digests;
  414. n = fn_d(e, NULL, &nids, 0);
  415. for (k = 0; k < n; ++k)
  416. if (!append_buf(&cap_buf, &cap_size, OBJ_nid2sn(nids[k])))
  417. goto end;
  418. skip_digests:
  419. fn_pk = ENGINE_get_pkey_meths(e);
  420. if (!fn_pk)
  421. goto skip_pmeths;
  422. n = fn_pk(e, NULL, &nids, 0);
  423. for (k = 0; k < n; ++k)
  424. if (!append_buf(&cap_buf, &cap_size, OBJ_nid2sn(nids[k])))
  425. goto end;
  426. skip_pmeths:
  427. if (cap_buf && (*cap_buf != '\0'))
  428. BIO_printf(out, " [%s]\n", cap_buf);
  429. OPENSSL_free(cap_buf);
  430. }
  431. if (test_avail) {
  432. BIO_printf(out, "%s", indent);
  433. if (ENGINE_init(e)) {
  434. BIO_printf(out, "[ available ]\n");
  435. util_do_cmds(e, post_cmds, out, indent);
  436. ENGINE_finish(e);
  437. } else {
  438. BIO_printf(out, "[ unavailable ]\n");
  439. if (test_avail_noise)
  440. ERR_print_errors_fp(stdout);
  441. ERR_clear_error();
  442. }
  443. }
  444. if ((verbose > 0) && !util_verbose(e, verbose, out, indent))
  445. goto end;
  446. ENGINE_free(e);
  447. } else
  448. ERR_print_errors(bio_err);
  449. }
  450. ret = 0;
  451. end:
  452. ERR_print_errors(bio_err);
  453. sk_OPENSSL_STRING_pop_free(engines, identity);
  454. sk_OPENSSL_STRING_pop_free(pre_cmds, identity);
  455. sk_OPENSSL_STRING_pop_free(post_cmds, identity);
  456. BIO_free_all(out);
  457. return (ret);
  458. }
  459. #endif