2
0

openssl.c 27 KB

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