2 * Copyright 1995-2018 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
10 #include <internal/cryptlib.h>
14 #include <openssl/bio.h>
15 #include <openssl/crypto.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 /* Needed to get the other O_xxx flags. */
27 #ifdef OPENSSL_SYS_VMS
31 #define INCLUDE_FUNCTION_TABLE
34 /* Structure to hold the number of columns to be displayed and the
35 * field width used to display them.
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_pkey_meth(void);
55 static void list_type(FUNC_TYPE ft
, int one
);
56 static void list_disabled(void);
57 char *default_config_file
= NULL
;
63 static void calculate_columns(DISPLAY_COLUMNS
*dc
)
68 for (f
= functions
; f
->name
!= NULL
; ++f
)
69 if (f
->type
== FT_general
|| f
->type
== FT_md
|| f
->type
== FT_cipher
)
70 if ((len
= strlen(f
->name
)) > maxlen
)
73 dc
->width
= maxlen
+ 2;
74 dc
->columns
= (80 - 1) / dc
->width
;
77 static int apps_startup()
80 signal(SIGPIPE
, SIG_IGN
);
83 /* Set non-default library initialisation settings */
84 if (!OPENSSL_init_ssl(OPENSSL_INIT_ENGINE_ALL_BUILTIN
85 | OPENSSL_INIT_LOAD_CONFIG
, NULL
))
93 static void apps_shutdown()
98 static char *make_config_name()
104 if ((t
= getenv("OPENSSL_CONF")) != NULL
)
105 return OPENSSL_strdup(t
);
107 t
= X509_get_default_cert_area();
108 len
= strlen(t
) + 1 + strlen(OPENSSL_CONF
) + 1;
109 p
= app_malloc(len
, "config filename buffer");
111 #ifndef OPENSSL_SYS_VMS
114 strcat(p
, OPENSSL_CONF
);
119 int main(int argc
, char *argv
[])
122 LHASH_OF(FUNCTION
) *prog
= NULL
;
123 char **copied_argv
= NULL
;
128 int first
, n
, i
, ret
= 0;
133 /* Set up some of the environment. */
134 default_config_file
= make_config_name();
135 bio_in
= dup_bio_in(FORMAT_TEXT
);
136 bio_out
= dup_bio_out(FORMAT_TEXT
);
137 bio_err
= dup_bio_err(FORMAT_TEXT
);
139 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
140 copied_argv
= argv
= copy_argv(&argc
, argv
);
141 #elif defined(_WIN32)
143 * Replace argv[] with UTF-8 encoded strings.
145 win32_utf8argv(&argc
, &argv
);
148 p
= getenv("OPENSSL_DEBUG_MEMORY");
149 if (p
!= NULL
&& strcmp(p
, "on") == 0)
150 CRYPTO_set_mem_debug(1);
151 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON
);
153 if (getenv("OPENSSL_FIPS")) {
154 BIO_printf(bio_err
, "FIPS mode not supported.\n");
158 if (!apps_startup()) {
160 "FATAL: Startup failure (dev note: apps_startup() failed)\n");
161 ERR_print_errors(bio_err
);
167 pname
= opt_progname(argv
[0]);
169 /* first check the program name */
171 fp
= lh_FUNCTION_retrieve(prog
, &f
);
174 ret
= fp
->func(argc
, argv
);
178 /* If there is stuff on the command line, run with that. */
182 ret
= do_cmd(prog
, argc
, argv
);
188 /* ok, lets enter interactive mode */
191 /* Read a line, continue reading if line ends with \ */
192 for (p
= buf
, n
= sizeof(buf
), i
= 0, first
= 1; n
> 0; first
= 0) {
193 prompt
= first
? "OpenSSL> " : "> ";
196 fputs(prompt
, stdout
);
198 if (!fgets(p
, n
, stdin
))
205 if (p
[i
- 2] != '\\')
212 extern char *readline(const char *);
213 extern void add_history(const char *cp
);
216 text
= readline(prompt
);
222 if (text
[i
- 1] != '\\') {
223 p
+= strlen(strcpy(p
, text
));
230 p
+= strlen(strcpy(p
, text
));
237 if (!chopup_args(&arg
, buf
)) {
238 BIO_printf(bio_err
, "Can't parse (no memory?)\n");
242 ret
= do_cmd(prog
, arg
.argc
, arg
.argv
);
243 if (ret
== EXIT_THE_PROGRAM
) {
248 BIO_printf(bio_err
, "error in %s\n", arg
.argv
[0]);
249 (void)BIO_flush(bio_out
);
250 (void)BIO_flush(bio_err
);
254 OPENSSL_free(copied_argv
);
255 OPENSSL_free(default_config_file
);
256 lh_FUNCTION_free(prog
);
257 OPENSSL_free(arg
.argv
);
261 BIO_free_all(bio_out
);
263 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
264 if (CRYPTO_mem_leaks(bio_err
) <= 0)
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 static void list_missing_help(void)
304 for (fp
= functions
; fp
->name
!= NULL
; fp
++) {
305 if ((o
= fp
->help
) != NULL
) {
306 /* If there is help, list what flags are not documented. */
307 for ( ; o
->name
!= NULL
; o
++) {
308 if (o
->helpstr
== NULL
)
309 BIO_printf(bio_out
, "%s %s\n", fp
->name
, o
->name
);
311 } else if (fp
->func
!= dgst_main
) {
312 /* If not aliased to the dgst command, */
313 BIO_printf(bio_out
, "%s *\n", fp
->name
);
318 static void list_options_for_command(const char *command
)
323 for (fp
= functions
; fp
->name
!= NULL
; fp
++)
324 if (strcmp(fp
->name
, command
) == 0)
326 if (fp
->name
== NULL
) {
327 BIO_printf(bio_err
, "Invalid command '%s'; type \"help\" for a list.\n",
332 if ((o
= fp
->help
) == NULL
)
335 for ( ; o
->name
!= NULL
; o
++) {
336 if (o
->name
== OPT_HELP_STR
337 || o
->name
== OPT_MORE_STR
338 || o
->name
[0] == '\0')
340 BIO_printf(bio_out
, "%s %c\n", o
->name
, o
->valtype
);
345 /* Unified enum for help and list commands. */
346 typedef enum HELPLIST_CHOICE
{
347 OPT_ERR
= -1, OPT_EOF
= 0, OPT_HELP
, OPT_ONE
,
348 OPT_COMMANDS
, OPT_DIGEST_COMMANDS
, OPT_OPTIONS
,
349 OPT_DIGEST_ALGORITHMS
, OPT_CIPHER_COMMANDS
, OPT_CIPHER_ALGORITHMS
,
350 OPT_PK_ALGORITHMS
, OPT_PK_METHOD
, OPT_DISABLED
, OPT_MISSING_HELP
353 const OPTIONS list_options
[] = {
354 {"help", OPT_HELP
, '-', "Display this summary"},
355 {"1", OPT_ONE
, '-', "List in one column"},
356 {"commands", OPT_COMMANDS
, '-', "List of standard commands"},
357 {"digest-commands", OPT_DIGEST_COMMANDS
, '-',
358 "List of message digest commands"},
359 {"digest-algorithms", OPT_DIGEST_ALGORITHMS
, '-',
360 "List of message digest algorithms"},
361 {"cipher-commands", OPT_CIPHER_COMMANDS
, '-', "List of cipher commands"},
362 {"cipher-algorithms", OPT_CIPHER_ALGORITHMS
, '-',
363 "List of cipher algorithms"},
364 {"public-key-algorithms", OPT_PK_ALGORITHMS
, '-',
365 "List of public key algorithms"},
366 {"public-key-methods", OPT_PK_METHOD
, '-',
367 "List of public key methods"},
368 {"disabled", OPT_DISABLED
, '-',
369 "List of disabled features"},
370 {"missing-help", OPT_MISSING_HELP
, '-',
371 "List missing detailed help strings"},
372 {"options", OPT_OPTIONS
, 's',
373 "List options for specified command"},
377 int list_main(int argc
, char **argv
)
381 int one
= 0, done
= 0;
383 prog
= opt_init(argc
, argv
, list_options
);
384 while ((o
= opt_next()) != OPT_EOF
) {
386 case OPT_EOF
: /* Never hit, but suppresses warning */
389 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
392 opt_help(list_options
);
398 list_type(FT_general
, one
);
400 case OPT_DIGEST_COMMANDS
:
401 list_type(FT_md
, one
);
403 case OPT_DIGEST_ALGORITHMS
:
404 EVP_MD_do_all_sorted(list_md_fn
, bio_out
);
406 case OPT_CIPHER_COMMANDS
:
407 list_type(FT_cipher
, one
);
409 case OPT_CIPHER_ALGORITHMS
:
410 EVP_CIPHER_do_all_sorted(list_cipher_fn
, bio_out
);
412 case OPT_PK_ALGORITHMS
:
421 case OPT_MISSING_HELP
:
425 list_options_for_command(opt_arg());
430 if (opt_num_rest() != 0) {
431 BIO_printf(bio_err
, "Extra arguments given.\n");
441 typedef enum HELP_CHOICE
{
442 OPT_hERR
= -1, OPT_hEOF
= 0, OPT_hHELP
445 const OPTIONS help_options
[] = {
446 {OPT_HELP_STR
, 1, '-', "Usage: help [options]\n"},
447 {OPT_HELP_STR
, 1, '-', " help [command]\n"},
448 {"help", OPT_hHELP
, '-', "Display this summary"},
453 int help_main(int argc
, char **argv
)
462 prog
= opt_init(argc
, argv
, help_options
);
463 while ((o
= opt_next()) != OPT_hEOF
) {
467 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
470 opt_help(help_options
);
475 if (opt_num_rest() == 1) {
478 new_argv
[0] = opt_rest()[0];
479 new_argv
[1] = "--help";
481 return do_cmd(prog_init(), 2, new_argv
);
483 if (opt_num_rest() != 0) {
484 BIO_printf(bio_err
, "Usage: %s\n", prog
);
488 calculate_columns(&dc
);
489 BIO_printf(bio_err
, "Standard commands");
492 for (fp
= functions
; fp
->name
!= NULL
; fp
++) {
494 if (i
++ % dc
.columns
== 0) {
495 BIO_printf(bio_err
, "\n");
498 if (fp
->type
!= tp
) {
501 BIO_printf(bio_err
, "\n");
505 "\nMessage Digest commands (see the `dgst' command for more details)\n");
506 } else if (tp
== FT_cipher
) {
509 "\nCipher commands (see the `enc' command for more details)\n");
512 BIO_printf(bio_err
, "%-*s", dc
.width
, fp
->name
);
514 BIO_printf(bio_err
, "\n\n");
518 static void list_type(FUNC_TYPE ft
, int one
)
525 calculate_columns(&dc
);
527 for (fp
= functions
; fp
->name
!= NULL
; fp
++) {
531 BIO_printf(bio_out
, "%s\n", fp
->name
);
533 if (i
% dc
.columns
== 0 && i
> 0)
534 BIO_printf(bio_out
, "\n");
535 BIO_printf(bio_out
, "%-*s", dc
.width
, fp
->name
);
540 BIO_printf(bio_out
, "\n\n");
543 static int do_cmd(LHASH_OF(FUNCTION
) *prog
, int argc
, char *argv
[])
547 if (argc
<= 0 || argv
[0] == NULL
)
550 fp
= lh_FUNCTION_retrieve(prog
, &f
);
552 if (EVP_get_digestbyname(argv
[0])) {
556 } else if (EVP_get_cipherbyname(argv
[0])) {
563 return fp
->func(argc
, argv
);
565 if ((strncmp(argv
[0], "no-", 3)) == 0) {
567 * User is asking if foo is unsupported, by trying to "run" the
568 * no-foo command. Strange.
570 f
.name
= argv
[0] + 3;
571 if (lh_FUNCTION_retrieve(prog
, &f
) == NULL
) {
572 BIO_printf(bio_out
, "%s\n", argv
[0]);
575 BIO_printf(bio_out
, "%s\n", argv
[0] + 3);
578 if (strcmp(argv
[0], "quit") == 0 || strcmp(argv
[0], "q") == 0 ||
579 strcmp(argv
[0], "exit") == 0 || strcmp(argv
[0], "bye") == 0)
580 /* Special value to mean "exit the program. */
581 return EXIT_THE_PROGRAM
;
583 BIO_printf(bio_err
, "Invalid command '%s'; type \"help\" for a list.\n",
588 static void list_pkey(void)
592 for (i
= 0; i
< EVP_PKEY_asn1_get_count(); i
++) {
593 const EVP_PKEY_ASN1_METHOD
*ameth
;
594 int pkey_id
, pkey_base_id
, pkey_flags
;
595 const char *pinfo
, *pem_str
;
596 ameth
= EVP_PKEY_asn1_get0(i
);
597 EVP_PKEY_asn1_get0_info(&pkey_id
, &pkey_base_id
, &pkey_flags
,
598 &pinfo
, &pem_str
, ameth
);
599 if (pkey_flags
& ASN1_PKEY_ALIAS
) {
600 BIO_printf(bio_out
, "Name: %s\n", OBJ_nid2ln(pkey_id
));
601 BIO_printf(bio_out
, "\tAlias for: %s\n",
602 OBJ_nid2ln(pkey_base_id
));
604 BIO_printf(bio_out
, "Name: %s\n", pinfo
);
605 BIO_printf(bio_out
, "\tType: %s Algorithm\n",
606 pkey_flags
& ASN1_PKEY_DYNAMIC
?
607 "External" : "Builtin");
608 BIO_printf(bio_out
, "\tOID: %s\n", OBJ_nid2ln(pkey_id
));
611 BIO_printf(bio_out
, "\tPEM string: %s\n", pem_str
);
617 static void list_pkey_meth(void)
620 size_t meth_count
= EVP_PKEY_meth_get_count();
622 for (i
= 0; i
< meth_count
; i
++) {
623 const EVP_PKEY_METHOD
*pmeth
= EVP_PKEY_meth_get0(i
);
624 int pkey_id
, pkey_flags
;
626 EVP_PKEY_meth_get0_info(&pkey_id
, &pkey_flags
, pmeth
);
627 BIO_printf(bio_out
, "%s\n", OBJ_nid2ln(pkey_id
));
628 BIO_printf(bio_out
, "\tType: %s Algorithm\n",
629 pkey_flags
& ASN1_PKEY_DYNAMIC
? "External" : "Builtin");
633 static int function_cmp(const FUNCTION
* a
, const FUNCTION
* b
)
635 return strncmp(a
->name
, b
->name
, 8);
638 static unsigned long function_hash(const FUNCTION
* a
)
640 return OPENSSL_LH_strhash(a
->name
);
643 static int SortFnByName(const void *_f1
, const void *_f2
)
645 const FUNCTION
*f1
= _f1
;
646 const FUNCTION
*f2
= _f2
;
648 if (f1
->type
!= f2
->type
)
649 return f1
->type
- f2
->type
;
650 return strcmp(f1
->name
, f2
->name
);
653 static void list_disabled(void)
655 BIO_puts(bio_out
, "Disabled algorithms:\n");
656 #ifdef OPENSSL_NO_ARIA
657 BIO_puts(bio_out
, "ARIA\n");
660 BIO_puts(bio_out
, "BF\n");
662 #ifdef OPENSSL_NO_BLAKE2
663 BIO_puts(bio_out
, "BLAKE2\n");
665 #ifdef OPENSSL_NO_CAMELLIA
666 BIO_puts(bio_out
, "CAMELLIA\n");
668 #ifdef OPENSSL_NO_CAST
669 BIO_puts(bio_out
, "CAST\n");
671 #ifdef OPENSSL_NO_CMAC
672 BIO_puts(bio_out
, "CMAC\n");
674 #ifdef OPENSSL_NO_CMS
675 BIO_puts(bio_out
, "CMS\n");
677 #ifdef OPENSSL_NO_COMP
678 BIO_puts(bio_out
, "COMP\n");
680 #ifdef OPENSSL_NO_DES
681 BIO_puts(bio_out
, "DES\n");
683 #ifdef OPENSSL_NO_DGRAM
684 BIO_puts(bio_out
, "DGRAM\n");
687 BIO_puts(bio_out
, "DH\n");
689 #ifdef OPENSSL_NO_DSA
690 BIO_puts(bio_out
, "DSA\n");
692 #if defined(OPENSSL_NO_DTLS)
693 BIO_puts(bio_out
, "DTLS\n");
695 #if defined(OPENSSL_NO_DTLS1)
696 BIO_puts(bio_out
, "DTLS1\n");
698 #if defined(OPENSSL_NO_DTLS1_2)
699 BIO_puts(bio_out
, "DTLS1_2\n");
702 BIO_puts(bio_out
, "EC\n");
704 #ifdef OPENSSL_NO_EC2M
705 BIO_puts(bio_out
, "EC2M\n");
707 #ifdef OPENSSL_NO_ENGINE
708 BIO_puts(bio_out
, "ENGINE\n");
710 #ifdef OPENSSL_NO_GOST
711 BIO_puts(bio_out
, "GOST\n");
713 #ifdef OPENSSL_NO_HEARTBEATS
714 BIO_puts(bio_out
, "HEARTBEATS\n");
716 #ifdef OPENSSL_NO_IDEA
717 BIO_puts(bio_out
, "IDEA\n");
719 #ifdef OPENSSL_NO_MD2
720 BIO_puts(bio_out
, "MD2\n");
722 #ifdef OPENSSL_NO_MD4
723 BIO_puts(bio_out
, "MD4\n");
725 #ifdef OPENSSL_NO_MD5
726 BIO_puts(bio_out
, "MD5\n");
728 #ifdef OPENSSL_NO_MDC2
729 BIO_puts(bio_out
, "MDC2\n");
731 #ifdef OPENSSL_NO_OCB
732 BIO_puts(bio_out
, "OCB\n");
734 #ifdef OPENSSL_NO_OCSP
735 BIO_puts(bio_out
, "OCSP\n");
737 #ifdef OPENSSL_NO_PSK
738 BIO_puts(bio_out
, "PSK\n");
740 #ifdef OPENSSL_NO_RC2
741 BIO_puts(bio_out
, "RC2\n");
743 #ifdef OPENSSL_NO_RC4
744 BIO_puts(bio_out
, "RC4\n");
746 #ifdef OPENSSL_NO_RC5
747 BIO_puts(bio_out
, "RC5\n");
749 #ifdef OPENSSL_NO_RMD160
750 BIO_puts(bio_out
, "RMD160\n");
752 #ifdef OPENSSL_NO_RSA
753 BIO_puts(bio_out
, "RSA\n");
755 #ifdef OPENSSL_NO_SCRYPT
756 BIO_puts(bio_out
, "SCRYPT\n");
758 #ifdef OPENSSL_NO_SCTP
759 BIO_puts(bio_out
, "SCTP\n");
761 #ifdef OPENSSL_NO_SEED
762 BIO_puts(bio_out
, "SEED\n");
764 #ifdef OPENSSL_NO_SM3
765 BIO_puts(bio_out
, "SM3\n");
767 #ifdef OPENSSL_NO_SM4
768 BIO_puts(bio_out
, "SM4\n");
770 #ifdef OPENSSL_NO_SOCK
771 BIO_puts(bio_out
, "SOCK\n");
773 #ifdef OPENSSL_NO_SRP
774 BIO_puts(bio_out
, "SRP\n");
776 #ifdef OPENSSL_NO_SRTP
777 BIO_puts(bio_out
, "SRTP\n");
779 #ifdef OPENSSL_NO_SSL3
780 BIO_puts(bio_out
, "SSL3\n");
782 #ifdef OPENSSL_NO_TLS1
783 BIO_puts(bio_out
, "TLS1\n");
785 #ifdef OPENSSL_NO_TLS1_1
786 BIO_puts(bio_out
, "TLS1_1\n");
788 #ifdef OPENSSL_NO_TLS1_2
789 BIO_puts(bio_out
, "TLS1_2\n");
791 #ifdef OPENSSL_NO_WHIRLPOOL
792 BIO_puts(bio_out
, "WHIRLPOOL\n");
795 BIO_puts(bio_out
, "ZLIB\n");
799 static LHASH_OF(FUNCTION
) *prog_init(void)
801 static LHASH_OF(FUNCTION
) *ret
= NULL
;
802 static int prog_inited
= 0;
811 /* Sort alphabetically within category. For nicer help displays. */
812 for (i
= 0, f
= functions
; f
->name
!= NULL
; ++f
, ++i
)
814 qsort(functions
, i
, sizeof(*functions
), SortFnByName
);
816 if ((ret
= lh_FUNCTION_new(function_hash
, function_cmp
)) == NULL
)
819 for (f
= functions
; f
->name
!= NULL
; f
++)
820 (void)lh_FUNCTION_insert(ret
, f
);