2 * Copyright 1995-2019 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"
21 static int verbose = 0;
23 static void legacy_cipher_fn(const EVP_CIPHER *c,
24 const char *from, const char *to, void *arg)
27 BIO_printf(arg, " %s\n", EVP_CIPHER_name(c));
33 BIO_printf(arg, " %s => %s\n", from, to);
37 DEFINE_STACK_OF(EVP_CIPHER)
38 static int cipher_cmp(const EVP_CIPHER * const *a,
39 const EVP_CIPHER * const *b)
41 int ret = strcasecmp(EVP_CIPHER_name(*a), EVP_CIPHER_name(*b));
44 ret = strcmp(OSSL_PROVIDER_name(EVP_CIPHER_provider(*a)),
45 OSSL_PROVIDER_name(EVP_CIPHER_provider(*b)));
50 static void collect_ciphers(EVP_CIPHER *cipher, void *stack)
52 STACK_OF(EVP_CIPHER) *cipher_stack = stack;
54 if (sk_EVP_CIPHER_push(cipher_stack, cipher) > 0)
55 EVP_CIPHER_up_ref(cipher);
58 static void list_ciphers(void)
60 STACK_OF(EVP_CIPHER) *ciphers = sk_EVP_CIPHER_new(cipher_cmp);
63 BIO_printf(bio_out, "Legacy:\n");
64 EVP_CIPHER_do_all_sorted(legacy_cipher_fn, bio_out);
66 BIO_printf(bio_out, "Provided:\n");
67 EVP_CIPHER_do_all_ex(NULL, collect_ciphers, ciphers);
68 sk_EVP_CIPHER_sort(ciphers);
69 for (i = 0; i < sk_EVP_CIPHER_num(ciphers); i++) {
70 const EVP_CIPHER *c = sk_EVP_CIPHER_value(ciphers, i);
72 BIO_printf(bio_out, " %s", EVP_CIPHER_name(c));
73 BIO_printf(bio_out, " @ %s\n",
74 OSSL_PROVIDER_name(EVP_CIPHER_provider(c)));
76 print_param_types("retrievable algorithm parameters",
77 EVP_CIPHER_gettable_params(c), 4);
78 print_param_types("retrievable operation parameters",
79 EVP_CIPHER_gettable_ctx_params(c), 4);
80 print_param_types("settable operation parameters",
81 EVP_CIPHER_settable_ctx_params(c), 4);
84 sk_EVP_CIPHER_pop_free(ciphers, EVP_CIPHER_free);
87 static void list_md_fn(const EVP_MD *m,
88 const char *from, const char *to, void *arg)
91 BIO_printf(arg, " %s\n", EVP_MD_name(m));
97 BIO_printf((BIO *)arg, " %s => %s\n", from, to);
101 DEFINE_STACK_OF(EVP_MD)
102 static int md_cmp(const EVP_MD * const *a, const EVP_MD * const *b)
104 int ret = strcasecmp(EVP_MD_name(*a), EVP_MD_name(*b));
107 ret = strcmp(OSSL_PROVIDER_name(EVP_MD_provider(*a)),
108 OSSL_PROVIDER_name(EVP_MD_provider(*b)));
113 static void collect_digests(EVP_MD *md, void *stack)
115 STACK_OF(EVP_MD) *digest_stack = stack;
117 if (sk_EVP_MD_push(digest_stack, md) > 0)
121 static void list_digests(void)
123 STACK_OF(EVP_MD) *digests = sk_EVP_MD_new(md_cmp);
126 BIO_printf(bio_out, "Legacy:\n");
127 EVP_MD_do_all_sorted(list_md_fn, bio_out);
129 BIO_printf(bio_out, "Provided:\n");
130 EVP_MD_do_all_ex(NULL, collect_digests, digests);
131 sk_EVP_MD_sort(digests);
132 for (i = 0; i < sk_EVP_MD_num(digests); i++) {
133 const EVP_MD *m = sk_EVP_MD_value(digests, i);
135 BIO_printf(bio_out, " %s", EVP_MD_name(m));
136 BIO_printf(bio_out, " @ %s\n",
137 OSSL_PROVIDER_name(EVP_MD_provider(m)));
139 print_param_types("retrievable algorithm parameters",
140 EVP_MD_gettable_params(m), 4);
141 print_param_types("retrievable operation parameters",
142 EVP_MD_gettable_ctx_params(m), 4);
143 print_param_types("settable operation parameters",
144 EVP_MD_settable_ctx_params(m), 4);
147 sk_EVP_MD_pop_free(digests, EVP_MD_free);
150 DEFINE_STACK_OF(EVP_MAC)
151 static int mac_cmp(const EVP_MAC * const *a, const EVP_MAC * const *b)
153 int ret = strcasecmp(EVP_MAC_name(*a), EVP_MAC_name(*b));
156 ret = strcmp(OSSL_PROVIDER_name(EVP_MAC_provider(*a)),
157 OSSL_PROVIDER_name(EVP_MAC_provider(*b)));
162 static void collect_macs(EVP_MAC *mac, void *stack)
164 STACK_OF(EVP_MAC) *mac_stack = stack;
166 if (sk_EVP_MAC_push(mac_stack, mac) > 0)
170 static void list_macs(void)
172 STACK_OF(EVP_MAC) *macs = sk_EVP_MAC_new(mac_cmp);
175 BIO_printf(bio_out, "Provided MACs:\n");
176 EVP_MAC_do_all_ex(NULL, collect_macs, macs);
177 sk_EVP_MAC_sort(macs);
178 for (i = 0; i < sk_EVP_MAC_num(macs); i++) {
179 const EVP_MAC *m = sk_EVP_MAC_value(macs, i);
181 BIO_printf(bio_out, " %s", EVP_MAC_name(m));
182 BIO_printf(bio_out, " @ %s\n",
183 OSSL_PROVIDER_name(EVP_MAC_provider(m)));
186 print_param_types("retrievable algorithm parameters",
187 EVP_MAC_gettable_params(m), 4);
188 print_param_types("retrievable operation parameters",
189 EVP_MAC_gettable_ctx_params(m), 4);
190 print_param_types("settable operation parameters",
191 EVP_MAC_settable_ctx_params(m), 4);
194 sk_EVP_MAC_pop_free(macs, EVP_MAC_free);
200 DEFINE_STACK_OF(EVP_KDF)
201 static int kdf_cmp(const EVP_KDF * const *a, const EVP_KDF * const *b)
203 int ret = strcasecmp(EVP_KDF_name(*a), EVP_KDF_name(*b));
206 ret = strcmp(OSSL_PROVIDER_name(EVP_KDF_provider(*a)),
207 OSSL_PROVIDER_name(EVP_KDF_provider(*b)));
212 static void collect_kdfs(EVP_KDF *kdf, void *stack)
214 STACK_OF(EVP_KDF) *kdf_stack = stack;
216 sk_EVP_KDF_push(kdf_stack, kdf);
220 static void list_kdfs(void)
222 STACK_OF(EVP_KDF) *kdfs = sk_EVP_KDF_new(kdf_cmp);
225 BIO_printf(bio_out, "Provided KDFs and PDFs:\n");
226 EVP_KDF_do_all_ex(NULL, collect_kdfs, kdfs);
227 sk_EVP_KDF_sort(kdfs);
228 for (i = 0; i < sk_EVP_KDF_num(kdfs); i++) {
229 const EVP_KDF *m = sk_EVP_KDF_value(kdfs, i);
231 BIO_printf(bio_out, " %s", EVP_KDF_name(m));
232 BIO_printf(bio_out, " @ %s\n",
233 OSSL_PROVIDER_name(EVP_KDF_provider(m)));
236 print_param_types("retrievable algorithm parameters",
237 EVP_KDF_gettable_params(m), 4);
238 print_param_types("retrievable operation parameters",
239 EVP_KDF_gettable_ctx_params(m), 4);
240 print_param_types("settable operation parameters",
241 EVP_KDF_settable_ctx_params(m), 4);
244 sk_EVP_KDF_pop_free(kdfs, EVP_KDF_free);
247 static void list_missing_help(void)
252 for (fp = functions; fp->name != NULL; fp++) {
253 if ((o = fp->help) != NULL) {
254 /* If there is help, list what flags are not documented. */
255 for ( ; o->name != NULL; o++) {
256 if (o->helpstr == NULL)
257 BIO_printf(bio_out, "%s %s\n", fp->name, o->name);
259 } else if (fp->func != dgst_main) {
260 /* If not aliased to the dgst command, */
261 BIO_printf(bio_out, "%s *\n", fp->name);
266 static void list_objects(void)
268 int max_nid = OBJ_new_nid(0);
270 char *oid_buf = NULL;
273 /* Skip 0, since that's NID_undef */
274 for (i = 1; i < max_nid; i++) {
275 const ASN1_OBJECT *obj = OBJ_nid2obj(i);
276 const char *sn = OBJ_nid2sn(i);
277 const char *ln = OBJ_nid2ln(i);
281 * If one of the retrieved objects somehow generated an error,
282 * we ignore it. The check for NID_undef below will detect the
283 * error and simply skip to the next NID.
287 if (OBJ_obj2nid(obj) == NID_undef)
290 if ((n = OBJ_obj2txt(NULL, 0, obj, 1)) == 0) {
291 BIO_printf(bio_out, "# None-OID object: %s, %s\n", sn, ln);
298 oid_buf = OPENSSL_realloc(oid_buf, n + 1);
299 if (oid_buf == NULL) {
300 BIO_printf(bio_err, "ERROR: Memory allocation\n");
305 if (OBJ_obj2txt(oid_buf, oid_size, obj, 1) < 0)
307 if (ln == NULL || strcmp(sn, ln) == 0)
308 BIO_printf(bio_out, "%s = %s\n", sn, oid_buf);
310 BIO_printf(bio_out, "%s = %s, %s\n", sn, ln, oid_buf);
313 OPENSSL_free(oid_buf);
316 static void list_options_for_command(const char *command)
321 for (fp = functions; fp->name != NULL; fp++)
322 if (strcmp(fp->name, command) == 0)
324 if (fp->name == NULL) {
325 BIO_printf(bio_err, "Invalid command '%s'; type \"help\" for a list.\n",
330 if ((o = fp->help) == NULL)
333 for ( ; o->name != NULL; o++) {
336 if (o->name == OPT_HELP_STR
337 || o->name == OPT_MORE_STR
338 || o->name[0] == '\0')
340 BIO_printf(bio_out, "%s %c\n", o->name, o->valtype);
341 BIO_printf(bio_out, "%s %c\n", o->name, c == '\0' ? '-' : c);
343 /* Always output the -- marker since it is sometimes documented. */
344 BIO_printf(bio_out, "- -\n");
347 static void list_type(FUNC_TYPE ft, int one)
353 memset(&dc, 0, sizeof(dc));
355 calculate_columns(functions, &dc);
357 for (fp = functions; fp->name != NULL; fp++) {
361 BIO_printf(bio_out, "%s\n", fp->name);
363 if (i % dc.columns == 0 && i > 0)
364 BIO_printf(bio_out, "\n");
365 BIO_printf(bio_out, "%-*s", dc.width, fp->name);
370 BIO_printf(bio_out, "\n\n");
373 static void list_pkey(void)
377 for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
378 const EVP_PKEY_ASN1_METHOD *ameth;
379 int pkey_id, pkey_base_id, pkey_flags;
380 const char *pinfo, *pem_str;
381 ameth = EVP_PKEY_asn1_get0(i);
382 EVP_PKEY_asn1_get0_info(&pkey_id, &pkey_base_id, &pkey_flags,
383 &pinfo, &pem_str, ameth);
384 if (pkey_flags & ASN1_PKEY_ALIAS) {
385 BIO_printf(bio_out, "Name: %s\n", OBJ_nid2ln(pkey_id));
386 BIO_printf(bio_out, "\tAlias for: %s\n",
387 OBJ_nid2ln(pkey_base_id));
389 BIO_printf(bio_out, "Name: %s\n", pinfo);
390 BIO_printf(bio_out, "\tType: %s Algorithm\n",
391 pkey_flags & ASN1_PKEY_DYNAMIC ?
392 "External" : "Builtin");
393 BIO_printf(bio_out, "\tOID: %s\n", OBJ_nid2ln(pkey_id));
396 BIO_printf(bio_out, "\tPEM string: %s\n", pem_str);
402 static void list_pkey_meth(void)
405 size_t meth_count = EVP_PKEY_meth_get_count();
407 for (i = 0; i < meth_count; i++) {
408 const EVP_PKEY_METHOD *pmeth = EVP_PKEY_meth_get0(i);
409 int pkey_id, pkey_flags;
411 EVP_PKEY_meth_get0_info(&pkey_id, &pkey_flags, pmeth);
412 BIO_printf(bio_out, "%s\n", OBJ_nid2ln(pkey_id));
413 BIO_printf(bio_out, "\tType: %s Algorithm\n",
414 pkey_flags & ASN1_PKEY_DYNAMIC ? "External" : "Builtin");
418 static void list_engines(void)
420 #ifndef OPENSSL_NO_ENGINE
423 BIO_puts(bio_out, "Engines:\n");
424 e = ENGINE_get_first();
426 BIO_printf(bio_out, "%s\n", ENGINE_get_id(e));
427 e = ENGINE_get_next(e);
430 BIO_puts(bio_out, "Engine support is disabled.\n");
434 static void list_disabled(void)
436 BIO_puts(bio_out, "Disabled algorithms:\n");
437 #ifdef OPENSSL_NO_ARIA
438 BIO_puts(bio_out, "ARIA\n");
441 BIO_puts(bio_out, "BF\n");
443 #ifdef OPENSSL_NO_BLAKE2
444 BIO_puts(bio_out, "BLAKE2\n");
446 #ifdef OPENSSL_NO_CAMELLIA
447 BIO_puts(bio_out, "CAMELLIA\n");
449 #ifdef OPENSSL_NO_CAST
450 BIO_puts(bio_out, "CAST\n");
452 #ifdef OPENSSL_NO_CMAC
453 BIO_puts(bio_out, "CMAC\n");
455 #ifdef OPENSSL_NO_CMS
456 BIO_puts(bio_out, "CMS\n");
458 #ifdef OPENSSL_NO_COMP
459 BIO_puts(bio_out, "COMP\n");
461 #ifdef OPENSSL_NO_DES
462 BIO_puts(bio_out, "DES\n");
464 #ifdef OPENSSL_NO_DGRAM
465 BIO_puts(bio_out, "DGRAM\n");
468 BIO_puts(bio_out, "DH\n");
470 #ifdef OPENSSL_NO_DSA
471 BIO_puts(bio_out, "DSA\n");
473 #if defined(OPENSSL_NO_DTLS)
474 BIO_puts(bio_out, "DTLS\n");
476 #if defined(OPENSSL_NO_DTLS1)
477 BIO_puts(bio_out, "DTLS1\n");
479 #if defined(OPENSSL_NO_DTLS1_2)
480 BIO_puts(bio_out, "DTLS1_2\n");
483 BIO_puts(bio_out, "EC\n");
485 #ifdef OPENSSL_NO_EC2M
486 BIO_puts(bio_out, "EC2M\n");
488 #ifdef OPENSSL_NO_ENGINE
489 BIO_puts(bio_out, "ENGINE\n");
491 #ifdef OPENSSL_NO_GOST
492 BIO_puts(bio_out, "GOST\n");
494 #ifdef OPENSSL_NO_IDEA
495 BIO_puts(bio_out, "IDEA\n");
497 #ifdef OPENSSL_NO_MD2
498 BIO_puts(bio_out, "MD2\n");
500 #ifdef OPENSSL_NO_MD4
501 BIO_puts(bio_out, "MD4\n");
503 #ifdef OPENSSL_NO_MD5
504 BIO_puts(bio_out, "MD5\n");
506 #ifdef OPENSSL_NO_MDC2
507 BIO_puts(bio_out, "MDC2\n");
509 #ifdef OPENSSL_NO_OCB
510 BIO_puts(bio_out, "OCB\n");
512 #ifdef OPENSSL_NO_OCSP
513 BIO_puts(bio_out, "OCSP\n");
515 #ifdef OPENSSL_NO_PSK
516 BIO_puts(bio_out, "PSK\n");
518 #ifdef OPENSSL_NO_RC2
519 BIO_puts(bio_out, "RC2\n");
521 #ifdef OPENSSL_NO_RC4
522 BIO_puts(bio_out, "RC4\n");
524 #ifdef OPENSSL_NO_RC5
525 BIO_puts(bio_out, "RC5\n");
527 #ifdef OPENSSL_NO_RMD160
528 BIO_puts(bio_out, "RMD160\n");
530 #ifdef OPENSSL_NO_RSA
531 BIO_puts(bio_out, "RSA\n");
533 #ifdef OPENSSL_NO_SCRYPT
534 BIO_puts(bio_out, "SCRYPT\n");
536 #ifdef OPENSSL_NO_SCTP
537 BIO_puts(bio_out, "SCTP\n");
539 #ifdef OPENSSL_NO_SEED
540 BIO_puts(bio_out, "SEED\n");
542 #ifdef OPENSSL_NO_SM2
543 BIO_puts(bio_out, "SM2\n");
545 #ifdef OPENSSL_NO_SM3
546 BIO_puts(bio_out, "SM3\n");
548 #ifdef OPENSSL_NO_SM4
549 BIO_puts(bio_out, "SM4\n");
551 #ifdef OPENSSL_NO_SOCK
552 BIO_puts(bio_out, "SOCK\n");
554 #ifdef OPENSSL_NO_SRP
555 BIO_puts(bio_out, "SRP\n");
557 #ifdef OPENSSL_NO_SRTP
558 BIO_puts(bio_out, "SRTP\n");
560 #ifdef OPENSSL_NO_SSL3
561 BIO_puts(bio_out, "SSL3\n");
563 #ifdef OPENSSL_NO_TLS1
564 BIO_puts(bio_out, "TLS1\n");
566 #ifdef OPENSSL_NO_TLS1_1
567 BIO_puts(bio_out, "TLS1_1\n");
569 #ifdef OPENSSL_NO_TLS1_2
570 BIO_puts(bio_out, "TLS1_2\n");
572 #ifdef OPENSSL_NO_WHIRLPOOL
573 BIO_puts(bio_out, "WHIRLPOOL\n");
576 BIO_puts(bio_out, "ZLIB\n");
580 /* Unified enum for help and list commands. */
581 typedef enum HELPLIST_CHOICE {
582 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, OPT_ONE, OPT_VERBOSE,
583 OPT_COMMANDS, OPT_DIGEST_COMMANDS, OPT_MAC_ALGORITHMS, OPT_OPTIONS,
584 OPT_DIGEST_ALGORITHMS, OPT_CIPHER_COMMANDS, OPT_CIPHER_ALGORITHMS,
585 OPT_PK_ALGORITHMS, OPT_PK_METHOD, OPT_ENGINES, OPT_DISABLED,
586 OPT_KDF_ALGORITHMS, OPT_MISSING_HELP, OPT_OBJECTS
589 const OPTIONS list_options[] = {
590 {"help", OPT_HELP, '-', "Display this summary"},
591 {"1", OPT_ONE, '-', "List in one column"},
592 {"verbose", OPT_VERBOSE, '-', "Verbose listing"},
593 {"commands", OPT_COMMANDS, '-', "List of standard commands"},
594 {"digest-commands", OPT_DIGEST_COMMANDS, '-',
595 "List of message digest commands"},
596 {"digest-algorithms", OPT_DIGEST_ALGORITHMS, '-',
597 "List of message digest algorithms"},
598 {"kdf-algorithms", OPT_KDF_ALGORITHMS, '-',
599 "List of key derivation and pseudo random function algorithms"},
600 {"mac-algorithms", OPT_MAC_ALGORITHMS, '-',
601 "List of message authentication code algorithms"},
602 {"cipher-commands", OPT_CIPHER_COMMANDS, '-', "List of cipher commands"},
603 {"cipher-algorithms", OPT_CIPHER_ALGORITHMS, '-',
604 "List of cipher algorithms"},
605 {"public-key-algorithms", OPT_PK_ALGORITHMS, '-',
606 "List of public key algorithms"},
607 {"public-key-methods", OPT_PK_METHOD, '-',
608 "List of public key methods"},
609 {"engines", OPT_ENGINES, '-',
610 "List of loaded engines"},
611 {"disabled", OPT_DISABLED, '-',
612 "List of disabled features"},
613 {"missing-help", OPT_MISSING_HELP, '-',
614 "List missing detailed help strings"},
615 {"options", OPT_OPTIONS, 's',
616 "List options for specified command"},
617 {"objects", OPT_OBJECTS, '-',
618 "List built in objects (OID<->name mappings)"},
622 int list_main(int argc, char **argv)
626 int one = 0, done = 0;
628 unsigned int commands:1;
629 unsigned int digest_commands:1;
630 unsigned int digest_algorithms:1;
631 unsigned int kdf_algorithms:1;
632 unsigned int mac_algorithms:1;
633 unsigned int cipher_commands:1;
634 unsigned int cipher_algorithms:1;
635 unsigned int pk_algorithms:1;
636 unsigned int pk_method:1;
637 unsigned int engines:1;
638 unsigned int disabled:1;
639 unsigned int missing_help:1;
640 unsigned int objects:1;
641 unsigned int options:1;
644 verbose = 0; /* Clear a possible previous call */
646 prog = opt_init(argc, argv, list_options);
647 while ((o = opt_next()) != OPT_EOF) {
649 case OPT_EOF: /* Never hit, but suppresses warning */
652 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
655 opt_help(list_options);
663 case OPT_DIGEST_COMMANDS:
664 todo.digest_commands = 1;
666 case OPT_DIGEST_ALGORITHMS:
667 todo.digest_algorithms = 1;
669 case OPT_KDF_ALGORITHMS:
670 todo.kdf_algorithms = 1;
672 case OPT_MAC_ALGORITHMS:
673 todo.mac_algorithms = 1;
675 case OPT_CIPHER_COMMANDS:
676 todo.cipher_commands = 1;
678 case OPT_CIPHER_ALGORITHMS:
679 todo.cipher_algorithms = 1;
681 case OPT_PK_ALGORITHMS:
682 todo.pk_algorithms = 1;
693 case OPT_MISSING_HELP:
694 todo.missing_help = 1;
700 list_options_for_command(opt_arg());
708 if (opt_num_rest() != 0) {
709 BIO_printf(bio_err, "Extra arguments given.\n");
714 list_type(FT_general, one);
715 if (todo.digest_commands)
716 list_type(FT_md, one);
717 if (todo.digest_algorithms)
719 if (todo.kdf_algorithms)
721 if (todo.mac_algorithms)
723 if (todo.cipher_commands)
724 list_type(FT_cipher, one);
725 if (todo.cipher_algorithms)
727 if (todo.pk_algorithms)
735 if (todo.missing_help)