f2c7ccf0c481d636083d0efc9a50fc86ea3e0311
[openssl.git] / apps / openssl.c
1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2  * All rights reserved.
3  *
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.
7  *
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).
14  *
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.
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions
24  * are met:
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)"
39  *
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
50  * SUCH DAMAGE.
51  *
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.]
56  */
57 /* ====================================================================
58  * Copyright (c) 1998-2006 The OpenSSL Project.  All rights reserved.
59  *
60  * Redistribution and use in source and binary forms, with or without
61  * modification, are permitted provided that the following conditions
62  * are met:
63  *
64  * 1. Redistributions of source code must retain the above copyright
65  *    notice, this list of conditions and the following disclaimer.
66  *
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
70  *    distribution.
71  *
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/)"
76  *
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.
81  *
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.
85  *
86  * 6. Redistributions of any form whatsoever must retain the following
87  *    acknowledgment:
88  *    "This product includes software developed by the OpenSSL Project
89  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90  *
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  * ====================================================================
104  *
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).
108  *
109  */
110
111 #include <stdio.h>
112 #include <string.h>
113 #include <stdlib.h>
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>
124 #endif
125 #include <openssl/err.h>
126 #ifdef OPENSSL_FIPS
127 # include <openssl/fips.h>
128 #endif
129 #define USE_SOCKETS /* needed for the _O_BINARY defs in the MS world */
130 #include "s_apps.h"
131 /* Needed to get the other O_xxx flags. */
132 #ifdef OPENSSL_SYS_VMS
133 # include <unixio.h>
134 #endif
135 #define INCLUDE_FUNCTION_TABLE
136 #include "apps.h"
137
138
139 #ifdef OPENSSL_NO_CAMELLIA
140 # define FORMAT "%-15s"
141 # define COLUMNS 5
142 #else
143 # define FORMAT "%-18s"
144 # define COLUMNS 4
145 #endif
146
147 /* Special sentinel to exit the program. */
148 #define EXIT_THE_PROGRAM (-1)
149
150 /*
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.
155  */
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;
163
164 static CONF *config = NULL;
165 BIO *bio_in = NULL;
166 BIO *bio_out = NULL;
167 BIO *bio_err = NULL;
168
169 static int apps_startup()
170 {
171 #ifdef SIGPIPE
172     signal(SIGPIPE, SIG_IGN);
173 #endif
174     CRYPTO_malloc_init();
175     ERR_load_crypto_strings();
176     ERR_load_SSL_strings();
177
178     OPENSSL_load_builtin_modules();
179     SSL_add_ssl_module();
180 #ifndef OPENSSL_NO_ENGINE
181     ENGINE_load_builtin_engines();
182 #endif
183     if (!app_load_modules(NULL)) {
184         ERR_print_errors(bio_err);
185         BIO_printf(bio_err, "Error loading default configuration\n");
186         return 0;
187     }
188
189     OpenSSL_add_all_algorithms();
190     OpenSSL_add_ssl_algorithms();
191     setup_ui_method();
192     /*SSL_library_init();*/
193     return 1;
194 }
195
196 static void apps_shutdown()
197 {
198 #ifndef OPENSSL_NO_ENGINE
199     ENGINE_cleanup();
200 #endif
201     destroy_ui_method();
202     CONF_modules_unload(1);
203 #ifndef OPENSSL_NO_COMP
204     COMP_zlib_cleanup();
205     SSL_COMP_free_compression_methods();
206 #endif
207     OBJ_cleanup();
208     EVP_cleanup();
209     CRYPTO_cleanup_all_ex_data();
210     ERR_remove_thread_state(NULL);
211     RAND_cleanup();
212     ERR_free_strings();
213 }
214
215 static char *make_config_name()
216 {
217     const char *t;
218     size_t len;
219     char *p;
220
221     if ((t = getenv("OPENSSL_CONF")) != NULL)
222         return OPENSSL_strdup(t);
223
224     t = X509_get_default_cert_area();
225     len = strlen(t) + 1 + strlen(OPENSSL_CONF) + 1;
226     p = app_malloc(len, "config filename buffer");
227     strcpy(p, t);
228 #ifndef OPENSSL_SYS_VMS
229     strcat(p, "/");
230 #endif
231     strcat(p, OPENSSL_CONF);
232
233     return p;
234 }
235
236 static void lock_dbg_cb(int mode, int type, const char *file, int line)
237 {
238     static int modes[CRYPTO_NUM_LOCKS];
239     const char *errstr = NULL;
240     int rw = mode & (CRYPTO_READ | CRYPTO_WRITE);
241
242     if (rw != CRYPTO_READ && rw != CRYPTO_WRITE) {
243         errstr = "invalid mode";
244         goto err;
245     }
246
247     if (type < 0 || type >= CRYPTO_NUM_LOCKS) {
248         errstr = "type out of bounds";
249         goto err;
250     }
251
252     if (mode & CRYPTO_LOCK) {
253         if (modes[type]) {
254             errstr = "already locked";
255             /* must not happen in a single-threaded program --> deadlock! */
256             goto err;
257         }
258         modes[type] = rw;
259     } else if (mode & CRYPTO_UNLOCK) {
260         if (!modes[type]) {
261             errstr = "not locked";
262             goto err;
263         }
264
265         if (modes[type] != rw) {
266             errstr = (rw == CRYPTO_READ) ?
267                 "CRYPTO_r_unlock on write lock" :
268                 "CRYPTO_w_unlock on read lock";
269         }
270
271         modes[type] = 0;
272     } else {
273         errstr = "invalid mode";
274         goto err;
275     }
276
277  err:
278     if (errstr) {
279         BIO_printf(bio_err,
280                    "openssl (lock_dbg_cb): %s (mode=%d, type=%d) at %s:%d\n",
281                    errstr, mode, type, file, line);
282     }
283 }
284
285 #if defined( OPENSSL_SYS_VMS)
286 extern char **copy_argv(int *argc, char **argv);
287 #endif
288
289 int main(int argc, char *argv[])
290 {
291     FUNCTION f, *fp;
292     LHASH_OF(FUNCTION) *prog = NULL;
293     char **copied_argv = NULL;
294     char *p, *pname;
295     char buf[1024];
296     const char *prompt;
297     ARGS arg;
298     int first, n, i, ret = 0;
299
300     arg.argv = NULL;
301     arg.size = 0;
302
303     /* Set up some of the environment. */
304     default_config_file = make_config_name();
305     bio_in = dup_bio_in(FORMAT_TEXT);
306     bio_out = dup_bio_out(FORMAT_TEXT);
307     bio_err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
308
309 #if defined( OPENSSL_SYS_VMS)
310     copied_argv = argv = copy_argv(&argc, argv);
311 #endif
312
313     p = getenv("OPENSSL_DEBUG_MEMORY");
314     if (p == NULL)
315         /* if not set, use compiled-in default */
316         ;
317     else if (strcmp(p, "off") != 0) {
318         CRYPTO_malloc_debug_init();
319         CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
320     } else {
321         CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
322     }
323     CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
324     CRYPTO_set_locking_callback(lock_dbg_cb);
325
326     if (getenv("OPENSSL_FIPS")) {
327 #ifdef OPENSSL_FIPS
328         if (!FIPS_mode_set(1)) {
329             ERR_load_crypto_strings();
330             ERR_print_errors(bio_err);
331             return 1;
332         }
333 #else
334         BIO_printf(bio_err, "FIPS mode not supported.\n");
335         return 1;
336 #endif
337     }
338
339     if (!apps_startup())
340         goto end;
341
342     prog = prog_init();
343     pname = opt_progname(argv[0]);
344
345     /* first check the program name */
346     f.name = pname;
347     fp = lh_FUNCTION_retrieve(prog, &f);
348     if (fp != NULL) {
349         argv[0] = pname;
350         ret = fp->func(argc, argv);
351         goto end;
352     }
353
354     /* If there is stuff on the command line, run with that. */
355     if (argc != 1) {
356         argc--;
357         argv++;
358         ret = do_cmd(prog, argc, argv);
359         if (ret < 0)
360             ret = 0;
361         goto end;
362     }
363
364     /* ok, lets enter interactive mode */
365     for (;;) {
366         ret = 0;
367         /* Read a line, continue reading if line ends with \ */
368         for (p = buf, n = sizeof buf, i = 0, first = 1; n > 0; first = 0) {
369             prompt = first ? "OpenSSL> " : "> ";
370             p[0] = '\0';
371 #ifndef READLINE
372             fputs(prompt, stdout);
373             fflush(stdout);
374             if (!fgets(p, n, stdin))
375                 goto end;
376             if (p[0] == '\0')
377                 goto end;
378             i = strlen(p);
379             if (i <= 1)
380                 break;
381             if (p[i - 2] != '\\')
382                 break;
383             i -= 2;
384             p += i;
385             n -= i;
386 #else
387             {
388                 extern char *readline(const char *);
389                 extern void add_history(const char *cp);
390                 char *text;
391
392                 char *text = readline(prompt);
393                 if (text == NULL)
394                     goto end;
395                 i = strlen(text);
396                 if (i == 0 || i > n)
397                     break;
398                 if (text[i - 1] != '\\') {
399                     p += strlen(strcpy(p, text));
400                     free(text);
401                     add_history(buf);
402                     break;
403                 }
404
405                 text[i - 1] = '\0';
406                 p += strlen(strcpy(p, text));
407                 free(text);
408                 n -= i;
409             }
410 #endif
411         }
412
413         if (!chopup_args(&arg, buf)) {
414             BIO_printf(bio_err, "Can't parse (no memory?)\n");
415             break;
416         }
417
418         ret = do_cmd(prog, arg.argc, arg.argv);
419         if (ret == EXIT_THE_PROGRAM) {
420             ret = 0;
421             goto end;
422         }
423         if (ret != 0)
424             BIO_printf(bio_err, "error in %s\n", arg.argv[0]);
425         (void)BIO_flush(bio_out);
426         (void)BIO_flush(bio_err);
427     }
428     ret = 1;
429  end:
430     OPENSSL_free(copied_argv);
431     OPENSSL_free(default_config_file);
432     NCONF_free(config);
433     config = NULL;
434     lh_FUNCTION_free(prog);
435     OPENSSL_free(arg.argv);
436
437     BIO_free(bio_in);
438     BIO_free_all(bio_out);
439     apps_shutdown();
440     CRYPTO_mem_leaks(bio_err);
441     BIO_free(bio_err);
442     return (ret);
443 }
444
445 OPTIONS exit_options[] = {
446     {NULL}
447 };
448
449 static void list_cipher_fn(const EVP_CIPHER *c,
450                            const char *from, const char *to, void *arg)
451 {
452     if (c)
453         BIO_printf(arg, "%s\n", EVP_CIPHER_name(c));
454     else {
455         if (!from)
456             from = "<undefined>";
457         if (!to)
458             to = "<undefined>";
459         BIO_printf(arg, "%s => %s\n", from, to);
460     }
461 }
462
463 static void list_md_fn(const EVP_MD *m,
464                        const char *from, const char *to, void *arg)
465 {
466     if (m)
467         BIO_printf(arg, "%s\n", EVP_MD_name(m));
468     else {
469         if (!from)
470             from = "<undefined>";
471         if (!to)
472             to = "<undefined>";
473         BIO_printf((BIO *)arg, "%s => %s\n", from, to);
474     }
475 }
476
477 /* Unified enum for help and list commands. */
478 typedef enum HELPLIST_CHOICE {
479     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
480     OPT_COMMANDS, OPT_DIGEST_COMMANDS,
481     OPT_DIGEST_ALGORITHMS, OPT_CIPHER_COMMANDS, OPT_CIPHER_ALGORITHMS,
482     OPT_PK_ALGORITHMS, OPT_DISABLED
483 } HELPLIST_CHOICE;
484
485 OPTIONS list_options[] = {
486     {"help", OPT_HELP, '-', "Display this summary"},
487     {"commands", OPT_COMMANDS, '-', "List of standard commands"},
488     {"digest-commands", OPT_DIGEST_COMMANDS, '-',
489      "List of message digest commands"},
490     {"digest-algorithms", OPT_DIGEST_ALGORITHMS, '-',
491      "List of message digest algorithms"},
492     {"cipher-commands", OPT_CIPHER_COMMANDS, '-', "List of cipher commands"},
493     {"cipher-algorithms", OPT_CIPHER_ALGORITHMS, '-',
494      "List of cipher algorithms"},
495     {"public-key-algorithms", OPT_PK_ALGORITHMS, '-',
496      "List of public key algorithms"},
497     {"disabled", OPT_DISABLED, '-',
498      "List of disabled features"},
499     {NULL}
500 };
501
502 int list_main(int argc, char **argv)
503 {
504     char *prog;
505     HELPLIST_CHOICE o;
506     int done = 0;
507
508     prog = opt_init(argc, argv, list_options);
509     while ((o = opt_next()) != OPT_EOF) {
510         switch (o) {
511         case OPT_EOF:  /* Never hit, but suppresses warning */
512         case OPT_ERR:
513             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
514             return 1;
515         case OPT_HELP:
516             opt_help(list_options);
517             break;
518         case OPT_COMMANDS:
519             list_type(FT_general);
520             break;
521         case OPT_DIGEST_COMMANDS:
522             list_type(FT_md);
523             break;
524         case OPT_DIGEST_ALGORITHMS:
525             EVP_MD_do_all_sorted(list_md_fn, bio_out);
526             break;
527         case OPT_CIPHER_COMMANDS:
528             list_type(FT_cipher);
529             break;
530         case OPT_CIPHER_ALGORITHMS:
531             EVP_CIPHER_do_all_sorted(list_cipher_fn, bio_out);
532             break;
533         case OPT_PK_ALGORITHMS:
534             list_pkey();
535             break;
536         case OPT_DISABLED:
537             list_disabled();
538             break;
539         }
540         done = 1;
541     }
542
543     if (!done) {
544         BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
545         return 1;
546     }
547
548     return 0;
549 }
550
551 OPTIONS help_options[] = {
552     {"help", OPT_HELP, '-', "Display this summary"},
553     {NULL}
554 };
555
556 int help_main(int argc, char **argv)
557 {
558     FUNCTION *fp;
559     int i, nl;
560     FUNC_TYPE tp;
561     char *prog;
562     HELPLIST_CHOICE o;
563
564     prog = opt_init(argc, argv, help_options);
565     while ((o = opt_next()) != OPT_EOF) {
566         switch (o) {
567         default:
568             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
569             return 1;
570         case OPT_HELP:
571             opt_help(help_options);
572             return 0;
573         }
574     }
575     argc = opt_num_rest();
576     argv = opt_rest();
577
578     if (argc != 0) {
579         BIO_printf(bio_err, "Usage: %s\n", prog);
580         return 1;
581     }
582
583     BIO_printf(bio_err, "\nStandard commands");
584     i = 0;
585     tp = FT_none;
586     for (fp = functions; fp->name != NULL; fp++) {
587         nl = 0;
588         if (((i++) % COLUMNS) == 0) {
589             BIO_printf(bio_err, "\n");
590             nl = 1;
591         }
592         if (fp->type != tp) {
593             tp = fp->type;
594             if (!nl)
595                 BIO_printf(bio_err, "\n");
596             if (tp == FT_md) {
597                 i = 1;
598                 BIO_printf(bio_err,
599                            "\nMessage Digest commands (see the `dgst' command for more details)\n");
600             } else if (tp == FT_cipher) {
601                 i = 1;
602                 BIO_printf(bio_err,
603                            "\nCipher commands (see the `enc' command for more details)\n");
604             }
605         }
606         BIO_printf(bio_err, FORMAT, fp->name);
607     }
608     BIO_printf(bio_err, "\n\n");
609     return 0;
610 }
611
612 int exit_main(int argc, char **argv)
613 {
614     return EXIT_THE_PROGRAM;
615 }
616
617 static void list_type(FUNC_TYPE ft)
618 {
619     FUNCTION *fp;
620     int i = 0;
621
622     for (fp = functions; fp->name != NULL; fp++)
623         if (fp->type == ft) {
624             if ((i++ % COLUMNS) == 0)
625                 BIO_printf(bio_out, "\n");
626             BIO_printf(bio_out, FORMAT, fp->name);
627         }
628     BIO_printf(bio_out, "\n");
629 }
630
631 static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[])
632 {
633     FUNCTION f, *fp;
634
635     if (argc <= 0 || argv[0] == NULL)
636         return (0);
637     f.name = argv[0];
638     fp = lh_FUNCTION_retrieve(prog, &f);
639     if (fp == NULL) {
640         if (EVP_get_digestbyname(argv[0])) {
641             f.type = FT_md;
642             f.func = dgst_main;
643             fp = &f;
644         } else if (EVP_get_cipherbyname(argv[0])) {
645             f.type = FT_cipher;
646             f.func = enc_main;
647             fp = &f;
648         }
649     }
650     if (fp != NULL) {
651         return (fp->func(argc, argv));
652     }
653     if ((strncmp(argv[0], "no-", 3)) == 0) {
654         /*
655          * User is asking if foo is unsupported, by trying to "run" the
656          * no-foo command.  Strange.
657          */
658         f.name = argv[0] + 3;
659         if (lh_FUNCTION_retrieve(prog, &f) == NULL) {
660             BIO_printf(bio_out, "%s\n", argv[0]);
661             return (0);
662         }
663         BIO_printf(bio_out, "%s\n", argv[0] + 3);
664         return 1;
665     }
666     if (strcmp(argv[0], "quit") == 0 || strcmp(argv[0], "q") == 0 ||
667         strcmp(argv[0], "exit") == 0 || strcmp(argv[0], "bye") == 0)
668         /* Special value to mean "exit the program. */
669         return EXIT_THE_PROGRAM;
670
671     BIO_printf(bio_err, "Invalid command '%s'; type \"help\" for a list.\n",
672                argv[0]);
673     return (1);
674 }
675
676 static void list_pkey(void)
677 {
678     int i;
679
680     for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
681         const EVP_PKEY_ASN1_METHOD *ameth;
682         int pkey_id, pkey_base_id, pkey_flags;
683         const char *pinfo, *pem_str;
684         ameth = EVP_PKEY_asn1_get0(i);
685         EVP_PKEY_asn1_get0_info(&pkey_id, &pkey_base_id, &pkey_flags,
686                                 &pinfo, &pem_str, ameth);
687         if (pkey_flags & ASN1_PKEY_ALIAS) {
688             BIO_printf(bio_out, "Name: %s\n", OBJ_nid2ln(pkey_id));
689             BIO_printf(bio_out, "\tAlias for: %s\n",
690                        OBJ_nid2ln(pkey_base_id));
691         } else {
692             BIO_printf(bio_out, "Name: %s\n", pinfo);
693             BIO_printf(bio_out, "\tType: %s Algorithm\n",
694                        pkey_flags & ASN1_PKEY_DYNAMIC ?
695                        "External" : "Builtin");
696             BIO_printf(bio_out, "\tOID: %s\n", OBJ_nid2ln(pkey_id));
697             if (pem_str == NULL)
698                 pem_str = "(none)";
699             BIO_printf(bio_out, "\tPEM string: %s\n", pem_str);
700         }
701
702     }
703 }
704
705 static int function_cmp(const FUNCTION * a, const FUNCTION * b)
706 {
707     return strncmp(a->name, b->name, 8);
708 }
709
710 static IMPLEMENT_LHASH_COMP_FN(function, FUNCTION)
711
712 static unsigned long function_hash(const FUNCTION * a)
713 {
714     return lh_strhash(a->name);
715 }
716
717 static IMPLEMENT_LHASH_HASH_FN(function, FUNCTION)
718
719 static int SortFnByName(const void *_f1, const void *_f2)
720 {
721     const FUNCTION *f1 = _f1;
722     const FUNCTION *f2 = _f2;
723
724     if (f1->type != f2->type)
725         return f1->type - f2->type;
726     return strcmp(f1->name, f2->name);
727 }
728
729 static void list_disabled(void)
730 {
731     BIO_puts(bio_out, "Disabled algorithms:\n");
732 #ifdef OPENSSL_NO_AES
733     BIO_puts(bio_out, "AES\n");
734 #endif
735 #ifdef OPENSSL_NO_BF
736     BIO_puts(bio_out, "BF\n");
737 #endif
738 #ifdef OPENSSL_NO_CAMELLIA
739     BIO_puts(bio_out, "CAMELLIA\n");
740 #endif
741 #ifdef OPENSSL_NO_CAST
742     BIO_puts(bio_out, "CAST\n");
743 #endif
744 #ifdef OPENSSL_NO_CMAC
745     BIO_puts(bio_out, "CMAC\n");
746 #endif
747 #ifdef OPENSSL_NO_CMS
748     BIO_puts(bio_out, "CMS\n");
749 #endif
750 #ifdef OPENSSL_NO_COMP
751     BIO_puts(bio_out, "COMP\n");
752 #endif
753 #ifdef OPENSSL_NO_DES
754     BIO_puts(bio_out, "DES\n");
755 #endif
756 #ifdef OPENSSL_NO_DGRAM
757     BIO_puts(bio_out, "DGRAM\n");
758 #endif
759 #ifdef OPENSSL_NO_DH
760     BIO_puts(bio_out, "DH\n");
761 #endif
762 #ifdef OPENSSL_NO_DSA
763     BIO_puts(bio_out, "DSA\n");
764 #endif
765 #if defined(OPENSSL_NO_DTLS)
766     BIO_puts(bio_out, "DTLS\n");
767 #endif
768 #ifdef OPENSSL_NO_EC
769     BIO_puts(bio_out, "EC\n");
770 #endif
771 #ifdef OPENSSL_NO_EC2M
772     BIO_puts(bio_out, "EC2M\n");
773 #endif
774 #ifdef OPENSSL_NO_ENGINE
775     BIO_puts(bio_out, "ENGINE\n");
776 #endif
777 #ifdef OPENSSL_NO_GOST
778     BIO_puts(bio_out, "GOST\n");
779 #endif
780 #ifdef OPENSSL_NO_HMAC
781     BIO_puts(bio_out, "HMAC\n");
782 #endif
783 #ifdef OPENSSL_NO_IDEA
784     BIO_puts(bio_out, "IDEA\n");
785 #endif
786 #ifdef OPENSSL_NO_JPAKE
787     BIO_puts(bio_out, "JPAKE\n");
788 #endif
789 #ifdef OPENSSL_NO_MD2
790     BIO_puts(bio_out, "MD2\n");
791 #endif
792 #ifdef OPENSSL_NO_MD4
793     BIO_puts(bio_out, "MD4\n");
794 #endif
795 #ifdef OPENSSL_NO_MD5
796     BIO_puts(bio_out, "MD5\n");
797 #endif
798 #ifdef OPENSSL_NO_MDC2
799     BIO_puts(bio_out, "MDC2\n");
800 #endif
801 #ifdef OPENSSL_NO_OCB
802     BIO_puts(bio_out, "OCB\n");
803 #endif
804 #ifdef OPENSSL_NO_OCSP
805     BIO_puts(bio_out, "OCSP\n");
806 #endif
807 #ifdef OPENSSL_NO_PSK
808     BIO_puts(bio_out, "PSK\n");
809 #endif
810 #ifdef OPENSSL_NO_RC2
811     BIO_puts(bio_out, "RC2\n");
812 #endif
813 #ifdef OPENSSL_NO_RC4
814     BIO_puts(bio_out, "RC4\n");
815 #endif
816 #ifdef OPENSSL_NO_RC5
817     BIO_puts(bio_out, "RC5\n");
818 #endif
819 #ifdef OPENSSL_NO_RMD160
820     BIO_puts(bio_out, "RMD160\n");
821 #endif
822 #ifdef OPENSSL_NO_RSA
823     BIO_puts(bio_out, "RSA\n");
824 #endif
825 #ifdef OPENSSL_NO_SCRYPT
826     BIO_puts(bio_out, "SCRYPT\n");
827 #endif
828 #ifdef OPENSSL_NO_SCT
829     BIO_puts(bio_out, "SCT\n");
830 #endif
831 #ifdef OPENSSL_NO_SCTP
832     BIO_puts(bio_out, "SCTP\n");
833 #endif
834 #ifdef OPENSSL_NO_SEED
835     BIO_puts(bio_out, "SEED\n");
836 #endif
837 #ifdef OPENSSL_NO_SHA
838     BIO_puts(bio_out, "SHA\n");
839 #endif
840 #ifdef OPENSSL_NO_SOCK
841     BIO_puts(bio_out, "SOCK\n");
842 #endif
843 #ifdef OPENSSL_NO_SRP
844     BIO_puts(bio_out, "SRP\n");
845 #endif
846 #ifdef OPENSSL_NO_SRTP
847     BIO_puts(bio_out, "SRTP\n");
848 #endif
849 #ifdef OPENSSL_NO_SSL3
850     BIO_puts(bio_out, "SSL3\n");
851 #endif
852 #ifdef OPENSSL_NO_WHIRLPOOL
853     BIO_puts(bio_out, "WHIRLPOOL\n");
854 #endif
855 #ifndef ZLIB
856     BIO_puts(bio_out, "ZLIB\n");
857 #endif
858 }
859
860 static LHASH_OF(FUNCTION) *prog_init(void)
861 {
862     LHASH_OF(FUNCTION) *ret;
863     FUNCTION *f;
864     size_t i;
865
866     /* Sort alphabetically within category. For nicer help displays. */
867     for (i = 0, f = functions; f->name != NULL; ++f, ++i) ;
868     qsort(functions, i, sizeof(*functions), SortFnByName);
869
870     if ((ret = lh_FUNCTION_new()) == NULL)
871         return (NULL);
872
873     for (f = functions; f->name != NULL; f++)
874         (void)lh_FUNCTION_insert(ret, f);
875     return (ret);
876 }