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