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>
24 #define USE_SOCKETS /* needed for the _O_BINARY defs in the MS world */
26 /* Needed to get the other O_xxx flags. */
27 #ifdef OPENSSL_SYS_VMS
30 #define INCLUDE_FUNCTION_TABLE
34 #ifdef OPENSSL_NO_CAMELLIA
35 # define FORMAT "%-15s"
38 # define FORMAT "%-18s"
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 int apps_startup()
66 signal(SIGPIPE, SIG_IGN);
69 /* Set non-default library initialisation settings */
70 if (!OPENSSL_init_crypto(OPENSSL_INIT_ENGINE_ALL_BUILTIN
71 | OPENSSL_INIT_LOAD_CONFIG, NULL))
79 static void apps_shutdown()
84 static char *make_config_name()
90 if ((t = getenv("OPENSSL_CONF")) != NULL)
91 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");
97 #ifndef OPENSSL_SYS_VMS
100 strcat(p, OPENSSL_CONF);
105 int main(int argc, char *argv[])
108 LHASH_OF(FUNCTION) *prog = NULL;
109 char **copied_argv = NULL;
114 int first, n, i, ret = 0;
119 /* Set up some of the environment. */
120 default_config_file = make_config_name();
121 bio_in = dup_bio_in(FORMAT_TEXT);
122 bio_out = dup_bio_out(FORMAT_TEXT);
123 bio_err = dup_bio_err(FORMAT_TEXT);
125 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
126 copied_argv = argv = copy_argv(&argc, argv);
127 #elif defined(_WIN32)
129 * Replace argv[] with UTF-8 encoded strings.
131 win32_utf8argv(&argc, &argv);
134 p = getenv("OPENSSL_DEBUG_MEMORY");
135 if (p != NULL && strcmp(p, "on") == 0)
136 CRYPTO_set_mem_debug(1);
137 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
139 if (getenv("OPENSSL_FIPS")) {
140 BIO_printf(bio_err, "FIPS mode not supported.\n");
144 if (!apps_startup()) {
146 "FATAL: Startup failure (dev note: apps_startup() failed)\n");
147 ERR_print_errors(bio_err);
153 pname = opt_progname(argv[0]);
155 /* first check the program name */
157 fp = lh_FUNCTION_retrieve(prog, &f);
160 ret = fp->func(argc, argv);
164 /* If there is stuff on the command line, run with that. */
168 ret = do_cmd(prog, argc, argv);
174 /* ok, lets enter interactive mode */
177 /* Read a line, continue reading if line ends with \ */
178 for (p = buf, n = sizeof buf, i = 0, first = 1; n > 0; first = 0) {
179 prompt = first ? "OpenSSL> " : "> ";
182 fputs(prompt, stdout);
184 if (!fgets(p, n, stdin))
191 if (p[i - 2] != '\\')
198 extern char *readline(const char *);
199 extern void add_history(const char *cp);
202 text = readline(prompt);
208 if (text[i - 1] != '\\') {
209 p += strlen(strcpy(p, text));
216 p += strlen(strcpy(p, text));
223 if (!chopup_args(&arg, buf)) {
224 BIO_printf(bio_err, "Can't parse (no memory?)\n");
228 ret = do_cmd(prog, arg.argc, arg.argv);
229 if (ret == EXIT_THE_PROGRAM) {
234 BIO_printf(bio_err, "error in %s\n", arg.argv[0]);
235 (void)BIO_flush(bio_out);
236 (void)BIO_flush(bio_err);
240 OPENSSL_free(copied_argv);
241 OPENSSL_free(default_config_file);
242 lh_FUNCTION_free(prog);
243 OPENSSL_free(arg.argv);
247 BIO_free_all(bio_out);
249 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
250 if (CRYPTO_mem_leaks(bio_err) <= 0)
257 const OPTIONS exit_options[] = {
261 static void list_cipher_fn(const EVP_CIPHER *c,
262 const char *from, const char *to, void *arg)
265 BIO_printf(arg, "%s\n", EVP_CIPHER_name(c));
268 from = "<undefined>";
271 BIO_printf(arg, "%s => %s\n", from, to);
275 static void list_md_fn(const EVP_MD *m,
276 const char *from, const char *to, void *arg)
279 BIO_printf(arg, "%s\n", EVP_MD_name(m));
282 from = "<undefined>";
285 BIO_printf((BIO *)arg, "%s => %s\n", from, to);
289 static void list_missing_help(void)
294 for (fp = functions; fp->name != NULL; fp++) {
295 if ((o = fp->help) != NULL) {
296 /* If there is help, list what flags are not documented. */
297 for ( ; o->name != NULL; o++) {
298 if (o->helpstr == NULL)
299 BIO_printf(bio_out, "%s %s\n", fp->name, o->name);
301 } else if (fp->func != dgst_main) {
302 /* If not aliased to the dgst command, */
303 BIO_printf(bio_out, "%s *\n", fp->name);
308 static void list_options_for_command(const char *command)
313 for (fp = functions; fp->name != NULL; fp++)
314 if (strcmp(fp->name, command) == 0)
316 if (fp->name == NULL) {
317 BIO_printf(bio_err, "Invalid command '%s'; type \"help\" for a list.\n",
322 if ((o = fp->help) == NULL)
325 for ( ; o->name != NULL; o++) {
326 if (o->name == OPT_HELP_STR
327 || o->name == OPT_MORE_STR
328 || o->name[0] == '\0')
330 BIO_printf(bio_out, "%s %c\n", o->name, o->valtype);
335 /* Unified enum for help and list commands. */
336 typedef enum HELPLIST_CHOICE {
337 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, OPT_ONE,
338 OPT_COMMANDS, OPT_DIGEST_COMMANDS, OPT_OPTIONS,
339 OPT_DIGEST_ALGORITHMS, OPT_CIPHER_COMMANDS, OPT_CIPHER_ALGORITHMS,
340 OPT_PK_ALGORITHMS, OPT_PK_METHOD, OPT_DISABLED, OPT_MISSING_HELP
343 const OPTIONS list_options[] = {
344 {"help", OPT_HELP, '-', "Display this summary"},
345 {"1", OPT_ONE, '-', "List in one column"},
346 {"commands", OPT_COMMANDS, '-', "List of standard commands"},
347 {"digest-commands", OPT_DIGEST_COMMANDS, '-',
348 "List of message digest commands"},
349 {"digest-algorithms", OPT_DIGEST_ALGORITHMS, '-',
350 "List of message digest algorithms"},
351 {"cipher-commands", OPT_CIPHER_COMMANDS, '-', "List of cipher commands"},
352 {"cipher-algorithms", OPT_CIPHER_ALGORITHMS, '-',
353 "List of cipher algorithms"},
354 {"public-key-algorithms", OPT_PK_ALGORITHMS, '-',
355 "List of public key algorithms"},
356 {"public-key-methods", OPT_PK_METHOD, '-',
357 "List of public key methods"},
358 {"disabled", OPT_DISABLED, '-',
359 "List of disabled features"},
360 {"missing-help", OPT_MISSING_HELP, '-',
361 "List missing detailed help strings"},
362 {"options", OPT_OPTIONS, 's',
363 "List options for specified command"},
367 int list_main(int argc, char **argv)
371 int one = 0, done = 0;
373 prog = opt_init(argc, argv, list_options);
374 while ((o = opt_next()) != OPT_EOF) {
376 case OPT_EOF: /* Never hit, but suppresses warning */
378 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
381 opt_help(list_options);
387 list_type(FT_general, one);
389 case OPT_DIGEST_COMMANDS:
390 list_type(FT_md, one);
392 case OPT_DIGEST_ALGORITHMS:
393 EVP_MD_do_all_sorted(list_md_fn, bio_out);
395 case OPT_CIPHER_COMMANDS:
396 list_type(FT_cipher, one);
398 case OPT_CIPHER_ALGORITHMS:
399 EVP_CIPHER_do_all_sorted(list_cipher_fn, bio_out);
401 case OPT_PK_ALGORITHMS:
410 case OPT_MISSING_HELP:
414 list_options_for_command(opt_arg());
421 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
428 typedef enum HELP_CHOICE {
429 OPT_hERR = -1, OPT_hEOF = 0, OPT_hHELP
432 const OPTIONS help_options[] = {
433 {"help", OPT_hHELP, '-', "Display this summary"},
438 int help_main(int argc, char **argv)
446 prog = opt_init(argc, argv, help_options);
447 while ((o = opt_next()) != OPT_hEOF) {
451 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
454 opt_help(help_options);
459 if (opt_num_rest() != 0) {
460 BIO_printf(bio_err, "Usage: %s\n", prog);
464 BIO_printf(bio_err, "\nStandard commands");
467 for (fp = functions; fp->name != NULL; fp++) {
469 if (((i++) % COLUMNS) == 0) {
470 BIO_printf(bio_err, "\n");
473 if (fp->type != tp) {
476 BIO_printf(bio_err, "\n");
480 "\nMessage Digest commands (see the `dgst' command for more details)\n");
481 } else if (tp == FT_cipher) {
484 "\nCipher commands (see the `enc' command for more details)\n");
487 BIO_printf(bio_err, FORMAT, fp->name);
489 BIO_printf(bio_err, "\n\n");
493 int exit_main(int argc, char **argv)
495 return EXIT_THE_PROGRAM;
498 static void list_type(FUNC_TYPE ft, int one)
503 for (fp = functions; fp->name != NULL; fp++) {
507 BIO_printf(bio_out, "%s\n", fp->name);
509 if ((i++ % COLUMNS) == 0 && fp != functions)
510 BIO_printf(bio_out, "\n");
511 BIO_printf(bio_out, FORMAT, fp->name);
515 BIO_printf(bio_out, "\n");
518 static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[])
522 if (argc <= 0 || argv[0] == NULL)
525 fp = lh_FUNCTION_retrieve(prog, &f);
527 if (EVP_get_digestbyname(argv[0])) {
531 } else if (EVP_get_cipherbyname(argv[0])) {
538 return (fp->func(argc, argv));
540 if ((strncmp(argv[0], "no-", 3)) == 0) {
542 * User is asking if foo is unsupported, by trying to "run" the
543 * no-foo command. Strange.
545 f.name = argv[0] + 3;
546 if (lh_FUNCTION_retrieve(prog, &f) == NULL) {
547 BIO_printf(bio_out, "%s\n", argv[0]);
550 BIO_printf(bio_out, "%s\n", argv[0] + 3);
553 if (strcmp(argv[0], "quit") == 0 || strcmp(argv[0], "q") == 0 ||
554 strcmp(argv[0], "exit") == 0 || strcmp(argv[0], "bye") == 0)
555 /* Special value to mean "exit the program. */
556 return EXIT_THE_PROGRAM;
558 BIO_printf(bio_err, "Invalid command '%s'; type \"help\" for a list.\n",
563 static void list_pkey(void)
567 for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
568 const EVP_PKEY_ASN1_METHOD *ameth;
569 int pkey_id, pkey_base_id, pkey_flags;
570 const char *pinfo, *pem_str;
571 ameth = EVP_PKEY_asn1_get0(i);
572 EVP_PKEY_asn1_get0_info(&pkey_id, &pkey_base_id, &pkey_flags,
573 &pinfo, &pem_str, ameth);
574 if (pkey_flags & ASN1_PKEY_ALIAS) {
575 BIO_printf(bio_out, "Name: %s\n", OBJ_nid2ln(pkey_id));
576 BIO_printf(bio_out, "\tAlias for: %s\n",
577 OBJ_nid2ln(pkey_base_id));
579 BIO_printf(bio_out, "Name: %s\n", pinfo);
580 BIO_printf(bio_out, "\tType: %s Algorithm\n",
581 pkey_flags & ASN1_PKEY_DYNAMIC ?
582 "External" : "Builtin");
583 BIO_printf(bio_out, "\tOID: %s\n", OBJ_nid2ln(pkey_id));
586 BIO_printf(bio_out, "\tPEM string: %s\n", pem_str);
592 static void list_pkey_meth(void)
595 size_t meth_count = EVP_PKEY_meth_get_count();
597 for (i = 0; i < meth_count; i++) {
598 const EVP_PKEY_METHOD *pmeth = EVP_PKEY_meth_get0(i);
599 int pkey_id, pkey_flags;
601 EVP_PKEY_meth_get0_info(&pkey_id, &pkey_flags, pmeth);
602 BIO_printf(bio_out, "%s\n", OBJ_nid2ln(pkey_id));
603 BIO_printf(bio_out, "\tType: %s Algorithm\n",
604 pkey_flags & ASN1_PKEY_DYNAMIC ? "External" : "Builtin");
608 static int function_cmp(const FUNCTION * a, const FUNCTION * b)
610 return strncmp(a->name, b->name, 8);
613 static unsigned long function_hash(const FUNCTION * a)
615 return OPENSSL_LH_strhash(a->name);
618 static int SortFnByName(const void *_f1, const void *_f2)
620 const FUNCTION *f1 = _f1;
621 const FUNCTION *f2 = _f2;
623 if (f1->type != f2->type)
624 return f1->type - f2->type;
625 return strcmp(f1->name, f2->name);
628 static void list_disabled(void)
630 BIO_puts(bio_out, "Disabled algorithms:\n");
631 #ifdef OPENSSL_NO_ARIA
632 BIO_puts(bio_out, "ARIA\n");
635 BIO_puts(bio_out, "BF\n");
637 #ifdef OPENSSL_NO_BLAKE2
638 BIO_puts(bio_out, "BLAKE2\n");
640 #ifdef OPENSSL_NO_CAMELLIA
641 BIO_puts(bio_out, "CAMELLIA\n");
643 #ifdef OPENSSL_NO_CAST
644 BIO_puts(bio_out, "CAST\n");
646 #ifdef OPENSSL_NO_CMAC
647 BIO_puts(bio_out, "CMAC\n");
649 #ifdef OPENSSL_NO_CMS
650 BIO_puts(bio_out, "CMS\n");
652 #ifdef OPENSSL_NO_COMP
653 BIO_puts(bio_out, "COMP\n");
655 #ifdef OPENSSL_NO_DES
656 BIO_puts(bio_out, "DES\n");
658 #ifdef OPENSSL_NO_DGRAM
659 BIO_puts(bio_out, "DGRAM\n");
662 BIO_puts(bio_out, "DH\n");
664 #ifdef OPENSSL_NO_DSA
665 BIO_puts(bio_out, "DSA\n");
667 #if defined(OPENSSL_NO_DTLS)
668 BIO_puts(bio_out, "DTLS\n");
670 #if defined(OPENSSL_NO_DTLS1)
671 BIO_puts(bio_out, "DTLS1\n");
673 #if defined(OPENSSL_NO_DTLS1_2)
674 BIO_puts(bio_out, "DTLS1_2\n");
677 BIO_puts(bio_out, "EC\n");
679 #ifdef OPENSSL_NO_EC2M
680 BIO_puts(bio_out, "EC2M\n");
682 #ifdef OPENSSL_NO_ENGINE
683 BIO_puts(bio_out, "ENGINE\n");
685 #ifdef OPENSSL_NO_GOST
686 BIO_puts(bio_out, "GOST\n");
688 #ifdef OPENSSL_NO_HEARTBEATS
689 BIO_puts(bio_out, "HEARTBEATS\n");
691 #ifdef OPENSSL_NO_IDEA
692 BIO_puts(bio_out, "IDEA\n");
694 #ifdef OPENSSL_NO_MD2
695 BIO_puts(bio_out, "MD2\n");
697 #ifdef OPENSSL_NO_MD4
698 BIO_puts(bio_out, "MD4\n");
700 #ifdef OPENSSL_NO_MD5
701 BIO_puts(bio_out, "MD5\n");
703 #ifdef OPENSSL_NO_MDC2
704 BIO_puts(bio_out, "MDC2\n");
706 #ifdef OPENSSL_NO_OCB
707 BIO_puts(bio_out, "OCB\n");
709 #ifdef OPENSSL_NO_OCSP
710 BIO_puts(bio_out, "OCSP\n");
712 #ifdef OPENSSL_NO_PSK
713 BIO_puts(bio_out, "PSK\n");
715 #ifdef OPENSSL_NO_RC2
716 BIO_puts(bio_out, "RC2\n");
718 #ifdef OPENSSL_NO_RC4
719 BIO_puts(bio_out, "RC4\n");
721 #ifdef OPENSSL_NO_RC5
722 BIO_puts(bio_out, "RC5\n");
724 #ifdef OPENSSL_NO_RMD160
725 BIO_puts(bio_out, "RMD160\n");
727 #ifdef OPENSSL_NO_RSA
728 BIO_puts(bio_out, "RSA\n");
730 #ifdef OPENSSL_NO_SCRYPT
731 BIO_puts(bio_out, "SCRYPT\n");
733 #ifdef OPENSSL_NO_SCTP
734 BIO_puts(bio_out, "SCTP\n");
736 #ifdef OPENSSL_NO_SEED
737 BIO_puts(bio_out, "SEED\n");
739 #ifdef OPENSSL_NO_SOCK
740 BIO_puts(bio_out, "SOCK\n");
742 #ifdef OPENSSL_NO_SRP
743 BIO_puts(bio_out, "SRP\n");
745 #ifdef OPENSSL_NO_SRTP
746 BIO_puts(bio_out, "SRTP\n");
748 #ifdef OPENSSL_NO_SSL3
749 BIO_puts(bio_out, "SSL3\n");
751 #ifdef OPENSSL_NO_TLS1
752 BIO_puts(bio_out, "TLS1\n");
754 #ifdef OPENSSL_NO_TLS1_1
755 BIO_puts(bio_out, "TLS1_1\n");
757 #ifdef OPENSSL_NO_TLS1_2
758 BIO_puts(bio_out, "TLS1_2\n");
760 #ifdef OPENSSL_NO_WHIRLPOOL
761 BIO_puts(bio_out, "WHIRLPOOL\n");
764 BIO_puts(bio_out, "ZLIB\n");
768 static LHASH_OF(FUNCTION) *prog_init(void)
770 LHASH_OF(FUNCTION) *ret;
774 /* Sort alphabetically within category. For nicer help displays. */
775 for (i = 0, f = functions; f->name != NULL; ++f, ++i) ;
776 qsort(functions, i, sizeof(*functions), SortFnByName);
778 if ((ret = lh_FUNCTION_new(function_hash, function_cmp)) == NULL)
781 for (f = functions; f->name != NULL; f++)
782 (void)lh_FUNCTION_insert(ret, f);