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