1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
57 /* ====================================================================
58 * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
86 * 6. Redistributions of any form whatsoever must retain the following
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
114 #include <openssl/bio.h>
115 #include <openssl/crypto.h>
116 #include <openssl/rand.h>
117 #include <openssl/lhash.h>
118 #include <openssl/conf.h>
119 #include <openssl/x509.h>
120 #include <openssl/pem.h>
121 #include <openssl/ssl.h>
122 #ifndef OPENSSL_NO_ENGINE
123 # include <openssl/engine.h>
125 #include <openssl/err.h>
127 # include <openssl/fips.h>
129 #define USE_SOCKETS /* needed for the _O_BINARY defs in the MS world */
131 /* Needed to get the other O_xxx flags. */
132 #ifdef OPENSSL_SYS_VMS
135 #define INCLUDE_FUNCTION_TABLE
139 #ifdef OPENSSL_NO_CAMELLIA
140 # define FORMAT "%-15s"
143 # define FORMAT "%-18s"
147 /* Special sentinel to exit the program. */
148 #define EXIT_THE_PROGRAM (-1)
151 * The LHASH callbacks ("hash" & "cmp") have been replaced by functions with
152 * the base prototypes (we cast each variable inside the function to the
153 * required type of "FUNCTION*"). This removes the necessity for
154 * macro-generated wrapper functions.
156 static LHASH_OF(FUNCTION) *prog_init(void);
157 static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[]);
158 static void list_pkey(void);
159 static void list_type(FUNC_TYPE ft);
160 static void list_disabled(void);
161 char *default_config_file = NULL;
163 static CONF *config = NULL;
168 static int apps_startup()
171 signal(SIGPIPE, SIG_IGN);
174 /* Set non-default library initialisation settings */
175 if (!OPENSSL_init_crypto(OPENSSL_INIT_ENGINE_ALL_BUILTIN
176 | OPENSSL_INIT_LOAD_CONFIG, NULL))
184 static void apps_shutdown()
189 static char *make_config_name()
195 if ((t = getenv("OPENSSL_CONF")) != NULL)
196 return OPENSSL_strdup(t);
198 t = X509_get_default_cert_area();
199 len = strlen(t) + 1 + strlen(OPENSSL_CONF) + 1;
200 p = app_malloc(len, "config filename buffer");
202 #ifndef OPENSSL_SYS_VMS
205 strcat(p, OPENSSL_CONF);
210 #if defined( OPENSSL_SYS_VMS)
211 extern char **copy_argv(int *argc, char **argv);
214 int main(int argc, char *argv[])
217 LHASH_OF(FUNCTION) *prog = NULL;
218 char **copied_argv = NULL;
223 int first, n, i, ret = 0;
228 /* Set up some of the environment. */
229 default_config_file = make_config_name();
230 bio_in = dup_bio_in(FORMAT_TEXT);
231 bio_out = dup_bio_out(FORMAT_TEXT);
232 bio_err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
234 #if defined( OPENSSL_SYS_VMS)
235 copied_argv = argv = copy_argv(&argc, argv);
238 p = getenv("OPENSSL_DEBUG_MEMORY");
239 if (p != NULL && strcmp(p, "on") == 0)
240 CRYPTO_set_mem_debug(1);
241 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
243 if (getenv("OPENSSL_FIPS")) {
245 if (!FIPS_mode_set(1)) {
246 ERR_print_errors(bio_err);
250 BIO_printf(bio_err, "FIPS mode not supported.\n");
259 pname = opt_progname(argv[0]);
261 /* first check the program name */
263 fp = lh_FUNCTION_retrieve(prog, &f);
266 ret = fp->func(argc, argv);
270 /* If there is stuff on the command line, run with that. */
274 ret = do_cmd(prog, argc, argv);
280 /* ok, lets enter interactive mode */
283 /* Read a line, continue reading if line ends with \ */
284 for (p = buf, n = sizeof buf, i = 0, first = 1; n > 0; first = 0) {
285 prompt = first ? "OpenSSL> " : "> ";
288 fputs(prompt, stdout);
290 if (!fgets(p, n, stdin))
297 if (p[i - 2] != '\\')
304 extern char *readline(const char *);
305 extern void add_history(const char *cp);
308 char *text = readline(prompt);
314 if (text[i - 1] != '\\') {
315 p += strlen(strcpy(p, text));
322 p += strlen(strcpy(p, text));
329 if (!chopup_args(&arg, buf)) {
330 BIO_printf(bio_err, "Can't parse (no memory?)\n");
334 ret = do_cmd(prog, arg.argc, arg.argv);
335 if (ret == EXIT_THE_PROGRAM) {
340 BIO_printf(bio_err, "error in %s\n", arg.argv[0]);
341 (void)BIO_flush(bio_out);
342 (void)BIO_flush(bio_err);
346 OPENSSL_free(copied_argv);
347 OPENSSL_free(default_config_file);
350 lh_FUNCTION_free(prog);
351 OPENSSL_free(arg.argv);
354 BIO_free_all(bio_out);
356 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
357 if (CRYPTO_mem_leaks(bio_err) <= 0)
364 OPTIONS exit_options[] = {
368 static void list_cipher_fn(const EVP_CIPHER *c,
369 const char *from, const char *to, void *arg)
372 BIO_printf(arg, "%s\n", EVP_CIPHER_name(c));
375 from = "<undefined>";
378 BIO_printf(arg, "%s => %s\n", from, to);
382 static void list_md_fn(const EVP_MD *m,
383 const char *from, const char *to, void *arg)
386 BIO_printf(arg, "%s\n", EVP_MD_name(m));
389 from = "<undefined>";
392 BIO_printf((BIO *)arg, "%s => %s\n", from, to);
396 /* Unified enum for help and list commands. */
397 typedef enum HELPLIST_CHOICE {
398 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
399 OPT_COMMANDS, OPT_DIGEST_COMMANDS,
400 OPT_DIGEST_ALGORITHMS, OPT_CIPHER_COMMANDS, OPT_CIPHER_ALGORITHMS,
401 OPT_PK_ALGORITHMS, OPT_DISABLED
404 OPTIONS list_options[] = {
405 {"help", OPT_HELP, '-', "Display this summary"},
406 {"commands", OPT_COMMANDS, '-', "List of standard commands"},
407 {"digest-commands", OPT_DIGEST_COMMANDS, '-',
408 "List of message digest commands"},
409 {"digest-algorithms", OPT_DIGEST_ALGORITHMS, '-',
410 "List of message digest algorithms"},
411 {"cipher-commands", OPT_CIPHER_COMMANDS, '-', "List of cipher commands"},
412 {"cipher-algorithms", OPT_CIPHER_ALGORITHMS, '-',
413 "List of cipher algorithms"},
414 {"public-key-algorithms", OPT_PK_ALGORITHMS, '-',
415 "List of public key algorithms"},
416 {"disabled", OPT_DISABLED, '-',
417 "List of disabled features"},
421 int list_main(int argc, char **argv)
427 prog = opt_init(argc, argv, list_options);
428 while ((o = opt_next()) != OPT_EOF) {
430 case OPT_EOF: /* Never hit, but suppresses warning */
432 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
435 opt_help(list_options);
438 list_type(FT_general);
440 case OPT_DIGEST_COMMANDS:
443 case OPT_DIGEST_ALGORITHMS:
444 EVP_MD_do_all_sorted(list_md_fn, bio_out);
446 case OPT_CIPHER_COMMANDS:
447 list_type(FT_cipher);
449 case OPT_CIPHER_ALGORITHMS:
450 EVP_CIPHER_do_all_sorted(list_cipher_fn, bio_out);
452 case OPT_PK_ALGORITHMS:
463 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
470 OPTIONS help_options[] = {
471 {"help", OPT_HELP, '-', "Display this summary"},
475 int help_main(int argc, char **argv)
483 prog = opt_init(argc, argv, help_options);
484 while ((o = opt_next()) != OPT_EOF) {
487 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
490 opt_help(help_options);
494 argc = opt_num_rest();
498 BIO_printf(bio_err, "Usage: %s\n", prog);
502 BIO_printf(bio_err, "\nStandard commands");
505 for (fp = functions; fp->name != NULL; fp++) {
507 if (((i++) % COLUMNS) == 0) {
508 BIO_printf(bio_err, "\n");
511 if (fp->type != tp) {
514 BIO_printf(bio_err, "\n");
518 "\nMessage Digest commands (see the `dgst' command for more details)\n");
519 } else if (tp == FT_cipher) {
522 "\nCipher commands (see the `enc' command for more details)\n");
525 BIO_printf(bio_err, FORMAT, fp->name);
527 BIO_printf(bio_err, "\n\n");
531 int exit_main(int argc, char **argv)
533 return EXIT_THE_PROGRAM;
536 static void list_type(FUNC_TYPE ft)
541 for (fp = functions; fp->name != NULL; fp++)
542 if (fp->type == ft) {
543 if ((i++ % COLUMNS) == 0)
544 BIO_printf(bio_out, "\n");
545 BIO_printf(bio_out, FORMAT, fp->name);
547 BIO_printf(bio_out, "\n");
550 static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[])
554 if (argc <= 0 || argv[0] == NULL)
557 fp = lh_FUNCTION_retrieve(prog, &f);
559 if (EVP_get_digestbyname(argv[0])) {
563 } else if (EVP_get_cipherbyname(argv[0])) {
570 return (fp->func(argc, argv));
572 if ((strncmp(argv[0], "no-", 3)) == 0) {
574 * User is asking if foo is unsupported, by trying to "run" the
575 * no-foo command. Strange.
577 f.name = argv[0] + 3;
578 if (lh_FUNCTION_retrieve(prog, &f) == NULL) {
579 BIO_printf(bio_out, "%s\n", argv[0]);
582 BIO_printf(bio_out, "%s\n", argv[0] + 3);
585 if (strcmp(argv[0], "quit") == 0 || strcmp(argv[0], "q") == 0 ||
586 strcmp(argv[0], "exit") == 0 || strcmp(argv[0], "bye") == 0)
587 /* Special value to mean "exit the program. */
588 return EXIT_THE_PROGRAM;
590 BIO_printf(bio_err, "Invalid command '%s'; type \"help\" for a list.\n",
595 static void list_pkey(void)
599 for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
600 const EVP_PKEY_ASN1_METHOD *ameth;
601 int pkey_id, pkey_base_id, pkey_flags;
602 const char *pinfo, *pem_str;
603 ameth = EVP_PKEY_asn1_get0(i);
604 EVP_PKEY_asn1_get0_info(&pkey_id, &pkey_base_id, &pkey_flags,
605 &pinfo, &pem_str, ameth);
606 if (pkey_flags & ASN1_PKEY_ALIAS) {
607 BIO_printf(bio_out, "Name: %s\n", OBJ_nid2ln(pkey_id));
608 BIO_printf(bio_out, "\tAlias for: %s\n",
609 OBJ_nid2ln(pkey_base_id));
611 BIO_printf(bio_out, "Name: %s\n", pinfo);
612 BIO_printf(bio_out, "\tType: %s Algorithm\n",
613 pkey_flags & ASN1_PKEY_DYNAMIC ?
614 "External" : "Builtin");
615 BIO_printf(bio_out, "\tOID: %s\n", OBJ_nid2ln(pkey_id));
618 BIO_printf(bio_out, "\tPEM string: %s\n", pem_str);
624 static int function_cmp(const FUNCTION * a, const FUNCTION * b)
626 return strncmp(a->name, b->name, 8);
629 static unsigned long function_hash(const FUNCTION * a)
631 return lh_strhash(a->name);
634 static int SortFnByName(const void *_f1, const void *_f2)
636 const FUNCTION *f1 = _f1;
637 const FUNCTION *f2 = _f2;
639 if (f1->type != f2->type)
640 return f1->type - f2->type;
641 return strcmp(f1->name, f2->name);
644 static void list_disabled(void)
646 BIO_puts(bio_out, "Disabled algorithms:\n");
647 #ifdef OPENSSL_NO_AES
648 BIO_puts(bio_out, "AES\n");
651 BIO_puts(bio_out, "BF\n");
653 #ifndef OPENSSL_NO_BLAKE2
654 BIO_puts(bio_out, "BLAKE2\n");
656 #ifdef OPENSSL_NO_CAMELLIA
657 BIO_puts(bio_out, "CAMELLIA\n");
659 #ifdef OPENSSL_NO_CAST
660 BIO_puts(bio_out, "CAST\n");
662 #ifdef OPENSSL_NO_CMAC
663 BIO_puts(bio_out, "CMAC\n");
665 #ifdef OPENSSL_NO_CMS
666 BIO_puts(bio_out, "CMS\n");
668 #ifdef OPENSSL_NO_COMP
669 BIO_puts(bio_out, "COMP\n");
671 #ifdef OPENSSL_NO_DES
672 BIO_puts(bio_out, "DES\n");
674 #ifdef OPENSSL_NO_DGRAM
675 BIO_puts(bio_out, "DGRAM\n");
678 BIO_puts(bio_out, "DH\n");
680 #ifdef OPENSSL_NO_DSA
681 BIO_puts(bio_out, "DSA\n");
683 #if defined(OPENSSL_NO_DTLS)
684 BIO_puts(bio_out, "DTLS\n");
686 #if defined(OPENSSL_NO_DTLS1)
687 BIO_puts(bio_out, "DTLS1\n");
689 #if defined(OPENSSL_NO_DTLS1_2)
690 BIO_puts(bio_out, "DTLS1_2\n");
693 BIO_puts(bio_out, "EC\n");
695 #ifdef OPENSSL_NO_EC2M
696 BIO_puts(bio_out, "EC2M\n");
698 #ifdef OPENSSL_NO_ENGINE
699 BIO_puts(bio_out, "ENGINE\n");
701 #ifdef OPENSSL_NO_GOST
702 BIO_puts(bio_out, "GOST\n");
704 #ifdef OPENSSL_NO_HEARTBEATS
705 BIO_puts(bio_out, "HEARTBEATS\n");
707 #ifdef OPENSSL_NO_HMAC
708 BIO_puts(bio_out, "HMAC\n");
710 #ifdef OPENSSL_NO_IDEA
711 BIO_puts(bio_out, "IDEA\n");
713 #ifdef OPENSSL_NO_MD2
714 BIO_puts(bio_out, "MD2\n");
716 #ifdef OPENSSL_NO_MD4
717 BIO_puts(bio_out, "MD4\n");
719 #ifdef OPENSSL_NO_MD5
720 BIO_puts(bio_out, "MD5\n");
722 #ifdef OPENSSL_NO_MDC2
723 BIO_puts(bio_out, "MDC2\n");
725 #ifdef OPENSSL_NO_OCB
726 BIO_puts(bio_out, "OCB\n");
728 #ifdef OPENSSL_NO_OCSP
729 BIO_puts(bio_out, "OCSP\n");
731 #ifdef OPENSSL_NO_PSK
732 BIO_puts(bio_out, "PSK\n");
734 #ifdef OPENSSL_NO_RC2
735 BIO_puts(bio_out, "RC2\n");
737 #ifdef OPENSSL_NO_RC4
738 BIO_puts(bio_out, "RC4\n");
740 #ifdef OPENSSL_NO_RC5
741 BIO_puts(bio_out, "RC5\n");
743 #ifdef OPENSSL_NO_RMD160
744 BIO_puts(bio_out, "RMD160\n");
746 #ifdef OPENSSL_NO_RSA
747 BIO_puts(bio_out, "RSA\n");
749 #ifdef OPENSSL_NO_SCRYPT
750 BIO_puts(bio_out, "SCRYPT\n");
752 #ifdef OPENSSL_NO_SCT
753 BIO_puts(bio_out, "SCT\n");
755 #ifdef OPENSSL_NO_SCTP
756 BIO_puts(bio_out, "SCTP\n");
758 #ifdef OPENSSL_NO_SEED
759 BIO_puts(bio_out, "SEED\n");
761 #ifdef OPENSSL_NO_SHA
762 BIO_puts(bio_out, "SHA\n");
764 #ifdef OPENSSL_NO_SOCK
765 BIO_puts(bio_out, "SOCK\n");
767 #ifdef OPENSSL_NO_SRP
768 BIO_puts(bio_out, "SRP\n");
770 #ifdef OPENSSL_NO_SRTP
771 BIO_puts(bio_out, "SRTP\n");
773 #ifdef OPENSSL_NO_SSL
774 BIO_puts(bio_out, "SSL\n");
776 #ifdef OPENSSL_NO_SSL3
777 BIO_puts(bio_out, "SSL3\n");
779 #if defined(OPENSSL_NO_TLS)
780 BIO_puts(bio_out, "TLS\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 LHASH_OF(FUNCTION) *ret;
805 /* Sort alphabetically within category. For nicer help displays. */
806 for (i = 0, f = functions; f->name != NULL; ++f, ++i) ;
807 qsort(functions, i, sizeof(*functions), SortFnByName);
809 if ((ret = lh_FUNCTION_new(function_hash, function_cmp)) == NULL)
812 for (f = functions; f->name != NULL; f++)
813 (void)lh_FUNCTION_insert(ret, f);