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);
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 BIO_printf(bio_out, "%s *\n", fp->name);
299 for ( ; o->name != NULL; o++) {
300 if (o->helpstr == NULL)
301 BIO_printf(bio_out, "%s %s\n", fp->name, o->name);
307 /* Unified enum for help and list commands. */
308 typedef enum HELPLIST_CHOICE {
309 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
310 OPT_COMMANDS, OPT_DIGEST_COMMANDS,
311 OPT_DIGEST_ALGORITHMS, OPT_CIPHER_COMMANDS, OPT_CIPHER_ALGORITHMS,
312 OPT_PK_ALGORITHMS, OPT_PK_METHOD, OPT_DISABLED, OPT_MISSING_HELP
315 const OPTIONS list_options[] = {
316 {"help", OPT_HELP, '-', "Display this summary"},
317 {"commands", OPT_COMMANDS, '-', "List of standard commands"},
318 {"digest-commands", OPT_DIGEST_COMMANDS, '-',
319 "List of message digest commands"},
320 {"digest-algorithms", OPT_DIGEST_ALGORITHMS, '-',
321 "List of message digest algorithms"},
322 {"cipher-commands", OPT_CIPHER_COMMANDS, '-', "List of cipher commands"},
323 {"cipher-algorithms", OPT_CIPHER_ALGORITHMS, '-',
324 "List of cipher algorithms"},
325 {"public-key-algorithms", OPT_PK_ALGORITHMS, '-',
326 "List of public key algorithms"},
327 {"public-key-methods", OPT_PK_METHOD, '-',
328 "List of public key methods"},
329 {"disabled", OPT_DISABLED, '-',
330 "List of disabled features"},
331 {"missing-help", OPT_MISSING_HELP, '-',
332 "List missing detailed help strings"},
336 int list_main(int argc, char **argv)
342 prog = opt_init(argc, argv, list_options);
343 while ((o = opt_next()) != OPT_EOF) {
345 case OPT_EOF: /* Never hit, but suppresses warning */
347 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
350 opt_help(list_options);
353 list_type(FT_general);
355 case OPT_DIGEST_COMMANDS:
358 case OPT_DIGEST_ALGORITHMS:
359 EVP_MD_do_all_sorted(list_md_fn, bio_out);
361 case OPT_CIPHER_COMMANDS:
362 list_type(FT_cipher);
364 case OPT_CIPHER_ALGORITHMS:
365 EVP_CIPHER_do_all_sorted(list_cipher_fn, bio_out);
367 case OPT_PK_ALGORITHMS:
376 case OPT_MISSING_HELP:
384 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
391 typedef enum HELP_CHOICE {
392 OPT_hERR = -1, OPT_hEOF = 0, OPT_hHELP
395 const OPTIONS help_options[] = {
396 {"help", OPT_hHELP, '-', "Display this summary"},
401 int help_main(int argc, char **argv)
409 prog = opt_init(argc, argv, help_options);
410 while ((o = opt_next()) != OPT_hEOF) {
414 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
417 opt_help(help_options);
422 if (opt_num_rest() != 0) {
423 BIO_printf(bio_err, "Usage: %s\n", prog);
427 BIO_printf(bio_err, "\nStandard commands");
430 for (fp = functions; fp->name != NULL; fp++) {
432 if (((i++) % COLUMNS) == 0) {
433 BIO_printf(bio_err, "\n");
436 if (fp->type != tp) {
439 BIO_printf(bio_err, "\n");
443 "\nMessage Digest commands (see the `dgst' command for more details)\n");
444 } else if (tp == FT_cipher) {
447 "\nCipher commands (see the `enc' command for more details)\n");
450 BIO_printf(bio_err, FORMAT, fp->name);
452 BIO_printf(bio_err, "\n\n");
456 int exit_main(int argc, char **argv)
458 return EXIT_THE_PROGRAM;
461 static void list_type(FUNC_TYPE ft)
466 for (fp = functions; fp->name != NULL; fp++)
467 if (fp->type == ft) {
468 if ((i++ % COLUMNS) == 0)
469 BIO_printf(bio_out, "\n");
470 BIO_printf(bio_out, FORMAT, fp->name);
472 BIO_printf(bio_out, "\n");
475 static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[])
479 if (argc <= 0 || argv[0] == NULL)
482 fp = lh_FUNCTION_retrieve(prog, &f);
484 if (EVP_get_digestbyname(argv[0])) {
488 } else if (EVP_get_cipherbyname(argv[0])) {
495 return (fp->func(argc, argv));
497 if ((strncmp(argv[0], "no-", 3)) == 0) {
499 * User is asking if foo is unsupported, by trying to "run" the
500 * no-foo command. Strange.
502 f.name = argv[0] + 3;
503 if (lh_FUNCTION_retrieve(prog, &f) == NULL) {
504 BIO_printf(bio_out, "%s\n", argv[0]);
507 BIO_printf(bio_out, "%s\n", argv[0] + 3);
510 if (strcmp(argv[0], "quit") == 0 || strcmp(argv[0], "q") == 0 ||
511 strcmp(argv[0], "exit") == 0 || strcmp(argv[0], "bye") == 0)
512 /* Special value to mean "exit the program. */
513 return EXIT_THE_PROGRAM;
515 BIO_printf(bio_err, "Invalid command '%s'; type \"help\" for a list.\n",
520 static void list_pkey(void)
524 for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
525 const EVP_PKEY_ASN1_METHOD *ameth;
526 int pkey_id, pkey_base_id, pkey_flags;
527 const char *pinfo, *pem_str;
528 ameth = EVP_PKEY_asn1_get0(i);
529 EVP_PKEY_asn1_get0_info(&pkey_id, &pkey_base_id, &pkey_flags,
530 &pinfo, &pem_str, ameth);
531 if (pkey_flags & ASN1_PKEY_ALIAS) {
532 BIO_printf(bio_out, "Name: %s\n", OBJ_nid2ln(pkey_id));
533 BIO_printf(bio_out, "\tAlias for: %s\n",
534 OBJ_nid2ln(pkey_base_id));
536 BIO_printf(bio_out, "Name: %s\n", pinfo);
537 BIO_printf(bio_out, "\tType: %s Algorithm\n",
538 pkey_flags & ASN1_PKEY_DYNAMIC ?
539 "External" : "Builtin");
540 BIO_printf(bio_out, "\tOID: %s\n", OBJ_nid2ln(pkey_id));
543 BIO_printf(bio_out, "\tPEM string: %s\n", pem_str);
549 static void list_pkey_meth(void)
552 size_t meth_count = EVP_PKEY_meth_get_count();
554 for (i = 0; i < meth_count; i++) {
555 const EVP_PKEY_METHOD *pmeth = EVP_PKEY_meth_get0(i);
556 int pkey_id, pkey_flags;
558 EVP_PKEY_meth_get0_info(&pkey_id, &pkey_flags, pmeth);
559 BIO_printf(bio_out, "%s\n", OBJ_nid2ln(pkey_id));
560 BIO_printf(bio_out, "\tType: %s Algorithm\n",
561 pkey_flags & ASN1_PKEY_DYNAMIC ? "External" : "Builtin");
565 static int function_cmp(const FUNCTION * a, const FUNCTION * b)
567 return strncmp(a->name, b->name, 8);
570 static unsigned long function_hash(const FUNCTION * a)
572 return OPENSSL_LH_strhash(a->name);
575 static int SortFnByName(const void *_f1, const void *_f2)
577 const FUNCTION *f1 = _f1;
578 const FUNCTION *f2 = _f2;
580 if (f1->type != f2->type)
581 return f1->type - f2->type;
582 return strcmp(f1->name, f2->name);
585 static void list_disabled(void)
587 BIO_puts(bio_out, "Disabled algorithms:\n");
588 #ifdef OPENSSL_NO_ARIA
589 BIO_puts(bio_out, "ARIA\n");
592 BIO_puts(bio_out, "BF\n");
594 #ifdef OPENSSL_NO_BLAKE2
595 BIO_puts(bio_out, "BLAKE2\n");
597 #ifdef OPENSSL_NO_CAMELLIA
598 BIO_puts(bio_out, "CAMELLIA\n");
600 #ifdef OPENSSL_NO_CAST
601 BIO_puts(bio_out, "CAST\n");
603 #ifdef OPENSSL_NO_CMAC
604 BIO_puts(bio_out, "CMAC\n");
606 #ifdef OPENSSL_NO_CMS
607 BIO_puts(bio_out, "CMS\n");
609 #ifdef OPENSSL_NO_COMP
610 BIO_puts(bio_out, "COMP\n");
612 #ifdef OPENSSL_NO_DES
613 BIO_puts(bio_out, "DES\n");
615 #ifdef OPENSSL_NO_DGRAM
616 BIO_puts(bio_out, "DGRAM\n");
619 BIO_puts(bio_out, "DH\n");
621 #ifdef OPENSSL_NO_DSA
622 BIO_puts(bio_out, "DSA\n");
624 #if defined(OPENSSL_NO_DTLS)
625 BIO_puts(bio_out, "DTLS\n");
627 #if defined(OPENSSL_NO_DTLS1)
628 BIO_puts(bio_out, "DTLS1\n");
630 #if defined(OPENSSL_NO_DTLS1_2)
631 BIO_puts(bio_out, "DTLS1_2\n");
634 BIO_puts(bio_out, "EC\n");
636 #ifdef OPENSSL_NO_EC2M
637 BIO_puts(bio_out, "EC2M\n");
639 #ifdef OPENSSL_NO_ENGINE
640 BIO_puts(bio_out, "ENGINE\n");
642 #ifdef OPENSSL_NO_GOST
643 BIO_puts(bio_out, "GOST\n");
645 #ifdef OPENSSL_NO_HEARTBEATS
646 BIO_puts(bio_out, "HEARTBEATS\n");
648 #ifdef OPENSSL_NO_IDEA
649 BIO_puts(bio_out, "IDEA\n");
651 #ifdef OPENSSL_NO_MD2
652 BIO_puts(bio_out, "MD2\n");
654 #ifdef OPENSSL_NO_MD4
655 BIO_puts(bio_out, "MD4\n");
657 #ifdef OPENSSL_NO_MD5
658 BIO_puts(bio_out, "MD5\n");
660 #ifdef OPENSSL_NO_MDC2
661 BIO_puts(bio_out, "MDC2\n");
663 #ifdef OPENSSL_NO_OCB
664 BIO_puts(bio_out, "OCB\n");
666 #ifdef OPENSSL_NO_OCSP
667 BIO_puts(bio_out, "OCSP\n");
669 #ifdef OPENSSL_NO_PSK
670 BIO_puts(bio_out, "PSK\n");
672 #ifdef OPENSSL_NO_RC2
673 BIO_puts(bio_out, "RC2\n");
675 #ifdef OPENSSL_NO_RC4
676 BIO_puts(bio_out, "RC4\n");
678 #ifdef OPENSSL_NO_RC5
679 BIO_puts(bio_out, "RC5\n");
681 #ifdef OPENSSL_NO_RMD160
682 BIO_puts(bio_out, "RMD160\n");
684 #ifdef OPENSSL_NO_RSA
685 BIO_puts(bio_out, "RSA\n");
687 #ifdef OPENSSL_NO_SCRYPT
688 BIO_puts(bio_out, "SCRYPT\n");
690 #ifdef OPENSSL_NO_SCTP
691 BIO_puts(bio_out, "SCTP\n");
693 #ifdef OPENSSL_NO_SEED
694 BIO_puts(bio_out, "SEED\n");
696 #ifdef OPENSSL_NO_SOCK
697 BIO_puts(bio_out, "SOCK\n");
699 #ifdef OPENSSL_NO_SRP
700 BIO_puts(bio_out, "SRP\n");
702 #ifdef OPENSSL_NO_SRTP
703 BIO_puts(bio_out, "SRTP\n");
705 #ifdef OPENSSL_NO_SSL3
706 BIO_puts(bio_out, "SSL3\n");
708 #ifdef OPENSSL_NO_TLS1
709 BIO_puts(bio_out, "TLS1\n");
711 #ifdef OPENSSL_NO_TLS1_1
712 BIO_puts(bio_out, "TLS1_1\n");
714 #ifdef OPENSSL_NO_TLS1_2
715 BIO_puts(bio_out, "TLS1_2\n");
717 #ifdef OPENSSL_NO_WHIRLPOOL
718 BIO_puts(bio_out, "WHIRLPOOL\n");
721 BIO_puts(bio_out, "ZLIB\n");
725 static LHASH_OF(FUNCTION) *prog_init(void)
727 LHASH_OF(FUNCTION) *ret;
731 /* Sort alphabetically within category. For nicer help displays. */
732 for (i = 0, f = functions; f->name != NULL; ++f, ++i) ;
733 qsort(functions, i, sizeof(*functions), SortFnByName);
735 if ((ret = lh_FUNCTION_new(function_hash, function_cmp)) == NULL)
738 for (f = functions; f->name != NULL; f++)
739 (void)lh_FUNCTION_insert(ret, f);