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