2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include <internal/cryptlib.h>
14 #include <openssl/bio.h>
15 #include <openssl/crypto.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>
24 #include <openssl/err.h>
26 /* Needed to get the other O_xxx flags. */
27 #ifdef OPENSSL_SYS_VMS
31 #define INCLUDE_FUNCTION_TABLE
34 /* Structure to hold the number of columns to be displayed and the
35 * field width used to display them.
42 /* Special sentinel to exit the program. */
43 #define EXIT_THE_PROGRAM (-1)
46 * The LHASH callbacks ("hash" & "cmp") have been replaced by functions with
47 * the base prototypes (we cast each variable inside the function to the
48 * required type of "FUNCTION*"). This removes the necessity for
49 * macro-generated wrapper functions.
51 static LHASH_OF(FUNCTION) *prog_init(void);
52 static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[]);
53 static void list_pkey(void);
54 static void list_pkey_meth(void);
55 static void list_type(FUNC_TYPE ft, int one);
56 static void list_disabled(void);
57 char *default_config_file = NULL;
63 static void calculate_columns(DISPLAY_COLUMNS *dc)
68 for (f = functions; f->name != NULL; ++f)
69 if (f->type == FT_general || f->type == FT_md || f->type == FT_cipher)
70 if ((len = strlen(f->name)) > maxlen)
73 dc->width = maxlen + 2;
74 dc->columns = (80 - 1) / dc->width;
77 static int apps_startup(void)
80 signal(SIGPIPE, SIG_IGN);
83 /* Set non-default library initialisation settings */
84 if (!OPENSSL_init_ssl(OPENSSL_INIT_ENGINE_ALL_BUILTIN
85 | OPENSSL_INIT_LOAD_CONFIG, NULL))
93 static void apps_shutdown(void)
96 destroy_prefix_method();
99 static char *make_config_name(void)
105 if ((t = getenv("OPENSSL_CONF")) != NULL)
106 return OPENSSL_strdup(t);
108 t = X509_get_default_cert_area();
109 len = strlen(t) + 1 + strlen(OPENSSL_CONF) + 1;
110 p = app_malloc(len, "config filename buffer");
112 #ifndef OPENSSL_SYS_VMS
115 strcat(p, OPENSSL_CONF);
120 int main(int argc, char *argv[])
123 LHASH_OF(FUNCTION) *prog = NULL;
124 char **copied_argv = NULL;
129 int first, n, i, ret = 0;
134 /* Set up some of the environment. */
135 default_config_file = make_config_name();
136 bio_in = dup_bio_in(FORMAT_TEXT);
137 bio_out = dup_bio_out(FORMAT_TEXT);
138 bio_err = dup_bio_err(FORMAT_TEXT);
140 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
141 copied_argv = argv = copy_argv(&argc, argv);
142 #elif defined(_WIN32)
144 * Replace argv[] with UTF-8 encoded strings.
146 win32_utf8argv(&argc, &argv);
149 p = getenv("OPENSSL_DEBUG_MEMORY");
150 if (p != NULL && strcmp(p, "on") == 0)
151 CRYPTO_set_mem_debug(1);
152 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
154 if (getenv("OPENSSL_FIPS")) {
155 BIO_printf(bio_err, "FIPS mode not supported.\n");
159 if (!apps_startup()) {
161 "FATAL: Startup failure (dev note: apps_startup() failed)\n");
162 ERR_print_errors(bio_err);
168 pname = opt_progname(argv[0]);
170 /* first check the program name */
172 fp = lh_FUNCTION_retrieve(prog, &f);
175 ret = fp->func(argc, argv);
179 /* If there is stuff on the command line, run with that. */
183 ret = do_cmd(prog, argc, argv);
189 /* ok, lets enter interactive mode */
192 /* Read a line, continue reading if line ends with \ */
193 for (p = buf, n = sizeof(buf), i = 0, first = 1; n > 0; first = 0) {
194 prompt = first ? "OpenSSL> " : "> ";
197 fputs(prompt, stdout);
199 if (!fgets(p, n, stdin))
206 if (p[i - 2] != '\\')
213 extern char *readline(const char *);
214 extern void add_history(const char *cp);
217 text = readline(prompt);
223 if (text[i - 1] != '\\') {
224 p += strlen(strcpy(p, text));
231 p += strlen(strcpy(p, text));
238 if (!chopup_args(&arg, buf)) {
239 BIO_printf(bio_err, "Can't parse (no memory?)\n");
243 ret = do_cmd(prog, arg.argc, arg.argv);
244 if (ret == EXIT_THE_PROGRAM) {
249 BIO_printf(bio_err, "error in %s\n", arg.argv[0]);
250 (void)BIO_flush(bio_out);
251 (void)BIO_flush(bio_err);
255 OPENSSL_free(copied_argv);
256 OPENSSL_free(default_config_file);
257 lh_FUNCTION_free(prog);
258 OPENSSL_free(arg.argv);
262 BIO_free_all(bio_out);
264 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
265 if (CRYPTO_mem_leaks(bio_err) <= 0)
272 static void list_cipher_fn(const EVP_CIPHER *c,
273 const char *from, const char *to, void *arg)
276 BIO_printf(arg, "%s\n", EVP_CIPHER_name(c));
279 from = "<undefined>";
282 BIO_printf(arg, "%s => %s\n", from, to);
286 static void list_md_fn(const EVP_MD *m,
287 const char *from, const char *to, void *arg)
290 BIO_printf(arg, "%s\n", EVP_MD_name(m));
293 from = "<undefined>";
296 BIO_printf((BIO *)arg, "%s => %s\n", from, to);
300 static void list_mac_fn(const EVP_MAC *m,
301 const char *from, const char *to, void *arg)
304 BIO_printf(arg, "%s\n", EVP_MAC_name(m));
307 from = "<undefined>";
310 BIO_printf(arg, "%s => %s\n", from, to);
314 static void list_missing_help(void)
319 for (fp = functions; fp->name != NULL; fp++) {
320 if ((o = fp->help) != NULL) {
321 /* If there is help, list what flags are not documented. */
322 for ( ; o->name != NULL; o++) {
323 if (o->helpstr == NULL)
324 BIO_printf(bio_out, "%s %s\n", fp->name, o->name);
326 } else if (fp->func != dgst_main) {
327 /* If not aliased to the dgst command, */
328 BIO_printf(bio_out, "%s *\n", fp->name);
333 static void list_objects(void)
335 int max_nid = OBJ_new_nid(0);
337 char *oid_buf = NULL;
340 /* Skip 0, since that's NID_undef */
341 for (i = 1; i < max_nid; i++) {
342 const ASN1_OBJECT *obj = OBJ_nid2obj(i);
343 const char *sn = OBJ_nid2sn(i);
344 const char *ln = OBJ_nid2ln(i);
348 * If one of the retrieved objects somehow generated an error,
349 * we ignore it. The check for NID_undef below will detect the
350 * error and simply skip to the next NID.
354 if (OBJ_obj2nid(obj) == NID_undef)
357 if ((n = OBJ_obj2txt(NULL, 0, obj, 1)) == 0) {
358 BIO_printf(bio_out, "# None-OID object: %s, %s\n", sn, ln);
365 oid_buf = OPENSSL_realloc(oid_buf, n + 1);
366 if (oid_buf == NULL) {
367 BIO_printf(bio_err, "ERROR: Memory allocation\n");
372 if (OBJ_obj2txt(oid_buf, oid_size, obj, 1) < 0)
374 if (ln == NULL || strcmp(sn, ln) == 0)
375 BIO_printf(bio_out, "%s = %s\n", sn, oid_buf);
377 BIO_printf(bio_out, "%s = %s, %s\n", sn, ln, oid_buf);
380 OPENSSL_free(oid_buf);
383 static void list_options_for_command(const char *command)
388 for (fp = functions; fp->name != NULL; fp++)
389 if (strcmp(fp->name, command) == 0)
391 if (fp->name == NULL) {
392 BIO_printf(bio_err, "Invalid command '%s'; type \"help\" for a list.\n",
397 if ((o = fp->help) == NULL)
400 for ( ; o->name != NULL; o++) {
401 if (o->name == OPT_HELP_STR
402 || o->name == OPT_MORE_STR
403 || o->name[0] == '\0')
405 BIO_printf(bio_out, "%s %c\n", o->name, o->valtype);
410 /* Unified enum for help and list commands. */
411 typedef enum HELPLIST_CHOICE {
412 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, OPT_ONE,
413 OPT_COMMANDS, OPT_DIGEST_COMMANDS, OPT_MAC_ALGORITHMS, OPT_OPTIONS,
414 OPT_DIGEST_ALGORITHMS, OPT_CIPHER_COMMANDS, OPT_CIPHER_ALGORITHMS,
415 OPT_PK_ALGORITHMS, OPT_PK_METHOD, OPT_DISABLED, OPT_MISSING_HELP,
419 const OPTIONS list_options[] = {
420 {"help", OPT_HELP, '-', "Display this summary"},
421 {"1", OPT_ONE, '-', "List in one column"},
422 {"commands", OPT_COMMANDS, '-', "List of standard commands"},
423 {"digest-commands", OPT_DIGEST_COMMANDS, '-',
424 "List of message digest commands"},
425 {"digest-algorithms", OPT_DIGEST_ALGORITHMS, '-',
426 "List of message digest algorithms"},
427 {"mac-algorithms", OPT_MAC_ALGORITHMS, '-',
428 "List of message authentication code algorithms"},
429 {"cipher-commands", OPT_CIPHER_COMMANDS, '-', "List of cipher commands"},
430 {"cipher-algorithms", OPT_CIPHER_ALGORITHMS, '-',
431 "List of cipher algorithms"},
432 {"public-key-algorithms", OPT_PK_ALGORITHMS, '-',
433 "List of public key algorithms"},
434 {"public-key-methods", OPT_PK_METHOD, '-',
435 "List of public key methods"},
436 {"disabled", OPT_DISABLED, '-',
437 "List of disabled features"},
438 {"missing-help", OPT_MISSING_HELP, '-',
439 "List missing detailed help strings"},
440 {"options", OPT_OPTIONS, 's',
441 "List options for specified command"},
442 {"objects", OPT_OBJECTS, '-',
443 "List built in objects (OID<->name mappings)"},
447 int list_main(int argc, char **argv)
451 int one = 0, done = 0;
453 prog = opt_init(argc, argv, list_options);
454 while ((o = opt_next()) != OPT_EOF) {
456 case OPT_EOF: /* Never hit, but suppresses warning */
459 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
462 opt_help(list_options);
468 list_type(FT_general, one);
470 case OPT_DIGEST_COMMANDS:
471 list_type(FT_md, one);
473 case OPT_DIGEST_ALGORITHMS:
474 EVP_MD_do_all_sorted(list_md_fn, bio_out);
476 case OPT_MAC_ALGORITHMS:
477 EVP_MAC_do_all_sorted(list_mac_fn, bio_out);
479 case OPT_CIPHER_COMMANDS:
480 list_type(FT_cipher, one);
482 case OPT_CIPHER_ALGORITHMS:
483 EVP_CIPHER_do_all_sorted(list_cipher_fn, bio_out);
485 case OPT_PK_ALGORITHMS:
494 case OPT_MISSING_HELP:
501 list_options_for_command(opt_arg());
506 if (opt_num_rest() != 0) {
507 BIO_printf(bio_err, "Extra arguments given.\n");
517 typedef enum HELP_CHOICE {
518 OPT_hERR = -1, OPT_hEOF = 0, OPT_hHELP
521 const OPTIONS help_options[] = {
522 {OPT_HELP_STR, 1, '-', "Usage: help [options]\n"},
523 {OPT_HELP_STR, 1, '-', " help [command]\n"},
524 {"help", OPT_hHELP, '-', "Display this summary"},
529 int help_main(int argc, char **argv)
538 prog = opt_init(argc, argv, help_options);
539 while ((o = opt_next()) != OPT_hEOF) {
543 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
546 opt_help(help_options);
551 if (opt_num_rest() == 1) {
554 new_argv[0] = opt_rest()[0];
555 new_argv[1] = "--help";
557 return do_cmd(prog_init(), 2, new_argv);
559 if (opt_num_rest() != 0) {
560 BIO_printf(bio_err, "Usage: %s\n", prog);
564 calculate_columns(&dc);
565 BIO_printf(bio_err, "Standard commands");
568 for (fp = functions; fp->name != NULL; fp++) {
570 if (i++ % dc.columns == 0) {
571 BIO_printf(bio_err, "\n");
574 if (fp->type != tp) {
577 BIO_printf(bio_err, "\n");
581 "\nMessage Digest commands (see the `dgst' command for more details)\n");
582 } else if (tp == FT_cipher) {
585 "\nCipher commands (see the `enc' command for more details)\n");
588 BIO_printf(bio_err, "%-*s", dc.width, fp->name);
590 BIO_printf(bio_err, "\n\n");
594 static void list_type(FUNC_TYPE ft, int one)
598 DISPLAY_COLUMNS dc = {0};
601 calculate_columns(&dc);
603 for (fp = functions; fp->name != NULL; fp++) {
607 BIO_printf(bio_out, "%s\n", fp->name);
609 if (i % dc.columns == 0 && i > 0)
610 BIO_printf(bio_out, "\n");
611 BIO_printf(bio_out, "%-*s", dc.width, fp->name);
616 BIO_printf(bio_out, "\n\n");
619 static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[])
623 if (argc <= 0 || argv[0] == NULL)
626 fp = lh_FUNCTION_retrieve(prog, &f);
628 if (EVP_get_digestbyname(argv[0])) {
632 } else if (EVP_get_cipherbyname(argv[0])) {
639 return fp->func(argc, argv);
641 if ((strncmp(argv[0], "no-", 3)) == 0) {
643 * User is asking if foo is unsupported, by trying to "run" the
644 * no-foo command. Strange.
646 f.name = argv[0] + 3;
647 if (lh_FUNCTION_retrieve(prog, &f) == NULL) {
648 BIO_printf(bio_out, "%s\n", argv[0]);
651 BIO_printf(bio_out, "%s\n", argv[0] + 3);
654 if (strcmp(argv[0], "quit") == 0 || strcmp(argv[0], "q") == 0 ||
655 strcmp(argv[0], "exit") == 0 || strcmp(argv[0], "bye") == 0)
656 /* Special value to mean "exit the program. */
657 return EXIT_THE_PROGRAM;
659 BIO_printf(bio_err, "Invalid command '%s'; type \"help\" for a list.\n",
664 static void list_pkey(void)
668 for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
669 const EVP_PKEY_ASN1_METHOD *ameth;
670 int pkey_id, pkey_base_id, pkey_flags;
671 const char *pinfo, *pem_str;
672 ameth = EVP_PKEY_asn1_get0(i);
673 EVP_PKEY_asn1_get0_info(&pkey_id, &pkey_base_id, &pkey_flags,
674 &pinfo, &pem_str, ameth);
675 if (pkey_flags & ASN1_PKEY_ALIAS) {
676 BIO_printf(bio_out, "Name: %s\n", OBJ_nid2ln(pkey_id));
677 BIO_printf(bio_out, "\tAlias for: %s\n",
678 OBJ_nid2ln(pkey_base_id));
680 BIO_printf(bio_out, "Name: %s\n", pinfo);
681 BIO_printf(bio_out, "\tType: %s Algorithm\n",
682 pkey_flags & ASN1_PKEY_DYNAMIC ?
683 "External" : "Builtin");
684 BIO_printf(bio_out, "\tOID: %s\n", OBJ_nid2ln(pkey_id));
687 BIO_printf(bio_out, "\tPEM string: %s\n", pem_str);
693 static void list_pkey_meth(void)
696 size_t meth_count = EVP_PKEY_meth_get_count();
698 for (i = 0; i < meth_count; i++) {
699 const EVP_PKEY_METHOD *pmeth = EVP_PKEY_meth_get0(i);
700 int pkey_id, pkey_flags;
702 EVP_PKEY_meth_get0_info(&pkey_id, &pkey_flags, pmeth);
703 BIO_printf(bio_out, "%s\n", OBJ_nid2ln(pkey_id));
704 BIO_printf(bio_out, "\tType: %s Algorithm\n",
705 pkey_flags & ASN1_PKEY_DYNAMIC ? "External" : "Builtin");
709 static int function_cmp(const FUNCTION * a, const FUNCTION * b)
711 return strncmp(a->name, b->name, 8);
714 static unsigned long function_hash(const FUNCTION * a)
716 return OPENSSL_LH_strhash(a->name);
719 static int SortFnByName(const void *_f1, const void *_f2)
721 const FUNCTION *f1 = _f1;
722 const FUNCTION *f2 = _f2;
724 if (f1->type != f2->type)
725 return f1->type - f2->type;
726 return strcmp(f1->name, f2->name);
729 static void list_disabled(void)
731 BIO_puts(bio_out, "Disabled algorithms:\n");
732 #ifdef OPENSSL_NO_ARIA
733 BIO_puts(bio_out, "ARIA\n");
736 BIO_puts(bio_out, "BF\n");
738 #ifdef OPENSSL_NO_BLAKE2
739 BIO_puts(bio_out, "BLAKE2\n");
741 #ifdef OPENSSL_NO_CAMELLIA
742 BIO_puts(bio_out, "CAMELLIA\n");
744 #ifdef OPENSSL_NO_CAST
745 BIO_puts(bio_out, "CAST\n");
747 #ifdef OPENSSL_NO_CMAC
748 BIO_puts(bio_out, "CMAC\n");
750 #ifdef OPENSSL_NO_CMS
751 BIO_puts(bio_out, "CMS\n");
753 #ifdef OPENSSL_NO_COMP
754 BIO_puts(bio_out, "COMP\n");
756 #ifdef OPENSSL_NO_DES
757 BIO_puts(bio_out, "DES\n");
759 #ifdef OPENSSL_NO_DGRAM
760 BIO_puts(bio_out, "DGRAM\n");
763 BIO_puts(bio_out, "DH\n");
765 #ifdef OPENSSL_NO_DSA
766 BIO_puts(bio_out, "DSA\n");
768 #if defined(OPENSSL_NO_DTLS)
769 BIO_puts(bio_out, "DTLS\n");
771 #if defined(OPENSSL_NO_DTLS1)
772 BIO_puts(bio_out, "DTLS1\n");
774 #if defined(OPENSSL_NO_DTLS1_2)
775 BIO_puts(bio_out, "DTLS1_2\n");
778 BIO_puts(bio_out, "EC\n");
780 #ifdef OPENSSL_NO_EC2M
781 BIO_puts(bio_out, "EC2M\n");
783 #ifdef OPENSSL_NO_ENGINE
784 BIO_puts(bio_out, "ENGINE\n");
786 #ifdef OPENSSL_NO_GOST
787 BIO_puts(bio_out, "GOST\n");
789 #ifdef OPENSSL_NO_HEARTBEATS
790 BIO_puts(bio_out, "HEARTBEATS\n");
792 #ifdef OPENSSL_NO_IDEA
793 BIO_puts(bio_out, "IDEA\n");
795 #ifdef OPENSSL_NO_MD2
796 BIO_puts(bio_out, "MD2\n");
798 #ifdef OPENSSL_NO_MD4
799 BIO_puts(bio_out, "MD4\n");
801 #ifdef OPENSSL_NO_MD5
802 BIO_puts(bio_out, "MD5\n");
804 #ifdef OPENSSL_NO_MDC2
805 BIO_puts(bio_out, "MDC2\n");
807 #ifdef OPENSSL_NO_OCB
808 BIO_puts(bio_out, "OCB\n");
810 #ifdef OPENSSL_NO_OCSP
811 BIO_puts(bio_out, "OCSP\n");
813 #ifdef OPENSSL_NO_PSK
814 BIO_puts(bio_out, "PSK\n");
816 #ifdef OPENSSL_NO_RC2
817 BIO_puts(bio_out, "RC2\n");
819 #ifdef OPENSSL_NO_RC4
820 BIO_puts(bio_out, "RC4\n");
822 #ifdef OPENSSL_NO_RC5
823 BIO_puts(bio_out, "RC5\n");
825 #ifdef OPENSSL_NO_RMD160
826 BIO_puts(bio_out, "RMD160\n");
828 #ifdef OPENSSL_NO_RSA
829 BIO_puts(bio_out, "RSA\n");
831 #ifdef OPENSSL_NO_SCRYPT
832 BIO_puts(bio_out, "SCRYPT\n");
834 #ifdef OPENSSL_NO_SCTP
835 BIO_puts(bio_out, "SCTP\n");
837 #ifdef OPENSSL_NO_SEED
838 BIO_puts(bio_out, "SEED\n");
840 #ifdef OPENSSL_NO_SM2
841 BIO_puts(bio_out, "SM2\n");
843 #ifdef OPENSSL_NO_SM3
844 BIO_puts(bio_out, "SM3\n");
846 #ifdef OPENSSL_NO_SM4
847 BIO_puts(bio_out, "SM4\n");
849 #ifdef OPENSSL_NO_SOCK
850 BIO_puts(bio_out, "SOCK\n");
852 #ifdef OPENSSL_NO_SRP
853 BIO_puts(bio_out, "SRP\n");
855 #ifdef OPENSSL_NO_SRTP
856 BIO_puts(bio_out, "SRTP\n");
858 #ifdef OPENSSL_NO_SSL3
859 BIO_puts(bio_out, "SSL3\n");
861 #ifdef OPENSSL_NO_TLS1
862 BIO_puts(bio_out, "TLS1\n");
864 #ifdef OPENSSL_NO_TLS1_1
865 BIO_puts(bio_out, "TLS1_1\n");
867 #ifdef OPENSSL_NO_TLS1_2
868 BIO_puts(bio_out, "TLS1_2\n");
870 #ifdef OPENSSL_NO_WHIRLPOOL
871 BIO_puts(bio_out, "WHIRLPOOL\n");
874 BIO_puts(bio_out, "ZLIB\n");
878 static LHASH_OF(FUNCTION) *prog_init(void)
880 static LHASH_OF(FUNCTION) *ret = NULL;
881 static int prog_inited = 0;
890 /* Sort alphabetically within category. For nicer help displays. */
891 for (i = 0, f = functions; f->name != NULL; ++f, ++i)
893 qsort(functions, i, sizeof(*functions), SortFnByName);
895 if ((ret = lh_FUNCTION_new(function_hash, function_cmp)) == NULL)
898 for (f = functions; f->name != NULL; f++)
899 (void)lh_FUNCTION_insert(ret, f);