engine.c 17 KB

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