engine.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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. #include <stdio.h>
  59. #include <stdlib.h>
  60. #include <string.h>
  61. #ifdef OPENSSL_NO_STDIO
  62. #define APPS_WIN16
  63. #endif
  64. #include "apps.h"
  65. #include <openssl/err.h>
  66. #ifndef OPENSSL_NO_ENGINE
  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(char *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_OF(OPENSSL_STRING) *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_OPENSSL_STRING_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_puts(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_puts(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_OPENSSL_STRING_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_OF(OPENSSL_STRING) *cmds,
  276. BIO *bio_out, const char *indent)
  277. {
  278. int loop, res, num = sk_OPENSSL_STRING_num(cmds);
  279. if(num < 0)
  280. {
  281. BIO_printf(bio_out, "[Error]: internal stack error\n");
  282. return;
  283. }
  284. for(loop = 0; loop < num; loop++)
  285. {
  286. char buf[256];
  287. const char *cmd, *arg;
  288. cmd = sk_OPENSSL_STRING_value(cmds, loop);
  289. res = 1; /* assume success */
  290. /* Check if this command has no ":arg" */
  291. if((arg = strstr(cmd, ":")) == NULL)
  292. {
  293. if(!ENGINE_ctrl_cmd_string(e, cmd, NULL, 0))
  294. res = 0;
  295. }
  296. else
  297. {
  298. if((int)(arg - cmd) > 254)
  299. {
  300. BIO_printf(bio_out,"[Error]: command name too long\n");
  301. return;
  302. }
  303. memcpy(buf, cmd, (int)(arg - cmd));
  304. buf[arg-cmd] = '\0';
  305. arg++; /* Move past the ":" */
  306. /* Call the command with the argument */
  307. if(!ENGINE_ctrl_cmd_string(e, buf, arg, 0))
  308. res = 0;
  309. }
  310. if(res)
  311. BIO_printf(bio_out, "[Success]: %s\n", cmd);
  312. else
  313. {
  314. BIO_printf(bio_out, "[Failure]: %s\n", cmd);
  315. ERR_print_errors(bio_out);
  316. }
  317. }
  318. }
  319. int MAIN(int, char **);
  320. int MAIN(int argc, char **argv)
  321. {
  322. int ret=1,i;
  323. const char **pp;
  324. int verbose=0, list_cap=0, test_avail=0, test_avail_noise = 0;
  325. ENGINE *e;
  326. STACK_OF(OPENSSL_STRING) *engines = sk_OPENSSL_STRING_new_null();
  327. STACK_OF(OPENSSL_STRING) *pre_cmds = sk_OPENSSL_STRING_new_null();
  328. STACK_OF(OPENSSL_STRING) *post_cmds = sk_OPENSSL_STRING_new_null();
  329. int badops=1;
  330. BIO *bio_out=NULL;
  331. const char *indent = " ";
  332. apps_startup();
  333. SSL_load_error_strings();
  334. if (bio_err == NULL)
  335. bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);
  336. if (!load_config(bio_err, NULL))
  337. goto end;
  338. bio_out=BIO_new_fp(stdout,BIO_NOCLOSE);
  339. #ifdef OPENSSL_SYS_VMS
  340. {
  341. BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  342. bio_out = BIO_push(tmpbio, bio_out);
  343. }
  344. #endif
  345. argc--;
  346. argv++;
  347. while (argc >= 1)
  348. {
  349. if (strncmp(*argv,"-v",2) == 0)
  350. {
  351. if(strspn(*argv + 1, "v") < strlen(*argv + 1))
  352. goto skip_arg_loop;
  353. if((verbose=strlen(*argv + 1)) > 4)
  354. goto skip_arg_loop;
  355. }
  356. else if (strcmp(*argv,"-c") == 0)
  357. list_cap=1;
  358. else if (strncmp(*argv,"-t",2) == 0)
  359. {
  360. test_avail=1;
  361. if(strspn(*argv + 1, "t") < strlen(*argv + 1))
  362. goto skip_arg_loop;
  363. if((test_avail_noise = strlen(*argv + 1) - 1) > 1)
  364. goto skip_arg_loop;
  365. }
  366. else if (strcmp(*argv,"-pre") == 0)
  367. {
  368. argc--; argv++;
  369. if (argc == 0)
  370. goto skip_arg_loop;
  371. sk_OPENSSL_STRING_push(pre_cmds,*argv);
  372. }
  373. else if (strcmp(*argv,"-post") == 0)
  374. {
  375. argc--; argv++;
  376. if (argc == 0)
  377. goto skip_arg_loop;
  378. sk_OPENSSL_STRING_push(post_cmds,*argv);
  379. }
  380. else if ((strncmp(*argv,"-h",2) == 0) ||
  381. (strcmp(*argv,"-?") == 0))
  382. goto skip_arg_loop;
  383. else
  384. sk_OPENSSL_STRING_push(engines,*argv);
  385. argc--;
  386. argv++;
  387. }
  388. /* Looks like everything went OK */
  389. badops = 0;
  390. skip_arg_loop:
  391. if (badops)
  392. {
  393. for (pp=engine_usage; (*pp != NULL); pp++)
  394. BIO_printf(bio_err,"%s",*pp);
  395. goto end;
  396. }
  397. if (sk_OPENSSL_STRING_num(engines) == 0)
  398. {
  399. for(e = ENGINE_get_first(); e != NULL; e = ENGINE_get_next(e))
  400. {
  401. sk_OPENSSL_STRING_push(engines,(char *)ENGINE_get_id(e));
  402. }
  403. }
  404. for (i=0; i<sk_OPENSSL_STRING_num(engines); i++)
  405. {
  406. const char *id = sk_OPENSSL_STRING_value(engines,i);
  407. if ((e = ENGINE_by_id(id)) != NULL)
  408. {
  409. const char *name = ENGINE_get_name(e);
  410. /* Do "id" first, then "name". Easier to auto-parse. */
  411. BIO_printf(bio_out, "(%s) %s\n", id, name);
  412. util_do_cmds(e, pre_cmds, bio_out, indent);
  413. if (strcmp(ENGINE_get_id(e), id) != 0)
  414. {
  415. BIO_printf(bio_out, "Loaded: (%s) %s\n",
  416. ENGINE_get_id(e), ENGINE_get_name(e));
  417. }
  418. if (list_cap)
  419. {
  420. int cap_size = 256;
  421. char *cap_buf = NULL;
  422. int k,n;
  423. const int *nids;
  424. ENGINE_CIPHERS_PTR fn_c;
  425. ENGINE_DIGESTS_PTR fn_d;
  426. ENGINE_PKEY_METHS_PTR fn_pk;
  427. if (ENGINE_get_RSA(e) != NULL
  428. && !append_buf(&cap_buf, "RSA",
  429. &cap_size, 256))
  430. goto end;
  431. if (ENGINE_get_DSA(e) != NULL
  432. && !append_buf(&cap_buf, "DSA",
  433. &cap_size, 256))
  434. goto end;
  435. if (ENGINE_get_DH(e) != NULL
  436. && !append_buf(&cap_buf, "DH",
  437. &cap_size, 256))
  438. goto end;
  439. if (ENGINE_get_RAND(e) != NULL
  440. && !append_buf(&cap_buf, "RAND",
  441. &cap_size, 256))
  442. goto end;
  443. fn_c = ENGINE_get_ciphers(e);
  444. if(!fn_c) goto skip_ciphers;
  445. n = fn_c(e, NULL, &nids, 0);
  446. for(k=0 ; k < n ; ++k)
  447. if(!append_buf(&cap_buf,
  448. OBJ_nid2sn(nids[k]),
  449. &cap_size, 256))
  450. goto end;
  451. skip_ciphers:
  452. fn_d = ENGINE_get_digests(e);
  453. if(!fn_d) goto skip_digests;
  454. n = fn_d(e, NULL, &nids, 0);
  455. for(k=0 ; k < n ; ++k)
  456. if(!append_buf(&cap_buf,
  457. OBJ_nid2sn(nids[k]),
  458. &cap_size, 256))
  459. goto end;
  460. skip_digests:
  461. fn_pk = ENGINE_get_pkey_meths(e);
  462. if(!fn_pk) goto skip_pmeths;
  463. n = fn_pk(e, NULL, &nids, 0);
  464. for(k=0 ; k < n ; ++k)
  465. if(!append_buf(&cap_buf,
  466. OBJ_nid2sn(nids[k]),
  467. &cap_size, 256))
  468. goto end;
  469. skip_pmeths:
  470. if (cap_buf && (*cap_buf != '\0'))
  471. BIO_printf(bio_out, " [%s]\n", cap_buf);
  472. OPENSSL_free(cap_buf);
  473. }
  474. if(test_avail)
  475. {
  476. BIO_printf(bio_out, "%s", indent);
  477. if (ENGINE_init(e))
  478. {
  479. BIO_printf(bio_out, "[ available ]\n");
  480. util_do_cmds(e, post_cmds, bio_out, indent);
  481. ENGINE_finish(e);
  482. }
  483. else
  484. {
  485. BIO_printf(bio_out, "[ unavailable ]\n");
  486. if(test_avail_noise)
  487. ERR_print_errors_fp(stdout);
  488. ERR_clear_error();
  489. }
  490. }
  491. if((verbose > 0) && !util_verbose(e, verbose, bio_out, indent))
  492. goto end;
  493. ENGINE_free(e);
  494. }
  495. else
  496. ERR_print_errors(bio_err);
  497. }
  498. ret=0;
  499. end:
  500. ERR_print_errors(bio_err);
  501. sk_OPENSSL_STRING_pop_free(engines, identity);
  502. sk_OPENSSL_STRING_pop_free(pre_cmds, identity);
  503. sk_OPENSSL_STRING_pop_free(post_cmds, identity);
  504. if (bio_out != NULL) BIO_free_all(bio_out);
  505. apps_shutdown();
  506. OPENSSL_EXIT(ret);
  507. }
  508. #else
  509. # if PEDANTIC
  510. static void *dummy=&dummy;
  511. # endif
  512. #endif