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>
19 static int verbose = 0;
21 static int describe_param_type(char *buf, size_t bufsz, const OSSL_PARAM *param)
23 const char *type_mod = "";
24 const char *type = NULL;
25 int show_type_number = 0;
28 switch (param->data_type) {
29 case OSSL_PARAM_UNSIGNED_INTEGER:
30 type_mod = "unsigned ";
32 case OSSL_PARAM_INTEGER:
35 case OSSL_PARAM_UTF8_PTR:
36 type_mod = "pointer to a ";
38 case OSSL_PARAM_UTF8_STRING:
39 type = "UTF8 encoded string";
41 case OSSL_PARAM_OCTET_PTR:
42 type_mod = "pointer to an ";
44 case OSSL_PARAM_OCTET_STRING:
45 type = "octet string";
48 type = "unknown type";
53 printed_len = BIO_snprintf(buf, bufsz, "%s: ", param->key);
54 if (printed_len > 0) {
58 printed_len = BIO_snprintf(buf, bufsz, "%s%s", type_mod, type);
59 if (printed_len > 0) {
63 if (show_type_number) {
64 printed_len = BIO_snprintf(buf, bufsz, " [%d]", param->data_type);
65 if (printed_len > 0) {
70 if (param->data_size == 0)
71 printed_len = BIO_snprintf(buf, bufsz, " (arbitrary size)");
73 printed_len = BIO_snprintf(buf, bufsz, " (max %zu bytes large)",
75 if (printed_len > 0) {
83 static int print_param_types(const char *thing, const OSSL_PARAM *pdefs)
86 BIO_printf(bio_out, " No declared %s\n", thing);
87 } else if (pdefs->key == NULL) {
89 * An empty list? This shouldn't happen, but let's just make sure to
90 * say something if there's a badly written provider...
92 BIO_printf(bio_out, " Empty list of %s (!!!)\n", thing);
94 BIO_printf(bio_out, " %s:\n", thing);
95 for (; pdefs->key != NULL; pdefs++) {
96 char buf[200]; /* This should be ample space */
98 describe_param_type(buf, sizeof(buf), pdefs);
99 BIO_printf(bio_out, " %s\n", buf);
105 static void legacy_cipher_fn(const EVP_CIPHER *c,
106 const char *from, const char *to, void *arg)
109 BIO_printf(arg, " %s\n", EVP_CIPHER_name(c));
112 from = "<undefined>";
115 BIO_printf(arg, " %s => %s\n", from, to);
119 DEFINE_STACK_OF(EVP_CIPHER)
120 static int cipher_cmp(const EVP_CIPHER * const *a,
121 const EVP_CIPHER * const *b)
123 int ret = strcasecmp(EVP_CIPHER_name(*a), EVP_CIPHER_name(*b));
126 ret = strcmp(OSSL_PROVIDER_name(EVP_CIPHER_provider(*a)),
127 OSSL_PROVIDER_name(EVP_CIPHER_provider(*b)));
132 static void collect_ciphers(EVP_CIPHER *cipher, void *stack)
134 STACK_OF(EVP_CIPHER) *cipher_stack = stack;
136 sk_EVP_CIPHER_push(cipher_stack, cipher);
137 EVP_CIPHER_up_ref(cipher);
140 static void list_ciphers(void)
142 STACK_OF(EVP_CIPHER) *ciphers = sk_EVP_CIPHER_new(cipher_cmp);
145 BIO_printf(bio_out, "Legacy:\n");
146 EVP_CIPHER_do_all_sorted(legacy_cipher_fn, bio_out);
148 BIO_printf(bio_out, "Provided:\n");
149 EVP_CIPHER_do_all_ex(NULL, collect_ciphers, ciphers);
150 sk_EVP_CIPHER_sort(ciphers);
151 for (i = 0; i < sk_EVP_CIPHER_num(ciphers); i++) {
152 const EVP_CIPHER *c = sk_EVP_CIPHER_value(ciphers, i);
154 BIO_printf(bio_out, " %s", EVP_CIPHER_name(c));
155 BIO_printf(bio_out, " @ %s\n",
156 OSSL_PROVIDER_name(EVP_CIPHER_provider(c)));
158 print_param_types("retrievable algorithm parameters",
159 EVP_CIPHER_gettable_params(c));
160 print_param_types("retrievable operation parameters",
161 EVP_CIPHER_CTX_gettable_params(c));
162 print_param_types("settable operation parameters",
163 EVP_CIPHER_CTX_settable_params(c));
166 sk_EVP_CIPHER_pop_free(ciphers, EVP_CIPHER_meth_free);
169 static void list_md_fn(const EVP_MD *m,
170 const char *from, const char *to, void *arg)
173 BIO_printf(arg, " %s\n", EVP_MD_name(m));
176 from = "<undefined>";
179 BIO_printf((BIO *)arg, " %s => %s\n", from, to);
183 DEFINE_STACK_OF(EVP_MD)
184 static int md_cmp(const EVP_MD * const *a, const EVP_MD * const *b)
186 int ret = strcasecmp(EVP_MD_name(*a), EVP_MD_name(*b));
189 ret = strcmp(OSSL_PROVIDER_name(EVP_MD_provider(*a)),
190 OSSL_PROVIDER_name(EVP_MD_provider(*b)));
195 static void collect_digests(EVP_MD *md, void *stack)
197 STACK_OF(EVP_MD) *digest_stack = stack;
199 sk_EVP_MD_push(digest_stack, md);
203 static void list_digests(void)
205 STACK_OF(EVP_MD) *digests = sk_EVP_MD_new(md_cmp);
208 BIO_printf(bio_out, "Legacy:\n");
209 EVP_MD_do_all_sorted(list_md_fn, bio_out);
211 BIO_printf(bio_out, "Provided:\n");
212 EVP_MD_do_all_ex(NULL, collect_digests, digests);
213 sk_EVP_MD_sort(digests);
214 for (i = 0; i < sk_EVP_MD_num(digests); i++) {
215 const EVP_MD *m = sk_EVP_MD_value(digests, i);
217 BIO_printf(bio_out, " %s", EVP_MD_name(m));
218 BIO_printf(bio_out, " @ %s\n",
219 OSSL_PROVIDER_name(EVP_MD_provider(m)));
221 print_param_types("retrievable algorithm parameters",
222 EVP_MD_gettable_params(m));
223 print_param_types("retrievable operation parameters",
224 EVP_MD_CTX_gettable_params(m));
225 print_param_types("settable operation parameters",
226 EVP_MD_CTX_settable_params(m));
229 sk_EVP_MD_pop_free(digests, EVP_MD_meth_free);
232 static void list_mac_fn(const EVP_MAC *m,
233 const char *from, const char *to, void *arg)
236 BIO_printf(arg, "%s\n", EVP_MAC_name(m));
239 from = "<undefined>";
242 BIO_printf(arg, "%s => %s\n", from, to);
246 static void list_missing_help(void)
251 for (fp = functions; fp->name != NULL; fp++) {
252 if ((o = fp->help) != NULL) {
253 /* If there is help, list what flags are not documented. */
254 for ( ; o->name != NULL; o++) {
255 if (o->helpstr == NULL)
256 BIO_printf(bio_out, "%s %s\n", fp->name, o->name);
258 } else if (fp->func != dgst_main) {
259 /* If not aliased to the dgst command, */
260 BIO_printf(bio_out, "%s *\n", fp->name);
265 static void list_objects(void)
267 int max_nid = OBJ_new_nid(0);
269 char *oid_buf = NULL;
272 /* Skip 0, since that's NID_undef */
273 for (i = 1; i < max_nid; i++) {
274 const ASN1_OBJECT *obj = OBJ_nid2obj(i);
275 const char *sn = OBJ_nid2sn(i);
276 const char *ln = OBJ_nid2ln(i);
280 * If one of the retrieved objects somehow generated an error,
281 * we ignore it. The check for NID_undef below will detect the
282 * error and simply skip to the next NID.
286 if (OBJ_obj2nid(obj) == NID_undef)
289 if ((n = OBJ_obj2txt(NULL, 0, obj, 1)) == 0) {
290 BIO_printf(bio_out, "# None-OID object: %s, %s\n", sn, ln);
297 oid_buf = OPENSSL_realloc(oid_buf, n + 1);
298 if (oid_buf == NULL) {
299 BIO_printf(bio_err, "ERROR: Memory allocation\n");
304 if (OBJ_obj2txt(oid_buf, oid_size, obj, 1) < 0)
306 if (ln == NULL || strcmp(sn, ln) == 0)
307 BIO_printf(bio_out, "%s = %s\n", sn, oid_buf);
309 BIO_printf(bio_out, "%s = %s, %s\n", sn, ln, oid_buf);
312 OPENSSL_free(oid_buf);
315 static void list_options_for_command(const char *command)
320 for (fp = functions; fp->name != NULL; fp++)
321 if (strcmp(fp->name, command) == 0)
323 if (fp->name == NULL) {
324 BIO_printf(bio_err, "Invalid command '%s'; type \"help\" for a list.\n",
329 if ((o = fp->help) == NULL)
332 for ( ; o->name != NULL; o++) {
333 if (o->name == OPT_HELP_STR
334 || o->name == OPT_MORE_STR
335 || o->name[0] == '\0')
337 BIO_printf(bio_out, "%s %c\n", o->name, o->valtype);
341 static void list_type(FUNC_TYPE ft, int one)
347 memset(&dc, 0, sizeof(dc));
349 calculate_columns(functions, &dc);
351 for (fp = functions; fp->name != NULL; fp++) {
355 BIO_printf(bio_out, "%s\n", fp->name);
357 if (i % dc.columns == 0 && i > 0)
358 BIO_printf(bio_out, "\n");
359 BIO_printf(bio_out, "%-*s", dc.width, fp->name);
364 BIO_printf(bio_out, "\n\n");
367 static void list_pkey(void)
371 for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
372 const EVP_PKEY_ASN1_METHOD *ameth;
373 int pkey_id, pkey_base_id, pkey_flags;
374 const char *pinfo, *pem_str;
375 ameth = EVP_PKEY_asn1_get0(i);
376 EVP_PKEY_asn1_get0_info(&pkey_id, &pkey_base_id, &pkey_flags,
377 &pinfo, &pem_str, ameth);
378 if (pkey_flags & ASN1_PKEY_ALIAS) {
379 BIO_printf(bio_out, "Name: %s\n", OBJ_nid2ln(pkey_id));
380 BIO_printf(bio_out, "\tAlias for: %s\n",
381 OBJ_nid2ln(pkey_base_id));
383 BIO_printf(bio_out, "Name: %s\n", pinfo);
384 BIO_printf(bio_out, "\tType: %s Algorithm\n",
385 pkey_flags & ASN1_PKEY_DYNAMIC ?
386 "External" : "Builtin");
387 BIO_printf(bio_out, "\tOID: %s\n", OBJ_nid2ln(pkey_id));
390 BIO_printf(bio_out, "\tPEM string: %s\n", pem_str);
396 static void list_pkey_meth(void)
399 size_t meth_count = EVP_PKEY_meth_get_count();
401 for (i = 0; i < meth_count; i++) {
402 const EVP_PKEY_METHOD *pmeth = EVP_PKEY_meth_get0(i);
403 int pkey_id, pkey_flags;
405 EVP_PKEY_meth_get0_info(&pkey_id, &pkey_flags, pmeth);
406 BIO_printf(bio_out, "%s\n", OBJ_nid2ln(pkey_id));
407 BIO_printf(bio_out, "\tType: %s Algorithm\n",
408 pkey_flags & ASN1_PKEY_DYNAMIC ? "External" : "Builtin");
412 static void list_engines(void)
414 #ifndef OPENSSL_NO_ENGINE
417 BIO_puts(bio_out, "Engines:\n");
418 e = ENGINE_get_first();
420 BIO_printf(bio_out, "%s\n", ENGINE_get_id(e));
421 e = ENGINE_get_next(e);
424 BIO_puts(bio_out, "Engine support is disabled.\n");
428 static void list_disabled(void)
430 BIO_puts(bio_out, "Disabled algorithms:\n");
431 #ifdef OPENSSL_NO_ARIA
432 BIO_puts(bio_out, "ARIA\n");
435 BIO_puts(bio_out, "BF\n");
437 #ifdef OPENSSL_NO_BLAKE2
438 BIO_puts(bio_out, "BLAKE2\n");
440 #ifdef OPENSSL_NO_CAMELLIA
441 BIO_puts(bio_out, "CAMELLIA\n");
443 #ifdef OPENSSL_NO_CAST
444 BIO_puts(bio_out, "CAST\n");
446 #ifdef OPENSSL_NO_CMAC
447 BIO_puts(bio_out, "CMAC\n");
449 #ifdef OPENSSL_NO_CMS
450 BIO_puts(bio_out, "CMS\n");
452 #ifdef OPENSSL_NO_COMP
453 BIO_puts(bio_out, "COMP\n");
455 #ifdef OPENSSL_NO_DES
456 BIO_puts(bio_out, "DES\n");
458 #ifdef OPENSSL_NO_DGRAM
459 BIO_puts(bio_out, "DGRAM\n");
462 BIO_puts(bio_out, "DH\n");
464 #ifdef OPENSSL_NO_DSA
465 BIO_puts(bio_out, "DSA\n");
467 #if defined(OPENSSL_NO_DTLS)
468 BIO_puts(bio_out, "DTLS\n");
470 #if defined(OPENSSL_NO_DTLS1)
471 BIO_puts(bio_out, "DTLS1\n");
473 #if defined(OPENSSL_NO_DTLS1_2)
474 BIO_puts(bio_out, "DTLS1_2\n");
477 BIO_puts(bio_out, "EC\n");
479 #ifdef OPENSSL_NO_EC2M
480 BIO_puts(bio_out, "EC2M\n");
482 #ifdef OPENSSL_NO_ENGINE
483 BIO_puts(bio_out, "ENGINE\n");
485 #ifdef OPENSSL_NO_GOST
486 BIO_puts(bio_out, "GOST\n");
488 #ifdef OPENSSL_NO_IDEA
489 BIO_puts(bio_out, "IDEA\n");
491 #ifdef OPENSSL_NO_MD2
492 BIO_puts(bio_out, "MD2\n");
494 #ifdef OPENSSL_NO_MD4
495 BIO_puts(bio_out, "MD4\n");
497 #ifdef OPENSSL_NO_MD5
498 BIO_puts(bio_out, "MD5\n");
500 #ifdef OPENSSL_NO_MDC2
501 BIO_puts(bio_out, "MDC2\n");
503 #ifdef OPENSSL_NO_OCB
504 BIO_puts(bio_out, "OCB\n");
506 #ifdef OPENSSL_NO_OCSP
507 BIO_puts(bio_out, "OCSP\n");
509 #ifdef OPENSSL_NO_PSK
510 BIO_puts(bio_out, "PSK\n");
512 #ifdef OPENSSL_NO_RC2
513 BIO_puts(bio_out, "RC2\n");
515 #ifdef OPENSSL_NO_RC4
516 BIO_puts(bio_out, "RC4\n");
518 #ifdef OPENSSL_NO_RC5
519 BIO_puts(bio_out, "RC5\n");
521 #ifdef OPENSSL_NO_RMD160
522 BIO_puts(bio_out, "RMD160\n");
524 #ifdef OPENSSL_NO_RSA
525 BIO_puts(bio_out, "RSA\n");
527 #ifdef OPENSSL_NO_SCRYPT
528 BIO_puts(bio_out, "SCRYPT\n");
530 #ifdef OPENSSL_NO_SCTP
531 BIO_puts(bio_out, "SCTP\n");
533 #ifdef OPENSSL_NO_SEED
534 BIO_puts(bio_out, "SEED\n");
536 #ifdef OPENSSL_NO_SM2
537 BIO_puts(bio_out, "SM2\n");
539 #ifdef OPENSSL_NO_SM3
540 BIO_puts(bio_out, "SM3\n");
542 #ifdef OPENSSL_NO_SM4
543 BIO_puts(bio_out, "SM4\n");
545 #ifdef OPENSSL_NO_SOCK
546 BIO_puts(bio_out, "SOCK\n");
548 #ifdef OPENSSL_NO_SRP
549 BIO_puts(bio_out, "SRP\n");
551 #ifdef OPENSSL_NO_SRTP
552 BIO_puts(bio_out, "SRTP\n");
554 #ifdef OPENSSL_NO_SSL3
555 BIO_puts(bio_out, "SSL3\n");
557 #ifdef OPENSSL_NO_TLS1
558 BIO_puts(bio_out, "TLS1\n");
560 #ifdef OPENSSL_NO_TLS1_1
561 BIO_puts(bio_out, "TLS1_1\n");
563 #ifdef OPENSSL_NO_TLS1_2
564 BIO_puts(bio_out, "TLS1_2\n");
566 #ifdef OPENSSL_NO_WHIRLPOOL
567 BIO_puts(bio_out, "WHIRLPOOL\n");
570 BIO_puts(bio_out, "ZLIB\n");
574 /* Unified enum for help and list commands. */
575 typedef enum HELPLIST_CHOICE {
576 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, OPT_ONE, OPT_VERBOSE,
577 OPT_COMMANDS, OPT_DIGEST_COMMANDS, OPT_MAC_ALGORITHMS, OPT_OPTIONS,
578 OPT_DIGEST_ALGORITHMS, OPT_CIPHER_COMMANDS, OPT_CIPHER_ALGORITHMS,
579 OPT_PK_ALGORITHMS, OPT_PK_METHOD, OPT_ENGINES, OPT_DISABLED,
580 OPT_MISSING_HELP, OPT_OBJECTS
583 const OPTIONS list_options[] = {
584 {"help", OPT_HELP, '-', "Display this summary"},
585 {"1", OPT_ONE, '-', "List in one column"},
586 {"verbose", OPT_VERBOSE, '-', "Verbose listing"},
587 {"commands", OPT_COMMANDS, '-', "List of standard commands"},
588 {"digest-commands", OPT_DIGEST_COMMANDS, '-',
589 "List of message digest commands"},
590 {"digest-algorithms", OPT_DIGEST_ALGORITHMS, '-',
591 "List of message digest algorithms"},
592 {"mac-algorithms", OPT_MAC_ALGORITHMS, '-',
593 "List of message authentication code algorithms"},
594 {"cipher-commands", OPT_CIPHER_COMMANDS, '-', "List of cipher commands"},
595 {"cipher-algorithms", OPT_CIPHER_ALGORITHMS, '-',
596 "List of cipher algorithms"},
597 {"public-key-algorithms", OPT_PK_ALGORITHMS, '-',
598 "List of public key algorithms"},
599 {"public-key-methods", OPT_PK_METHOD, '-',
600 "List of public key methods"},
601 {"engines", OPT_ENGINES, '-',
602 "List of loaded engines"},
603 {"disabled", OPT_DISABLED, '-',
604 "List of disabled features"},
605 {"missing-help", OPT_MISSING_HELP, '-',
606 "List missing detailed help strings"},
607 {"options", OPT_OPTIONS, 's',
608 "List options for specified command"},
609 {"objects", OPT_OBJECTS, '-',
610 "List built in objects (OID<->name mappings)"},
614 int list_main(int argc, char **argv)
618 int one = 0, done = 0;
620 unsigned int commands:1;
621 unsigned int digest_commands:1;
622 unsigned int digest_algorithms:1;
623 unsigned int mac_algorithms:1;
624 unsigned int cipher_commands:1;
625 unsigned int cipher_algorithms:1;
626 unsigned int pk_algorithms:1;
627 unsigned int pk_method:1;
628 unsigned int engines:1;
629 unsigned int disabled:1;
630 unsigned int missing_help:1;
631 unsigned int objects:1;
632 unsigned int options:1;
635 verbose = 0; /* Clear a possible previous call */
637 prog = opt_init(argc, argv, list_options);
638 while ((o = opt_next()) != OPT_EOF) {
640 case OPT_EOF: /* Never hit, but suppresses warning */
643 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
646 opt_help(list_options);
654 case OPT_DIGEST_COMMANDS:
655 todo.digest_commands = 1;
657 case OPT_DIGEST_ALGORITHMS:
658 todo.digest_algorithms = 1;
660 case OPT_MAC_ALGORITHMS:
661 todo.mac_algorithms = 1;
663 case OPT_CIPHER_COMMANDS:
664 todo.cipher_commands = 1;
666 case OPT_CIPHER_ALGORITHMS:
667 todo.cipher_algorithms = 1;
669 case OPT_PK_ALGORITHMS:
670 todo.pk_algorithms = 1;
681 case OPT_MISSING_HELP:
682 todo.missing_help = 1;
688 list_options_for_command(opt_arg());
696 if (opt_num_rest() != 0) {
697 BIO_printf(bio_err, "Extra arguments given.\n");
702 list_type(FT_general, one);
703 if (todo.digest_commands)
704 list_type(FT_md, one);
705 if (todo.digest_algorithms)
707 if (todo.mac_algorithms)
708 EVP_MAC_do_all_sorted(list_mac_fn, bio_out);
709 if (todo.cipher_commands)
710 list_type(FT_cipher, one);
711 if (todo.cipher_algorithms)
713 if (todo.pk_algorithms)
721 if (todo.missing_help)