openssl.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]
  56. */
  57. /* ====================================================================
  58. * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
  59. *
  60. * Redistribution and use in source and binary forms, with or without
  61. * modification, are permitted provided that the following conditions
  62. * are met:
  63. *
  64. * 1. Redistributions of source code must retain the above copyright
  65. * notice, this list of conditions and the following disclaimer.
  66. *
  67. * 2. Redistributions in binary form must reproduce the above copyright
  68. * notice, this list of conditions and the following disclaimer in
  69. * the documentation and/or other materials provided with the
  70. * distribution.
  71. *
  72. * 3. All advertising materials mentioning features or use of this
  73. * software must display the following acknowledgment:
  74. * "This product includes software developed by the OpenSSL Project
  75. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  76. *
  77. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  78. * endorse or promote products derived from this software without
  79. * prior written permission. For written permission, please contact
  80. * openssl-core@openssl.org.
  81. *
  82. * 5. Products derived from this software may not be called "OpenSSL"
  83. * nor may "OpenSSL" appear in their names without prior written
  84. * permission of the OpenSSL Project.
  85. *
  86. * 6. Redistributions of any form whatsoever must retain the following
  87. * acknowledgment:
  88. * "This product includes software developed by the OpenSSL Project
  89. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  90. *
  91. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  92. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  93. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  94. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  95. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  96. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  97. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  98. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  99. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  100. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  101. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  102. * OF THE POSSIBILITY OF SUCH DAMAGE.
  103. * ====================================================================
  104. *
  105. * This product includes cryptographic software written by Eric Young
  106. * (eay@cryptsoft.com). This product includes software written by Tim
  107. * Hudson (tjh@cryptsoft.com).
  108. *
  109. */
  110. #include <stdio.h>
  111. #include <string.h>
  112. #include <stdlib.h>
  113. #include <openssl/bio.h>
  114. #include <openssl/crypto.h>
  115. #include <openssl/rand.h>
  116. #include <openssl/lhash.h>
  117. #include <openssl/conf.h>
  118. #include <openssl/x509.h>
  119. #include <openssl/pem.h>
  120. #include <openssl/ssl.h>
  121. #ifndef OPENSSL_NO_ENGINE
  122. # include <openssl/engine.h>
  123. #endif
  124. #include <openssl/err.h>
  125. #ifdef OPENSSL_FIPS
  126. # include <openssl/fips.h>
  127. #endif
  128. #define USE_SOCKETS /* needed for the _O_BINARY defs in the MS world */
  129. #include "s_apps.h"
  130. /* Needed to get the other O_xxx flags. */
  131. #ifdef OPENSSL_SYS_VMS
  132. # include <unixio.h>
  133. #endif
  134. #define INCLUDE_FUNCTION_TABLE
  135. #include "apps.h"
  136. #ifdef OPENSSL_NO_CAMELLIA
  137. # define FORMAT "%-15s"
  138. # define COLUMNS 5
  139. #else
  140. # define FORMAT "%-18s"
  141. # define COLUMNS 4
  142. #endif
  143. /* Special sentinel to exit the program. */
  144. #define EXIT_THE_PROGRAM (-1)
  145. /*
  146. * The LHASH callbacks ("hash" & "cmp") have been replaced by functions with
  147. * the base prototypes (we cast each variable inside the function to the
  148. * required type of "FUNCTION*"). This removes the necessity for
  149. * macro-generated wrapper functions.
  150. */
  151. static LHASH_OF(FUNCTION) *prog_init(void);
  152. static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[]);
  153. static void list_pkey(void);
  154. static void list_type(FUNC_TYPE ft);
  155. static void list_disabled(void);
  156. char *default_config_file = NULL;
  157. static CONF *config = NULL;
  158. BIO *bio_in = NULL;
  159. BIO *bio_out = NULL;
  160. BIO *bio_err = NULL;
  161. static int apps_startup()
  162. {
  163. #ifdef SIGPIPE
  164. signal(SIGPIPE, SIG_IGN);
  165. #endif
  166. /* Set non-default library initialisation settings */
  167. if (!OPENSSL_init_crypto(OPENSSL_INIT_ENGINE_ALL_BUILTIN
  168. | OPENSSL_INIT_LOAD_CONFIG, NULL))
  169. return 0;
  170. setup_ui_method();
  171. return 1;
  172. }
  173. static void apps_shutdown()
  174. {
  175. destroy_ui_method();
  176. }
  177. static char *make_config_name()
  178. {
  179. const char *t;
  180. size_t len;
  181. char *p;
  182. if ((t = getenv("OPENSSL_CONF")) != NULL)
  183. return OPENSSL_strdup(t);
  184. t = X509_get_default_cert_area();
  185. len = strlen(t) + 1 + strlen(OPENSSL_CONF) + 1;
  186. p = app_malloc(len, "config filename buffer");
  187. strcpy(p, t);
  188. #ifndef OPENSSL_SYS_VMS
  189. strcat(p, "/");
  190. #endif
  191. strcat(p, OPENSSL_CONF);
  192. return p;
  193. }
  194. static void lock_dbg_cb(int mode, int type, const char *file, int line)
  195. {
  196. static int modes[CRYPTO_NUM_LOCKS];
  197. const char *errstr = NULL;
  198. int rw = mode & (CRYPTO_READ | CRYPTO_WRITE);
  199. if (rw != CRYPTO_READ && rw != CRYPTO_WRITE) {
  200. errstr = "invalid mode";
  201. goto err;
  202. }
  203. if (type < 0 || type >= CRYPTO_NUM_LOCKS) {
  204. errstr = "type out of bounds";
  205. goto err;
  206. }
  207. if (mode & CRYPTO_LOCK) {
  208. if (modes[type]) {
  209. errstr = "already locked";
  210. /* must not happen in a single-threaded program --> deadlock! */
  211. goto err;
  212. }
  213. modes[type] = rw;
  214. } else if (mode & CRYPTO_UNLOCK) {
  215. if (!modes[type]) {
  216. errstr = "not locked";
  217. goto err;
  218. }
  219. if (modes[type] != rw) {
  220. errstr = (rw == CRYPTO_READ) ?
  221. "CRYPTO_r_unlock on write lock" :
  222. "CRYPTO_w_unlock on read lock";
  223. }
  224. modes[type] = 0;
  225. } else {
  226. errstr = "invalid mode";
  227. goto err;
  228. }
  229. err:
  230. if (errstr) {
  231. BIO_printf(bio_err,
  232. "openssl (lock_dbg_cb): %s (mode=%d, type=%d) at %s:%d\n",
  233. errstr, mode, type, file, line);
  234. }
  235. }
  236. #if defined( OPENSSL_SYS_VMS)
  237. extern char **copy_argv(int *argc, char **argv);
  238. #endif
  239. int main(int argc, char *argv[])
  240. {
  241. FUNCTION f, *fp;
  242. LHASH_OF(FUNCTION) *prog = NULL;
  243. char **copied_argv = NULL;
  244. char *p, *pname;
  245. char buf[1024];
  246. const char *prompt;
  247. ARGS arg;
  248. int first, n, i, ret = 0;
  249. arg.argv = NULL;
  250. arg.size = 0;
  251. /* Set up some of the environment. */
  252. default_config_file = make_config_name();
  253. bio_in = dup_bio_in(FORMAT_TEXT);
  254. bio_out = dup_bio_out(FORMAT_TEXT);
  255. bio_err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
  256. #if defined( OPENSSL_SYS_VMS)
  257. copied_argv = argv = copy_argv(&argc, argv);
  258. #endif
  259. p = getenv("OPENSSL_DEBUG_MEMORY");
  260. if (p != NULL && strcmp(p, "on") == 0)
  261. CRYPTO_set_mem_debug(1);
  262. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
  263. CRYPTO_set_locking_callback(lock_dbg_cb);
  264. if (getenv("OPENSSL_FIPS")) {
  265. #ifdef OPENSSL_FIPS
  266. if (!FIPS_mode_set(1)) {
  267. ERR_print_errors(bio_err);
  268. return 1;
  269. }
  270. #else
  271. BIO_printf(bio_err, "FIPS mode not supported.\n");
  272. return 1;
  273. #endif
  274. }
  275. if (!apps_startup())
  276. goto end;
  277. prog = prog_init();
  278. pname = opt_progname(argv[0]);
  279. /* first check the program name */
  280. f.name = pname;
  281. fp = lh_FUNCTION_retrieve(prog, &f);
  282. if (fp != NULL) {
  283. argv[0] = pname;
  284. ret = fp->func(argc, argv);
  285. goto end;
  286. }
  287. /* If there is stuff on the command line, run with that. */
  288. if (argc != 1) {
  289. argc--;
  290. argv++;
  291. ret = do_cmd(prog, argc, argv);
  292. if (ret < 0)
  293. ret = 0;
  294. goto end;
  295. }
  296. /* ok, lets enter interactive mode */
  297. for (;;) {
  298. ret = 0;
  299. /* Read a line, continue reading if line ends with \ */
  300. for (p = buf, n = sizeof buf, i = 0, first = 1; n > 0; first = 0) {
  301. prompt = first ? "OpenSSL> " : "> ";
  302. p[0] = '\0';
  303. #ifndef READLINE
  304. fputs(prompt, stdout);
  305. fflush(stdout);
  306. if (!fgets(p, n, stdin))
  307. goto end;
  308. if (p[0] == '\0')
  309. goto end;
  310. i = strlen(p);
  311. if (i <= 1)
  312. break;
  313. if (p[i - 2] != '\\')
  314. break;
  315. i -= 2;
  316. p += i;
  317. n -= i;
  318. #else
  319. {
  320. extern char *readline(const char *);
  321. extern void add_history(const char *cp);
  322. char *text;
  323. char *text = readline(prompt);
  324. if (text == NULL)
  325. goto end;
  326. i = strlen(text);
  327. if (i == 0 || i > n)
  328. break;
  329. if (text[i - 1] != '\\') {
  330. p += strlen(strcpy(p, text));
  331. free(text);
  332. add_history(buf);
  333. break;
  334. }
  335. text[i - 1] = '\0';
  336. p += strlen(strcpy(p, text));
  337. free(text);
  338. n -= i;
  339. }
  340. #endif
  341. }
  342. if (!chopup_args(&arg, buf)) {
  343. BIO_printf(bio_err, "Can't parse (no memory?)\n");
  344. break;
  345. }
  346. ret = do_cmd(prog, arg.argc, arg.argv);
  347. if (ret == EXIT_THE_PROGRAM) {
  348. ret = 0;
  349. goto end;
  350. }
  351. if (ret != 0)
  352. BIO_printf(bio_err, "error in %s\n", arg.argv[0]);
  353. (void)BIO_flush(bio_out);
  354. (void)BIO_flush(bio_err);
  355. }
  356. ret = 1;
  357. end:
  358. OPENSSL_free(copied_argv);
  359. OPENSSL_free(default_config_file);
  360. NCONF_free(config);
  361. config = NULL;
  362. lh_FUNCTION_free(prog);
  363. OPENSSL_free(arg.argv);
  364. BIO_free(bio_in);
  365. BIO_free_all(bio_out);
  366. apps_shutdown();
  367. #ifndef OPENSSL_NO_CRYPTO_MDEBUG
  368. if (CRYPTO_mem_leaks(bio_err) <= 0)
  369. ret = 1;
  370. #endif
  371. BIO_free(bio_err);
  372. EXIT(ret);
  373. }
  374. OPTIONS exit_options[] = {
  375. {NULL}
  376. };
  377. static void list_cipher_fn(const EVP_CIPHER *c,
  378. const char *from, const char *to, void *arg)
  379. {
  380. if (c)
  381. BIO_printf(arg, "%s\n", EVP_CIPHER_name(c));
  382. else {
  383. if (!from)
  384. from = "<undefined>";
  385. if (!to)
  386. to = "<undefined>";
  387. BIO_printf(arg, "%s => %s\n", from, to);
  388. }
  389. }
  390. static void list_md_fn(const EVP_MD *m,
  391. const char *from, const char *to, void *arg)
  392. {
  393. if (m)
  394. BIO_printf(arg, "%s\n", EVP_MD_name(m));
  395. else {
  396. if (!from)
  397. from = "<undefined>";
  398. if (!to)
  399. to = "<undefined>";
  400. BIO_printf((BIO *)arg, "%s => %s\n", from, to);
  401. }
  402. }
  403. /* Unified enum for help and list commands. */
  404. typedef enum HELPLIST_CHOICE {
  405. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  406. OPT_COMMANDS, OPT_DIGEST_COMMANDS,
  407. OPT_DIGEST_ALGORITHMS, OPT_CIPHER_COMMANDS, OPT_CIPHER_ALGORITHMS,
  408. OPT_PK_ALGORITHMS, OPT_DISABLED
  409. } HELPLIST_CHOICE;
  410. OPTIONS list_options[] = {
  411. {"help", OPT_HELP, '-', "Display this summary"},
  412. {"commands", OPT_COMMANDS, '-', "List of standard commands"},
  413. {"digest-commands", OPT_DIGEST_COMMANDS, '-',
  414. "List of message digest commands"},
  415. {"digest-algorithms", OPT_DIGEST_ALGORITHMS, '-',
  416. "List of message digest algorithms"},
  417. {"cipher-commands", OPT_CIPHER_COMMANDS, '-', "List of cipher commands"},
  418. {"cipher-algorithms", OPT_CIPHER_ALGORITHMS, '-',
  419. "List of cipher algorithms"},
  420. {"public-key-algorithms", OPT_PK_ALGORITHMS, '-',
  421. "List of public key algorithms"},
  422. {"disabled", OPT_DISABLED, '-',
  423. "List of disabled features"},
  424. {NULL}
  425. };
  426. int list_main(int argc, char **argv)
  427. {
  428. char *prog;
  429. HELPLIST_CHOICE o;
  430. int done = 0;
  431. prog = opt_init(argc, argv, list_options);
  432. while ((o = opt_next()) != OPT_EOF) {
  433. switch (o) {
  434. case OPT_EOF: /* Never hit, but suppresses warning */
  435. case OPT_ERR:
  436. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  437. return 1;
  438. case OPT_HELP:
  439. opt_help(list_options);
  440. break;
  441. case OPT_COMMANDS:
  442. list_type(FT_general);
  443. break;
  444. case OPT_DIGEST_COMMANDS:
  445. list_type(FT_md);
  446. break;
  447. case OPT_DIGEST_ALGORITHMS:
  448. EVP_MD_do_all_sorted(list_md_fn, bio_out);
  449. break;
  450. case OPT_CIPHER_COMMANDS:
  451. list_type(FT_cipher);
  452. break;
  453. case OPT_CIPHER_ALGORITHMS:
  454. EVP_CIPHER_do_all_sorted(list_cipher_fn, bio_out);
  455. break;
  456. case OPT_PK_ALGORITHMS:
  457. list_pkey();
  458. break;
  459. case OPT_DISABLED:
  460. list_disabled();
  461. break;
  462. }
  463. done = 1;
  464. }
  465. if (!done) {
  466. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  467. return 1;
  468. }
  469. return 0;
  470. }
  471. OPTIONS help_options[] = {
  472. {"help", OPT_HELP, '-', "Display this summary"},
  473. {NULL}
  474. };
  475. int help_main(int argc, char **argv)
  476. {
  477. FUNCTION *fp;
  478. int i, nl;
  479. FUNC_TYPE tp;
  480. char *prog;
  481. HELPLIST_CHOICE o;
  482. prog = opt_init(argc, argv, help_options);
  483. while ((o = opt_next()) != OPT_EOF) {
  484. switch (o) {
  485. default:
  486. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  487. return 1;
  488. case OPT_HELP:
  489. opt_help(help_options);
  490. return 0;
  491. }
  492. }
  493. argc = opt_num_rest();
  494. argv = opt_rest();
  495. if (argc != 0) {
  496. BIO_printf(bio_err, "Usage: %s\n", prog);
  497. return 1;
  498. }
  499. BIO_printf(bio_err, "\nStandard commands");
  500. i = 0;
  501. tp = FT_none;
  502. for (fp = functions; fp->name != NULL; fp++) {
  503. nl = 0;
  504. if (((i++) % COLUMNS) == 0) {
  505. BIO_printf(bio_err, "\n");
  506. nl = 1;
  507. }
  508. if (fp->type != tp) {
  509. tp = fp->type;
  510. if (!nl)
  511. BIO_printf(bio_err, "\n");
  512. if (tp == FT_md) {
  513. i = 1;
  514. BIO_printf(bio_err,
  515. "\nMessage Digest commands (see the `dgst' command for more details)\n");
  516. } else if (tp == FT_cipher) {
  517. i = 1;
  518. BIO_printf(bio_err,
  519. "\nCipher commands (see the `enc' command for more details)\n");
  520. }
  521. }
  522. BIO_printf(bio_err, FORMAT, fp->name);
  523. }
  524. BIO_printf(bio_err, "\n\n");
  525. return 0;
  526. }
  527. int exit_main(int argc, char **argv)
  528. {
  529. return EXIT_THE_PROGRAM;
  530. }
  531. static void list_type(FUNC_TYPE ft)
  532. {
  533. FUNCTION *fp;
  534. int i = 0;
  535. for (fp = functions; fp->name != NULL; fp++)
  536. if (fp->type == ft) {
  537. if ((i++ % COLUMNS) == 0)
  538. BIO_printf(bio_out, "\n");
  539. BIO_printf(bio_out, FORMAT, fp->name);
  540. }
  541. BIO_printf(bio_out, "\n");
  542. }
  543. static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[])
  544. {
  545. FUNCTION f, *fp;
  546. if (argc <= 0 || argv[0] == NULL)
  547. return (0);
  548. f.name = argv[0];
  549. fp = lh_FUNCTION_retrieve(prog, &f);
  550. if (fp == NULL) {
  551. if (EVP_get_digestbyname(argv[0])) {
  552. f.type = FT_md;
  553. f.func = dgst_main;
  554. fp = &f;
  555. } else if (EVP_get_cipherbyname(argv[0])) {
  556. f.type = FT_cipher;
  557. f.func = enc_main;
  558. fp = &f;
  559. }
  560. }
  561. if (fp != NULL) {
  562. return (fp->func(argc, argv));
  563. }
  564. if ((strncmp(argv[0], "no-", 3)) == 0) {
  565. /*
  566. * User is asking if foo is unsupported, by trying to "run" the
  567. * no-foo command. Strange.
  568. */
  569. f.name = argv[0] + 3;
  570. if (lh_FUNCTION_retrieve(prog, &f) == NULL) {
  571. BIO_printf(bio_out, "%s\n", argv[0]);
  572. return (0);
  573. }
  574. BIO_printf(bio_out, "%s\n", argv[0] + 3);
  575. return 1;
  576. }
  577. if (strcmp(argv[0], "quit") == 0 || strcmp(argv[0], "q") == 0 ||
  578. strcmp(argv[0], "exit") == 0 || strcmp(argv[0], "bye") == 0)
  579. /* Special value to mean "exit the program. */
  580. return EXIT_THE_PROGRAM;
  581. BIO_printf(bio_err, "Invalid command '%s'; type \"help\" for a list.\n",
  582. argv[0]);
  583. return (1);
  584. }
  585. static void list_pkey(void)
  586. {
  587. int i;
  588. for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
  589. const EVP_PKEY_ASN1_METHOD *ameth;
  590. int pkey_id, pkey_base_id, pkey_flags;
  591. const char *pinfo, *pem_str;
  592. ameth = EVP_PKEY_asn1_get0(i);
  593. EVP_PKEY_asn1_get0_info(&pkey_id, &pkey_base_id, &pkey_flags,
  594. &pinfo, &pem_str, ameth);
  595. if (pkey_flags & ASN1_PKEY_ALIAS) {
  596. BIO_printf(bio_out, "Name: %s\n", OBJ_nid2ln(pkey_id));
  597. BIO_printf(bio_out, "\tAlias for: %s\n",
  598. OBJ_nid2ln(pkey_base_id));
  599. } else {
  600. BIO_printf(bio_out, "Name: %s\n", pinfo);
  601. BIO_printf(bio_out, "\tType: %s Algorithm\n",
  602. pkey_flags & ASN1_PKEY_DYNAMIC ?
  603. "External" : "Builtin");
  604. BIO_printf(bio_out, "\tOID: %s\n", OBJ_nid2ln(pkey_id));
  605. if (pem_str == NULL)
  606. pem_str = "(none)";
  607. BIO_printf(bio_out, "\tPEM string: %s\n", pem_str);
  608. }
  609. }
  610. }
  611. static int function_cmp(const FUNCTION * a, const FUNCTION * b)
  612. {
  613. return strncmp(a->name, b->name, 8);
  614. }
  615. static unsigned long function_hash(const FUNCTION * a)
  616. {
  617. return lh_strhash(a->name);
  618. }
  619. static int SortFnByName(const void *_f1, const void *_f2)
  620. {
  621. const FUNCTION *f1 = _f1;
  622. const FUNCTION *f2 = _f2;
  623. if (f1->type != f2->type)
  624. return f1->type - f2->type;
  625. return strcmp(f1->name, f2->name);
  626. }
  627. static void list_disabled(void)
  628. {
  629. BIO_puts(bio_out, "Disabled algorithms:\n");
  630. #ifdef OPENSSL_NO_AES
  631. BIO_puts(bio_out, "AES\n");
  632. #endif
  633. #ifdef OPENSSL_NO_BF
  634. BIO_puts(bio_out, "BF\n");
  635. #endif
  636. #ifdef OPENSSL_NO_CAMELLIA
  637. BIO_puts(bio_out, "CAMELLIA\n");
  638. #endif
  639. #ifdef OPENSSL_NO_CAST
  640. BIO_puts(bio_out, "CAST\n");
  641. #endif
  642. #ifdef OPENSSL_NO_CMAC
  643. BIO_puts(bio_out, "CMAC\n");
  644. #endif
  645. #ifdef OPENSSL_NO_CMS
  646. BIO_puts(bio_out, "CMS\n");
  647. #endif
  648. #ifdef OPENSSL_NO_COMP
  649. BIO_puts(bio_out, "COMP\n");
  650. #endif
  651. #ifdef OPENSSL_NO_DES
  652. BIO_puts(bio_out, "DES\n");
  653. #endif
  654. #ifdef OPENSSL_NO_DGRAM
  655. BIO_puts(bio_out, "DGRAM\n");
  656. #endif
  657. #ifdef OPENSSL_NO_DH
  658. BIO_puts(bio_out, "DH\n");
  659. #endif
  660. #ifdef OPENSSL_NO_DSA
  661. BIO_puts(bio_out, "DSA\n");
  662. #endif
  663. #if defined(OPENSSL_NO_DTLS)
  664. BIO_puts(bio_out, "DTLS\n");
  665. #endif
  666. #if defined(OPENSSL_NO_DTLS1)
  667. BIO_puts(bio_out, "DTLS1\n");
  668. #endif
  669. #if defined(OPENSSL_NO_DTLS1_2)
  670. BIO_puts(bio_out, "DTLS1_2\n");
  671. #endif
  672. #ifdef OPENSSL_NO_EC
  673. BIO_puts(bio_out, "EC\n");
  674. #endif
  675. #ifdef OPENSSL_NO_EC2M
  676. BIO_puts(bio_out, "EC2M\n");
  677. #endif
  678. #ifdef OPENSSL_NO_ENGINE
  679. BIO_puts(bio_out, "ENGINE\n");
  680. #endif
  681. #ifdef OPENSSL_NO_GOST
  682. BIO_puts(bio_out, "GOST\n");
  683. #endif
  684. #ifdef OPENSSL_NO_HEARTBEATS
  685. BIO_puts(bio_out, "HEARTBEATS\n");
  686. #endif
  687. #ifdef OPENSSL_NO_HMAC
  688. BIO_puts(bio_out, "HMAC\n");
  689. #endif
  690. #ifdef OPENSSL_NO_IDEA
  691. BIO_puts(bio_out, "IDEA\n");
  692. #endif
  693. #ifdef OPENSSL_NO_JPAKE
  694. BIO_puts(bio_out, "JPAKE\n");
  695. #endif
  696. #ifdef OPENSSL_NO_MD2
  697. BIO_puts(bio_out, "MD2\n");
  698. #endif
  699. #ifdef OPENSSL_NO_MD4
  700. BIO_puts(bio_out, "MD4\n");
  701. #endif
  702. #ifdef OPENSSL_NO_MD5
  703. BIO_puts(bio_out, "MD5\n");
  704. #endif
  705. #ifdef OPENSSL_NO_MDC2
  706. BIO_puts(bio_out, "MDC2\n");
  707. #endif
  708. #ifdef OPENSSL_NO_OCB
  709. BIO_puts(bio_out, "OCB\n");
  710. #endif
  711. #ifdef OPENSSL_NO_OCSP
  712. BIO_puts(bio_out, "OCSP\n");
  713. #endif
  714. #ifdef OPENSSL_NO_PSK
  715. BIO_puts(bio_out, "PSK\n");
  716. #endif
  717. #ifdef OPENSSL_NO_RC2
  718. BIO_puts(bio_out, "RC2\n");
  719. #endif
  720. #ifdef OPENSSL_NO_RC4
  721. BIO_puts(bio_out, "RC4\n");
  722. #endif
  723. #ifdef OPENSSL_NO_RC5
  724. BIO_puts(bio_out, "RC5\n");
  725. #endif
  726. #ifdef OPENSSL_NO_RMD160
  727. BIO_puts(bio_out, "RMD160\n");
  728. #endif
  729. #ifdef OPENSSL_NO_RSA
  730. BIO_puts(bio_out, "RSA\n");
  731. #endif
  732. #ifdef OPENSSL_NO_SCRYPT
  733. BIO_puts(bio_out, "SCRYPT\n");
  734. #endif
  735. #ifdef OPENSSL_NO_SCT
  736. BIO_puts(bio_out, "SCT\n");
  737. #endif
  738. #ifdef OPENSSL_NO_SCTP
  739. BIO_puts(bio_out, "SCTP\n");
  740. #endif
  741. #ifdef OPENSSL_NO_SEED
  742. BIO_puts(bio_out, "SEED\n");
  743. #endif
  744. #ifdef OPENSSL_NO_SHA
  745. BIO_puts(bio_out, "SHA\n");
  746. #endif
  747. #ifdef OPENSSL_NO_SOCK
  748. BIO_puts(bio_out, "SOCK\n");
  749. #endif
  750. #ifdef OPENSSL_NO_SRP
  751. BIO_puts(bio_out, "SRP\n");
  752. #endif
  753. #ifdef OPENSSL_NO_SRTP
  754. BIO_puts(bio_out, "SRTP\n");
  755. #endif
  756. #ifdef OPENSSL_NO_SSL
  757. BIO_puts(bio_out, "SSL\n");
  758. #endif
  759. #ifdef OPENSSL_NO_SSL3
  760. BIO_puts(bio_out, "SSL3\n");
  761. #endif
  762. #if defined(OPENSSL_NO_TLS)
  763. BIO_puts(bio_out, "TLS\n");
  764. #endif
  765. #ifdef OPENSSL_NO_TLS1
  766. BIO_puts(bio_out, "TLS1\n");
  767. #endif
  768. #ifdef OPENSSL_NO_TLS1_1
  769. BIO_puts(bio_out, "TLS1_1\n");
  770. #endif
  771. #ifdef OPENSSL_NO_TLS1_2
  772. BIO_puts(bio_out, "TLS1_2\n");
  773. #endif
  774. #ifdef OPENSSL_NO_WHIRLPOOL
  775. BIO_puts(bio_out, "WHIRLPOOL\n");
  776. #endif
  777. #ifndef ZLIB
  778. BIO_puts(bio_out, "ZLIB\n");
  779. #endif
  780. }
  781. static LHASH_OF(FUNCTION) *prog_init(void)
  782. {
  783. LHASH_OF(FUNCTION) *ret;
  784. FUNCTION *f;
  785. size_t i;
  786. /* Sort alphabetically within category. For nicer help displays. */
  787. for (i = 0, f = functions; f->name != NULL; ++f, ++i) ;
  788. qsort(functions, i, sizeof(*functions), SortFnByName);
  789. if ((ret = lh_FUNCTION_new(function_hash, function_cmp)) == NULL)
  790. return (NULL);
  791. for (f = functions; f->name != NULL; f++)
  792. (void)lh_FUNCTION_insert(ret, f);
  793. return (ret);
  794. }