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_type(FUNC_TYPE ft);
55 static void list_disabled(void);
56 char *default_config_file = NULL;
62 static int apps_startup()
65 signal(SIGPIPE, SIG_IGN);
68 /* Set non-default library initialisation settings */
69 if (!OPENSSL_init_crypto(OPENSSL_INIT_ENGINE_ALL_BUILTIN
70 | OPENSSL_INIT_LOAD_CONFIG, NULL))
80 static void apps_shutdown()
87 static char *make_config_name()
93 if ((t = getenv("OPENSSL_CONF")) != NULL)
94 return OPENSSL_strdup(t);
96 t = X509_get_default_cert_area();
97 len = strlen(t) + 1 + strlen(OPENSSL_CONF) + 1;
98 p = app_malloc(len, "config filename buffer");
100 #ifndef OPENSSL_SYS_VMS
103 strcat(p, OPENSSL_CONF);
108 int main(int argc, char *argv[])
111 LHASH_OF(FUNCTION) *prog = NULL;
112 char **copied_argv = NULL;
117 int first, n, i, ret = 0;
122 /* Set up some of the environment. */
123 default_config_file = make_config_name();
124 bio_in = dup_bio_in(FORMAT_TEXT);
125 bio_out = dup_bio_out(FORMAT_TEXT);
126 bio_err = dup_bio_err(FORMAT_TEXT);
128 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
129 copied_argv = argv = copy_argv(&argc, argv);
130 #elif defined(_WIN32)
132 * Replace argv[] with UTF-8 encoded strings.
134 win32_utf8argv(&argc, &argv);
137 p = getenv("OPENSSL_DEBUG_MEMORY");
138 if (p != NULL && strcmp(p, "on") == 0)
139 CRYPTO_set_mem_debug(1);
140 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
142 if (getenv("OPENSSL_FIPS")) {
143 BIO_printf(bio_err, "FIPS mode not supported.\n");
151 pname = opt_progname(argv[0]);
153 /* first check the program name */
155 fp = lh_FUNCTION_retrieve(prog, &f);
158 ret = fp->func(argc, argv);
162 /* If there is stuff on the command line, run with that. */
166 ret = do_cmd(prog, argc, argv);
172 /* ok, lets enter interactive mode */
175 /* Read a line, continue reading if line ends with \ */
176 for (p = buf, n = sizeof buf, i = 0, first = 1; n > 0; first = 0) {
177 prompt = first ? "OpenSSL> " : "> ";
180 fputs(prompt, stdout);
182 if (!fgets(p, n, stdin))
189 if (p[i - 2] != '\\')
196 extern char *readline(const char *);
197 extern void add_history(const char *cp);
200 text = readline(prompt);
206 if (text[i - 1] != '\\') {
207 p += strlen(strcpy(p, text));
214 p += strlen(strcpy(p, text));
221 if (!chopup_args(&arg, buf)) {
222 BIO_printf(bio_err, "Can't parse (no memory?)\n");
226 ret = do_cmd(prog, arg.argc, arg.argv);
227 if (ret == EXIT_THE_PROGRAM) {
232 BIO_printf(bio_err, "error in %s\n", arg.argv[0]);
233 (void)BIO_flush(bio_out);
234 (void)BIO_flush(bio_err);
238 OPENSSL_free(copied_argv);
239 OPENSSL_free(default_config_file);
240 lh_FUNCTION_free(prog);
241 OPENSSL_free(arg.argv);
244 BIO_free_all(bio_out);
246 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
247 if (CRYPTO_mem_leaks(bio_err) <= 0)
254 const OPTIONS exit_options[] = {
258 static void list_cipher_fn(const EVP_CIPHER *c,
259 const char *from, const char *to, void *arg)
262 BIO_printf(arg, "%s\n", EVP_CIPHER_name(c));
265 from = "<undefined>";
268 BIO_printf(arg, "%s => %s\n", from, to);
272 static void list_md_fn(const EVP_MD *m,
273 const char *from, const char *to, void *arg)
276 BIO_printf(arg, "%s\n", EVP_MD_name(m));
279 from = "<undefined>";
282 BIO_printf((BIO *)arg, "%s => %s\n", from, to);
286 static void list_missing_help(void)
291 for (fp = functions; fp->name != NULL; fp++) {
292 if ((o = fp->help) == NULL) {
293 BIO_printf(bio_out, "%s *\n", fp->name);
296 for ( ; o->name != NULL; o++) {
297 if (o->helpstr == NULL)
298 BIO_printf(bio_out, "%s %s\n", fp->name, o->name);
304 /* Unified enum for help and list commands. */
305 typedef enum HELPLIST_CHOICE {
306 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
307 OPT_COMMANDS, OPT_DIGEST_COMMANDS,
308 OPT_DIGEST_ALGORITHMS, OPT_CIPHER_COMMANDS, OPT_CIPHER_ALGORITHMS,
309 OPT_PK_ALGORITHMS, OPT_DISABLED, OPT_MISSING_HELP
312 const OPTIONS list_options[] = {
313 {"help", OPT_HELP, '-', "Display this summary"},
314 {"commands", OPT_COMMANDS, '-', "List of standard commands"},
315 {"digest-commands", OPT_DIGEST_COMMANDS, '-',
316 "List of message digest commands"},
317 {"digest-algorithms", OPT_DIGEST_ALGORITHMS, '-',
318 "List of message digest algorithms"},
319 {"cipher-commands", OPT_CIPHER_COMMANDS, '-', "List of cipher commands"},
320 {"cipher-algorithms", OPT_CIPHER_ALGORITHMS, '-',
321 "List of cipher algorithms"},
322 {"public-key-algorithms", OPT_PK_ALGORITHMS, '-',
323 "List of public key algorithms"},
324 {"disabled", OPT_DISABLED, '-',
325 "List of disabled features"},
326 {"missing-help", OPT_MISSING_HELP, '-',
327 "List missing detailed help strings"},
331 int list_main(int argc, char **argv)
337 prog = opt_init(argc, argv, list_options);
338 while ((o = opt_next()) != OPT_EOF) {
340 case OPT_EOF: /* Never hit, but suppresses warning */
342 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
345 opt_help(list_options);
348 list_type(FT_general);
350 case OPT_DIGEST_COMMANDS:
353 case OPT_DIGEST_ALGORITHMS:
354 EVP_MD_do_all_sorted(list_md_fn, bio_out);
356 case OPT_CIPHER_COMMANDS:
357 list_type(FT_cipher);
359 case OPT_CIPHER_ALGORITHMS:
360 EVP_CIPHER_do_all_sorted(list_cipher_fn, bio_out);
362 case OPT_PK_ALGORITHMS:
368 case OPT_MISSING_HELP:
376 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
383 typedef enum HELP_CHOICE {
384 OPT_hERR = -1, OPT_hEOF = 0, OPT_hHELP
387 const OPTIONS help_options[] = {
388 {"help", OPT_hHELP, '-', "Display this summary"},
393 int help_main(int argc, char **argv)
401 prog = opt_init(argc, argv, help_options);
402 while ((o = opt_next()) != OPT_hEOF) {
406 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
409 opt_help(help_options);
414 if (opt_num_rest() != 0) {
415 BIO_printf(bio_err, "Usage: %s\n", prog);
419 BIO_printf(bio_err, "\nStandard commands");
422 for (fp = functions; fp->name != NULL; fp++) {
424 if (((i++) % COLUMNS) == 0) {
425 BIO_printf(bio_err, "\n");
428 if (fp->type != tp) {
431 BIO_printf(bio_err, "\n");
435 "\nMessage Digest commands (see the `dgst' command for more details)\n");
436 } else if (tp == FT_cipher) {
439 "\nCipher commands (see the `enc' command for more details)\n");
442 BIO_printf(bio_err, FORMAT, fp->name);
444 BIO_printf(bio_err, "\n\n");
448 int exit_main(int argc, char **argv)
450 return EXIT_THE_PROGRAM;
453 static void list_type(FUNC_TYPE ft)
458 for (fp = functions; fp->name != NULL; fp++)
459 if (fp->type == ft) {
460 if ((i++ % COLUMNS) == 0)
461 BIO_printf(bio_out, "\n");
462 BIO_printf(bio_out, FORMAT, fp->name);
464 BIO_printf(bio_out, "\n");
467 static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[])
471 if (argc <= 0 || argv[0] == NULL)
474 fp = lh_FUNCTION_retrieve(prog, &f);
476 if (EVP_get_digestbyname(argv[0])) {
480 } else if (EVP_get_cipherbyname(argv[0])) {
487 return (fp->func(argc, argv));
489 if ((strncmp(argv[0], "no-", 3)) == 0) {
491 * User is asking if foo is unsupported, by trying to "run" the
492 * no-foo command. Strange.
494 f.name = argv[0] + 3;
495 if (lh_FUNCTION_retrieve(prog, &f) == NULL) {
496 BIO_printf(bio_out, "%s\n", argv[0]);
499 BIO_printf(bio_out, "%s\n", argv[0] + 3);
502 if (strcmp(argv[0], "quit") == 0 || strcmp(argv[0], "q") == 0 ||
503 strcmp(argv[0], "exit") == 0 || strcmp(argv[0], "bye") == 0)
504 /* Special value to mean "exit the program. */
505 return EXIT_THE_PROGRAM;
507 BIO_printf(bio_err, "Invalid command '%s'; type \"help\" for a list.\n",
512 static void list_pkey(void)
516 for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
517 const EVP_PKEY_ASN1_METHOD *ameth;
518 int pkey_id, pkey_base_id, pkey_flags;
519 const char *pinfo, *pem_str;
520 ameth = EVP_PKEY_asn1_get0(i);
521 EVP_PKEY_asn1_get0_info(&pkey_id, &pkey_base_id, &pkey_flags,
522 &pinfo, &pem_str, ameth);
523 if (pkey_flags & ASN1_PKEY_ALIAS) {
524 BIO_printf(bio_out, "Name: %s\n", OBJ_nid2ln(pkey_id));
525 BIO_printf(bio_out, "\tAlias for: %s\n",
526 OBJ_nid2ln(pkey_base_id));
528 BIO_printf(bio_out, "Name: %s\n", pinfo);
529 BIO_printf(bio_out, "\tType: %s Algorithm\n",
530 pkey_flags & ASN1_PKEY_DYNAMIC ?
531 "External" : "Builtin");
532 BIO_printf(bio_out, "\tOID: %s\n", OBJ_nid2ln(pkey_id));
535 BIO_printf(bio_out, "\tPEM string: %s\n", pem_str);
541 static int function_cmp(const FUNCTION * a, const FUNCTION * b)
543 return strncmp(a->name, b->name, 8);
546 static unsigned long function_hash(const FUNCTION * a)
548 return OPENSSL_LH_strhash(a->name);
551 static int SortFnByName(const void *_f1, const void *_f2)
553 const FUNCTION *f1 = _f1;
554 const FUNCTION *f2 = _f2;
556 if (f1->type != f2->type)
557 return f1->type - f2->type;
558 return strcmp(f1->name, f2->name);
561 static void list_disabled(void)
563 BIO_puts(bio_out, "Disabled algorithms:\n");
564 #ifdef OPENSSL_NO_ARIA
565 BIO_puts(bio_out, "ARIA\n");
568 BIO_puts(bio_out, "BF\n");
570 #ifdef OPENSSL_NO_BLAKE2
571 BIO_puts(bio_out, "BLAKE2\n");
573 #ifdef OPENSSL_NO_CAMELLIA
574 BIO_puts(bio_out, "CAMELLIA\n");
576 #ifdef OPENSSL_NO_CAST
577 BIO_puts(bio_out, "CAST\n");
579 #ifdef OPENSSL_NO_CMAC
580 BIO_puts(bio_out, "CMAC\n");
582 #ifdef OPENSSL_NO_CMS
583 BIO_puts(bio_out, "CMS\n");
585 #ifdef OPENSSL_NO_COMP
586 BIO_puts(bio_out, "COMP\n");
588 #ifdef OPENSSL_NO_DES
589 BIO_puts(bio_out, "DES\n");
591 #ifdef OPENSSL_NO_DGRAM
592 BIO_puts(bio_out, "DGRAM\n");
595 BIO_puts(bio_out, "DH\n");
597 #ifdef OPENSSL_NO_DSA
598 BIO_puts(bio_out, "DSA\n");
600 #if defined(OPENSSL_NO_DTLS)
601 BIO_puts(bio_out, "DTLS\n");
603 #if defined(OPENSSL_NO_DTLS1)
604 BIO_puts(bio_out, "DTLS1\n");
606 #if defined(OPENSSL_NO_DTLS1_2)
607 BIO_puts(bio_out, "DTLS1_2\n");
610 BIO_puts(bio_out, "EC\n");
612 #ifdef OPENSSL_NO_EC2M
613 BIO_puts(bio_out, "EC2M\n");
615 #ifdef OPENSSL_NO_ENGINE
616 BIO_puts(bio_out, "ENGINE\n");
618 #ifdef OPENSSL_NO_GOST
619 BIO_puts(bio_out, "GOST\n");
621 #ifdef OPENSSL_NO_HEARTBEATS
622 BIO_puts(bio_out, "HEARTBEATS\n");
624 #ifdef OPENSSL_NO_IDEA
625 BIO_puts(bio_out, "IDEA\n");
627 #ifdef OPENSSL_NO_MD2
628 BIO_puts(bio_out, "MD2\n");
630 #ifdef OPENSSL_NO_MD4
631 BIO_puts(bio_out, "MD4\n");
633 #ifdef OPENSSL_NO_MD5
634 BIO_puts(bio_out, "MD5\n");
636 #ifdef OPENSSL_NO_MDC2
637 BIO_puts(bio_out, "MDC2\n");
639 #ifdef OPENSSL_NO_OCB
640 BIO_puts(bio_out, "OCB\n");
642 #ifdef OPENSSL_NO_OCSP
643 BIO_puts(bio_out, "OCSP\n");
645 #ifdef OPENSSL_NO_PSK
646 BIO_puts(bio_out, "PSK\n");
648 #ifdef OPENSSL_NO_RC2
649 BIO_puts(bio_out, "RC2\n");
651 #ifdef OPENSSL_NO_RC4
652 BIO_puts(bio_out, "RC4\n");
654 #ifdef OPENSSL_NO_RC5
655 BIO_puts(bio_out, "RC5\n");
657 #ifdef OPENSSL_NO_RMD160
658 BIO_puts(bio_out, "RMD160\n");
660 #ifdef OPENSSL_NO_RSA
661 BIO_puts(bio_out, "RSA\n");
663 #ifdef OPENSSL_NO_SCRYPT
664 BIO_puts(bio_out, "SCRYPT\n");
666 #ifdef OPENSSL_NO_SCTP
667 BIO_puts(bio_out, "SCTP\n");
669 #ifdef OPENSSL_NO_SEED
670 BIO_puts(bio_out, "SEED\n");
672 #ifdef OPENSSL_NO_SOCK
673 BIO_puts(bio_out, "SOCK\n");
675 #ifdef OPENSSL_NO_SRP
676 BIO_puts(bio_out, "SRP\n");
678 #ifdef OPENSSL_NO_SRTP
679 BIO_puts(bio_out, "SRTP\n");
681 #ifdef OPENSSL_NO_SSL3
682 BIO_puts(bio_out, "SSL3\n");
684 #ifdef OPENSSL_NO_TLS1
685 BIO_puts(bio_out, "TLS1\n");
687 #ifdef OPENSSL_NO_TLS1_1
688 BIO_puts(bio_out, "TLS1_1\n");
690 #ifdef OPENSSL_NO_TLS1_2
691 BIO_puts(bio_out, "TLS1_2\n");
693 #ifdef OPENSSL_NO_WHIRLPOOL
694 BIO_puts(bio_out, "WHIRLPOOL\n");
697 BIO_puts(bio_out, "ZLIB\n");
701 static LHASH_OF(FUNCTION) *prog_init(void)
703 LHASH_OF(FUNCTION) *ret;
707 /* Sort alphabetically within category. For nicer help displays. */
708 for (i = 0, f = functions; f->name != NULL; ++f, ++i) ;
709 qsort(functions, i, sizeof(*functions), SortFnByName);
711 if ((ret = lh_FUNCTION_new(function_hash, function_cmp)) == NULL)
714 for (f = functions; f->name != NULL; f++)
715 (void)lh_FUNCTION_insert(ret, f);