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/rand.h>
16 #include <openssl/lhash.h>
17 #include <openssl/conf.h>
18 #include <openssl/x509.h>
19 #include <openssl/pem.h>
20 #include <openssl/ssl.h>
21 #ifndef OPENSSL_NO_ENGINE
22 # include <openssl/engine.h>
24 #include <openssl/err.h>
26 # include <openssl/fips.h>
28 #define USE_SOCKETS /* needed for the _O_BINARY defs in the MS world */
30 /* Needed to get the other O_xxx flags. */
31 #ifdef OPENSSL_SYS_VMS
34 #define INCLUDE_FUNCTION_TABLE
38 #ifdef OPENSSL_NO_CAMELLIA
39 # define FORMAT "%-15s"
42 # define FORMAT "%-18s"
46 /* Special sentinel to exit the program. */
47 #define EXIT_THE_PROGRAM (-1)
50 * The LHASH callbacks ("hash" & "cmp") have been replaced by functions with
51 * the base prototypes (we cast each variable inside the function to the
52 * required type of "FUNCTION*"). This removes the necessity for
53 * macro-generated wrapper functions.
55 static LHASH_OF(FUNCTION) *prog_init(void);
56 static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[]);
57 static void list_pkey(void);
58 static void list_type(FUNC_TYPE ft);
59 static void list_disabled(void);
60 char *default_config_file = NULL;
62 static CONF *config = NULL;
67 static int apps_startup()
70 signal(SIGPIPE, SIG_IGN);
73 /* Set non-default library initialisation settings */
74 if (!OPENSSL_init_crypto(OPENSSL_INIT_ENGINE_ALL_BUILTIN
75 | OPENSSL_INIT_LOAD_CONFIG, NULL))
85 static void apps_shutdown()
92 static char *make_config_name()
98 if ((t = getenv("OPENSSL_CONF")) != NULL)
99 return OPENSSL_strdup(t);
101 t = X509_get_default_cert_area();
102 len = strlen(t) + 1 + strlen(OPENSSL_CONF) + 1;
103 p = app_malloc(len, "config filename buffer");
105 #ifndef OPENSSL_SYS_VMS
108 strcat(p, OPENSSL_CONF);
113 int main(int argc, char *argv[])
116 LHASH_OF(FUNCTION) *prog = NULL;
117 char **copied_argv = NULL;
122 int first, n, i, ret = 0;
127 /* Set up some of the environment. */
128 default_config_file = make_config_name();
129 bio_in = dup_bio_in(FORMAT_TEXT);
130 bio_out = dup_bio_out(FORMAT_TEXT);
131 bio_err = dup_bio_err(FORMAT_TEXT);
133 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
134 copied_argv = argv = copy_argv(&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")) {
144 if (!FIPS_mode_set(1)) {
145 ERR_print_errors(bio_err);
149 BIO_printf(bio_err, "FIPS mode not supported.\n");
158 pname = opt_progname(argv[0]);
160 /* first check the program name */
162 fp = lh_FUNCTION_retrieve(prog, &f);
165 ret = fp->func(argc, argv);
169 /* If there is stuff on the command line, run with that. */
173 ret = do_cmd(prog, argc, argv);
179 /* ok, lets enter interactive mode */
182 /* Read a line, continue reading if line ends with \ */
183 for (p = buf, n = sizeof buf, i = 0, first = 1; n > 0; first = 0) {
184 prompt = first ? "OpenSSL> " : "> ";
187 fputs(prompt, stdout);
189 if (!fgets(p, n, stdin))
196 if (p[i - 2] != '\\')
203 extern char *readline(const char *);
204 extern void add_history(const char *cp);
207 text = readline(prompt);
213 if (text[i - 1] != '\\') {
214 p += strlen(strcpy(p, text));
221 p += strlen(strcpy(p, text));
228 if (!chopup_args(&arg, buf)) {
229 BIO_printf(bio_err, "Can't parse (no memory?)\n");
233 ret = do_cmd(prog, arg.argc, arg.argv);
234 if (ret == EXIT_THE_PROGRAM) {
239 BIO_printf(bio_err, "error in %s\n", arg.argv[0]);
240 (void)BIO_flush(bio_out);
241 (void)BIO_flush(bio_err);
245 OPENSSL_free(copied_argv);
246 OPENSSL_free(default_config_file);
249 lh_FUNCTION_free(prog);
250 OPENSSL_free(arg.argv);
253 BIO_free_all(bio_out);
255 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
256 if (CRYPTO_mem_leaks(bio_err) <= 0)
263 OPTIONS exit_options[] = {
267 static void list_cipher_fn(const EVP_CIPHER *c,
268 const char *from, const char *to, void *arg)
271 BIO_printf(arg, "%s\n", EVP_CIPHER_name(c));
274 from = "<undefined>";
277 BIO_printf(arg, "%s => %s\n", from, to);
281 static void list_md_fn(const EVP_MD *m,
282 const char *from, const char *to, void *arg)
285 BIO_printf(arg, "%s\n", EVP_MD_name(m));
288 from = "<undefined>";
291 BIO_printf((BIO *)arg, "%s => %s\n", from, to);
295 /* Unified enum for help and list commands. */
296 typedef enum HELPLIST_CHOICE {
297 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
298 OPT_COMMANDS, OPT_DIGEST_COMMANDS,
299 OPT_DIGEST_ALGORITHMS, OPT_CIPHER_COMMANDS, OPT_CIPHER_ALGORITHMS,
300 OPT_PK_ALGORITHMS, OPT_DISABLED
303 OPTIONS list_options[] = {
304 {"help", OPT_HELP, '-', "Display this summary"},
305 {"commands", OPT_COMMANDS, '-', "List of standard commands"},
306 {"digest-commands", OPT_DIGEST_COMMANDS, '-',
307 "List of message digest commands"},
308 {"digest-algorithms", OPT_DIGEST_ALGORITHMS, '-',
309 "List of message digest algorithms"},
310 {"cipher-commands", OPT_CIPHER_COMMANDS, '-', "List of cipher commands"},
311 {"cipher-algorithms", OPT_CIPHER_ALGORITHMS, '-',
312 "List of cipher algorithms"},
313 {"public-key-algorithms", OPT_PK_ALGORITHMS, '-',
314 "List of public key algorithms"},
315 {"disabled", OPT_DISABLED, '-',
316 "List of disabled features"},
320 int list_main(int argc, char **argv)
326 prog = opt_init(argc, argv, list_options);
327 while ((o = opt_next()) != OPT_EOF) {
329 case OPT_EOF: /* Never hit, but suppresses warning */
331 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
334 opt_help(list_options);
337 list_type(FT_general);
339 case OPT_DIGEST_COMMANDS:
342 case OPT_DIGEST_ALGORITHMS:
343 EVP_MD_do_all_sorted(list_md_fn, bio_out);
345 case OPT_CIPHER_COMMANDS:
346 list_type(FT_cipher);
348 case OPT_CIPHER_ALGORITHMS:
349 EVP_CIPHER_do_all_sorted(list_cipher_fn, bio_out);
351 case OPT_PK_ALGORITHMS:
362 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
369 OPTIONS help_options[] = {
370 {"help", OPT_HELP, '-', "Display this summary"},
374 int help_main(int argc, char **argv)
382 prog = opt_init(argc, argv, help_options);
383 while ((o = opt_next()) != OPT_EOF) {
386 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
389 opt_help(help_options);
394 if (opt_num_rest() != 0) {
395 BIO_printf(bio_err, "Usage: %s\n", prog);
399 BIO_printf(bio_err, "\nStandard commands");
402 for (fp = functions; fp->name != NULL; fp++) {
404 if (((i++) % COLUMNS) == 0) {
405 BIO_printf(bio_err, "\n");
408 if (fp->type != tp) {
411 BIO_printf(bio_err, "\n");
415 "\nMessage Digest commands (see the `dgst' command for more details)\n");
416 } else if (tp == FT_cipher) {
419 "\nCipher commands (see the `enc' command for more details)\n");
422 BIO_printf(bio_err, FORMAT, fp->name);
424 BIO_printf(bio_err, "\n\n");
428 int exit_main(int argc, char **argv)
430 return EXIT_THE_PROGRAM;
433 static void list_type(FUNC_TYPE ft)
438 for (fp = functions; fp->name != NULL; fp++)
439 if (fp->type == ft) {
440 if ((i++ % COLUMNS) == 0)
441 BIO_printf(bio_out, "\n");
442 BIO_printf(bio_out, FORMAT, fp->name);
444 BIO_printf(bio_out, "\n");
447 static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[])
451 if (argc <= 0 || argv[0] == NULL)
454 fp = lh_FUNCTION_retrieve(prog, &f);
456 if (EVP_get_digestbyname(argv[0])) {
460 } else if (EVP_get_cipherbyname(argv[0])) {
467 return (fp->func(argc, argv));
469 if ((strncmp(argv[0], "no-", 3)) == 0) {
471 * User is asking if foo is unsupported, by trying to "run" the
472 * no-foo command. Strange.
474 f.name = argv[0] + 3;
475 if (lh_FUNCTION_retrieve(prog, &f) == NULL) {
476 BIO_printf(bio_out, "%s\n", argv[0]);
479 BIO_printf(bio_out, "%s\n", argv[0] + 3);
482 if (strcmp(argv[0], "quit") == 0 || strcmp(argv[0], "q") == 0 ||
483 strcmp(argv[0], "exit") == 0 || strcmp(argv[0], "bye") == 0)
484 /* Special value to mean "exit the program. */
485 return EXIT_THE_PROGRAM;
487 BIO_printf(bio_err, "Invalid command '%s'; type \"help\" for a list.\n",
492 static void list_pkey(void)
496 for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
497 const EVP_PKEY_ASN1_METHOD *ameth;
498 int pkey_id, pkey_base_id, pkey_flags;
499 const char *pinfo, *pem_str;
500 ameth = EVP_PKEY_asn1_get0(i);
501 EVP_PKEY_asn1_get0_info(&pkey_id, &pkey_base_id, &pkey_flags,
502 &pinfo, &pem_str, ameth);
503 if (pkey_flags & ASN1_PKEY_ALIAS) {
504 BIO_printf(bio_out, "Name: %s\n", OBJ_nid2ln(pkey_id));
505 BIO_printf(bio_out, "\tAlias for: %s\n",
506 OBJ_nid2ln(pkey_base_id));
508 BIO_printf(bio_out, "Name: %s\n", pinfo);
509 BIO_printf(bio_out, "\tType: %s Algorithm\n",
510 pkey_flags & ASN1_PKEY_DYNAMIC ?
511 "External" : "Builtin");
512 BIO_printf(bio_out, "\tOID: %s\n", OBJ_nid2ln(pkey_id));
515 BIO_printf(bio_out, "\tPEM string: %s\n", pem_str);
521 static int function_cmp(const FUNCTION * a, const FUNCTION * b)
523 return strncmp(a->name, b->name, 8);
526 static unsigned long function_hash(const FUNCTION * a)
528 return OPENSSL_LH_strhash(a->name);
531 static int SortFnByName(const void *_f1, const void *_f2)
533 const FUNCTION *f1 = _f1;
534 const FUNCTION *f2 = _f2;
536 if (f1->type != f2->type)
537 return f1->type - f2->type;
538 return strcmp(f1->name, f2->name);
541 static void list_disabled(void)
543 BIO_puts(bio_out, "Disabled algorithms:\n");
545 BIO_puts(bio_out, "BF\n");
547 #ifndef OPENSSL_NO_BLAKE2
548 BIO_puts(bio_out, "BLAKE2\n");
550 #ifdef OPENSSL_NO_CAMELLIA
551 BIO_puts(bio_out, "CAMELLIA\n");
553 #ifdef OPENSSL_NO_CAST
554 BIO_puts(bio_out, "CAST\n");
556 #ifdef OPENSSL_NO_CMAC
557 BIO_puts(bio_out, "CMAC\n");
559 #ifdef OPENSSL_NO_CMS
560 BIO_puts(bio_out, "CMS\n");
562 #ifdef OPENSSL_NO_COMP
563 BIO_puts(bio_out, "COMP\n");
565 #ifdef OPENSSL_NO_DES
566 BIO_puts(bio_out, "DES\n");
568 #ifdef OPENSSL_NO_DGRAM
569 BIO_puts(bio_out, "DGRAM\n");
572 BIO_puts(bio_out, "DH\n");
574 #ifdef OPENSSL_NO_DSA
575 BIO_puts(bio_out, "DSA\n");
577 #if defined(OPENSSL_NO_DTLS)
578 BIO_puts(bio_out, "DTLS\n");
580 #if defined(OPENSSL_NO_DTLS1)
581 BIO_puts(bio_out, "DTLS1\n");
583 #if defined(OPENSSL_NO_DTLS1_2)
584 BIO_puts(bio_out, "DTLS1_2\n");
587 BIO_puts(bio_out, "EC\n");
589 #ifdef OPENSSL_NO_EC2M
590 BIO_puts(bio_out, "EC2M\n");
592 #ifdef OPENSSL_NO_ENGINE
593 BIO_puts(bio_out, "ENGINE\n");
595 #ifdef OPENSSL_NO_GOST
596 BIO_puts(bio_out, "GOST\n");
598 #ifdef OPENSSL_NO_HEARTBEATS
599 BIO_puts(bio_out, "HEARTBEATS\n");
601 #ifdef OPENSSL_NO_IDEA
602 BIO_puts(bio_out, "IDEA\n");
604 #ifdef OPENSSL_NO_MD2
605 BIO_puts(bio_out, "MD2\n");
607 #ifdef OPENSSL_NO_MD4
608 BIO_puts(bio_out, "MD4\n");
610 #ifdef OPENSSL_NO_MD5
611 BIO_puts(bio_out, "MD5\n");
613 #ifdef OPENSSL_NO_MDC2
614 BIO_puts(bio_out, "MDC2\n");
616 #ifdef OPENSSL_NO_OCB
617 BIO_puts(bio_out, "OCB\n");
619 #ifdef OPENSSL_NO_OCSP
620 BIO_puts(bio_out, "OCSP\n");
622 #ifdef OPENSSL_NO_PSK
623 BIO_puts(bio_out, "PSK\n");
625 #ifdef OPENSSL_NO_RC2
626 BIO_puts(bio_out, "RC2\n");
628 #ifdef OPENSSL_NO_RC4
629 BIO_puts(bio_out, "RC4\n");
631 #ifdef OPENSSL_NO_RC5
632 BIO_puts(bio_out, "RC5\n");
634 #ifdef OPENSSL_NO_RMD160
635 BIO_puts(bio_out, "RMD160\n");
637 #ifdef OPENSSL_NO_RSA
638 BIO_puts(bio_out, "RSA\n");
640 #ifdef OPENSSL_NO_SCRYPT
641 BIO_puts(bio_out, "SCRYPT\n");
643 #ifdef OPENSSL_NO_SCTP
644 BIO_puts(bio_out, "SCTP\n");
646 #ifdef OPENSSL_NO_SEED
647 BIO_puts(bio_out, "SEED\n");
649 #ifdef OPENSSL_NO_SOCK
650 BIO_puts(bio_out, "SOCK\n");
652 #ifdef OPENSSL_NO_SRP
653 BIO_puts(bio_out, "SRP\n");
655 #ifdef OPENSSL_NO_SRTP
656 BIO_puts(bio_out, "SRTP\n");
658 #ifdef OPENSSL_NO_SSL3
659 BIO_puts(bio_out, "SSL3\n");
661 #ifdef OPENSSL_NO_TLS1
662 BIO_puts(bio_out, "TLS1\n");
664 #ifdef OPENSSL_NO_TLS1_1
665 BIO_puts(bio_out, "TLS1_1\n");
667 #ifdef OPENSSL_NO_TLS1_2
668 BIO_puts(bio_out, "TLS1_2\n");
670 #ifdef OPENSSL_NO_WHIRLPOOL
671 BIO_puts(bio_out, "WHIRLPOOL\n");
674 BIO_puts(bio_out, "ZLIB\n");
678 static LHASH_OF(FUNCTION) *prog_init(void)
680 LHASH_OF(FUNCTION) *ret;
684 /* Sort alphabetically within category. For nicer help displays. */
685 for (i = 0, f = functions; f->name != NULL; ++f, ++i) ;
686 qsort(functions, i, sizeof(*functions), SortFnByName);
688 if ((ret = lh_FUNCTION_new(function_hash, function_cmp)) == NULL)
691 for (f = functions; f->name != NULL; f++)
692 (void)lh_FUNCTION_insert(ret, f);