119d3e8ff679622df8726b5fdd4ec7ed3754cc08
[openssl.git] / apps / openssl.c
1 /*
2  * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <internal/cryptlib.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <stdlib.h>
14 #include <openssl/bio.h>
15 #include <openssl/crypto.h>
16 #include <openssl/trace.h>
17 #include <openssl/lhash.h>
18 #include <openssl/conf.h>
19 #include <openssl/x509.h>
20 #include <openssl/pem.h>
21 #include <openssl/ssl.h>
22 #ifndef OPENSSL_NO_ENGINE
23 # include <openssl/engine.h>
24 #endif
25 #include <openssl/err.h>
26 #include "s_apps.h"
27 /* Needed to get the other O_xxx flags. */
28 #ifdef OPENSSL_SYS_VMS
29 # include <unixio.h>
30 #endif
31 #include "apps.h"
32 #define INCLUDE_FUNCTION_TABLE
33 #include "progs.h"
34
35 /* Structure to hold the number of columns to be displayed and the
36  * field width used to display them.
37  */
38 typedef struct {
39     int columns;
40     int width;
41 } DISPLAY_COLUMNS;
42
43 /* Special sentinel to exit the program. */
44 #define EXIT_THE_PROGRAM (-1)
45
46 /*
47  * The LHASH callbacks ("hash" & "cmp") have been replaced by functions with
48  * the base prototypes (we cast each variable inside the function to the
49  * required type of "FUNCTION*"). This removes the necessity for
50  * macro-generated wrapper functions.
51  */
52 static LHASH_OF(FUNCTION) *prog_init(void);
53 static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[]);
54 static void list_pkey(void);
55 static void list_pkey_meth(void);
56 static void list_type(FUNC_TYPE ft, int one);
57 static void list_engines(void);
58 static void list_disabled(void);
59 char *default_config_file = NULL;
60
61 BIO *bio_in = NULL;
62 BIO *bio_out = NULL;
63 BIO *bio_err = NULL;
64
65 static void calculate_columns(DISPLAY_COLUMNS *dc)
66 {
67     FUNCTION *f;
68     int len, maxlen = 0;
69
70     for (f = functions; f->name != NULL; ++f)
71         if (f->type == FT_general || f->type == FT_md || f->type == FT_cipher)
72             if ((len = strlen(f->name)) > maxlen)
73                 maxlen = len;
74
75     dc->width = maxlen + 2;
76     dc->columns = (80 - 1) / dc->width;
77 }
78
79 static int apps_startup(void)
80 {
81 #ifdef SIGPIPE
82     signal(SIGPIPE, SIG_IGN);
83 #endif
84
85     /* Set non-default library initialisation settings */
86     if (!OPENSSL_init_ssl(OPENSSL_INIT_ENGINE_ALL_BUILTIN
87                           | OPENSSL_INIT_LOAD_CONFIG, NULL))
88         return 0;
89
90     setup_ui_method();
91
92     return 1;
93 }
94
95 static void apps_shutdown(void)
96 {
97     destroy_ui_method();
98 }
99
100 static char *make_config_name(void)
101 {
102     const char *t;
103     size_t len;
104     char *p;
105
106     if ((t = getenv("OPENSSL_CONF")) != NULL)
107         return OPENSSL_strdup(t);
108
109     t = X509_get_default_cert_area();
110     len = strlen(t) + 1 + strlen(OPENSSL_CONF) + 1;
111     p = app_malloc(len, "config filename buffer");
112     strcpy(p, t);
113 #ifndef OPENSSL_SYS_VMS
114     strcat(p, "/");
115 #endif
116     strcat(p, OPENSSL_CONF);
117
118     return p;
119 }
120
121 typedef struct tracedata_st {
122     BIO *bio;
123     unsigned int ingroup:1;
124 } tracedata;
125
126 static size_t internal_trace_cb(const char *buf, size_t cnt,
127                                 int category, int cmd, void *vdata)
128 {
129     int ret = 0;
130     tracedata *trace_data = vdata;
131     union {
132         CRYPTO_THREAD_ID tid;
133         unsigned long ltid;
134     } tid;
135     char buffer[256];
136
137     switch (cmd) {
138     case OSSL_TRACE_CTRL_BEGIN:
139         if (!ossl_assert(!trace_data->ingroup))
140             return 0;
141         trace_data->ingroup = 1;
142
143         tid.ltid = 0;
144         tid.tid = CRYPTO_THREAD_get_current_id();
145
146         BIO_snprintf(buffer, sizeof(buffer), "TRACE[%lx]:%s: ", tid.ltid,
147                      OSSL_trace_get_category_name(category));
148         BIO_ctrl(trace_data->bio, PREFIX_CTRL_SET_PREFIX,
149                  strlen(buffer), buffer);
150         break;
151     case OSSL_TRACE_CTRL_WRITE:
152         if (!ossl_assert(trace_data->ingroup))
153             return 0;
154
155         ret = BIO_write(trace_data->bio, buf, cnt);
156         break;
157     case OSSL_TRACE_CTRL_END:
158         if (!ossl_assert(trace_data->ingroup))
159             return 0;
160         trace_data->ingroup = 0;
161
162         BIO_ctrl(trace_data->bio, PREFIX_CTRL_SET_PREFIX, 0, NULL);
163
164         break;
165     }
166
167     return ret < 0 ? 0 : ret;
168 }
169
170 DEFINE_STACK_OF(tracedata)
171 static STACK_OF(tracedata) *trace_data_stack;
172
173 static void tracedata_free(tracedata *data)
174 {
175     BIO_free_all(data->bio);
176     OPENSSL_free(data);
177 }
178
179 static STACK_OF(tracedata) *trace_data_stack;
180
181 static void cleanup_trace(void)
182 {
183     sk_tracedata_pop_free(trace_data_stack, tracedata_free);
184 }
185
186 static void setup_trace(const char *str)
187 {
188     char *val;
189
190     trace_data_stack = sk_tracedata_new_null();
191     val = OPENSSL_strdup(str);
192
193     if (val != NULL) {
194         char *valp = val;
195         char *item;
196
197         for (valp = val; (item = strtok(valp, ",")) != NULL; valp = NULL) {
198             int category = OSSL_trace_get_category_num(item);
199
200             if (category >= 0) {
201                 BIO *channel = BIO_push(BIO_new(apps_bf_prefix()),
202                                         dup_bio_err(FORMAT_TEXT));
203                 tracedata *trace_data = OPENSSL_zalloc(sizeof(*trace_data));
204
205                 if (trace_data == NULL
206                     || (trace_data->bio = channel) == NULL
207                     || OSSL_trace_set_callback(category, internal_trace_cb,
208                                                trace_data) == 0
209                     || sk_tracedata_push(trace_data_stack, trace_data) == 0) {
210                     OSSL_trace_set_callback(category, NULL, NULL);
211                     BIO_free_all(channel);
212                     fprintf(stderr,
213                             "warning: unable to setup trace callback for category '%s'.\n",
214                             item);
215                 }
216             } else {
217                 fprintf(stderr,
218                         "warning: unknown trace category: '%s'.\n",
219                         item);
220             }
221         }
222     }
223
224     OPENSSL_free(val);
225     atexit(cleanup_trace);
226 }
227
228 int main(int argc, char *argv[])
229 {
230     FUNCTION f, *fp;
231     LHASH_OF(FUNCTION) *prog = NULL;
232     char *p, *pname;
233     char buf[1024];
234     const char *prompt;
235     ARGS arg;
236     int first, n, i, ret = 0;
237
238     arg.argv = NULL;
239     arg.size = 0;
240
241     /* Set up some of the environment. */
242     default_config_file = make_config_name();
243     bio_in = dup_bio_in(FORMAT_TEXT);
244     bio_out = dup_bio_out(FORMAT_TEXT);
245     bio_err = dup_bio_err(FORMAT_TEXT);
246
247 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
248     argv = copy_argv(&argc, argv);
249 #elif defined(_WIN32)
250     /*
251      * Replace argv[] with UTF-8 encoded strings.
252      */
253     win32_utf8argv(&argc, &argv);
254 #endif
255
256     /*
257      * We use the prefix method to get the trace output we want.  Since some
258      * trace outputs happen with OPENSSL_cleanup(), which is run automatically
259      * after exit(), we need to destroy the prefix method as late as possible.
260      */
261     atexit(destroy_prefix_method);
262
263     setup_trace(getenv("OPENSSL_TRACE"));
264
265     p = getenv("OPENSSL_DEBUG_MEMORY");
266     if (p != NULL && strcmp(p, "on") == 0)
267         CRYPTO_set_mem_debug(1);
268     CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
269
270     if (getenv("OPENSSL_FIPS")) {
271         BIO_printf(bio_err, "FIPS mode not supported.\n");
272         return 1;
273     }
274
275     if (!apps_startup()) {
276         BIO_printf(bio_err,
277                    "FATAL: Startup failure (dev note: apps_startup() failed)\n");
278         ERR_print_errors(bio_err);
279         ret = 1;
280         goto end;
281     }
282
283     prog = prog_init();
284     pname = opt_progname(argv[0]);
285
286     /* first check the program name */
287     f.name = pname;
288     fp = lh_FUNCTION_retrieve(prog, &f);
289     if (fp != NULL) {
290         argv[0] = pname;
291         ret = fp->func(argc, argv);
292         goto end;
293     }
294
295     /* If there is stuff on the command line, run with that. */
296     if (argc != 1) {
297         argc--;
298         argv++;
299         ret = do_cmd(prog, argc, argv);
300         if (ret < 0)
301             ret = 0;
302         goto end;
303     }
304
305     /* ok, lets enter interactive mode */
306     for (;;) {
307         ret = 0;
308         /* Read a line, continue reading if line ends with \ */
309         for (p = buf, n = sizeof(buf), i = 0, first = 1; n > 0; first = 0) {
310             prompt = first ? "OpenSSL> " : "> ";
311             p[0] = '\0';
312 #ifndef READLINE
313             fputs(prompt, stdout);
314             fflush(stdout);
315             if (!fgets(p, n, stdin))
316                 goto end;
317             if (p[0] == '\0')
318                 goto end;
319             i = strlen(p);
320             if (i <= 1)
321                 break;
322             if (p[i - 2] != '\\')
323                 break;
324             i -= 2;
325             p += i;
326             n -= i;
327 #else
328             {
329                 extern char *readline(const char *);
330                 extern void add_history(const char *cp);
331                 char *text;
332
333                 text = readline(prompt);
334                 if (text == NULL)
335                     goto end;
336                 i = strlen(text);
337                 if (i == 0 || i > n)
338                     break;
339                 if (text[i - 1] != '\\') {
340                     p += strlen(strcpy(p, text));
341                     free(text);
342                     add_history(buf);
343                     break;
344                 }
345
346                 text[i - 1] = '\0';
347                 p += strlen(strcpy(p, text));
348                 free(text);
349                 n -= i;
350             }
351 #endif
352         }
353
354         if (!chopup_args(&arg, buf)) {
355             BIO_printf(bio_err, "Can't parse (no memory?)\n");
356             break;
357         }
358
359         ret = do_cmd(prog, arg.argc, arg.argv);
360         if (ret == EXIT_THE_PROGRAM) {
361             ret = 0;
362             goto end;
363         }
364         if (ret != 0)
365             BIO_printf(bio_err, "error in %s\n", arg.argv[0]);
366         (void)BIO_flush(bio_out);
367         (void)BIO_flush(bio_err);
368     }
369     ret = 1;
370  end:
371     OPENSSL_free(default_config_file);
372     lh_FUNCTION_free(prog);
373     OPENSSL_free(arg.argv);
374     app_RAND_write();
375
376     BIO_free(bio_in);
377     BIO_free_all(bio_out);
378     apps_shutdown();
379 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
380     if (CRYPTO_mem_leaks(bio_err) <= 0)
381         ret = 1;
382 #endif
383     BIO_free(bio_err);
384     EXIT(ret);
385 }
386
387 static void list_cipher_fn(const EVP_CIPHER *c,
388                            const char *from, const char *to, void *arg)
389 {
390     if (c != NULL) {
391         BIO_printf(arg, "%s\n", EVP_CIPHER_name(c));
392     } else {
393         if (from == NULL)
394             from = "<undefined>";
395         if (to == NULL)
396             to = "<undefined>";
397         BIO_printf(arg, "%s => %s\n", from, to);
398     }
399 }
400
401 static void list_md_fn(const EVP_MD *m,
402                        const char *from, const char *to, void *arg)
403 {
404     if (m != NULL) {
405         BIO_printf(arg, "%s\n", EVP_MD_name(m));
406     } else {
407         if (from == NULL)
408             from = "<undefined>";
409         if (to == NULL)
410             to = "<undefined>";
411         BIO_printf((BIO *)arg, "%s => %s\n", from, to);
412     }
413 }
414
415 static void list_mac_fn(const EVP_MAC *m,
416                         const char *from, const char *to, void *arg)
417 {
418     if (m != NULL) {
419         BIO_printf(arg, "%s\n", EVP_MAC_name(m));
420     } else {
421         if (from == NULL)
422             from = "<undefined>";
423         if (to == NULL)
424             to = "<undefined>";
425         BIO_printf(arg, "%s => %s\n", from, to);
426     }
427 }
428
429 static void list_missing_help(void)
430 {
431     const FUNCTION *fp;
432     const OPTIONS *o;
433
434     for (fp = functions; fp->name != NULL; fp++) {
435         if ((o = fp->help) != NULL) {
436             /* If there is help, list what flags are not documented. */
437             for ( ; o->name != NULL; o++) {
438                 if (o->helpstr == NULL)
439                     BIO_printf(bio_out, "%s %s\n", fp->name, o->name);
440             }
441         } else if (fp->func != dgst_main) {
442             /* If not aliased to the dgst command, */
443             BIO_printf(bio_out, "%s *\n", fp->name);
444         }
445     }
446 }
447
448 static void list_objects(void)
449 {
450     int max_nid = OBJ_new_nid(0);
451     int i;
452     char *oid_buf = NULL;
453     int oid_size = 0;
454
455     /* Skip 0, since that's NID_undef */
456     for (i = 1; i < max_nid; i++) {
457         const ASN1_OBJECT *obj = OBJ_nid2obj(i);
458         const char *sn = OBJ_nid2sn(i);
459         const char *ln = OBJ_nid2ln(i);
460         int n = 0;
461
462         /*
463          * If one of the retrieved objects somehow generated an error,
464          * we ignore it.  The check for NID_undef below will detect the
465          * error and simply skip to the next NID.
466          */
467         ERR_clear_error();
468
469         if (OBJ_obj2nid(obj) == NID_undef)
470             continue;
471
472         if ((n = OBJ_obj2txt(NULL, 0, obj, 1)) == 0) {
473             BIO_printf(bio_out, "# None-OID object: %s, %s\n", sn, ln);
474             continue;
475         }
476         if (n < 0)
477             break;               /* Error */
478
479         if (n > oid_size) {
480             oid_buf = OPENSSL_realloc(oid_buf, n + 1);
481             if (oid_buf == NULL) {
482                 BIO_printf(bio_err, "ERROR: Memory allocation\n");
483                 break;           /* Error */
484             }
485             oid_size = n + 1;
486         }
487         if (OBJ_obj2txt(oid_buf, oid_size, obj, 1) < 0)
488             break;               /* Error */
489         if (ln == NULL || strcmp(sn, ln) == 0)
490             BIO_printf(bio_out, "%s = %s\n", sn, oid_buf);
491         else
492             BIO_printf(bio_out, "%s = %s, %s\n", sn, ln, oid_buf);
493     }
494
495     OPENSSL_free(oid_buf);
496 }
497
498 static void list_options_for_command(const char *command)
499 {
500     const FUNCTION *fp;
501     const OPTIONS *o;
502
503     for (fp = functions; fp->name != NULL; fp++)
504         if (strcmp(fp->name, command) == 0)
505             break;
506     if (fp->name == NULL) {
507         BIO_printf(bio_err, "Invalid command '%s'; type \"help\" for a list.\n",
508                 command);
509         return;
510     }
511
512     if ((o = fp->help) == NULL)
513         return;
514
515     for ( ; o->name != NULL; o++) {
516         if (o->name == OPT_HELP_STR
517                 || o->name == OPT_MORE_STR
518                 || o->name[0] == '\0')
519             continue;
520         BIO_printf(bio_out, "%s %c\n", o->name, o->valtype);
521     }
522 }
523
524
525 /* Unified enum for help and list commands. */
526 typedef enum HELPLIST_CHOICE {
527     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, OPT_ONE,
528     OPT_COMMANDS, OPT_DIGEST_COMMANDS, OPT_MAC_ALGORITHMS, OPT_OPTIONS,
529     OPT_DIGEST_ALGORITHMS, OPT_CIPHER_COMMANDS, OPT_CIPHER_ALGORITHMS,
530     OPT_PK_ALGORITHMS, OPT_PK_METHOD, OPT_ENGINES, OPT_DISABLED,
531     OPT_MISSING_HELP, OPT_OBJECTS
532 } HELPLIST_CHOICE;
533
534 const OPTIONS list_options[] = {
535     {"help", OPT_HELP, '-', "Display this summary"},
536     {"1", OPT_ONE, '-', "List in one column"},
537     {"commands", OPT_COMMANDS, '-', "List of standard commands"},
538     {"digest-commands", OPT_DIGEST_COMMANDS, '-',
539      "List of message digest commands"},
540     {"digest-algorithms", OPT_DIGEST_ALGORITHMS, '-',
541      "List of message digest algorithms"},
542     {"mac-algorithms", OPT_MAC_ALGORITHMS, '-',
543      "List of message authentication code algorithms"},
544     {"cipher-commands", OPT_CIPHER_COMMANDS, '-', "List of cipher commands"},
545     {"cipher-algorithms", OPT_CIPHER_ALGORITHMS, '-',
546      "List of cipher algorithms"},
547     {"public-key-algorithms", OPT_PK_ALGORITHMS, '-',
548      "List of public key algorithms"},
549     {"public-key-methods", OPT_PK_METHOD, '-',
550      "List of public key methods"},
551     {"engines", OPT_ENGINES, '-',
552      "List of loaded engines"},
553     {"disabled", OPT_DISABLED, '-',
554      "List of disabled features"},
555     {"missing-help", OPT_MISSING_HELP, '-',
556      "List missing detailed help strings"},
557     {"options", OPT_OPTIONS, 's',
558      "List options for specified command"},
559     {"objects", OPT_OBJECTS, '-',
560      "List built in objects (OID<->name mappings)"},
561     {NULL}
562 };
563
564 int list_main(int argc, char **argv)
565 {
566     char *prog;
567     HELPLIST_CHOICE o;
568     int one = 0, done = 0;
569
570     prog = opt_init(argc, argv, list_options);
571     while ((o = opt_next()) != OPT_EOF) {
572         switch (o) {
573         case OPT_EOF:  /* Never hit, but suppresses warning */
574         case OPT_ERR:
575 opthelp:
576             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
577             return 1;
578         case OPT_HELP:
579             opt_help(list_options);
580             break;
581         case OPT_ONE:
582             one = 1;
583             break;
584         case OPT_COMMANDS:
585             list_type(FT_general, one);
586             break;
587         case OPT_DIGEST_COMMANDS:
588             list_type(FT_md, one);
589             break;
590         case OPT_DIGEST_ALGORITHMS:
591             EVP_MD_do_all_sorted(list_md_fn, bio_out);
592             break;
593         case OPT_MAC_ALGORITHMS:
594             EVP_MAC_do_all_sorted(list_mac_fn, bio_out);
595             break;
596         case OPT_CIPHER_COMMANDS:
597             list_type(FT_cipher, one);
598             break;
599         case OPT_CIPHER_ALGORITHMS:
600             EVP_CIPHER_do_all_sorted(list_cipher_fn, bio_out);
601             break;
602         case OPT_PK_ALGORITHMS:
603             list_pkey();
604             break;
605         case OPT_PK_METHOD:
606             list_pkey_meth();
607             break;
608         case OPT_ENGINES:
609             list_engines();
610             break;
611         case OPT_DISABLED:
612             list_disabled();
613             break;
614         case OPT_MISSING_HELP:
615             list_missing_help();
616             break;
617         case OPT_OBJECTS:
618             list_objects();
619             break;
620         case OPT_OPTIONS:
621             list_options_for_command(opt_arg());
622             break;
623         }
624         done = 1;
625     }
626     if (opt_num_rest() != 0) {
627         BIO_printf(bio_err, "Extra arguments given.\n");
628         goto opthelp;
629     }
630
631     if (!done)
632         goto opthelp;
633
634     return 0;
635 }
636
637 typedef enum HELP_CHOICE {
638     OPT_hERR = -1, OPT_hEOF = 0, OPT_hHELP
639 } HELP_CHOICE;
640
641 const OPTIONS help_options[] = {
642     {OPT_HELP_STR, 1, '-', "Usage: help [options]\n"},
643     {OPT_HELP_STR, 1, '-', "       help [command]\n"},
644     {"help", OPT_hHELP, '-', "Display this summary"},
645     {NULL}
646 };
647
648
649 int help_main(int argc, char **argv)
650 {
651     FUNCTION *fp;
652     int i, nl;
653     FUNC_TYPE tp;
654     char *prog;
655     HELP_CHOICE o;
656     DISPLAY_COLUMNS dc;
657
658     prog = opt_init(argc, argv, help_options);
659     while ((o = opt_next()) != OPT_hEOF) {
660         switch (o) {
661         case OPT_hERR:
662         case OPT_hEOF:
663             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
664             return 1;
665         case OPT_hHELP:
666             opt_help(help_options);
667             return 0;
668         }
669     }
670
671     if (opt_num_rest() == 1) {
672         char *new_argv[3];
673
674         new_argv[0] = opt_rest()[0];
675         new_argv[1] = "--help";
676         new_argv[2] = NULL;
677         return do_cmd(prog_init(), 2, new_argv);
678     }
679     if (opt_num_rest() != 0) {
680         BIO_printf(bio_err, "Usage: %s\n", prog);
681         return 1;
682     }
683
684     calculate_columns(&dc);
685     BIO_printf(bio_err, "Standard commands");
686     i = 0;
687     tp = FT_none;
688     for (fp = functions; fp->name != NULL; fp++) {
689         nl = 0;
690         if (i++ % dc.columns == 0) {
691             BIO_printf(bio_err, "\n");
692             nl = 1;
693         }
694         if (fp->type != tp) {
695             tp = fp->type;
696             if (!nl)
697                 BIO_printf(bio_err, "\n");
698             if (tp == FT_md) {
699                 i = 1;
700                 BIO_printf(bio_err,
701                            "\nMessage Digest commands (see the `dgst' command for more details)\n");
702             } else if (tp == FT_cipher) {
703                 i = 1;
704                 BIO_printf(bio_err,
705                            "\nCipher commands (see the `enc' command for more details)\n");
706             }
707         }
708         BIO_printf(bio_err, "%-*s", dc.width, fp->name);
709     }
710     BIO_printf(bio_err, "\n\n");
711     return 0;
712 }
713
714 static void list_type(FUNC_TYPE ft, int one)
715 {
716     FUNCTION *fp;
717     int i = 0;
718     DISPLAY_COLUMNS dc = {0};
719
720     if (!one)
721         calculate_columns(&dc);
722
723     for (fp = functions; fp->name != NULL; fp++) {
724         if (fp->type != ft)
725             continue;
726         if (one) {
727             BIO_printf(bio_out, "%s\n", fp->name);
728         } else {
729             if (i % dc.columns == 0 && i > 0)
730                 BIO_printf(bio_out, "\n");
731             BIO_printf(bio_out, "%-*s", dc.width, fp->name);
732             i++;
733         }
734     }
735     if (!one)
736         BIO_printf(bio_out, "\n\n");
737 }
738
739 static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[])
740 {
741     FUNCTION f, *fp;
742
743     if (argc <= 0 || argv[0] == NULL)
744         return 0;
745     f.name = argv[0];
746     fp = lh_FUNCTION_retrieve(prog, &f);
747     if (fp == NULL) {
748         if (EVP_get_digestbyname(argv[0])) {
749             f.type = FT_md;
750             f.func = dgst_main;
751             fp = &f;
752         } else if (EVP_get_cipherbyname(argv[0])) {
753             f.type = FT_cipher;
754             f.func = enc_main;
755             fp = &f;
756         }
757     }
758     if (fp != NULL) {
759         return fp->func(argc, argv);
760     }
761     if ((strncmp(argv[0], "no-", 3)) == 0) {
762         /*
763          * User is asking if foo is unsupported, by trying to "run" the
764          * no-foo command.  Strange.
765          */
766         f.name = argv[0] + 3;
767         if (lh_FUNCTION_retrieve(prog, &f) == NULL) {
768             BIO_printf(bio_out, "%s\n", argv[0]);
769             return 0;
770         }
771         BIO_printf(bio_out, "%s\n", argv[0] + 3);
772         return 1;
773     }
774     if (strcmp(argv[0], "quit") == 0 || strcmp(argv[0], "q") == 0 ||
775         strcmp(argv[0], "exit") == 0 || strcmp(argv[0], "bye") == 0)
776         /* Special value to mean "exit the program. */
777         return EXIT_THE_PROGRAM;
778
779     BIO_printf(bio_err, "Invalid command '%s'; type \"help\" for a list.\n",
780                argv[0]);
781     return 1;
782 }
783
784 static void list_pkey(void)
785 {
786     int i;
787
788     for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
789         const EVP_PKEY_ASN1_METHOD *ameth;
790         int pkey_id, pkey_base_id, pkey_flags;
791         const char *pinfo, *pem_str;
792         ameth = EVP_PKEY_asn1_get0(i);
793         EVP_PKEY_asn1_get0_info(&pkey_id, &pkey_base_id, &pkey_flags,
794                                 &pinfo, &pem_str, ameth);
795         if (pkey_flags & ASN1_PKEY_ALIAS) {
796             BIO_printf(bio_out, "Name: %s\n", OBJ_nid2ln(pkey_id));
797             BIO_printf(bio_out, "\tAlias for: %s\n",
798                        OBJ_nid2ln(pkey_base_id));
799         } else {
800             BIO_printf(bio_out, "Name: %s\n", pinfo);
801             BIO_printf(bio_out, "\tType: %s Algorithm\n",
802                        pkey_flags & ASN1_PKEY_DYNAMIC ?
803                        "External" : "Builtin");
804             BIO_printf(bio_out, "\tOID: %s\n", OBJ_nid2ln(pkey_id));
805             if (pem_str == NULL)
806                 pem_str = "(none)";
807             BIO_printf(bio_out, "\tPEM string: %s\n", pem_str);
808         }
809
810     }
811 }
812
813 static void list_pkey_meth(void)
814 {
815     size_t i;
816     size_t meth_count = EVP_PKEY_meth_get_count();
817
818     for (i = 0; i < meth_count; i++) {
819         const EVP_PKEY_METHOD *pmeth = EVP_PKEY_meth_get0(i);
820         int pkey_id, pkey_flags;
821
822         EVP_PKEY_meth_get0_info(&pkey_id, &pkey_flags, pmeth);
823         BIO_printf(bio_out, "%s\n", OBJ_nid2ln(pkey_id));
824         BIO_printf(bio_out, "\tType: %s Algorithm\n",
825                    pkey_flags & ASN1_PKEY_DYNAMIC ?  "External" : "Builtin");
826     }
827 }
828
829 static int function_cmp(const FUNCTION * a, const FUNCTION * b)
830 {
831     return strncmp(a->name, b->name, 8);
832 }
833
834 static unsigned long function_hash(const FUNCTION * a)
835 {
836     return OPENSSL_LH_strhash(a->name);
837 }
838
839 static int SortFnByName(const void *_f1, const void *_f2)
840 {
841     const FUNCTION *f1 = _f1;
842     const FUNCTION *f2 = _f2;
843
844     if (f1->type != f2->type)
845         return f1->type - f2->type;
846     return strcmp(f1->name, f2->name);
847 }
848
849 static void list_engines(void)
850 {
851 #ifndef OPENSSL_NO_ENGINE
852     ENGINE *e;
853
854     BIO_puts(bio_out, "Engines:\n");
855     e = ENGINE_get_first();
856     while (e) {
857         BIO_printf(bio_out, "%s\n", ENGINE_get_id(e));
858         e = ENGINE_get_next(e);
859     }
860 #else
861     BIO_puts(bio_out, "Engine support is disabled.\n");
862 #endif
863 }
864
865 static void list_disabled(void)
866 {
867     BIO_puts(bio_out, "Disabled algorithms:\n");
868 #ifdef OPENSSL_NO_ARIA
869     BIO_puts(bio_out, "ARIA\n");
870 #endif
871 #ifdef OPENSSL_NO_BF
872     BIO_puts(bio_out, "BF\n");
873 #endif
874 #ifdef OPENSSL_NO_BLAKE2
875     BIO_puts(bio_out, "BLAKE2\n");
876 #endif
877 #ifdef OPENSSL_NO_CAMELLIA
878     BIO_puts(bio_out, "CAMELLIA\n");
879 #endif
880 #ifdef OPENSSL_NO_CAST
881     BIO_puts(bio_out, "CAST\n");
882 #endif
883 #ifdef OPENSSL_NO_CMAC
884     BIO_puts(bio_out, "CMAC\n");
885 #endif
886 #ifdef OPENSSL_NO_CMS
887     BIO_puts(bio_out, "CMS\n");
888 #endif
889 #ifdef OPENSSL_NO_COMP
890     BIO_puts(bio_out, "COMP\n");
891 #endif
892 #ifdef OPENSSL_NO_DES
893     BIO_puts(bio_out, "DES\n");
894 #endif
895 #ifdef OPENSSL_NO_DGRAM
896     BIO_puts(bio_out, "DGRAM\n");
897 #endif
898 #ifdef OPENSSL_NO_DH
899     BIO_puts(bio_out, "DH\n");
900 #endif
901 #ifdef OPENSSL_NO_DSA
902     BIO_puts(bio_out, "DSA\n");
903 #endif
904 #if defined(OPENSSL_NO_DTLS)
905     BIO_puts(bio_out, "DTLS\n");
906 #endif
907 #if defined(OPENSSL_NO_DTLS1)
908     BIO_puts(bio_out, "DTLS1\n");
909 #endif
910 #if defined(OPENSSL_NO_DTLS1_2)
911     BIO_puts(bio_out, "DTLS1_2\n");
912 #endif
913 #ifdef OPENSSL_NO_EC
914     BIO_puts(bio_out, "EC\n");
915 #endif
916 #ifdef OPENSSL_NO_EC2M
917     BIO_puts(bio_out, "EC2M\n");
918 #endif
919 #ifdef OPENSSL_NO_ENGINE
920     BIO_puts(bio_out, "ENGINE\n");
921 #endif
922 #ifdef OPENSSL_NO_GOST
923     BIO_puts(bio_out, "GOST\n");
924 #endif
925 #ifdef OPENSSL_NO_HEARTBEATS
926     BIO_puts(bio_out, "HEARTBEATS\n");
927 #endif
928 #ifdef OPENSSL_NO_IDEA
929     BIO_puts(bio_out, "IDEA\n");
930 #endif
931 #ifdef OPENSSL_NO_MD2
932     BIO_puts(bio_out, "MD2\n");
933 #endif
934 #ifdef OPENSSL_NO_MD4
935     BIO_puts(bio_out, "MD4\n");
936 #endif
937 #ifdef OPENSSL_NO_MD5
938     BIO_puts(bio_out, "MD5\n");
939 #endif
940 #ifdef OPENSSL_NO_MDC2
941     BIO_puts(bio_out, "MDC2\n");
942 #endif
943 #ifdef OPENSSL_NO_OCB
944     BIO_puts(bio_out, "OCB\n");
945 #endif
946 #ifdef OPENSSL_NO_OCSP
947     BIO_puts(bio_out, "OCSP\n");
948 #endif
949 #ifdef OPENSSL_NO_PSK
950     BIO_puts(bio_out, "PSK\n");
951 #endif
952 #ifdef OPENSSL_NO_RC2
953     BIO_puts(bio_out, "RC2\n");
954 #endif
955 #ifdef OPENSSL_NO_RC4
956     BIO_puts(bio_out, "RC4\n");
957 #endif
958 #ifdef OPENSSL_NO_RC5
959     BIO_puts(bio_out, "RC5\n");
960 #endif
961 #ifdef OPENSSL_NO_RMD160
962     BIO_puts(bio_out, "RMD160\n");
963 #endif
964 #ifdef OPENSSL_NO_RSA
965     BIO_puts(bio_out, "RSA\n");
966 #endif
967 #ifdef OPENSSL_NO_SCRYPT
968     BIO_puts(bio_out, "SCRYPT\n");
969 #endif
970 #ifdef OPENSSL_NO_SCTP
971     BIO_puts(bio_out, "SCTP\n");
972 #endif
973 #ifdef OPENSSL_NO_SEED
974     BIO_puts(bio_out, "SEED\n");
975 #endif
976 #ifdef OPENSSL_NO_SM2
977     BIO_puts(bio_out, "SM2\n");
978 #endif
979 #ifdef OPENSSL_NO_SM3
980     BIO_puts(bio_out, "SM3\n");
981 #endif
982 #ifdef OPENSSL_NO_SM4
983     BIO_puts(bio_out, "SM4\n");
984 #endif
985 #ifdef OPENSSL_NO_SOCK
986     BIO_puts(bio_out, "SOCK\n");
987 #endif
988 #ifdef OPENSSL_NO_SRP
989     BIO_puts(bio_out, "SRP\n");
990 #endif
991 #ifdef OPENSSL_NO_SRTP
992     BIO_puts(bio_out, "SRTP\n");
993 #endif
994 #ifdef OPENSSL_NO_SSL3
995     BIO_puts(bio_out, "SSL3\n");
996 #endif
997 #ifdef OPENSSL_NO_TLS1
998     BIO_puts(bio_out, "TLS1\n");
999 #endif
1000 #ifdef OPENSSL_NO_TLS1_1
1001     BIO_puts(bio_out, "TLS1_1\n");
1002 #endif
1003 #ifdef OPENSSL_NO_TLS1_2
1004     BIO_puts(bio_out, "TLS1_2\n");
1005 #endif
1006 #ifdef OPENSSL_NO_WHIRLPOOL
1007     BIO_puts(bio_out, "WHIRLPOOL\n");
1008 #endif
1009 #ifndef ZLIB
1010     BIO_puts(bio_out, "ZLIB\n");
1011 #endif
1012 }
1013
1014 static LHASH_OF(FUNCTION) *prog_init(void)
1015 {
1016     static LHASH_OF(FUNCTION) *ret = NULL;
1017     static int prog_inited = 0;
1018     FUNCTION *f;
1019     size_t i;
1020
1021     if (prog_inited)
1022         return ret;
1023
1024     prog_inited = 1;
1025
1026     /* Sort alphabetically within category. For nicer help displays. */
1027     for (i = 0, f = functions; f->name != NULL; ++f, ++i)
1028         ;
1029     qsort(functions, i, sizeof(*functions), SortFnByName);
1030
1031     if ((ret = lh_FUNCTION_new(function_hash, function_cmp)) == NULL)
1032         return NULL;
1033
1034     for (f = functions; f->name != NULL; f++)
1035         (void)lh_FUNCTION_insert(ret, f);
1036     return ret;
1037 }