2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
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
11 #include <openssl/evp.h>
12 #include <openssl/err.h>
13 #include <openssl/provider.h>
14 #include <openssl/safestack.h>
15 #include <openssl/kdf.h>
17 #include "app_params.h"
22 DEFINE_STACK_OF_CSTRING()
24 static int verbose = 0;
26 static void legacy_cipher_fn(const EVP_CIPHER *c,
27 const char *from, const char *to, void *arg)
30 BIO_printf(arg, " %s\n", EVP_CIPHER_name(c));
36 BIO_printf(arg, " %s => %s\n", from, to);
40 DEFINE_STACK_OF(EVP_CIPHER)
41 static int cipher_cmp(const EVP_CIPHER * const *a,
42 const EVP_CIPHER * const *b)
44 int ret = EVP_CIPHER_number(*a) - EVP_CIPHER_number(*b);
47 ret = strcmp(OSSL_PROVIDER_name(EVP_CIPHER_provider(*a)),
48 OSSL_PROVIDER_name(EVP_CIPHER_provider(*b)));
53 static void collect_ciphers(EVP_CIPHER *cipher, void *stack)
55 STACK_OF(EVP_CIPHER) *cipher_stack = stack;
57 if (sk_EVP_CIPHER_push(cipher_stack, cipher) > 0)
58 EVP_CIPHER_up_ref(cipher);
61 static void list_ciphers(void)
63 STACK_OF(EVP_CIPHER) *ciphers = sk_EVP_CIPHER_new(cipher_cmp);
66 BIO_printf(bio_out, "Legacy:\n");
67 EVP_CIPHER_do_all_sorted(legacy_cipher_fn, bio_out);
69 BIO_printf(bio_out, "Provided:\n");
70 EVP_CIPHER_do_all_provided(NULL, collect_ciphers, ciphers);
71 sk_EVP_CIPHER_sort(ciphers);
72 for (i = 0; i < sk_EVP_CIPHER_num(ciphers); i++) {
73 const EVP_CIPHER *c = sk_EVP_CIPHER_value(ciphers, i);
74 STACK_OF(OPENSSL_CSTRING) *names =
75 sk_OPENSSL_CSTRING_new(name_cmp);
77 EVP_CIPHER_names_do_all(c, collect_names, names);
79 BIO_printf(bio_out, " ");
80 print_names(bio_out, names);
81 BIO_printf(bio_out, " @ %s\n",
82 OSSL_PROVIDER_name(EVP_CIPHER_provider(c)));
84 sk_OPENSSL_CSTRING_free(names);
87 print_param_types("retrievable algorithm parameters",
88 EVP_CIPHER_gettable_params(c), 4);
89 print_param_types("retrievable operation parameters",
90 EVP_CIPHER_gettable_ctx_params(c), 4);
91 print_param_types("settable operation parameters",
92 EVP_CIPHER_settable_ctx_params(c), 4);
95 sk_EVP_CIPHER_pop_free(ciphers, EVP_CIPHER_free);
98 static void list_md_fn(const EVP_MD *m,
99 const char *from, const char *to, void *arg)
102 BIO_printf(arg, " %s\n", EVP_MD_name(m));
105 from = "<undefined>";
108 BIO_printf((BIO *)arg, " %s => %s\n", from, to);
112 DEFINE_STACK_OF(EVP_MD)
113 static int md_cmp(const EVP_MD * const *a, const EVP_MD * const *b)
115 int ret = EVP_MD_number(*a) - EVP_MD_number(*b);
118 ret = strcmp(OSSL_PROVIDER_name(EVP_MD_provider(*a)),
119 OSSL_PROVIDER_name(EVP_MD_provider(*b)));
124 static void collect_digests(EVP_MD *md, void *stack)
126 STACK_OF(EVP_MD) *digest_stack = stack;
128 if (sk_EVP_MD_push(digest_stack, md) > 0)
132 static void list_digests(void)
134 STACK_OF(EVP_MD) *digests = sk_EVP_MD_new(md_cmp);
137 BIO_printf(bio_out, "Legacy:\n");
138 EVP_MD_do_all_sorted(list_md_fn, bio_out);
140 BIO_printf(bio_out, "Provided:\n");
141 EVP_MD_do_all_provided(NULL, collect_digests, digests);
142 sk_EVP_MD_sort(digests);
143 for (i = 0; i < sk_EVP_MD_num(digests); i++) {
144 const EVP_MD *m = sk_EVP_MD_value(digests, i);
145 STACK_OF(OPENSSL_CSTRING) *names =
146 sk_OPENSSL_CSTRING_new(name_cmp);
148 EVP_MD_names_do_all(m, collect_names, names);
150 BIO_printf(bio_out, " ");
151 print_names(bio_out, names);
152 BIO_printf(bio_out, " @ %s\n",
153 OSSL_PROVIDER_name(EVP_MD_provider(m)));
155 sk_OPENSSL_CSTRING_free(names);
158 print_param_types("retrievable algorithm parameters",
159 EVP_MD_gettable_params(m), 4);
160 print_param_types("retrievable operation parameters",
161 EVP_MD_gettable_ctx_params(m), 4);
162 print_param_types("settable operation parameters",
163 EVP_MD_settable_ctx_params(m), 4);
166 sk_EVP_MD_pop_free(digests, EVP_MD_free);
169 DEFINE_STACK_OF(EVP_MAC)
170 static int mac_cmp(const EVP_MAC * const *a, const EVP_MAC * const *b)
172 int ret = EVP_MAC_number(*a) - EVP_MAC_number(*b);
175 ret = strcmp(OSSL_PROVIDER_name(EVP_MAC_provider(*a)),
176 OSSL_PROVIDER_name(EVP_MAC_provider(*b)));
181 static void collect_macs(EVP_MAC *mac, void *stack)
183 STACK_OF(EVP_MAC) *mac_stack = stack;
185 if (sk_EVP_MAC_push(mac_stack, mac) > 0)
189 static void list_macs(void)
191 STACK_OF(EVP_MAC) *macs = sk_EVP_MAC_new(mac_cmp);
194 BIO_printf(bio_out, "Provided MACs:\n");
195 EVP_MAC_do_all_provided(NULL, collect_macs, macs);
196 sk_EVP_MAC_sort(macs);
197 for (i = 0; i < sk_EVP_MAC_num(macs); i++) {
198 const EVP_MAC *m = sk_EVP_MAC_value(macs, i);
199 STACK_OF(OPENSSL_CSTRING) *names =
200 sk_OPENSSL_CSTRING_new(name_cmp);
202 EVP_MAC_names_do_all(m, collect_names, names);
204 BIO_printf(bio_out, " ");
205 print_names(bio_out, names);
206 BIO_printf(bio_out, " @ %s\n",
207 OSSL_PROVIDER_name(EVP_MAC_provider(m)));
209 sk_OPENSSL_CSTRING_free(names);
212 print_param_types("retrievable algorithm parameters",
213 EVP_MAC_gettable_params(m), 4);
214 print_param_types("retrievable operation parameters",
215 EVP_MAC_gettable_ctx_params(m), 4);
216 print_param_types("settable operation parameters",
217 EVP_MAC_settable_ctx_params(m), 4);
220 sk_EVP_MAC_pop_free(macs, EVP_MAC_free);
226 DEFINE_STACK_OF(EVP_KDF)
227 static int kdf_cmp(const EVP_KDF * const *a, const EVP_KDF * const *b)
229 int ret = EVP_KDF_number(*a) - EVP_KDF_number(*b);
232 ret = strcmp(OSSL_PROVIDER_name(EVP_KDF_provider(*a)),
233 OSSL_PROVIDER_name(EVP_KDF_provider(*b)));
238 static void collect_kdfs(EVP_KDF *kdf, void *stack)
240 STACK_OF(EVP_KDF) *kdf_stack = stack;
242 sk_EVP_KDF_push(kdf_stack, kdf);
246 static void list_kdfs(void)
248 STACK_OF(EVP_KDF) *kdfs = sk_EVP_KDF_new(kdf_cmp);
251 BIO_printf(bio_out, "Provided KDFs and PDFs:\n");
252 EVP_KDF_do_all_provided(NULL, collect_kdfs, kdfs);
253 sk_EVP_KDF_sort(kdfs);
254 for (i = 0; i < sk_EVP_KDF_num(kdfs); i++) {
255 const EVP_KDF *k = sk_EVP_KDF_value(kdfs, i);
256 STACK_OF(OPENSSL_CSTRING) *names =
257 sk_OPENSSL_CSTRING_new(name_cmp);
259 EVP_KDF_names_do_all(k, collect_names, names);
261 BIO_printf(bio_out, " ");
262 print_names(bio_out, names);
263 BIO_printf(bio_out, " @ %s\n",
264 OSSL_PROVIDER_name(EVP_KDF_provider(k)));
266 sk_OPENSSL_CSTRING_free(names);
269 print_param_types("retrievable algorithm parameters",
270 EVP_KDF_gettable_params(k), 4);
271 print_param_types("retrievable operation parameters",
272 EVP_KDF_gettable_ctx_params(k), 4);
273 print_param_types("settable operation parameters",
274 EVP_KDF_settable_ctx_params(k), 4);
277 sk_EVP_KDF_pop_free(kdfs, EVP_KDF_free);
280 static void list_missing_help(void)
285 for (fp = functions; fp->name != NULL; fp++) {
286 if ((o = fp->help) != NULL) {
287 /* If there is help, list what flags are not documented. */
288 for ( ; o->name != NULL; o++) {
289 if (o->helpstr == NULL)
290 BIO_printf(bio_out, "%s %s\n", fp->name, o->name);
292 } else if (fp->func != dgst_main) {
293 /* If not aliased to the dgst command, */
294 BIO_printf(bio_out, "%s *\n", fp->name);
299 static void list_objects(void)
301 int max_nid = OBJ_new_nid(0);
303 char *oid_buf = NULL;
306 /* Skip 0, since that's NID_undef */
307 for (i = 1; i < max_nid; i++) {
308 const ASN1_OBJECT *obj = OBJ_nid2obj(i);
309 const char *sn = OBJ_nid2sn(i);
310 const char *ln = OBJ_nid2ln(i);
314 * If one of the retrieved objects somehow generated an error,
315 * we ignore it. The check for NID_undef below will detect the
316 * error and simply skip to the next NID.
320 if (OBJ_obj2nid(obj) == NID_undef)
323 if ((n = OBJ_obj2txt(NULL, 0, obj, 1)) == 0) {
324 BIO_printf(bio_out, "# None-OID object: %s, %s\n", sn, ln);
331 oid_buf = OPENSSL_realloc(oid_buf, n + 1);
332 if (oid_buf == NULL) {
333 BIO_printf(bio_err, "ERROR: Memory allocation\n");
338 if (OBJ_obj2txt(oid_buf, oid_size, obj, 1) < 0)
340 if (ln == NULL || strcmp(sn, ln) == 0)
341 BIO_printf(bio_out, "%s = %s\n", sn, oid_buf);
343 BIO_printf(bio_out, "%s = %s, %s\n", sn, ln, oid_buf);
346 OPENSSL_free(oid_buf);
349 static void list_options_for_command(const char *command)
354 for (fp = functions; fp->name != NULL; fp++)
355 if (strcmp(fp->name, command) == 0)
357 if (fp->name == NULL) {
358 BIO_printf(bio_err, "Invalid command '%s'; type \"help\" for a list.\n",
363 if ((o = fp->help) == NULL)
366 for ( ; o->name != NULL; o++) {
369 if (o->name == OPT_PARAM_STR)
372 if (o->name == OPT_HELP_STR
373 || o->name == OPT_MORE_STR
374 || o->name == OPT_SECTION_STR
375 || o->name[0] == '\0')
377 BIO_printf(bio_out, "%s %c\n", o->name, c == '\0' ? '-' : c);
379 /* Always output the -- marker since it is sometimes documented. */
380 BIO_printf(bio_out, "- -\n");
383 static void list_type(FUNC_TYPE ft, int one)
389 memset(&dc, 0, sizeof(dc));
391 calculate_columns(functions, &dc);
393 for (fp = functions; fp->name != NULL; fp++) {
397 BIO_printf(bio_out, "%s\n", fp->name);
399 if (i % dc.columns == 0 && i > 0)
400 BIO_printf(bio_out, "\n");
401 BIO_printf(bio_out, "%-*s", dc.width, fp->name);
406 BIO_printf(bio_out, "\n\n");
409 static void list_pkey(void)
413 for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
414 const EVP_PKEY_ASN1_METHOD *ameth;
415 int pkey_id, pkey_base_id, pkey_flags;
416 const char *pinfo, *pem_str;
417 ameth = EVP_PKEY_asn1_get0(i);
418 EVP_PKEY_asn1_get0_info(&pkey_id, &pkey_base_id, &pkey_flags,
419 &pinfo, &pem_str, ameth);
420 if (pkey_flags & ASN1_PKEY_ALIAS) {
421 BIO_printf(bio_out, "Name: %s\n", OBJ_nid2ln(pkey_id));
422 BIO_printf(bio_out, "\tAlias for: %s\n",
423 OBJ_nid2ln(pkey_base_id));
425 BIO_printf(bio_out, "Name: %s\n", pinfo);
426 BIO_printf(bio_out, "\tType: %s Algorithm\n",
427 pkey_flags & ASN1_PKEY_DYNAMIC ?
428 "External" : "Builtin");
429 BIO_printf(bio_out, "\tOID: %s\n", OBJ_nid2ln(pkey_id));
432 BIO_printf(bio_out, "\tPEM string: %s\n", pem_str);
438 static void list_pkey_meth(void)
441 size_t meth_count = EVP_PKEY_meth_get_count();
443 for (i = 0; i < meth_count; i++) {
444 const EVP_PKEY_METHOD *pmeth = EVP_PKEY_meth_get0(i);
445 int pkey_id, pkey_flags;
447 EVP_PKEY_meth_get0_info(&pkey_id, &pkey_flags, pmeth);
448 BIO_printf(bio_out, "%s\n", OBJ_nid2ln(pkey_id));
449 BIO_printf(bio_out, "\tType: %s Algorithm\n",
450 pkey_flags & ASN1_PKEY_DYNAMIC ? "External" : "Builtin");
454 static void list_engines(void)
456 #ifndef OPENSSL_NO_ENGINE
459 BIO_puts(bio_out, "Engines:\n");
460 e = ENGINE_get_first();
462 BIO_printf(bio_out, "%s\n", ENGINE_get_id(e));
463 e = ENGINE_get_next(e);
466 BIO_puts(bio_out, "Engine support is disabled.\n");
470 static void list_disabled(void)
472 BIO_puts(bio_out, "Disabled algorithms:\n");
473 #ifdef OPENSSL_NO_ARIA
474 BIO_puts(bio_out, "ARIA\n");
477 BIO_puts(bio_out, "BF\n");
479 #ifdef OPENSSL_NO_BLAKE2
480 BIO_puts(bio_out, "BLAKE2\n");
482 #ifdef OPENSSL_NO_CAMELLIA
483 BIO_puts(bio_out, "CAMELLIA\n");
485 #ifdef OPENSSL_NO_CAST
486 BIO_puts(bio_out, "CAST\n");
488 #ifdef OPENSSL_NO_CMAC
489 BIO_puts(bio_out, "CMAC\n");
491 #ifdef OPENSSL_NO_CMS
492 BIO_puts(bio_out, "CMS\n");
494 #ifdef OPENSSL_NO_COMP
495 BIO_puts(bio_out, "COMP\n");
497 #ifdef OPENSSL_NO_DES
498 BIO_puts(bio_out, "DES\n");
500 #ifdef OPENSSL_NO_DGRAM
501 BIO_puts(bio_out, "DGRAM\n");
504 BIO_puts(bio_out, "DH\n");
506 #ifdef OPENSSL_NO_DSA
507 BIO_puts(bio_out, "DSA\n");
509 #if defined(OPENSSL_NO_DTLS)
510 BIO_puts(bio_out, "DTLS\n");
512 #if defined(OPENSSL_NO_DTLS1)
513 BIO_puts(bio_out, "DTLS1\n");
515 #if defined(OPENSSL_NO_DTLS1_2)
516 BIO_puts(bio_out, "DTLS1_2\n");
519 BIO_puts(bio_out, "EC\n");
521 #ifdef OPENSSL_NO_EC2M
522 BIO_puts(bio_out, "EC2M\n");
524 #ifdef OPENSSL_NO_ENGINE
525 BIO_puts(bio_out, "ENGINE\n");
527 #ifdef OPENSSL_NO_GOST
528 BIO_puts(bio_out, "GOST\n");
530 #ifdef OPENSSL_NO_IDEA
531 BIO_puts(bio_out, "IDEA\n");
533 #ifdef OPENSSL_NO_MD2
534 BIO_puts(bio_out, "MD2\n");
536 #ifdef OPENSSL_NO_MD4
537 BIO_puts(bio_out, "MD4\n");
539 #ifdef OPENSSL_NO_MD5
540 BIO_puts(bio_out, "MD5\n");
542 #ifdef OPENSSL_NO_MDC2
543 BIO_puts(bio_out, "MDC2\n");
545 #ifdef OPENSSL_NO_OCB
546 BIO_puts(bio_out, "OCB\n");
548 #ifdef OPENSSL_NO_OCSP
549 BIO_puts(bio_out, "OCSP\n");
551 #ifdef OPENSSL_NO_PSK
552 BIO_puts(bio_out, "PSK\n");
554 #ifdef OPENSSL_NO_RC2
555 BIO_puts(bio_out, "RC2\n");
557 #ifdef OPENSSL_NO_RC4
558 BIO_puts(bio_out, "RC4\n");
560 #ifdef OPENSSL_NO_RC5
561 BIO_puts(bio_out, "RC5\n");
563 #ifdef OPENSSL_NO_RMD160
564 BIO_puts(bio_out, "RMD160\n");
566 #ifdef OPENSSL_NO_RSA
567 BIO_puts(bio_out, "RSA\n");
569 #ifdef OPENSSL_NO_SCRYPT
570 BIO_puts(bio_out, "SCRYPT\n");
572 #ifdef OPENSSL_NO_SCTP
573 BIO_puts(bio_out, "SCTP\n");
575 #ifdef OPENSSL_NO_SEED
576 BIO_puts(bio_out, "SEED\n");
578 #ifdef OPENSSL_NO_SM2
579 BIO_puts(bio_out, "SM2\n");
581 #ifdef OPENSSL_NO_SM3
582 BIO_puts(bio_out, "SM3\n");
584 #ifdef OPENSSL_NO_SM4
585 BIO_puts(bio_out, "SM4\n");
587 #ifdef OPENSSL_NO_SOCK
588 BIO_puts(bio_out, "SOCK\n");
590 #ifdef OPENSSL_NO_SRP
591 BIO_puts(bio_out, "SRP\n");
593 #ifdef OPENSSL_NO_SRTP
594 BIO_puts(bio_out, "SRTP\n");
596 #ifdef OPENSSL_NO_SSL3
597 BIO_puts(bio_out, "SSL3\n");
599 #ifdef OPENSSL_NO_TLS1
600 BIO_puts(bio_out, "TLS1\n");
602 #ifdef OPENSSL_NO_TLS1_1
603 BIO_puts(bio_out, "TLS1_1\n");
605 #ifdef OPENSSL_NO_TLS1_2
606 BIO_puts(bio_out, "TLS1_2\n");
608 #ifdef OPENSSL_NO_WHIRLPOOL
609 BIO_puts(bio_out, "WHIRLPOOL\n");
612 BIO_puts(bio_out, "ZLIB\n");
616 /* Unified enum for help and list commands. */
617 typedef enum HELPLIST_CHOICE {
618 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, OPT_ONE, OPT_VERBOSE,
619 OPT_COMMANDS, OPT_DIGEST_COMMANDS, OPT_MAC_ALGORITHMS, OPT_OPTIONS,
620 OPT_DIGEST_ALGORITHMS, OPT_CIPHER_COMMANDS, OPT_CIPHER_ALGORITHMS,
621 OPT_PK_ALGORITHMS, OPT_PK_METHOD, OPT_ENGINES, OPT_DISABLED,
622 OPT_KDF_ALGORITHMS, OPT_MISSING_HELP, OPT_OBJECTS,
626 const OPTIONS list_options[] = {
628 OPT_SECTION("General"),
629 {"help", OPT_HELP, '-', "Display this summary"},
631 OPT_SECTION("Output"),
632 {"1", OPT_ONE, '-', "List in one column"},
633 {"verbose", OPT_VERBOSE, '-', "Verbose listing"},
634 {"commands", OPT_COMMANDS, '-', "List of standard commands"},
635 {"standard-commands", OPT_COMMANDS, '-', "List of standard commands"},
636 {"digest-commands", OPT_DIGEST_COMMANDS, '-',
637 "List of message digest commands"},
638 {"digest-algorithms", OPT_DIGEST_ALGORITHMS, '-',
639 "List of message digest algorithms"},
640 {"kdf-algorithms", OPT_KDF_ALGORITHMS, '-',
641 "List of key derivation and pseudo random function algorithms"},
642 {"mac-algorithms", OPT_MAC_ALGORITHMS, '-',
643 "List of message authentication code algorithms"},
644 {"cipher-commands", OPT_CIPHER_COMMANDS, '-', "List of cipher commands"},
645 {"cipher-algorithms", OPT_CIPHER_ALGORITHMS, '-',
646 "List of cipher algorithms"},
647 {"public-key-algorithms", OPT_PK_ALGORITHMS, '-',
648 "List of public key algorithms"},
649 {"public-key-methods", OPT_PK_METHOD, '-',
650 "List of public key methods"},
651 {"engines", OPT_ENGINES, '-',
652 "List of loaded engines"},
653 {"disabled", OPT_DISABLED, '-',
654 "List of disabled features"},
655 {"missing-help", OPT_MISSING_HELP, '-',
656 "List missing detailed help strings"},
657 {"options", OPT_OPTIONS, 's',
658 "List options for specified command"},
659 {"objects", OPT_OBJECTS, '-',
660 "List built in objects (OID<->name mappings)"},
666 int list_main(int argc, char **argv)
670 int one = 0, done = 0;
672 unsigned int commands:1;
673 unsigned int digest_commands:1;
674 unsigned int digest_algorithms:1;
675 unsigned int kdf_algorithms:1;
676 unsigned int mac_algorithms:1;
677 unsigned int cipher_commands:1;
678 unsigned int cipher_algorithms:1;
679 unsigned int pk_algorithms:1;
680 unsigned int pk_method:1;
681 unsigned int engines:1;
682 unsigned int disabled:1;
683 unsigned int missing_help:1;
684 unsigned int objects:1;
685 unsigned int options:1;
688 verbose = 0; /* Clear a possible previous call */
690 prog = opt_init(argc, argv, list_options);
691 while ((o = opt_next()) != OPT_EOF) {
693 case OPT_EOF: /* Never hit, but suppresses warning */
696 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
699 opt_help(list_options);
707 case OPT_DIGEST_COMMANDS:
708 todo.digest_commands = 1;
710 case OPT_DIGEST_ALGORITHMS:
711 todo.digest_algorithms = 1;
713 case OPT_KDF_ALGORITHMS:
714 todo.kdf_algorithms = 1;
716 case OPT_MAC_ALGORITHMS:
717 todo.mac_algorithms = 1;
719 case OPT_CIPHER_COMMANDS:
720 todo.cipher_commands = 1;
722 case OPT_CIPHER_ALGORITHMS:
723 todo.cipher_algorithms = 1;
725 case OPT_PK_ALGORITHMS:
726 todo.pk_algorithms = 1;
737 case OPT_MISSING_HELP:
738 todo.missing_help = 1;
744 list_options_for_command(opt_arg());
750 if (!opt_provider(o))
756 if (opt_num_rest() != 0) {
757 BIO_printf(bio_err, "Extra arguments given.\n");
762 list_type(FT_general, one);
763 if (todo.digest_commands)
764 list_type(FT_md, one);
765 if (todo.digest_algorithms)
767 if (todo.kdf_algorithms)
769 if (todo.mac_algorithms)
771 if (todo.cipher_commands)
772 list_type(FT_cipher, one);
773 if (todo.cipher_algorithms)
775 if (todo.pk_algorithms)
783 if (todo.missing_help)