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