VMS: move copy_argc to its own module and make it an aux source
[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/lhash.h>
17 #include <openssl/conf.h>
18 #include <openssl/x509.h>
19 #include <openssl/pem.h>
20 #include <openssl/ssl.h>
21 #ifndef OPENSSL_NO_ENGINE
22 # include <openssl/engine.h>
23 #endif
24 #include <openssl/err.h>
25 #include "s_apps.h"
26 /* Needed to get the other O_xxx flags. */
27 #ifdef OPENSSL_SYS_VMS
28 # include <unixio.h>
29 #endif
30 #include "apps.h"
31 #define INCLUDE_FUNCTION_TABLE
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_disabled(void);
57 char *default_config_file = NULL;
58
59 BIO *bio_in = NULL;
60 BIO *bio_out = NULL;
61 BIO *bio_err = NULL;
62
63 static void calculate_columns(DISPLAY_COLUMNS *dc)
64 {
65     FUNCTION *f;
66     int len, maxlen = 0;
67
68     for (f = functions; f->name != NULL; ++f)
69         if (f->type == FT_general || f->type == FT_md || f->type == FT_cipher)
70             if ((len = strlen(f->name)) > maxlen)
71                 maxlen = len;
72
73     dc->width = maxlen + 2;
74     dc->columns = (80 - 1) / dc->width;
75 }
76
77 static int apps_startup(void)
78 {
79 #ifdef SIGPIPE
80     signal(SIGPIPE, SIG_IGN);
81 #endif
82
83     /* Set non-default library initialisation settings */
84     if (!OPENSSL_init_ssl(OPENSSL_INIT_ENGINE_ALL_BUILTIN
85                           | OPENSSL_INIT_LOAD_CONFIG, NULL))
86         return 0;
87
88     setup_ui_method();
89
90     return 1;
91 }
92
93 static void apps_shutdown(void)
94 {
95     destroy_ui_method();
96     destroy_prefix_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 int main(int argc, char *argv[])
121 {
122     FUNCTION f, *fp;
123     LHASH_OF(FUNCTION) *prog = NULL;
124     char *p, *pname;
125     char buf[1024];
126     const char *prompt;
127     ARGS arg;
128     int first, n, i, ret = 0;
129
130     arg.argv = NULL;
131     arg.size = 0;
132
133     /* Set up some of the environment. */
134     default_config_file = make_config_name();
135     bio_in = dup_bio_in(FORMAT_TEXT);
136     bio_out = dup_bio_out(FORMAT_TEXT);
137     bio_err = dup_bio_err(FORMAT_TEXT);
138
139 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
140     argv = copy_argv(&argc, argv);
141 #elif defined(_WIN32)
142     /*
143      * Replace argv[] with UTF-8 encoded strings.
144      */
145     win32_utf8argv(&argc, &argv);
146 #endif
147
148     p = getenv("OPENSSL_DEBUG_MEMORY");
149     if (p != NULL && strcmp(p, "on") == 0)
150         CRYPTO_set_mem_debug(1);
151     CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
152
153     if (getenv("OPENSSL_FIPS")) {
154         BIO_printf(bio_err, "FIPS mode not supported.\n");
155         return 1;
156     }
157
158     if (!apps_startup()) {
159         BIO_printf(bio_err,
160                    "FATAL: Startup failure (dev note: apps_startup() failed)\n");
161         ERR_print_errors(bio_err);
162         ret = 1;
163         goto end;
164     }
165
166     prog = prog_init();
167     pname = opt_progname(argv[0]);
168
169     /* first check the program name */
170     f.name = pname;
171     fp = lh_FUNCTION_retrieve(prog, &f);
172     if (fp != NULL) {
173         argv[0] = pname;
174         ret = fp->func(argc, argv);
175         goto end;
176     }
177
178     /* If there is stuff on the command line, run with that. */
179     if (argc != 1) {
180         argc--;
181         argv++;
182         ret = do_cmd(prog, argc, argv);
183         if (ret < 0)
184             ret = 0;
185         goto end;
186     }
187
188     /* ok, lets enter interactive mode */
189     for (;;) {
190         ret = 0;
191         /* Read a line, continue reading if line ends with \ */
192         for (p = buf, n = sizeof(buf), i = 0, first = 1; n > 0; first = 0) {
193             prompt = first ? "OpenSSL> " : "> ";
194             p[0] = '\0';
195 #ifndef READLINE
196             fputs(prompt, stdout);
197             fflush(stdout);
198             if (!fgets(p, n, stdin))
199                 goto end;
200             if (p[0] == '\0')
201                 goto end;
202             i = strlen(p);
203             if (i <= 1)
204                 break;
205             if (p[i - 2] != '\\')
206                 break;
207             i -= 2;
208             p += i;
209             n -= i;
210 #else
211             {
212                 extern char *readline(const char *);
213                 extern void add_history(const char *cp);
214                 char *text;
215
216                 text = readline(prompt);
217                 if (text == NULL)
218                     goto end;
219                 i = strlen(text);
220                 if (i == 0 || i > n)
221                     break;
222                 if (text[i - 1] != '\\') {
223                     p += strlen(strcpy(p, text));
224                     free(text);
225                     add_history(buf);
226                     break;
227                 }
228
229                 text[i - 1] = '\0';
230                 p += strlen(strcpy(p, text));
231                 free(text);
232                 n -= i;
233             }
234 #endif
235         }
236
237         if (!chopup_args(&arg, buf)) {
238             BIO_printf(bio_err, "Can't parse (no memory?)\n");
239             break;
240         }
241
242         ret = do_cmd(prog, arg.argc, arg.argv);
243         if (ret == EXIT_THE_PROGRAM) {
244             ret = 0;
245             goto end;
246         }
247         if (ret != 0)
248             BIO_printf(bio_err, "error in %s\n", arg.argv[0]);
249         (void)BIO_flush(bio_out);
250         (void)BIO_flush(bio_err);
251     }
252     ret = 1;
253  end:
254     OPENSSL_free(default_config_file);
255     lh_FUNCTION_free(prog);
256     OPENSSL_free(arg.argv);
257     app_RAND_write();
258
259     BIO_free(bio_in);
260     BIO_free_all(bio_out);
261     apps_shutdown();
262 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
263     if (CRYPTO_mem_leaks(bio_err) <= 0)
264         ret = 1;
265 #endif
266     BIO_free(bio_err);
267     EXIT(ret);
268 }
269
270 static void list_cipher_fn(const EVP_CIPHER *c,
271                            const char *from, const char *to, void *arg)
272 {
273     if (c != NULL) {
274         BIO_printf(arg, "%s\n", EVP_CIPHER_name(c));
275     } else {
276         if (from == NULL)
277             from = "<undefined>";
278         if (to == NULL)
279             to = "<undefined>";
280         BIO_printf(arg, "%s => %s\n", from, to);
281     }
282 }
283
284 static void list_md_fn(const EVP_MD *m,
285                        const char *from, const char *to, void *arg)
286 {
287     if (m != NULL) {
288         BIO_printf(arg, "%s\n", EVP_MD_name(m));
289     } else {
290         if (from == NULL)
291             from = "<undefined>";
292         if (to == NULL)
293             to = "<undefined>";
294         BIO_printf((BIO *)arg, "%s => %s\n", from, to);
295     }
296 }
297
298 static void list_mac_fn(const EVP_MAC *m,
299                         const char *from, const char *to, void *arg)
300 {
301     if (m != NULL) {
302         BIO_printf(arg, "%s\n", EVP_MAC_name(m));
303     } else {
304         if (from == NULL)
305             from = "<undefined>";
306         if (to == NULL)
307             to = "<undefined>";
308         BIO_printf(arg, "%s => %s\n", from, to);
309     }
310 }
311
312 static void list_missing_help(void)
313 {
314     const FUNCTION *fp;
315     const OPTIONS *o;
316
317     for (fp = functions; fp->name != NULL; fp++) {
318         if ((o = fp->help) != NULL) {
319             /* If there is help, list what flags are not documented. */
320             for ( ; o->name != NULL; o++) {
321                 if (o->helpstr == NULL)
322                     BIO_printf(bio_out, "%s %s\n", fp->name, o->name);
323             }
324         } else if (fp->func != dgst_main) {
325             /* If not aliased to the dgst command, */
326             BIO_printf(bio_out, "%s *\n", fp->name);
327         }
328     }
329 }
330
331 static void list_objects(void)
332 {
333     int max_nid = OBJ_new_nid(0);
334     int i;
335     char *oid_buf = NULL;
336     int oid_size = 0;
337
338     /* Skip 0, since that's NID_undef */
339     for (i = 1; i < max_nid; i++) {
340         const ASN1_OBJECT *obj = OBJ_nid2obj(i);
341         const char *sn = OBJ_nid2sn(i);
342         const char *ln = OBJ_nid2ln(i);
343         int n = 0;
344
345         /*
346          * If one of the retrieved objects somehow generated an error,
347          * we ignore it.  The check for NID_undef below will detect the
348          * error and simply skip to the next NID.
349          */
350         ERR_clear_error();
351
352         if (OBJ_obj2nid(obj) == NID_undef)
353             continue;
354
355         if ((n = OBJ_obj2txt(NULL, 0, obj, 1)) == 0) {
356             BIO_printf(bio_out, "# None-OID object: %s, %s\n", sn, ln);
357             continue;
358         }
359         if (n < 0)
360             break;               /* Error */
361
362         if (n > oid_size) {
363             oid_buf = OPENSSL_realloc(oid_buf, n + 1);
364             if (oid_buf == NULL) {
365                 BIO_printf(bio_err, "ERROR: Memory allocation\n");
366                 break;           /* Error */
367             }
368             oid_size = n + 1;
369         }
370         if (OBJ_obj2txt(oid_buf, oid_size, obj, 1) < 0)
371             break;               /* Error */
372         if (ln == NULL || strcmp(sn, ln) == 0)
373             BIO_printf(bio_out, "%s = %s\n", sn, oid_buf);
374         else
375             BIO_printf(bio_out, "%s = %s, %s\n", sn, ln, oid_buf);
376     }
377
378     OPENSSL_free(oid_buf);
379 }
380
381 static void list_options_for_command(const char *command)
382 {
383     const FUNCTION *fp;
384     const OPTIONS *o;
385
386     for (fp = functions; fp->name != NULL; fp++)
387         if (strcmp(fp->name, command) == 0)
388             break;
389     if (fp->name == NULL) {
390         BIO_printf(bio_err, "Invalid command '%s'; type \"help\" for a list.\n",
391                 command);
392         return;
393     }
394
395     if ((o = fp->help) == NULL)
396         return;
397
398     for ( ; o->name != NULL; o++) {
399         if (o->name == OPT_HELP_STR
400                 || o->name == OPT_MORE_STR
401                 || o->name[0] == '\0')
402             continue;
403         BIO_printf(bio_out, "%s %c\n", o->name, o->valtype);
404     }
405 }
406
407
408 /* Unified enum for help and list commands. */
409 typedef enum HELPLIST_CHOICE {
410     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, OPT_ONE,
411     OPT_COMMANDS, OPT_DIGEST_COMMANDS, OPT_MAC_ALGORITHMS, OPT_OPTIONS,
412     OPT_DIGEST_ALGORITHMS, OPT_CIPHER_COMMANDS, OPT_CIPHER_ALGORITHMS,
413     OPT_PK_ALGORITHMS, OPT_PK_METHOD, OPT_DISABLED, OPT_MISSING_HELP,
414     OPT_OBJECTS
415 } HELPLIST_CHOICE;
416
417 const OPTIONS list_options[] = {
418     {"help", OPT_HELP, '-', "Display this summary"},
419     {"1", OPT_ONE, '-', "List in one column"},
420     {"commands", OPT_COMMANDS, '-', "List of standard commands"},
421     {"digest-commands", OPT_DIGEST_COMMANDS, '-',
422      "List of message digest commands"},
423     {"digest-algorithms", OPT_DIGEST_ALGORITHMS, '-',
424      "List of message digest algorithms"},
425     {"mac-algorithms", OPT_MAC_ALGORITHMS, '-',
426      "List of message authentication code algorithms"},
427     {"cipher-commands", OPT_CIPHER_COMMANDS, '-', "List of cipher commands"},
428     {"cipher-algorithms", OPT_CIPHER_ALGORITHMS, '-',
429      "List of cipher algorithms"},
430     {"public-key-algorithms", OPT_PK_ALGORITHMS, '-',
431      "List of public key algorithms"},
432     {"public-key-methods", OPT_PK_METHOD, '-',
433      "List of public key methods"},
434     {"disabled", OPT_DISABLED, '-',
435      "List of disabled features"},
436     {"missing-help", OPT_MISSING_HELP, '-',
437      "List missing detailed help strings"},
438     {"options", OPT_OPTIONS, 's',
439      "List options for specified command"},
440     {"objects", OPT_OBJECTS, '-',
441      "List built in objects (OID<->name mappings)"},
442     {NULL}
443 };
444
445 int list_main(int argc, char **argv)
446 {
447     char *prog;
448     HELPLIST_CHOICE o;
449     int one = 0, done = 0;
450
451     prog = opt_init(argc, argv, list_options);
452     while ((o = opt_next()) != OPT_EOF) {
453         switch (o) {
454         case OPT_EOF:  /* Never hit, but suppresses warning */
455         case OPT_ERR:
456 opthelp:
457             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
458             return 1;
459         case OPT_HELP:
460             opt_help(list_options);
461             break;
462         case OPT_ONE:
463             one = 1;
464             break;
465         case OPT_COMMANDS:
466             list_type(FT_general, one);
467             break;
468         case OPT_DIGEST_COMMANDS:
469             list_type(FT_md, one);
470             break;
471         case OPT_DIGEST_ALGORITHMS:
472             EVP_MD_do_all_sorted(list_md_fn, bio_out);
473             break;
474         case OPT_MAC_ALGORITHMS:
475             EVP_MAC_do_all_sorted(list_mac_fn, bio_out);
476             break;
477         case OPT_CIPHER_COMMANDS:
478             list_type(FT_cipher, one);
479             break;
480         case OPT_CIPHER_ALGORITHMS:
481             EVP_CIPHER_do_all_sorted(list_cipher_fn, bio_out);
482             break;
483         case OPT_PK_ALGORITHMS:
484             list_pkey();
485             break;
486         case OPT_PK_METHOD:
487             list_pkey_meth();
488             break;
489         case OPT_DISABLED:
490             list_disabled();
491             break;
492         case OPT_MISSING_HELP:
493             list_missing_help();
494             break;
495         case OPT_OBJECTS:
496             list_objects();
497             break;
498         case OPT_OPTIONS:
499             list_options_for_command(opt_arg());
500             break;
501         }
502         done = 1;
503     }
504     if (opt_num_rest() != 0) {
505         BIO_printf(bio_err, "Extra arguments given.\n");
506         goto opthelp;
507     }
508
509     if (!done)
510         goto opthelp;
511
512     return 0;
513 }
514
515 typedef enum HELP_CHOICE {
516     OPT_hERR = -1, OPT_hEOF = 0, OPT_hHELP
517 } HELP_CHOICE;
518
519 const OPTIONS help_options[] = {
520     {OPT_HELP_STR, 1, '-', "Usage: help [options]\n"},
521     {OPT_HELP_STR, 1, '-', "       help [command]\n"},
522     {"help", OPT_hHELP, '-', "Display this summary"},
523     {NULL}
524 };
525
526
527 int help_main(int argc, char **argv)
528 {
529     FUNCTION *fp;
530     int i, nl;
531     FUNC_TYPE tp;
532     char *prog;
533     HELP_CHOICE o;
534     DISPLAY_COLUMNS dc;
535
536     prog = opt_init(argc, argv, help_options);
537     while ((o = opt_next()) != OPT_hEOF) {
538         switch (o) {
539         case OPT_hERR:
540         case OPT_hEOF:
541             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
542             return 1;
543         case OPT_hHELP:
544             opt_help(help_options);
545             return 0;
546         }
547     }
548
549     if (opt_num_rest() == 1) {
550         char *new_argv[3];
551
552         new_argv[0] = opt_rest()[0];
553         new_argv[1] = "--help";
554         new_argv[2] = NULL;
555         return do_cmd(prog_init(), 2, new_argv);
556     }
557     if (opt_num_rest() != 0) {
558         BIO_printf(bio_err, "Usage: %s\n", prog);
559         return 1;
560     }
561
562     calculate_columns(&dc);
563     BIO_printf(bio_err, "Standard commands");
564     i = 0;
565     tp = FT_none;
566     for (fp = functions; fp->name != NULL; fp++) {
567         nl = 0;
568         if (i++ % dc.columns == 0) {
569             BIO_printf(bio_err, "\n");
570             nl = 1;
571         }
572         if (fp->type != tp) {
573             tp = fp->type;
574             if (!nl)
575                 BIO_printf(bio_err, "\n");
576             if (tp == FT_md) {
577                 i = 1;
578                 BIO_printf(bio_err,
579                            "\nMessage Digest commands (see the `dgst' command for more details)\n");
580             } else if (tp == FT_cipher) {
581                 i = 1;
582                 BIO_printf(bio_err,
583                            "\nCipher commands (see the `enc' command for more details)\n");
584             }
585         }
586         BIO_printf(bio_err, "%-*s", dc.width, fp->name);
587     }
588     BIO_printf(bio_err, "\n\n");
589     return 0;
590 }
591
592 static void list_type(FUNC_TYPE ft, int one)
593 {
594     FUNCTION *fp;
595     int i = 0;
596     DISPLAY_COLUMNS dc = {0};
597
598     if (!one)
599         calculate_columns(&dc);
600
601     for (fp = functions; fp->name != NULL; fp++) {
602         if (fp->type != ft)
603             continue;
604         if (one) {
605             BIO_printf(bio_out, "%s\n", fp->name);
606         } else {
607             if (i % dc.columns == 0 && i > 0)
608                 BIO_printf(bio_out, "\n");
609             BIO_printf(bio_out, "%-*s", dc.width, fp->name);
610             i++;
611         }
612     }
613     if (!one)
614         BIO_printf(bio_out, "\n\n");
615 }
616
617 static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[])
618 {
619     FUNCTION f, *fp;
620
621     if (argc <= 0 || argv[0] == NULL)
622         return 0;
623     f.name = argv[0];
624     fp = lh_FUNCTION_retrieve(prog, &f);
625     if (fp == NULL) {
626         if (EVP_get_digestbyname(argv[0])) {
627             f.type = FT_md;
628             f.func = dgst_main;
629             fp = &f;
630         } else if (EVP_get_cipherbyname(argv[0])) {
631             f.type = FT_cipher;
632             f.func = enc_main;
633             fp = &f;
634         }
635     }
636     if (fp != NULL) {
637         return fp->func(argc, argv);
638     }
639     if ((strncmp(argv[0], "no-", 3)) == 0) {
640         /*
641          * User is asking if foo is unsupported, by trying to "run" the
642          * no-foo command.  Strange.
643          */
644         f.name = argv[0] + 3;
645         if (lh_FUNCTION_retrieve(prog, &f) == NULL) {
646             BIO_printf(bio_out, "%s\n", argv[0]);
647             return 0;
648         }
649         BIO_printf(bio_out, "%s\n", argv[0] + 3);
650         return 1;
651     }
652     if (strcmp(argv[0], "quit") == 0 || strcmp(argv[0], "q") == 0 ||
653         strcmp(argv[0], "exit") == 0 || strcmp(argv[0], "bye") == 0)
654         /* Special value to mean "exit the program. */
655         return EXIT_THE_PROGRAM;
656
657     BIO_printf(bio_err, "Invalid command '%s'; type \"help\" for a list.\n",
658                argv[0]);
659     return 1;
660 }
661
662 static void list_pkey(void)
663 {
664     int i;
665
666     for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
667         const EVP_PKEY_ASN1_METHOD *ameth;
668         int pkey_id, pkey_base_id, pkey_flags;
669         const char *pinfo, *pem_str;
670         ameth = EVP_PKEY_asn1_get0(i);
671         EVP_PKEY_asn1_get0_info(&pkey_id, &pkey_base_id, &pkey_flags,
672                                 &pinfo, &pem_str, ameth);
673         if (pkey_flags & ASN1_PKEY_ALIAS) {
674             BIO_printf(bio_out, "Name: %s\n", OBJ_nid2ln(pkey_id));
675             BIO_printf(bio_out, "\tAlias for: %s\n",
676                        OBJ_nid2ln(pkey_base_id));
677         } else {
678             BIO_printf(bio_out, "Name: %s\n", pinfo);
679             BIO_printf(bio_out, "\tType: %s Algorithm\n",
680                        pkey_flags & ASN1_PKEY_DYNAMIC ?
681                        "External" : "Builtin");
682             BIO_printf(bio_out, "\tOID: %s\n", OBJ_nid2ln(pkey_id));
683             if (pem_str == NULL)
684                 pem_str = "(none)";
685             BIO_printf(bio_out, "\tPEM string: %s\n", pem_str);
686         }
687
688     }
689 }
690
691 static void list_pkey_meth(void)
692 {
693     size_t i;
694     size_t meth_count = EVP_PKEY_meth_get_count();
695
696     for (i = 0; i < meth_count; i++) {
697         const EVP_PKEY_METHOD *pmeth = EVP_PKEY_meth_get0(i);
698         int pkey_id, pkey_flags;
699
700         EVP_PKEY_meth_get0_info(&pkey_id, &pkey_flags, pmeth);
701         BIO_printf(bio_out, "%s\n", OBJ_nid2ln(pkey_id));
702         BIO_printf(bio_out, "\tType: %s Algorithm\n",
703                    pkey_flags & ASN1_PKEY_DYNAMIC ?  "External" : "Builtin");
704     }
705 }
706
707 static int function_cmp(const FUNCTION * a, const FUNCTION * b)
708 {
709     return strncmp(a->name, b->name, 8);
710 }
711
712 static unsigned long function_hash(const FUNCTION * a)
713 {
714     return OPENSSL_LH_strhash(a->name);
715 }
716
717 static int SortFnByName(const void *_f1, const void *_f2)
718 {
719     const FUNCTION *f1 = _f1;
720     const FUNCTION *f2 = _f2;
721
722     if (f1->type != f2->type)
723         return f1->type - f2->type;
724     return strcmp(f1->name, f2->name);
725 }
726
727 static void list_disabled(void)
728 {
729     BIO_puts(bio_out, "Disabled algorithms:\n");
730 #ifdef OPENSSL_NO_ARIA
731     BIO_puts(bio_out, "ARIA\n");
732 #endif
733 #ifdef OPENSSL_NO_BF
734     BIO_puts(bio_out, "BF\n");
735 #endif
736 #ifdef OPENSSL_NO_BLAKE2
737     BIO_puts(bio_out, "BLAKE2\n");
738 #endif
739 #ifdef OPENSSL_NO_CAMELLIA
740     BIO_puts(bio_out, "CAMELLIA\n");
741 #endif
742 #ifdef OPENSSL_NO_CAST
743     BIO_puts(bio_out, "CAST\n");
744 #endif
745 #ifdef OPENSSL_NO_CMAC
746     BIO_puts(bio_out, "CMAC\n");
747 #endif
748 #ifdef OPENSSL_NO_CMS
749     BIO_puts(bio_out, "CMS\n");
750 #endif
751 #ifdef OPENSSL_NO_COMP
752     BIO_puts(bio_out, "COMP\n");
753 #endif
754 #ifdef OPENSSL_NO_DES
755     BIO_puts(bio_out, "DES\n");
756 #endif
757 #ifdef OPENSSL_NO_DGRAM
758     BIO_puts(bio_out, "DGRAM\n");
759 #endif
760 #ifdef OPENSSL_NO_DH
761     BIO_puts(bio_out, "DH\n");
762 #endif
763 #ifdef OPENSSL_NO_DSA
764     BIO_puts(bio_out, "DSA\n");
765 #endif
766 #if defined(OPENSSL_NO_DTLS)
767     BIO_puts(bio_out, "DTLS\n");
768 #endif
769 #if defined(OPENSSL_NO_DTLS1)
770     BIO_puts(bio_out, "DTLS1\n");
771 #endif
772 #if defined(OPENSSL_NO_DTLS1_2)
773     BIO_puts(bio_out, "DTLS1_2\n");
774 #endif
775 #ifdef OPENSSL_NO_EC
776     BIO_puts(bio_out, "EC\n");
777 #endif
778 #ifdef OPENSSL_NO_EC2M
779     BIO_puts(bio_out, "EC2M\n");
780 #endif
781 #ifdef OPENSSL_NO_ENGINE
782     BIO_puts(bio_out, "ENGINE\n");
783 #endif
784 #ifdef OPENSSL_NO_GOST
785     BIO_puts(bio_out, "GOST\n");
786 #endif
787 #ifdef OPENSSL_NO_HEARTBEATS
788     BIO_puts(bio_out, "HEARTBEATS\n");
789 #endif
790 #ifdef OPENSSL_NO_IDEA
791     BIO_puts(bio_out, "IDEA\n");
792 #endif
793 #ifdef OPENSSL_NO_MD2
794     BIO_puts(bio_out, "MD2\n");
795 #endif
796 #ifdef OPENSSL_NO_MD4
797     BIO_puts(bio_out, "MD4\n");
798 #endif
799 #ifdef OPENSSL_NO_MD5
800     BIO_puts(bio_out, "MD5\n");
801 #endif
802 #ifdef OPENSSL_NO_MDC2
803     BIO_puts(bio_out, "MDC2\n");
804 #endif
805 #ifdef OPENSSL_NO_OCB
806     BIO_puts(bio_out, "OCB\n");
807 #endif
808 #ifdef OPENSSL_NO_OCSP
809     BIO_puts(bio_out, "OCSP\n");
810 #endif
811 #ifdef OPENSSL_NO_PSK
812     BIO_puts(bio_out, "PSK\n");
813 #endif
814 #ifdef OPENSSL_NO_RC2
815     BIO_puts(bio_out, "RC2\n");
816 #endif
817 #ifdef OPENSSL_NO_RC4
818     BIO_puts(bio_out, "RC4\n");
819 #endif
820 #ifdef OPENSSL_NO_RC5
821     BIO_puts(bio_out, "RC5\n");
822 #endif
823 #ifdef OPENSSL_NO_RMD160
824     BIO_puts(bio_out, "RMD160\n");
825 #endif
826 #ifdef OPENSSL_NO_RSA
827     BIO_puts(bio_out, "RSA\n");
828 #endif
829 #ifdef OPENSSL_NO_SCRYPT
830     BIO_puts(bio_out, "SCRYPT\n");
831 #endif
832 #ifdef OPENSSL_NO_SCTP
833     BIO_puts(bio_out, "SCTP\n");
834 #endif
835 #ifdef OPENSSL_NO_SEED
836     BIO_puts(bio_out, "SEED\n");
837 #endif
838 #ifdef OPENSSL_NO_SM2
839     BIO_puts(bio_out, "SM2\n");
840 #endif
841 #ifdef OPENSSL_NO_SM3
842     BIO_puts(bio_out, "SM3\n");
843 #endif
844 #ifdef OPENSSL_NO_SM4
845     BIO_puts(bio_out, "SM4\n");
846 #endif
847 #ifdef OPENSSL_NO_SOCK
848     BIO_puts(bio_out, "SOCK\n");
849 #endif
850 #ifdef OPENSSL_NO_SRP
851     BIO_puts(bio_out, "SRP\n");
852 #endif
853 #ifdef OPENSSL_NO_SRTP
854     BIO_puts(bio_out, "SRTP\n");
855 #endif
856 #ifdef OPENSSL_NO_SSL3
857     BIO_puts(bio_out, "SSL3\n");
858 #endif
859 #ifdef OPENSSL_NO_TLS1
860     BIO_puts(bio_out, "TLS1\n");
861 #endif
862 #ifdef OPENSSL_NO_TLS1_1
863     BIO_puts(bio_out, "TLS1_1\n");
864 #endif
865 #ifdef OPENSSL_NO_TLS1_2
866     BIO_puts(bio_out, "TLS1_2\n");
867 #endif
868 #ifdef OPENSSL_NO_WHIRLPOOL
869     BIO_puts(bio_out, "WHIRLPOOL\n");
870 #endif
871 #ifndef ZLIB
872     BIO_puts(bio_out, "ZLIB\n");
873 #endif
874 }
875
876 static LHASH_OF(FUNCTION) *prog_init(void)
877 {
878     static LHASH_OF(FUNCTION) *ret = NULL;
879     static int prog_inited = 0;
880     FUNCTION *f;
881     size_t i;
882
883     if (prog_inited)
884         return ret;
885
886     prog_inited = 1;
887
888     /* Sort alphabetically within category. For nicer help displays. */
889     for (i = 0, f = functions; f->name != NULL; ++f, ++i)
890         ;
891     qsort(functions, i, sizeof(*functions), SortFnByName);
892
893     if ((ret = lh_FUNCTION_new(function_hash, function_cmp)) == NULL)
894         return NULL;
895
896     for (f = functions; f->name != NULL; f++)
897         (void)lh_FUNCTION_insert(ret, f);
898     return ret;
899 }