2 * Copyright 1995-2016 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;
61 static CONF *config = NULL;
66 static int apps_startup()
69 signal(SIGPIPE, SIG_IGN);
72 /* Set non-default library initialisation settings */
73 if (!OPENSSL_init_crypto(OPENSSL_INIT_ENGINE_ALL_BUILTIN
74 | OPENSSL_INIT_LOAD_CONFIG, NULL))
84 static void apps_shutdown()
91 static char *make_config_name()
97 if ((t = getenv("OPENSSL_CONF")) != NULL)
98 return OPENSSL_strdup(t);
100 t = X509_get_default_cert_area();
101 len = strlen(t) + 1 + strlen(OPENSSL_CONF) + 1;
102 p = app_malloc(len, "config filename buffer");
104 #ifndef OPENSSL_SYS_VMS
107 strcat(p, OPENSSL_CONF);
112 int main(int argc, char *argv[])
115 LHASH_OF(FUNCTION) *prog = NULL;
116 char **copied_argv = NULL;
121 int first, n, i, ret = 0;
126 /* Set up some of the environment. */
127 default_config_file = make_config_name();
128 bio_in = dup_bio_in(FORMAT_TEXT);
129 bio_out = dup_bio_out(FORMAT_TEXT);
130 bio_err = dup_bio_err(FORMAT_TEXT);
132 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
133 copied_argv = argv = copy_argv(&argc, argv);
134 #elif defined(_WIN32)
136 * Replace argv[] with UTF-8 encoded strings.
138 win32_utf8argv(&argc, &argv);
141 p = getenv("OPENSSL_DEBUG_MEMORY");
142 if (p != NULL && strcmp(p, "on") == 0)
143 CRYPTO_set_mem_debug(1);
144 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
146 if (getenv("OPENSSL_FIPS")) {
148 if (!FIPS_mode_set(1)) {
149 ERR_print_errors(bio_err);
153 BIO_printf(bio_err, "FIPS mode not supported.\n");
162 pname = opt_progname(argv[0]);
164 /* first check the program name */
166 fp = lh_FUNCTION_retrieve(prog, &f);
169 ret = fp->func(argc, argv);
173 /* If there is stuff on the command line, run with that. */
177 ret = do_cmd(prog, argc, argv);
183 /* ok, lets enter interactive mode */
186 /* Read a line, continue reading if line ends with \ */
187 for (p = buf, n = sizeof buf, i = 0, first = 1; n > 0; first = 0) {
188 prompt = first ? "OpenSSL> " : "> ";
191 fputs(prompt, stdout);
193 if (!fgets(p, n, stdin))
200 if (p[i - 2] != '\\')
207 extern char *readline(const char *);
208 extern void add_history(const char *cp);
211 text = readline(prompt);
217 if (text[i - 1] != '\\') {
218 p += strlen(strcpy(p, text));
225 p += strlen(strcpy(p, text));
232 if (!chopup_args(&arg, buf)) {
233 BIO_printf(bio_err, "Can't parse (no memory?)\n");
237 ret = do_cmd(prog, arg.argc, arg.argv);
238 if (ret == EXIT_THE_PROGRAM) {
243 BIO_printf(bio_err, "error in %s\n", arg.argv[0]);
244 (void)BIO_flush(bio_out);
245 (void)BIO_flush(bio_err);
249 OPENSSL_free(copied_argv);
250 OPENSSL_free(default_config_file);
253 lh_FUNCTION_free(prog);
254 OPENSSL_free(arg.argv);
257 BIO_free_all(bio_out);
259 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
260 if (CRYPTO_mem_leaks(bio_err) <= 0)
267 OPTIONS exit_options[] = {
271 static void list_cipher_fn(const EVP_CIPHER *c,
272 const char *from, const char *to, void *arg)
275 BIO_printf(arg, "%s\n", EVP_CIPHER_name(c));
278 from = "<undefined>";
281 BIO_printf(arg, "%s => %s\n", from, to);
285 static void list_md_fn(const EVP_MD *m,
286 const char *from, const char *to, void *arg)
289 BIO_printf(arg, "%s\n", EVP_MD_name(m));
292 from = "<undefined>";
295 BIO_printf((BIO *)arg, "%s => %s\n", from, to);
299 /* Unified enum for help and list commands. */
300 typedef enum HELPLIST_CHOICE {
301 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
302 OPT_COMMANDS, OPT_DIGEST_COMMANDS,
303 OPT_DIGEST_ALGORITHMS, OPT_CIPHER_COMMANDS, OPT_CIPHER_ALGORITHMS,
304 OPT_PK_ALGORITHMS, OPT_DISABLED
307 OPTIONS list_options[] = {
308 {"help", OPT_HELP, '-', "Display this summary"},
309 {"commands", OPT_COMMANDS, '-', "List of standard commands"},
310 {"digest-commands", OPT_DIGEST_COMMANDS, '-',
311 "List of message digest commands"},
312 {"digest-algorithms", OPT_DIGEST_ALGORITHMS, '-',
313 "List of message digest algorithms"},
314 {"cipher-commands", OPT_CIPHER_COMMANDS, '-', "List of cipher commands"},
315 {"cipher-algorithms", OPT_CIPHER_ALGORITHMS, '-',
316 "List of cipher algorithms"},
317 {"public-key-algorithms", OPT_PK_ALGORITHMS, '-',
318 "List of public key algorithms"},
319 {"disabled", OPT_DISABLED, '-',
320 "List of disabled features"},
324 int list_main(int argc, char **argv)
330 prog = opt_init(argc, argv, list_options);
331 while ((o = opt_next()) != OPT_EOF) {
333 case OPT_EOF: /* Never hit, but suppresses warning */
335 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
338 opt_help(list_options);
341 list_type(FT_general);
343 case OPT_DIGEST_COMMANDS:
346 case OPT_DIGEST_ALGORITHMS:
347 EVP_MD_do_all_sorted(list_md_fn, bio_out);
349 case OPT_CIPHER_COMMANDS:
350 list_type(FT_cipher);
352 case OPT_CIPHER_ALGORITHMS:
353 EVP_CIPHER_do_all_sorted(list_cipher_fn, bio_out);
355 case OPT_PK_ALGORITHMS:
366 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
373 OPTIONS help_options[] = {
374 {"help", OPT_HELP, '-', "Display this summary"},
378 int help_main(int argc, char **argv)
386 prog = opt_init(argc, argv, help_options);
387 while ((o = opt_next()) != OPT_EOF) {
390 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
393 opt_help(help_options);
398 if (opt_num_rest() != 0) {
399 BIO_printf(bio_err, "Usage: %s\n", prog);
403 BIO_printf(bio_err, "\nStandard commands");
406 for (fp = functions; fp->name != NULL; fp++) {
408 if (((i++) % COLUMNS) == 0) {
409 BIO_printf(bio_err, "\n");
412 if (fp->type != tp) {
415 BIO_printf(bio_err, "\n");
419 "\nMessage Digest commands (see the `dgst' command for more details)\n");
420 } else if (tp == FT_cipher) {
423 "\nCipher commands (see the `enc' command for more details)\n");
426 BIO_printf(bio_err, FORMAT, fp->name);
428 BIO_printf(bio_err, "\n\n");
432 int exit_main(int argc, char **argv)
434 return EXIT_THE_PROGRAM;
437 static void list_type(FUNC_TYPE ft)
442 for (fp = functions; fp->name != NULL; fp++)
443 if (fp->type == ft) {
444 if ((i++ % COLUMNS) == 0)
445 BIO_printf(bio_out, "\n");
446 BIO_printf(bio_out, FORMAT, fp->name);
448 BIO_printf(bio_out, "\n");
451 static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[])
455 if (argc <= 0 || argv[0] == NULL)
458 fp = lh_FUNCTION_retrieve(prog, &f);
460 if (EVP_get_digestbyname(argv[0])) {
464 } else if (EVP_get_cipherbyname(argv[0])) {
471 return (fp->func(argc, argv));
473 if ((strncmp(argv[0], "no-", 3)) == 0) {
475 * User is asking if foo is unsupported, by trying to "run" the
476 * no-foo command. Strange.
478 f.name = argv[0] + 3;
479 if (lh_FUNCTION_retrieve(prog, &f) == NULL) {
480 BIO_printf(bio_out, "%s\n", argv[0]);
483 BIO_printf(bio_out, "%s\n", argv[0] + 3);
486 if (strcmp(argv[0], "quit") == 0 || strcmp(argv[0], "q") == 0 ||
487 strcmp(argv[0], "exit") == 0 || strcmp(argv[0], "bye") == 0)
488 /* Special value to mean "exit the program. */
489 return EXIT_THE_PROGRAM;
491 BIO_printf(bio_err, "Invalid command '%s'; type \"help\" for a list.\n",
496 static void list_pkey(void)
500 for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
501 const EVP_PKEY_ASN1_METHOD *ameth;
502 int pkey_id, pkey_base_id, pkey_flags;
503 const char *pinfo, *pem_str;
504 ameth = EVP_PKEY_asn1_get0(i);
505 EVP_PKEY_asn1_get0_info(&pkey_id, &pkey_base_id, &pkey_flags,
506 &pinfo, &pem_str, ameth);
507 if (pkey_flags & ASN1_PKEY_ALIAS) {
508 BIO_printf(bio_out, "Name: %s\n", OBJ_nid2ln(pkey_id));
509 BIO_printf(bio_out, "\tAlias for: %s\n",
510 OBJ_nid2ln(pkey_base_id));
512 BIO_printf(bio_out, "Name: %s\n", pinfo);
513 BIO_printf(bio_out, "\tType: %s Algorithm\n",
514 pkey_flags & ASN1_PKEY_DYNAMIC ?
515 "External" : "Builtin");
516 BIO_printf(bio_out, "\tOID: %s\n", OBJ_nid2ln(pkey_id));
519 BIO_printf(bio_out, "\tPEM string: %s\n", pem_str);
525 static int function_cmp(const FUNCTION * a, const FUNCTION * b)
527 return strncmp(a->name, b->name, 8);
530 static unsigned long function_hash(const FUNCTION * a)
532 return OPENSSL_LH_strhash(a->name);
535 static int SortFnByName(const void *_f1, const void *_f2)
537 const FUNCTION *f1 = _f1;
538 const FUNCTION *f2 = _f2;
540 if (f1->type != f2->type)
541 return f1->type - f2->type;
542 return strcmp(f1->name, f2->name);
545 static void list_disabled(void)
547 BIO_puts(bio_out, "Disabled algorithms:\n");
549 BIO_puts(bio_out, "BF\n");
551 #ifndef OPENSSL_NO_BLAKE2
552 BIO_puts(bio_out, "BLAKE2\n");
554 #ifdef OPENSSL_NO_CAMELLIA
555 BIO_puts(bio_out, "CAMELLIA\n");
557 #ifdef OPENSSL_NO_CAST
558 BIO_puts(bio_out, "CAST\n");
560 #ifdef OPENSSL_NO_CMAC
561 BIO_puts(bio_out, "CMAC\n");
563 #ifdef OPENSSL_NO_CMS
564 BIO_puts(bio_out, "CMS\n");
566 #ifdef OPENSSL_NO_COMP
567 BIO_puts(bio_out, "COMP\n");
569 #ifdef OPENSSL_NO_DES
570 BIO_puts(bio_out, "DES\n");
572 #ifdef OPENSSL_NO_DGRAM
573 BIO_puts(bio_out, "DGRAM\n");
576 BIO_puts(bio_out, "DH\n");
578 #ifdef OPENSSL_NO_DSA
579 BIO_puts(bio_out, "DSA\n");
581 #if defined(OPENSSL_NO_DTLS)
582 BIO_puts(bio_out, "DTLS\n");
584 #if defined(OPENSSL_NO_DTLS1)
585 BIO_puts(bio_out, "DTLS1\n");
587 #if defined(OPENSSL_NO_DTLS1_2)
588 BIO_puts(bio_out, "DTLS1_2\n");
591 BIO_puts(bio_out, "EC\n");
593 #ifdef OPENSSL_NO_EC2M
594 BIO_puts(bio_out, "EC2M\n");
596 #ifdef OPENSSL_NO_ENGINE
597 BIO_puts(bio_out, "ENGINE\n");
599 #ifdef OPENSSL_NO_GOST
600 BIO_puts(bio_out, "GOST\n");
602 #ifdef OPENSSL_NO_HEARTBEATS
603 BIO_puts(bio_out, "HEARTBEATS\n");
605 #ifdef OPENSSL_NO_IDEA
606 BIO_puts(bio_out, "IDEA\n");
608 #ifdef OPENSSL_NO_MD2
609 BIO_puts(bio_out, "MD2\n");
611 #ifdef OPENSSL_NO_MD4
612 BIO_puts(bio_out, "MD4\n");
614 #ifdef OPENSSL_NO_MD5
615 BIO_puts(bio_out, "MD5\n");
617 #ifdef OPENSSL_NO_MDC2
618 BIO_puts(bio_out, "MDC2\n");
620 #ifdef OPENSSL_NO_OCB
621 BIO_puts(bio_out, "OCB\n");
623 #ifdef OPENSSL_NO_OCSP
624 BIO_puts(bio_out, "OCSP\n");
626 #ifdef OPENSSL_NO_PSK
627 BIO_puts(bio_out, "PSK\n");
629 #ifdef OPENSSL_NO_RC2
630 BIO_puts(bio_out, "RC2\n");
632 #ifdef OPENSSL_NO_RC4
633 BIO_puts(bio_out, "RC4\n");
635 #ifdef OPENSSL_NO_RC5
636 BIO_puts(bio_out, "RC5\n");
638 #ifdef OPENSSL_NO_RMD160
639 BIO_puts(bio_out, "RMD160\n");
641 #ifdef OPENSSL_NO_RSA
642 BIO_puts(bio_out, "RSA\n");
644 #ifdef OPENSSL_NO_SCRYPT
645 BIO_puts(bio_out, "SCRYPT\n");
647 #ifdef OPENSSL_NO_SCTP
648 BIO_puts(bio_out, "SCTP\n");
650 #ifdef OPENSSL_NO_SEED
651 BIO_puts(bio_out, "SEED\n");
653 #ifdef OPENSSL_NO_SOCK
654 BIO_puts(bio_out, "SOCK\n");
656 #ifdef OPENSSL_NO_SRP
657 BIO_puts(bio_out, "SRP\n");
659 #ifdef OPENSSL_NO_SRTP
660 BIO_puts(bio_out, "SRTP\n");
662 #ifdef OPENSSL_NO_SSL3
663 BIO_puts(bio_out, "SSL3\n");
665 #ifdef OPENSSL_NO_TLS1
666 BIO_puts(bio_out, "TLS1\n");
668 #ifdef OPENSSL_NO_TLS1_1
669 BIO_puts(bio_out, "TLS1_1\n");
671 #ifdef OPENSSL_NO_TLS1_2
672 BIO_puts(bio_out, "TLS1_2\n");
674 #ifdef OPENSSL_NO_WHIRLPOOL
675 BIO_puts(bio_out, "WHIRLPOOL\n");
678 BIO_puts(bio_out, "ZLIB\n");
682 static LHASH_OF(FUNCTION) *prog_init(void)
684 LHASH_OF(FUNCTION) *ret;
688 /* Sort alphabetically within category. For nicer help displays. */
689 for (i = 0, f = functions; f->name != NULL; ++f, ++i) ;
690 qsort(functions, i, sizeof(*functions), SortFnByName);
692 if ((ret = lh_FUNCTION_new(function_hash, function_cmp)) == NULL)
695 for (f = functions; f->name != NULL; f++)
696 (void)lh_FUNCTION_insert(ret, f);