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
34 #ifdef OPENSSL_NO_CAMELLIA
35 # define FORMAT "%-15s"
38 # define FORMAT "%-18s"
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_type(FUNC_TYPE ft
);
55 static void list_disabled(void);
56 char *default_config_file
= NULL
;
62 static int apps_startup()
65 signal(SIGPIPE
, SIG_IGN
);
68 /* Set non-default library initialisation settings */
69 if (!OPENSSL_init_crypto(OPENSSL_INIT_ENGINE_ALL_BUILTIN
70 | OPENSSL_INIT_LOAD_CONFIG
, NULL
))
78 static void apps_shutdown()
83 static char *make_config_name()
89 if ((t
= getenv("OPENSSL_CONF")) != NULL
)
90 return OPENSSL_strdup(t
);
92 t
= X509_get_default_cert_area();
93 len
= strlen(t
) + 1 + strlen(OPENSSL_CONF
) + 1;
94 p
= app_malloc(len
, "config filename buffer");
96 #ifndef OPENSSL_SYS_VMS
99 strcat(p
, OPENSSL_CONF
);
104 int main(int argc
, char *argv
[])
107 LHASH_OF(FUNCTION
) *prog
= NULL
;
108 char **copied_argv
= NULL
;
113 int first
, n
, i
, ret
= 0;
118 /* Set up some of the environment. */
119 default_config_file
= make_config_name();
120 bio_in
= dup_bio_in(FORMAT_TEXT
);
121 bio_out
= dup_bio_out(FORMAT_TEXT
);
122 bio_err
= dup_bio_err(FORMAT_TEXT
);
124 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
125 copied_argv
= argv
= copy_argv(&argc
, argv
);
126 #elif defined(_WIN32)
128 * Replace argv[] with UTF-8 encoded strings.
130 win32_utf8argv(&argc
, &argv
);
133 p
= getenv("OPENSSL_DEBUG_MEMORY");
134 if (p
!= NULL
&& strcmp(p
, "on") == 0)
135 CRYPTO_set_mem_debug(1);
136 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON
);
138 if (getenv("OPENSSL_FIPS")) {
139 BIO_printf(bio_err
, "FIPS mode not supported.\n");
143 if (!apps_startup()) {
145 "FATAL: Startup failure (dev note: apps_startup() failed)\n");
146 ERR_print_errors(bio_err
);
152 pname
= opt_progname(argv
[0]);
154 /* first check the program name */
156 fp
= lh_FUNCTION_retrieve(prog
, &f
);
159 ret
= fp
->func(argc
, argv
);
163 /* If there is stuff on the command line, run with that. */
167 ret
= do_cmd(prog
, argc
, argv
);
173 /* ok, lets enter interactive mode */
176 /* Read a line, continue reading if line ends with \ */
177 for (p
= buf
, n
= sizeof buf
, i
= 0, first
= 1; n
> 0; first
= 0) {
178 prompt
= first
? "OpenSSL> " : "> ";
181 fputs(prompt
, stdout
);
183 if (!fgets(p
, n
, stdin
))
190 if (p
[i
- 2] != '\\')
197 extern char *readline(const char *);
198 extern void add_history(const char *cp
);
201 text
= readline(prompt
);
207 if (text
[i
- 1] != '\\') {
208 p
+= strlen(strcpy(p
, text
));
215 p
+= strlen(strcpy(p
, text
));
222 if (!chopup_args(&arg
, buf
)) {
223 BIO_printf(bio_err
, "Can't parse (no memory?)\n");
227 ret
= do_cmd(prog
, arg
.argc
, arg
.argv
);
228 if (ret
== EXIT_THE_PROGRAM
) {
233 BIO_printf(bio_err
, "error in %s\n", arg
.argv
[0]);
234 (void)BIO_flush(bio_out
);
235 (void)BIO_flush(bio_err
);
239 OPENSSL_free(copied_argv
);
240 OPENSSL_free(default_config_file
);
241 lh_FUNCTION_free(prog
);
242 OPENSSL_free(arg
.argv
);
245 BIO_free_all(bio_out
);
247 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
248 if (CRYPTO_mem_leaks(bio_err
) <= 0)
255 const OPTIONS exit_options
[] = {
259 static void list_cipher_fn(const EVP_CIPHER
*c
,
260 const char *from
, const char *to
, void *arg
)
263 BIO_printf(arg
, "%s\n", EVP_CIPHER_name(c
));
266 from
= "<undefined>";
269 BIO_printf(arg
, "%s => %s\n", from
, to
);
273 static void list_md_fn(const EVP_MD
*m
,
274 const char *from
, const char *to
, void *arg
)
277 BIO_printf(arg
, "%s\n", EVP_MD_name(m
));
280 from
= "<undefined>";
283 BIO_printf((BIO
*)arg
, "%s => %s\n", from
, to
);
287 static void list_missing_help(void)
292 for (fp
= functions
; fp
->name
!= NULL
; fp
++) {
293 if ((o
= fp
->help
) == NULL
) {
294 BIO_printf(bio_out
, "%s *\n", fp
->name
);
297 for ( ; o
->name
!= NULL
; o
++) {
298 if (o
->helpstr
== NULL
)
299 BIO_printf(bio_out
, "%s %s\n", fp
->name
, o
->name
);
305 /* Unified enum for help and list commands. */
306 typedef enum HELPLIST_CHOICE
{
307 OPT_ERR
= -1, OPT_EOF
= 0, OPT_HELP
,
308 OPT_COMMANDS
, OPT_DIGEST_COMMANDS
,
309 OPT_DIGEST_ALGORITHMS
, OPT_CIPHER_COMMANDS
, OPT_CIPHER_ALGORITHMS
,
310 OPT_PK_ALGORITHMS
, OPT_DISABLED
, OPT_MISSING_HELP
313 const OPTIONS list_options
[] = {
314 {"help", OPT_HELP
, '-', "Display this summary"},
315 {"commands", OPT_COMMANDS
, '-', "List of standard commands"},
316 {"digest-commands", OPT_DIGEST_COMMANDS
, '-',
317 "List of message digest commands"},
318 {"digest-algorithms", OPT_DIGEST_ALGORITHMS
, '-',
319 "List of message digest algorithms"},
320 {"cipher-commands", OPT_CIPHER_COMMANDS
, '-', "List of cipher commands"},
321 {"cipher-algorithms", OPT_CIPHER_ALGORITHMS
, '-',
322 "List of cipher algorithms"},
323 {"public-key-algorithms", OPT_PK_ALGORITHMS
, '-',
324 "List of public key algorithms"},
325 {"disabled", OPT_DISABLED
, '-',
326 "List of disabled features"},
327 {"missing-help", OPT_MISSING_HELP
, '-',
328 "List missing detailed help strings"},
332 int list_main(int argc
, char **argv
)
338 prog
= opt_init(argc
, argv
, list_options
);
339 while ((o
= opt_next()) != OPT_EOF
) {
341 case OPT_EOF
: /* Never hit, but suppresses warning */
343 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
346 opt_help(list_options
);
349 list_type(FT_general
);
351 case OPT_DIGEST_COMMANDS
:
354 case OPT_DIGEST_ALGORITHMS
:
355 EVP_MD_do_all_sorted(list_md_fn
, bio_out
);
357 case OPT_CIPHER_COMMANDS
:
358 list_type(FT_cipher
);
360 case OPT_CIPHER_ALGORITHMS
:
361 EVP_CIPHER_do_all_sorted(list_cipher_fn
, bio_out
);
363 case OPT_PK_ALGORITHMS
:
369 case OPT_MISSING_HELP
:
377 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
384 typedef enum HELP_CHOICE
{
385 OPT_hERR
= -1, OPT_hEOF
= 0, OPT_hHELP
388 const OPTIONS help_options
[] = {
389 {"help", OPT_hHELP
, '-', "Display this summary"},
394 int help_main(int argc
, char **argv
)
402 prog
= opt_init(argc
, argv
, help_options
);
403 while ((o
= opt_next()) != OPT_hEOF
) {
407 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
410 opt_help(help_options
);
415 if (opt_num_rest() != 0) {
416 BIO_printf(bio_err
, "Usage: %s\n", prog
);
420 BIO_printf(bio_err
, "\nStandard commands");
423 for (fp
= functions
; fp
->name
!= NULL
; fp
++) {
425 if (((i
++) % COLUMNS
) == 0) {
426 BIO_printf(bio_err
, "\n");
429 if (fp
->type
!= tp
) {
432 BIO_printf(bio_err
, "\n");
436 "\nMessage Digest commands (see the `dgst' command for more details)\n");
437 } else if (tp
== FT_cipher
) {
440 "\nCipher commands (see the `enc' command for more details)\n");
443 BIO_printf(bio_err
, FORMAT
, fp
->name
);
445 BIO_printf(bio_err
, "\n\n");
449 int exit_main(int argc
, char **argv
)
451 return EXIT_THE_PROGRAM
;
454 static void list_type(FUNC_TYPE ft
)
459 for (fp
= functions
; fp
->name
!= NULL
; fp
++)
460 if (fp
->type
== ft
) {
461 if ((i
++ % COLUMNS
) == 0)
462 BIO_printf(bio_out
, "\n");
463 BIO_printf(bio_out
, FORMAT
, fp
->name
);
465 BIO_printf(bio_out
, "\n");
468 static int do_cmd(LHASH_OF(FUNCTION
) *prog
, int argc
, char *argv
[])
472 if (argc
<= 0 || argv
[0] == NULL
)
475 fp
= lh_FUNCTION_retrieve(prog
, &f
);
477 if (EVP_get_digestbyname(argv
[0])) {
481 } else if (EVP_get_cipherbyname(argv
[0])) {
488 return (fp
->func(argc
, argv
));
490 if ((strncmp(argv
[0], "no-", 3)) == 0) {
492 * User is asking if foo is unsupported, by trying to "run" the
493 * no-foo command. Strange.
495 f
.name
= argv
[0] + 3;
496 if (lh_FUNCTION_retrieve(prog
, &f
) == NULL
) {
497 BIO_printf(bio_out
, "%s\n", argv
[0]);
500 BIO_printf(bio_out
, "%s\n", argv
[0] + 3);
503 if (strcmp(argv
[0], "quit") == 0 || strcmp(argv
[0], "q") == 0 ||
504 strcmp(argv
[0], "exit") == 0 || strcmp(argv
[0], "bye") == 0)
505 /* Special value to mean "exit the program. */
506 return EXIT_THE_PROGRAM
;
508 BIO_printf(bio_err
, "Invalid command '%s'; type \"help\" for a list.\n",
513 static void list_pkey(void)
517 for (i
= 0; i
< EVP_PKEY_asn1_get_count(); i
++) {
518 const EVP_PKEY_ASN1_METHOD
*ameth
;
519 int pkey_id
, pkey_base_id
, pkey_flags
;
520 const char *pinfo
, *pem_str
;
521 ameth
= EVP_PKEY_asn1_get0(i
);
522 EVP_PKEY_asn1_get0_info(&pkey_id
, &pkey_base_id
, &pkey_flags
,
523 &pinfo
, &pem_str
, ameth
);
524 if (pkey_flags
& ASN1_PKEY_ALIAS
) {
525 BIO_printf(bio_out
, "Name: %s\n", OBJ_nid2ln(pkey_id
));
526 BIO_printf(bio_out
, "\tAlias for: %s\n",
527 OBJ_nid2ln(pkey_base_id
));
529 BIO_printf(bio_out
, "Name: %s\n", pinfo
);
530 BIO_printf(bio_out
, "\tType: %s Algorithm\n",
531 pkey_flags
& ASN1_PKEY_DYNAMIC
?
532 "External" : "Builtin");
533 BIO_printf(bio_out
, "\tOID: %s\n", OBJ_nid2ln(pkey_id
));
536 BIO_printf(bio_out
, "\tPEM string: %s\n", pem_str
);
542 static int function_cmp(const FUNCTION
* a
, const FUNCTION
* b
)
544 return strncmp(a
->name
, b
->name
, 8);
547 static unsigned long function_hash(const FUNCTION
* a
)
549 return OPENSSL_LH_strhash(a
->name
);
552 static int SortFnByName(const void *_f1
, const void *_f2
)
554 const FUNCTION
*f1
= _f1
;
555 const FUNCTION
*f2
= _f2
;
557 if (f1
->type
!= f2
->type
)
558 return f1
->type
- f2
->type
;
559 return strcmp(f1
->name
, f2
->name
);
562 static void list_disabled(void)
564 BIO_puts(bio_out
, "Disabled algorithms:\n");
565 #ifdef OPENSSL_NO_ARIA
566 BIO_puts(bio_out
, "ARIA\n");
569 BIO_puts(bio_out
, "BF\n");
571 #ifdef OPENSSL_NO_BLAKE2
572 BIO_puts(bio_out
, "BLAKE2\n");
574 #ifdef OPENSSL_NO_CAMELLIA
575 BIO_puts(bio_out
, "CAMELLIA\n");
577 #ifdef OPENSSL_NO_CAST
578 BIO_puts(bio_out
, "CAST\n");
580 #ifdef OPENSSL_NO_CMAC
581 BIO_puts(bio_out
, "CMAC\n");
583 #ifdef OPENSSL_NO_CMS
584 BIO_puts(bio_out
, "CMS\n");
586 #ifdef OPENSSL_NO_COMP
587 BIO_puts(bio_out
, "COMP\n");
589 #ifdef OPENSSL_NO_DES
590 BIO_puts(bio_out
, "DES\n");
592 #ifdef OPENSSL_NO_DGRAM
593 BIO_puts(bio_out
, "DGRAM\n");
596 BIO_puts(bio_out
, "DH\n");
598 #ifdef OPENSSL_NO_DSA
599 BIO_puts(bio_out
, "DSA\n");
601 #if defined(OPENSSL_NO_DTLS)
602 BIO_puts(bio_out
, "DTLS\n");
604 #if defined(OPENSSL_NO_DTLS1)
605 BIO_puts(bio_out
, "DTLS1\n");
607 #if defined(OPENSSL_NO_DTLS1_2)
608 BIO_puts(bio_out
, "DTLS1_2\n");
611 BIO_puts(bio_out
, "EC\n");
613 #ifdef OPENSSL_NO_EC2M
614 BIO_puts(bio_out
, "EC2M\n");
616 #ifdef OPENSSL_NO_ENGINE
617 BIO_puts(bio_out
, "ENGINE\n");
619 #ifdef OPENSSL_NO_GOST
620 BIO_puts(bio_out
, "GOST\n");
622 #ifdef OPENSSL_NO_HEARTBEATS
623 BIO_puts(bio_out
, "HEARTBEATS\n");
625 #ifdef OPENSSL_NO_IDEA
626 BIO_puts(bio_out
, "IDEA\n");
628 #ifdef OPENSSL_NO_MD2
629 BIO_puts(bio_out
, "MD2\n");
631 #ifdef OPENSSL_NO_MD4
632 BIO_puts(bio_out
, "MD4\n");
634 #ifdef OPENSSL_NO_MD5
635 BIO_puts(bio_out
, "MD5\n");
637 #ifdef OPENSSL_NO_MDC2
638 BIO_puts(bio_out
, "MDC2\n");
640 #ifdef OPENSSL_NO_OCB
641 BIO_puts(bio_out
, "OCB\n");
643 #ifdef OPENSSL_NO_OCSP
644 BIO_puts(bio_out
, "OCSP\n");
646 #ifdef OPENSSL_NO_PSK
647 BIO_puts(bio_out
, "PSK\n");
649 #ifdef OPENSSL_NO_RC2
650 BIO_puts(bio_out
, "RC2\n");
652 #ifdef OPENSSL_NO_RC4
653 BIO_puts(bio_out
, "RC4\n");
655 #ifdef OPENSSL_NO_RC5
656 BIO_puts(bio_out
, "RC5\n");
658 #ifdef OPENSSL_NO_RMD160
659 BIO_puts(bio_out
, "RMD160\n");
661 #ifdef OPENSSL_NO_RSA
662 BIO_puts(bio_out
, "RSA\n");
664 #ifdef OPENSSL_NO_SCRYPT
665 BIO_puts(bio_out
, "SCRYPT\n");
667 #ifdef OPENSSL_NO_SCTP
668 BIO_puts(bio_out
, "SCTP\n");
670 #ifdef OPENSSL_NO_SEED
671 BIO_puts(bio_out
, "SEED\n");
673 #ifdef OPENSSL_NO_SOCK
674 BIO_puts(bio_out
, "SOCK\n");
676 #ifdef OPENSSL_NO_SRP
677 BIO_puts(bio_out
, "SRP\n");
679 #ifdef OPENSSL_NO_SRTP
680 BIO_puts(bio_out
, "SRTP\n");
682 #ifdef OPENSSL_NO_SSL3
683 BIO_puts(bio_out
, "SSL3\n");
685 #ifdef OPENSSL_NO_TLS1
686 BIO_puts(bio_out
, "TLS1\n");
688 #ifdef OPENSSL_NO_TLS1_1
689 BIO_puts(bio_out
, "TLS1_1\n");
691 #ifdef OPENSSL_NO_TLS1_2
692 BIO_puts(bio_out
, "TLS1_2\n");
694 #ifdef OPENSSL_NO_WHIRLPOOL
695 BIO_puts(bio_out
, "WHIRLPOOL\n");
698 BIO_puts(bio_out
, "ZLIB\n");
702 static LHASH_OF(FUNCTION
) *prog_init(void)
704 LHASH_OF(FUNCTION
) *ret
;
708 /* Sort alphabetically within category. For nicer help displays. */
709 for (i
= 0, f
= functions
; f
->name
!= NULL
; ++f
, ++i
) ;
710 qsort(functions
, i
, sizeof(*functions
), SortFnByName
);
712 if ((ret
= lh_FUNCTION_new(function_hash
, function_cmp
)) == NULL
)
715 for (f
= functions
; f
->name
!= NULL
; f
++)
716 (void)lh_FUNCTION_insert(ret
, f
);