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 /* needed for the _O_BINARY defs in the MS world */
128 #include <openssl/err.h>
130 # include <openssl/fips.h>
132 #define INCLUDE_FUNCTION_TABLE
136 #ifdef OPENSSL_NO_CAMELLIA
137 # define FORMAT "%-15s"
140 # define FORMAT "%-18s"
144 /* Special sentinel to exit the program. */
145 #define EXIT_THE_PROGRAM (-1)
148 * The LHASH callbacks ("hash" & "cmp") have been replaced by functions with
149 * the base prototypes (we cast each variable inside the function to the
150 * required type of "FUNCTION*"). This removes the necessity for
151 * macro-generated wrapper functions.
153 DECLARE_LHASH_OF(FUNCTION);
154 static LHASH_OF(FUNCTION) *prog_init(void);
155 static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[]);
156 static void list_pkey(void);
157 static void list_type(FUNC_TYPE ft);
158 char *default_config_file = NULL;
165 static void apps_startup()
168 signal(SIGPIPE, SIG_IGN);
170 CRYPTO_malloc_init();
171 ERR_load_crypto_strings();
172 ERR_load_SSL_strings();
173 OpenSSL_add_all_algorithms();
174 OpenSSL_add_ssl_algorithms();
175 OPENSSL_load_builtin_modules();
177 /*SSL_library_init();*/
178 #ifndef OPENSSL_NO_ENGINE
179 ENGINE_load_builtin_engines();
183 static void apps_shutdown()
185 #ifndef OPENSSL_NO_ENGINE
189 CONF_modules_unload(1);
190 #ifndef OPENSSL_NO_COMP
192 SSL_COMP_free_compression_methods();
196 CRYPTO_cleanup_all_ex_data();
197 ERR_remove_thread_state(NULL);
202 static char *make_config_name()
208 if ((t = getenv("OPENSSL_CONF")) != NULL
209 || (t = getenv("SSLEAY_CONF")) != NULL)
210 return BUF_strdup(t);
212 t = X509_get_default_cert_area();
213 len = strlen(t) + 1 + strlen(OPENSSL_CONF) + 1;
214 p = app_malloc(len, "config filename buffer");
216 #ifndef OPENSSL_SYS_VMS
219 strcat(p, OPENSSL_CONF);
224 static void lock_dbg_cb(int mode, int type, const char *file, int line)
226 static int modes[CRYPTO_NUM_LOCKS];
227 const char *errstr = NULL;
228 int rw = mode & (CRYPTO_READ | CRYPTO_WRITE);
230 if (rw != CRYPTO_READ && rw != CRYPTO_WRITE) {
231 errstr = "invalid mode";
235 if (type < 0 || type >= CRYPTO_NUM_LOCKS) {
236 errstr = "type out of bounds";
240 if (mode & CRYPTO_LOCK) {
242 errstr = "already locked";
243 /* must not happen in a single-threaded program --> deadlock! */
247 } else if (mode & CRYPTO_UNLOCK) {
249 errstr = "not locked";
253 if (modes[type] != rw) {
254 errstr = (rw == CRYPTO_READ) ?
255 "CRYPTO_r_unlock on write lock" :
256 "CRYPTO_w_unlock on read lock";
261 errstr = "invalid mode";
268 "openssl (lock_dbg_cb): %s (mode=%d, type=%d) at %s:%d\n",
269 errstr, mode, type, file, line);
273 BIO *dup_bio_in(void)
275 return BIO_new_fp(stdin, BIO_NOCLOSE | BIO_FP_TEXT);
278 BIO *dup_bio_out(void)
280 BIO *b = BIO_new_fp(stdout, BIO_NOCLOSE | BIO_FP_TEXT);
281 #ifdef OPENSSL_SYS_VMS
282 b = BIO_push(BIO_new(BIO_f_linebuffer()), b);
287 void unbuffer(FILE *fp)
292 static BIO *bio_open_default_(const char *filename, const char *mode, int quiet)
296 if (filename == NULL || strcmp(filename, "-") == 0) {
297 ret = *mode == 'r' ? dup_bio_in() : dup_bio_out();
305 "Can't open %s, %s\n",
306 *mode == 'r' ? "stdin" : "stdout", strerror(errno));
308 ret = BIO_new_file(filename, mode);
316 "Can't open %s for %s, %s\n",
318 *mode == 'r' ? "reading" : "writing", strerror(errno));
320 ERR_print_errors(bio_err);
323 BIO *bio_open_default(const char *filename, const char *mode)
325 return bio_open_default_(filename, mode, 0);
327 BIO *bio_open_default_quiet(const char *filename, const char *mode)
329 return bio_open_default_(filename, mode, 1);
332 #if defined( OPENSSL_SYS_VMS)
333 extern char **copy_argv(int *argc, char **argv);
336 int main(int argc, char *argv[])
339 LHASH_OF(FUNCTION) *prog = NULL;
340 char **copied_argv = NULL;
345 int first, n, i, ret = 0;
350 /* Set up some of the environment. */
351 default_config_file = make_config_name();
352 bio_in = dup_bio_in();
353 bio_out = dup_bio_out();
354 bio_err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
356 #if defined( OPENSSL_SYS_VMS)
357 copied_argv = argv = copy_argv(&argc, argv);
360 p = getenv("OPENSSL_DEBUG_MEMORY");
362 /* if not set, use compiled-in default */
364 else if (strcmp(p, "off") != 0) {
365 CRYPTO_malloc_debug_init();
366 CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
368 CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
370 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
371 CRYPTO_set_locking_callback(lock_dbg_cb);
373 if (getenv("OPENSSL_FIPS")) {
375 if (!FIPS_mode_set(1)) {
376 ERR_load_crypto_strings();
377 ERR_print_errors(bio_err);
381 BIO_printf(bio_err, "FIPS mode not supported.\n");
389 * If first argument is a colon, skip it. Because in "interactive"
390 * mode our prompt is a colon and we can cut/paste whole lines
391 * by doing this hack.
393 if (argv[1] && strcmp(argv[1], ":") == 0) {
399 pname = opt_progname(argv[0]);
401 /* first check the program name */
403 fp = lh_FUNCTION_retrieve(prog, &f);
406 ret = fp->func(argc, argv);
410 /* If there is stuff on the command line, run with that. */
414 ret = do_cmd(prog, argc, argv);
420 /* ok, lets enter interactive mode */
423 /* Read a line, continue reading if line ends with \ */
424 for (p = buf, n = sizeof buf, i = 0, first = 1; n > 0; first = 0) {
425 prompt = first ? "openssl : " : "> ";
428 fputs(prompt, stdout);
430 if (!fgets(p, n, stdin))
437 if (p[i - 2] != '\\')
444 extern char *readline(const char *);
445 extern void add_history(const char *cp);
448 char *text = readline(prompt);
454 if (text[i - 1] != '\\') {
455 p += strlen(strcpy(p, text));
462 p += strlen(strcpy(p, text));
469 if (!chopup_args(&arg, buf)) {
470 BIO_printf(bio_err, "Can't parse (no memory?)\n");
474 ret = do_cmd(prog, arg.argc, arg.argv);
475 if (ret == EXIT_THE_PROGRAM) {
480 BIO_printf(bio_err, "error in %s\n", arg.argv[0]);
481 (void)BIO_flush(bio_out);
482 (void)BIO_flush(bio_err);
486 OPENSSL_free(copied_argv);
487 OPENSSL_free(default_config_file);
490 lh_FUNCTION_free(prog);
491 OPENSSL_free(arg.argv);
494 BIO_free_all(bio_out);
496 CRYPTO_mem_leaks(bio_err);
501 OPTIONS exit_options[] = {
505 static void list_cipher_fn(const EVP_CIPHER *c,
506 const char *from, const char *to, void *arg)
509 BIO_printf(arg, "%s\n", EVP_CIPHER_name(c));
512 from = "<undefined>";
515 BIO_printf(arg, "%s => %s\n", from, to);
519 static void list_md_fn(const EVP_MD *m,
520 const char *from, const char *to, void *arg)
523 BIO_printf(arg, "%s\n", EVP_MD_name(m));
526 from = "<undefined>";
529 BIO_printf((BIO *)arg, "%s => %s\n", from, to);
533 /* Unified enum for help and list commands. */
534 typedef enum HELPLIST_CHOICE {
535 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
536 OPT_COMMANDS, OPT_DIGEST_COMMANDS,
537 OPT_DIGEST_ALGORITHMS, OPT_CIPHER_COMMANDS, OPT_CIPHER_ALGORITHMS,
541 OPTIONS list_options[] = {
542 {"help", OPT_HELP, '-', "Display this summary"},
543 {"commands", OPT_COMMANDS, '-', "List of standard commands"},
544 {"digest-commands", OPT_DIGEST_COMMANDS, '-',
545 "List of message digest commands"},
546 {"digest-algorithms", OPT_DIGEST_ALGORITHMS, '-',
547 "List of message digest algorithms"},
548 {"cipher-commands", OPT_CIPHER_COMMANDS, '-', "List of cipher commands"},
549 {"cipher-algorithms", OPT_CIPHER_ALGORITHMS, '-',
550 "List of cipher algorithms"},
551 {"public-key-algorithms", OPT_PK_ALGORITHMS, '-',
552 "List of public key algorithms"},
556 int list_main(int argc, char **argv)
561 prog = opt_init(argc, argv, list_options);
562 while ((o = opt_next()) != OPT_EOF) {
566 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
569 opt_help(list_options);
572 list_type(FT_general);
574 case OPT_DIGEST_COMMANDS:
577 case OPT_DIGEST_ALGORITHMS:
578 EVP_MD_do_all_sorted(list_md_fn, bio_out);
580 case OPT_CIPHER_COMMANDS:
581 list_type(FT_cipher);
583 case OPT_CIPHER_ALGORITHMS:
584 EVP_CIPHER_do_all_sorted(list_cipher_fn, bio_out);
586 case OPT_PK_ALGORITHMS:
595 OPTIONS help_options[] = {
596 {"help", OPT_HELP, '-', "Display this summary"},
600 int help_main(int argc, char **argv)
608 prog = opt_init(argc, argv, help_options);
609 while ((o = opt_next()) != OPT_EOF) {
612 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
615 opt_help(help_options);
619 argc = opt_num_rest();
623 BIO_printf(bio_err, "Usage: %s\n", prog);
627 BIO_printf(bio_err, "\nStandard commands");
630 for (fp = functions; fp->name != NULL; fp++) {
632 if (((i++) % COLUMNS) == 0) {
633 BIO_printf(bio_err, "\n");
636 if (fp->type != tp) {
639 BIO_printf(bio_err, "\n");
643 "\nMessage Digest commands (see the `dgst' command for more details)\n");
644 } else if (tp == FT_cipher) {
647 "\nCipher commands (see the `enc' command for more details)\n");
650 BIO_printf(bio_err, FORMAT, fp->name);
652 BIO_printf(bio_err, "\n\n");
656 int exit_main(int argc, char **argv)
658 return EXIT_THE_PROGRAM;
661 static void list_type(FUNC_TYPE ft)
666 for (fp = functions; fp->name != NULL; fp++)
667 if (fp->type == ft) {
668 if ((i++ % COLUMNS) == 0)
669 BIO_printf(bio_out, "\n");
670 BIO_printf(bio_out, FORMAT, fp->name);
672 BIO_printf(bio_out, "\n");
675 static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[])
679 if (argc <= 0 || argv[0] == NULL)
682 fp = lh_FUNCTION_retrieve(prog, &f);
684 if (EVP_get_digestbyname(argv[0])) {
688 } else if (EVP_get_cipherbyname(argv[0])) {
695 return (fp->func(argc, argv));
697 if ((strncmp(argv[0], "no-", 3)) == 0) {
699 * User is asking if foo is unsupported, by trying to "run" the
700 * no-foo command. Strange.
702 f.name = argv[0] + 3;
703 if (lh_FUNCTION_retrieve(prog, &f) == NULL) {
704 BIO_printf(bio_out, "%s\n", argv[0]);
707 BIO_printf(bio_out, "%s\n", argv[0] + 3);
710 if (strcmp(argv[0], "quit") == 0 || strcmp(argv[0], "q") == 0 ||
711 strcmp(argv[0], "exit") == 0 || strcmp(argv[0], "bye") == 0)
712 /* Special value to mean "exit the program. */
713 return EXIT_THE_PROGRAM;
715 BIO_printf(bio_err, "Invalid command '%s'; type \"help\" for a list.\n",
720 static void list_pkey(void)
724 for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
725 const EVP_PKEY_ASN1_METHOD *ameth;
726 int pkey_id, pkey_base_id, pkey_flags;
727 const char *pinfo, *pem_str;
728 ameth = EVP_PKEY_asn1_get0(i);
729 EVP_PKEY_asn1_get0_info(&pkey_id, &pkey_base_id, &pkey_flags,
730 &pinfo, &pem_str, ameth);
731 if (pkey_flags & ASN1_PKEY_ALIAS) {
732 BIO_printf(bio_out, "Name: %s\n", OBJ_nid2ln(pkey_id));
733 BIO_printf(bio_out, "\tAlias for: %s\n",
734 OBJ_nid2ln(pkey_base_id));
736 BIO_printf(bio_out, "Name: %s\n", pinfo);
737 BIO_printf(bio_out, "\tType: %s Algorithm\n",
738 pkey_flags & ASN1_PKEY_DYNAMIC ?
739 "External" : "Builtin");
740 BIO_printf(bio_out, "\tOID: %s\n", OBJ_nid2ln(pkey_id));
743 BIO_printf(bio_out, "\tPEM string: %s\n", pem_str);
749 static int function_cmp(const FUNCTION * a, const FUNCTION * b)
751 return strncmp(a->name, b->name, 8);
754 static IMPLEMENT_LHASH_COMP_FN(function, FUNCTION)
756 static unsigned long function_hash(const FUNCTION * a)
758 return lh_strhash(a->name);
761 static IMPLEMENT_LHASH_HASH_FN(function, FUNCTION)
763 static int SortFnByName(const void *_f1, const void *_f2)
765 const FUNCTION *f1 = _f1;
766 const FUNCTION *f2 = _f2;
768 if (f1->type != f2->type)
769 return f1->type - f2->type;
770 return strcmp(f1->name, f2->name);
773 static LHASH_OF(FUNCTION) *prog_init(void)
775 LHASH_OF(FUNCTION) *ret;
779 /* Sort alphabetically within category. For nicer help displays. */
780 for (i = 0, f = functions; f->name != NULL; ++f, ++i) ;
781 qsort(functions, i, sizeof(*functions), SortFnByName);
783 if ((ret = lh_FUNCTION_new()) == NULL)
786 for (f = functions; f->name != NULL; f++)
787 (void)lh_FUNCTION_insert(ret, f);