engine.c 16 KB

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