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