openssl.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  1. /*
  2. * Copyright 1995-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 <internal/cryptlib.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include <openssl/bio.h>
  14. #include <openssl/crypto.h>
  15. #include <openssl/lhash.h>
  16. #include <openssl/conf.h>
  17. #include <openssl/x509.h>
  18. #include <openssl/pem.h>
  19. #include <openssl/ssl.h>
  20. #ifndef OPENSSL_NO_ENGINE
  21. # include <openssl/engine.h>
  22. #endif
  23. #include <openssl/err.h>
  24. #include "s_apps.h"
  25. /* Needed to get the other O_xxx flags. */
  26. #ifdef OPENSSL_SYS_VMS
  27. # include <unixio.h>
  28. #endif
  29. #include "apps.h"
  30. #define INCLUDE_FUNCTION_TABLE
  31. #include "progs.h"
  32. /* Structure to hold the number of columns to be displayed and the
  33. * field width used to display them.
  34. */
  35. typedef struct {
  36. int columns;
  37. int width;
  38. } DISPLAY_COLUMNS;
  39. /* Special sentinel to exit the program. */
  40. #define EXIT_THE_PROGRAM (-1)
  41. /*
  42. * The LHASH callbacks ("hash" & "cmp") have been replaced by functions with
  43. * the base prototypes (we cast each variable inside the function to the
  44. * required type of "FUNCTION*"). This removes the necessity for
  45. * macro-generated wrapper functions.
  46. */
  47. static LHASH_OF(FUNCTION) *prog_init(void);
  48. static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[]);
  49. static void list_pkey(void);
  50. static void list_pkey_meth(void);
  51. static void list_type(FUNC_TYPE ft, int one);
  52. static void list_disabled(void);
  53. char *default_config_file = NULL;
  54. BIO *bio_in = NULL;
  55. BIO *bio_out = NULL;
  56. BIO *bio_err = NULL;
  57. static void calculate_columns(DISPLAY_COLUMNS *dc)
  58. {
  59. FUNCTION *f;
  60. int len, maxlen = 0;
  61. for (f = functions; f->name != NULL; ++f)
  62. if (f->type == FT_general || f->type == FT_md || f->type == FT_cipher)
  63. if ((len = strlen(f->name)) > maxlen)
  64. maxlen = len;
  65. dc->width = maxlen + 2;
  66. dc->columns = (80 - 1) / dc->width;
  67. }
  68. static int apps_startup(void)
  69. {
  70. #ifdef SIGPIPE
  71. signal(SIGPIPE, SIG_IGN);
  72. #endif
  73. /* Set non-default library initialisation settings */
  74. if (!OPENSSL_init_ssl(OPENSSL_INIT_ENGINE_ALL_BUILTIN
  75. | OPENSSL_INIT_LOAD_CONFIG, NULL))
  76. return 0;
  77. setup_ui_method();
  78. return 1;
  79. }
  80. static void apps_shutdown(void)
  81. {
  82. destroy_ui_method();
  83. destroy_prefix_method();
  84. }
  85. static char *make_config_name(void)
  86. {
  87. const char *t;
  88. size_t len;
  89. char *p;
  90. if ((t = getenv("OPENSSL_CONF")) != NULL)
  91. return OPENSSL_strdup(t);
  92. t = X509_get_default_cert_area();
  93. len = strlen(t) + 1 + strlen(OPENSSL_CONF) + 1;
  94. p = app_malloc(len, "config filename buffer");
  95. strcpy(p, t);
  96. #ifndef OPENSSL_SYS_VMS
  97. strcat(p, "/");
  98. #endif
  99. strcat(p, OPENSSL_CONF);
  100. return p;
  101. }
  102. int main(int argc, char *argv[])
  103. {
  104. FUNCTION f, *fp;
  105. LHASH_OF(FUNCTION) *prog = NULL;
  106. char **copied_argv = NULL;
  107. char *p, *pname;
  108. char buf[1024];
  109. const char *prompt;
  110. ARGS arg;
  111. int first, n, i, ret = 0;
  112. arg.argv = NULL;
  113. arg.size = 0;
  114. /* Set up some of the environment. */
  115. default_config_file = make_config_name();
  116. bio_in = dup_bio_in(FORMAT_TEXT);
  117. bio_out = dup_bio_out(FORMAT_TEXT);
  118. bio_err = dup_bio_err(FORMAT_TEXT);
  119. #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
  120. copied_argv = argv = copy_argv(&argc, argv);
  121. #elif defined(_WIN32)
  122. /*
  123. * Replace argv[] with UTF-8 encoded strings.
  124. */
  125. win32_utf8argv(&argc, &argv);
  126. #endif
  127. p = getenv("OPENSSL_DEBUG_MEMORY");
  128. if (p != NULL && strcmp(p, "on") == 0)
  129. CRYPTO_set_mem_debug(1);
  130. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
  131. if (getenv("OPENSSL_FIPS")) {
  132. BIO_printf(bio_err, "FIPS mode not supported.\n");
  133. return 1;
  134. }
  135. if (!apps_startup()) {
  136. BIO_printf(bio_err,
  137. "FATAL: Startup failure (dev note: apps_startup() failed)\n");
  138. ERR_print_errors(bio_err);
  139. ret = 1;
  140. goto end;
  141. }
  142. prog = prog_init();
  143. pname = opt_progname(argv[0]);
  144. /* first check the program name */
  145. f.name = pname;
  146. fp = lh_FUNCTION_retrieve(prog, &f);
  147. if (fp != NULL) {
  148. argv[0] = pname;
  149. ret = fp->func(argc, argv);
  150. goto end;
  151. }
  152. /* If there is stuff on the command line, run with that. */
  153. if (argc != 1) {
  154. argc--;
  155. argv++;
  156. ret = do_cmd(prog, argc, argv);
  157. if (ret < 0)
  158. ret = 0;
  159. goto end;
  160. }
  161. /* ok, lets enter interactive mode */
  162. for (;;) {
  163. ret = 0;
  164. /* Read a line, continue reading if line ends with \ */
  165. for (p = buf, n = sizeof(buf), i = 0, first = 1; n > 0; first = 0) {
  166. prompt = first ? "OpenSSL> " : "> ";
  167. p[0] = '\0';
  168. #ifndef READLINE
  169. fputs(prompt, stdout);
  170. fflush(stdout);
  171. if (!fgets(p, n, stdin))
  172. goto end;
  173. if (p[0] == '\0')
  174. goto end;
  175. i = strlen(p);
  176. if (i <= 1)
  177. break;
  178. if (p[i - 2] != '\\')
  179. break;
  180. i -= 2;
  181. p += i;
  182. n -= i;
  183. #else
  184. {
  185. extern char *readline(const char *);
  186. extern void add_history(const char *cp);
  187. char *text;
  188. text = readline(prompt);
  189. if (text == NULL)
  190. goto end;
  191. i = strlen(text);
  192. if (i == 0 || i > n)
  193. break;
  194. if (text[i - 1] != '\\') {
  195. p += strlen(strcpy(p, text));
  196. free(text);
  197. add_history(buf);
  198. break;
  199. }
  200. text[i - 1] = '\0';
  201. p += strlen(strcpy(p, text));
  202. free(text);
  203. n -= i;
  204. }
  205. #endif
  206. }
  207. if (!chopup_args(&arg, buf)) {
  208. BIO_printf(bio_err, "Can't parse (no memory?)\n");
  209. break;
  210. }
  211. ret = do_cmd(prog, arg.argc, arg.argv);
  212. if (ret == EXIT_THE_PROGRAM) {
  213. ret = 0;
  214. goto end;
  215. }
  216. if (ret != 0)
  217. BIO_printf(bio_err, "error in %s\n", arg.argv[0]);
  218. (void)BIO_flush(bio_out);
  219. (void)BIO_flush(bio_err);
  220. }
  221. ret = 1;
  222. end:
  223. OPENSSL_free(copied_argv);
  224. OPENSSL_free(default_config_file);
  225. lh_FUNCTION_free(prog);
  226. OPENSSL_free(arg.argv);
  227. app_RAND_write();
  228. BIO_free(bio_in);
  229. BIO_free_all(bio_out);
  230. apps_shutdown();
  231. #ifndef OPENSSL_NO_CRYPTO_MDEBUG
  232. if (CRYPTO_mem_leaks(bio_err) <= 0)
  233. ret = 1;
  234. #endif
  235. BIO_free(bio_err);
  236. EXIT(ret);
  237. }
  238. static void list_cipher_fn(const EVP_CIPHER *c,
  239. const char *from, const char *to, void *arg)
  240. {
  241. if (c != NULL) {
  242. BIO_printf(arg, "%s\n", EVP_CIPHER_name(c));
  243. } else {
  244. if (from == NULL)
  245. from = "<undefined>";
  246. if (to == NULL)
  247. to = "<undefined>";
  248. BIO_printf(arg, "%s => %s\n", from, to);
  249. }
  250. }
  251. static void list_md_fn(const EVP_MD *m,
  252. const char *from, const char *to, void *arg)
  253. {
  254. if (m != NULL) {
  255. BIO_printf(arg, "%s\n", EVP_MD_name(m));
  256. } else {
  257. if (from == NULL)
  258. from = "<undefined>";
  259. if (to == NULL)
  260. to = "<undefined>";
  261. BIO_printf((BIO *)arg, "%s => %s\n", from, to);
  262. }
  263. }
  264. static void list_mac_fn(const EVP_MAC *m,
  265. const char *from, const char *to, void *arg)
  266. {
  267. if (m != NULL) {
  268. BIO_printf(arg, "%s\n", EVP_MAC_name(m));
  269. } else {
  270. if (from == NULL)
  271. from = "<undefined>";
  272. if (to == NULL)
  273. to = "<undefined>";
  274. BIO_printf(arg, "%s => %s\n", from, to);
  275. }
  276. }
  277. static void list_missing_help(void)
  278. {
  279. const FUNCTION *fp;
  280. const OPTIONS *o;
  281. for (fp = functions; fp->name != NULL; fp++) {
  282. if ((o = fp->help) != NULL) {
  283. /* If there is help, list what flags are not documented. */
  284. for ( ; o->name != NULL; o++) {
  285. if (o->helpstr == NULL)
  286. BIO_printf(bio_out, "%s %s\n", fp->name, o->name);
  287. }
  288. } else if (fp->func != dgst_main) {
  289. /* If not aliased to the dgst command, */
  290. BIO_printf(bio_out, "%s *\n", fp->name);
  291. }
  292. }
  293. }
  294. static void list_objects(void)
  295. {
  296. int max_nid = OBJ_new_nid(0);
  297. int i;
  298. char *oid_buf = NULL;
  299. int oid_size = 0;
  300. /* Skip 0, since that's NID_undef */
  301. for (i = 1; i < max_nid; i++) {
  302. const ASN1_OBJECT *obj = OBJ_nid2obj(i);
  303. const char *sn = OBJ_nid2sn(i);
  304. const char *ln = OBJ_nid2ln(i);
  305. int n = 0;
  306. /*
  307. * If one of the retrieved objects somehow generated an error,
  308. * we ignore it. The check for NID_undef below will detect the
  309. * error and simply skip to the next NID.
  310. */
  311. ERR_clear_error();
  312. if (OBJ_obj2nid(obj) == NID_undef)
  313. continue;
  314. if ((n = OBJ_obj2txt(NULL, 0, obj, 1)) == 0) {
  315. BIO_printf(bio_out, "# None-OID object: %s, %s\n", sn, ln);
  316. continue;
  317. }
  318. if (n < 0)
  319. break; /* Error */
  320. if (n > oid_size) {
  321. oid_buf = OPENSSL_realloc(oid_buf, n + 1);
  322. if (oid_buf == NULL) {
  323. BIO_printf(bio_err, "ERROR: Memory allocation\n");
  324. break; /* Error */
  325. }
  326. oid_size = n + 1;
  327. }
  328. if (OBJ_obj2txt(oid_buf, oid_size, obj, 1) < 0)
  329. break; /* Error */
  330. if (ln == NULL || strcmp(sn, ln) == 0)
  331. BIO_printf(bio_out, "%s = %s\n", sn, oid_buf);
  332. else
  333. BIO_printf(bio_out, "%s = %s, %s\n", sn, ln, oid_buf);
  334. }
  335. OPENSSL_free(oid_buf);
  336. }
  337. static void list_options_for_command(const char *command)
  338. {
  339. const FUNCTION *fp;
  340. const OPTIONS *o;
  341. for (fp = functions; fp->name != NULL; fp++)
  342. if (strcmp(fp->name, command) == 0)
  343. break;
  344. if (fp->name == NULL) {
  345. BIO_printf(bio_err, "Invalid command '%s'; type \"help\" for a list.\n",
  346. command);
  347. return;
  348. }
  349. if ((o = fp->help) == NULL)
  350. return;
  351. for ( ; o->name != NULL; o++) {
  352. if (o->name == OPT_HELP_STR
  353. || o->name == OPT_MORE_STR
  354. || o->name[0] == '\0')
  355. continue;
  356. BIO_printf(bio_out, "%s %c\n", o->name, o->valtype);
  357. }
  358. }
  359. /* Unified enum for help and list commands. */
  360. typedef enum HELPLIST_CHOICE {
  361. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, OPT_ONE,
  362. OPT_COMMANDS, OPT_DIGEST_COMMANDS, OPT_MAC_ALGORITHMS, OPT_OPTIONS,
  363. OPT_DIGEST_ALGORITHMS, OPT_CIPHER_COMMANDS, OPT_CIPHER_ALGORITHMS,
  364. OPT_PK_ALGORITHMS, OPT_PK_METHOD, OPT_DISABLED, OPT_MISSING_HELP,
  365. OPT_OBJECTS
  366. } HELPLIST_CHOICE;
  367. const OPTIONS list_options[] = {
  368. {"help", OPT_HELP, '-', "Display this summary"},
  369. {"1", OPT_ONE, '-', "List in one column"},
  370. {"commands", OPT_COMMANDS, '-', "List of standard commands"},
  371. {"digest-commands", OPT_DIGEST_COMMANDS, '-',
  372. "List of message digest commands"},
  373. {"digest-algorithms", OPT_DIGEST_ALGORITHMS, '-',
  374. "List of message digest algorithms"},
  375. {"mac-algorithms", OPT_MAC_ALGORITHMS, '-',
  376. "List of message authentication code algorithms"},
  377. {"cipher-commands", OPT_CIPHER_COMMANDS, '-', "List of cipher commands"},
  378. {"cipher-algorithms", OPT_CIPHER_ALGORITHMS, '-',
  379. "List of cipher algorithms"},
  380. {"public-key-algorithms", OPT_PK_ALGORITHMS, '-',
  381. "List of public key algorithms"},
  382. {"public-key-methods", OPT_PK_METHOD, '-',
  383. "List of public key methods"},
  384. {"disabled", OPT_DISABLED, '-',
  385. "List of disabled features"},
  386. {"missing-help", OPT_MISSING_HELP, '-',
  387. "List missing detailed help strings"},
  388. {"options", OPT_OPTIONS, 's',
  389. "List options for specified command"},
  390. {"objects", OPT_OBJECTS, '-',
  391. "List built in objects (OID<->name mappings)"},
  392. {NULL}
  393. };
  394. int list_main(int argc, char **argv)
  395. {
  396. char *prog;
  397. HELPLIST_CHOICE o;
  398. int one = 0, done = 0;
  399. prog = opt_init(argc, argv, list_options);
  400. while ((o = opt_next()) != OPT_EOF) {
  401. switch (o) {
  402. case OPT_EOF: /* Never hit, but suppresses warning */
  403. case OPT_ERR:
  404. opthelp:
  405. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  406. return 1;
  407. case OPT_HELP:
  408. opt_help(list_options);
  409. break;
  410. case OPT_ONE:
  411. one = 1;
  412. break;
  413. case OPT_COMMANDS:
  414. list_type(FT_general, one);
  415. break;
  416. case OPT_DIGEST_COMMANDS:
  417. list_type(FT_md, one);
  418. break;
  419. case OPT_DIGEST_ALGORITHMS:
  420. EVP_MD_do_all_sorted(list_md_fn, bio_out);
  421. break;
  422. case OPT_MAC_ALGORITHMS:
  423. EVP_MAC_do_all_sorted(list_mac_fn, bio_out);
  424. break;
  425. case OPT_CIPHER_COMMANDS:
  426. list_type(FT_cipher, one);
  427. break;
  428. case OPT_CIPHER_ALGORITHMS:
  429. EVP_CIPHER_do_all_sorted(list_cipher_fn, bio_out);
  430. break;
  431. case OPT_PK_ALGORITHMS:
  432. list_pkey();
  433. break;
  434. case OPT_PK_METHOD:
  435. list_pkey_meth();
  436. break;
  437. case OPT_DISABLED:
  438. list_disabled();
  439. break;
  440. case OPT_MISSING_HELP:
  441. list_missing_help();
  442. break;
  443. case OPT_OBJECTS:
  444. list_objects();
  445. break;
  446. case OPT_OPTIONS:
  447. list_options_for_command(opt_arg());
  448. break;
  449. }
  450. done = 1;
  451. }
  452. if (opt_num_rest() != 0) {
  453. BIO_printf(bio_err, "Extra arguments given.\n");
  454. goto opthelp;
  455. }
  456. if (!done)
  457. goto opthelp;
  458. return 0;
  459. }
  460. typedef enum HELP_CHOICE {
  461. OPT_hERR = -1, OPT_hEOF = 0, OPT_hHELP
  462. } HELP_CHOICE;
  463. const OPTIONS help_options[] = {
  464. {OPT_HELP_STR, 1, '-', "Usage: help [options]\n"},
  465. {OPT_HELP_STR, 1, '-', " help [command]\n"},
  466. {"help", OPT_hHELP, '-', "Display this summary"},
  467. {NULL}
  468. };
  469. int help_main(int argc, char **argv)
  470. {
  471. FUNCTION *fp;
  472. int i, nl;
  473. FUNC_TYPE tp;
  474. char *prog;
  475. HELP_CHOICE o;
  476. DISPLAY_COLUMNS dc;
  477. prog = opt_init(argc, argv, help_options);
  478. while ((o = opt_next()) != OPT_hEOF) {
  479. switch (o) {
  480. case OPT_hERR:
  481. case OPT_hEOF:
  482. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  483. return 1;
  484. case OPT_hHELP:
  485. opt_help(help_options);
  486. return 0;
  487. }
  488. }
  489. if (opt_num_rest() == 1) {
  490. char *new_argv[3];
  491. new_argv[0] = opt_rest()[0];
  492. new_argv[1] = "--help";
  493. new_argv[2] = NULL;
  494. return do_cmd(prog_init(), 2, new_argv);
  495. }
  496. if (opt_num_rest() != 0) {
  497. BIO_printf(bio_err, "Usage: %s\n", prog);
  498. return 1;
  499. }
  500. calculate_columns(&dc);
  501. BIO_printf(bio_err, "Standard commands");
  502. i = 0;
  503. tp = FT_none;
  504. for (fp = functions; fp->name != NULL; fp++) {
  505. nl = 0;
  506. if (i++ % dc.columns == 0) {
  507. BIO_printf(bio_err, "\n");
  508. nl = 1;
  509. }
  510. if (fp->type != tp) {
  511. tp = fp->type;
  512. if (!nl)
  513. BIO_printf(bio_err, "\n");
  514. if (tp == FT_md) {
  515. i = 1;
  516. BIO_printf(bio_err,
  517. "\nMessage Digest commands (see the `dgst' command for more details)\n");
  518. } else if (tp == FT_cipher) {
  519. i = 1;
  520. BIO_printf(bio_err,
  521. "\nCipher commands (see the `enc' command for more details)\n");
  522. }
  523. }
  524. BIO_printf(bio_err, "%-*s", dc.width, fp->name);
  525. }
  526. BIO_printf(bio_err, "\n\n");
  527. return 0;
  528. }
  529. static void list_type(FUNC_TYPE ft, int one)
  530. {
  531. FUNCTION *fp;
  532. int i = 0;
  533. DISPLAY_COLUMNS dc = {0};
  534. if (!one)
  535. calculate_columns(&dc);
  536. for (fp = functions; fp->name != NULL; fp++) {
  537. if (fp->type != ft)
  538. continue;
  539. if (one) {
  540. BIO_printf(bio_out, "%s\n", fp->name);
  541. } else {
  542. if (i % dc.columns == 0 && i > 0)
  543. BIO_printf(bio_out, "\n");
  544. BIO_printf(bio_out, "%-*s", dc.width, fp->name);
  545. i++;
  546. }
  547. }
  548. if (!one)
  549. BIO_printf(bio_out, "\n\n");
  550. }
  551. static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[])
  552. {
  553. FUNCTION f, *fp;
  554. if (argc <= 0 || argv[0] == NULL)
  555. return 0;
  556. f.name = argv[0];
  557. fp = lh_FUNCTION_retrieve(prog, &f);
  558. if (fp == NULL) {
  559. if (EVP_get_digestbyname(argv[0])) {
  560. f.type = FT_md;
  561. f.func = dgst_main;
  562. fp = &f;
  563. } else if (EVP_get_cipherbyname(argv[0])) {
  564. f.type = FT_cipher;
  565. f.func = enc_main;
  566. fp = &f;
  567. }
  568. }
  569. if (fp != NULL) {
  570. return fp->func(argc, argv);
  571. }
  572. if ((strncmp(argv[0], "no-", 3)) == 0) {
  573. /*
  574. * User is asking if foo is unsupported, by trying to "run" the
  575. * no-foo command. Strange.
  576. */
  577. f.name = argv[0] + 3;
  578. if (lh_FUNCTION_retrieve(prog, &f) == NULL) {
  579. BIO_printf(bio_out, "%s\n", argv[0]);
  580. return 0;
  581. }
  582. BIO_printf(bio_out, "%s\n", argv[0] + 3);
  583. return 1;
  584. }
  585. if (strcmp(argv[0], "quit") == 0 || strcmp(argv[0], "q") == 0 ||
  586. strcmp(argv[0], "exit") == 0 || strcmp(argv[0], "bye") == 0)
  587. /* Special value to mean "exit the program. */
  588. return EXIT_THE_PROGRAM;
  589. BIO_printf(bio_err, "Invalid command '%s'; type \"help\" for a list.\n",
  590. argv[0]);
  591. return 1;
  592. }
  593. static void list_pkey(void)
  594. {
  595. int i;
  596. for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
  597. const EVP_PKEY_ASN1_METHOD *ameth;
  598. int pkey_id, pkey_base_id, pkey_flags;
  599. const char *pinfo, *pem_str;
  600. ameth = EVP_PKEY_asn1_get0(i);
  601. EVP_PKEY_asn1_get0_info(&pkey_id, &pkey_base_id, &pkey_flags,
  602. &pinfo, &pem_str, ameth);
  603. if (pkey_flags & ASN1_PKEY_ALIAS) {
  604. BIO_printf(bio_out, "Name: %s\n", OBJ_nid2ln(pkey_id));
  605. BIO_printf(bio_out, "\tAlias for: %s\n",
  606. OBJ_nid2ln(pkey_base_id));
  607. } else {
  608. BIO_printf(bio_out, "Name: %s\n", pinfo);
  609. BIO_printf(bio_out, "\tType: %s Algorithm\n",
  610. pkey_flags & ASN1_PKEY_DYNAMIC ?
  611. "External" : "Builtin");
  612. BIO_printf(bio_out, "\tOID: %s\n", OBJ_nid2ln(pkey_id));
  613. if (pem_str == NULL)
  614. pem_str = "(none)";
  615. BIO_printf(bio_out, "\tPEM string: %s\n", pem_str);
  616. }
  617. }
  618. }
  619. static void list_pkey_meth(void)
  620. {
  621. size_t i;
  622. size_t meth_count = EVP_PKEY_meth_get_count();
  623. for (i = 0; i < meth_count; i++) {
  624. const EVP_PKEY_METHOD *pmeth = EVP_PKEY_meth_get0(i);
  625. int pkey_id, pkey_flags;
  626. EVP_PKEY_meth_get0_info(&pkey_id, &pkey_flags, pmeth);
  627. BIO_printf(bio_out, "%s\n", OBJ_nid2ln(pkey_id));
  628. BIO_printf(bio_out, "\tType: %s Algorithm\n",
  629. pkey_flags & ASN1_PKEY_DYNAMIC ? "External" : "Builtin");
  630. }
  631. }
  632. static int function_cmp(const FUNCTION * a, const FUNCTION * b)
  633. {
  634. return strncmp(a->name, b->name, 8);
  635. }
  636. static unsigned long function_hash(const FUNCTION * a)
  637. {
  638. return OPENSSL_LH_strhash(a->name);
  639. }
  640. static int SortFnByName(const void *_f1, const void *_f2)
  641. {
  642. const FUNCTION *f1 = _f1;
  643. const FUNCTION *f2 = _f2;
  644. if (f1->type != f2->type)
  645. return f1->type - f2->type;
  646. return strcmp(f1->name, f2->name);
  647. }
  648. static void list_disabled(void)
  649. {
  650. BIO_puts(bio_out, "Disabled algorithms:\n");
  651. #ifdef OPENSSL_NO_ARIA
  652. BIO_puts(bio_out, "ARIA\n");
  653. #endif
  654. #ifdef OPENSSL_NO_BF
  655. BIO_puts(bio_out, "BF\n");
  656. #endif
  657. #ifdef OPENSSL_NO_BLAKE2
  658. BIO_puts(bio_out, "BLAKE2\n");
  659. #endif
  660. #ifdef OPENSSL_NO_CAMELLIA
  661. BIO_puts(bio_out, "CAMELLIA\n");
  662. #endif
  663. #ifdef OPENSSL_NO_CAST
  664. BIO_puts(bio_out, "CAST\n");
  665. #endif
  666. #ifdef OPENSSL_NO_CMAC
  667. BIO_puts(bio_out, "CMAC\n");
  668. #endif
  669. #ifdef OPENSSL_NO_CMS
  670. BIO_puts(bio_out, "CMS\n");
  671. #endif
  672. #ifdef OPENSSL_NO_COMP
  673. BIO_puts(bio_out, "COMP\n");
  674. #endif
  675. #ifdef OPENSSL_NO_DES
  676. BIO_puts(bio_out, "DES\n");
  677. #endif
  678. #ifdef OPENSSL_NO_DGRAM
  679. BIO_puts(bio_out, "DGRAM\n");
  680. #endif
  681. #ifdef OPENSSL_NO_DH
  682. BIO_puts(bio_out, "DH\n");
  683. #endif
  684. #ifdef OPENSSL_NO_DSA
  685. BIO_puts(bio_out, "DSA\n");
  686. #endif
  687. #if defined(OPENSSL_NO_DTLS)
  688. BIO_puts(bio_out, "DTLS\n");
  689. #endif
  690. #if defined(OPENSSL_NO_DTLS1)
  691. BIO_puts(bio_out, "DTLS1\n");
  692. #endif
  693. #if defined(OPENSSL_NO_DTLS1_2)
  694. BIO_puts(bio_out, "DTLS1_2\n");
  695. #endif
  696. #ifdef OPENSSL_NO_EC
  697. BIO_puts(bio_out, "EC\n");
  698. #endif
  699. #ifdef OPENSSL_NO_EC2M
  700. BIO_puts(bio_out, "EC2M\n");
  701. #endif
  702. #ifdef OPENSSL_NO_ENGINE
  703. BIO_puts(bio_out, "ENGINE\n");
  704. #endif
  705. #ifdef OPENSSL_NO_GOST
  706. BIO_puts(bio_out, "GOST\n");
  707. #endif
  708. #ifdef OPENSSL_NO_HEARTBEATS
  709. BIO_puts(bio_out, "HEARTBEATS\n");
  710. #endif
  711. #ifdef OPENSSL_NO_IDEA
  712. BIO_puts(bio_out, "IDEA\n");
  713. #endif
  714. #ifdef OPENSSL_NO_MD2
  715. BIO_puts(bio_out, "MD2\n");
  716. #endif
  717. #ifdef OPENSSL_NO_MD4
  718. BIO_puts(bio_out, "MD4\n");
  719. #endif
  720. #ifdef OPENSSL_NO_MD5
  721. BIO_puts(bio_out, "MD5\n");
  722. #endif
  723. #ifdef OPENSSL_NO_MDC2
  724. BIO_puts(bio_out, "MDC2\n");
  725. #endif
  726. #ifdef OPENSSL_NO_OCB
  727. BIO_puts(bio_out, "OCB\n");
  728. #endif
  729. #ifdef OPENSSL_NO_OCSP
  730. BIO_puts(bio_out, "OCSP\n");
  731. #endif
  732. #ifdef OPENSSL_NO_PSK
  733. BIO_puts(bio_out, "PSK\n");
  734. #endif
  735. #ifdef OPENSSL_NO_RC2
  736. BIO_puts(bio_out, "RC2\n");
  737. #endif
  738. #ifdef OPENSSL_NO_RC4
  739. BIO_puts(bio_out, "RC4\n");
  740. #endif
  741. #ifdef OPENSSL_NO_RC5
  742. BIO_puts(bio_out, "RC5\n");
  743. #endif
  744. #ifdef OPENSSL_NO_RMD160
  745. BIO_puts(bio_out, "RMD160\n");
  746. #endif
  747. #ifdef OPENSSL_NO_RSA
  748. BIO_puts(bio_out, "RSA\n");
  749. #endif
  750. #ifdef OPENSSL_NO_SCRYPT
  751. BIO_puts(bio_out, "SCRYPT\n");
  752. #endif
  753. #ifdef OPENSSL_NO_SCTP
  754. BIO_puts(bio_out, "SCTP\n");
  755. #endif
  756. #ifdef OPENSSL_NO_SEED
  757. BIO_puts(bio_out, "SEED\n");
  758. #endif
  759. #ifdef OPENSSL_NO_SM2
  760. BIO_puts(bio_out, "SM2\n");
  761. #endif
  762. #ifdef OPENSSL_NO_SM3
  763. BIO_puts(bio_out, "SM3\n");
  764. #endif
  765. #ifdef OPENSSL_NO_SM4
  766. BIO_puts(bio_out, "SM4\n");
  767. #endif
  768. #ifdef OPENSSL_NO_SOCK
  769. BIO_puts(bio_out, "SOCK\n");
  770. #endif
  771. #ifdef OPENSSL_NO_SRP
  772. BIO_puts(bio_out, "SRP\n");
  773. #endif
  774. #ifdef OPENSSL_NO_SRTP
  775. BIO_puts(bio_out, "SRTP\n");
  776. #endif
  777. #ifdef OPENSSL_NO_SSL3
  778. BIO_puts(bio_out, "SSL3\n");
  779. #endif
  780. #ifdef OPENSSL_NO_TLS1
  781. BIO_puts(bio_out, "TLS1\n");
  782. #endif
  783. #ifdef OPENSSL_NO_TLS1_1
  784. BIO_puts(bio_out, "TLS1_1\n");
  785. #endif
  786. #ifdef OPENSSL_NO_TLS1_2
  787. BIO_puts(bio_out, "TLS1_2\n");
  788. #endif
  789. #ifdef OPENSSL_NO_WHIRLPOOL
  790. BIO_puts(bio_out, "WHIRLPOOL\n");
  791. #endif
  792. #ifndef ZLIB
  793. BIO_puts(bio_out, "ZLIB\n");
  794. #endif
  795. }
  796. static LHASH_OF(FUNCTION) *prog_init(void)
  797. {
  798. static LHASH_OF(FUNCTION) *ret = NULL;
  799. static int prog_inited = 0;
  800. FUNCTION *f;
  801. size_t i;
  802. if (prog_inited)
  803. return ret;
  804. prog_inited = 1;
  805. /* Sort alphabetically within category. For nicer help displays. */
  806. for (i = 0, f = functions; f->name != NULL; ++f, ++i)
  807. ;
  808. qsort(functions, i, sizeof(*functions), SortFnByName);
  809. if ((ret = lh_FUNCTION_new(function_hash, function_cmp)) == NULL)
  810. return NULL;
  811. for (f = functions; f->name != NULL; f++)
  812. (void)lh_FUNCTION_insert(ret, f);
  813. return ret;
  814. }