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