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