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
33 /* Structure to hold the number of columns to be displayed and the
34 * field width used to display them.
41 /* Special sentinel to exit the program. */
42 #define EXIT_THE_PROGRAM (-1)
45 * The LHASH callbacks ("hash" & "cmp") have been replaced by functions with
46 * the base prototypes (we cast each variable inside the function to the
47 * required type of "FUNCTION*"). This removes the necessity for
48 * macro-generated wrapper functions.
50 static LHASH_OF(FUNCTION
) *prog_init(void);
51 static int do_cmd(LHASH_OF(FUNCTION
) *prog
, int argc
, char *argv
[]);
52 static void list_pkey(void);
53 static void list_pkey_meth(void);
54 static void list_type(FUNC_TYPE ft
, int one
);
55 static void list_disabled(void);
56 char *default_config_file
= NULL
;
62 static void calculate_columns(DISPLAY_COLUMNS
*dc
)
67 for (f
= functions
; f
->name
!= NULL
; ++f
)
68 if (f
->type
== FT_general
|| f
->type
== FT_md
|| f
->type
== FT_cipher
)
69 if ((len
= strlen(f
->name
)) > maxlen
)
72 dc
->width
= maxlen
+ 2;
73 dc
->columns
= (80 - 1) / dc
->width
;
76 static int apps_startup()
79 signal(SIGPIPE
, SIG_IGN
);
82 /* Set non-default library initialisation settings */
83 if (!OPENSSL_init_crypto(OPENSSL_INIT_ENGINE_ALL_BUILTIN
84 | OPENSSL_INIT_LOAD_CONFIG
, NULL
))
92 static void apps_shutdown()
97 static char *make_config_name()
103 if ((t
= getenv("OPENSSL_CONF")) != NULL
)
104 return OPENSSL_strdup(t
);
106 t
= X509_get_default_cert_area();
107 len
= strlen(t
) + 1 + strlen(OPENSSL_CONF
) + 1;
108 p
= app_malloc(len
, "config filename buffer");
110 #ifndef OPENSSL_SYS_VMS
113 strcat(p
, OPENSSL_CONF
);
118 int main(int argc
, char *argv
[])
121 LHASH_OF(FUNCTION
) *prog
= NULL
;
122 char **copied_argv
= NULL
;
127 int first
, n
, i
, ret
= 0;
132 /* Set up some of the environment. */
133 default_config_file
= make_config_name();
134 bio_in
= dup_bio_in(FORMAT_TEXT
);
135 bio_out
= dup_bio_out(FORMAT_TEXT
);
136 bio_err
= dup_bio_err(FORMAT_TEXT
);
138 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
139 copied_argv
= argv
= copy_argv(&argc
, argv
);
140 #elif defined(_WIN32)
142 * Replace argv[] with UTF-8 encoded strings.
144 win32_utf8argv(&argc
, &argv
);
147 p
= getenv("OPENSSL_DEBUG_MEMORY");
148 if (p
!= NULL
&& strcmp(p
, "on") == 0)
149 CRYPTO_set_mem_debug(1);
150 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON
);
152 if (getenv("OPENSSL_FIPS")) {
153 BIO_printf(bio_err
, "FIPS mode not supported.\n");
157 if (!apps_startup()) {
159 "FATAL: Startup failure (dev note: apps_startup() failed)\n");
160 ERR_print_errors(bio_err
);
166 pname
= opt_progname(argv
[0]);
168 /* first check the program name */
170 fp
= lh_FUNCTION_retrieve(prog
, &f
);
173 ret
= fp
->func(argc
, argv
);
177 /* If there is stuff on the command line, run with that. */
181 ret
= do_cmd(prog
, argc
, argv
);
187 /* ok, lets enter interactive mode */
190 /* Read a line, continue reading if line ends with \ */
191 for (p
= buf
, n
= sizeof buf
, i
= 0, first
= 1; n
> 0; first
= 0) {
192 prompt
= first
? "OpenSSL> " : "> ";
195 fputs(prompt
, stdout
);
197 if (!fgets(p
, n
, stdin
))
204 if (p
[i
- 2] != '\\')
211 extern char *readline(const char *);
212 extern void add_history(const char *cp
);
215 text
= readline(prompt
);
221 if (text
[i
- 1] != '\\') {
222 p
+= strlen(strcpy(p
, text
));
229 p
+= strlen(strcpy(p
, text
));
236 if (!chopup_args(&arg
, buf
)) {
237 BIO_printf(bio_err
, "Can't parse (no memory?)\n");
241 ret
= do_cmd(prog
, arg
.argc
, arg
.argv
);
242 if (ret
== EXIT_THE_PROGRAM
) {
247 BIO_printf(bio_err
, "error in %s\n", arg
.argv
[0]);
248 (void)BIO_flush(bio_out
);
249 (void)BIO_flush(bio_err
);
253 OPENSSL_free(copied_argv
);
254 OPENSSL_free(default_config_file
);
255 lh_FUNCTION_free(prog
);
256 OPENSSL_free(arg
.argv
);
260 BIO_free_all(bio_out
);
262 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
263 if (CRYPTO_mem_leaks(bio_err
) <= 0)
270 const OPTIONS exit_options
[] = {
274 static void list_cipher_fn(const EVP_CIPHER
*c
,
275 const char *from
, const char *to
, void *arg
)
278 BIO_printf(arg
, "%s\n", EVP_CIPHER_name(c
));
281 from
= "<undefined>";
284 BIO_printf(arg
, "%s => %s\n", from
, to
);
288 static void list_md_fn(const EVP_MD
*m
,
289 const char *from
, const char *to
, void *arg
)
292 BIO_printf(arg
, "%s\n", EVP_MD_name(m
));
295 from
= "<undefined>";
298 BIO_printf((BIO
*)arg
, "%s => %s\n", from
, to
);
302 static void list_missing_help(void)
307 for (fp
= functions
; fp
->name
!= NULL
; fp
++) {
308 if ((o
= fp
->help
) != NULL
) {
309 /* If there is help, list what flags are not documented. */
310 for ( ; o
->name
!= NULL
; o
++) {
311 if (o
->helpstr
== NULL
)
312 BIO_printf(bio_out
, "%s %s\n", fp
->name
, o
->name
);
314 } else if (fp
->func
!= dgst_main
) {
315 /* If not aliased to the dgst command, */
316 BIO_printf(bio_out
, "%s *\n", fp
->name
);
321 static void list_options_for_command(const char *command
)
326 for (fp
= functions
; fp
->name
!= NULL
; fp
++)
327 if (strcmp(fp
->name
, command
) == 0)
329 if (fp
->name
== NULL
) {
330 BIO_printf(bio_err
, "Invalid command '%s'; type \"help\" for a list.\n",
335 if ((o
= fp
->help
) == NULL
)
338 for ( ; o
->name
!= NULL
; o
++) {
339 if (o
->name
== OPT_HELP_STR
340 || o
->name
== OPT_MORE_STR
341 || o
->name
[0] == '\0')
343 BIO_printf(bio_out
, "%s %c\n", o
->name
, o
->valtype
);
348 /* Unified enum for help and list commands. */
349 typedef enum HELPLIST_CHOICE
{
350 OPT_ERR
= -1, OPT_EOF
= 0, OPT_HELP
, OPT_ONE
,
351 OPT_COMMANDS
, OPT_DIGEST_COMMANDS
, OPT_OPTIONS
,
352 OPT_DIGEST_ALGORITHMS
, OPT_CIPHER_COMMANDS
, OPT_CIPHER_ALGORITHMS
,
353 OPT_PK_ALGORITHMS
, OPT_PK_METHOD
, OPT_DISABLED
, OPT_MISSING_HELP
356 const OPTIONS list_options
[] = {
357 {"help", OPT_HELP
, '-', "Display this summary"},
358 {"1", OPT_ONE
, '-', "List in one column"},
359 {"commands", OPT_COMMANDS
, '-', "List of standard commands"},
360 {"digest-commands", OPT_DIGEST_COMMANDS
, '-',
361 "List of message digest commands"},
362 {"digest-algorithms", OPT_DIGEST_ALGORITHMS
, '-',
363 "List of message digest algorithms"},
364 {"cipher-commands", OPT_CIPHER_COMMANDS
, '-', "List of cipher commands"},
365 {"cipher-algorithms", OPT_CIPHER_ALGORITHMS
, '-',
366 "List of cipher algorithms"},
367 {"public-key-algorithms", OPT_PK_ALGORITHMS
, '-',
368 "List of public key algorithms"},
369 {"public-key-methods", OPT_PK_METHOD
, '-',
370 "List of public key methods"},
371 {"disabled", OPT_DISABLED
, '-',
372 "List of disabled features"},
373 {"missing-help", OPT_MISSING_HELP
, '-',
374 "List missing detailed help strings"},
375 {"options", OPT_OPTIONS
, 's',
376 "List options for specified command"},
380 int list_main(int argc
, char **argv
)
384 int one
= 0, done
= 0;
386 prog
= opt_init(argc
, argv
, list_options
);
387 while ((o
= opt_next()) != OPT_EOF
) {
389 case OPT_EOF
: /* Never hit, but suppresses warning */
392 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
395 opt_help(list_options
);
401 list_type(FT_general
, one
);
403 case OPT_DIGEST_COMMANDS
:
404 list_type(FT_md
, one
);
406 case OPT_DIGEST_ALGORITHMS
:
407 EVP_MD_do_all_sorted(list_md_fn
, bio_out
);
409 case OPT_CIPHER_COMMANDS
:
410 list_type(FT_cipher
, one
);
412 case OPT_CIPHER_ALGORITHMS
:
413 EVP_CIPHER_do_all_sorted(list_cipher_fn
, bio_out
);
415 case OPT_PK_ALGORITHMS
:
424 case OPT_MISSING_HELP
:
428 list_options_for_command(opt_arg());
433 if (opt_num_rest() != 0) {
434 BIO_printf(bio_err
, "Extra arguments given.\n");
444 typedef enum HELP_CHOICE
{
445 OPT_hERR
= -1, OPT_hEOF
= 0, OPT_hHELP
448 const OPTIONS help_options
[] = {
449 {"help", OPT_hHELP
, '-', "Display this summary"},
454 int help_main(int argc
, char **argv
)
463 prog
= opt_init(argc
, argv
, help_options
);
464 while ((o
= opt_next()) != OPT_hEOF
) {
468 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
471 opt_help(help_options
);
476 if (opt_num_rest() != 0) {
477 BIO_printf(bio_err
, "Usage: %s\n", prog
);
481 calculate_columns(&dc
);
482 BIO_printf(bio_err
, "Standard commands");
485 for (fp
= functions
; fp
->name
!= NULL
; fp
++) {
487 if (i
++ % dc
.columns
== 0) {
488 BIO_printf(bio_err
, "\n");
491 if (fp
->type
!= tp
) {
494 BIO_printf(bio_err
, "\n");
498 "\nMessage Digest commands (see the `dgst' command for more details)\n");
499 } else if (tp
== FT_cipher
) {
502 "\nCipher commands (see the `enc' command for more details)\n");
505 BIO_printf(bio_err
, "%-*s", dc
.width
, fp
->name
);
507 BIO_printf(bio_err
, "\n\n");
511 int exit_main(int argc
, char **argv
)
513 return EXIT_THE_PROGRAM
;
516 static void list_type(FUNC_TYPE ft
, int one
)
523 calculate_columns(&dc
);
525 for (fp
= functions
; fp
->name
!= NULL
; fp
++) {
529 BIO_printf(bio_out
, "%s\n", fp
->name
);
531 if (i
% dc
.columns
== 0 && i
> 0)
532 BIO_printf(bio_out
, "\n");
533 BIO_printf(bio_out
, "%-*s", dc
.width
, fp
->name
);
538 BIO_printf(bio_out
, "\n\n");
541 static int do_cmd(LHASH_OF(FUNCTION
) *prog
, int argc
, char *argv
[])
545 if (argc
<= 0 || argv
[0] == NULL
)
548 fp
= lh_FUNCTION_retrieve(prog
, &f
);
550 if (EVP_get_digestbyname(argv
[0])) {
554 } else if (EVP_get_cipherbyname(argv
[0])) {
561 return (fp
->func(argc
, argv
));
563 if ((strncmp(argv
[0], "no-", 3)) == 0) {
565 * User is asking if foo is unsupported, by trying to "run" the
566 * no-foo command. Strange.
568 f
.name
= argv
[0] + 3;
569 if (lh_FUNCTION_retrieve(prog
, &f
) == NULL
) {
570 BIO_printf(bio_out
, "%s\n", argv
[0]);
573 BIO_printf(bio_out
, "%s\n", argv
[0] + 3);
576 if (strcmp(argv
[0], "quit") == 0 || strcmp(argv
[0], "q") == 0 ||
577 strcmp(argv
[0], "exit") == 0 || strcmp(argv
[0], "bye") == 0)
578 /* Special value to mean "exit the program. */
579 return EXIT_THE_PROGRAM
;
581 BIO_printf(bio_err
, "Invalid command '%s'; type \"help\" for a list.\n",
586 static void list_pkey(void)
590 for (i
= 0; i
< EVP_PKEY_asn1_get_count(); i
++) {
591 const EVP_PKEY_ASN1_METHOD
*ameth
;
592 int pkey_id
, pkey_base_id
, pkey_flags
;
593 const char *pinfo
, *pem_str
;
594 ameth
= EVP_PKEY_asn1_get0(i
);
595 EVP_PKEY_asn1_get0_info(&pkey_id
, &pkey_base_id
, &pkey_flags
,
596 &pinfo
, &pem_str
, ameth
);
597 if (pkey_flags
& ASN1_PKEY_ALIAS
) {
598 BIO_printf(bio_out
, "Name: %s\n", OBJ_nid2ln(pkey_id
));
599 BIO_printf(bio_out
, "\tAlias for: %s\n",
600 OBJ_nid2ln(pkey_base_id
));
602 BIO_printf(bio_out
, "Name: %s\n", pinfo
);
603 BIO_printf(bio_out
, "\tType: %s Algorithm\n",
604 pkey_flags
& ASN1_PKEY_DYNAMIC
?
605 "External" : "Builtin");
606 BIO_printf(bio_out
, "\tOID: %s\n", OBJ_nid2ln(pkey_id
));
609 BIO_printf(bio_out
, "\tPEM string: %s\n", pem_str
);
615 static void list_pkey_meth(void)
618 size_t meth_count
= EVP_PKEY_meth_get_count();
620 for (i
= 0; i
< meth_count
; i
++) {
621 const EVP_PKEY_METHOD
*pmeth
= EVP_PKEY_meth_get0(i
);
622 int pkey_id
, pkey_flags
;
624 EVP_PKEY_meth_get0_info(&pkey_id
, &pkey_flags
, pmeth
);
625 BIO_printf(bio_out
, "%s\n", OBJ_nid2ln(pkey_id
));
626 BIO_printf(bio_out
, "\tType: %s Algorithm\n",
627 pkey_flags
& ASN1_PKEY_DYNAMIC
? "External" : "Builtin");
631 static int function_cmp(const FUNCTION
* a
, const FUNCTION
* b
)
633 return strncmp(a
->name
, b
->name
, 8);
636 static unsigned long function_hash(const FUNCTION
* a
)
638 return OPENSSL_LH_strhash(a
->name
);
641 static int SortFnByName(const void *_f1
, const void *_f2
)
643 const FUNCTION
*f1
= _f1
;
644 const FUNCTION
*f2
= _f2
;
646 if (f1
->type
!= f2
->type
)
647 return f1
->type
- f2
->type
;
648 return strcmp(f1
->name
, f2
->name
);
651 static void list_disabled(void)
653 BIO_puts(bio_out
, "Disabled algorithms:\n");
654 #ifdef OPENSSL_NO_ARIA
655 BIO_puts(bio_out
, "ARIA\n");
658 BIO_puts(bio_out
, "BF\n");
660 #ifdef OPENSSL_NO_BLAKE2
661 BIO_puts(bio_out
, "BLAKE2\n");
663 #ifdef OPENSSL_NO_CAMELLIA
664 BIO_puts(bio_out
, "CAMELLIA\n");
666 #ifdef OPENSSL_NO_CAST
667 BIO_puts(bio_out
, "CAST\n");
669 #ifdef OPENSSL_NO_CMAC
670 BIO_puts(bio_out
, "CMAC\n");
672 #ifdef OPENSSL_NO_CMS
673 BIO_puts(bio_out
, "CMS\n");
675 #ifdef OPENSSL_NO_COMP
676 BIO_puts(bio_out
, "COMP\n");
678 #ifdef OPENSSL_NO_DES
679 BIO_puts(bio_out
, "DES\n");
681 #ifdef OPENSSL_NO_DGRAM
682 BIO_puts(bio_out
, "DGRAM\n");
685 BIO_puts(bio_out
, "DH\n");
687 #ifdef OPENSSL_NO_DSA
688 BIO_puts(bio_out
, "DSA\n");
690 #if defined(OPENSSL_NO_DTLS)
691 BIO_puts(bio_out
, "DTLS\n");
693 #if defined(OPENSSL_NO_DTLS1)
694 BIO_puts(bio_out
, "DTLS1\n");
696 #if defined(OPENSSL_NO_DTLS1_2)
697 BIO_puts(bio_out
, "DTLS1_2\n");
700 BIO_puts(bio_out
, "EC\n");
702 #ifdef OPENSSL_NO_EC2M
703 BIO_puts(bio_out
, "EC2M\n");
705 #ifdef OPENSSL_NO_ENGINE
706 BIO_puts(bio_out
, "ENGINE\n");
708 #ifdef OPENSSL_NO_GOST
709 BIO_puts(bio_out
, "GOST\n");
711 #ifdef OPENSSL_NO_HEARTBEATS
712 BIO_puts(bio_out
, "HEARTBEATS\n");
714 #ifdef OPENSSL_NO_IDEA
715 BIO_puts(bio_out
, "IDEA\n");
717 #ifdef OPENSSL_NO_MD2
718 BIO_puts(bio_out
, "MD2\n");
720 #ifdef OPENSSL_NO_MD4
721 BIO_puts(bio_out
, "MD4\n");
723 #ifdef OPENSSL_NO_MD5
724 BIO_puts(bio_out
, "MD5\n");
726 #ifdef OPENSSL_NO_MDC2
727 BIO_puts(bio_out
, "MDC2\n");
729 #ifdef OPENSSL_NO_OCB
730 BIO_puts(bio_out
, "OCB\n");
732 #ifdef OPENSSL_NO_OCSP
733 BIO_puts(bio_out
, "OCSP\n");
735 #ifdef OPENSSL_NO_PSK
736 BIO_puts(bio_out
, "PSK\n");
738 #ifdef OPENSSL_NO_RC2
739 BIO_puts(bio_out
, "RC2\n");
741 #ifdef OPENSSL_NO_RC4
742 BIO_puts(bio_out
, "RC4\n");
744 #ifdef OPENSSL_NO_RC5
745 BIO_puts(bio_out
, "RC5\n");
747 #ifdef OPENSSL_NO_RMD160
748 BIO_puts(bio_out
, "RMD160\n");
750 #ifdef OPENSSL_NO_RSA
751 BIO_puts(bio_out
, "RSA\n");
753 #ifdef OPENSSL_NO_SCRYPT
754 BIO_puts(bio_out
, "SCRYPT\n");
756 #ifdef OPENSSL_NO_SCTP
757 BIO_puts(bio_out
, "SCTP\n");
759 #ifdef OPENSSL_NO_SEED
760 BIO_puts(bio_out
, "SEED\n");
762 #ifdef OPENSSL_NO_SOCK
763 BIO_puts(bio_out
, "SOCK\n");
765 #ifdef OPENSSL_NO_SRP
766 BIO_puts(bio_out
, "SRP\n");
768 #ifdef OPENSSL_NO_SRTP
769 BIO_puts(bio_out
, "SRTP\n");
771 #ifdef OPENSSL_NO_SSL3
772 BIO_puts(bio_out
, "SSL3\n");
774 #ifdef OPENSSL_NO_TLS1
775 BIO_puts(bio_out
, "TLS1\n");
777 #ifdef OPENSSL_NO_TLS1_1
778 BIO_puts(bio_out
, "TLS1_1\n");
780 #ifdef OPENSSL_NO_TLS1_2
781 BIO_puts(bio_out
, "TLS1_2\n");
783 #ifdef OPENSSL_NO_WHIRLPOOL
784 BIO_puts(bio_out
, "WHIRLPOOL\n");
787 BIO_puts(bio_out
, "ZLIB\n");
791 static LHASH_OF(FUNCTION
) *prog_init(void)
793 LHASH_OF(FUNCTION
) *ret
;
797 /* Sort alphabetically within category. For nicer help displays. */
798 for (i
= 0, f
= functions
; f
->name
!= NULL
; ++f
, ++i
) ;
799 qsort(functions
, i
, sizeof(*functions
), SortFnByName
);
801 if ((ret
= lh_FUNCTION_new(function_hash
, function_cmp
)) == NULL
)
804 for (f
= functions
; f
->name
!= NULL
; f
++)
805 (void)lh_FUNCTION_insert(ret
, f
);