engine.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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(char *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. BIO_printf(bio_out, "NUMERIC");
  136. started = 1;
  137. }
  138. /*
  139. * Now we check that no combinations of the mutually exclusive NUMERIC,
  140. * STRING, and NO_INPUT flags have been used. Future flags that can be
  141. * OR'd together with these would need to added after these to preserve
  142. * the testing logic.
  143. */
  144. if (flags & ENGINE_CMD_FLAG_STRING) {
  145. if (started) {
  146. BIO_printf(bio_out, "|");
  147. err = 1;
  148. }
  149. BIO_printf(bio_out, "STRING");
  150. started = 1;
  151. }
  152. if (flags & ENGINE_CMD_FLAG_NO_INPUT) {
  153. if (started) {
  154. BIO_printf(bio_out, "|");
  155. err = 1;
  156. }
  157. BIO_printf(bio_out, "NO_INPUT");
  158. started = 1;
  159. }
  160. /* Check for unknown flags */
  161. flags = flags & ~ENGINE_CMD_FLAG_NUMERIC &
  162. ~ENGINE_CMD_FLAG_STRING &
  163. ~ENGINE_CMD_FLAG_NO_INPUT & ~ENGINE_CMD_FLAG_INTERNAL;
  164. if (flags) {
  165. if (started)
  166. BIO_printf(bio_out, "|");
  167. BIO_printf(bio_out, "<0x%04X>", flags);
  168. }
  169. if (err)
  170. BIO_printf(bio_out, " <illegal flags!>");
  171. BIO_printf(bio_out, "\n");
  172. return 1;
  173. }
  174. static int util_verbose(ENGINE *e, int verbose, BIO *bio_out,
  175. const char *indent)
  176. {
  177. static const int line_wrap = 78;
  178. int num;
  179. int ret = 0;
  180. char *name = NULL;
  181. char *desc = NULL;
  182. int flags;
  183. int xpos = 0;
  184. STACK_OF(OPENSSL_STRING) *cmds = NULL;
  185. if (!ENGINE_ctrl(e, ENGINE_CTRL_HAS_CTRL_FUNCTION, 0, NULL, NULL) ||
  186. ((num = ENGINE_ctrl(e, ENGINE_CTRL_GET_FIRST_CMD_TYPE,
  187. 0, NULL, NULL)) <= 0)) {
  188. # if 0
  189. BIO_printf(bio_out, "%s<no control commands>\n", indent);
  190. # endif
  191. return 1;
  192. }
  193. cmds = sk_OPENSSL_STRING_new_null();
  194. if (!cmds)
  195. goto err;
  196. do {
  197. int len;
  198. /* Get the command input flags */
  199. if ((flags = ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FLAGS, num,
  200. NULL, NULL)) < 0)
  201. goto err;
  202. if (!(flags & ENGINE_CMD_FLAG_INTERNAL) || verbose >= 4) {
  203. /* Get the command name */
  204. if ((len = ENGINE_ctrl(e, ENGINE_CTRL_GET_NAME_LEN_FROM_CMD, num,
  205. NULL, NULL)) <= 0)
  206. goto err;
  207. if ((name = OPENSSL_malloc(len + 1)) == NULL)
  208. goto err;
  209. if (ENGINE_ctrl(e, ENGINE_CTRL_GET_NAME_FROM_CMD, num, name,
  210. NULL) <= 0)
  211. goto err;
  212. /* Get the command description */
  213. if ((len = ENGINE_ctrl(e, ENGINE_CTRL_GET_DESC_LEN_FROM_CMD, num,
  214. NULL, NULL)) < 0)
  215. goto err;
  216. if (len > 0) {
  217. if ((desc = OPENSSL_malloc(len + 1)) == NULL)
  218. goto err;
  219. if (ENGINE_ctrl(e, ENGINE_CTRL_GET_DESC_FROM_CMD, num, desc,
  220. NULL) <= 0)
  221. goto err;
  222. }
  223. /* Now decide on the output */
  224. if (xpos == 0)
  225. /* Do an indent */
  226. xpos = BIO_puts(bio_out, indent);
  227. else
  228. /* Otherwise prepend a ", " */
  229. xpos += BIO_printf(bio_out, ", ");
  230. if (verbose == 1) {
  231. /*
  232. * We're just listing names, comma-delimited
  233. */
  234. if ((xpos > (int)strlen(indent)) &&
  235. (xpos + (int)strlen(name) > line_wrap)) {
  236. BIO_printf(bio_out, "\n");
  237. xpos = BIO_puts(bio_out, indent);
  238. }
  239. xpos += BIO_printf(bio_out, "%s", name);
  240. } else {
  241. /* We're listing names plus descriptions */
  242. BIO_printf(bio_out, "%s: %s\n", name,
  243. (desc == NULL) ? "<no description>" : desc);
  244. /* ... and sometimes input flags */
  245. if ((verbose >= 3) && !util_flags(bio_out, flags, indent))
  246. goto err;
  247. xpos = 0;
  248. }
  249. }
  250. OPENSSL_free(name);
  251. name = NULL;
  252. if (desc) {
  253. OPENSSL_free(desc);
  254. desc = NULL;
  255. }
  256. /* Move to the next command */
  257. num = ENGINE_ctrl(e, ENGINE_CTRL_GET_NEXT_CMD_TYPE, num, NULL, NULL);
  258. } while (num > 0);
  259. if (xpos > 0)
  260. BIO_printf(bio_out, "\n");
  261. ret = 1;
  262. err:
  263. if (cmds)
  264. sk_OPENSSL_STRING_pop_free(cmds, identity);
  265. if (name)
  266. OPENSSL_free(name);
  267. if (desc)
  268. OPENSSL_free(desc);
  269. return ret;
  270. }
  271. static void util_do_cmds(ENGINE *e, STACK_OF(OPENSSL_STRING) *cmds,
  272. BIO *bio_out, const char *indent)
  273. {
  274. int loop, res, num = sk_OPENSSL_STRING_num(cmds);
  275. if (num < 0) {
  276. BIO_printf(bio_out, "[Error]: internal stack error\n");
  277. return;
  278. }
  279. for (loop = 0; loop < num; loop++) {
  280. char buf[256];
  281. const char *cmd, *arg;
  282. cmd = sk_OPENSSL_STRING_value(cmds, loop);
  283. res = 1; /* assume success */
  284. /* Check if this command has no ":arg" */
  285. if ((arg = strstr(cmd, ":")) == NULL) {
  286. if (!ENGINE_ctrl_cmd_string(e, cmd, NULL, 0))
  287. res = 0;
  288. } else {
  289. if ((int)(arg - cmd) > 254) {
  290. BIO_printf(bio_out, "[Error]: command name too long\n");
  291. return;
  292. }
  293. memcpy(buf, cmd, (int)(arg - cmd));
  294. buf[arg - cmd] = '\0';
  295. arg++; /* Move past the ":" */
  296. /* Call the command with the argument */
  297. if (!ENGINE_ctrl_cmd_string(e, buf, arg, 0))
  298. res = 0;
  299. }
  300. if (res)
  301. BIO_printf(bio_out, "[Success]: %s\n", cmd);
  302. else {
  303. BIO_printf(bio_out, "[Failure]: %s\n", cmd);
  304. ERR_print_errors(bio_out);
  305. }
  306. }
  307. }
  308. int MAIN(int, char **);
  309. int MAIN(int argc, char **argv)
  310. {
  311. int ret = 1, i;
  312. const char **pp;
  313. int verbose = 0, list_cap = 0, test_avail = 0, test_avail_noise = 0;
  314. ENGINE *e;
  315. STACK_OF(OPENSSL_STRING) *engines = sk_OPENSSL_STRING_new_null();
  316. STACK_OF(OPENSSL_STRING) *pre_cmds = sk_OPENSSL_STRING_new_null();
  317. STACK_OF(OPENSSL_STRING) *post_cmds = sk_OPENSSL_STRING_new_null();
  318. int badops = 1;
  319. BIO *bio_out = NULL;
  320. const char *indent = " ";
  321. apps_startup();
  322. SSL_load_error_strings();
  323. if (bio_err == NULL)
  324. bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
  325. if (!load_config(bio_err, NULL))
  326. goto end;
  327. bio_out = BIO_new_fp(stdout, BIO_NOCLOSE);
  328. # ifdef OPENSSL_SYS_VMS
  329. {
  330. BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  331. bio_out = BIO_push(tmpbio, bio_out);
  332. }
  333. # endif
  334. argc--;
  335. argv++;
  336. while (argc >= 1) {
  337. if (strncmp(*argv, "-v", 2) == 0) {
  338. if (strspn(*argv + 1, "v") < strlen(*argv + 1))
  339. goto skip_arg_loop;
  340. if ((verbose = strlen(*argv + 1)) > 4)
  341. goto skip_arg_loop;
  342. } else if (strcmp(*argv, "-c") == 0)
  343. list_cap = 1;
  344. else if (strncmp(*argv, "-t", 2) == 0) {
  345. test_avail = 1;
  346. if (strspn(*argv + 1, "t") < strlen(*argv + 1))
  347. goto skip_arg_loop;
  348. if ((test_avail_noise = strlen(*argv + 1) - 1) > 1)
  349. goto skip_arg_loop;
  350. } else if (strcmp(*argv, "-pre") == 0) {
  351. argc--;
  352. argv++;
  353. if (argc == 0)
  354. goto skip_arg_loop;
  355. sk_OPENSSL_STRING_push(pre_cmds, *argv);
  356. } else if (strcmp(*argv, "-post") == 0) {
  357. argc--;
  358. argv++;
  359. if (argc == 0)
  360. goto skip_arg_loop;
  361. sk_OPENSSL_STRING_push(post_cmds, *argv);
  362. } else if ((strncmp(*argv, "-h", 2) == 0) ||
  363. (strcmp(*argv, "-?") == 0))
  364. goto skip_arg_loop;
  365. else
  366. sk_OPENSSL_STRING_push(engines, *argv);
  367. argc--;
  368. argv++;
  369. }
  370. /* Looks like everything went OK */
  371. badops = 0;
  372. skip_arg_loop:
  373. if (badops) {
  374. for (pp = engine_usage; (*pp != NULL); pp++)
  375. BIO_printf(bio_err, "%s", *pp);
  376. goto end;
  377. }
  378. if (sk_OPENSSL_STRING_num(engines) == 0) {
  379. for (e = ENGINE_get_first(); e != NULL; e = ENGINE_get_next(e)) {
  380. sk_OPENSSL_STRING_push(engines, (char *)ENGINE_get_id(e));
  381. }
  382. }
  383. for (i = 0; i < sk_OPENSSL_STRING_num(engines); i++) {
  384. const char *id = sk_OPENSSL_STRING_value(engines, i);
  385. if ((e = ENGINE_by_id(id)) != NULL) {
  386. const char *name = ENGINE_get_name(e);
  387. /*
  388. * Do "id" first, then "name". Easier to auto-parse.
  389. */
  390. BIO_printf(bio_out, "(%s) %s\n", id, name);
  391. util_do_cmds(e, pre_cmds, bio_out, indent);
  392. if (strcmp(ENGINE_get_id(e), id) != 0) {
  393. BIO_printf(bio_out, "Loaded: (%s) %s\n",
  394. ENGINE_get_id(e), ENGINE_get_name(e));
  395. }
  396. if (list_cap) {
  397. int cap_size = 256;
  398. char *cap_buf = NULL;
  399. int k, n;
  400. const int *nids;
  401. ENGINE_CIPHERS_PTR fn_c;
  402. ENGINE_DIGESTS_PTR fn_d;
  403. ENGINE_PKEY_METHS_PTR fn_pk;
  404. if (ENGINE_get_RSA(e) != NULL
  405. && !append_buf(&cap_buf, "RSA", &cap_size, 256))
  406. goto end;
  407. if (ENGINE_get_DSA(e) != NULL
  408. && !append_buf(&cap_buf, "DSA", &cap_size, 256))
  409. goto end;
  410. if (ENGINE_get_DH(e) != NULL
  411. && !append_buf(&cap_buf, "DH", &cap_size, 256))
  412. goto end;
  413. if (ENGINE_get_RAND(e) != NULL
  414. && !append_buf(&cap_buf, "RAND", &cap_size, 256))
  415. goto end;
  416. fn_c = ENGINE_get_ciphers(e);
  417. if (!fn_c)
  418. goto skip_ciphers;
  419. n = fn_c(e, NULL, &nids, 0);
  420. for (k = 0; k < n; ++k)
  421. if (!append_buf(&cap_buf,
  422. OBJ_nid2sn(nids[k]), &cap_size, 256))
  423. goto end;
  424. skip_ciphers:
  425. fn_d = ENGINE_get_digests(e);
  426. if (!fn_d)
  427. goto skip_digests;
  428. n = fn_d(e, NULL, &nids, 0);
  429. for (k = 0; k < n; ++k)
  430. if (!append_buf(&cap_buf,
  431. OBJ_nid2sn(nids[k]), &cap_size, 256))
  432. goto end;
  433. skip_digests:
  434. fn_pk = ENGINE_get_pkey_meths(e);
  435. if (!fn_pk)
  436. goto skip_pmeths;
  437. n = fn_pk(e, NULL, &nids, 0);
  438. for (k = 0; k < n; ++k)
  439. if (!append_buf(&cap_buf,
  440. OBJ_nid2sn(nids[k]), &cap_size, 256))
  441. goto end;
  442. skip_pmeths:
  443. if (cap_buf && (*cap_buf != '\0'))
  444. BIO_printf(bio_out, " [%s]\n", cap_buf);
  445. OPENSSL_free(cap_buf);
  446. }
  447. if (test_avail) {
  448. BIO_printf(bio_out, "%s", indent);
  449. if (ENGINE_init(e)) {
  450. BIO_printf(bio_out, "[ available ]\n");
  451. util_do_cmds(e, post_cmds, bio_out, indent);
  452. ENGINE_finish(e);
  453. } else {
  454. BIO_printf(bio_out, "[ unavailable ]\n");
  455. if (test_avail_noise)
  456. ERR_print_errors_fp(stdout);
  457. ERR_clear_error();
  458. }
  459. }
  460. if ((verbose > 0) && !util_verbose(e, verbose, bio_out, indent))
  461. goto end;
  462. ENGINE_free(e);
  463. } else
  464. ERR_print_errors(bio_err);
  465. }
  466. ret = 0;
  467. end:
  468. ERR_print_errors(bio_err);
  469. sk_OPENSSL_STRING_pop_free(engines, identity);
  470. sk_OPENSSL_STRING_pop_free(pre_cmds, identity);
  471. sk_OPENSSL_STRING_pop_free(post_cmds, identity);
  472. if (bio_out != NULL)
  473. BIO_free_all(bio_out);
  474. apps_shutdown();
  475. OPENSSL_EXIT(ret);
  476. }
  477. #else
  478. # if PEDANTIC
  479. static void *dummy = &dummy;
  480. # endif
  481. #endif