58a2d0f700947f2fcf7db31e905907c6fc4dca8b
[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 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 void apps_startup()
169 {
170 #ifdef SIGPIPE
171     signal(SIGPIPE, SIG_IGN);
172 #endif
173     CRYPTO_malloc_init();
174     ERR_load_crypto_strings();
175     ERR_load_SSL_strings();
176     OpenSSL_add_all_algorithms();
177     OpenSSL_add_ssl_algorithms();
178     OPENSSL_load_builtin_modules();
179     setup_ui_method();
180     /*SSL_library_init();*/
181 #ifndef OPENSSL_NO_ENGINE
182     ENGINE_load_builtin_engines();
183 #endif
184 }
185
186 static void apps_shutdown()
187 {
188 #ifndef OPENSSL_NO_ENGINE
189     ENGINE_cleanup();
190 #endif
191     destroy_ui_method();
192     CONF_modules_unload(1);
193 #ifndef OPENSSL_NO_COMP
194     COMP_zlib_cleanup();
195     SSL_COMP_free_compression_methods();
196 #endif
197     OBJ_cleanup();
198     EVP_cleanup();
199     CRYPTO_cleanup_all_ex_data();
200     ERR_remove_thread_state(NULL);
201     RAND_cleanup();
202     ERR_free_strings();
203 }
204
205 static char *make_config_name()
206 {
207     const char *t;
208     size_t len;
209     char *p;
210
211     if ((t = getenv("OPENSSL_CONF")) != NULL
212         || (t = getenv("SSLEAY_CONF")) != NULL)
213         return BUF_strdup(t);
214
215     t = X509_get_default_cert_area();
216     len = strlen(t) + 1 + strlen(OPENSSL_CONF) + 1;
217     p = app_malloc(len, "config filename buffer");
218     strcpy(p, t);
219 #ifndef OPENSSL_SYS_VMS
220     strcat(p, "/");
221 #endif
222     strcat(p, OPENSSL_CONF);
223
224     return p;
225 }
226
227 static void lock_dbg_cb(int mode, int type, const char *file, int line)
228 {
229     static int modes[CRYPTO_NUM_LOCKS];
230     const char *errstr = NULL;
231     int rw = mode & (CRYPTO_READ | CRYPTO_WRITE);
232
233     if (rw != CRYPTO_READ && rw != CRYPTO_WRITE) {
234         errstr = "invalid mode";
235         goto err;
236     }
237
238     if (type < 0 || type >= CRYPTO_NUM_LOCKS) {
239         errstr = "type out of bounds";
240         goto err;
241     }
242
243     if (mode & CRYPTO_LOCK) {
244         if (modes[type]) {
245             errstr = "already locked";
246             /* must not happen in a single-threaded program --> deadlock! */
247             goto err;
248         }
249         modes[type] = rw;
250     } else if (mode & CRYPTO_UNLOCK) {
251         if (!modes[type]) {
252             errstr = "not locked";
253             goto err;
254         }
255
256         if (modes[type] != rw) {
257             errstr = (rw == CRYPTO_READ) ?
258                 "CRYPTO_r_unlock on write lock" :
259                 "CRYPTO_w_unlock on read lock";
260         }
261
262         modes[type] = 0;
263     } else {
264         errstr = "invalid mode";
265         goto err;
266     }
267
268  err:
269     if (errstr) {
270         BIO_printf(bio_err,
271                    "openssl (lock_dbg_cb): %s (mode=%d, type=%d) at %s:%d\n",
272                    errstr, mode, type, file, line);
273     }
274 }
275
276 #if defined( OPENSSL_SYS_VMS)
277 extern char **copy_argv(int *argc, char **argv);
278 #endif
279
280 int main(int argc, char *argv[])
281 {
282     FUNCTION f, *fp;
283     LHASH_OF(FUNCTION) *prog = NULL;
284     char **copied_argv = NULL;
285     char *p, *pname;
286     char buf[1024];
287     const char *prompt;
288     ARGS arg;
289     int first, n, i, ret = 0;
290
291     arg.argv = NULL;
292     arg.size = 0;
293
294     /* Set up some of the environment. */
295     default_config_file = make_config_name();
296     bio_in = dup_bio_in(FORMAT_TEXT);
297     bio_out = dup_bio_out(FORMAT_TEXT);
298     bio_err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
299
300 #if defined( OPENSSL_SYS_VMS)
301     copied_argv = argv = copy_argv(&argc, argv);
302 #endif
303
304     p = getenv("OPENSSL_DEBUG_MEMORY");
305     if (p == NULL)
306         /* if not set, use compiled-in default */
307         ;
308     else if (strcmp(p, "off") != 0) {
309         CRYPTO_malloc_debug_init();
310         CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
311     } else {
312         CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
313     }
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     apps_startup();
331
332     /*
333      * If first argument is a colon, skip it.  Because in "interactive"
334      * mode our prompt is a colon and we can cut/paste whole lines
335      * by doing this hack.
336      */
337     if (argv[1] && strcmp(argv[1], ":") == 0) {
338         argv[1] = argv[0];
339         argc--;
340         argv++;
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
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     {NULL}
498 };
499
500 int list_main(int argc, char **argv)
501 {
502     char *prog;
503     HELPLIST_CHOICE o;
504
505     prog = opt_init(argc, argv, list_options);
506     while ((o = opt_next()) != OPT_EOF) {
507         switch (o) {
508         case OPT_EOF:
509         case OPT_ERR:
510             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
511             return 1;
512         case OPT_HELP:
513             opt_help(list_options);
514             break;
515         case OPT_COMMANDS:
516             list_type(FT_general);
517             break;
518         case OPT_DIGEST_COMMANDS:
519             list_type(FT_md);
520             break;
521         case OPT_DIGEST_ALGORITHMS:
522             EVP_MD_do_all_sorted(list_md_fn, bio_out);
523             break;
524         case OPT_CIPHER_COMMANDS:
525             list_type(FT_cipher);
526             break;
527         case OPT_CIPHER_ALGORITHMS:
528             EVP_CIPHER_do_all_sorted(list_cipher_fn, bio_out);
529             break;
530         case OPT_PK_ALGORITHMS:
531             list_pkey();
532             break;
533         }
534     }
535
536     return 0;
537 }
538
539 OPTIONS help_options[] = {
540     {"help", OPT_HELP, '-', "Display this summary"},
541     {NULL}
542 };
543
544 int help_main(int argc, char **argv)
545 {
546     FUNCTION *fp;
547     int i, nl;
548     FUNC_TYPE tp;
549     char *prog;
550     HELPLIST_CHOICE o;
551
552     prog = opt_init(argc, argv, help_options);
553     while ((o = opt_next()) != OPT_EOF) {
554         switch (o) {
555         default:
556             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
557             return 1;
558         case OPT_HELP:
559             opt_help(help_options);
560             return 0;
561         }
562     }
563     argc = opt_num_rest();
564     argv = opt_rest();
565
566     if (argc != 0) {
567         BIO_printf(bio_err, "Usage: %s\n", prog);
568         return 1;
569     }
570
571     BIO_printf(bio_err, "\nStandard commands");
572     i = 0;
573     tp = FT_none;
574     for (fp = functions; fp->name != NULL; fp++) {
575         nl = 0;
576         if (((i++) % COLUMNS) == 0) {
577             BIO_printf(bio_err, "\n");
578             nl = 1;
579         }
580         if (fp->type != tp) {
581             tp = fp->type;
582             if (!nl)
583                 BIO_printf(bio_err, "\n");
584             if (tp == FT_md) {
585                 i = 1;
586                 BIO_printf(bio_err,
587                            "\nMessage Digest commands (see the `dgst' command for more details)\n");
588             } else if (tp == FT_cipher) {
589                 i = 1;
590                 BIO_printf(bio_err,
591                            "\nCipher commands (see the `enc' command for more details)\n");
592             }
593         }
594         BIO_printf(bio_err, FORMAT, fp->name);
595     }
596     BIO_printf(bio_err, "\n\n");
597     return 0;
598 }
599
600 int exit_main(int argc, char **argv)
601 {
602     return EXIT_THE_PROGRAM;
603 }
604
605 static void list_type(FUNC_TYPE ft)
606 {
607     FUNCTION *fp;
608     int i = 0;
609
610     for (fp = functions; fp->name != NULL; fp++)
611         if (fp->type == ft) {
612             if ((i++ % COLUMNS) == 0)
613                 BIO_printf(bio_out, "\n");
614             BIO_printf(bio_out, FORMAT, fp->name);
615         }
616     BIO_printf(bio_out, "\n");
617 }
618
619 static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[])
620 {
621     FUNCTION f, *fp;
622
623     if (argc <= 0 || argv[0] == NULL)
624         return (0);
625     f.name = argv[0];
626     fp = lh_FUNCTION_retrieve(prog, &f);
627     if (fp == NULL) {
628         if (EVP_get_digestbyname(argv[0])) {
629             f.type = FT_md;
630             f.func = dgst_main;
631             fp = &f;
632         } else if (EVP_get_cipherbyname(argv[0])) {
633             f.type = FT_cipher;
634             f.func = enc_main;
635             fp = &f;
636         }
637     }
638     if (fp != NULL) {
639         return (fp->func(argc, argv));
640     }
641     if ((strncmp(argv[0], "no-", 3)) == 0) {
642         /*
643          * User is asking if foo is unsupported, by trying to "run" the
644          * no-foo command.  Strange.
645          */
646         f.name = argv[0] + 3;
647         if (lh_FUNCTION_retrieve(prog, &f) == NULL) {
648             BIO_printf(bio_out, "%s\n", argv[0]);
649             return (0);
650         }
651         BIO_printf(bio_out, "%s\n", argv[0] + 3);
652         return 1;
653     }
654     if (strcmp(argv[0], "quit") == 0 || strcmp(argv[0], "q") == 0 ||
655         strcmp(argv[0], "exit") == 0 || strcmp(argv[0], "bye") == 0)
656         /* Special value to mean "exit the program. */
657         return EXIT_THE_PROGRAM;
658
659     BIO_printf(bio_err, "Invalid command '%s'; type \"help\" for a list.\n",
660                argv[0]);
661     return (1);
662 }
663
664 static void list_pkey(void)
665 {
666     int i;
667
668     for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
669         const EVP_PKEY_ASN1_METHOD *ameth;
670         int pkey_id, pkey_base_id, pkey_flags;
671         const char *pinfo, *pem_str;
672         ameth = EVP_PKEY_asn1_get0(i);
673         EVP_PKEY_asn1_get0_info(&pkey_id, &pkey_base_id, &pkey_flags,
674                                 &pinfo, &pem_str, ameth);
675         if (pkey_flags & ASN1_PKEY_ALIAS) {
676             BIO_printf(bio_out, "Name: %s\n", OBJ_nid2ln(pkey_id));
677             BIO_printf(bio_out, "\tAlias for: %s\n",
678                        OBJ_nid2ln(pkey_base_id));
679         } else {
680             BIO_printf(bio_out, "Name: %s\n", pinfo);
681             BIO_printf(bio_out, "\tType: %s Algorithm\n",
682                        pkey_flags & ASN1_PKEY_DYNAMIC ?
683                        "External" : "Builtin");
684             BIO_printf(bio_out, "\tOID: %s\n", OBJ_nid2ln(pkey_id));
685             if (pem_str == NULL)
686                 pem_str = "(none)";
687             BIO_printf(bio_out, "\tPEM string: %s\n", pem_str);
688         }
689
690     }
691 }
692
693 static int function_cmp(const FUNCTION * a, const FUNCTION * b)
694 {
695     return strncmp(a->name, b->name, 8);
696 }
697
698 static IMPLEMENT_LHASH_COMP_FN(function, FUNCTION)
699
700 static unsigned long function_hash(const FUNCTION * a)
701 {
702     return lh_strhash(a->name);
703 }
704
705 static IMPLEMENT_LHASH_HASH_FN(function, FUNCTION)
706
707 static int SortFnByName(const void *_f1, const void *_f2)
708 {
709     const FUNCTION *f1 = _f1;
710     const FUNCTION *f2 = _f2;
711
712     if (f1->type != f2->type)
713         return f1->type - f2->type;
714     return strcmp(f1->name, f2->name);
715 }
716
717 static LHASH_OF(FUNCTION) *prog_init(void)
718 {
719     LHASH_OF(FUNCTION) *ret;
720     FUNCTION *f;
721     size_t i;
722
723     /* Sort alphabetically within category. For nicer help displays. */
724     for (i = 0, f = functions; f->name != NULL; ++f, ++i) ;
725     qsort(functions, i, sizeof(*functions), SortFnByName);
726
727     if ((ret = lh_FUNCTION_new()) == NULL)
728         return (NULL);
729
730     for (f = functions; f->name != NULL; f++)
731         (void)lh_FUNCTION_insert(ret, f);
732     return (ret);
733 }