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 DECLARE_LHASH_OF(FUNCTION);
157 static LHASH_OF(FUNCTION) *prog_init(void);
158 static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[]);
159 static void list_pkey(void);
160 static void list_type(FUNC_TYPE ft);
161 static void list_disabled(void);
162 char *default_config_file = NULL;
164 static CONF *config = NULL;
169 static void apps_startup()
172 signal(SIGPIPE, SIG_IGN);
174 CRYPTO_malloc_init();
175 ERR_load_crypto_strings();
176 ERR_load_SSL_strings();
177 OpenSSL_add_all_algorithms();
178 OpenSSL_add_ssl_algorithms();
179 OPENSSL_load_builtin_modules();
181 /*SSL_library_init();*/
182 #ifndef OPENSSL_NO_ENGINE
183 ENGINE_load_builtin_engines();
187 static void apps_shutdown()
189 #ifndef OPENSSL_NO_ENGINE
193 CONF_modules_unload(1);
194 #ifndef OPENSSL_NO_COMP
196 SSL_COMP_free_compression_methods();
200 CRYPTO_cleanup_all_ex_data();
201 ERR_remove_thread_state(NULL);
206 static char *make_config_name()
212 if ((t = getenv("OPENSSL_CONF")) != NULL
213 || (t = getenv("SSLEAY_CONF")) != NULL)
214 return BUF_strdup(t);
216 t = X509_get_default_cert_area();
217 len = strlen(t) + 1 + strlen(OPENSSL_CONF) + 1;
218 p = app_malloc(len, "config filename buffer");
220 #ifndef OPENSSL_SYS_VMS
223 strcat(p, OPENSSL_CONF);
228 static void lock_dbg_cb(int mode, int type, const char *file, int line)
230 static int modes[CRYPTO_NUM_LOCKS];
231 const char *errstr = NULL;
232 int rw = mode & (CRYPTO_READ | CRYPTO_WRITE);
234 if (rw != CRYPTO_READ && rw != CRYPTO_WRITE) {
235 errstr = "invalid mode";
239 if (type < 0 || type >= CRYPTO_NUM_LOCKS) {
240 errstr = "type out of bounds";
244 if (mode & CRYPTO_LOCK) {
246 errstr = "already locked";
247 /* must not happen in a single-threaded program --> deadlock! */
251 } else if (mode & CRYPTO_UNLOCK) {
253 errstr = "not locked";
257 if (modes[type] != rw) {
258 errstr = (rw == CRYPTO_READ) ?
259 "CRYPTO_r_unlock on write lock" :
260 "CRYPTO_w_unlock on read lock";
265 errstr = "invalid mode";
272 "openssl (lock_dbg_cb): %s (mode=%d, type=%d) at %s:%d\n",
273 errstr, mode, type, file, line);
277 #if defined( OPENSSL_SYS_VMS)
278 extern char **copy_argv(int *argc, char **argv);
281 int main(int argc, char *argv[])
284 LHASH_OF(FUNCTION) *prog = NULL;
285 char **copied_argv = NULL;
290 int first, n, i, ret = 0;
295 /* Set up some of the environment. */
296 default_config_file = make_config_name();
297 bio_in = dup_bio_in(FORMAT_TEXT);
298 bio_out = dup_bio_out(FORMAT_TEXT);
299 bio_err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
301 #if defined( OPENSSL_SYS_VMS)
302 copied_argv = argv = copy_argv(&argc, argv);
305 p = getenv("OPENSSL_DEBUG_MEMORY");
307 /* if not set, use compiled-in default */
309 else if (strcmp(p, "off") != 0) {
310 CRYPTO_malloc_debug_init();
311 CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
313 CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
315 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
316 CRYPTO_set_locking_callback(lock_dbg_cb);
318 if (getenv("OPENSSL_FIPS")) {
320 if (!FIPS_mode_set(1)) {
321 ERR_load_crypto_strings();
322 ERR_print_errors(bio_err);
326 BIO_printf(bio_err, "FIPS mode not supported.\n");
334 * If first argument is a colon, skip it. Because in "interactive"
335 * mode our prompt is a colon and we can cut/paste whole lines
336 * by doing this hack.
338 if (argv[1] && strcmp(argv[1], ":") == 0) {
344 pname = opt_progname(argv[0]);
346 /* first check the program name */
348 fp = lh_FUNCTION_retrieve(prog, &f);
351 ret = fp->func(argc, argv);
355 /* If there is stuff on the command line, run with that. */
359 ret = do_cmd(prog, argc, argv);
365 /* ok, lets enter interactive mode */
368 /* Read a line, continue reading if line ends with \ */
369 for (p = buf, n = sizeof buf, i = 0, first = 1; n > 0; first = 0) {
370 prompt = first ? "openssl : " : "> ";
373 fputs(prompt, stdout);
375 if (!fgets(p, n, stdin))
382 if (p[i - 2] != '\\')
389 extern char *readline(const char *);
390 extern void add_history(const char *cp);
393 char *text = readline(prompt);
399 if (text[i - 1] != '\\') {
400 p += strlen(strcpy(p, text));
407 p += strlen(strcpy(p, text));
414 if (!chopup_args(&arg, buf)) {
415 BIO_printf(bio_err, "Can't parse (no memory?)\n");
419 ret = do_cmd(prog, arg.argc, arg.argv);
420 if (ret == EXIT_THE_PROGRAM) {
425 BIO_printf(bio_err, "error in %s\n", arg.argv[0]);
426 (void)BIO_flush(bio_out);
427 (void)BIO_flush(bio_err);
431 OPENSSL_free(copied_argv);
432 OPENSSL_free(default_config_file);
435 lh_FUNCTION_free(prog);
436 OPENSSL_free(arg.argv);
439 BIO_free_all(bio_out);
441 CRYPTO_mem_leaks(bio_err);
446 OPTIONS exit_options[] = {
450 static void list_cipher_fn(const EVP_CIPHER *c,
451 const char *from, const char *to, void *arg)
454 BIO_printf(arg, "%s\n", EVP_CIPHER_name(c));
457 from = "<undefined>";
460 BIO_printf(arg, "%s => %s\n", from, to);
464 static void list_md_fn(const EVP_MD *m,
465 const char *from, const char *to, void *arg)
468 BIO_printf(arg, "%s\n", EVP_MD_name(m));
471 from = "<undefined>";
474 BIO_printf((BIO *)arg, "%s => %s\n", from, to);
478 /* Unified enum for help and list commands. */
479 typedef enum HELPLIST_CHOICE {
480 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
481 OPT_COMMANDS, OPT_DIGEST_COMMANDS,
482 OPT_DIGEST_ALGORITHMS, OPT_CIPHER_COMMANDS, OPT_CIPHER_ALGORITHMS,
483 OPT_PK_ALGORITHMS, OPT_DISABLED
486 OPTIONS list_options[] = {
487 {"help", OPT_HELP, '-', "Display this summary"},
488 {"commands", OPT_COMMANDS, '-', "List of standard commands"},
489 {"digest-commands", OPT_DIGEST_COMMANDS, '-',
490 "List of message digest commands"},
491 {"digest-algorithms", OPT_DIGEST_ALGORITHMS, '-',
492 "List of message digest algorithms"},
493 {"cipher-commands", OPT_CIPHER_COMMANDS, '-', "List of cipher commands"},
494 {"cipher-algorithms", OPT_CIPHER_ALGORITHMS, '-',
495 "List of cipher algorithms"},
496 {"public-key-algorithms", OPT_PK_ALGORITHMS, '-',
497 "List of public key algorithms"},
498 {"disabled", OPT_DISABLED, '-',
499 "List of disabled features"},
503 int list_main(int argc, char **argv)
508 prog = opt_init(argc, argv, list_options);
509 while ((o = opt_next()) != OPT_EOF) {
513 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
516 opt_help(list_options);
519 list_type(FT_general);
521 case OPT_DIGEST_COMMANDS:
524 case OPT_DIGEST_ALGORITHMS:
525 EVP_MD_do_all_sorted(list_md_fn, bio_out);
527 case OPT_CIPHER_COMMANDS:
528 list_type(FT_cipher);
530 case OPT_CIPHER_ALGORITHMS:
531 EVP_CIPHER_do_all_sorted(list_cipher_fn, bio_out);
533 case OPT_PK_ALGORITHMS:
545 OPTIONS help_options[] = {
546 {"help", OPT_HELP, '-', "Display this summary"},
550 int help_main(int argc, char **argv)
558 prog = opt_init(argc, argv, help_options);
559 while ((o = opt_next()) != OPT_EOF) {
562 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
565 opt_help(help_options);
569 argc = opt_num_rest();
573 BIO_printf(bio_err, "Usage: %s\n", prog);
577 BIO_printf(bio_err, "\nStandard commands");
580 for (fp = functions; fp->name != NULL; fp++) {
582 if (((i++) % COLUMNS) == 0) {
583 BIO_printf(bio_err, "\n");
586 if (fp->type != tp) {
589 BIO_printf(bio_err, "\n");
593 "\nMessage Digest commands (see the `dgst' command for more details)\n");
594 } else if (tp == FT_cipher) {
597 "\nCipher commands (see the `enc' command for more details)\n");
600 BIO_printf(bio_err, FORMAT, fp->name);
602 BIO_printf(bio_err, "\n\n");
606 int exit_main(int argc, char **argv)
608 return EXIT_THE_PROGRAM;
611 static void list_type(FUNC_TYPE ft)
616 for (fp = functions; fp->name != NULL; fp++)
617 if (fp->type == ft) {
618 if ((i++ % COLUMNS) == 0)
619 BIO_printf(bio_out, "\n");
620 BIO_printf(bio_out, FORMAT, fp->name);
622 BIO_printf(bio_out, "\n");
625 static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[])
629 if (argc <= 0 || argv[0] == NULL)
632 fp = lh_FUNCTION_retrieve(prog, &f);
634 if (EVP_get_digestbyname(argv[0])) {
638 } else if (EVP_get_cipherbyname(argv[0])) {
645 return (fp->func(argc, argv));
647 if ((strncmp(argv[0], "no-", 3)) == 0) {
649 * User is asking if foo is unsupported, by trying to "run" the
650 * no-foo command. Strange.
652 f.name = argv[0] + 3;
653 if (lh_FUNCTION_retrieve(prog, &f) == NULL) {
654 BIO_printf(bio_out, "%s\n", argv[0]);
657 BIO_printf(bio_out, "%s\n", argv[0] + 3);
660 if (strcmp(argv[0], "quit") == 0 || strcmp(argv[0], "q") == 0 ||
661 strcmp(argv[0], "exit") == 0 || strcmp(argv[0], "bye") == 0)
662 /* Special value to mean "exit the program. */
663 return EXIT_THE_PROGRAM;
665 BIO_printf(bio_err, "Invalid command '%s'; type \"help\" for a list.\n",
670 static void list_pkey(void)
674 for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
675 const EVP_PKEY_ASN1_METHOD *ameth;
676 int pkey_id, pkey_base_id, pkey_flags;
677 const char *pinfo, *pem_str;
678 ameth = EVP_PKEY_asn1_get0(i);
679 EVP_PKEY_asn1_get0_info(&pkey_id, &pkey_base_id, &pkey_flags,
680 &pinfo, &pem_str, ameth);
681 if (pkey_flags & ASN1_PKEY_ALIAS) {
682 BIO_printf(bio_out, "Name: %s\n", OBJ_nid2ln(pkey_id));
683 BIO_printf(bio_out, "\tAlias for: %s\n",
684 OBJ_nid2ln(pkey_base_id));
686 BIO_printf(bio_out, "Name: %s\n", pinfo);
687 BIO_printf(bio_out, "\tType: %s Algorithm\n",
688 pkey_flags & ASN1_PKEY_DYNAMIC ?
689 "External" : "Builtin");
690 BIO_printf(bio_out, "\tOID: %s\n", OBJ_nid2ln(pkey_id));
693 BIO_printf(bio_out, "\tPEM string: %s\n", pem_str);
699 static int function_cmp(const FUNCTION * a, const FUNCTION * b)
701 return strncmp(a->name, b->name, 8);
704 static IMPLEMENT_LHASH_COMP_FN(function, FUNCTION)
706 static unsigned long function_hash(const FUNCTION * a)
708 return lh_strhash(a->name);
711 static IMPLEMENT_LHASH_HASH_FN(function, FUNCTION)
713 static int SortFnByName(const void *_f1, const void *_f2)
715 const FUNCTION *f1 = _f1;
716 const FUNCTION *f2 = _f2;
718 if (f1->type != f2->type)
719 return f1->type - f2->type;
720 return strcmp(f1->name, f2->name);
723 static void list_disabled(void)
725 BIO_puts(bio_out, "Disabled algorithms:\n");
726 #ifdef OPENSSL_NO_AES
727 BIO_puts(bio_out, "AES\n");
730 BIO_puts(bio_out, "BF\n");
732 #ifdef OPENSSL_NO_CAMELLIA
733 BIO_puts(bio_out, "CAMELLIA\n");
735 #ifdef OPENSSL_NO_CAST
736 BIO_puts(bio_out, "CAST\n");
738 #ifdef OPENSSL_NO_CMS
739 BIO_puts(bio_out, "CMS\n");
741 #ifdef OPENSSL_NO_COMP
742 BIO_puts(bio_out, "COMP\n");
744 #ifdef OPENSSL_NO_DES
745 BIO_puts(bio_out, "DES\n");
748 BIO_puts(bio_out, "DH\n");
750 #ifdef OPENSSL_NO_DSA
751 BIO_puts(bio_out, "DSA\n");
753 #ifdef OPENSSL_NO_DTLS1
754 BIO_puts(bio_out, "DTLS1\n");
757 BIO_puts(bio_out, "EC\n");
759 #ifdef OPENSSL_NO_EC2M
760 BIO_puts(bio_out, "EC2M\n");
762 #ifdef OPENSSL_NO_ENGINE
763 BIO_puts(bio_out, "ENGINE\n");
765 #ifdef OPENSSL_NO_IDEA
766 BIO_puts(bio_out, "IDEA\n");
768 #ifdef OPENSSL_NO_JPAKE
769 BIO_puts(bio_out, "JPAKE\n");
771 #ifdef OPENSSL_NO_MD2
772 BIO_puts(bio_out, "MD2\n");
774 #ifdef OPENSSL_NO_MD4
775 BIO_puts(bio_out, "MD4\n");
777 #ifdef OPENSSL_NO_MD5
778 BIO_puts(bio_out, "MD5\n");
780 #ifdef OPENSSL_NO_MDC2
781 BIO_puts(bio_out, "MDC2\n");
783 #ifdef OPENSSL_NO_MD_GHOST94
784 BIO_puts(bio_out, "MD_GHOST94\n");
786 #ifdef OPENSSL_NO_OCSP
787 BIO_puts(bio_out, "OCSP\n");
789 #ifdef OPENSSL_NO_PSK
790 BIO_puts(bio_out, "PSK\n");
792 #ifdef OPENSSL_NO_RC2
793 BIO_puts(bio_out, "RC2\n");
795 #ifdef OPENSSL_NO_RC4
796 BIO_puts(bio_out, "RC4\n");
798 #ifdef OPENSSL_NO_RC5
799 BIO_puts(bio_out, "RC5\n");
801 #ifdef OPENSSL_NO_RMD160
802 BIO_puts(bio_out, "RMD160\n");
804 #ifdef OPENSSL_NO_RSA
805 BIO_puts(bio_out, "RSA\n");
807 #ifdef OPENSSL_NO_SCRYPT
808 BIO_puts(bio_out, "SCRYPT\n");
810 #ifdef OPENSSL_NO_SEED
811 BIO_puts(bio_out, "SEED\n");
813 #ifdef OPENSSL_NO_SOCK
814 BIO_puts(bio_out, "SOCK\n");
816 #ifdef OPENSSL_NO_SRP
817 BIO_puts(bio_out, "SRP\n");
819 #ifdef OPENSSL_NO_SRTP
820 BIO_puts(bio_out, "SRTP\n");
822 #ifdef OPENSSL_NO_SSL3
823 BIO_puts(bio_out, "SSL3\n");
825 #ifdef OPENSSL_NO_WHIRLPOOL
826 BIO_puts(bio_out, "WHIRLPOOL\n");
829 BIO_puts(bio_out, "ZLIB\n");
833 static LHASH_OF(FUNCTION) *prog_init(void)
835 LHASH_OF(FUNCTION) *ret;
839 /* Sort alphabetically within category. For nicer help displays. */
840 for (i = 0, f = functions; f->name != NULL; ++f, ++i) ;
841 qsort(functions, i, sizeof(*functions), SortFnByName);
843 if ((ret = lh_FUNCTION_new()) == NULL)
846 for (f = functions; f->name != NULL; f++)
847 (void)lh_FUNCTION_insert(ret, f);