2 * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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
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>
23 #include <openssl/err.h>
25 /* Needed to get the other O_xxx flags. */
26 #ifdef OPENSSL_SYS_VMS
29 #define INCLUDE_FUNCTION_TABLE
32 /* Structure to hold the number of columns to be displayed and the
33 * field width used to display them.
40 /* Special sentinel to exit the program. */
41 #define EXIT_THE_PROGRAM (-1)
44 * The LHASH callbacks ("hash" & "cmp") have been replaced by functions with
45 * the base prototypes (we cast each variable inside the function to the
46 * required type of "FUNCTION*"). This removes the necessity for
47 * macro-generated wrapper functions.
49 static LHASH_OF(FUNCTION) *prog_init(void);
50 static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[]);
51 static void list_pkey(void);
52 static void list_pkey_meth(void);
53 static void list_type(FUNC_TYPE ft, int one);
54 static void list_disabled(void);
55 char *default_config_file = NULL;
61 static void calculate_columns(DISPLAY_COLUMNS *dc)
66 for (f = functions; f->name != NULL; ++f)
67 if (f->type == FT_general || f->type == FT_md || f->type == FT_cipher)
68 if ((len = strlen(f->name)) > maxlen)
71 dc->width = maxlen + 2;
72 dc->columns = (80 - 1) / dc->width;
75 static int apps_startup()
78 signal(SIGPIPE, SIG_IGN);
81 /* Set non-default library initialisation settings */
82 if (!OPENSSL_init_crypto(OPENSSL_INIT_ENGINE_ALL_BUILTIN
83 | OPENSSL_INIT_LOAD_CONFIG, NULL))
91 static void apps_shutdown()
96 static char *make_config_name()
102 if ((t = getenv("OPENSSL_CONF")) != NULL)
103 return OPENSSL_strdup(t);
105 t = X509_get_default_cert_area();
106 len = strlen(t) + 1 + strlen(OPENSSL_CONF) + 1;
107 p = app_malloc(len, "config filename buffer");
109 #ifndef OPENSSL_SYS_VMS
112 strcat(p, OPENSSL_CONF);
117 int main(int argc, char *argv[])
120 LHASH_OF(FUNCTION) *prog = NULL;
121 char **copied_argv = NULL;
126 int first, n, i, ret = 0;
131 /* Set up some of the environment. */
132 default_config_file = make_config_name();
133 bio_in = dup_bio_in(FORMAT_TEXT);
134 bio_out = dup_bio_out(FORMAT_TEXT);
135 bio_err = dup_bio_err(FORMAT_TEXT);
137 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
138 copied_argv = argv = copy_argv(&argc, argv);
139 #elif defined(_WIN32)
141 * Replace argv[] with UTF-8 encoded strings.
143 win32_utf8argv(&argc, &argv);
146 p = getenv("OPENSSL_DEBUG_MEMORY");
147 if (p != NULL && strcmp(p, "on") == 0)
148 CRYPTO_set_mem_debug(1);
149 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
151 if (getenv("OPENSSL_FIPS")) {
152 BIO_printf(bio_err, "FIPS mode not supported.\n");
156 if (!apps_startup()) {
158 "FATAL: Startup failure (dev note: apps_startup() failed)\n");
159 ERR_print_errors(bio_err);
165 pname = opt_progname(argv[0]);
167 /* first check the program name */
169 fp = lh_FUNCTION_retrieve(prog, &f);
172 ret = fp->func(argc, argv);
176 /* If there is stuff on the command line, run with that. */
180 ret = do_cmd(prog, argc, argv);
186 /* ok, lets enter interactive mode */
189 /* Read a line, continue reading if line ends with \ */
190 for (p = buf, n = sizeof buf, i = 0, first = 1; n > 0; first = 0) {
191 prompt = first ? "OpenSSL> " : "> ";
194 fputs(prompt, stdout);
196 if (!fgets(p, n, stdin))
203 if (p[i - 2] != '\\')
210 extern char *readline(const char *);
211 extern void add_history(const char *cp);
214 text = readline(prompt);
220 if (text[i - 1] != '\\') {
221 p += strlen(strcpy(p, text));
228 p += strlen(strcpy(p, text));
235 if (!chopup_args(&arg, buf)) {
236 BIO_printf(bio_err, "Can't parse (no memory?)\n");
240 ret = do_cmd(prog, arg.argc, arg.argv);
241 if (ret == EXIT_THE_PROGRAM) {
246 BIO_printf(bio_err, "error in %s\n", arg.argv[0]);
247 (void)BIO_flush(bio_out);
248 (void)BIO_flush(bio_err);
252 OPENSSL_free(copied_argv);
253 OPENSSL_free(default_config_file);
254 lh_FUNCTION_free(prog);
255 OPENSSL_free(arg.argv);
259 BIO_free_all(bio_out);
261 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
262 if (CRYPTO_mem_leaks(bio_err) <= 0)
269 static void list_cipher_fn(const EVP_CIPHER *c,
270 const char *from, const char *to, void *arg)
273 BIO_printf(arg, "%s\n", EVP_CIPHER_name(c));
276 from = "<undefined>";
279 BIO_printf(arg, "%s => %s\n", from, to);
283 static void list_md_fn(const EVP_MD *m,
284 const char *from, const char *to, void *arg)
287 BIO_printf(arg, "%s\n", EVP_MD_name(m));
290 from = "<undefined>";
293 BIO_printf((BIO *)arg, "%s => %s\n", from, to);
297 static void list_missing_help(void)
302 for (fp = functions; fp->name != NULL; fp++) {
303 if ((o = fp->help) != NULL) {
304 /* If there is help, list what flags are not documented. */
305 for ( ; o->name != NULL; o++) {
306 if (o->helpstr == NULL)
307 BIO_printf(bio_out, "%s %s\n", fp->name, o->name);
309 } else if (fp->func != dgst_main) {
310 /* If not aliased to the dgst command, */
311 BIO_printf(bio_out, "%s *\n", fp->name);
316 static void list_options_for_command(const char *command)
321 for (fp = functions; fp->name != NULL; fp++)
322 if (strcmp(fp->name, command) == 0)
324 if (fp->name == NULL) {
325 BIO_printf(bio_err, "Invalid command '%s'; type \"help\" for a list.\n",
330 if ((o = fp->help) == NULL)
333 for ( ; o->name != NULL; o++) {
334 if (o->name == OPT_HELP_STR
335 || o->name == OPT_MORE_STR
336 || o->name[0] == '\0')
338 BIO_printf(bio_out, "%s %c\n", o->name, o->valtype);
343 /* Unified enum for help and list commands. */
344 typedef enum HELPLIST_CHOICE {
345 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, OPT_ONE,
346 OPT_COMMANDS, OPT_DIGEST_COMMANDS, OPT_OPTIONS,
347 OPT_DIGEST_ALGORITHMS, OPT_CIPHER_COMMANDS, OPT_CIPHER_ALGORITHMS,
348 OPT_PK_ALGORITHMS, OPT_PK_METHOD, OPT_DISABLED, OPT_MISSING_HELP
351 const OPTIONS list_options[] = {
352 {"help", OPT_HELP, '-', "Display this summary"},
353 {"1", OPT_ONE, '-', "List in one column"},
354 {"commands", OPT_COMMANDS, '-', "List of standard commands"},
355 {"digest-commands", OPT_DIGEST_COMMANDS, '-',
356 "List of message digest commands"},
357 {"digest-algorithms", OPT_DIGEST_ALGORITHMS, '-',
358 "List of message digest algorithms"},
359 {"cipher-commands", OPT_CIPHER_COMMANDS, '-', "List of cipher commands"},
360 {"cipher-algorithms", OPT_CIPHER_ALGORITHMS, '-',
361 "List of cipher algorithms"},
362 {"public-key-algorithms", OPT_PK_ALGORITHMS, '-',
363 "List of public key algorithms"},
364 {"public-key-methods", OPT_PK_METHOD, '-',
365 "List of public key methods"},
366 {"disabled", OPT_DISABLED, '-',
367 "List of disabled features"},
368 {"missing-help", OPT_MISSING_HELP, '-',
369 "List missing detailed help strings"},
370 {"options", OPT_OPTIONS, 's',
371 "List options for specified command"},
375 int list_main(int argc, char **argv)
379 int one = 0, done = 0;
381 prog = opt_init(argc, argv, list_options);
382 while ((o = opt_next()) != OPT_EOF) {
384 case OPT_EOF: /* Never hit, but suppresses warning */
387 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
390 opt_help(list_options);
396 list_type(FT_general, one);
398 case OPT_DIGEST_COMMANDS:
399 list_type(FT_md, one);
401 case OPT_DIGEST_ALGORITHMS:
402 EVP_MD_do_all_sorted(list_md_fn, bio_out);
404 case OPT_CIPHER_COMMANDS:
405 list_type(FT_cipher, one);
407 case OPT_CIPHER_ALGORITHMS:
408 EVP_CIPHER_do_all_sorted(list_cipher_fn, bio_out);
410 case OPT_PK_ALGORITHMS:
419 case OPT_MISSING_HELP:
423 list_options_for_command(opt_arg());
428 if (opt_num_rest() != 0) {
429 BIO_printf(bio_err, "Extra arguments given.\n");
439 typedef enum HELP_CHOICE {
440 OPT_hERR = -1, OPT_hEOF = 0, OPT_hHELP
443 const OPTIONS help_options[] = {
444 {"help", OPT_hHELP, '-', "Display this summary"},
449 int help_main(int argc, char **argv)
458 prog = opt_init(argc, argv, help_options);
459 while ((o = opt_next()) != OPT_hEOF) {
463 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
466 opt_help(help_options);
471 if (opt_num_rest() != 0) {
472 BIO_printf(bio_err, "Usage: %s\n", prog);
476 calculate_columns(&dc);
477 BIO_printf(bio_err, "Standard commands");
480 for (fp = functions; fp->name != NULL; fp++) {
482 if (i++ % dc.columns == 0) {
483 BIO_printf(bio_err, "\n");
486 if (fp->type != tp) {
489 BIO_printf(bio_err, "\n");
493 "\nMessage Digest commands (see the `dgst' command for more details)\n");
494 } else if (tp == FT_cipher) {
497 "\nCipher commands (see the `enc' command for more details)\n");
500 BIO_printf(bio_err, "%-*s", dc.width, fp->name);
502 BIO_printf(bio_err, "\n\n");
506 static void list_type(FUNC_TYPE ft, int one)
513 calculate_columns(&dc);
515 for (fp = functions; fp->name != NULL; fp++) {
519 BIO_printf(bio_out, "%s\n", fp->name);
521 if (i % dc.columns == 0 && i > 0)
522 BIO_printf(bio_out, "\n");
523 BIO_printf(bio_out, "%-*s", dc.width, fp->name);
528 BIO_printf(bio_out, "\n\n");
531 static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[])
535 if (argc <= 0 || argv[0] == NULL)
538 fp = lh_FUNCTION_retrieve(prog, &f);
540 if (EVP_get_digestbyname(argv[0])) {
544 } else if (EVP_get_cipherbyname(argv[0])) {
551 return (fp->func(argc, argv));
553 if ((strncmp(argv[0], "no-", 3)) == 0) {
555 * User is asking if foo is unsupported, by trying to "run" the
556 * no-foo command. Strange.
558 f.name = argv[0] + 3;
559 if (lh_FUNCTION_retrieve(prog, &f) == NULL) {
560 BIO_printf(bio_out, "%s\n", argv[0]);
563 BIO_printf(bio_out, "%s\n", argv[0] + 3);
566 if (strcmp(argv[0], "quit") == 0 || strcmp(argv[0], "q") == 0 ||
567 strcmp(argv[0], "exit") == 0 || strcmp(argv[0], "bye") == 0)
568 /* Special value to mean "exit the program. */
569 return EXIT_THE_PROGRAM;
571 BIO_printf(bio_err, "Invalid command '%s'; type \"help\" for a list.\n",
576 static void list_pkey(void)
580 for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
581 const EVP_PKEY_ASN1_METHOD *ameth;
582 int pkey_id, pkey_base_id, pkey_flags;
583 const char *pinfo, *pem_str;
584 ameth = EVP_PKEY_asn1_get0(i);
585 EVP_PKEY_asn1_get0_info(&pkey_id, &pkey_base_id, &pkey_flags,
586 &pinfo, &pem_str, ameth);
587 if (pkey_flags & ASN1_PKEY_ALIAS) {
588 BIO_printf(bio_out, "Name: %s\n", OBJ_nid2ln(pkey_id));
589 BIO_printf(bio_out, "\tAlias for: %s\n",
590 OBJ_nid2ln(pkey_base_id));
592 BIO_printf(bio_out, "Name: %s\n", pinfo);
593 BIO_printf(bio_out, "\tType: %s Algorithm\n",
594 pkey_flags & ASN1_PKEY_DYNAMIC ?
595 "External" : "Builtin");
596 BIO_printf(bio_out, "\tOID: %s\n", OBJ_nid2ln(pkey_id));
599 BIO_printf(bio_out, "\tPEM string: %s\n", pem_str);
605 static void list_pkey_meth(void)
608 size_t meth_count = EVP_PKEY_meth_get_count();
610 for (i = 0; i < meth_count; i++) {
611 const EVP_PKEY_METHOD *pmeth = EVP_PKEY_meth_get0(i);
612 int pkey_id, pkey_flags;
614 EVP_PKEY_meth_get0_info(&pkey_id, &pkey_flags, pmeth);
615 BIO_printf(bio_out, "%s\n", OBJ_nid2ln(pkey_id));
616 BIO_printf(bio_out, "\tType: %s Algorithm\n",
617 pkey_flags & ASN1_PKEY_DYNAMIC ? "External" : "Builtin");
621 static int function_cmp(const FUNCTION * a, const FUNCTION * b)
623 return strncmp(a->name, b->name, 8);
626 static unsigned long function_hash(const FUNCTION * a)
628 return OPENSSL_LH_strhash(a->name);
631 static int SortFnByName(const void *_f1, const void *_f2)
633 const FUNCTION *f1 = _f1;
634 const FUNCTION *f2 = _f2;
636 if (f1->type != f2->type)
637 return f1->type - f2->type;
638 return strcmp(f1->name, f2->name);
641 static void list_disabled(void)
643 BIO_puts(bio_out, "Disabled algorithms:\n");
644 #ifdef OPENSSL_NO_ARIA
645 BIO_puts(bio_out, "ARIA\n");
648 BIO_puts(bio_out, "BF\n");
650 #ifdef OPENSSL_NO_BLAKE2
651 BIO_puts(bio_out, "BLAKE2\n");
653 #ifdef OPENSSL_NO_CAMELLIA
654 BIO_puts(bio_out, "CAMELLIA\n");
656 #ifdef OPENSSL_NO_CAST
657 BIO_puts(bio_out, "CAST\n");
659 #ifdef OPENSSL_NO_CMAC
660 BIO_puts(bio_out, "CMAC\n");
662 #ifdef OPENSSL_NO_CMS
663 BIO_puts(bio_out, "CMS\n");
665 #ifdef OPENSSL_NO_COMP
666 BIO_puts(bio_out, "COMP\n");
668 #ifdef OPENSSL_NO_DES
669 BIO_puts(bio_out, "DES\n");
671 #ifdef OPENSSL_NO_DGRAM
672 BIO_puts(bio_out, "DGRAM\n");
675 BIO_puts(bio_out, "DH\n");
677 #ifdef OPENSSL_NO_DSA
678 BIO_puts(bio_out, "DSA\n");
680 #if defined(OPENSSL_NO_DTLS)
681 BIO_puts(bio_out, "DTLS\n");
683 #if defined(OPENSSL_NO_DTLS1)
684 BIO_puts(bio_out, "DTLS1\n");
686 #if defined(OPENSSL_NO_DTLS1_2)
687 BIO_puts(bio_out, "DTLS1_2\n");
690 BIO_puts(bio_out, "EC\n");
692 #ifdef OPENSSL_NO_EC2M
693 BIO_puts(bio_out, "EC2M\n");
695 #ifdef OPENSSL_NO_ENGINE
696 BIO_puts(bio_out, "ENGINE\n");
698 #ifdef OPENSSL_NO_GOST
699 BIO_puts(bio_out, "GOST\n");
701 #ifdef OPENSSL_NO_HEARTBEATS
702 BIO_puts(bio_out, "HEARTBEATS\n");
704 #ifdef OPENSSL_NO_IDEA
705 BIO_puts(bio_out, "IDEA\n");
707 #ifdef OPENSSL_NO_MD2
708 BIO_puts(bio_out, "MD2\n");
710 #ifdef OPENSSL_NO_MD4
711 BIO_puts(bio_out, "MD4\n");
713 #ifdef OPENSSL_NO_MD5
714 BIO_puts(bio_out, "MD5\n");
716 #ifdef OPENSSL_NO_MDC2
717 BIO_puts(bio_out, "MDC2\n");
719 #ifdef OPENSSL_NO_OCB
720 BIO_puts(bio_out, "OCB\n");
722 #ifdef OPENSSL_NO_OCSP
723 BIO_puts(bio_out, "OCSP\n");
725 #ifdef OPENSSL_NO_PSK
726 BIO_puts(bio_out, "PSK\n");
728 #ifdef OPENSSL_NO_RC2
729 BIO_puts(bio_out, "RC2\n");
731 #ifdef OPENSSL_NO_RC4
732 BIO_puts(bio_out, "RC4\n");
734 #ifdef OPENSSL_NO_RC5
735 BIO_puts(bio_out, "RC5\n");
737 #ifdef OPENSSL_NO_RMD160
738 BIO_puts(bio_out, "RMD160\n");
740 #ifdef OPENSSL_NO_RSA
741 BIO_puts(bio_out, "RSA\n");
743 #ifdef OPENSSL_NO_SCRYPT
744 BIO_puts(bio_out, "SCRYPT\n");
746 #ifdef OPENSSL_NO_SCTP
747 BIO_puts(bio_out, "SCTP\n");
749 #ifdef OPENSSL_NO_SEED
750 BIO_puts(bio_out, "SEED\n");
752 #ifdef OPENSSL_NO_SOCK
753 BIO_puts(bio_out, "SOCK\n");
755 #ifdef OPENSSL_NO_SRP
756 BIO_puts(bio_out, "SRP\n");
758 #ifdef OPENSSL_NO_SRTP
759 BIO_puts(bio_out, "SRTP\n");
761 #ifdef OPENSSL_NO_SSL3
762 BIO_puts(bio_out, "SSL3\n");
764 #ifdef OPENSSL_NO_TLS1
765 BIO_puts(bio_out, "TLS1\n");
767 #ifdef OPENSSL_NO_TLS1_1
768 BIO_puts(bio_out, "TLS1_1\n");
770 #ifdef OPENSSL_NO_TLS1_2
771 BIO_puts(bio_out, "TLS1_2\n");
773 #ifdef OPENSSL_NO_WHIRLPOOL
774 BIO_puts(bio_out, "WHIRLPOOL\n");
777 BIO_puts(bio_out, "ZLIB\n");
781 static LHASH_OF(FUNCTION) *prog_init(void)
783 LHASH_OF(FUNCTION) *ret;
787 /* Sort alphabetically within category. For nicer help displays. */
788 for (i = 0, f = functions; f->name != NULL; ++f, ++i) ;
789 qsort(functions, i, sizeof(*functions), SortFnByName);
791 if ((ret = lh_FUNCTION_new(function_hash, function_cmp)) == NULL)
794 for (f = functions; f->name != NULL; f++)
795 (void)lh_FUNCTION_insert(ret, f);