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