b2ddef920191f22487a5788be139a0db968abe3f
[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 /* We need to use some deprecated APIs */
11 #define OPENSSL_SUPPRESS_DEPRECATED
12
13 #include <string.h>
14 #include <openssl/evp.h>
15 #include <openssl/err.h>
16 #include <openssl/provider.h>
17 #include <openssl/safestack.h>
18 #include <openssl/kdf.h>
19 #include <openssl/encoder.h>
20 #include <openssl/decoder.h>
21 #include <openssl/core_names.h>
22 #include <openssl/rand.h>
23 #include "apps.h"
24 #include "app_params.h"
25 #include "progs.h"
26 #include "opt.h"
27 #include "names.h"
28
29 static int verbose = 0;
30
31 static void legacy_cipher_fn(const EVP_CIPHER *c,
32                              const char *from, const char *to, void *arg)
33 {
34     if (c != NULL) {
35         BIO_printf(arg, "  %s\n", EVP_CIPHER_name(c));
36     } else {
37         if (from == NULL)
38             from = "<undefined>";
39         if (to == NULL)
40             to = "<undefined>";
41         BIO_printf(arg, "  %s => %s\n", from, to);
42     }
43 }
44
45 DEFINE_STACK_OF(EVP_CIPHER)
46 static int cipher_cmp(const EVP_CIPHER * const *a,
47                       const EVP_CIPHER * const *b)
48 {
49     int ret = EVP_CIPHER_number(*a) - EVP_CIPHER_number(*b);
50
51     if (ret == 0)
52         ret = strcmp(OSSL_PROVIDER_name(EVP_CIPHER_provider(*a)),
53                      OSSL_PROVIDER_name(EVP_CIPHER_provider(*b)));
54
55     return ret;
56 }
57
58 static void collect_ciphers(EVP_CIPHER *cipher, void *stack)
59 {
60     STACK_OF(EVP_CIPHER) *cipher_stack = stack;
61
62     if (sk_EVP_CIPHER_push(cipher_stack, cipher) > 0)
63         EVP_CIPHER_up_ref(cipher);
64 }
65
66 static void list_ciphers(void)
67 {
68     STACK_OF(EVP_CIPHER) *ciphers = sk_EVP_CIPHER_new(cipher_cmp);
69     int i;
70
71     if (ciphers == NULL) {
72         BIO_printf(bio_err, "ERROR: Memory allocation\n");
73         return;
74     }
75     BIO_printf(bio_out, "Legacy:\n");
76     EVP_CIPHER_do_all_sorted(legacy_cipher_fn, bio_out);
77
78     BIO_printf(bio_out, "Provided:\n");
79     EVP_CIPHER_do_all_provided(NULL, collect_ciphers, ciphers);
80     sk_EVP_CIPHER_sort(ciphers);
81     for (i = 0; i < sk_EVP_CIPHER_num(ciphers); i++) {
82         const EVP_CIPHER *c = sk_EVP_CIPHER_value(ciphers, i);
83         STACK_OF(OPENSSL_CSTRING) *names =
84             sk_OPENSSL_CSTRING_new(name_cmp);
85
86         EVP_CIPHER_names_do_all(c, collect_names, names);
87
88         BIO_printf(bio_out, "  ");
89         print_names(bio_out, names);
90         BIO_printf(bio_out, " @ %s\n",
91                    OSSL_PROVIDER_name(EVP_CIPHER_provider(c)));
92
93         sk_OPENSSL_CSTRING_free(names);
94
95         if (verbose) {
96             print_param_types("retrievable algorithm parameters",
97                               EVP_CIPHER_gettable_params(c), 4);
98             print_param_types("retrievable operation parameters",
99                               EVP_CIPHER_gettable_ctx_params(c), 4);
100             print_param_types("settable operation parameters",
101                               EVP_CIPHER_settable_ctx_params(c), 4);
102         }
103     }
104     sk_EVP_CIPHER_pop_free(ciphers, EVP_CIPHER_free);
105 }
106
107 static void list_md_fn(const EVP_MD *m,
108                        const char *from, const char *to, void *arg)
109 {
110     if (m != NULL) {
111         BIO_printf(arg, "  %s\n", EVP_MD_name(m));
112     } else {
113         if (from == NULL)
114             from = "<undefined>";
115         if (to == NULL)
116             to = "<undefined>";
117         BIO_printf((BIO *)arg, "  %s => %s\n", from, to);
118     }
119 }
120
121 DEFINE_STACK_OF(EVP_MD)
122 static int md_cmp(const EVP_MD * const *a, const EVP_MD * const *b)
123 {
124     int ret = EVP_MD_number(*a) - EVP_MD_number(*b);
125
126     if (ret == 0)
127         ret = strcmp(OSSL_PROVIDER_name(EVP_MD_provider(*a)),
128                      OSSL_PROVIDER_name(EVP_MD_provider(*b)));
129
130     return ret;
131 }
132
133 static void collect_digests(EVP_MD *md, void *stack)
134 {
135     STACK_OF(EVP_MD) *digest_stack = stack;
136
137     if (sk_EVP_MD_push(digest_stack, md) > 0)
138         EVP_MD_up_ref(md);
139 }
140
141 static void list_digests(void)
142 {
143     STACK_OF(EVP_MD) *digests = sk_EVP_MD_new(md_cmp);
144     int i;
145
146     if (digests == NULL) {
147         BIO_printf(bio_err, "ERROR: Memory allocation\n");
148         return;
149     }
150     BIO_printf(bio_out, "Legacy:\n");
151     EVP_MD_do_all_sorted(list_md_fn, bio_out);
152
153     BIO_printf(bio_out, "Provided:\n");
154     EVP_MD_do_all_provided(NULL, collect_digests, digests);
155     sk_EVP_MD_sort(digests);
156     for (i = 0; i < sk_EVP_MD_num(digests); i++) {
157         const EVP_MD *m = sk_EVP_MD_value(digests, i);
158         STACK_OF(OPENSSL_CSTRING) *names =
159             sk_OPENSSL_CSTRING_new(name_cmp);
160
161         EVP_MD_names_do_all(m, collect_names, names);
162
163         BIO_printf(bio_out, "  ");
164         print_names(bio_out, names);
165         BIO_printf(bio_out, " @ %s\n",
166                    OSSL_PROVIDER_name(EVP_MD_provider(m)));
167
168         sk_OPENSSL_CSTRING_free(names);
169
170         if (verbose) {
171             print_param_types("retrievable algorithm parameters",
172                               EVP_MD_gettable_params(m), 4);
173             print_param_types("retrievable operation parameters",
174                               EVP_MD_gettable_ctx_params(m), 4);
175             print_param_types("settable operation parameters",
176                               EVP_MD_settable_ctx_params(m), 4);
177         }
178     }
179     sk_EVP_MD_pop_free(digests, EVP_MD_free);
180 }
181
182 DEFINE_STACK_OF(EVP_MAC)
183 static int mac_cmp(const EVP_MAC * const *a, const EVP_MAC * const *b)
184 {
185     int ret = EVP_MAC_number(*a) - EVP_MAC_number(*b);
186
187     if (ret == 0)
188         ret = strcmp(OSSL_PROVIDER_name(EVP_MAC_provider(*a)),
189                      OSSL_PROVIDER_name(EVP_MAC_provider(*b)));
190
191     return ret;
192 }
193
194 static void collect_macs(EVP_MAC *mac, void *stack)
195 {
196     STACK_OF(EVP_MAC) *mac_stack = stack;
197
198     if (sk_EVP_MAC_push(mac_stack, mac) > 0)
199         EVP_MAC_up_ref(mac);
200 }
201
202 static void list_macs(void)
203 {
204     STACK_OF(EVP_MAC) *macs = sk_EVP_MAC_new(mac_cmp);
205     int i;
206
207     if (macs == NULL) {
208         BIO_printf(bio_err, "ERROR: Memory allocation\n");
209         return;
210     }
211     BIO_printf(bio_out, "Provided MACs:\n");
212     EVP_MAC_do_all_provided(NULL, collect_macs, macs);
213     sk_EVP_MAC_sort(macs);
214     for (i = 0; i < sk_EVP_MAC_num(macs); i++) {
215         const EVP_MAC *m = sk_EVP_MAC_value(macs, i);
216         STACK_OF(OPENSSL_CSTRING) *names =
217             sk_OPENSSL_CSTRING_new(name_cmp);
218
219         EVP_MAC_names_do_all(m, collect_names, names);
220
221         BIO_printf(bio_out, "  ");
222         print_names(bio_out, names);
223         BIO_printf(bio_out, " @ %s\n",
224                    OSSL_PROVIDER_name(EVP_MAC_provider(m)));
225
226         sk_OPENSSL_CSTRING_free(names);
227
228         if (verbose) {
229             print_param_types("retrievable algorithm parameters",
230                               EVP_MAC_gettable_params(m), 4);
231             print_param_types("retrievable operation parameters",
232                               EVP_MAC_gettable_ctx_params(m), 4);
233             print_param_types("settable operation parameters",
234                               EVP_MAC_settable_ctx_params(m), 4);
235         }
236     }
237     sk_EVP_MAC_pop_free(macs, EVP_MAC_free);
238 }
239
240 /*
241  * KDFs and PRFs
242  */
243 DEFINE_STACK_OF(EVP_KDF)
244 static int kdf_cmp(const EVP_KDF * const *a, const EVP_KDF * const *b)
245 {
246     int ret = EVP_KDF_number(*a) - EVP_KDF_number(*b);
247
248     if (ret == 0)
249         ret = strcmp(OSSL_PROVIDER_name(EVP_KDF_provider(*a)),
250                      OSSL_PROVIDER_name(EVP_KDF_provider(*b)));
251
252     return ret;
253 }
254
255 static void collect_kdfs(EVP_KDF *kdf, void *stack)
256 {
257     STACK_OF(EVP_KDF) *kdf_stack = stack;
258
259     sk_EVP_KDF_push(kdf_stack, kdf);
260     EVP_KDF_up_ref(kdf);
261 }
262
263 static void list_kdfs(void)
264 {
265     STACK_OF(EVP_KDF) *kdfs = sk_EVP_KDF_new(kdf_cmp);
266     int i;
267
268     if (kdfs == NULL) {
269         BIO_printf(bio_err, "ERROR: Memory allocation\n");
270         return;
271     }
272     BIO_printf(bio_out, "Provided KDFs and PDFs:\n");
273     EVP_KDF_do_all_provided(NULL, collect_kdfs, kdfs);
274     sk_EVP_KDF_sort(kdfs);
275     for (i = 0; i < sk_EVP_KDF_num(kdfs); i++) {
276         const EVP_KDF *k = sk_EVP_KDF_value(kdfs, i);
277         STACK_OF(OPENSSL_CSTRING) *names =
278             sk_OPENSSL_CSTRING_new(name_cmp);
279
280         EVP_KDF_names_do_all(k, collect_names, names);
281
282         BIO_printf(bio_out, "  ");
283         print_names(bio_out, names);
284         BIO_printf(bio_out, " @ %s\n",
285                    OSSL_PROVIDER_name(EVP_KDF_provider(k)));
286
287         sk_OPENSSL_CSTRING_free(names);
288
289         if (verbose) {
290             print_param_types("retrievable algorithm parameters",
291                               EVP_KDF_gettable_params(k), 4);
292             print_param_types("retrievable operation parameters",
293                               EVP_KDF_gettable_ctx_params(k), 4);
294             print_param_types("settable operation parameters",
295                               EVP_KDF_settable_ctx_params(k), 4);
296         }
297     }
298     sk_EVP_KDF_pop_free(kdfs, EVP_KDF_free);
299 }
300
301 /*
302  * RANDs
303  */
304 DEFINE_STACK_OF(EVP_RAND)
305
306 static int rand_cmp(const EVP_RAND * const *a, const EVP_RAND * const *b)
307 {
308     int ret = strcasecmp(EVP_RAND_name(*a), EVP_RAND_name(*b));
309
310     if (ret == 0)
311         ret = strcmp(OSSL_PROVIDER_name(EVP_RAND_provider(*a)),
312                      OSSL_PROVIDER_name(EVP_RAND_provider(*b)));
313
314     return ret;
315 }
316
317 static void collect_rands(EVP_RAND *rand, void *stack)
318 {
319     STACK_OF(EVP_RAND) *rand_stack = stack;
320
321     sk_EVP_RAND_push(rand_stack, rand);
322     EVP_RAND_up_ref(rand);
323 }
324
325 static void list_random_generators(void)
326 {
327     STACK_OF(EVP_RAND) *rands = sk_EVP_RAND_new(rand_cmp);
328     int i;
329
330     if (rands == NULL) {
331         BIO_printf(bio_err, "ERROR: Memory allocation\n");
332         return;
333     }
334     BIO_printf(bio_out, "Provided RNGs and seed sources:\n");
335     EVP_RAND_do_all_provided(NULL, collect_rands, rands);
336     sk_EVP_RAND_sort(rands);
337     for (i = 0; i < sk_EVP_RAND_num(rands); i++) {
338         const EVP_RAND *m = sk_EVP_RAND_value(rands, i);
339
340         BIO_printf(bio_out, "  %s", EVP_RAND_name(m));
341         BIO_printf(bio_out, " @ %s\n",
342                    OSSL_PROVIDER_name(EVP_RAND_provider(m)));
343
344         if (verbose) {
345             print_param_types("retrievable algorithm parameters",
346                               EVP_RAND_gettable_params(m), 4);
347             print_param_types("retrievable operation parameters",
348                               EVP_RAND_gettable_ctx_params(m), 4);
349             print_param_types("settable operation parameters",
350                               EVP_RAND_settable_ctx_params(m), 4);
351         }
352     }
353     sk_EVP_RAND_pop_free(rands, EVP_RAND_free);
354 }
355
356 static void display_random(const char *name, EVP_RAND_CTX *drbg)
357 {
358     EVP_RAND *rand;
359     uint64_t u;
360     const char *p;
361     const OSSL_PARAM *gettables;
362     OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
363     unsigned char buf[1000];
364
365     BIO_printf(bio_out, "%s:\n", name);
366     if (drbg != NULL) {
367         rand = EVP_RAND_CTX_rand(drbg);
368
369         BIO_printf(bio_out, "  %s", EVP_RAND_name(rand));
370         BIO_printf(bio_out, " @ %s\n",
371                    OSSL_PROVIDER_name(EVP_RAND_provider(rand)));
372
373         switch (EVP_RAND_state(drbg)) {
374         case EVP_RAND_STATE_UNINITIALISED:
375             p = "uninitialised";
376             break;
377         case EVP_RAND_STATE_READY:
378             p = "ready";
379             break;
380         case EVP_RAND_STATE_ERROR:
381             p = "error";
382             break;
383         default:
384             p = "unknown";
385             break;
386         }
387         BIO_printf(bio_out, "  state = %s\n", p);
388
389         gettables = EVP_RAND_gettable_ctx_params(rand);
390         if (gettables != NULL)
391             for (; gettables->key != NULL; gettables++) {
392                 /* State has been dealt with already, so ignore */
393                 if (strcasecmp(gettables->key, OSSL_RAND_PARAM_STATE) == 0)
394                     continue;
395                 /* Outside of verbose mode, we skip non-string values */
396                 if (gettables->data_type != OSSL_PARAM_UTF8_STRING
397                         && gettables->data_type != OSSL_PARAM_UTF8_PTR
398                         && !verbose)
399                     continue;
400                 params->key = gettables->key;
401                 params->data_type = gettables->data_type;
402                 if (gettables->data_type == OSSL_PARAM_UNSIGNED_INTEGER
403                         || gettables->data_type == OSSL_PARAM_INTEGER) {
404                     params->data = &u;
405                     params->data_size = sizeof(u);
406                 } else {
407                     params->data = buf;
408                     params->data_size = sizeof(buf);
409                 }
410                 params->return_size = 0;
411                 if (EVP_RAND_get_ctx_params(drbg, params))
412                     print_param_value(params, 2);
413             }
414     }
415 }
416
417 static void list_random_instances(void)
418 {
419     display_random("primary", RAND_get0_primary(NULL));
420     display_random("public", RAND_get0_public(NULL));
421     display_random("private", RAND_get0_private(NULL));
422 }
423
424 /*
425  * Encoders
426  */
427 DEFINE_STACK_OF(OSSL_ENCODER)
428 static int encoder_cmp(const OSSL_ENCODER * const *a,
429                        const OSSL_ENCODER * const *b)
430 {
431     int ret = OSSL_ENCODER_number(*a) - OSSL_ENCODER_number(*b);
432
433     if (ret == 0)
434         ret = strcmp(OSSL_PROVIDER_name(OSSL_ENCODER_provider(*a)),
435                      OSSL_PROVIDER_name(OSSL_ENCODER_provider(*b)));
436     return ret;
437 }
438
439 static void collect_encoders(OSSL_ENCODER *encoder, void *stack)
440 {
441     STACK_OF(OSSL_ENCODER) *encoder_stack = stack;
442
443     sk_OSSL_ENCODER_push(encoder_stack, encoder);
444     OSSL_ENCODER_up_ref(encoder);
445 }
446
447 static void list_encoders(void)
448 {
449     STACK_OF(OSSL_ENCODER) *encoders;
450     int i;
451
452     encoders = sk_OSSL_ENCODER_new(encoder_cmp);
453     if (encoders == NULL) {
454         BIO_printf(bio_err, "ERROR: Memory allocation\n");
455         return;
456     }
457     BIO_printf(bio_out, "Provided ENCODERs:\n");
458     OSSL_ENCODER_do_all_provided(NULL, collect_encoders, encoders);
459     sk_OSSL_ENCODER_sort(encoders);
460
461     for (i = 0; i < sk_OSSL_ENCODER_num(encoders); i++) {
462         OSSL_ENCODER *k = sk_OSSL_ENCODER_value(encoders, i);
463         STACK_OF(OPENSSL_CSTRING) *names =
464             sk_OPENSSL_CSTRING_new(name_cmp);
465
466         OSSL_ENCODER_names_do_all(k, collect_names, names);
467
468         BIO_printf(bio_out, "  ");
469         print_names(bio_out, names);
470         BIO_printf(bio_out, " @ %s (%s)\n",
471                    OSSL_PROVIDER_name(OSSL_ENCODER_provider(k)),
472                    OSSL_ENCODER_properties(k));
473
474         sk_OPENSSL_CSTRING_free(names);
475
476         if (verbose) {
477             print_param_types("settable operation parameters",
478                               OSSL_ENCODER_settable_ctx_params(k), 4);
479         }
480     }
481     sk_OSSL_ENCODER_pop_free(encoders, OSSL_ENCODER_free);
482 }
483
484 /*
485  * Decoders
486  */
487 DEFINE_STACK_OF(OSSL_DECODER)
488 static int decoder_cmp(const OSSL_DECODER * const *a,
489                        const OSSL_DECODER * const *b)
490 {
491     int ret = OSSL_DECODER_number(*a) - OSSL_DECODER_number(*b);
492
493     if (ret == 0)
494         ret = strcmp(OSSL_PROVIDER_name(OSSL_DECODER_provider(*a)),
495                      OSSL_PROVIDER_name(OSSL_DECODER_provider(*b)));
496     return ret;
497 }
498
499 static void collect_decoders(OSSL_DECODER *decoder, void *stack)
500 {
501     STACK_OF(OSSL_DECODER) *decoder_stack = stack;
502
503     sk_OSSL_DECODER_push(decoder_stack, decoder);
504     OSSL_DECODER_up_ref(decoder);
505 }
506
507 static void list_decoders(void)
508 {
509     STACK_OF(OSSL_DECODER) *decoders;
510     int i;
511
512     decoders = sk_OSSL_DECODER_new(decoder_cmp);
513     if (decoders == NULL) {
514         BIO_printf(bio_err, "ERROR: Memory allocation\n");
515         return;
516     }
517     BIO_printf(bio_out, "Provided DECODERs:\n");
518     OSSL_DECODER_do_all_provided(NULL, collect_decoders,
519                                  decoders);
520     sk_OSSL_DECODER_sort(decoders);
521
522     for (i = 0; i < sk_OSSL_DECODER_num(decoders); i++) {
523         OSSL_DECODER *k = sk_OSSL_DECODER_value(decoders, i);
524         STACK_OF(OPENSSL_CSTRING) *names =
525             sk_OPENSSL_CSTRING_new(name_cmp);
526
527         OSSL_DECODER_names_do_all(k, collect_names, names);
528
529         BIO_printf(bio_out, "  ");
530         print_names(bio_out, names);
531         BIO_printf(bio_out, " @ %s (%s)\n",
532                    OSSL_PROVIDER_name(OSSL_DECODER_provider(k)),
533                    OSSL_DECODER_properties(k));
534
535         sk_OPENSSL_CSTRING_free(names);
536
537         if (verbose) {
538             print_param_types("settable operation parameters",
539                               OSSL_DECODER_settable_ctx_params(k), 4);
540         }
541     }
542     sk_OSSL_DECODER_pop_free(decoders, OSSL_DECODER_free);
543 }
544
545 static void list_missing_help(void)
546 {
547     const FUNCTION *fp;
548     const OPTIONS *o;
549
550     for (fp = functions; fp->name != NULL; fp++) {
551         if ((o = fp->help) != NULL) {
552             /* If there is help, list what flags are not documented. */
553             for ( ; o->name != NULL; o++) {
554                 if (o->helpstr == NULL)
555                     BIO_printf(bio_out, "%s %s\n", fp->name, o->name);
556             }
557         } else if (fp->func != dgst_main) {
558             /* If not aliased to the dgst command, */
559             BIO_printf(bio_out, "%s *\n", fp->name);
560         }
561     }
562 }
563
564 static void list_objects(void)
565 {
566     int max_nid = OBJ_new_nid(0);
567     int i;
568     char *oid_buf = NULL;
569     int oid_size = 0;
570
571     /* Skip 0, since that's NID_undef */
572     for (i = 1; i < max_nid; i++) {
573         const ASN1_OBJECT *obj = OBJ_nid2obj(i);
574         const char *sn = OBJ_nid2sn(i);
575         const char *ln = OBJ_nid2ln(i);
576         int n = 0;
577
578         /*
579          * If one of the retrieved objects somehow generated an error,
580          * we ignore it.  The check for NID_undef below will detect the
581          * error and simply skip to the next NID.
582          */
583         ERR_clear_error();
584
585         if (OBJ_obj2nid(obj) == NID_undef)
586             continue;
587
588         if ((n = OBJ_obj2txt(NULL, 0, obj, 1)) == 0) {
589             BIO_printf(bio_out, "# None-OID object: %s, %s\n", sn, ln);
590             continue;
591         }
592         if (n < 0)
593             break;               /* Error */
594
595         if (n > oid_size) {
596             oid_buf = OPENSSL_realloc(oid_buf, n + 1);
597             if (oid_buf == NULL) {
598                 BIO_printf(bio_err, "ERROR: Memory allocation\n");
599                 break;           /* Error */
600             }
601             oid_size = n + 1;
602         }
603         if (OBJ_obj2txt(oid_buf, oid_size, obj, 1) < 0)
604             break;               /* Error */
605         if (ln == NULL || strcmp(sn, ln) == 0)
606             BIO_printf(bio_out, "%s = %s\n", sn, oid_buf);
607         else
608             BIO_printf(bio_out, "%s = %s, %s\n", sn, ln, oid_buf);
609     }
610
611     OPENSSL_free(oid_buf);
612 }
613
614 static void list_options_for_command(const char *command)
615 {
616     const FUNCTION *fp;
617     const OPTIONS *o;
618
619     for (fp = functions; fp->name != NULL; fp++)
620         if (strcmp(fp->name, command) == 0)
621             break;
622     if (fp->name == NULL) {
623         BIO_printf(bio_err, "Invalid command '%s'; type \"help\" for a list.\n",
624                    command);
625         return;
626     }
627
628     if ((o = fp->help) == NULL)
629         return;
630
631     for ( ; o->name != NULL; o++) {
632         char c = o->valtype;
633
634         if (o->name == OPT_PARAM_STR)
635             break;
636
637         if (o->name == OPT_HELP_STR
638                 || o->name == OPT_MORE_STR
639                 || o->name == OPT_SECTION_STR
640                 || o->name[0] == '\0')
641             continue;
642         BIO_printf(bio_out, "%s %c\n", o->name, c == '\0' ? '-' : c);
643     }
644     /* Always output the -- marker since it is sometimes documented. */
645     BIO_printf(bio_out, "- -\n");
646 }
647
648 static void list_type(FUNC_TYPE ft, int one)
649 {
650     FUNCTION *fp;
651     int i = 0;
652     DISPLAY_COLUMNS dc;
653
654     memset(&dc, 0, sizeof(dc));
655     if (!one)
656         calculate_columns(functions, &dc);
657
658     for (fp = functions; fp->name != NULL; fp++) {
659         if (fp->type != ft)
660             continue;
661         if (one) {
662             BIO_printf(bio_out, "%s\n", fp->name);
663         } else {
664             if (i % dc.columns == 0 && i > 0)
665                 BIO_printf(bio_out, "\n");
666             BIO_printf(bio_out, "%-*s", dc.width, fp->name);
667             i++;
668         }
669     }
670     if (!one)
671         BIO_printf(bio_out, "\n\n");
672 }
673
674 static void list_pkey(void)
675 {
676     int i;
677
678     for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
679         const EVP_PKEY_ASN1_METHOD *ameth;
680         int pkey_id, pkey_base_id, pkey_flags;
681         const char *pinfo, *pem_str;
682         ameth = EVP_PKEY_asn1_get0(i);
683         EVP_PKEY_asn1_get0_info(&pkey_id, &pkey_base_id, &pkey_flags,
684                                 &pinfo, &pem_str, ameth);
685         if (pkey_flags & ASN1_PKEY_ALIAS) {
686             BIO_printf(bio_out, "Name: %s\n", OBJ_nid2ln(pkey_id));
687             BIO_printf(bio_out, "\tAlias for: %s\n",
688                        OBJ_nid2ln(pkey_base_id));
689         } else {
690             BIO_printf(bio_out, "Name: %s\n", pinfo);
691             BIO_printf(bio_out, "\tType: %s Algorithm\n",
692                        pkey_flags & ASN1_PKEY_DYNAMIC ?
693                        "External" : "Builtin");
694             BIO_printf(bio_out, "\tOID: %s\n", OBJ_nid2ln(pkey_id));
695             if (pem_str == NULL)
696                 pem_str = "(none)";
697             BIO_printf(bio_out, "\tPEM string: %s\n", pem_str);
698         }
699
700     }
701 }
702
703 #ifndef OPENSSL_NO_DEPRECATED_3_0
704 static void list_pkey_meth(void)
705 {
706     size_t i;
707     size_t meth_count = EVP_PKEY_meth_get_count();
708
709     for (i = 0; i < meth_count; i++) {
710         const EVP_PKEY_METHOD *pmeth = EVP_PKEY_meth_get0(i);
711         int pkey_id, pkey_flags;
712
713         EVP_PKEY_meth_get0_info(&pkey_id, &pkey_flags, pmeth);
714         BIO_printf(bio_out, "%s\n", OBJ_nid2ln(pkey_id));
715         BIO_printf(bio_out, "\tType: %s Algorithm\n",
716                    pkey_flags & ASN1_PKEY_DYNAMIC ?  "External" : "Builtin");
717     }
718 }
719 #endif
720
721 #ifndef OPENSSL_NO_DEPRECATED_3_0
722 static void list_engines(void)
723 {
724 # ifndef OPENSSL_NO_ENGINE
725     ENGINE *e;
726
727     BIO_puts(bio_out, "Engines:\n");
728     e = ENGINE_get_first();
729     while (e) {
730         BIO_printf(bio_out, "%s\n", ENGINE_get_id(e));
731         e = ENGINE_get_next(e);
732     }
733 # else
734     BIO_puts(bio_out, "Engine support is disabled.\n");
735 # endif
736 }
737 #endif
738
739 static void list_disabled(void)
740 {
741     BIO_puts(bio_out, "Disabled algorithms:\n");
742 #ifdef OPENSSL_NO_ARIA
743     BIO_puts(bio_out, "ARIA\n");
744 #endif
745 #ifdef OPENSSL_NO_BF
746     BIO_puts(bio_out, "BF\n");
747 #endif
748 #ifdef OPENSSL_NO_BLAKE2
749     BIO_puts(bio_out, "BLAKE2\n");
750 #endif
751 #ifdef OPENSSL_NO_CAMELLIA
752     BIO_puts(bio_out, "CAMELLIA\n");
753 #endif
754 #ifdef OPENSSL_NO_CAST
755     BIO_puts(bio_out, "CAST\n");
756 #endif
757 #ifdef OPENSSL_NO_CMAC
758     BIO_puts(bio_out, "CMAC\n");
759 #endif
760 #ifdef OPENSSL_NO_CMS
761     BIO_puts(bio_out, "CMS\n");
762 #endif
763 #ifdef OPENSSL_NO_COMP
764     BIO_puts(bio_out, "COMP\n");
765 #endif
766 #ifdef OPENSSL_NO_DES
767     BIO_puts(bio_out, "DES\n");
768 #endif
769 #ifdef OPENSSL_NO_DGRAM
770     BIO_puts(bio_out, "DGRAM\n");
771 #endif
772 #ifdef OPENSSL_NO_DH
773     BIO_puts(bio_out, "DH\n");
774 #endif
775 #ifdef OPENSSL_NO_DSA
776     BIO_puts(bio_out, "DSA\n");
777 #endif
778 #if defined(OPENSSL_NO_DTLS)
779     BIO_puts(bio_out, "DTLS\n");
780 #endif
781 #if defined(OPENSSL_NO_DTLS1)
782     BIO_puts(bio_out, "DTLS1\n");
783 #endif
784 #if defined(OPENSSL_NO_DTLS1_2)
785     BIO_puts(bio_out, "DTLS1_2\n");
786 #endif
787 #ifdef OPENSSL_NO_EC
788     BIO_puts(bio_out, "EC\n");
789 #endif
790 #ifdef OPENSSL_NO_EC2M
791     BIO_puts(bio_out, "EC2M\n");
792 #endif
793 #if defined(OPENSSL_NO_ENGINE) && !defined(OPENSSL_NO_DEPRECATED_3_0)
794     BIO_puts(bio_out, "ENGINE\n");
795 #endif
796 #ifdef OPENSSL_NO_GOST
797     BIO_puts(bio_out, "GOST\n");
798 #endif
799 #ifdef OPENSSL_NO_IDEA
800     BIO_puts(bio_out, "IDEA\n");
801 #endif
802 #ifdef OPENSSL_NO_MD2
803     BIO_puts(bio_out, "MD2\n");
804 #endif
805 #ifdef OPENSSL_NO_MD4
806     BIO_puts(bio_out, "MD4\n");
807 #endif
808 #ifdef OPENSSL_NO_MD5
809     BIO_puts(bio_out, "MD5\n");
810 #endif
811 #ifdef OPENSSL_NO_MDC2
812     BIO_puts(bio_out, "MDC2\n");
813 #endif
814 #ifdef OPENSSL_NO_OCB
815     BIO_puts(bio_out, "OCB\n");
816 #endif
817 #ifdef OPENSSL_NO_OCSP
818     BIO_puts(bio_out, "OCSP\n");
819 #endif
820 #ifdef OPENSSL_NO_PSK
821     BIO_puts(bio_out, "PSK\n");
822 #endif
823 #ifdef OPENSSL_NO_RC2
824     BIO_puts(bio_out, "RC2\n");
825 #endif
826 #ifdef OPENSSL_NO_RC4
827     BIO_puts(bio_out, "RC4\n");
828 #endif
829 #ifdef OPENSSL_NO_RC5
830     BIO_puts(bio_out, "RC5\n");
831 #endif
832 #ifdef OPENSSL_NO_RMD160
833     BIO_puts(bio_out, "RMD160\n");
834 #endif
835 #ifdef OPENSSL_NO_RSA
836     BIO_puts(bio_out, "RSA\n");
837 #endif
838 #ifdef OPENSSL_NO_SCRYPT
839     BIO_puts(bio_out, "SCRYPT\n");
840 #endif
841 #ifdef OPENSSL_NO_SCTP
842     BIO_puts(bio_out, "SCTP\n");
843 #endif
844 #ifdef OPENSSL_NO_SEED
845     BIO_puts(bio_out, "SEED\n");
846 #endif
847 #ifdef OPENSSL_NO_SM2
848     BIO_puts(bio_out, "SM2\n");
849 #endif
850 #ifdef OPENSSL_NO_SM3
851     BIO_puts(bio_out, "SM3\n");
852 #endif
853 #ifdef OPENSSL_NO_SM4
854     BIO_puts(bio_out, "SM4\n");
855 #endif
856 #ifdef OPENSSL_NO_SOCK
857     BIO_puts(bio_out, "SOCK\n");
858 #endif
859 #ifdef OPENSSL_NO_SRP
860     BIO_puts(bio_out, "SRP\n");
861 #endif
862 #ifdef OPENSSL_NO_SRTP
863     BIO_puts(bio_out, "SRTP\n");
864 #endif
865 #ifdef OPENSSL_NO_SSL3
866     BIO_puts(bio_out, "SSL3\n");
867 #endif
868 #ifdef OPENSSL_NO_TLS1
869     BIO_puts(bio_out, "TLS1\n");
870 #endif
871 #ifdef OPENSSL_NO_TLS1_1
872     BIO_puts(bio_out, "TLS1_1\n");
873 #endif
874 #ifdef OPENSSL_NO_TLS1_2
875     BIO_puts(bio_out, "TLS1_2\n");
876 #endif
877 #ifdef OPENSSL_NO_WHIRLPOOL
878     BIO_puts(bio_out, "WHIRLPOOL\n");
879 #endif
880 #ifndef ZLIB
881     BIO_puts(bio_out, "ZLIB\n");
882 #endif
883 }
884
885 /* Unified enum for help and list commands. */
886 typedef enum HELPLIST_CHOICE {
887     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, OPT_ONE, OPT_VERBOSE,
888     OPT_COMMANDS, OPT_DIGEST_COMMANDS, OPT_MAC_ALGORITHMS, OPT_OPTIONS,
889     OPT_DIGEST_ALGORITHMS, OPT_CIPHER_COMMANDS, OPT_CIPHER_ALGORITHMS,
890     OPT_PK_ALGORITHMS, OPT_PK_METHOD, OPT_DISABLED,
891     OPT_KDF_ALGORITHMS, OPT_RANDOM_INSTANCES, OPT_RANDOM_GENERATORS,
892     OPT_ENCODERS, OPT_DECODERS,
893     OPT_MISSING_HELP, OPT_OBJECTS,
894 #ifndef OPENSSL_NO_DEPRECATED_3_0
895     OPT_ENGINES, 
896 #endif
897     OPT_PROV_ENUM
898 } HELPLIST_CHOICE;
899
900 const OPTIONS list_options[] = {
901
902     OPT_SECTION("General"),
903     {"help", OPT_HELP, '-', "Display this summary"},
904
905     OPT_SECTION("Output"),
906     {"1", OPT_ONE, '-', "List in one column"},
907     {"verbose", OPT_VERBOSE, '-', "Verbose listing"},
908     {"commands", OPT_COMMANDS, '-', "List of standard commands"},
909     {"standard-commands", OPT_COMMANDS, '-', "List of standard commands"},
910     {"digest-commands", OPT_DIGEST_COMMANDS, '-',
911      "List of message digest commands"},
912     {"digest-algorithms", OPT_DIGEST_ALGORITHMS, '-',
913      "List of message digest algorithms"},
914     {"kdf-algorithms", OPT_KDF_ALGORITHMS, '-',
915      "List of key derivation and pseudo random function algorithms"},
916     {"random-instances", OPT_RANDOM_INSTANCES, '-',
917      "List the primary, pubic and private random number generator details"},
918     {"random-generators", OPT_RANDOM_GENERATORS, '-',
919      "List of random number generators"},
920     {"mac-algorithms", OPT_MAC_ALGORITHMS, '-',
921      "List of message authentication code algorithms"},
922     {"cipher-commands", OPT_CIPHER_COMMANDS, '-', "List of cipher commands"},
923     {"cipher-algorithms", OPT_CIPHER_ALGORITHMS, '-',
924      "List of cipher algorithms"},
925     {"encoders", OPT_ENCODERS, '-', "List of encoding methods" },
926     {"decoders", OPT_DECODERS, '-', "List of decoding methods" },
927     {"public-key-algorithms", OPT_PK_ALGORITHMS, '-',
928      "List of public key algorithms"},
929 #ifndef OPENSSL_NO_DEPRECATED_3_0
930     {"public-key-methods", OPT_PK_METHOD, '-',
931      "List of public key methods"},
932     {"engines", OPT_ENGINES, '-',
933      "List of loaded engines"},
934 #endif
935     {"disabled", OPT_DISABLED, '-', "List of disabled features"},
936     {"missing-help", OPT_MISSING_HELP, '-',
937      "List missing detailed help strings"},
938     {"options", OPT_OPTIONS, 's',
939      "List options for specified command"},
940     {"objects", OPT_OBJECTS, '-',
941      "List built in objects (OID<->name mappings)"},
942
943     OPT_PROV_OPTIONS,
944     {NULL}
945 };
946
947 int list_main(int argc, char **argv)
948 {
949     char *prog;
950     HELPLIST_CHOICE o;
951     int one = 0, done = 0;
952     struct {
953         unsigned int commands:1;
954         unsigned int random_instances:1;
955         unsigned int random_generators:1;
956         unsigned int digest_commands:1;
957         unsigned int digest_algorithms:1;
958         unsigned int kdf_algorithms:1;
959         unsigned int mac_algorithms:1;
960         unsigned int cipher_commands:1;
961         unsigned int cipher_algorithms:1;
962         unsigned int encoder_algorithms:1;
963         unsigned int decoder_algorithms:1;
964         unsigned int pk_algorithms:1;
965         unsigned int pk_method:1;
966 #ifndef OPENSSL_NO_DEPRECATED_3_0
967         unsigned int engines:1;
968 #endif
969         unsigned int disabled:1;
970         unsigned int missing_help:1;
971         unsigned int objects:1;
972         unsigned int options:1;
973     } todo = { 0, };
974
975     verbose = 0;                 /* Clear a possible previous call */
976
977     prog = opt_init(argc, argv, list_options);
978     while ((o = opt_next()) != OPT_EOF) {
979         switch (o) {
980         case OPT_EOF:  /* Never hit, but suppresses warning */
981         case OPT_ERR:
982 opthelp:
983             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
984             return 1;
985         case OPT_HELP:
986             opt_help(list_options);
987             break;
988         case OPT_ONE:
989             one = 1;
990             break;
991         case OPT_COMMANDS:
992             todo.commands = 1;
993             break;
994         case OPT_DIGEST_COMMANDS:
995             todo.digest_commands = 1;
996             break;
997         case OPT_DIGEST_ALGORITHMS:
998             todo.digest_algorithms = 1;
999             break;
1000         case OPT_KDF_ALGORITHMS:
1001             todo.kdf_algorithms = 1;
1002             break;
1003         case OPT_RANDOM_INSTANCES:
1004             todo.random_instances = 1;
1005             break;
1006         case OPT_RANDOM_GENERATORS:
1007             todo.random_generators = 1;
1008             break;
1009         case OPT_MAC_ALGORITHMS:
1010             todo.mac_algorithms = 1;
1011             break;
1012         case OPT_CIPHER_COMMANDS:
1013             todo.cipher_commands = 1;
1014             break;
1015         case OPT_CIPHER_ALGORITHMS:
1016             todo.cipher_algorithms = 1;
1017             break;
1018         case OPT_ENCODERS:
1019             todo.encoder_algorithms = 1;
1020             break;
1021         case OPT_DECODERS:
1022             todo.decoder_algorithms = 1;
1023             break;
1024         case OPT_PK_ALGORITHMS:
1025             todo.pk_algorithms = 1;
1026             break;
1027         case OPT_PK_METHOD:
1028             todo.pk_method = 1;
1029             break;
1030 #ifndef OPENSSL_NO_DEPRECATED_3_0
1031         case OPT_ENGINES:
1032             todo.engines = 1;
1033             break;
1034 #endif
1035         case OPT_DISABLED:
1036             todo.disabled = 1;
1037             break;
1038         case OPT_MISSING_HELP:
1039             todo.missing_help = 1;
1040             break;
1041         case OPT_OBJECTS:
1042             todo.objects = 1;
1043             break;
1044         case OPT_OPTIONS:
1045             list_options_for_command(opt_arg());
1046             break;
1047         case OPT_VERBOSE:
1048             verbose = 1;
1049             break;
1050         case OPT_PROV_CASES:
1051             if (!opt_provider(o))
1052                 return 1;
1053             break;
1054         }
1055         done = 1;
1056     }
1057     if (opt_num_rest() != 0) {
1058         BIO_printf(bio_err, "Extra arguments given.\n");
1059         goto opthelp;
1060     }
1061
1062     if (todo.commands)
1063         list_type(FT_general, one);
1064     if (todo.random_instances)
1065         list_random_instances();
1066     if (todo.random_generators)
1067         list_random_generators();
1068     if (todo.digest_commands)
1069         list_type(FT_md, one);
1070     if (todo.digest_algorithms)
1071         list_digests();
1072     if (todo.kdf_algorithms)
1073         list_kdfs();
1074     if (todo.mac_algorithms)
1075         list_macs();
1076     if (todo.cipher_commands)
1077         list_type(FT_cipher, one);
1078     if (todo.cipher_algorithms)
1079         list_ciphers();
1080     if (todo.encoder_algorithms)
1081         list_encoders();
1082     if (todo.decoder_algorithms)
1083         list_decoders();
1084     if (todo.pk_algorithms)
1085         list_pkey();
1086 #ifndef OPENSSL_NO_DEPRECATED_3_0
1087     if (todo.pk_method)
1088         list_pkey_meth();
1089     if (todo.engines)
1090         list_engines();
1091 #endif
1092     if (todo.disabled)
1093         list_disabled();
1094     if (todo.missing_help)
1095         list_missing_help();
1096     if (todo.objects)
1097         list_objects();
1098
1099     if (!done)
1100         goto opthelp;
1101
1102     return 0;
1103 }