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);
136 p = getenv("OPENSSL_DEBUG_MEMORY");
137 if (p != NULL && strcmp(p, "on") == 0)
138 CRYPTO_set_mem_debug(1);
139 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
141 if (getenv("OPENSSL_FIPS")) {
143 if (!FIPS_mode_set(1)) {
144 ERR_print_errors(bio_err);
148 BIO_printf(bio_err, "FIPS mode not supported.\n");
157 pname = opt_progname(argv[0]);
159 /* first check the program name */
161 fp = lh_FUNCTION_retrieve(prog, &f);
164 ret = fp->func(argc, argv);
168 /* If there is stuff on the command line, run with that. */
172 ret = do_cmd(prog, argc, argv);
178 /* ok, lets enter interactive mode */
181 /* Read a line, continue reading if line ends with \ */
182 for (p = buf, n = sizeof buf, i = 0, first = 1; n > 0; first = 0) {
183 prompt = first ? "OpenSSL> " : "> ";
186 fputs(prompt, stdout);
188 if (!fgets(p, n, stdin))
195 if (p[i - 2] != '\\')
202 extern char *readline(const char *);
203 extern void add_history(const char *cp);
206 text = readline(prompt);
212 if (text[i - 1] != '\\') {
213 p += strlen(strcpy(p, text));
220 p += strlen(strcpy(p, text));
227 if (!chopup_args(&arg, buf)) {
228 BIO_printf(bio_err, "Can't parse (no memory?)\n");
232 ret = do_cmd(prog, arg.argc, arg.argv);
233 if (ret == EXIT_THE_PROGRAM) {
238 BIO_printf(bio_err, "error in %s\n", arg.argv[0]);
239 (void)BIO_flush(bio_out);
240 (void)BIO_flush(bio_err);
244 OPENSSL_free(copied_argv);
245 OPENSSL_free(default_config_file);
248 lh_FUNCTION_free(prog);
249 OPENSSL_free(arg.argv);
252 BIO_free_all(bio_out);
254 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
255 if (CRYPTO_mem_leaks(bio_err) <= 0)
262 OPTIONS exit_options[] = {
266 static void list_cipher_fn(const EVP_CIPHER *c,
267 const char *from, const char *to, void *arg)
270 BIO_printf(arg, "%s\n", EVP_CIPHER_name(c));
273 from = "<undefined>";
276 BIO_printf(arg, "%s => %s\n", from, to);
280 static void list_md_fn(const EVP_MD *m,
281 const char *from, const char *to, void *arg)
284 BIO_printf(arg, "%s\n", EVP_MD_name(m));
287 from = "<undefined>";
290 BIO_printf((BIO *)arg, "%s => %s\n", from, to);
294 /* Unified enum for help and list commands. */
295 typedef enum HELPLIST_CHOICE {
296 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
297 OPT_COMMANDS, OPT_DIGEST_COMMANDS,
298 OPT_DIGEST_ALGORITHMS, OPT_CIPHER_COMMANDS, OPT_CIPHER_ALGORITHMS,
299 OPT_PK_ALGORITHMS, OPT_DISABLED
302 OPTIONS list_options[] = {
303 {"help", OPT_HELP, '-', "Display this summary"},
304 {"commands", OPT_COMMANDS, '-', "List of standard commands"},
305 {"digest-commands", OPT_DIGEST_COMMANDS, '-',
306 "List of message digest commands"},
307 {"digest-algorithms", OPT_DIGEST_ALGORITHMS, '-',
308 "List of message digest algorithms"},
309 {"cipher-commands", OPT_CIPHER_COMMANDS, '-', "List of cipher commands"},
310 {"cipher-algorithms", OPT_CIPHER_ALGORITHMS, '-',
311 "List of cipher algorithms"},
312 {"public-key-algorithms", OPT_PK_ALGORITHMS, '-',
313 "List of public key algorithms"},
314 {"disabled", OPT_DISABLED, '-',
315 "List of disabled features"},
319 int list_main(int argc, char **argv)
325 prog = opt_init(argc, argv, list_options);
326 while ((o = opt_next()) != OPT_EOF) {
328 case OPT_EOF: /* Never hit, but suppresses warning */
330 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
333 opt_help(list_options);
336 list_type(FT_general);
338 case OPT_DIGEST_COMMANDS:
341 case OPT_DIGEST_ALGORITHMS:
342 EVP_MD_do_all_sorted(list_md_fn, bio_out);
344 case OPT_CIPHER_COMMANDS:
345 list_type(FT_cipher);
347 case OPT_CIPHER_ALGORITHMS:
348 EVP_CIPHER_do_all_sorted(list_cipher_fn, bio_out);
350 case OPT_PK_ALGORITHMS:
361 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
368 OPTIONS help_options[] = {
369 {"help", OPT_HELP, '-', "Display this summary"},
373 int help_main(int argc, char **argv)
381 prog = opt_init(argc, argv, help_options);
382 while ((o = opt_next()) != OPT_EOF) {
385 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
388 opt_help(help_options);
393 if (opt_num_rest() != 0) {
394 BIO_printf(bio_err, "Usage: %s\n", prog);
398 BIO_printf(bio_err, "\nStandard commands");
401 for (fp = functions; fp->name != NULL; fp++) {
403 if (((i++) % COLUMNS) == 0) {
404 BIO_printf(bio_err, "\n");
407 if (fp->type != tp) {
410 BIO_printf(bio_err, "\n");
414 "\nMessage Digest commands (see the `dgst' command for more details)\n");
415 } else if (tp == FT_cipher) {
418 "\nCipher commands (see the `enc' command for more details)\n");
421 BIO_printf(bio_err, FORMAT, fp->name);
423 BIO_printf(bio_err, "\n\n");
427 int exit_main(int argc, char **argv)
429 return EXIT_THE_PROGRAM;
432 static void list_type(FUNC_TYPE ft)
437 for (fp = functions; fp->name != NULL; fp++)
438 if (fp->type == ft) {
439 if ((i++ % COLUMNS) == 0)
440 BIO_printf(bio_out, "\n");
441 BIO_printf(bio_out, FORMAT, fp->name);
443 BIO_printf(bio_out, "\n");
446 static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[])
450 if (argc <= 0 || argv[0] == NULL)
453 fp = lh_FUNCTION_retrieve(prog, &f);
455 if (EVP_get_digestbyname(argv[0])) {
459 } else if (EVP_get_cipherbyname(argv[0])) {
466 return (fp->func(argc, argv));
468 if ((strncmp(argv[0], "no-", 3)) == 0) {
470 * User is asking if foo is unsupported, by trying to "run" the
471 * no-foo command. Strange.
473 f.name = argv[0] + 3;
474 if (lh_FUNCTION_retrieve(prog, &f) == NULL) {
475 BIO_printf(bio_out, "%s\n", argv[0]);
478 BIO_printf(bio_out, "%s\n", argv[0] + 3);
481 if (strcmp(argv[0], "quit") == 0 || strcmp(argv[0], "q") == 0 ||
482 strcmp(argv[0], "exit") == 0 || strcmp(argv[0], "bye") == 0)
483 /* Special value to mean "exit the program. */
484 return EXIT_THE_PROGRAM;
486 BIO_printf(bio_err, "Invalid command '%s'; type \"help\" for a list.\n",
491 static void list_pkey(void)
495 for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
496 const EVP_PKEY_ASN1_METHOD *ameth;
497 int pkey_id, pkey_base_id, pkey_flags;
498 const char *pinfo, *pem_str;
499 ameth = EVP_PKEY_asn1_get0(i);
500 EVP_PKEY_asn1_get0_info(&pkey_id, &pkey_base_id, &pkey_flags,
501 &pinfo, &pem_str, ameth);
502 if (pkey_flags & ASN1_PKEY_ALIAS) {
503 BIO_printf(bio_out, "Name: %s\n", OBJ_nid2ln(pkey_id));
504 BIO_printf(bio_out, "\tAlias for: %s\n",
505 OBJ_nid2ln(pkey_base_id));
507 BIO_printf(bio_out, "Name: %s\n", pinfo);
508 BIO_printf(bio_out, "\tType: %s Algorithm\n",
509 pkey_flags & ASN1_PKEY_DYNAMIC ?
510 "External" : "Builtin");
511 BIO_printf(bio_out, "\tOID: %s\n", OBJ_nid2ln(pkey_id));
514 BIO_printf(bio_out, "\tPEM string: %s\n", pem_str);
520 static int function_cmp(const FUNCTION * a, const FUNCTION * b)
522 return strncmp(a->name, b->name, 8);
525 static unsigned long function_hash(const FUNCTION * a)
527 return OPENSSL_LH_strhash(a->name);
530 static int SortFnByName(const void *_f1, const void *_f2)
532 const FUNCTION *f1 = _f1;
533 const FUNCTION *f2 = _f2;
535 if (f1->type != f2->type)
536 return f1->type - f2->type;
537 return strcmp(f1->name, f2->name);
540 static void list_disabled(void)
542 BIO_puts(bio_out, "Disabled algorithms:\n");
544 BIO_puts(bio_out, "BF\n");
546 #ifndef OPENSSL_NO_BLAKE2
547 BIO_puts(bio_out, "BLAKE2\n");
549 #ifdef OPENSSL_NO_CAMELLIA
550 BIO_puts(bio_out, "CAMELLIA\n");
552 #ifdef OPENSSL_NO_CAST
553 BIO_puts(bio_out, "CAST\n");
555 #ifdef OPENSSL_NO_CMAC
556 BIO_puts(bio_out, "CMAC\n");
558 #ifdef OPENSSL_NO_CMS
559 BIO_puts(bio_out, "CMS\n");
561 #ifdef OPENSSL_NO_COMP
562 BIO_puts(bio_out, "COMP\n");
564 #ifdef OPENSSL_NO_DES
565 BIO_puts(bio_out, "DES\n");
567 #ifdef OPENSSL_NO_DGRAM
568 BIO_puts(bio_out, "DGRAM\n");
571 BIO_puts(bio_out, "DH\n");
573 #ifdef OPENSSL_NO_DSA
574 BIO_puts(bio_out, "DSA\n");
576 #if defined(OPENSSL_NO_DTLS)
577 BIO_puts(bio_out, "DTLS\n");
579 #if defined(OPENSSL_NO_DTLS1)
580 BIO_puts(bio_out, "DTLS1\n");
582 #if defined(OPENSSL_NO_DTLS1_2)
583 BIO_puts(bio_out, "DTLS1_2\n");
586 BIO_puts(bio_out, "EC\n");
588 #ifdef OPENSSL_NO_EC2M
589 BIO_puts(bio_out, "EC2M\n");
591 #ifdef OPENSSL_NO_ENGINE
592 BIO_puts(bio_out, "ENGINE\n");
594 #ifdef OPENSSL_NO_GOST
595 BIO_puts(bio_out, "GOST\n");
597 #ifdef OPENSSL_NO_HEARTBEATS
598 BIO_puts(bio_out, "HEARTBEATS\n");
600 #ifdef OPENSSL_NO_IDEA
601 BIO_puts(bio_out, "IDEA\n");
603 #ifdef OPENSSL_NO_MD2
604 BIO_puts(bio_out, "MD2\n");
606 #ifdef OPENSSL_NO_MD4
607 BIO_puts(bio_out, "MD4\n");
609 #ifdef OPENSSL_NO_MD5
610 BIO_puts(bio_out, "MD5\n");
612 #ifdef OPENSSL_NO_MDC2
613 BIO_puts(bio_out, "MDC2\n");
615 #ifdef OPENSSL_NO_OCB
616 BIO_puts(bio_out, "OCB\n");
618 #ifdef OPENSSL_NO_OCSP
619 BIO_puts(bio_out, "OCSP\n");
621 #ifdef OPENSSL_NO_PSK
622 BIO_puts(bio_out, "PSK\n");
624 #ifdef OPENSSL_NO_RC2
625 BIO_puts(bio_out, "RC2\n");
627 #ifdef OPENSSL_NO_RC4
628 BIO_puts(bio_out, "RC4\n");
630 #ifdef OPENSSL_NO_RC5
631 BIO_puts(bio_out, "RC5\n");
633 #ifdef OPENSSL_NO_RMD160
634 BIO_puts(bio_out, "RMD160\n");
636 #ifdef OPENSSL_NO_RSA
637 BIO_puts(bio_out, "RSA\n");
639 #ifdef OPENSSL_NO_SCRYPT
640 BIO_puts(bio_out, "SCRYPT\n");
642 #ifdef OPENSSL_NO_SCTP
643 BIO_puts(bio_out, "SCTP\n");
645 #ifdef OPENSSL_NO_SEED
646 BIO_puts(bio_out, "SEED\n");
648 #ifdef OPENSSL_NO_SOCK
649 BIO_puts(bio_out, "SOCK\n");
651 #ifdef OPENSSL_NO_SRP
652 BIO_puts(bio_out, "SRP\n");
654 #ifdef OPENSSL_NO_SRTP
655 BIO_puts(bio_out, "SRTP\n");
657 #ifdef OPENSSL_NO_SSL3
658 BIO_puts(bio_out, "SSL3\n");
660 #ifdef OPENSSL_NO_TLS1
661 BIO_puts(bio_out, "TLS1\n");
663 #ifdef OPENSSL_NO_TLS1_1
664 BIO_puts(bio_out, "TLS1_1\n");
666 #ifdef OPENSSL_NO_TLS1_2
667 BIO_puts(bio_out, "TLS1_2\n");
669 #ifdef OPENSSL_NO_WHIRLPOOL
670 BIO_puts(bio_out, "WHIRLPOOL\n");
673 BIO_puts(bio_out, "ZLIB\n");
677 static LHASH_OF(FUNCTION) *prog_init(void)
679 LHASH_OF(FUNCTION) *ret;
683 /* Sort alphabetically within category. For nicer help displays. */
684 for (i = 0, f = functions; f->name != NULL; ++f, ++i) ;
685 qsort(functions, i, sizeof(*functions), SortFnByName);
687 if ((ret = lh_FUNCTION_new(function_hash, function_cmp)) == NULL)
690 for (f = functions; f->name != NULL; f++)
691 (void)lh_FUNCTION_insert(ret, f);