xref: /freebsd/crypto/openssl/apps/list.c (revision e9ac41698b2f322d55ccf9da50a3596edb2c1800)
1 /*
2  * Copyright 1995-2024 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/store.h>
22 #include <openssl/core_names.h>
23 #include <openssl/rand.h>
24 #include "apps.h"
25 #include "app_params.h"
26 #include "progs.h"
27 #include "opt.h"
28 #include "names.h"
29 
30 static int verbose = 0;
31 static const char *select_name = NULL;
32 
33 /* Checks to see if algorithms are fetchable */
34 #define IS_FETCHABLE(type, TYPE)                                \
35     static int is_ ## type ## _fetchable(const TYPE *alg)       \
36     {                                                           \
37         TYPE *impl;                                             \
38         const char *propq = app_get0_propq();                   \
39         OSSL_LIB_CTX *libctx = app_get0_libctx();               \
40         const char *name = TYPE ## _get0_name(alg);             \
41                                                                 \
42         ERR_set_mark();                                         \
43         impl = TYPE ## _fetch(libctx, name, propq);             \
44         ERR_pop_to_mark();                                      \
45         if (impl == NULL)                                       \
46             return 0;                                           \
47         TYPE ## _free(impl);                                    \
48         return 1;                                               \
49     }
50 IS_FETCHABLE(cipher, EVP_CIPHER)
51 IS_FETCHABLE(digest, EVP_MD)
52 IS_FETCHABLE(mac, EVP_MAC)
53 IS_FETCHABLE(kdf, EVP_KDF)
54 IS_FETCHABLE(rand, EVP_RAND)
55 IS_FETCHABLE(keymgmt, EVP_KEYMGMT)
56 IS_FETCHABLE(signature, EVP_SIGNATURE)
57 IS_FETCHABLE(kem, EVP_KEM)
58 IS_FETCHABLE(asym_cipher, EVP_ASYM_CIPHER)
59 IS_FETCHABLE(keyexch, EVP_KEYEXCH)
60 IS_FETCHABLE(decoder, OSSL_DECODER)
61 IS_FETCHABLE(encoder, OSSL_ENCODER)
62 
63 #ifndef OPENSSL_NO_DEPRECATED_3_0
64 static int include_legacy(void)
65 {
66     return app_get0_propq() == NULL;
67 }
68 
69 static void legacy_cipher_fn(const EVP_CIPHER *c,
70                              const char *from, const char *to, void *arg)
71 {
72     if (select_name != NULL
73         && (c == NULL
74             || OPENSSL_strcasecmp(select_name,  EVP_CIPHER_get0_name(c)) != 0))
75         return;
76     if (c != NULL) {
77         BIO_printf(arg, "  %s\n", EVP_CIPHER_get0_name(c));
78     } else {
79         if (from == NULL)
80             from = "<undefined>";
81         if (to == NULL)
82             to = "<undefined>";
83         BIO_printf(arg, "  %s => %s\n", from, to);
84     }
85 }
86 #endif
87 
88 DEFINE_STACK_OF(EVP_CIPHER)
89 static int cipher_cmp(const EVP_CIPHER * const *a,
90                       const EVP_CIPHER * const *b)
91 {
92     return strcmp(OSSL_PROVIDER_get0_name(EVP_CIPHER_get0_provider(*a)),
93                   OSSL_PROVIDER_get0_name(EVP_CIPHER_get0_provider(*b)));
94 }
95 
96 static void collect_ciphers(EVP_CIPHER *cipher, void *stack)
97 {
98     STACK_OF(EVP_CIPHER) *cipher_stack = stack;
99 
100     if (is_cipher_fetchable(cipher)
101             && sk_EVP_CIPHER_push(cipher_stack, cipher) > 0)
102         EVP_CIPHER_up_ref(cipher);
103 }
104 
105 static void list_ciphers(void)
106 {
107     STACK_OF(EVP_CIPHER) *ciphers = sk_EVP_CIPHER_new(cipher_cmp);
108     int i;
109 
110     if (ciphers == NULL) {
111         BIO_printf(bio_err, "ERROR: Memory allocation\n");
112         return;
113     }
114 #ifndef OPENSSL_NO_DEPRECATED_3_0
115     if (include_legacy()) {
116         BIO_printf(bio_out, "Legacy:\n");
117         EVP_CIPHER_do_all_sorted(legacy_cipher_fn, bio_out);
118     }
119 #endif
120 
121     BIO_printf(bio_out, "Provided:\n");
122     EVP_CIPHER_do_all_provided(app_get0_libctx(), collect_ciphers, ciphers);
123     sk_EVP_CIPHER_sort(ciphers);
124     for (i = 0; i < sk_EVP_CIPHER_num(ciphers); i++) {
125         const EVP_CIPHER *c = sk_EVP_CIPHER_value(ciphers, i);
126         STACK_OF(OPENSSL_CSTRING) *names = NULL;
127 
128         if (select_name != NULL && !EVP_CIPHER_is_a(c, select_name))
129             continue;
130 
131         names = sk_OPENSSL_CSTRING_new(name_cmp);
132         if (names != NULL && EVP_CIPHER_names_do_all(c, collect_names, names)) {
133             BIO_printf(bio_out, "  ");
134             print_names(bio_out, names);
135 
136             BIO_printf(bio_out, " @ %s\n",
137                        OSSL_PROVIDER_get0_name(EVP_CIPHER_get0_provider(c)));
138 
139             if (verbose) {
140                 const char *desc = EVP_CIPHER_get0_description(c);
141 
142                 if (desc != NULL)
143                     BIO_printf(bio_out, "    description: %s\n", desc);
144                 print_param_types("retrievable algorithm parameters",
145                                   EVP_CIPHER_gettable_params(c), 4);
146                 print_param_types("retrievable operation parameters",
147                                   EVP_CIPHER_gettable_ctx_params(c), 4);
148                 print_param_types("settable operation parameters",
149                                   EVP_CIPHER_settable_ctx_params(c), 4);
150             }
151         }
152         sk_OPENSSL_CSTRING_free(names);
153     }
154     sk_EVP_CIPHER_pop_free(ciphers, EVP_CIPHER_free);
155 }
156 
157 #ifndef OPENSSL_NO_DEPRECATED_3_0
158 static void legacy_md_fn(const EVP_MD *m,
159                        const char *from, const char *to, void *arg)
160 {
161     if (m != NULL) {
162         BIO_printf(arg, "  %s\n", EVP_MD_get0_name(m));
163     } else {
164         if (from == NULL)
165             from = "<undefined>";
166         if (to == NULL)
167             to = "<undefined>";
168         BIO_printf((BIO *)arg, "  %s => %s\n", from, to);
169     }
170 }
171 #endif
172 
173 DEFINE_STACK_OF(EVP_MD)
174 static int md_cmp(const EVP_MD * const *a, const EVP_MD * const *b)
175 {
176     return strcmp(OSSL_PROVIDER_get0_name(EVP_MD_get0_provider(*a)),
177                   OSSL_PROVIDER_get0_name(EVP_MD_get0_provider(*b)));
178 }
179 
180 static void collect_digests(EVP_MD *digest, void *stack)
181 {
182     STACK_OF(EVP_MD) *digest_stack = stack;
183 
184     if (is_digest_fetchable(digest)
185             && sk_EVP_MD_push(digest_stack, digest) > 0)
186         EVP_MD_up_ref(digest);
187 }
188 
189 static void list_digests(void)
190 {
191     STACK_OF(EVP_MD) *digests = sk_EVP_MD_new(md_cmp);
192     int i;
193 
194     if (digests == NULL) {
195         BIO_printf(bio_err, "ERROR: Memory allocation\n");
196         return;
197     }
198 #ifndef OPENSSL_NO_DEPRECATED_3_0
199     if (include_legacy()) {
200         BIO_printf(bio_out, "Legacy:\n");
201         EVP_MD_do_all_sorted(legacy_md_fn, bio_out);
202     }
203 #endif
204 
205     BIO_printf(bio_out, "Provided:\n");
206     EVP_MD_do_all_provided(app_get0_libctx(), collect_digests, digests);
207     sk_EVP_MD_sort(digests);
208     for (i = 0; i < sk_EVP_MD_num(digests); i++) {
209         const EVP_MD *m = sk_EVP_MD_value(digests, i);
210         STACK_OF(OPENSSL_CSTRING) *names = NULL;
211 
212         if (select_name != NULL && !EVP_MD_is_a(m, select_name))
213             continue;
214 
215         names = sk_OPENSSL_CSTRING_new(name_cmp);
216         if (names != NULL && EVP_MD_names_do_all(m, collect_names, names)) {
217             BIO_printf(bio_out, "  ");
218             print_names(bio_out, names);
219 
220             BIO_printf(bio_out, " @ %s\n",
221                        OSSL_PROVIDER_get0_name(EVP_MD_get0_provider(m)));
222 
223             if (verbose) {
224                 const char *desc = EVP_MD_get0_description(m);
225 
226                 if (desc != NULL)
227                     BIO_printf(bio_out, "    description: %s\n", desc);
228                 print_param_types("retrievable algorithm parameters",
229                                 EVP_MD_gettable_params(m), 4);
230                 print_param_types("retrievable operation parameters",
231                                 EVP_MD_gettable_ctx_params(m), 4);
232                 print_param_types("settable operation parameters",
233                                 EVP_MD_settable_ctx_params(m), 4);
234             }
235         }
236         sk_OPENSSL_CSTRING_free(names);
237     }
238     sk_EVP_MD_pop_free(digests, EVP_MD_free);
239 }
240 
241 DEFINE_STACK_OF(EVP_MAC)
242 static int mac_cmp(const EVP_MAC * const *a, const EVP_MAC * const *b)
243 {
244     return strcmp(OSSL_PROVIDER_get0_name(EVP_MAC_get0_provider(*a)),
245                   OSSL_PROVIDER_get0_name(EVP_MAC_get0_provider(*b)));
246 }
247 
248 static void collect_macs(EVP_MAC *mac, void *stack)
249 {
250     STACK_OF(EVP_MAC) *mac_stack = stack;
251 
252     if (is_mac_fetchable(mac)
253             && sk_EVP_MAC_push(mac_stack, mac) > 0)
254         EVP_MAC_up_ref(mac);
255 }
256 
257 static void list_macs(void)
258 {
259     STACK_OF(EVP_MAC) *macs = sk_EVP_MAC_new(mac_cmp);
260     int i;
261 
262     if (macs == NULL) {
263         BIO_printf(bio_err, "ERROR: Memory allocation\n");
264         return;
265     }
266     BIO_printf(bio_out, "Provided MACs:\n");
267     EVP_MAC_do_all_provided(app_get0_libctx(), collect_macs, macs);
268     sk_EVP_MAC_sort(macs);
269     for (i = 0; i < sk_EVP_MAC_num(macs); i++) {
270         const EVP_MAC *m = sk_EVP_MAC_value(macs, i);
271         STACK_OF(OPENSSL_CSTRING) *names = NULL;
272 
273         if (select_name != NULL && !EVP_MAC_is_a(m, select_name))
274             continue;
275 
276         names = sk_OPENSSL_CSTRING_new(name_cmp);
277         if (names != NULL && EVP_MAC_names_do_all(m, collect_names, names)) {
278             BIO_printf(bio_out, "  ");
279             print_names(bio_out, names);
280 
281             BIO_printf(bio_out, " @ %s\n",
282                        OSSL_PROVIDER_get0_name(EVP_MAC_get0_provider(m)));
283 
284             if (verbose) {
285                 const char *desc = EVP_MAC_get0_description(m);
286 
287                 if (desc != NULL)
288                     BIO_printf(bio_out, "    description: %s\n", desc);
289                 print_param_types("retrievable algorithm parameters",
290                                 EVP_MAC_gettable_params(m), 4);
291                 print_param_types("retrievable operation parameters",
292                                 EVP_MAC_gettable_ctx_params(m), 4);
293                 print_param_types("settable operation parameters",
294                                 EVP_MAC_settable_ctx_params(m), 4);
295             }
296         }
297         sk_OPENSSL_CSTRING_free(names);
298     }
299     sk_EVP_MAC_pop_free(macs, EVP_MAC_free);
300 }
301 
302 /*
303  * KDFs and PRFs
304  */
305 DEFINE_STACK_OF(EVP_KDF)
306 static int kdf_cmp(const EVP_KDF * const *a, const EVP_KDF * const *b)
307 {
308     return strcmp(OSSL_PROVIDER_get0_name(EVP_KDF_get0_provider(*a)),
309                   OSSL_PROVIDER_get0_name(EVP_KDF_get0_provider(*b)));
310 }
311 
312 static void collect_kdfs(EVP_KDF *kdf, void *stack)
313 {
314     STACK_OF(EVP_KDF) *kdf_stack = stack;
315 
316     if (is_kdf_fetchable(kdf)
317             && sk_EVP_KDF_push(kdf_stack, kdf) > 0)
318         EVP_KDF_up_ref(kdf);
319 }
320 
321 static void list_kdfs(void)
322 {
323     STACK_OF(EVP_KDF) *kdfs = sk_EVP_KDF_new(kdf_cmp);
324     int i;
325 
326     if (kdfs == NULL) {
327         BIO_printf(bio_err, "ERROR: Memory allocation\n");
328         return;
329     }
330     BIO_printf(bio_out, "Provided KDFs and PDFs:\n");
331     EVP_KDF_do_all_provided(app_get0_libctx(), collect_kdfs, kdfs);
332     sk_EVP_KDF_sort(kdfs);
333     for (i = 0; i < sk_EVP_KDF_num(kdfs); i++) {
334         const EVP_KDF *k = sk_EVP_KDF_value(kdfs, i);
335         STACK_OF(OPENSSL_CSTRING) *names = NULL;
336 
337         if (select_name != NULL && !EVP_KDF_is_a(k, select_name))
338             continue;
339 
340         names = sk_OPENSSL_CSTRING_new(name_cmp);
341         if (names != NULL && EVP_KDF_names_do_all(k, collect_names, names)) {
342             BIO_printf(bio_out, "  ");
343             print_names(bio_out, names);
344 
345             BIO_printf(bio_out, " @ %s\n",
346                        OSSL_PROVIDER_get0_name(EVP_KDF_get0_provider(k)));
347 
348             if (verbose) {
349                 const char *desc = EVP_KDF_get0_description(k);
350 
351                 if (desc != NULL)
352                     BIO_printf(bio_out, "    description: %s\n", desc);
353                 print_param_types("retrievable algorithm parameters",
354                                 EVP_KDF_gettable_params(k), 4);
355                 print_param_types("retrievable operation parameters",
356                                 EVP_KDF_gettable_ctx_params(k), 4);
357                 print_param_types("settable operation parameters",
358                                 EVP_KDF_settable_ctx_params(k), 4);
359             }
360         }
361         sk_OPENSSL_CSTRING_free(names);
362     }
363     sk_EVP_KDF_pop_free(kdfs, EVP_KDF_free);
364 }
365 
366 /*
367  * RANDs
368  */
369 DEFINE_STACK_OF(EVP_RAND)
370 
371 static int rand_cmp(const EVP_RAND * const *a, const EVP_RAND * const *b)
372 {
373     int ret = OPENSSL_strcasecmp(EVP_RAND_get0_name(*a), EVP_RAND_get0_name(*b));
374 
375     if (ret == 0)
376         ret = strcmp(OSSL_PROVIDER_get0_name(EVP_RAND_get0_provider(*a)),
377                      OSSL_PROVIDER_get0_name(EVP_RAND_get0_provider(*b)));
378 
379     return ret;
380 }
381 
382 static void collect_rands(EVP_RAND *rand, void *stack)
383 {
384     STACK_OF(EVP_RAND) *rand_stack = stack;
385 
386     if (is_rand_fetchable(rand)
387             && sk_EVP_RAND_push(rand_stack, rand) > 0)
388         EVP_RAND_up_ref(rand);
389 }
390 
391 static void list_random_generators(void)
392 {
393     STACK_OF(EVP_RAND) *rands = sk_EVP_RAND_new(rand_cmp);
394     int i;
395 
396     if (rands == NULL) {
397         BIO_printf(bio_err, "ERROR: Memory allocation\n");
398         return;
399     }
400     BIO_printf(bio_out, "Provided RNGs and seed sources:\n");
401     EVP_RAND_do_all_provided(app_get0_libctx(), collect_rands, rands);
402     sk_EVP_RAND_sort(rands);
403     for (i = 0; i < sk_EVP_RAND_num(rands); i++) {
404         const EVP_RAND *m = sk_EVP_RAND_value(rands, i);
405 
406         if (select_name != NULL
407             && OPENSSL_strcasecmp(EVP_RAND_get0_name(m), select_name) != 0)
408             continue;
409         BIO_printf(bio_out, "  %s", EVP_RAND_get0_name(m));
410         BIO_printf(bio_out, " @ %s\n",
411                    OSSL_PROVIDER_get0_name(EVP_RAND_get0_provider(m)));
412 
413         if (verbose) {
414             const char *desc = EVP_RAND_get0_description(m);
415 
416             if (desc != NULL)
417                 BIO_printf(bio_out, "    description: %s\n", desc);
418             print_param_types("retrievable algorithm parameters",
419                               EVP_RAND_gettable_params(m), 4);
420             print_param_types("retrievable operation parameters",
421                               EVP_RAND_gettable_ctx_params(m), 4);
422             print_param_types("settable operation parameters",
423                               EVP_RAND_settable_ctx_params(m), 4);
424         }
425     }
426     sk_EVP_RAND_pop_free(rands, EVP_RAND_free);
427 }
428 
429 static void display_random(const char *name, EVP_RAND_CTX *drbg)
430 {
431     EVP_RAND *rand;
432     uint64_t u;
433     const char *p;
434     const OSSL_PARAM *gettables;
435     OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
436     unsigned char buf[1000];
437 
438     BIO_printf(bio_out, "%s:\n", name);
439     if (drbg != NULL) {
440         rand = EVP_RAND_CTX_get0_rand(drbg);
441 
442         BIO_printf(bio_out, "  %s", EVP_RAND_get0_name(rand));
443         BIO_printf(bio_out, " @ %s\n",
444                    OSSL_PROVIDER_get0_name(EVP_RAND_get0_provider(rand)));
445 
446         switch (EVP_RAND_get_state(drbg)) {
447         case EVP_RAND_STATE_UNINITIALISED:
448             p = "uninitialised";
449             break;
450         case EVP_RAND_STATE_READY:
451             p = "ready";
452             break;
453         case EVP_RAND_STATE_ERROR:
454             p = "error";
455             break;
456         default:
457             p = "unknown";
458             break;
459         }
460         BIO_printf(bio_out, "  state = %s\n", p);
461 
462         gettables = EVP_RAND_gettable_ctx_params(rand);
463         if (gettables != NULL)
464             for (; gettables->key != NULL; gettables++) {
465                 /* State has been dealt with already, so ignore */
466                 if (OPENSSL_strcasecmp(gettables->key, OSSL_RAND_PARAM_STATE) == 0)
467                     continue;
468                 /* Outside of verbose mode, we skip non-string values */
469                 if (gettables->data_type != OSSL_PARAM_UTF8_STRING
470                         && gettables->data_type != OSSL_PARAM_UTF8_PTR
471                         && !verbose)
472                     continue;
473                 params->key = gettables->key;
474                 params->data_type = gettables->data_type;
475                 if (gettables->data_type == OSSL_PARAM_UNSIGNED_INTEGER
476                         || gettables->data_type == OSSL_PARAM_INTEGER) {
477                     params->data = &u;
478                     params->data_size = sizeof(u);
479                 } else {
480                     params->data = buf;
481                     params->data_size = sizeof(buf);
482                 }
483                 params->return_size = 0;
484                 if (EVP_RAND_CTX_get_params(drbg, params))
485                     print_param_value(params, 2);
486             }
487     }
488 }
489 
490 static void list_random_instances(void)
491 {
492     display_random("primary", RAND_get0_primary(NULL));
493     display_random("public", RAND_get0_public(NULL));
494     display_random("private", RAND_get0_private(NULL));
495 }
496 
497 /*
498  * Encoders
499  */
500 DEFINE_STACK_OF(OSSL_ENCODER)
501 static int encoder_cmp(const OSSL_ENCODER * const *a,
502                        const OSSL_ENCODER * const *b)
503 {
504     return strcmp(OSSL_PROVIDER_get0_name(OSSL_ENCODER_get0_provider(*a)),
505                   OSSL_PROVIDER_get0_name(OSSL_ENCODER_get0_provider(*b)));
506 }
507 
508 static void collect_encoders(OSSL_ENCODER *encoder, void *stack)
509 {
510     STACK_OF(OSSL_ENCODER) *encoder_stack = stack;
511 
512     if (is_encoder_fetchable(encoder)
513             && sk_OSSL_ENCODER_push(encoder_stack, encoder) > 0)
514         OSSL_ENCODER_up_ref(encoder);
515 }
516 
517 static void list_encoders(void)
518 {
519     STACK_OF(OSSL_ENCODER) *encoders;
520     int i;
521 
522     encoders = sk_OSSL_ENCODER_new(encoder_cmp);
523     if (encoders == NULL) {
524         BIO_printf(bio_err, "ERROR: Memory allocation\n");
525         return;
526     }
527     BIO_printf(bio_out, "Provided ENCODERs:\n");
528     OSSL_ENCODER_do_all_provided(app_get0_libctx(), collect_encoders,
529                                  encoders);
530     sk_OSSL_ENCODER_sort(encoders);
531 
532     for (i = 0; i < sk_OSSL_ENCODER_num(encoders); i++) {
533         OSSL_ENCODER *k = sk_OSSL_ENCODER_value(encoders, i);
534         STACK_OF(OPENSSL_CSTRING) *names = NULL;
535 
536         if (select_name != NULL && !OSSL_ENCODER_is_a(k, select_name))
537             continue;
538 
539         names = sk_OPENSSL_CSTRING_new(name_cmp);
540         if (names != NULL && OSSL_ENCODER_names_do_all(k, collect_names, names)) {
541             BIO_printf(bio_out, "  ");
542             print_names(bio_out, names);
543 
544             BIO_printf(bio_out, " @ %s (%s)\n",
545                     OSSL_PROVIDER_get0_name(OSSL_ENCODER_get0_provider(k)),
546                     OSSL_ENCODER_get0_properties(k));
547 
548             if (verbose) {
549                 const char *desc = OSSL_ENCODER_get0_description(k);
550 
551                 if (desc != NULL)
552                     BIO_printf(bio_out, "    description: %s\n", desc);
553                 print_param_types("settable operation parameters",
554                                 OSSL_ENCODER_settable_ctx_params(k), 4);
555             }
556         }
557         sk_OPENSSL_CSTRING_free(names);
558     }
559     sk_OSSL_ENCODER_pop_free(encoders, OSSL_ENCODER_free);
560 }
561 
562 /*
563  * Decoders
564  */
565 DEFINE_STACK_OF(OSSL_DECODER)
566 static int decoder_cmp(const OSSL_DECODER * const *a,
567                        const OSSL_DECODER * const *b)
568 {
569     return strcmp(OSSL_PROVIDER_get0_name(OSSL_DECODER_get0_provider(*a)),
570                   OSSL_PROVIDER_get0_name(OSSL_DECODER_get0_provider(*b)));
571 }
572 
573 static void collect_decoders(OSSL_DECODER *decoder, void *stack)
574 {
575     STACK_OF(OSSL_DECODER) *decoder_stack = stack;
576 
577     if (is_decoder_fetchable(decoder)
578             && sk_OSSL_DECODER_push(decoder_stack, decoder) > 0)
579         OSSL_DECODER_up_ref(decoder);
580 }
581 
582 static void list_decoders(void)
583 {
584     STACK_OF(OSSL_DECODER) *decoders;
585     int i;
586 
587     decoders = sk_OSSL_DECODER_new(decoder_cmp);
588     if (decoders == NULL) {
589         BIO_printf(bio_err, "ERROR: Memory allocation\n");
590         return;
591     }
592     BIO_printf(bio_out, "Provided DECODERs:\n");
593     OSSL_DECODER_do_all_provided(app_get0_libctx(), collect_decoders,
594                                  decoders);
595     sk_OSSL_DECODER_sort(decoders);
596 
597     for (i = 0; i < sk_OSSL_DECODER_num(decoders); i++) {
598         OSSL_DECODER *k = sk_OSSL_DECODER_value(decoders, i);
599         STACK_OF(OPENSSL_CSTRING) *names = NULL;
600 
601         if (select_name != NULL && !OSSL_DECODER_is_a(k, select_name))
602             continue;
603 
604         names = sk_OPENSSL_CSTRING_new(name_cmp);
605         if (names != NULL && OSSL_DECODER_names_do_all(k, collect_names, names)) {
606             BIO_printf(bio_out, "  ");
607             print_names(bio_out, names);
608 
609             BIO_printf(bio_out, " @ %s (%s)\n",
610                        OSSL_PROVIDER_get0_name(OSSL_DECODER_get0_provider(k)),
611                        OSSL_DECODER_get0_properties(k));
612 
613             if (verbose) {
614                 const char *desc = OSSL_DECODER_get0_description(k);
615 
616                 if (desc != NULL)
617                     BIO_printf(bio_out, "    description: %s\n", desc);
618                 print_param_types("settable operation parameters",
619                                 OSSL_DECODER_settable_ctx_params(k), 4);
620             }
621         }
622         sk_OPENSSL_CSTRING_free(names);
623     }
624     sk_OSSL_DECODER_pop_free(decoders, OSSL_DECODER_free);
625 }
626 
627 DEFINE_STACK_OF(EVP_KEYMGMT)
628 static int keymanager_cmp(const EVP_KEYMGMT * const *a,
629                           const EVP_KEYMGMT * const *b)
630 {
631     return strcmp(OSSL_PROVIDER_get0_name(EVP_KEYMGMT_get0_provider(*a)),
632                   OSSL_PROVIDER_get0_name(EVP_KEYMGMT_get0_provider(*b)));
633 }
634 
635 static void collect_keymanagers(EVP_KEYMGMT *km, void *stack)
636 {
637     STACK_OF(EVP_KEYMGMT) *km_stack = stack;
638 
639     if (is_keymgmt_fetchable(km)
640             && sk_EVP_KEYMGMT_push(km_stack, km) > 0)
641         EVP_KEYMGMT_up_ref(km);
642 }
643 
644 static void list_keymanagers(void)
645 {
646     int i;
647     STACK_OF(EVP_KEYMGMT) *km_stack = sk_EVP_KEYMGMT_new(keymanager_cmp);
648 
649     EVP_KEYMGMT_do_all_provided(app_get0_libctx(), collect_keymanagers,
650                                 km_stack);
651     sk_EVP_KEYMGMT_sort(km_stack);
652 
653     for (i = 0; i < sk_EVP_KEYMGMT_num(km_stack); i++) {
654         EVP_KEYMGMT *k = sk_EVP_KEYMGMT_value(km_stack, i);
655         STACK_OF(OPENSSL_CSTRING) *names = NULL;
656 
657         if (select_name != NULL && !EVP_KEYMGMT_is_a(k, select_name))
658             continue;
659 
660         names = sk_OPENSSL_CSTRING_new(name_cmp);
661         if (names != NULL && EVP_KEYMGMT_names_do_all(k, collect_names, names)) {
662             const char *desc = EVP_KEYMGMT_get0_description(k);
663 
664             BIO_printf(bio_out, "  Name: ");
665             if (desc != NULL)
666                 BIO_printf(bio_out, "%s", desc);
667             else
668                 BIO_printf(bio_out, "%s", sk_OPENSSL_CSTRING_value(names, 0));
669             BIO_printf(bio_out, "\n");
670             BIO_printf(bio_out, "    Type: Provider Algorithm\n");
671             BIO_printf(bio_out, "    IDs: ");
672             print_names(bio_out, names);
673             BIO_printf(bio_out, " @ %s\n",
674                     OSSL_PROVIDER_get0_name(EVP_KEYMGMT_get0_provider(k)));
675 
676             if (verbose) {
677                 print_param_types("settable key generation parameters",
678                                 EVP_KEYMGMT_gen_settable_params(k), 4);
679                 print_param_types("settable operation parameters",
680                                 EVP_KEYMGMT_settable_params(k), 4);
681                 print_param_types("retrievable operation parameters",
682                                 EVP_KEYMGMT_gettable_params(k), 4);
683             }
684         }
685         sk_OPENSSL_CSTRING_free(names);
686     }
687     sk_EVP_KEYMGMT_pop_free(km_stack, EVP_KEYMGMT_free);
688 }
689 
690 DEFINE_STACK_OF(EVP_SIGNATURE)
691 static int signature_cmp(const EVP_SIGNATURE * const *a,
692                          const EVP_SIGNATURE * const *b)
693 {
694     return strcmp(OSSL_PROVIDER_get0_name(EVP_SIGNATURE_get0_provider(*a)),
695                   OSSL_PROVIDER_get0_name(EVP_SIGNATURE_get0_provider(*b)));
696 }
697 
698 static void collect_signatures(EVP_SIGNATURE *sig, void *stack)
699 {
700     STACK_OF(EVP_SIGNATURE) *sig_stack = stack;
701 
702     if (is_signature_fetchable(sig)
703             && sk_EVP_SIGNATURE_push(sig_stack, sig) > 0)
704         EVP_SIGNATURE_up_ref(sig);
705 }
706 
707 static void list_signatures(void)
708 {
709     int i, count = 0;
710     STACK_OF(EVP_SIGNATURE) *sig_stack = sk_EVP_SIGNATURE_new(signature_cmp);
711 
712     EVP_SIGNATURE_do_all_provided(app_get0_libctx(), collect_signatures,
713                                   sig_stack);
714     sk_EVP_SIGNATURE_sort(sig_stack);
715 
716     for (i = 0; i < sk_EVP_SIGNATURE_num(sig_stack); i++) {
717         EVP_SIGNATURE *k = sk_EVP_SIGNATURE_value(sig_stack, i);
718         STACK_OF(OPENSSL_CSTRING) *names = NULL;
719 
720         if (select_name != NULL && !EVP_SIGNATURE_is_a(k, select_name))
721             continue;
722 
723         names = sk_OPENSSL_CSTRING_new(name_cmp);
724         if (names != NULL && EVP_SIGNATURE_names_do_all(k, collect_names, names)) {
725             count++;
726             BIO_printf(bio_out, "  ");
727             print_names(bio_out, names);
728 
729             BIO_printf(bio_out, " @ %s\n",
730                     OSSL_PROVIDER_get0_name(EVP_SIGNATURE_get0_provider(k)));
731 
732             if (verbose) {
733                 const char *desc = EVP_SIGNATURE_get0_description(k);
734 
735                 if (desc != NULL)
736                     BIO_printf(bio_out, "    description: %s\n", desc);
737                 print_param_types("settable operation parameters",
738                                 EVP_SIGNATURE_settable_ctx_params(k), 4);
739                 print_param_types("retrievable operation parameters",
740                                 EVP_SIGNATURE_gettable_ctx_params(k), 4);
741             }
742         }
743         sk_OPENSSL_CSTRING_free(names);
744     }
745     sk_EVP_SIGNATURE_pop_free(sig_stack, EVP_SIGNATURE_free);
746     if (count == 0)
747         BIO_printf(bio_out, " -\n");
748 }
749 
750 DEFINE_STACK_OF(EVP_KEM)
751 static int kem_cmp(const EVP_KEM * const *a,
752                    const EVP_KEM * const *b)
753 {
754     return strcmp(OSSL_PROVIDER_get0_name(EVP_KEM_get0_provider(*a)),
755                   OSSL_PROVIDER_get0_name(EVP_KEM_get0_provider(*b)));
756 }
757 
758 static void collect_kem(EVP_KEM *kem, void *stack)
759 {
760     STACK_OF(EVP_KEM) *kem_stack = stack;
761 
762     if (is_kem_fetchable(kem)
763             && sk_EVP_KEM_push(kem_stack, kem) > 0)
764         EVP_KEM_up_ref(kem);
765 }
766 
767 static void list_kems(void)
768 {
769     int i, count = 0;
770     STACK_OF(EVP_KEM) *kem_stack = sk_EVP_KEM_new(kem_cmp);
771 
772     EVP_KEM_do_all_provided(app_get0_libctx(), collect_kem, kem_stack);
773     sk_EVP_KEM_sort(kem_stack);
774 
775     for (i = 0; i < sk_EVP_KEM_num(kem_stack); i++) {
776         EVP_KEM *k = sk_EVP_KEM_value(kem_stack, i);
777         STACK_OF(OPENSSL_CSTRING) *names = NULL;
778 
779         if (select_name != NULL && !EVP_KEM_is_a(k, select_name))
780             continue;
781 
782         names = sk_OPENSSL_CSTRING_new(name_cmp);
783         if (names != NULL && EVP_KEM_names_do_all(k, collect_names, names)) {
784             count++;
785             BIO_printf(bio_out, "  ");
786             print_names(bio_out, names);
787 
788             BIO_printf(bio_out, " @ %s\n",
789                        OSSL_PROVIDER_get0_name(EVP_KEM_get0_provider(k)));
790 
791             if (verbose) {
792                 const char *desc = EVP_KEM_get0_description(k);
793 
794                 if (desc != NULL)
795                     BIO_printf(bio_out, "    description: %s\n", desc);
796                 print_param_types("settable operation parameters",
797                                 EVP_KEM_settable_ctx_params(k), 4);
798                 print_param_types("retrievable operation parameters",
799                                 EVP_KEM_gettable_ctx_params(k), 4);
800             }
801         }
802         sk_OPENSSL_CSTRING_free(names);
803     }
804     sk_EVP_KEM_pop_free(kem_stack, EVP_KEM_free);
805     if (count == 0)
806         BIO_printf(bio_out, " -\n");
807 }
808 
809 DEFINE_STACK_OF(EVP_ASYM_CIPHER)
810 static int asymcipher_cmp(const EVP_ASYM_CIPHER * const *a,
811                           const EVP_ASYM_CIPHER * const *b)
812 {
813     return strcmp(OSSL_PROVIDER_get0_name(EVP_ASYM_CIPHER_get0_provider(*a)),
814                   OSSL_PROVIDER_get0_name(EVP_ASYM_CIPHER_get0_provider(*b)));
815 }
816 
817 static void collect_asymciph(EVP_ASYM_CIPHER *asym_cipher, void *stack)
818 {
819     STACK_OF(EVP_ASYM_CIPHER) *asym_cipher_stack = stack;
820 
821     if (is_asym_cipher_fetchable(asym_cipher)
822             && sk_EVP_ASYM_CIPHER_push(asym_cipher_stack, asym_cipher) > 0)
823         EVP_ASYM_CIPHER_up_ref(asym_cipher);
824 }
825 
826 static void list_asymciphers(void)
827 {
828     int i, count = 0;
829     STACK_OF(EVP_ASYM_CIPHER) *asymciph_stack =
830         sk_EVP_ASYM_CIPHER_new(asymcipher_cmp);
831 
832     EVP_ASYM_CIPHER_do_all_provided(app_get0_libctx(), collect_asymciph,
833                                     asymciph_stack);
834     sk_EVP_ASYM_CIPHER_sort(asymciph_stack);
835 
836     for (i = 0; i < sk_EVP_ASYM_CIPHER_num(asymciph_stack); i++) {
837         EVP_ASYM_CIPHER *k = sk_EVP_ASYM_CIPHER_value(asymciph_stack, i);
838         STACK_OF(OPENSSL_CSTRING) *names = NULL;
839 
840         if (select_name != NULL && !EVP_ASYM_CIPHER_is_a(k, select_name))
841             continue;
842 
843         names = sk_OPENSSL_CSTRING_new(name_cmp);
844         if (names != NULL
845                 && EVP_ASYM_CIPHER_names_do_all(k, collect_names, names)) {
846             count++;
847             BIO_printf(bio_out, "  ");
848             print_names(bio_out, names);
849 
850             BIO_printf(bio_out, " @ %s\n",
851                     OSSL_PROVIDER_get0_name(EVP_ASYM_CIPHER_get0_provider(k)));
852 
853             if (verbose) {
854                 const char *desc = EVP_ASYM_CIPHER_get0_description(k);
855 
856                 if (desc != NULL)
857                     BIO_printf(bio_out, "    description: %s\n", desc);
858                 print_param_types("settable operation parameters",
859                                 EVP_ASYM_CIPHER_settable_ctx_params(k), 4);
860                 print_param_types("retrievable operation parameters",
861                                 EVP_ASYM_CIPHER_gettable_ctx_params(k), 4);
862             }
863         }
864         sk_OPENSSL_CSTRING_free(names);
865     }
866     sk_EVP_ASYM_CIPHER_pop_free(asymciph_stack, EVP_ASYM_CIPHER_free);
867     if (count == 0)
868         BIO_printf(bio_out, " -\n");
869 }
870 
871 DEFINE_STACK_OF(EVP_KEYEXCH)
872 static int kex_cmp(const EVP_KEYEXCH * const *a,
873                    const EVP_KEYEXCH * const *b)
874 {
875     return strcmp(OSSL_PROVIDER_get0_name(EVP_KEYEXCH_get0_provider(*a)),
876                   OSSL_PROVIDER_get0_name(EVP_KEYEXCH_get0_provider(*b)));
877 }
878 
879 static void collect_kex(EVP_KEYEXCH *kex, void *stack)
880 {
881     STACK_OF(EVP_KEYEXCH) *kex_stack = stack;
882 
883     if (is_keyexch_fetchable(kex)
884             && sk_EVP_KEYEXCH_push(kex_stack, kex) > 0)
885         EVP_KEYEXCH_up_ref(kex);
886 }
887 
888 static void list_keyexchanges(void)
889 {
890     int i, count = 0;
891     STACK_OF(EVP_KEYEXCH) *kex_stack = sk_EVP_KEYEXCH_new(kex_cmp);
892 
893     EVP_KEYEXCH_do_all_provided(app_get0_libctx(), collect_kex, kex_stack);
894     sk_EVP_KEYEXCH_sort(kex_stack);
895 
896     for (i = 0; i < sk_EVP_KEYEXCH_num(kex_stack); i++) {
897         EVP_KEYEXCH *k = sk_EVP_KEYEXCH_value(kex_stack, i);
898         STACK_OF(OPENSSL_CSTRING) *names = NULL;
899 
900         if (select_name != NULL && !EVP_KEYEXCH_is_a(k, select_name))
901             continue;
902 
903         names = sk_OPENSSL_CSTRING_new(name_cmp);
904         if (names != NULL && EVP_KEYEXCH_names_do_all(k, collect_names, names)) {
905             count++;
906             BIO_printf(bio_out, "  ");
907             print_names(bio_out, names);
908 
909             BIO_printf(bio_out, " @ %s\n",
910                     OSSL_PROVIDER_get0_name(EVP_KEYEXCH_get0_provider(k)));
911 
912             if (verbose) {
913                 const char *desc = EVP_KEYEXCH_get0_description(k);
914 
915                 if (desc != NULL)
916                     BIO_printf(bio_out, "    description: %s\n", desc);
917                 print_param_types("settable operation parameters",
918                                 EVP_KEYEXCH_settable_ctx_params(k), 4);
919                 print_param_types("retrievable operation parameters",
920                                 EVP_KEYEXCH_gettable_ctx_params(k), 4);
921             }
922         }
923         sk_OPENSSL_CSTRING_free(names);
924     }
925     sk_EVP_KEYEXCH_pop_free(kex_stack, EVP_KEYEXCH_free);
926     if (count == 0)
927         BIO_printf(bio_out, " -\n");
928 }
929 
930 static void list_objects(void)
931 {
932     int max_nid = OBJ_new_nid(0);
933     int i;
934     char *oid_buf = NULL;
935     int oid_size = 0;
936 
937     /* Skip 0, since that's NID_undef */
938     for (i = 1; i < max_nid; i++) {
939         const ASN1_OBJECT *obj = OBJ_nid2obj(i);
940         const char *sn = OBJ_nid2sn(i);
941         const char *ln = OBJ_nid2ln(i);
942         int n = 0;
943 
944         /*
945          * If one of the retrieved objects somehow generated an error,
946          * we ignore it.  The check for NID_undef below will detect the
947          * error and simply skip to the next NID.
948          */
949         ERR_clear_error();
950 
951         if (OBJ_obj2nid(obj) == NID_undef)
952             continue;
953 
954         if ((n = OBJ_obj2txt(NULL, 0, obj, 1)) == 0) {
955             BIO_printf(bio_out, "# None-OID object: %s, %s\n", sn, ln);
956             continue;
957         }
958         if (n < 0)
959             break;               /* Error */
960 
961         if (n > oid_size) {
962             oid_buf = OPENSSL_realloc(oid_buf, n + 1);
963             if (oid_buf == NULL) {
964                 BIO_printf(bio_err, "ERROR: Memory allocation\n");
965                 break;           /* Error */
966             }
967             oid_size = n + 1;
968         }
969         if (OBJ_obj2txt(oid_buf, oid_size, obj, 1) < 0)
970             break;               /* Error */
971         if (ln == NULL || strcmp(sn, ln) == 0)
972             BIO_printf(bio_out, "%s = %s\n", sn, oid_buf);
973         else
974             BIO_printf(bio_out, "%s = %s, %s\n", sn, ln, oid_buf);
975     }
976 
977     OPENSSL_free(oid_buf);
978 }
979 
980 static void list_options_for_command(const char *command)
981 {
982     const FUNCTION *fp;
983     const OPTIONS *o;
984 
985     for (fp = functions; fp->name != NULL; fp++)
986         if (strcmp(fp->name, command) == 0)
987             break;
988     if (fp->name == NULL) {
989         BIO_printf(bio_err, "Invalid command '%s'; type \"help\" for a list.\n",
990                    command);
991         return;
992     }
993 
994     if ((o = fp->help) == NULL)
995         return;
996 
997     for ( ; o->name != NULL; o++) {
998         char c = o->valtype;
999 
1000         if (o->name == OPT_PARAM_STR)
1001             break;
1002 
1003         if (o->name == OPT_HELP_STR
1004                 || o->name == OPT_MORE_STR
1005                 || o->name == OPT_SECTION_STR
1006                 || o->name[0] == '\0')
1007             continue;
1008         BIO_printf(bio_out, "%s %c\n", o->name, c == '\0' ? '-' : c);
1009     }
1010     /* Always output the -- marker since it is sometimes documented. */
1011     BIO_printf(bio_out, "- -\n");
1012 }
1013 
1014 static int is_md_available(const char *name)
1015 {
1016     EVP_MD *md;
1017     const char *propq = app_get0_propq();
1018 
1019     /* Look through providers' digests */
1020     ERR_set_mark();
1021     md = EVP_MD_fetch(app_get0_libctx(), name, propq);
1022     ERR_pop_to_mark();
1023     if (md != NULL) {
1024         EVP_MD_free(md);
1025         return 1;
1026     }
1027 
1028     return propq != NULL || get_digest_from_engine(name) == NULL ? 0 : 1;
1029 }
1030 
1031 static int is_cipher_available(const char *name)
1032 {
1033     EVP_CIPHER *cipher;
1034     const char *propq = app_get0_propq();
1035 
1036     /* Look through providers' ciphers */
1037     ERR_set_mark();
1038     cipher = EVP_CIPHER_fetch(app_get0_libctx(), name, propq);
1039     ERR_pop_to_mark();
1040     if (cipher != NULL) {
1041         EVP_CIPHER_free(cipher);
1042         return 1;
1043     }
1044 
1045     return propq != NULL || get_cipher_from_engine(name) == NULL ? 0 : 1;
1046 }
1047 
1048 static void list_type(FUNC_TYPE ft, int one)
1049 {
1050     FUNCTION *fp;
1051     int i = 0;
1052     DISPLAY_COLUMNS dc;
1053 
1054     memset(&dc, 0, sizeof(dc));
1055     if (!one)
1056         calculate_columns(functions, &dc);
1057 
1058     for (fp = functions; fp->name != NULL; fp++) {
1059         if (fp->type != ft)
1060             continue;
1061         switch (ft) {
1062         case FT_cipher:
1063             if (!is_cipher_available(fp->name))
1064                 continue;
1065             break;
1066         case FT_md:
1067             if (!is_md_available(fp->name))
1068                 continue;
1069             break;
1070         default:
1071             break;
1072         }
1073         if (one) {
1074             BIO_printf(bio_out, "%s\n", fp->name);
1075         } else {
1076             if (i % dc.columns == 0 && i > 0)
1077                 BIO_printf(bio_out, "\n");
1078             BIO_printf(bio_out, "%-*s", dc.width, fp->name);
1079             i++;
1080         }
1081     }
1082     if (!one)
1083         BIO_printf(bio_out, "\n\n");
1084 }
1085 
1086 static void list_pkey(void)
1087 {
1088 #ifndef OPENSSL_NO_DEPRECATED_3_0
1089     int i;
1090 
1091     if (select_name == NULL && include_legacy()) {
1092         BIO_printf(bio_out, "Legacy:\n");
1093         for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
1094             const EVP_PKEY_ASN1_METHOD *ameth;
1095             int pkey_id, pkey_base_id, pkey_flags;
1096             const char *pinfo, *pem_str;
1097             ameth = EVP_PKEY_asn1_get0(i);
1098             EVP_PKEY_asn1_get0_info(&pkey_id, &pkey_base_id, &pkey_flags,
1099                                     &pinfo, &pem_str, ameth);
1100             if (pkey_flags & ASN1_PKEY_ALIAS) {
1101                 BIO_printf(bio_out, " Name: %s\n", OBJ_nid2ln(pkey_id));
1102                 BIO_printf(bio_out, "\tAlias for: %s\n",
1103                            OBJ_nid2ln(pkey_base_id));
1104             } else {
1105                 BIO_printf(bio_out, " Name: %s\n", pinfo);
1106                 BIO_printf(bio_out, "\tType: %s Algorithm\n",
1107                            pkey_flags & ASN1_PKEY_DYNAMIC ?
1108                            "External" : "Builtin");
1109                 BIO_printf(bio_out, "\tOID: %s\n", OBJ_nid2ln(pkey_id));
1110                 if (pem_str == NULL)
1111                     pem_str = "(none)";
1112                 BIO_printf(bio_out, "\tPEM string: %s\n", pem_str);
1113             }
1114         }
1115     }
1116 #endif
1117     BIO_printf(bio_out, "Provided:\n");
1118     BIO_printf(bio_out, " Key Managers:\n");
1119     list_keymanagers();
1120 }
1121 
1122 static void list_pkey_meth(void)
1123 {
1124 #ifndef OPENSSL_NO_DEPRECATED_3_0
1125     size_t i;
1126     size_t meth_count = EVP_PKEY_meth_get_count();
1127 
1128     if (select_name == NULL && include_legacy()) {
1129         BIO_printf(bio_out, "Legacy:\n");
1130         for (i = 0; i < meth_count; i++) {
1131             const EVP_PKEY_METHOD *pmeth = EVP_PKEY_meth_get0(i);
1132             int pkey_id, pkey_flags;
1133 
1134             EVP_PKEY_meth_get0_info(&pkey_id, &pkey_flags, pmeth);
1135             BIO_printf(bio_out, " %s\n", OBJ_nid2ln(pkey_id));
1136             BIO_printf(bio_out, "\tType: %s Algorithm\n",
1137                        pkey_flags & ASN1_PKEY_DYNAMIC ?  "External" : "Builtin");
1138         }
1139     }
1140 #endif
1141     BIO_printf(bio_out, "Provided:\n");
1142     BIO_printf(bio_out, " Encryption:\n");
1143     list_asymciphers();
1144     BIO_printf(bio_out, " Key Exchange:\n");
1145     list_keyexchanges();
1146     BIO_printf(bio_out, " Signatures:\n");
1147     list_signatures();
1148     BIO_printf(bio_out, " Key encapsulation:\n");
1149     list_kems();
1150 }
1151 
1152 DEFINE_STACK_OF(OSSL_STORE_LOADER)
1153 static int store_cmp(const OSSL_STORE_LOADER * const *a,
1154                      const OSSL_STORE_LOADER * const *b)
1155 {
1156     return strcmp(OSSL_PROVIDER_get0_name(OSSL_STORE_LOADER_get0_provider(*a)),
1157                   OSSL_PROVIDER_get0_name(OSSL_STORE_LOADER_get0_provider(*b)));
1158 }
1159 
1160 static void collect_store_loaders(OSSL_STORE_LOADER *store, void *stack)
1161 {
1162     STACK_OF(OSSL_STORE_LOADER) *store_stack = stack;
1163 
1164     if (sk_OSSL_STORE_LOADER_push(store_stack, store) > 0)
1165         OSSL_STORE_LOADER_up_ref(store);
1166 }
1167 
1168 static void list_store_loaders(void)
1169 {
1170     STACK_OF(OSSL_STORE_LOADER) *stores = sk_OSSL_STORE_LOADER_new(store_cmp);
1171     int i;
1172 
1173     if (stores == NULL) {
1174         BIO_printf(bio_err, "ERROR: Memory allocation\n");
1175         return;
1176     }
1177     BIO_printf(bio_out, "Provided STORE LOADERs:\n");
1178     OSSL_STORE_LOADER_do_all_provided(app_get0_libctx(), collect_store_loaders,
1179                                       stores);
1180     sk_OSSL_STORE_LOADER_sort(stores);
1181     for (i = 0; i < sk_OSSL_STORE_LOADER_num(stores); i++) {
1182         const OSSL_STORE_LOADER *m = sk_OSSL_STORE_LOADER_value(stores, i);
1183         STACK_OF(OPENSSL_CSTRING) *names = NULL;
1184 
1185         if (select_name != NULL && !OSSL_STORE_LOADER_is_a(m, select_name))
1186             continue;
1187 
1188         names = sk_OPENSSL_CSTRING_new(name_cmp);
1189         if (names != NULL && OSSL_STORE_LOADER_names_do_all(m, collect_names,
1190                                                             names)) {
1191             BIO_printf(bio_out, "  ");
1192             print_names(bio_out, names);
1193 
1194             BIO_printf(bio_out, " @ %s\n",
1195                        OSSL_PROVIDER_get0_name(OSSL_STORE_LOADER_get0_provider(m)));
1196         }
1197         sk_OPENSSL_CSTRING_free(names);
1198     }
1199     sk_OSSL_STORE_LOADER_pop_free(stores, OSSL_STORE_LOADER_free);
1200 }
1201 
1202 DEFINE_STACK_OF(OSSL_PROVIDER)
1203 static int provider_cmp(const OSSL_PROVIDER * const *a,
1204                         const OSSL_PROVIDER * const *b)
1205 {
1206     return strcmp(OSSL_PROVIDER_get0_name(*a), OSSL_PROVIDER_get0_name(*b));
1207 }
1208 
1209 static int collect_providers(OSSL_PROVIDER *provider, void *stack)
1210 {
1211     STACK_OF(OSSL_PROVIDER) *provider_stack = stack;
1212     /*
1213      * If OK - result is the index of inserted data
1214      * Error - result is -1 or 0
1215      */
1216     return sk_OSSL_PROVIDER_push(provider_stack, provider) > 0 ? 1 : 0;
1217 }
1218 
1219 static void list_provider_info(void)
1220 {
1221     STACK_OF(OSSL_PROVIDER) *providers = sk_OSSL_PROVIDER_new(provider_cmp);
1222     OSSL_PARAM params[5];
1223     char *name, *version, *buildinfo;
1224     int status;
1225     int i;
1226 
1227     if (providers == NULL) {
1228         BIO_printf(bio_err, "ERROR: Memory allocation\n");
1229         return;
1230     }
1231 
1232     if (OSSL_PROVIDER_do_all(NULL, &collect_providers, providers) != 1) {
1233         sk_OSSL_PROVIDER_free(providers);
1234         BIO_printf(bio_err, "ERROR: Memory allocation\n");
1235         return;
1236     }
1237 
1238     BIO_printf(bio_out, "Providers:\n");
1239     sk_OSSL_PROVIDER_sort(providers);
1240     for (i = 0; i < sk_OSSL_PROVIDER_num(providers); i++) {
1241         const OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(providers, i);
1242         const char *provname = OSSL_PROVIDER_get0_name(prov);
1243 
1244         BIO_printf(bio_out, "  %s\n", provname);
1245 
1246         /* Query the "known" information parameters, the order matches below */
1247         params[0] = OSSL_PARAM_construct_utf8_ptr(OSSL_PROV_PARAM_NAME,
1248                                                   &name, 0);
1249         params[1] = OSSL_PARAM_construct_utf8_ptr(OSSL_PROV_PARAM_VERSION,
1250                                                   &version, 0);
1251         params[2] = OSSL_PARAM_construct_int(OSSL_PROV_PARAM_STATUS, &status);
1252         params[3] = OSSL_PARAM_construct_utf8_ptr(OSSL_PROV_PARAM_BUILDINFO,
1253                                                   &buildinfo, 0);
1254         params[4] = OSSL_PARAM_construct_end();
1255         OSSL_PARAM_set_all_unmodified(params);
1256         if (!OSSL_PROVIDER_get_params(prov, params)) {
1257             BIO_printf(bio_err,
1258                        "WARNING: Unable to query provider parameters for %s\n",
1259                        provname);
1260         } else {
1261             /* Print out the provider information, the params order matches above */
1262             if (OSSL_PARAM_modified(params))
1263                 BIO_printf(bio_out, "    name: %s\n", name);
1264             if (OSSL_PARAM_modified(params + 1))
1265                 BIO_printf(bio_out, "    version: %s\n", version);
1266             if (OSSL_PARAM_modified(params + 2))
1267                 BIO_printf(bio_out, "    status: %sactive\n", status ? "" : "in");
1268             if (verbose) {
1269                 if (OSSL_PARAM_modified(params + 3))
1270                     BIO_printf(bio_out, "    build info: %s\n", buildinfo);
1271                 print_param_types("gettable provider parameters",
1272                                   OSSL_PROVIDER_gettable_params(prov), 4);
1273             }
1274         }
1275     }
1276     sk_OSSL_PROVIDER_free(providers);
1277 }
1278 
1279 #ifndef OPENSSL_NO_DEPRECATED_3_0
1280 static void list_engines(void)
1281 {
1282 # ifndef OPENSSL_NO_ENGINE
1283     ENGINE *e;
1284 
1285     BIO_puts(bio_out, "Engines:\n");
1286     e = ENGINE_get_first();
1287     while (e) {
1288         BIO_printf(bio_out, "%s\n", ENGINE_get_id(e));
1289         e = ENGINE_get_next(e);
1290     }
1291 # else
1292     BIO_puts(bio_out, "Engine support is disabled.\n");
1293 # endif
1294 }
1295 #endif
1296 
1297 static void list_disabled(void)
1298 {
1299     BIO_puts(bio_out, "Disabled algorithms:\n");
1300 #ifdef OPENSSL_NO_ARIA
1301     BIO_puts(bio_out, "ARIA\n");
1302 #endif
1303 #ifdef OPENSSL_NO_BF
1304     BIO_puts(bio_out, "BF\n");
1305 #endif
1306 #ifdef OPENSSL_NO_BLAKE2
1307     BIO_puts(bio_out, "BLAKE2\n");
1308 #endif
1309 #ifdef OPENSSL_NO_CAMELLIA
1310     BIO_puts(bio_out, "CAMELLIA\n");
1311 #endif
1312 #ifdef OPENSSL_NO_CAST
1313     BIO_puts(bio_out, "CAST\n");
1314 #endif
1315 #ifdef OPENSSL_NO_CMAC
1316     BIO_puts(bio_out, "CMAC\n");
1317 #endif
1318 #ifdef OPENSSL_NO_CMS
1319     BIO_puts(bio_out, "CMS\n");
1320 #endif
1321 #ifdef OPENSSL_NO_COMP
1322     BIO_puts(bio_out, "COMP\n");
1323 #endif
1324 #ifdef OPENSSL_NO_DES
1325     BIO_puts(bio_out, "DES\n");
1326 #endif
1327 #ifdef OPENSSL_NO_DGRAM
1328     BIO_puts(bio_out, "DGRAM\n");
1329 #endif
1330 #ifdef OPENSSL_NO_DH
1331     BIO_puts(bio_out, "DH\n");
1332 #endif
1333 #ifdef OPENSSL_NO_DSA
1334     BIO_puts(bio_out, "DSA\n");
1335 #endif
1336 #if defined(OPENSSL_NO_DTLS)
1337     BIO_puts(bio_out, "DTLS\n");
1338 #endif
1339 #if defined(OPENSSL_NO_DTLS1)
1340     BIO_puts(bio_out, "DTLS1\n");
1341 #endif
1342 #if defined(OPENSSL_NO_DTLS1_2)
1343     BIO_puts(bio_out, "DTLS1_2\n");
1344 #endif
1345 #ifdef OPENSSL_NO_EC
1346     BIO_puts(bio_out, "EC\n");
1347 #endif
1348 #ifdef OPENSSL_NO_EC2M
1349     BIO_puts(bio_out, "EC2M\n");
1350 #endif
1351 #if defined(OPENSSL_NO_ENGINE) && !defined(OPENSSL_NO_DEPRECATED_3_0)
1352     BIO_puts(bio_out, "ENGINE\n");
1353 #endif
1354 #ifdef OPENSSL_NO_GOST
1355     BIO_puts(bio_out, "GOST\n");
1356 #endif
1357 #ifdef OPENSSL_NO_IDEA
1358     BIO_puts(bio_out, "IDEA\n");
1359 #endif
1360 #ifdef OPENSSL_NO_MD2
1361     BIO_puts(bio_out, "MD2\n");
1362 #endif
1363 #ifdef OPENSSL_NO_MD4
1364     BIO_puts(bio_out, "MD4\n");
1365 #endif
1366 #ifdef OPENSSL_NO_MD5
1367     BIO_puts(bio_out, "MD5\n");
1368 #endif
1369 #ifdef OPENSSL_NO_MDC2
1370     BIO_puts(bio_out, "MDC2\n");
1371 #endif
1372 #ifdef OPENSSL_NO_OCB
1373     BIO_puts(bio_out, "OCB\n");
1374 #endif
1375 #ifdef OPENSSL_NO_OCSP
1376     BIO_puts(bio_out, "OCSP\n");
1377 #endif
1378 #ifdef OPENSSL_NO_PSK
1379     BIO_puts(bio_out, "PSK\n");
1380 #endif
1381 #ifdef OPENSSL_NO_RC2
1382     BIO_puts(bio_out, "RC2\n");
1383 #endif
1384 #ifdef OPENSSL_NO_RC4
1385     BIO_puts(bio_out, "RC4\n");
1386 #endif
1387 #ifdef OPENSSL_NO_RC5
1388     BIO_puts(bio_out, "RC5\n");
1389 #endif
1390 #ifdef OPENSSL_NO_RMD160
1391     BIO_puts(bio_out, "RMD160\n");
1392 #endif
1393 #ifdef OPENSSL_NO_SCRYPT
1394     BIO_puts(bio_out, "SCRYPT\n");
1395 #endif
1396 #ifdef OPENSSL_NO_SCTP
1397     BIO_puts(bio_out, "SCTP\n");
1398 #endif
1399 #ifdef OPENSSL_NO_SEED
1400     BIO_puts(bio_out, "SEED\n");
1401 #endif
1402 #ifdef OPENSSL_NO_SM2
1403     BIO_puts(bio_out, "SM2\n");
1404 #endif
1405 #ifdef OPENSSL_NO_SM3
1406     BIO_puts(bio_out, "SM3\n");
1407 #endif
1408 #ifdef OPENSSL_NO_SM4
1409     BIO_puts(bio_out, "SM4\n");
1410 #endif
1411 #ifdef OPENSSL_NO_SOCK
1412     BIO_puts(bio_out, "SOCK\n");
1413 #endif
1414 #ifdef OPENSSL_NO_SRP
1415     BIO_puts(bio_out, "SRP\n");
1416 #endif
1417 #ifdef OPENSSL_NO_SRTP
1418     BIO_puts(bio_out, "SRTP\n");
1419 #endif
1420 #ifdef OPENSSL_NO_SSL3
1421     BIO_puts(bio_out, "SSL3\n");
1422 #endif
1423 #ifdef OPENSSL_NO_TLS1
1424     BIO_puts(bio_out, "TLS1\n");
1425 #endif
1426 #ifdef OPENSSL_NO_TLS1_1
1427     BIO_puts(bio_out, "TLS1_1\n");
1428 #endif
1429 #ifdef OPENSSL_NO_TLS1_2
1430     BIO_puts(bio_out, "TLS1_2\n");
1431 #endif
1432 #ifdef OPENSSL_NO_WHIRLPOOL
1433     BIO_puts(bio_out, "WHIRLPOOL\n");
1434 #endif
1435 #ifndef ZLIB
1436     BIO_puts(bio_out, "ZLIB\n");
1437 #endif
1438 }
1439 
1440 /* Unified enum for help and list commands. */
1441 typedef enum HELPLIST_CHOICE {
1442     OPT_COMMON,
1443     OPT_ONE, OPT_VERBOSE,
1444     OPT_COMMANDS, OPT_DIGEST_COMMANDS, OPT_MAC_ALGORITHMS, OPT_OPTIONS,
1445     OPT_DIGEST_ALGORITHMS, OPT_CIPHER_COMMANDS, OPT_CIPHER_ALGORITHMS,
1446     OPT_PK_ALGORITHMS, OPT_PK_METHOD, OPT_DISABLED,
1447     OPT_KDF_ALGORITHMS, OPT_RANDOM_INSTANCES, OPT_RANDOM_GENERATORS,
1448     OPT_ENCODERS, OPT_DECODERS, OPT_KEYMANAGERS, OPT_KEYEXCHANGE_ALGORITHMS,
1449     OPT_KEM_ALGORITHMS, OPT_SIGNATURE_ALGORITHMS, OPT_ASYM_CIPHER_ALGORITHMS,
1450     OPT_STORE_LOADERS, OPT_PROVIDER_INFO,
1451     OPT_OBJECTS, OPT_SELECT_NAME,
1452 #ifndef OPENSSL_NO_DEPRECATED_3_0
1453     OPT_ENGINES,
1454 #endif
1455     OPT_PROV_ENUM
1456 } HELPLIST_CHOICE;
1457 
1458 const OPTIONS list_options[] = {
1459 
1460     OPT_SECTION("General"),
1461     {"help", OPT_HELP, '-', "Display this summary"},
1462 
1463     OPT_SECTION("Output"),
1464     {"1", OPT_ONE, '-', "List in one column"},
1465     {"verbose", OPT_VERBOSE, '-', "Verbose listing"},
1466     {"select", OPT_SELECT_NAME, 's', "Select a single algorithm"},
1467     {"commands", OPT_COMMANDS, '-', "List of standard commands"},
1468     {"standard-commands", OPT_COMMANDS, '-', "List of standard commands"},
1469 #ifndef OPENSSL_NO_DEPRECATED_3_0
1470     {"digest-commands", OPT_DIGEST_COMMANDS, '-',
1471      "List of message digest commands (deprecated)"},
1472 #endif
1473     {"digest-algorithms", OPT_DIGEST_ALGORITHMS, '-',
1474      "List of message digest algorithms"},
1475     {"kdf-algorithms", OPT_KDF_ALGORITHMS, '-',
1476      "List of key derivation and pseudo random function algorithms"},
1477     {"random-instances", OPT_RANDOM_INSTANCES, '-',
1478      "List the primary, public and private random number generator details"},
1479     {"random-generators", OPT_RANDOM_GENERATORS, '-',
1480      "List of random number generators"},
1481     {"mac-algorithms", OPT_MAC_ALGORITHMS, '-',
1482      "List of message authentication code algorithms"},
1483 #ifndef OPENSSL_NO_DEPRECATED_3_0
1484     {"cipher-commands", OPT_CIPHER_COMMANDS, '-',
1485     "List of cipher commands (deprecated)"},
1486 #endif
1487     {"cipher-algorithms", OPT_CIPHER_ALGORITHMS, '-',
1488      "List of symmetric cipher algorithms"},
1489     {"encoders", OPT_ENCODERS, '-', "List of encoding methods" },
1490     {"decoders", OPT_DECODERS, '-', "List of decoding methods" },
1491     {"key-managers", OPT_KEYMANAGERS, '-', "List of key managers" },
1492     {"key-exchange-algorithms", OPT_KEYEXCHANGE_ALGORITHMS, '-',
1493      "List of key exchange algorithms" },
1494     {"kem-algorithms", OPT_KEM_ALGORITHMS, '-',
1495      "List of key encapsulation mechanism algorithms" },
1496     {"signature-algorithms", OPT_SIGNATURE_ALGORITHMS, '-',
1497      "List of signature algorithms" },
1498     {"asymcipher-algorithms", OPT_ASYM_CIPHER_ALGORITHMS, '-',
1499       "List of asymmetric cipher algorithms" },
1500     {"public-key-algorithms", OPT_PK_ALGORITHMS, '-',
1501      "List of public key algorithms"},
1502     {"public-key-methods", OPT_PK_METHOD, '-',
1503      "List of public key methods"},
1504     {"store-loaders", OPT_STORE_LOADERS, '-',
1505      "List of store loaders"},
1506     {"providers", OPT_PROVIDER_INFO, '-',
1507      "List of provider information"},
1508 #ifndef OPENSSL_NO_DEPRECATED_3_0
1509     {"engines", OPT_ENGINES, '-',
1510      "List of loaded engines"},
1511 #endif
1512     {"disabled", OPT_DISABLED, '-', "List of disabled features"},
1513     {"options", OPT_OPTIONS, 's',
1514      "List options for specified command"},
1515     {"objects", OPT_OBJECTS, '-',
1516      "List built in objects (OID<->name mappings)"},
1517 
1518     OPT_PROV_OPTIONS,
1519     {NULL}
1520 };
1521 
1522 int list_main(int argc, char **argv)
1523 {
1524     char *prog;
1525     HELPLIST_CHOICE o;
1526     int one = 0, done = 0;
1527     struct {
1528         unsigned int commands:1;
1529         unsigned int random_instances:1;
1530         unsigned int random_generators:1;
1531         unsigned int digest_commands:1;
1532         unsigned int digest_algorithms:1;
1533         unsigned int kdf_algorithms:1;
1534         unsigned int mac_algorithms:1;
1535         unsigned int cipher_commands:1;
1536         unsigned int cipher_algorithms:1;
1537         unsigned int encoder_algorithms:1;
1538         unsigned int decoder_algorithms:1;
1539         unsigned int keymanager_algorithms:1;
1540         unsigned int signature_algorithms:1;
1541         unsigned int keyexchange_algorithms:1;
1542         unsigned int kem_algorithms:1;
1543         unsigned int asym_cipher_algorithms:1;
1544         unsigned int pk_algorithms:1;
1545         unsigned int pk_method:1;
1546         unsigned int store_loaders:1;
1547         unsigned int provider_info:1;
1548 #ifndef OPENSSL_NO_DEPRECATED_3_0
1549         unsigned int engines:1;
1550 #endif
1551         unsigned int disabled:1;
1552         unsigned int objects:1;
1553         unsigned int options:1;
1554     } todo = { 0, };
1555 
1556     verbose = 0;                 /* Clear a possible previous call */
1557 
1558     prog = opt_init(argc, argv, list_options);
1559     while ((o = opt_next()) != OPT_EOF) {
1560         switch (o) {
1561         case OPT_EOF:  /* Never hit, but suppresses warning */
1562         case OPT_ERR:
1563 opthelp:
1564             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
1565             return 1;
1566         case OPT_HELP:
1567             opt_help(list_options);
1568             return 0;
1569         case OPT_ONE:
1570             one = 1;
1571             break;
1572         case OPT_COMMANDS:
1573             todo.commands = 1;
1574             break;
1575         case OPT_DIGEST_COMMANDS:
1576             todo.digest_commands = 1;
1577             break;
1578         case OPT_DIGEST_ALGORITHMS:
1579             todo.digest_algorithms = 1;
1580             break;
1581         case OPT_KDF_ALGORITHMS:
1582             todo.kdf_algorithms = 1;
1583             break;
1584         case OPT_RANDOM_INSTANCES:
1585             todo.random_instances = 1;
1586             break;
1587         case OPT_RANDOM_GENERATORS:
1588             todo.random_generators = 1;
1589             break;
1590         case OPT_MAC_ALGORITHMS:
1591             todo.mac_algorithms = 1;
1592             break;
1593         case OPT_CIPHER_COMMANDS:
1594             todo.cipher_commands = 1;
1595             break;
1596         case OPT_CIPHER_ALGORITHMS:
1597             todo.cipher_algorithms = 1;
1598             break;
1599         case OPT_ENCODERS:
1600             todo.encoder_algorithms = 1;
1601             break;
1602         case OPT_DECODERS:
1603             todo.decoder_algorithms = 1;
1604             break;
1605         case OPT_KEYMANAGERS:
1606             todo.keymanager_algorithms = 1;
1607             break;
1608         case OPT_SIGNATURE_ALGORITHMS:
1609             todo.signature_algorithms = 1;
1610             break;
1611         case OPT_KEYEXCHANGE_ALGORITHMS:
1612             todo.keyexchange_algorithms = 1;
1613             break;
1614         case OPT_KEM_ALGORITHMS:
1615             todo.kem_algorithms = 1;
1616             break;
1617         case OPT_ASYM_CIPHER_ALGORITHMS:
1618             todo.asym_cipher_algorithms = 1;
1619             break;
1620         case OPT_PK_ALGORITHMS:
1621             todo.pk_algorithms = 1;
1622             break;
1623         case OPT_PK_METHOD:
1624             todo.pk_method = 1;
1625             break;
1626         case OPT_STORE_LOADERS:
1627             todo.store_loaders = 1;
1628             break;
1629         case OPT_PROVIDER_INFO:
1630             todo.provider_info = 1;
1631             break;
1632 #ifndef OPENSSL_NO_DEPRECATED_3_0
1633         case OPT_ENGINES:
1634             todo.engines = 1;
1635             break;
1636 #endif
1637         case OPT_DISABLED:
1638             todo.disabled = 1;
1639             break;
1640         case OPT_OBJECTS:
1641             todo.objects = 1;
1642             break;
1643         case OPT_OPTIONS:
1644             list_options_for_command(opt_arg());
1645             break;
1646         case OPT_VERBOSE:
1647             verbose = 1;
1648             break;
1649         case OPT_SELECT_NAME:
1650             select_name = opt_arg();
1651             break;
1652         case OPT_PROV_CASES:
1653             if (!opt_provider(o))
1654                 return 1;
1655             break;
1656         }
1657         done = 1;
1658     }
1659 
1660     /* No extra arguments. */
1661     if (opt_num_rest() != 0)
1662         goto opthelp;
1663 
1664     if (todo.commands)
1665         list_type(FT_general, one);
1666     if (todo.random_instances)
1667         list_random_instances();
1668     if (todo.random_generators)
1669         list_random_generators();
1670     if (todo.digest_commands)
1671         list_type(FT_md, one);
1672     if (todo.digest_algorithms)
1673         list_digests();
1674     if (todo.kdf_algorithms)
1675         list_kdfs();
1676     if (todo.mac_algorithms)
1677         list_macs();
1678     if (todo.cipher_commands)
1679         list_type(FT_cipher, one);
1680     if (todo.cipher_algorithms)
1681         list_ciphers();
1682     if (todo.encoder_algorithms)
1683         list_encoders();
1684     if (todo.decoder_algorithms)
1685         list_decoders();
1686     if (todo.keymanager_algorithms)
1687         list_keymanagers();
1688     if (todo.signature_algorithms)
1689         list_signatures();
1690     if (todo.asym_cipher_algorithms)
1691         list_asymciphers();
1692     if (todo.keyexchange_algorithms)
1693         list_keyexchanges();
1694     if (todo.kem_algorithms)
1695         list_kems();
1696     if (todo.pk_algorithms)
1697         list_pkey();
1698     if (todo.pk_method)
1699         list_pkey_meth();
1700     if (todo.store_loaders)
1701         list_store_loaders();
1702     if (todo.provider_info)
1703         list_provider_info();
1704 #ifndef OPENSSL_NO_DEPRECATED_3_0
1705     if (todo.engines)
1706         list_engines();
1707 #endif
1708     if (todo.disabled)
1709         list_disabled();
1710     if (todo.objects)
1711         list_objects();
1712 
1713     if (!done)
1714         goto opthelp;
1715 
1716     return 0;
1717 }
1718