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