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