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 # include <openssl/fips.h>
27 #define USE_SOCKETS /* needed for the _O_BINARY defs in the MS world */
29 /* Needed to get the other O_xxx flags. */
30 #ifdef OPENSSL_SYS_VMS
33 #define INCLUDE_FUNCTION_TABLE
37 #ifdef OPENSSL_NO_CAMELLIA
38 # define FORMAT "%-15s"
41 # define FORMAT "%-18s"
45 /* Special sentinel to exit the program. */
46 #define EXIT_THE_PROGRAM (-1)
49 * The LHASH callbacks ("hash" & "cmp") have been replaced by functions with
50 * the base prototypes (we cast each variable inside the function to the
51 * required type of "FUNCTION*"). This removes the necessity for
52 * macro-generated wrapper functions.
54 static LHASH_OF(FUNCTION) *prog_init(void);
55 static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[]);
56 static void list_pkey(void);
57 static void list_type(FUNC_TYPE ft);
58 static void list_disabled(void);
59 char *default_config_file = NULL;
65 static int apps_startup()
68 signal(SIGPIPE, SIG_IGN);
71 /* Set non-default library initialisation settings */
72 if (!OPENSSL_init_crypto(OPENSSL_INIT_ENGINE_ALL_BUILTIN
73 | OPENSSL_INIT_LOAD_CONFIG, NULL))
83 static void apps_shutdown()
90 static char *make_config_name()
96 if ((t = getenv("OPENSSL_CONF")) != NULL)
97 return OPENSSL_strdup(t);
99 t = X509_get_default_cert_area();
100 len = strlen(t) + 1 + strlen(OPENSSL_CONF) + 1;
101 p = app_malloc(len, "config filename buffer");
103 #ifndef OPENSSL_SYS_VMS
106 strcat(p, OPENSSL_CONF);
111 int main(int argc, char *argv[])
114 LHASH_OF(FUNCTION) *prog = NULL;
115 char **copied_argv = NULL;
120 int first, n, i, ret = 0;
125 /* Set up some of the environment. */
126 default_config_file = make_config_name();
127 bio_in = dup_bio_in(FORMAT_TEXT);
128 bio_out = dup_bio_out(FORMAT_TEXT);
129 bio_err = dup_bio_err(FORMAT_TEXT);
131 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
132 copied_argv = argv = copy_argv(&argc, argv);
133 #elif defined(_WIN32)
135 * Replace argv[] with UTF-8 encoded strings.
137 win32_utf8argv(&argc, &argv);
140 p = getenv("OPENSSL_DEBUG_MEMORY");
141 if (p != NULL && strcmp(p, "on") == 0)
142 CRYPTO_set_mem_debug(1);
143 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
145 if (getenv("OPENSSL_FIPS")) {
147 if (!FIPS_mode_set(1)) {
148 ERR_print_errors(bio_err);
152 BIO_printf(bio_err, "FIPS mode not supported.\n");
161 pname = opt_progname(argv[0]);
163 /* first check the program name */
165 fp = lh_FUNCTION_retrieve(prog, &f);
168 ret = fp->func(argc, argv);
172 /* If there is stuff on the command line, run with that. */
176 ret = do_cmd(prog, argc, argv);
182 /* ok, lets enter interactive mode */
185 /* Read a line, continue reading if line ends with \ */
186 for (p = buf, n = sizeof buf, i = 0, first = 1; n > 0; first = 0) {
187 prompt = first ? "OpenSSL> " : "> ";
190 fputs(prompt, stdout);
192 if (!fgets(p, n, stdin))
199 if (p[i - 2] != '\\')
206 extern char *readline(const char *);
207 extern void add_history(const char *cp);
210 text = readline(prompt);
216 if (text[i - 1] != '\\') {
217 p += strlen(strcpy(p, text));
224 p += strlen(strcpy(p, text));
231 if (!chopup_args(&arg, buf)) {
232 BIO_printf(bio_err, "Can't parse (no memory?)\n");
236 ret = do_cmd(prog, arg.argc, arg.argv);
237 if (ret == EXIT_THE_PROGRAM) {
242 BIO_printf(bio_err, "error in %s\n", arg.argv[0]);
243 (void)BIO_flush(bio_out);
244 (void)BIO_flush(bio_err);
248 OPENSSL_free(copied_argv);
249 OPENSSL_free(default_config_file);
250 lh_FUNCTION_free(prog);
251 OPENSSL_free(arg.argv);
254 BIO_free_all(bio_out);
256 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
257 if (CRYPTO_mem_leaks(bio_err) <= 0)
264 const OPTIONS exit_options[] = {
268 static void list_cipher_fn(const EVP_CIPHER *c,
269 const char *from, const char *to, void *arg)
272 BIO_printf(arg, "%s\n", EVP_CIPHER_name(c));
275 from = "<undefined>";
278 BIO_printf(arg, "%s => %s\n", from, to);
282 static void list_md_fn(const EVP_MD *m,
283 const char *from, const char *to, void *arg)
286 BIO_printf(arg, "%s\n", EVP_MD_name(m));
289 from = "<undefined>";
292 BIO_printf((BIO *)arg, "%s => %s\n", from, to);
296 static void list_missing_help(void)
301 for (fp = functions; fp->name != NULL; fp++) {
302 if ((o = fp->help) == NULL) {
303 BIO_printf(bio_out, "%s *\n", fp->name);
306 for ( ; o->name != NULL; o++) {
307 if (o->helpstr == NULL)
308 BIO_printf(bio_out, "%s %s\n", fp->name, o->name);
314 /* Unified enum for help and list commands. */
315 typedef enum HELPLIST_CHOICE {
316 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
317 OPT_COMMANDS, OPT_DIGEST_COMMANDS,
318 OPT_DIGEST_ALGORITHMS, OPT_CIPHER_COMMANDS, OPT_CIPHER_ALGORITHMS,
319 OPT_PK_ALGORITHMS, OPT_DISABLED, OPT_MISSING_HELP
322 const OPTIONS list_options[] = {
323 {"help", OPT_HELP, '-', "Display this summary"},
324 {"commands", OPT_COMMANDS, '-', "List of standard commands"},
325 {"digest-commands", OPT_DIGEST_COMMANDS, '-',
326 "List of message digest commands"},
327 {"digest-algorithms", OPT_DIGEST_ALGORITHMS, '-',
328 "List of message digest algorithms"},
329 {"cipher-commands", OPT_CIPHER_COMMANDS, '-', "List of cipher commands"},
330 {"cipher-algorithms", OPT_CIPHER_ALGORITHMS, '-',
331 "List of cipher algorithms"},
332 {"public-key-algorithms", OPT_PK_ALGORITHMS, '-',
333 "List of public key algorithms"},
334 {"disabled", OPT_DISABLED, '-',
335 "List of disabled features"},
336 {"missing-help", OPT_MISSING_HELP, '-',
337 "List missing detailed help strings"},
341 int list_main(int argc, char **argv)
347 prog = opt_init(argc, argv, list_options);
348 while ((o = opt_next()) != OPT_EOF) {
350 case OPT_EOF: /* Never hit, but suppresses warning */
352 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
355 opt_help(list_options);
358 list_type(FT_general);
360 case OPT_DIGEST_COMMANDS:
363 case OPT_DIGEST_ALGORITHMS:
364 EVP_MD_do_all_sorted(list_md_fn, bio_out);
366 case OPT_CIPHER_COMMANDS:
367 list_type(FT_cipher);
369 case OPT_CIPHER_ALGORITHMS:
370 EVP_CIPHER_do_all_sorted(list_cipher_fn, bio_out);
372 case OPT_PK_ALGORITHMS:
378 case OPT_MISSING_HELP:
386 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
393 typedef enum HELP_CHOICE {
394 OPT_hERR = -1, OPT_hEOF = 0, OPT_hHELP
397 const OPTIONS help_options[] = {
398 {"help", OPT_hHELP, '-', "Display this summary"},
403 int help_main(int argc, char **argv)
411 prog = opt_init(argc, argv, help_options);
412 while ((o = opt_next()) != OPT_hEOF) {
416 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
419 opt_help(help_options);
424 if (opt_num_rest() != 0) {
425 BIO_printf(bio_err, "Usage: %s\n", prog);
429 BIO_printf(bio_err, "\nStandard commands");
432 for (fp = functions; fp->name != NULL; fp++) {
434 if (((i++) % COLUMNS) == 0) {
435 BIO_printf(bio_err, "\n");
438 if (fp->type != tp) {
441 BIO_printf(bio_err, "\n");
445 "\nMessage Digest commands (see the `dgst' command for more details)\n");
446 } else if (tp == FT_cipher) {
449 "\nCipher commands (see the `enc' command for more details)\n");
452 BIO_printf(bio_err, FORMAT, fp->name);
454 BIO_printf(bio_err, "\n\n");
458 int exit_main(int argc, char **argv)
460 return EXIT_THE_PROGRAM;
463 static void list_type(FUNC_TYPE ft)
468 for (fp = functions; fp->name != NULL; fp++)
469 if (fp->type == ft) {
470 if ((i++ % COLUMNS) == 0)
471 BIO_printf(bio_out, "\n");
472 BIO_printf(bio_out, FORMAT, fp->name);
474 BIO_printf(bio_out, "\n");
477 static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[])
481 if (argc <= 0 || argv[0] == NULL)
484 fp = lh_FUNCTION_retrieve(prog, &f);
486 if (EVP_get_digestbyname(argv[0])) {
490 } else if (EVP_get_cipherbyname(argv[0])) {
497 return (fp->func(argc, argv));
499 if ((strncmp(argv[0], "no-", 3)) == 0) {
501 * User is asking if foo is unsupported, by trying to "run" the
502 * no-foo command. Strange.
504 f.name = argv[0] + 3;
505 if (lh_FUNCTION_retrieve(prog, &f) == NULL) {
506 BIO_printf(bio_out, "%s\n", argv[0]);
509 BIO_printf(bio_out, "%s\n", argv[0] + 3);
512 if (strcmp(argv[0], "quit") == 0 || strcmp(argv[0], "q") == 0 ||
513 strcmp(argv[0], "exit") == 0 || strcmp(argv[0], "bye") == 0)
514 /* Special value to mean "exit the program. */
515 return EXIT_THE_PROGRAM;
517 BIO_printf(bio_err, "Invalid command '%s'; type \"help\" for a list.\n",
522 static void list_pkey(void)
526 for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
527 const EVP_PKEY_ASN1_METHOD *ameth;
528 int pkey_id, pkey_base_id, pkey_flags;
529 const char *pinfo, *pem_str;
530 ameth = EVP_PKEY_asn1_get0(i);
531 EVP_PKEY_asn1_get0_info(&pkey_id, &pkey_base_id, &pkey_flags,
532 &pinfo, &pem_str, ameth);
533 if (pkey_flags & ASN1_PKEY_ALIAS) {
534 BIO_printf(bio_out, "Name: %s\n", OBJ_nid2ln(pkey_id));
535 BIO_printf(bio_out, "\tAlias for: %s\n",
536 OBJ_nid2ln(pkey_base_id));
538 BIO_printf(bio_out, "Name: %s\n", pinfo);
539 BIO_printf(bio_out, "\tType: %s Algorithm\n",
540 pkey_flags & ASN1_PKEY_DYNAMIC ?
541 "External" : "Builtin");
542 BIO_printf(bio_out, "\tOID: %s\n", OBJ_nid2ln(pkey_id));
545 BIO_printf(bio_out, "\tPEM string: %s\n", pem_str);
551 static int function_cmp(const FUNCTION * a, const FUNCTION * b)
553 return strncmp(a->name, b->name, 8);
556 static unsigned long function_hash(const FUNCTION * a)
558 return OPENSSL_LH_strhash(a->name);
561 static int SortFnByName(const void *_f1, const void *_f2)
563 const FUNCTION *f1 = _f1;
564 const FUNCTION *f2 = _f2;
566 if (f1->type != f2->type)
567 return f1->type - f2->type;
568 return strcmp(f1->name, f2->name);
571 static void list_disabled(void)
573 BIO_puts(bio_out, "Disabled algorithms:\n");
574 #ifdef OPENSSL_NO_ARIA
575 BIO_puts(bio_out, "ARIA\n");
578 BIO_puts(bio_out, "BF\n");
580 #ifdef OPENSSL_NO_BLAKE2
581 BIO_puts(bio_out, "BLAKE2\n");
583 #ifdef OPENSSL_NO_CAMELLIA
584 BIO_puts(bio_out, "CAMELLIA\n");
586 #ifdef OPENSSL_NO_CAST
587 BIO_puts(bio_out, "CAST\n");
589 #ifdef OPENSSL_NO_CMAC
590 BIO_puts(bio_out, "CMAC\n");
592 #ifdef OPENSSL_NO_CMS
593 BIO_puts(bio_out, "CMS\n");
595 #ifdef OPENSSL_NO_COMP
596 BIO_puts(bio_out, "COMP\n");
598 #ifdef OPENSSL_NO_DES
599 BIO_puts(bio_out, "DES\n");
601 #ifdef OPENSSL_NO_DGRAM
602 BIO_puts(bio_out, "DGRAM\n");
605 BIO_puts(bio_out, "DH\n");
607 #ifdef OPENSSL_NO_DSA
608 BIO_puts(bio_out, "DSA\n");
610 #if defined(OPENSSL_NO_DTLS)
611 BIO_puts(bio_out, "DTLS\n");
613 #if defined(OPENSSL_NO_DTLS1)
614 BIO_puts(bio_out, "DTLS1\n");
616 #if defined(OPENSSL_NO_DTLS1_2)
617 BIO_puts(bio_out, "DTLS1_2\n");
620 BIO_puts(bio_out, "EC\n");
622 #ifdef OPENSSL_NO_EC2M
623 BIO_puts(bio_out, "EC2M\n");
625 #ifdef OPENSSL_NO_ENGINE
626 BIO_puts(bio_out, "ENGINE\n");
628 #ifdef OPENSSL_NO_GOST
629 BIO_puts(bio_out, "GOST\n");
631 #ifdef OPENSSL_NO_HEARTBEATS
632 BIO_puts(bio_out, "HEARTBEATS\n");
634 #ifdef OPENSSL_NO_IDEA
635 BIO_puts(bio_out, "IDEA\n");
637 #ifdef OPENSSL_NO_MD2
638 BIO_puts(bio_out, "MD2\n");
640 #ifdef OPENSSL_NO_MD4
641 BIO_puts(bio_out, "MD4\n");
643 #ifdef OPENSSL_NO_MD5
644 BIO_puts(bio_out, "MD5\n");
646 #ifdef OPENSSL_NO_MDC2
647 BIO_puts(bio_out, "MDC2\n");
649 #ifdef OPENSSL_NO_OCB
650 BIO_puts(bio_out, "OCB\n");
652 #ifdef OPENSSL_NO_OCSP
653 BIO_puts(bio_out, "OCSP\n");
655 #ifdef OPENSSL_NO_PSK
656 BIO_puts(bio_out, "PSK\n");
658 #ifdef OPENSSL_NO_RC2
659 BIO_puts(bio_out, "RC2\n");
661 #ifdef OPENSSL_NO_RC4
662 BIO_puts(bio_out, "RC4\n");
664 #ifdef OPENSSL_NO_RC5
665 BIO_puts(bio_out, "RC5\n");
667 #ifdef OPENSSL_NO_RMD160
668 BIO_puts(bio_out, "RMD160\n");
670 #ifdef OPENSSL_NO_RSA
671 BIO_puts(bio_out, "RSA\n");
673 #ifdef OPENSSL_NO_SCRYPT
674 BIO_puts(bio_out, "SCRYPT\n");
676 #ifdef OPENSSL_NO_SCTP
677 BIO_puts(bio_out, "SCTP\n");
679 #ifdef OPENSSL_NO_SEED
680 BIO_puts(bio_out, "SEED\n");
682 #ifdef OPENSSL_NO_SOCK
683 BIO_puts(bio_out, "SOCK\n");
685 #ifdef OPENSSL_NO_SRP
686 BIO_puts(bio_out, "SRP\n");
688 #ifdef OPENSSL_NO_SRTP
689 BIO_puts(bio_out, "SRTP\n");
691 #ifdef OPENSSL_NO_SSL3
692 BIO_puts(bio_out, "SSL3\n");
694 #ifdef OPENSSL_NO_TLS1
695 BIO_puts(bio_out, "TLS1\n");
697 #ifdef OPENSSL_NO_TLS1_1
698 BIO_puts(bio_out, "TLS1_1\n");
700 #ifdef OPENSSL_NO_TLS1_2
701 BIO_puts(bio_out, "TLS1_2\n");
703 #ifdef OPENSSL_NO_WHIRLPOOL
704 BIO_puts(bio_out, "WHIRLPOOL\n");
707 BIO_puts(bio_out, "ZLIB\n");
711 static LHASH_OF(FUNCTION) *prog_init(void)
713 LHASH_OF(FUNCTION) *ret;
717 /* Sort alphabetically within category. For nicer help displays. */
718 for (i = 0, f = functions; f->name != NULL; ++f, ++i) ;
719 qsort(functions, i, sizeof(*functions), SortFnByName);
721 if ((ret = lh_FUNCTION_new(function_hash, function_cmp)) == NULL)
724 for (f = functions; f->name != NULL; f++)
725 (void)lh_FUNCTION_insert(ret, f);