coverity 1462556 Resource leak
[openssl.git] / apps / list.c
1 /*
2  * Copyright 1995-2020 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 <string.h>
11 #include <openssl/evp.h>
12 #include <openssl/err.h>
13 #include <openssl/provider.h>
14 #include <openssl/safestack.h>
15 #include <openssl/kdf.h>
16 #include "apps.h"
17 #include "app_params.h"
18 #include "progs.h"
19 #include "opt.h"
20 #include "names.h"
21
22 DEFINE_STACK_OF_CSTRING()
23
24 static int verbose = 0;
25
26 static void legacy_cipher_fn(const EVP_CIPHER *c,
27                              const char *from, const char *to, void *arg)
28 {
29     if (c != NULL) {
30         BIO_printf(arg, "  %s\n", EVP_CIPHER_name(c));
31     } else {
32         if (from == NULL)
33             from = "<undefined>";
34         if (to == NULL)
35             to = "<undefined>";
36         BIO_printf(arg, "  %s => %s\n", from, to);
37     }
38 }
39
40 DEFINE_STACK_OF(EVP_CIPHER)
41 static int cipher_cmp(const EVP_CIPHER * const *a,
42                       const EVP_CIPHER * const *b)
43 {
44     int ret = EVP_CIPHER_number(*a) - EVP_CIPHER_number(*b);
45
46     if (ret == 0)
47         ret = strcmp(OSSL_PROVIDER_name(EVP_CIPHER_provider(*a)),
48                      OSSL_PROVIDER_name(EVP_CIPHER_provider(*b)));
49
50     return ret;
51 }
52
53 static void collect_ciphers(EVP_CIPHER *cipher, void *stack)
54 {
55     STACK_OF(EVP_CIPHER) *cipher_stack = stack;
56
57     if (sk_EVP_CIPHER_push(cipher_stack, cipher) > 0)
58         EVP_CIPHER_up_ref(cipher);
59 }
60
61 static void list_ciphers(void)
62 {
63     STACK_OF(EVP_CIPHER) *ciphers = sk_EVP_CIPHER_new(cipher_cmp);
64     int i;
65
66     BIO_printf(bio_out, "Legacy:\n");
67     EVP_CIPHER_do_all_sorted(legacy_cipher_fn, bio_out);
68
69     BIO_printf(bio_out, "Provided:\n");
70     EVP_CIPHER_do_all_provided(NULL, collect_ciphers, ciphers);
71     sk_EVP_CIPHER_sort(ciphers);
72     for (i = 0; i < sk_EVP_CIPHER_num(ciphers); i++) {
73         const EVP_CIPHER *c = sk_EVP_CIPHER_value(ciphers, i);
74         STACK_OF(OPENSSL_CSTRING) *names =
75             sk_OPENSSL_CSTRING_new(name_cmp);
76
77         EVP_CIPHER_names_do_all(c, collect_names, names);
78
79         BIO_printf(bio_out, "  ");
80         print_names(bio_out, names);
81         BIO_printf(bio_out, " @ %s\n",
82                    OSSL_PROVIDER_name(EVP_CIPHER_provider(c)));
83
84         sk_OPENSSL_CSTRING_free(names);
85
86         if (verbose) {
87             print_param_types("retrievable algorithm parameters",
88                               EVP_CIPHER_gettable_params(c), 4);
89             print_param_types("retrievable operation parameters",
90                               EVP_CIPHER_gettable_ctx_params(c), 4);
91             print_param_types("settable operation parameters",
92                               EVP_CIPHER_settable_ctx_params(c), 4);
93         }
94     }
95     sk_EVP_CIPHER_pop_free(ciphers, EVP_CIPHER_free);
96 }
97
98 static void list_md_fn(const EVP_MD *m,
99                        const char *from, const char *to, void *arg)
100 {
101     if (m != NULL) {
102         BIO_printf(arg, "  %s\n", EVP_MD_name(m));
103     } else {
104         if (from == NULL)
105             from = "<undefined>";
106         if (to == NULL)
107             to = "<undefined>";
108         BIO_printf((BIO *)arg, "  %s => %s\n", from, to);
109     }
110 }
111
112 DEFINE_STACK_OF(EVP_MD)
113 static int md_cmp(const EVP_MD * const *a, const EVP_MD * const *b)
114 {
115     int ret = EVP_MD_number(*a) - EVP_MD_number(*b);
116
117     if (ret == 0)
118         ret = strcmp(OSSL_PROVIDER_name(EVP_MD_provider(*a)),
119                      OSSL_PROVIDER_name(EVP_MD_provider(*b)));
120
121     return ret;
122 }
123
124 static void collect_digests(EVP_MD *md, void *stack)
125 {
126     STACK_OF(EVP_MD) *digest_stack = stack;
127
128     if (sk_EVP_MD_push(digest_stack, md) > 0)
129         EVP_MD_up_ref(md);
130 }
131
132 static void list_digests(void)
133 {
134     STACK_OF(EVP_MD) *digests = sk_EVP_MD_new(md_cmp);
135     int i;
136
137     BIO_printf(bio_out, "Legacy:\n");
138     EVP_MD_do_all_sorted(list_md_fn, bio_out);
139
140     BIO_printf(bio_out, "Provided:\n");
141     EVP_MD_do_all_provided(NULL, collect_digests, digests);
142     sk_EVP_MD_sort(digests);
143     for (i = 0; i < sk_EVP_MD_num(digests); i++) {
144         const EVP_MD *m = sk_EVP_MD_value(digests, i);
145         STACK_OF(OPENSSL_CSTRING) *names =
146             sk_OPENSSL_CSTRING_new(name_cmp);
147
148         EVP_MD_names_do_all(m, collect_names, names);
149
150         BIO_printf(bio_out, "  ");
151         print_names(bio_out, names);
152         BIO_printf(bio_out, " @ %s\n",
153                    OSSL_PROVIDER_name(EVP_MD_provider(m)));
154
155         sk_OPENSSL_CSTRING_free(names);
156
157         if (verbose) {
158             print_param_types("retrievable algorithm parameters",
159                               EVP_MD_gettable_params(m), 4);
160             print_param_types("retrievable operation parameters",
161                               EVP_MD_gettable_ctx_params(m), 4);
162             print_param_types("settable operation parameters",
163                               EVP_MD_settable_ctx_params(m), 4);
164         }
165     }
166     sk_EVP_MD_pop_free(digests, EVP_MD_free);
167 }
168
169 DEFINE_STACK_OF(EVP_MAC)
170 static int mac_cmp(const EVP_MAC * const *a, const EVP_MAC * const *b)
171 {
172     int ret = EVP_MAC_number(*a) - EVP_MAC_number(*b);
173
174     if (ret == 0)
175         ret = strcmp(OSSL_PROVIDER_name(EVP_MAC_provider(*a)),
176                      OSSL_PROVIDER_name(EVP_MAC_provider(*b)));
177
178     return ret;
179 }
180
181 static void collect_macs(EVP_MAC *mac, void *stack)
182 {
183     STACK_OF(EVP_MAC) *mac_stack = stack;
184
185     if (sk_EVP_MAC_push(mac_stack, mac) > 0)
186         EVP_MAC_up_ref(mac);
187 }
188
189 static void list_macs(void)
190 {
191     STACK_OF(EVP_MAC) *macs = sk_EVP_MAC_new(mac_cmp);
192     int i;
193
194     BIO_printf(bio_out, "Provided MACs:\n");
195     EVP_MAC_do_all_provided(NULL, collect_macs, macs);
196     sk_EVP_MAC_sort(macs);
197     for (i = 0; i < sk_EVP_MAC_num(macs); i++) {
198         const EVP_MAC *m = sk_EVP_MAC_value(macs, i);
199         STACK_OF(OPENSSL_CSTRING) *names =
200             sk_OPENSSL_CSTRING_new(name_cmp);
201
202         EVP_MAC_names_do_all(m, collect_names, names);
203
204         BIO_printf(bio_out, "  ");
205         print_names(bio_out, names);
206         BIO_printf(bio_out, " @ %s\n",
207                    OSSL_PROVIDER_name(EVP_MAC_provider(m)));
208
209         sk_OPENSSL_CSTRING_free(names);
210
211         if (verbose) {
212             print_param_types("retrievable algorithm parameters",
213                               EVP_MAC_gettable_params(m), 4);
214             print_param_types("retrievable operation parameters",
215                               EVP_MAC_gettable_ctx_params(m), 4);
216             print_param_types("settable operation parameters",
217                               EVP_MAC_settable_ctx_params(m), 4);
218         }
219     }
220     sk_EVP_MAC_pop_free(macs, EVP_MAC_free);
221 }
222
223 /*
224  * KDFs and PRFs
225  */
226 DEFINE_STACK_OF(EVP_KDF)
227 static int kdf_cmp(const EVP_KDF * const *a, const EVP_KDF * const *b)
228 {
229     int ret = EVP_KDF_number(*a) - EVP_KDF_number(*b);
230
231     if (ret == 0)
232         ret = strcmp(OSSL_PROVIDER_name(EVP_KDF_provider(*a)),
233                      OSSL_PROVIDER_name(EVP_KDF_provider(*b)));
234
235     return ret;
236 }
237
238 static void collect_kdfs(EVP_KDF *kdf, void *stack)
239 {
240     STACK_OF(EVP_KDF) *kdf_stack = stack;
241
242     sk_EVP_KDF_push(kdf_stack, kdf);
243     EVP_KDF_up_ref(kdf);
244 }
245
246 static void list_kdfs(void)
247 {
248     STACK_OF(EVP_KDF) *kdfs = sk_EVP_KDF_new(kdf_cmp);
249     int i;
250
251     BIO_printf(bio_out, "Provided KDFs and PDFs:\n");
252     EVP_KDF_do_all_provided(NULL, collect_kdfs, kdfs);
253     sk_EVP_KDF_sort(kdfs);
254     for (i = 0; i < sk_EVP_KDF_num(kdfs); i++) {
255         const EVP_KDF *k = sk_EVP_KDF_value(kdfs, i);
256         STACK_OF(OPENSSL_CSTRING) *names =
257             sk_OPENSSL_CSTRING_new(name_cmp);
258
259         EVP_KDF_names_do_all(k, collect_names, names);
260
261         BIO_printf(bio_out, "  ");
262         print_names(bio_out, names);
263         BIO_printf(bio_out, " @ %s\n",
264                    OSSL_PROVIDER_name(EVP_KDF_provider(k)));
265
266         sk_OPENSSL_CSTRING_free(names);
267
268         if (verbose) {
269             print_param_types("retrievable algorithm parameters",
270                               EVP_KDF_gettable_params(k), 4);
271             print_param_types("retrievable operation parameters",
272                               EVP_KDF_gettable_ctx_params(k), 4);
273             print_param_types("settable operation parameters",
274                               EVP_KDF_settable_ctx_params(k), 4);
275         }
276     }
277     sk_EVP_KDF_pop_free(kdfs, EVP_KDF_free);
278 }
279
280 static void list_missing_help(void)
281 {
282     const FUNCTION *fp;
283     const OPTIONS *o;
284
285     for (fp = functions; fp->name != NULL; fp++) {
286         if ((o = fp->help) != NULL) {
287             /* If there is help, list what flags are not documented. */
288             for ( ; o->name != NULL; o++) {
289                 if (o->helpstr == NULL)
290                     BIO_printf(bio_out, "%s %s\n", fp->name, o->name);
291             }
292         } else if (fp->func != dgst_main) {
293             /* If not aliased to the dgst command, */
294             BIO_printf(bio_out, "%s *\n", fp->name);
295         }
296     }
297 }
298
299 static void list_objects(void)
300 {
301     int max_nid = OBJ_new_nid(0);
302     int i;
303     char *oid_buf = NULL;
304     int oid_size = 0;
305
306     /* Skip 0, since that's NID_undef */
307     for (i = 1; i < max_nid; i++) {
308         const ASN1_OBJECT *obj = OBJ_nid2obj(i);
309         const char *sn = OBJ_nid2sn(i);
310         const char *ln = OBJ_nid2ln(i);
311         int n = 0;
312
313         /*
314          * If one of the retrieved objects somehow generated an error,
315          * we ignore it.  The check for NID_undef below will detect the
316          * error and simply skip to the next NID.
317          */
318         ERR_clear_error();
319
320         if (OBJ_obj2nid(obj) == NID_undef)
321             continue;
322
323         if ((n = OBJ_obj2txt(NULL, 0, obj, 1)) == 0) {
324             BIO_printf(bio_out, "# None-OID object: %s, %s\n", sn, ln);
325             continue;
326         }
327         if (n < 0)
328             break;               /* Error */
329
330         if (n > oid_size) {
331             oid_buf = OPENSSL_realloc(oid_buf, n + 1);
332             if (oid_buf == NULL) {
333                 BIO_printf(bio_err, "ERROR: Memory allocation\n");
334                 break;           /* Error */
335             }
336             oid_size = n + 1;
337         }
338         if (OBJ_obj2txt(oid_buf, oid_size, obj, 1) < 0)
339             break;               /* Error */
340         if (ln == NULL || strcmp(sn, ln) == 0)
341             BIO_printf(bio_out, "%s = %s\n", sn, oid_buf);
342         else
343             BIO_printf(bio_out, "%s = %s, %s\n", sn, ln, oid_buf);
344     }
345
346     OPENSSL_free(oid_buf);
347 }
348
349 static void list_options_for_command(const char *command)
350 {
351     const FUNCTION *fp;
352     const OPTIONS *o;
353
354     for (fp = functions; fp->name != NULL; fp++)
355         if (strcmp(fp->name, command) == 0)
356             break;
357     if (fp->name == NULL) {
358         BIO_printf(bio_err, "Invalid command '%s'; type \"help\" for a list.\n",
359                 command);
360         return;
361     }
362
363     if ((o = fp->help) == NULL)
364         return;
365
366     for ( ; o->name != NULL; o++) {
367         char c = o->valtype;
368
369         if (o->name == OPT_PARAM_STR)
370             break;
371
372         if (o->name == OPT_HELP_STR
373                 || o->name == OPT_MORE_STR
374                 || o->name == OPT_SECTION_STR
375                 || o->name[0] == '\0')
376             continue;
377         BIO_printf(bio_out, "%s %c\n", o->name, c == '\0' ? '-' : c);
378     }
379     /* Always output the -- marker since it is sometimes documented. */
380     BIO_printf(bio_out, "- -\n");
381 }
382
383 static void list_type(FUNC_TYPE ft, int one)
384 {
385     FUNCTION *fp;
386     int i = 0;
387     DISPLAY_COLUMNS dc;
388
389     memset(&dc, 0, sizeof(dc));
390     if (!one)
391         calculate_columns(functions, &dc);
392
393     for (fp = functions; fp->name != NULL; fp++) {
394         if (fp->type != ft)
395             continue;
396         if (one) {
397             BIO_printf(bio_out, "%s\n", fp->name);
398         } else {
399             if (i % dc.columns == 0 && i > 0)
400                 BIO_printf(bio_out, "\n");
401             BIO_printf(bio_out, "%-*s", dc.width, fp->name);
402             i++;
403         }
404     }
405     if (!one)
406         BIO_printf(bio_out, "\n\n");
407 }
408
409 static void list_pkey(void)
410 {
411     int i;
412
413     for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
414         const EVP_PKEY_ASN1_METHOD *ameth;
415         int pkey_id, pkey_base_id, pkey_flags;
416         const char *pinfo, *pem_str;
417         ameth = EVP_PKEY_asn1_get0(i);
418         EVP_PKEY_asn1_get0_info(&pkey_id, &pkey_base_id, &pkey_flags,
419                                 &pinfo, &pem_str, ameth);
420         if (pkey_flags & ASN1_PKEY_ALIAS) {
421             BIO_printf(bio_out, "Name: %s\n", OBJ_nid2ln(pkey_id));
422             BIO_printf(bio_out, "\tAlias for: %s\n",
423                        OBJ_nid2ln(pkey_base_id));
424         } else {
425             BIO_printf(bio_out, "Name: %s\n", pinfo);
426             BIO_printf(bio_out, "\tType: %s Algorithm\n",
427                        pkey_flags & ASN1_PKEY_DYNAMIC ?
428                        "External" : "Builtin");
429             BIO_printf(bio_out, "\tOID: %s\n", OBJ_nid2ln(pkey_id));
430             if (pem_str == NULL)
431                 pem_str = "(none)";
432             BIO_printf(bio_out, "\tPEM string: %s\n", pem_str);
433         }
434
435     }
436 }
437
438 static void list_pkey_meth(void)
439 {
440     size_t i;
441     size_t meth_count = EVP_PKEY_meth_get_count();
442
443     for (i = 0; i < meth_count; i++) {
444         const EVP_PKEY_METHOD *pmeth = EVP_PKEY_meth_get0(i);
445         int pkey_id, pkey_flags;
446
447         EVP_PKEY_meth_get0_info(&pkey_id, &pkey_flags, pmeth);
448         BIO_printf(bio_out, "%s\n", OBJ_nid2ln(pkey_id));
449         BIO_printf(bio_out, "\tType: %s Algorithm\n",
450                    pkey_flags & ASN1_PKEY_DYNAMIC ?  "External" : "Builtin");
451     }
452 }
453
454 static void list_engines(void)
455 {
456 #ifndef OPENSSL_NO_ENGINE
457     ENGINE *e;
458
459     BIO_puts(bio_out, "Engines:\n");
460     e = ENGINE_get_first();
461     while (e) {
462         BIO_printf(bio_out, "%s\n", ENGINE_get_id(e));
463         e = ENGINE_get_next(e);
464     }
465 #else
466     BIO_puts(bio_out, "Engine support is disabled.\n");
467 #endif
468 }
469
470 static void list_disabled(void)
471 {
472     BIO_puts(bio_out, "Disabled algorithms:\n");
473 #ifdef OPENSSL_NO_ARIA
474     BIO_puts(bio_out, "ARIA\n");
475 #endif
476 #ifdef OPENSSL_NO_BF
477     BIO_puts(bio_out, "BF\n");
478 #endif
479 #ifdef OPENSSL_NO_BLAKE2
480     BIO_puts(bio_out, "BLAKE2\n");
481 #endif
482 #ifdef OPENSSL_NO_CAMELLIA
483     BIO_puts(bio_out, "CAMELLIA\n");
484 #endif
485 #ifdef OPENSSL_NO_CAST
486     BIO_puts(bio_out, "CAST\n");
487 #endif
488 #ifdef OPENSSL_NO_CMAC
489     BIO_puts(bio_out, "CMAC\n");
490 #endif
491 #ifdef OPENSSL_NO_CMS
492     BIO_puts(bio_out, "CMS\n");
493 #endif
494 #ifdef OPENSSL_NO_COMP
495     BIO_puts(bio_out, "COMP\n");
496 #endif
497 #ifdef OPENSSL_NO_DES
498     BIO_puts(bio_out, "DES\n");
499 #endif
500 #ifdef OPENSSL_NO_DGRAM
501     BIO_puts(bio_out, "DGRAM\n");
502 #endif
503 #ifdef OPENSSL_NO_DH
504     BIO_puts(bio_out, "DH\n");
505 #endif
506 #ifdef OPENSSL_NO_DSA
507     BIO_puts(bio_out, "DSA\n");
508 #endif
509 #if defined(OPENSSL_NO_DTLS)
510     BIO_puts(bio_out, "DTLS\n");
511 #endif
512 #if defined(OPENSSL_NO_DTLS1)
513     BIO_puts(bio_out, "DTLS1\n");
514 #endif
515 #if defined(OPENSSL_NO_DTLS1_2)
516     BIO_puts(bio_out, "DTLS1_2\n");
517 #endif
518 #ifdef OPENSSL_NO_EC
519     BIO_puts(bio_out, "EC\n");
520 #endif
521 #ifdef OPENSSL_NO_EC2M
522     BIO_puts(bio_out, "EC2M\n");
523 #endif
524 #ifdef OPENSSL_NO_ENGINE
525     BIO_puts(bio_out, "ENGINE\n");
526 #endif
527 #ifdef OPENSSL_NO_GOST
528     BIO_puts(bio_out, "GOST\n");
529 #endif
530 #ifdef OPENSSL_NO_IDEA
531     BIO_puts(bio_out, "IDEA\n");
532 #endif
533 #ifdef OPENSSL_NO_MD2
534     BIO_puts(bio_out, "MD2\n");
535 #endif
536 #ifdef OPENSSL_NO_MD4
537     BIO_puts(bio_out, "MD4\n");
538 #endif
539 #ifdef OPENSSL_NO_MD5
540     BIO_puts(bio_out, "MD5\n");
541 #endif
542 #ifdef OPENSSL_NO_MDC2
543     BIO_puts(bio_out, "MDC2\n");
544 #endif
545 #ifdef OPENSSL_NO_OCB
546     BIO_puts(bio_out, "OCB\n");
547 #endif
548 #ifdef OPENSSL_NO_OCSP
549     BIO_puts(bio_out, "OCSP\n");
550 #endif
551 #ifdef OPENSSL_NO_PSK
552     BIO_puts(bio_out, "PSK\n");
553 #endif
554 #ifdef OPENSSL_NO_RC2
555     BIO_puts(bio_out, "RC2\n");
556 #endif
557 #ifdef OPENSSL_NO_RC4
558     BIO_puts(bio_out, "RC4\n");
559 #endif
560 #ifdef OPENSSL_NO_RC5
561     BIO_puts(bio_out, "RC5\n");
562 #endif
563 #ifdef OPENSSL_NO_RMD160
564     BIO_puts(bio_out, "RMD160\n");
565 #endif
566 #ifdef OPENSSL_NO_RSA
567     BIO_puts(bio_out, "RSA\n");
568 #endif
569 #ifdef OPENSSL_NO_SCRYPT
570     BIO_puts(bio_out, "SCRYPT\n");
571 #endif
572 #ifdef OPENSSL_NO_SCTP
573     BIO_puts(bio_out, "SCTP\n");
574 #endif
575 #ifdef OPENSSL_NO_SEED
576     BIO_puts(bio_out, "SEED\n");
577 #endif
578 #ifdef OPENSSL_NO_SM2
579     BIO_puts(bio_out, "SM2\n");
580 #endif
581 #ifdef OPENSSL_NO_SM3
582     BIO_puts(bio_out, "SM3\n");
583 #endif
584 #ifdef OPENSSL_NO_SM4
585     BIO_puts(bio_out, "SM4\n");
586 #endif
587 #ifdef OPENSSL_NO_SOCK
588     BIO_puts(bio_out, "SOCK\n");
589 #endif
590 #ifdef OPENSSL_NO_SRP
591     BIO_puts(bio_out, "SRP\n");
592 #endif
593 #ifdef OPENSSL_NO_SRTP
594     BIO_puts(bio_out, "SRTP\n");
595 #endif
596 #ifdef OPENSSL_NO_SSL3
597     BIO_puts(bio_out, "SSL3\n");
598 #endif
599 #ifdef OPENSSL_NO_TLS1
600     BIO_puts(bio_out, "TLS1\n");
601 #endif
602 #ifdef OPENSSL_NO_TLS1_1
603     BIO_puts(bio_out, "TLS1_1\n");
604 #endif
605 #ifdef OPENSSL_NO_TLS1_2
606     BIO_puts(bio_out, "TLS1_2\n");
607 #endif
608 #ifdef OPENSSL_NO_WHIRLPOOL
609     BIO_puts(bio_out, "WHIRLPOOL\n");
610 #endif
611 #ifndef ZLIB
612     BIO_puts(bio_out, "ZLIB\n");
613 #endif
614 }
615
616 /* Unified enum for help and list commands. */
617 typedef enum HELPLIST_CHOICE {
618     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, OPT_ONE, OPT_VERBOSE,
619     OPT_COMMANDS, OPT_DIGEST_COMMANDS, OPT_MAC_ALGORITHMS, OPT_OPTIONS,
620     OPT_DIGEST_ALGORITHMS, OPT_CIPHER_COMMANDS, OPT_CIPHER_ALGORITHMS,
621     OPT_PK_ALGORITHMS, OPT_PK_METHOD, OPT_ENGINES, OPT_DISABLED,
622     OPT_KDF_ALGORITHMS, OPT_MISSING_HELP, OPT_OBJECTS,
623     OPT_PROV_ENUM
624 } HELPLIST_CHOICE;
625
626 const OPTIONS list_options[] = {
627
628     OPT_SECTION("General"),
629     {"help", OPT_HELP, '-', "Display this summary"},
630
631     OPT_SECTION("Output"),
632     {"1", OPT_ONE, '-', "List in one column"},
633     {"verbose", OPT_VERBOSE, '-', "Verbose listing"},
634     {"commands", OPT_COMMANDS, '-', "List of standard commands"},
635     {"standard-commands", OPT_COMMANDS, '-', "List of standard commands"},
636     {"digest-commands", OPT_DIGEST_COMMANDS, '-',
637      "List of message digest commands"},
638     {"digest-algorithms", OPT_DIGEST_ALGORITHMS, '-',
639      "List of message digest algorithms"},
640     {"kdf-algorithms", OPT_KDF_ALGORITHMS, '-',
641      "List of key derivation and pseudo random function algorithms"},
642     {"mac-algorithms", OPT_MAC_ALGORITHMS, '-',
643      "List of message authentication code algorithms"},
644     {"cipher-commands", OPT_CIPHER_COMMANDS, '-', "List of cipher commands"},
645     {"cipher-algorithms", OPT_CIPHER_ALGORITHMS, '-',
646      "List of cipher algorithms"},
647     {"public-key-algorithms", OPT_PK_ALGORITHMS, '-',
648      "List of public key algorithms"},
649     {"public-key-methods", OPT_PK_METHOD, '-',
650      "List of public key methods"},
651     {"engines", OPT_ENGINES, '-',
652      "List of loaded engines"},
653     {"disabled", OPT_DISABLED, '-',
654      "List of disabled features"},
655     {"missing-help", OPT_MISSING_HELP, '-',
656      "List missing detailed help strings"},
657     {"options", OPT_OPTIONS, 's',
658      "List options for specified command"},
659     {"objects", OPT_OBJECTS, '-',
660      "List built in objects (OID<->name mappings)"},
661
662     OPT_PROV_OPTIONS,
663     {NULL}
664 };
665
666 int list_main(int argc, char **argv)
667 {
668     char *prog;
669     HELPLIST_CHOICE o;
670     int one = 0, done = 0;
671     struct {
672         unsigned int commands:1;
673         unsigned int digest_commands:1;
674         unsigned int digest_algorithms:1;
675         unsigned int kdf_algorithms:1;
676         unsigned int mac_algorithms:1;
677         unsigned int cipher_commands:1;
678         unsigned int cipher_algorithms:1;
679         unsigned int pk_algorithms:1;
680         unsigned int pk_method:1;
681         unsigned int engines:1;
682         unsigned int disabled:1;
683         unsigned int missing_help:1;
684         unsigned int objects:1;
685         unsigned int options:1;
686     } todo = { 0, };
687
688     verbose = 0;                 /* Clear a possible previous call */
689
690     prog = opt_init(argc, argv, list_options);
691     while ((o = opt_next()) != OPT_EOF) {
692         switch (o) {
693         case OPT_EOF:  /* Never hit, but suppresses warning */
694         case OPT_ERR:
695 opthelp:
696             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
697             return 1;
698         case OPT_HELP:
699             opt_help(list_options);
700             break;
701         case OPT_ONE:
702             one = 1;
703             break;
704         case OPT_COMMANDS:
705             todo.commands = 1;
706             break;
707         case OPT_DIGEST_COMMANDS:
708             todo.digest_commands = 1;
709             break;
710         case OPT_DIGEST_ALGORITHMS:
711             todo.digest_algorithms = 1;
712             break;
713         case OPT_KDF_ALGORITHMS:
714             todo.kdf_algorithms = 1;
715             break;
716         case OPT_MAC_ALGORITHMS:
717             todo.mac_algorithms = 1;
718             break;
719         case OPT_CIPHER_COMMANDS:
720             todo.cipher_commands = 1;
721             break;
722         case OPT_CIPHER_ALGORITHMS:
723             todo.cipher_algorithms = 1;
724             break;
725         case OPT_PK_ALGORITHMS:
726             todo.pk_algorithms = 1;
727             break;
728         case OPT_PK_METHOD:
729             todo.pk_method = 1;
730             break;
731         case OPT_ENGINES:
732             todo.engines = 1;
733             break;
734         case OPT_DISABLED:
735             todo.disabled = 1;
736             break;
737         case OPT_MISSING_HELP:
738             todo.missing_help = 1;
739             break;
740         case OPT_OBJECTS:
741             todo.objects = 1;
742             break;
743         case OPT_OPTIONS:
744             list_options_for_command(opt_arg());
745             break;
746         case OPT_VERBOSE:
747             verbose = 1;
748             break;
749         case OPT_PROV_CASES:
750             if (!opt_provider(o))
751                 return 1;
752             break;
753         }
754         done = 1;
755     }
756     if (opt_num_rest() != 0) {
757         BIO_printf(bio_err, "Extra arguments given.\n");
758         goto opthelp;
759     }
760
761     if (todo.commands)
762         list_type(FT_general, one);
763     if (todo.digest_commands)
764         list_type(FT_md, one);
765     if (todo.digest_algorithms)
766         list_digests();
767     if (todo.kdf_algorithms)
768         list_kdfs();
769     if (todo.mac_algorithms)
770         list_macs();
771     if (todo.cipher_commands)
772         list_type(FT_cipher, one);
773     if (todo.cipher_algorithms)
774         list_ciphers();
775     if (todo.pk_algorithms)
776         list_pkey();
777     if (todo.pk_method)
778         list_pkey_meth();
779     if (todo.engines)
780         list_engines();
781     if (todo.disabled)
782         list_disabled();
783     if (todo.missing_help)
784         list_missing_help();
785     if (todo.objects)
786         list_objects();
787
788     if (!done)
789         goto opthelp;
790
791     return 0;
792 }