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;
128 int first, n, i, ret = 0;
133 /* Set up some of the environment. */
134 default_config_file = make_config_name();
135 bio_in = dup_bio_in(FORMAT_TEXT);
136 bio_out = dup_bio_out(FORMAT_TEXT);
137 bio_err = dup_bio_err(FORMAT_TEXT);
139 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
140 argv = copy_argv(&argc, argv);
141 #elif defined(_WIN32)
143 * Replace argv[] with UTF-8 encoded strings.
145 win32_utf8argv(&argc, &argv);
148 p = getenv("OPENSSL_DEBUG_MEMORY");
149 if (p != NULL && strcmp(p, "on") == 0)
150 CRYPTO_set_mem_debug(1);
151 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
153 if (getenv("OPENSSL_FIPS")) {
154 BIO_printf(bio_err, "FIPS mode not supported.\n");
158 if (!apps_startup()) {
160 "FATAL: Startup failure (dev note: apps_startup() failed)\n");
161 ERR_print_errors(bio_err);
167 pname = opt_progname(argv[0]);
169 /* first check the program name */
171 fp = lh_FUNCTION_retrieve(prog, &f);
174 ret = fp->func(argc, argv);
178 /* If there is stuff on the command line, run with that. */
182 ret = do_cmd(prog, argc, argv);
188 /* ok, lets enter interactive mode */
191 /* Read a line, continue reading if line ends with \ */
192 for (p = buf, n = sizeof(buf), i = 0, first = 1; n > 0; first = 0) {
193 prompt = first ? "OpenSSL> " : "> ";
196 fputs(prompt, stdout);
198 if (!fgets(p, n, stdin))
205 if (p[i - 2] != '\\')
212 extern char *readline(const char *);
213 extern void add_history(const char *cp);
216 text = readline(prompt);
222 if (text[i - 1] != '\\') {
223 p += strlen(strcpy(p, text));
230 p += strlen(strcpy(p, text));
237 if (!chopup_args(&arg, buf)) {
238 BIO_printf(bio_err, "Can't parse (no memory?)\n");
242 ret = do_cmd(prog, arg.argc, arg.argv);
243 if (ret == EXIT_THE_PROGRAM) {
248 BIO_printf(bio_err, "error in %s\n", arg.argv[0]);
249 (void)BIO_flush(bio_out);
250 (void)BIO_flush(bio_err);
254 OPENSSL_free(default_config_file);
255 lh_FUNCTION_free(prog);
256 OPENSSL_free(arg.argv);
260 BIO_free_all(bio_out);
262 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
263 if (CRYPTO_mem_leaks(bio_err) <= 0)
270 static void list_cipher_fn(const EVP_CIPHER *c,
271 const char *from, const char *to, void *arg)
274 BIO_printf(arg, "%s\n", EVP_CIPHER_name(c));
277 from = "<undefined>";
280 BIO_printf(arg, "%s => %s\n", from, to);
284 static void list_md_fn(const EVP_MD *m,
285 const char *from, const char *to, void *arg)
288 BIO_printf(arg, "%s\n", EVP_MD_name(m));
291 from = "<undefined>";
294 BIO_printf((BIO *)arg, "%s => %s\n", from, to);
298 static void list_mac_fn(const EVP_MAC *m,
299 const char *from, const char *to, void *arg)
302 BIO_printf(arg, "%s\n", EVP_MAC_name(m));
305 from = "<undefined>";
308 BIO_printf(arg, "%s => %s\n", from, to);
312 static void list_missing_help(void)
317 for (fp = functions; fp->name != NULL; fp++) {
318 if ((o = fp->help) != NULL) {
319 /* If there is help, list what flags are not documented. */
320 for ( ; o->name != NULL; o++) {
321 if (o->helpstr == NULL)
322 BIO_printf(bio_out, "%s %s\n", fp->name, o->name);
324 } else if (fp->func != dgst_main) {
325 /* If not aliased to the dgst command, */
326 BIO_printf(bio_out, "%s *\n", fp->name);
331 static void list_objects(void)
333 int max_nid = OBJ_new_nid(0);
335 char *oid_buf = NULL;
338 /* Skip 0, since that's NID_undef */
339 for (i = 1; i < max_nid; i++) {
340 const ASN1_OBJECT *obj = OBJ_nid2obj(i);
341 const char *sn = OBJ_nid2sn(i);
342 const char *ln = OBJ_nid2ln(i);
346 * If one of the retrieved objects somehow generated an error,
347 * we ignore it. The check for NID_undef below will detect the
348 * error and simply skip to the next NID.
352 if (OBJ_obj2nid(obj) == NID_undef)
355 if ((n = OBJ_obj2txt(NULL, 0, obj, 1)) == 0) {
356 BIO_printf(bio_out, "# None-OID object: %s, %s\n", sn, ln);
363 oid_buf = OPENSSL_realloc(oid_buf, n + 1);
364 if (oid_buf == NULL) {
365 BIO_printf(bio_err, "ERROR: Memory allocation\n");
370 if (OBJ_obj2txt(oid_buf, oid_size, obj, 1) < 0)
372 if (ln == NULL || strcmp(sn, ln) == 0)
373 BIO_printf(bio_out, "%s = %s\n", sn, oid_buf);
375 BIO_printf(bio_out, "%s = %s, %s\n", sn, ln, oid_buf);
378 OPENSSL_free(oid_buf);
381 static void list_options_for_command(const char *command)
386 for (fp = functions; fp->name != NULL; fp++)
387 if (strcmp(fp->name, command) == 0)
389 if (fp->name == NULL) {
390 BIO_printf(bio_err, "Invalid command '%s'; type \"help\" for a list.\n",
395 if ((o = fp->help) == NULL)
398 for ( ; o->name != NULL; o++) {
399 if (o->name == OPT_HELP_STR
400 || o->name == OPT_MORE_STR
401 || o->name[0] == '\0')
403 BIO_printf(bio_out, "%s %c\n", o->name, o->valtype);
408 /* Unified enum for help and list commands. */
409 typedef enum HELPLIST_CHOICE {
410 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, OPT_ONE,
411 OPT_COMMANDS, OPT_DIGEST_COMMANDS, OPT_MAC_ALGORITHMS, OPT_OPTIONS,
412 OPT_DIGEST_ALGORITHMS, OPT_CIPHER_COMMANDS, OPT_CIPHER_ALGORITHMS,
413 OPT_PK_ALGORITHMS, OPT_PK_METHOD, OPT_DISABLED, OPT_MISSING_HELP,
417 const OPTIONS list_options[] = {
418 {"help", OPT_HELP, '-', "Display this summary"},
419 {"1", OPT_ONE, '-', "List in one column"},
420 {"commands", OPT_COMMANDS, '-', "List of standard commands"},
421 {"digest-commands", OPT_DIGEST_COMMANDS, '-',
422 "List of message digest commands"},
423 {"digest-algorithms", OPT_DIGEST_ALGORITHMS, '-',
424 "List of message digest algorithms"},
425 {"mac-algorithms", OPT_MAC_ALGORITHMS, '-',
426 "List of message authentication code algorithms"},
427 {"cipher-commands", OPT_CIPHER_COMMANDS, '-', "List of cipher commands"},
428 {"cipher-algorithms", OPT_CIPHER_ALGORITHMS, '-',
429 "List of cipher algorithms"},
430 {"public-key-algorithms", OPT_PK_ALGORITHMS, '-',
431 "List of public key algorithms"},
432 {"public-key-methods", OPT_PK_METHOD, '-',
433 "List of public key methods"},
434 {"disabled", OPT_DISABLED, '-',
435 "List of disabled features"},
436 {"missing-help", OPT_MISSING_HELP, '-',
437 "List missing detailed help strings"},
438 {"options", OPT_OPTIONS, 's',
439 "List options for specified command"},
440 {"objects", OPT_OBJECTS, '-',
441 "List built in objects (OID<->name mappings)"},
445 int list_main(int argc, char **argv)
449 int one = 0, done = 0;
451 prog = opt_init(argc, argv, list_options);
452 while ((o = opt_next()) != OPT_EOF) {
454 case OPT_EOF: /* Never hit, but suppresses warning */
457 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
460 opt_help(list_options);
466 list_type(FT_general, one);
468 case OPT_DIGEST_COMMANDS:
469 list_type(FT_md, one);
471 case OPT_DIGEST_ALGORITHMS:
472 EVP_MD_do_all_sorted(list_md_fn, bio_out);
474 case OPT_MAC_ALGORITHMS:
475 EVP_MAC_do_all_sorted(list_mac_fn, bio_out);
477 case OPT_CIPHER_COMMANDS:
478 list_type(FT_cipher, one);
480 case OPT_CIPHER_ALGORITHMS:
481 EVP_CIPHER_do_all_sorted(list_cipher_fn, bio_out);
483 case OPT_PK_ALGORITHMS:
492 case OPT_MISSING_HELP:
499 list_options_for_command(opt_arg());
504 if (opt_num_rest() != 0) {
505 BIO_printf(bio_err, "Extra arguments given.\n");
515 typedef enum HELP_CHOICE {
516 OPT_hERR = -1, OPT_hEOF = 0, OPT_hHELP
519 const OPTIONS help_options[] = {
520 {OPT_HELP_STR, 1, '-', "Usage: help [options]\n"},
521 {OPT_HELP_STR, 1, '-', " help [command]\n"},
522 {"help", OPT_hHELP, '-', "Display this summary"},
527 int help_main(int argc, char **argv)
536 prog = opt_init(argc, argv, help_options);
537 while ((o = opt_next()) != OPT_hEOF) {
541 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
544 opt_help(help_options);
549 if (opt_num_rest() == 1) {
552 new_argv[0] = opt_rest()[0];
553 new_argv[1] = "--help";
555 return do_cmd(prog_init(), 2, new_argv);
557 if (opt_num_rest() != 0) {
558 BIO_printf(bio_err, "Usage: %s\n", prog);
562 calculate_columns(&dc);
563 BIO_printf(bio_err, "Standard commands");
566 for (fp = functions; fp->name != NULL; fp++) {
568 if (i++ % dc.columns == 0) {
569 BIO_printf(bio_err, "\n");
572 if (fp->type != tp) {
575 BIO_printf(bio_err, "\n");
579 "\nMessage Digest commands (see the `dgst' command for more details)\n");
580 } else if (tp == FT_cipher) {
583 "\nCipher commands (see the `enc' command for more details)\n");
586 BIO_printf(bio_err, "%-*s", dc.width, fp->name);
588 BIO_printf(bio_err, "\n\n");
592 static void list_type(FUNC_TYPE ft, int one)
596 DISPLAY_COLUMNS dc = {0};
599 calculate_columns(&dc);
601 for (fp = functions; fp->name != NULL; fp++) {
605 BIO_printf(bio_out, "%s\n", fp->name);
607 if (i % dc.columns == 0 && i > 0)
608 BIO_printf(bio_out, "\n");
609 BIO_printf(bio_out, "%-*s", dc.width, fp->name);
614 BIO_printf(bio_out, "\n\n");
617 static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[])
621 if (argc <= 0 || argv[0] == NULL)
624 fp = lh_FUNCTION_retrieve(prog, &f);
626 if (EVP_get_digestbyname(argv[0])) {
630 } else if (EVP_get_cipherbyname(argv[0])) {
637 return fp->func(argc, argv);
639 if ((strncmp(argv[0], "no-", 3)) == 0) {
641 * User is asking if foo is unsupported, by trying to "run" the
642 * no-foo command. Strange.
644 f.name = argv[0] + 3;
645 if (lh_FUNCTION_retrieve(prog, &f) == NULL) {
646 BIO_printf(bio_out, "%s\n", argv[0]);
649 BIO_printf(bio_out, "%s\n", argv[0] + 3);
652 if (strcmp(argv[0], "quit") == 0 || strcmp(argv[0], "q") == 0 ||
653 strcmp(argv[0], "exit") == 0 || strcmp(argv[0], "bye") == 0)
654 /* Special value to mean "exit the program. */
655 return EXIT_THE_PROGRAM;
657 BIO_printf(bio_err, "Invalid command '%s'; type \"help\" for a list.\n",
662 static void list_pkey(void)
666 for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
667 const EVP_PKEY_ASN1_METHOD *ameth;
668 int pkey_id, pkey_base_id, pkey_flags;
669 const char *pinfo, *pem_str;
670 ameth = EVP_PKEY_asn1_get0(i);
671 EVP_PKEY_asn1_get0_info(&pkey_id, &pkey_base_id, &pkey_flags,
672 &pinfo, &pem_str, ameth);
673 if (pkey_flags & ASN1_PKEY_ALIAS) {
674 BIO_printf(bio_out, "Name: %s\n", OBJ_nid2ln(pkey_id));
675 BIO_printf(bio_out, "\tAlias for: %s\n",
676 OBJ_nid2ln(pkey_base_id));
678 BIO_printf(bio_out, "Name: %s\n", pinfo);
679 BIO_printf(bio_out, "\tType: %s Algorithm\n",
680 pkey_flags & ASN1_PKEY_DYNAMIC ?
681 "External" : "Builtin");
682 BIO_printf(bio_out, "\tOID: %s\n", OBJ_nid2ln(pkey_id));
685 BIO_printf(bio_out, "\tPEM string: %s\n", pem_str);
691 static void list_pkey_meth(void)
694 size_t meth_count = EVP_PKEY_meth_get_count();
696 for (i = 0; i < meth_count; i++) {
697 const EVP_PKEY_METHOD *pmeth = EVP_PKEY_meth_get0(i);
698 int pkey_id, pkey_flags;
700 EVP_PKEY_meth_get0_info(&pkey_id, &pkey_flags, pmeth);
701 BIO_printf(bio_out, "%s\n", OBJ_nid2ln(pkey_id));
702 BIO_printf(bio_out, "\tType: %s Algorithm\n",
703 pkey_flags & ASN1_PKEY_DYNAMIC ? "External" : "Builtin");
707 static int function_cmp(const FUNCTION * a, const FUNCTION * b)
709 return strncmp(a->name, b->name, 8);
712 static unsigned long function_hash(const FUNCTION * a)
714 return OPENSSL_LH_strhash(a->name);
717 static int SortFnByName(const void *_f1, const void *_f2)
719 const FUNCTION *f1 = _f1;
720 const FUNCTION *f2 = _f2;
722 if (f1->type != f2->type)
723 return f1->type - f2->type;
724 return strcmp(f1->name, f2->name);
727 static void list_disabled(void)
729 BIO_puts(bio_out, "Disabled algorithms:\n");
730 #ifdef OPENSSL_NO_ARIA
731 BIO_puts(bio_out, "ARIA\n");
734 BIO_puts(bio_out, "BF\n");
736 #ifdef OPENSSL_NO_BLAKE2
737 BIO_puts(bio_out, "BLAKE2\n");
739 #ifdef OPENSSL_NO_CAMELLIA
740 BIO_puts(bio_out, "CAMELLIA\n");
742 #ifdef OPENSSL_NO_CAST
743 BIO_puts(bio_out, "CAST\n");
745 #ifdef OPENSSL_NO_CMAC
746 BIO_puts(bio_out, "CMAC\n");
748 #ifdef OPENSSL_NO_CMS
749 BIO_puts(bio_out, "CMS\n");
751 #ifdef OPENSSL_NO_COMP
752 BIO_puts(bio_out, "COMP\n");
754 #ifdef OPENSSL_NO_DES
755 BIO_puts(bio_out, "DES\n");
757 #ifdef OPENSSL_NO_DGRAM
758 BIO_puts(bio_out, "DGRAM\n");
761 BIO_puts(bio_out, "DH\n");
763 #ifdef OPENSSL_NO_DSA
764 BIO_puts(bio_out, "DSA\n");
766 #if defined(OPENSSL_NO_DTLS)
767 BIO_puts(bio_out, "DTLS\n");
769 #if defined(OPENSSL_NO_DTLS1)
770 BIO_puts(bio_out, "DTLS1\n");
772 #if defined(OPENSSL_NO_DTLS1_2)
773 BIO_puts(bio_out, "DTLS1_2\n");
776 BIO_puts(bio_out, "EC\n");
778 #ifdef OPENSSL_NO_EC2M
779 BIO_puts(bio_out, "EC2M\n");
781 #ifdef OPENSSL_NO_ENGINE
782 BIO_puts(bio_out, "ENGINE\n");
784 #ifdef OPENSSL_NO_GOST
785 BIO_puts(bio_out, "GOST\n");
787 #ifdef OPENSSL_NO_HEARTBEATS
788 BIO_puts(bio_out, "HEARTBEATS\n");
790 #ifdef OPENSSL_NO_IDEA
791 BIO_puts(bio_out, "IDEA\n");
793 #ifdef OPENSSL_NO_MD2
794 BIO_puts(bio_out, "MD2\n");
796 #ifdef OPENSSL_NO_MD4
797 BIO_puts(bio_out, "MD4\n");
799 #ifdef OPENSSL_NO_MD5
800 BIO_puts(bio_out, "MD5\n");
802 #ifdef OPENSSL_NO_MDC2
803 BIO_puts(bio_out, "MDC2\n");
805 #ifdef OPENSSL_NO_OCB
806 BIO_puts(bio_out, "OCB\n");
808 #ifdef OPENSSL_NO_OCSP
809 BIO_puts(bio_out, "OCSP\n");
811 #ifdef OPENSSL_NO_PSK
812 BIO_puts(bio_out, "PSK\n");
814 #ifdef OPENSSL_NO_RC2
815 BIO_puts(bio_out, "RC2\n");
817 #ifdef OPENSSL_NO_RC4
818 BIO_puts(bio_out, "RC4\n");
820 #ifdef OPENSSL_NO_RC5
821 BIO_puts(bio_out, "RC5\n");
823 #ifdef OPENSSL_NO_RMD160
824 BIO_puts(bio_out, "RMD160\n");
826 #ifdef OPENSSL_NO_RSA
827 BIO_puts(bio_out, "RSA\n");
829 #ifdef OPENSSL_NO_SCRYPT
830 BIO_puts(bio_out, "SCRYPT\n");
832 #ifdef OPENSSL_NO_SCTP
833 BIO_puts(bio_out, "SCTP\n");
835 #ifdef OPENSSL_NO_SEED
836 BIO_puts(bio_out, "SEED\n");
838 #ifdef OPENSSL_NO_SM2
839 BIO_puts(bio_out, "SM2\n");
841 #ifdef OPENSSL_NO_SM3
842 BIO_puts(bio_out, "SM3\n");
844 #ifdef OPENSSL_NO_SM4
845 BIO_puts(bio_out, "SM4\n");
847 #ifdef OPENSSL_NO_SOCK
848 BIO_puts(bio_out, "SOCK\n");
850 #ifdef OPENSSL_NO_SRP
851 BIO_puts(bio_out, "SRP\n");
853 #ifdef OPENSSL_NO_SRTP
854 BIO_puts(bio_out, "SRTP\n");
856 #ifdef OPENSSL_NO_SSL3
857 BIO_puts(bio_out, "SSL3\n");
859 #ifdef OPENSSL_NO_TLS1
860 BIO_puts(bio_out, "TLS1\n");
862 #ifdef OPENSSL_NO_TLS1_1
863 BIO_puts(bio_out, "TLS1_1\n");
865 #ifdef OPENSSL_NO_TLS1_2
866 BIO_puts(bio_out, "TLS1_2\n");
868 #ifdef OPENSSL_NO_WHIRLPOOL
869 BIO_puts(bio_out, "WHIRLPOOL\n");
872 BIO_puts(bio_out, "ZLIB\n");
876 static LHASH_OF(FUNCTION) *prog_init(void)
878 static LHASH_OF(FUNCTION) *ret = NULL;
879 static int prog_inited = 0;
888 /* Sort alphabetically within category. For nicer help displays. */
889 for (i = 0, f = functions; f->name != NULL; ++f, ++i)
891 qsort(functions, i, sizeof(*functions), SortFnByName);
893 if ((ret = lh_FUNCTION_new(function_hash, function_cmp)) == NULL)
896 for (f = functions; f->name != NULL; f++)
897 (void)lh_FUNCTION_insert(ret, f);