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>
16 #include "app_params.h"
20 static int verbose = 0;
22 static void legacy_cipher_fn(const EVP_CIPHER *c,
23 const char *from, const char *to, void *arg)
26 BIO_printf(arg, " %s\n", EVP_CIPHER_name(c));
32 BIO_printf(arg, " %s => %s\n", from, to);
36 DEFINE_STACK_OF(EVP_CIPHER)
37 static int cipher_cmp(const EVP_CIPHER * const *a,
38 const EVP_CIPHER * const *b)
40 int ret = strcasecmp(EVP_CIPHER_name(*a), EVP_CIPHER_name(*b));
43 ret = strcmp(OSSL_PROVIDER_name(EVP_CIPHER_provider(*a)),
44 OSSL_PROVIDER_name(EVP_CIPHER_provider(*b)));
49 static void collect_ciphers(EVP_CIPHER *cipher, void *stack)
51 STACK_OF(EVP_CIPHER) *cipher_stack = stack;
53 if (sk_EVP_CIPHER_push(cipher_stack, cipher) > 0)
54 EVP_CIPHER_up_ref(cipher);
57 static void list_ciphers(void)
59 STACK_OF(EVP_CIPHER) *ciphers = sk_EVP_CIPHER_new(cipher_cmp);
62 BIO_printf(bio_out, "Legacy:\n");
63 EVP_CIPHER_do_all_sorted(legacy_cipher_fn, bio_out);
65 BIO_printf(bio_out, "Provided:\n");
66 EVP_CIPHER_do_all_ex(NULL, collect_ciphers, ciphers);
67 sk_EVP_CIPHER_sort(ciphers);
68 for (i = 0; i < sk_EVP_CIPHER_num(ciphers); i++) {
69 const EVP_CIPHER *c = sk_EVP_CIPHER_value(ciphers, i);
71 BIO_printf(bio_out, " %s", EVP_CIPHER_name(c));
72 BIO_printf(bio_out, " @ %s\n",
73 OSSL_PROVIDER_name(EVP_CIPHER_provider(c)));
75 print_param_types("retrievable algorithm parameters",
76 EVP_CIPHER_gettable_params(c), 4);
77 print_param_types("retrievable operation parameters",
78 EVP_CIPHER_CTX_gettable_params(c), 4);
79 print_param_types("settable operation parameters",
80 EVP_CIPHER_CTX_settable_params(c), 4);
83 sk_EVP_CIPHER_pop_free(ciphers, EVP_CIPHER_free);
86 static void list_md_fn(const EVP_MD *m,
87 const char *from, const char *to, void *arg)
90 BIO_printf(arg, " %s\n", EVP_MD_name(m));
96 BIO_printf((BIO *)arg, " %s => %s\n", from, to);
100 DEFINE_STACK_OF(EVP_MD)
101 static int md_cmp(const EVP_MD * const *a, const EVP_MD * const *b)
103 int ret = strcasecmp(EVP_MD_name(*a), EVP_MD_name(*b));
106 ret = strcmp(OSSL_PROVIDER_name(EVP_MD_provider(*a)),
107 OSSL_PROVIDER_name(EVP_MD_provider(*b)));
112 static void collect_digests(EVP_MD *md, void *stack)
114 STACK_OF(EVP_MD) *digest_stack = stack;
116 if (sk_EVP_MD_push(digest_stack, md) > 0)
120 static void list_digests(void)
122 STACK_OF(EVP_MD) *digests = sk_EVP_MD_new(md_cmp);
125 BIO_printf(bio_out, "Legacy:\n");
126 EVP_MD_do_all_sorted(list_md_fn, bio_out);
128 BIO_printf(bio_out, "Provided:\n");
129 EVP_MD_do_all_ex(NULL, collect_digests, digests);
130 sk_EVP_MD_sort(digests);
131 for (i = 0; i < sk_EVP_MD_num(digests); i++) {
132 const EVP_MD *m = sk_EVP_MD_value(digests, i);
134 BIO_printf(bio_out, " %s", EVP_MD_name(m));
135 BIO_printf(bio_out, " @ %s\n",
136 OSSL_PROVIDER_name(EVP_MD_provider(m)));
138 print_param_types("retrievable algorithm parameters",
139 EVP_MD_gettable_params(m), 4);
140 print_param_types("retrievable operation parameters",
141 EVP_MD_CTX_gettable_params(m), 4);
142 print_param_types("settable operation parameters",
143 EVP_MD_CTX_settable_params(m), 4);
146 sk_EVP_MD_pop_free(digests, EVP_MD_free);
149 DEFINE_STACK_OF(EVP_MAC)
150 static int mac_cmp(const EVP_MAC * const *a, const EVP_MAC * const *b)
152 int ret = strcasecmp(EVP_MAC_name(*a), EVP_MAC_name(*b));
155 ret = strcmp(OSSL_PROVIDER_name(EVP_MAC_provider(*a)),
156 OSSL_PROVIDER_name(EVP_MAC_provider(*b)));
161 static void collect_macs(EVP_MAC *mac, void *stack)
163 STACK_OF(EVP_MAC) *mac_stack = stack;
165 if (sk_EVP_MAC_push(mac_stack, mac) > 0)
169 static void list_macs(void)
171 STACK_OF(EVP_MAC) *macs = sk_EVP_MAC_new(mac_cmp);
174 BIO_printf(bio_out, "Provided MACs:\n");
175 EVP_MAC_do_all_ex(NULL, collect_macs, macs);
176 sk_EVP_MAC_sort(macs);
177 for (i = 0; i < sk_EVP_MAC_num(macs); i++) {
178 const EVP_MAC *m = sk_EVP_MAC_value(macs, i);
180 BIO_printf(bio_out, " %s", EVP_MAC_name(m));
181 BIO_printf(bio_out, " @ %s\n",
182 OSSL_PROVIDER_name(EVP_MAC_provider(m)));
185 print_param_types("retrievable algorithm parameters",
186 EVP_MAC_gettable_params(m), 4);
187 print_param_types("retrievable operation parameters",
188 EVP_MAC_CTX_gettable_params(m), 4);
189 print_param_types("settable operation parameters",
190 EVP_MAC_CTX_settable_params(m), 4);
193 sk_EVP_MAC_pop_free(macs, EVP_MAC_free);
196 static void list_missing_help(void)
201 for (fp = functions; fp->name != NULL; fp++) {
202 if ((o = fp->help) != NULL) {
203 /* If there is help, list what flags are not documented. */
204 for ( ; o->name != NULL; o++) {
205 if (o->helpstr == NULL)
206 BIO_printf(bio_out, "%s %s\n", fp->name, o->name);
208 } else if (fp->func != dgst_main) {
209 /* If not aliased to the dgst command, */
210 BIO_printf(bio_out, "%s *\n", fp->name);
215 static void list_objects(void)
217 int max_nid = OBJ_new_nid(0);
219 char *oid_buf = NULL;
222 /* Skip 0, since that's NID_undef */
223 for (i = 1; i < max_nid; i++) {
224 const ASN1_OBJECT *obj = OBJ_nid2obj(i);
225 const char *sn = OBJ_nid2sn(i);
226 const char *ln = OBJ_nid2ln(i);
230 * If one of the retrieved objects somehow generated an error,
231 * we ignore it. The check for NID_undef below will detect the
232 * error and simply skip to the next NID.
236 if (OBJ_obj2nid(obj) == NID_undef)
239 if ((n = OBJ_obj2txt(NULL, 0, obj, 1)) == 0) {
240 BIO_printf(bio_out, "# None-OID object: %s, %s\n", sn, ln);
247 oid_buf = OPENSSL_realloc(oid_buf, n + 1);
248 if (oid_buf == NULL) {
249 BIO_printf(bio_err, "ERROR: Memory allocation\n");
254 if (OBJ_obj2txt(oid_buf, oid_size, obj, 1) < 0)
256 if (ln == NULL || strcmp(sn, ln) == 0)
257 BIO_printf(bio_out, "%s = %s\n", sn, oid_buf);
259 BIO_printf(bio_out, "%s = %s, %s\n", sn, ln, oid_buf);
262 OPENSSL_free(oid_buf);
265 static void list_options_for_command(const char *command)
270 for (fp = functions; fp->name != NULL; fp++)
271 if (strcmp(fp->name, command) == 0)
273 if (fp->name == NULL) {
274 BIO_printf(bio_err, "Invalid command '%s'; type \"help\" for a list.\n",
279 if ((o = fp->help) == NULL)
282 for ( ; o->name != NULL; o++) {
283 if (o->name == OPT_HELP_STR
284 || o->name == OPT_MORE_STR
285 || o->name[0] == '\0')
287 BIO_printf(bio_out, "%s %c\n", o->name, o->valtype);
291 static void list_type(FUNC_TYPE ft, int one)
297 memset(&dc, 0, sizeof(dc));
299 calculate_columns(functions, &dc);
301 for (fp = functions; fp->name != NULL; fp++) {
305 BIO_printf(bio_out, "%s\n", fp->name);
307 if (i % dc.columns == 0 && i > 0)
308 BIO_printf(bio_out, "\n");
309 BIO_printf(bio_out, "%-*s", dc.width, fp->name);
314 BIO_printf(bio_out, "\n\n");
317 static void list_pkey(void)
321 for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
322 const EVP_PKEY_ASN1_METHOD *ameth;
323 int pkey_id, pkey_base_id, pkey_flags;
324 const char *pinfo, *pem_str;
325 ameth = EVP_PKEY_asn1_get0(i);
326 EVP_PKEY_asn1_get0_info(&pkey_id, &pkey_base_id, &pkey_flags,
327 &pinfo, &pem_str, ameth);
328 if (pkey_flags & ASN1_PKEY_ALIAS) {
329 BIO_printf(bio_out, "Name: %s\n", OBJ_nid2ln(pkey_id));
330 BIO_printf(bio_out, "\tAlias for: %s\n",
331 OBJ_nid2ln(pkey_base_id));
333 BIO_printf(bio_out, "Name: %s\n", pinfo);
334 BIO_printf(bio_out, "\tType: %s Algorithm\n",
335 pkey_flags & ASN1_PKEY_DYNAMIC ?
336 "External" : "Builtin");
337 BIO_printf(bio_out, "\tOID: %s\n", OBJ_nid2ln(pkey_id));
340 BIO_printf(bio_out, "\tPEM string: %s\n", pem_str);
346 static void list_pkey_meth(void)
349 size_t meth_count = EVP_PKEY_meth_get_count();
351 for (i = 0; i < meth_count; i++) {
352 const EVP_PKEY_METHOD *pmeth = EVP_PKEY_meth_get0(i);
353 int pkey_id, pkey_flags;
355 EVP_PKEY_meth_get0_info(&pkey_id, &pkey_flags, pmeth);
356 BIO_printf(bio_out, "%s\n", OBJ_nid2ln(pkey_id));
357 BIO_printf(bio_out, "\tType: %s Algorithm\n",
358 pkey_flags & ASN1_PKEY_DYNAMIC ? "External" : "Builtin");
362 static void list_engines(void)
364 #ifndef OPENSSL_NO_ENGINE
367 BIO_puts(bio_out, "Engines:\n");
368 e = ENGINE_get_first();
370 BIO_printf(bio_out, "%s\n", ENGINE_get_id(e));
371 e = ENGINE_get_next(e);
374 BIO_puts(bio_out, "Engine support is disabled.\n");
378 static void list_disabled(void)
380 BIO_puts(bio_out, "Disabled algorithms:\n");
381 #ifdef OPENSSL_NO_ARIA
382 BIO_puts(bio_out, "ARIA\n");
385 BIO_puts(bio_out, "BF\n");
387 #ifdef OPENSSL_NO_BLAKE2
388 BIO_puts(bio_out, "BLAKE2\n");
390 #ifdef OPENSSL_NO_CAMELLIA
391 BIO_puts(bio_out, "CAMELLIA\n");
393 #ifdef OPENSSL_NO_CAST
394 BIO_puts(bio_out, "CAST\n");
396 #ifdef OPENSSL_NO_CMAC
397 BIO_puts(bio_out, "CMAC\n");
399 #ifdef OPENSSL_NO_CMS
400 BIO_puts(bio_out, "CMS\n");
402 #ifdef OPENSSL_NO_COMP
403 BIO_puts(bio_out, "COMP\n");
405 #ifdef OPENSSL_NO_DES
406 BIO_puts(bio_out, "DES\n");
408 #ifdef OPENSSL_NO_DGRAM
409 BIO_puts(bio_out, "DGRAM\n");
412 BIO_puts(bio_out, "DH\n");
414 #ifdef OPENSSL_NO_DSA
415 BIO_puts(bio_out, "DSA\n");
417 #if defined(OPENSSL_NO_DTLS)
418 BIO_puts(bio_out, "DTLS\n");
420 #if defined(OPENSSL_NO_DTLS1)
421 BIO_puts(bio_out, "DTLS1\n");
423 #if defined(OPENSSL_NO_DTLS1_2)
424 BIO_puts(bio_out, "DTLS1_2\n");
427 BIO_puts(bio_out, "EC\n");
429 #ifdef OPENSSL_NO_EC2M
430 BIO_puts(bio_out, "EC2M\n");
432 #ifdef OPENSSL_NO_ENGINE
433 BIO_puts(bio_out, "ENGINE\n");
435 #ifdef OPENSSL_NO_GOST
436 BIO_puts(bio_out, "GOST\n");
438 #ifdef OPENSSL_NO_IDEA
439 BIO_puts(bio_out, "IDEA\n");
441 #ifdef OPENSSL_NO_MD2
442 BIO_puts(bio_out, "MD2\n");
444 #ifdef OPENSSL_NO_MD4
445 BIO_puts(bio_out, "MD4\n");
447 #ifdef OPENSSL_NO_MD5
448 BIO_puts(bio_out, "MD5\n");
450 #ifdef OPENSSL_NO_MDC2
451 BIO_puts(bio_out, "MDC2\n");
453 #ifdef OPENSSL_NO_OCB
454 BIO_puts(bio_out, "OCB\n");
456 #ifdef OPENSSL_NO_OCSP
457 BIO_puts(bio_out, "OCSP\n");
459 #ifdef OPENSSL_NO_PSK
460 BIO_puts(bio_out, "PSK\n");
462 #ifdef OPENSSL_NO_RC2
463 BIO_puts(bio_out, "RC2\n");
465 #ifdef OPENSSL_NO_RC4
466 BIO_puts(bio_out, "RC4\n");
468 #ifdef OPENSSL_NO_RC5
469 BIO_puts(bio_out, "RC5\n");
471 #ifdef OPENSSL_NO_RMD160
472 BIO_puts(bio_out, "RMD160\n");
474 #ifdef OPENSSL_NO_RSA
475 BIO_puts(bio_out, "RSA\n");
477 #ifdef OPENSSL_NO_SCRYPT
478 BIO_puts(bio_out, "SCRYPT\n");
480 #ifdef OPENSSL_NO_SCTP
481 BIO_puts(bio_out, "SCTP\n");
483 #ifdef OPENSSL_NO_SEED
484 BIO_puts(bio_out, "SEED\n");
486 #ifdef OPENSSL_NO_SM2
487 BIO_puts(bio_out, "SM2\n");
489 #ifdef OPENSSL_NO_SM3
490 BIO_puts(bio_out, "SM3\n");
492 #ifdef OPENSSL_NO_SM4
493 BIO_puts(bio_out, "SM4\n");
495 #ifdef OPENSSL_NO_SOCK
496 BIO_puts(bio_out, "SOCK\n");
498 #ifdef OPENSSL_NO_SRP
499 BIO_puts(bio_out, "SRP\n");
501 #ifdef OPENSSL_NO_SRTP
502 BIO_puts(bio_out, "SRTP\n");
504 #ifdef OPENSSL_NO_SSL3
505 BIO_puts(bio_out, "SSL3\n");
507 #ifdef OPENSSL_NO_TLS1
508 BIO_puts(bio_out, "TLS1\n");
510 #ifdef OPENSSL_NO_TLS1_1
511 BIO_puts(bio_out, "TLS1_1\n");
513 #ifdef OPENSSL_NO_TLS1_2
514 BIO_puts(bio_out, "TLS1_2\n");
516 #ifdef OPENSSL_NO_WHIRLPOOL
517 BIO_puts(bio_out, "WHIRLPOOL\n");
520 BIO_puts(bio_out, "ZLIB\n");
524 /* Unified enum for help and list commands. */
525 typedef enum HELPLIST_CHOICE {
526 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, OPT_ONE, OPT_VERBOSE,
527 OPT_COMMANDS, OPT_DIGEST_COMMANDS, OPT_MAC_ALGORITHMS, OPT_OPTIONS,
528 OPT_DIGEST_ALGORITHMS, OPT_CIPHER_COMMANDS, OPT_CIPHER_ALGORITHMS,
529 OPT_PK_ALGORITHMS, OPT_PK_METHOD, OPT_ENGINES, OPT_DISABLED,
530 OPT_MISSING_HELP, OPT_OBJECTS
533 const OPTIONS list_options[] = {
534 {"help", OPT_HELP, '-', "Display this summary"},
535 {"1", OPT_ONE, '-', "List in one column"},
536 {"verbose", OPT_VERBOSE, '-', "Verbose listing"},
537 {"commands", OPT_COMMANDS, '-', "List of standard commands"},
538 {"digest-commands", OPT_DIGEST_COMMANDS, '-',
539 "List of message digest commands"},
540 {"digest-algorithms", OPT_DIGEST_ALGORITHMS, '-',
541 "List of message digest algorithms"},
542 {"mac-algorithms", OPT_MAC_ALGORITHMS, '-',
543 "List of message authentication code algorithms"},
544 {"cipher-commands", OPT_CIPHER_COMMANDS, '-', "List of cipher commands"},
545 {"cipher-algorithms", OPT_CIPHER_ALGORITHMS, '-',
546 "List of cipher algorithms"},
547 {"public-key-algorithms", OPT_PK_ALGORITHMS, '-',
548 "List of public key algorithms"},
549 {"public-key-methods", OPT_PK_METHOD, '-',
550 "List of public key methods"},
551 {"engines", OPT_ENGINES, '-',
552 "List of loaded engines"},
553 {"disabled", OPT_DISABLED, '-',
554 "List of disabled features"},
555 {"missing-help", OPT_MISSING_HELP, '-',
556 "List missing detailed help strings"},
557 {"options", OPT_OPTIONS, 's',
558 "List options for specified command"},
559 {"objects", OPT_OBJECTS, '-',
560 "List built in objects (OID<->name mappings)"},
564 int list_main(int argc, char **argv)
568 int one = 0, done = 0;
570 unsigned int commands:1;
571 unsigned int digest_commands:1;
572 unsigned int digest_algorithms:1;
573 unsigned int mac_algorithms:1;
574 unsigned int cipher_commands:1;
575 unsigned int cipher_algorithms:1;
576 unsigned int pk_algorithms:1;
577 unsigned int pk_method:1;
578 unsigned int engines:1;
579 unsigned int disabled:1;
580 unsigned int missing_help:1;
581 unsigned int objects:1;
582 unsigned int options:1;
585 verbose = 0; /* Clear a possible previous call */
587 prog = opt_init(argc, argv, list_options);
588 while ((o = opt_next()) != OPT_EOF) {
590 case OPT_EOF: /* Never hit, but suppresses warning */
593 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
596 opt_help(list_options);
604 case OPT_DIGEST_COMMANDS:
605 todo.digest_commands = 1;
607 case OPT_DIGEST_ALGORITHMS:
608 todo.digest_algorithms = 1;
610 case OPT_MAC_ALGORITHMS:
611 todo.mac_algorithms = 1;
613 case OPT_CIPHER_COMMANDS:
614 todo.cipher_commands = 1;
616 case OPT_CIPHER_ALGORITHMS:
617 todo.cipher_algorithms = 1;
619 case OPT_PK_ALGORITHMS:
620 todo.pk_algorithms = 1;
631 case OPT_MISSING_HELP:
632 todo.missing_help = 1;
638 list_options_for_command(opt_arg());
646 if (opt_num_rest() != 0) {
647 BIO_printf(bio_err, "Extra arguments given.\n");
652 list_type(FT_general, one);
653 if (todo.digest_commands)
654 list_type(FT_md, one);
655 if (todo.digest_algorithms)
657 if (todo.mac_algorithms)
659 if (todo.cipher_commands)
660 list_type(FT_cipher, one);
661 if (todo.cipher_algorithms)
663 if (todo.pk_algorithms)
671 if (todo.missing_help)