engine.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. /* apps/engine.c -*- mode: C; c-file-style: "eay" -*- */
  2. /* Written by Richard Levitte <richard@levitte.org> for the OpenSSL
  3. * project 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. #ifndef OPENSSL_NO_ENGINE
  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. #include <openssl/engine.h>
  68. #include <openssl/ssl.h>
  69. #undef PROG
  70. #define PROG engine_main
  71. static const char *engine_usage[]={
  72. "usage: engine opts [engine ...]\n",
  73. " -v[v[v[v]]] - verbose mode, for each engine, list its 'control commands'\n",
  74. " -vv will additionally display each command's description\n",
  75. " -vvv will also add the input flags for each command\n",
  76. " -vvvv will also show internal input flags\n",
  77. " -c - for each engine, also list the capabilities\n",
  78. " -t[t] - for each engine, check that they are really available\n",
  79. " -tt will display error trace for unavailable engines\n",
  80. " -pre <cmd> - runs command 'cmd' against the ENGINE before any attempts\n",
  81. " to load it (if -t is used)\n",
  82. " -post <cmd> - runs command 'cmd' against the ENGINE after loading it\n",
  83. " (only used if -t is also provided)\n",
  84. " NB: -pre and -post will be applied to all ENGINEs supplied on the command\n",
  85. " line, or all supported ENGINEs if none are specified.\n",
  86. " Eg. '-pre \"SO_PATH:/lib/libdriver.so\"' calls command \"SO_PATH\" with\n",
  87. " argument \"/lib/libdriver.so\".\n",
  88. NULL
  89. };
  90. static void identity(void *ptr)
  91. {
  92. return;
  93. }
  94. static int append_buf(char **buf, const char *s, int *size, int step)
  95. {
  96. int l = strlen(s);
  97. if (*buf == NULL)
  98. {
  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. {
  109. *size += step;
  110. *buf = OPENSSL_realloc(*buf, *size);
  111. }
  112. if (*buf == NULL)
  113. return 0;
  114. if (**buf != '\0')
  115. BUF_strlcat(*buf, ", ", *size);
  116. BUF_strlcat(*buf, s, *size);
  117. return 1;
  118. }
  119. static int util_flags(BIO *bio_out, unsigned int flags, const char *indent)
  120. {
  121. int started = 0, err = 0;
  122. /* Indent before displaying input flags */
  123. BIO_printf(bio_out, "%s%s(input flags): ", indent, indent);
  124. if(flags == 0)
  125. {
  126. BIO_printf(bio_out, "<no flags>\n");
  127. return 1;
  128. }
  129. /* If the object is internal, mark it in a way that shows instead of
  130. * having it part of all the other flags, even if it really is. */
  131. if(flags & ENGINE_CMD_FLAG_INTERNAL)
  132. {
  133. BIO_printf(bio_out, "[Internal] ");
  134. }
  135. if(flags & ENGINE_CMD_FLAG_NUMERIC)
  136. {
  137. BIO_printf(bio_out, "NUMERIC");
  138. started = 1;
  139. }
  140. /* Now we check that no combinations of the mutually exclusive NUMERIC,
  141. * STRING, and NO_INPUT flags have been used. Future flags that can be
  142. * OR'd together with these would need to added after these to preserve
  143. * the testing logic. */
  144. if(flags & ENGINE_CMD_FLAG_STRING)
  145. {
  146. if(started)
  147. {
  148. BIO_printf(bio_out, "|");
  149. err = 1;
  150. }
  151. BIO_printf(bio_out, "STRING");
  152. started = 1;
  153. }
  154. if(flags & ENGINE_CMD_FLAG_NO_INPUT)
  155. {
  156. if(started)
  157. {
  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 &
  168. ~ENGINE_CMD_FLAG_INTERNAL;
  169. if(flags)
  170. {
  171. if(started) BIO_printf(bio_out, "|");
  172. BIO_printf(bio_out, "<0x%04X>", flags);
  173. }
  174. if(err)
  175. BIO_printf(bio_out, " <illegal flags!>");
  176. BIO_printf(bio_out, "\n");
  177. return 1;
  178. }
  179. static int util_verbose(ENGINE *e, int verbose, BIO *bio_out, 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. {
  193. #if 0
  194. BIO_printf(bio_out, "%s<no control commands>\n", indent);
  195. #endif
  196. return 1;
  197. }
  198. cmds = sk_new_null();
  199. if(!cmds)
  200. goto err;
  201. do {
  202. int len;
  203. /* Get the command input flags */
  204. if((flags = ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FLAGS, num,
  205. NULL, NULL)) < 0)
  206. goto err;
  207. if (!(flags & ENGINE_CMD_FLAG_INTERNAL) || verbose >= 4)
  208. {
  209. /* Get the command name */
  210. if((len = ENGINE_ctrl(e, ENGINE_CTRL_GET_NAME_LEN_FROM_CMD, num,
  211. NULL, NULL)) <= 0)
  212. goto err;
  213. if((name = OPENSSL_malloc(len + 1)) == NULL)
  214. goto err;
  215. if(ENGINE_ctrl(e, ENGINE_CTRL_GET_NAME_FROM_CMD, num, name,
  216. NULL) <= 0)
  217. goto err;
  218. /* Get the command description */
  219. if((len = ENGINE_ctrl(e, ENGINE_CTRL_GET_DESC_LEN_FROM_CMD, num,
  220. NULL, NULL)) < 0)
  221. goto err;
  222. if(len > 0)
  223. {
  224. if((desc = OPENSSL_malloc(len + 1)) == NULL)
  225. goto err;
  226. if(ENGINE_ctrl(e, ENGINE_CTRL_GET_DESC_FROM_CMD, num, desc,
  227. NULL) <= 0)
  228. goto err;
  229. }
  230. /* Now decide on the output */
  231. if(xpos == 0)
  232. /* Do an indent */
  233. xpos = BIO_printf(bio_out, indent);
  234. else
  235. /* Otherwise prepend a ", " */
  236. xpos += BIO_printf(bio_out, ", ");
  237. if(verbose == 1)
  238. {
  239. /* We're just listing names, comma-delimited */
  240. if((xpos > (int)strlen(indent)) &&
  241. (xpos + (int)strlen(name) > line_wrap))
  242. {
  243. BIO_printf(bio_out, "\n");
  244. xpos = BIO_printf(bio_out, indent);
  245. }
  246. xpos += BIO_printf(bio_out, "%s", name);
  247. }
  248. else
  249. {
  250. /* We're listing names plus descriptions */
  251. BIO_printf(bio_out, "%s: %s\n", name,
  252. (desc == NULL) ? "<no description>" : desc);
  253. /* ... and sometimes input flags */
  254. if((verbose >= 3) && !util_flags(bio_out, flags,
  255. indent))
  256. goto err;
  257. xpos = 0;
  258. }
  259. }
  260. OPENSSL_free(name); name = NULL;
  261. if(desc) { OPENSSL_free(desc); desc = NULL; }
  262. /* Move to the next command */
  263. num = ENGINE_ctrl(e, ENGINE_CTRL_GET_NEXT_CMD_TYPE,
  264. num, NULL, NULL);
  265. } while(num > 0);
  266. if(xpos > 0)
  267. BIO_printf(bio_out, "\n");
  268. ret = 1;
  269. err:
  270. if(cmds) sk_pop_free(cmds, identity);
  271. if(name) OPENSSL_free(name);
  272. if(desc) OPENSSL_free(desc);
  273. return ret;
  274. }
  275. static void util_do_cmds(ENGINE *e, STACK *cmds, BIO *bio_out, const char *indent)
  276. {
  277. int loop, res, num = sk_num(cmds);
  278. if(num < 0)
  279. {
  280. BIO_printf(bio_out, "[Error]: internal stack error\n");
  281. return;
  282. }
  283. for(loop = 0; loop < num; loop++)
  284. {
  285. char buf[256];
  286. const char *cmd, *arg;
  287. cmd = sk_value(cmds, loop);
  288. res = 1; /* assume success */
  289. /* Check if this command has no ":arg" */
  290. if((arg = strstr(cmd, ":")) == NULL)
  291. {
  292. if(!ENGINE_ctrl_cmd_string(e, cmd, NULL, 0))
  293. res = 0;
  294. }
  295. else
  296. {
  297. if((int)(arg - cmd) > 254)
  298. {
  299. BIO_printf(bio_out,"[Error]: command name too long\n");
  300. return;
  301. }
  302. memcpy(buf, cmd, (int)(arg - cmd));
  303. buf[arg-cmd] = '\0';
  304. arg++; /* Move past the ":" */
  305. /* Call the command with the argument */
  306. if(!ENGINE_ctrl_cmd_string(e, buf, arg, 0))
  307. res = 0;
  308. }
  309. if(res)
  310. BIO_printf(bio_out, "[Success]: %s\n", cmd);
  311. else
  312. {
  313. BIO_printf(bio_out, "[Failure]: %s\n", cmd);
  314. ERR_print_errors(bio_out);
  315. }
  316. }
  317. }
  318. int MAIN(int, char **);
  319. int MAIN(int argc, char **argv)
  320. {
  321. int ret=1,i;
  322. const char **pp;
  323. int verbose=0, list_cap=0, test_avail=0, test_avail_noise = 0;
  324. ENGINE *e;
  325. STACK *engines = sk_new_null();
  326. STACK *pre_cmds = sk_new_null();
  327. STACK *post_cmds = sk_new_null();
  328. int badops=1;
  329. BIO *bio_out=NULL;
  330. const char *indent = " ";
  331. apps_startup();
  332. SSL_load_error_strings();
  333. if (bio_err == NULL)
  334. bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);
  335. if (!load_config(bio_err, NULL))
  336. goto end;
  337. bio_out=BIO_new_fp(stdout,BIO_NOCLOSE);
  338. #ifdef OPENSSL_SYS_VMS
  339. {
  340. BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  341. bio_out = BIO_push(tmpbio, bio_out);
  342. }
  343. #endif
  344. argc--;
  345. argv++;
  346. while (argc >= 1)
  347. {
  348. if (strncmp(*argv,"-v",2) == 0)
  349. {
  350. if(strspn(*argv + 1, "v") < strlen(*argv + 1))
  351. goto skip_arg_loop;
  352. if((verbose=strlen(*argv + 1)) > 4)
  353. goto skip_arg_loop;
  354. }
  355. else if (strcmp(*argv,"-c") == 0)
  356. list_cap=1;
  357. else if (strncmp(*argv,"-t",2) == 0)
  358. {
  359. test_avail=1;
  360. if(strspn(*argv + 1, "t") < strlen(*argv + 1))
  361. goto skip_arg_loop;
  362. if((test_avail_noise = strlen(*argv + 1) - 1) > 1)
  363. goto skip_arg_loop;
  364. }
  365. else if (strcmp(*argv,"-pre") == 0)
  366. {
  367. argc--; argv++;
  368. if (argc == 0)
  369. goto skip_arg_loop;
  370. sk_push(pre_cmds,*argv);
  371. }
  372. else if (strcmp(*argv,"-post") == 0)
  373. {
  374. argc--; argv++;
  375. if (argc == 0)
  376. goto skip_arg_loop;
  377. sk_push(post_cmds,*argv);
  378. }
  379. else if ((strncmp(*argv,"-h",2) == 0) ||
  380. (strcmp(*argv,"-?") == 0))
  381. goto skip_arg_loop;
  382. else
  383. sk_push(engines,*argv);
  384. argc--;
  385. argv++;
  386. }
  387. /* Looks like everything went OK */
  388. badops = 0;
  389. skip_arg_loop:
  390. if (badops)
  391. {
  392. for (pp=engine_usage; (*pp != NULL); pp++)
  393. BIO_printf(bio_err,"%s",*pp);
  394. goto end;
  395. }
  396. if (sk_num(engines) == 0)
  397. {
  398. for(e = ENGINE_get_first(); e != NULL; e = ENGINE_get_next(e))
  399. {
  400. sk_push(engines,(char *)ENGINE_get_id(e));
  401. }
  402. }
  403. for (i=0; i<sk_num(engines); i++)
  404. {
  405. const char *id = sk_value(engines,i);
  406. if ((e = ENGINE_by_id(id)) != NULL)
  407. {
  408. const char *name = ENGINE_get_name(e);
  409. /* Do "id" first, then "name". Easier to auto-parse. */
  410. BIO_printf(bio_out, "(%s) %s\n", id, name);
  411. util_do_cmds(e, pre_cmds, bio_out, indent);
  412. if (strcmp(ENGINE_get_id(e), id) != 0)
  413. {
  414. BIO_printf(bio_out, "Loaded: (%s) %s\n",
  415. ENGINE_get_id(e), ENGINE_get_name(e));
  416. }
  417. if (list_cap)
  418. {
  419. int cap_size = 256;
  420. char *cap_buf = NULL;
  421. int k,n;
  422. const int *nids;
  423. ENGINE_CIPHERS_PTR fn_c;
  424. ENGINE_DIGESTS_PTR fn_d;
  425. if (ENGINE_get_RSA(e) != NULL
  426. && !append_buf(&cap_buf, "RSA",
  427. &cap_size, 256))
  428. goto end;
  429. if (ENGINE_get_DSA(e) != NULL
  430. && !append_buf(&cap_buf, "DSA",
  431. &cap_size, 256))
  432. goto end;
  433. if (ENGINE_get_DH(e) != NULL
  434. && !append_buf(&cap_buf, "DH",
  435. &cap_size, 256))
  436. goto end;
  437. if (ENGINE_get_RAND(e) != NULL
  438. && !append_buf(&cap_buf, "RAND",
  439. &cap_size, 256))
  440. goto end;
  441. fn_c = ENGINE_get_ciphers(e);
  442. if(!fn_c) goto skip_ciphers;
  443. n = fn_c(e, NULL, &nids, 0);
  444. for(k=0 ; k < n ; ++k)
  445. if(!append_buf(&cap_buf,
  446. OBJ_nid2sn(nids[k]),
  447. &cap_size, 256))
  448. goto end;
  449. skip_ciphers:
  450. fn_d = ENGINE_get_digests(e);
  451. if(!fn_d) goto skip_digests;
  452. n = fn_d(e, NULL, &nids, 0);
  453. for(k=0 ; k < n ; ++k)
  454. if(!append_buf(&cap_buf,
  455. OBJ_nid2sn(nids[k]),
  456. &cap_size, 256))
  457. goto end;
  458. skip_digests:
  459. if (cap_buf && (*cap_buf != '\0'))
  460. BIO_printf(bio_out, " [%s]\n", cap_buf);
  461. OPENSSL_free(cap_buf);
  462. }
  463. if(test_avail)
  464. {
  465. BIO_printf(bio_out, "%s", indent);
  466. if (ENGINE_init(e))
  467. {
  468. BIO_printf(bio_out, "[ available ]\n");
  469. util_do_cmds(e, post_cmds, bio_out, indent);
  470. ENGINE_finish(e);
  471. }
  472. else
  473. {
  474. BIO_printf(bio_out, "[ unavailable ]\n");
  475. if(test_avail_noise)
  476. ERR_print_errors_fp(stdout);
  477. ERR_clear_error();
  478. }
  479. }
  480. if((verbose > 0) && !util_verbose(e, verbose, bio_out, indent))
  481. goto end;
  482. ENGINE_free(e);
  483. }
  484. else
  485. ERR_print_errors(bio_err);
  486. }
  487. ret=0;
  488. end:
  489. ERR_print_errors(bio_err);
  490. sk_pop_free(engines, identity);
  491. sk_pop_free(pre_cmds, identity);
  492. sk_pop_free(post_cmds, identity);
  493. if (bio_out != NULL) BIO_free_all(bio_out);
  494. apps_shutdown();
  495. OPENSSL_EXIT(ret);
  496. }
  497. #else
  498. # if PEDANTIC
  499. static void *dummy=&dummy;
  500. # endif
  501. #endif