engine.c 16 KB

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